File Coverage

blib/lib/Business/Mondo/Transaction.pm
Criterion Covered Total %
statement 40 40 100.0
branch 2 2 100.0
condition n/a
subroutine 12 12 100.0
pod 2 4 50.0
total 56 58 96.5


line stmt bran cond sub pod time code
1             package Business::Mondo::Transaction;
2              
3             =head1 NAME
4              
5             Business::Mondo::Transaction
6              
7             =head1 DESCRIPTION
8              
9             A class for a Mondo transaction, extends L<Business::Mondo::Resource>
10              
11             =cut
12              
13 10     10   34 use strict;
  10         11  
  10         226  
14 10     10   26 use warnings;
  10         9  
  10         173  
15              
16 10     10   28 use Moo;
  10         10  
  10         35  
17             extends 'Business::Mondo::Resource';
18             with 'Business::Mondo::Utils';
19             with 'Business::Mondo::Currency';
20              
21 10     10   7019 use Types::Standard qw/ :all /;
  10         525957  
  10         85  
22 10     10   263671 use Business::Mondo::Merchant;
  10         28  
  10         317  
23 10     10   3590 use Business::Mondo::Attachment;
  10         26  
  10         300  
24 10     10   54 use DateTime::Format::DateParse;
  10         12  
  10         145  
25 10     10   5295 use Locale::Currency::Format;
  10         31388  
  10         5344  
26              
27             =head1 ATTRIBUTES
28              
29             The Transaction class has the following attributes (with their type).
30              
31             id (Str)
32             account_id (Str)
33             category (Str)
34             dedupe_id (Str)
35             description (Str)
36             notes (Str)
37             scheme (Str)
38             account_balance (Int)
39             amount (Int)
40             local_amount (Int)
41             counterparty (HashRef)
42             metadata (HashRef)
43             is_load (Bool)
44             originator (Bool)
45             merchant (Business::Mondo::Merchant)
46             currency (Data::Currency)
47             local_currency (Data::Currency)
48             created (DateTime)
49             updated (DateTime)
50             settled (DateTime)
51             attachments (ArrayRef[Business::Mondo::Attachment])
52              
53             Note that if a HashRef or Str is passed to ->merchant it will be coerced
54             into a Business::Mondo::Merchant object. When a Str is passed to ->currency
55             / ->local_currency this will be coerced to a Data::Currency object, and
56             when a Str is passed to ->created / ->updated / ->settled this will be
57             coerced to a DateTime object.
58              
59             =cut
60              
61             has [ qw/
62             id account_id category dedupe_id description notes scheme
63             / ] => (
64             is => 'ro',
65             isa => Str,
66             );
67              
68             has [ qw/ account_balance amount local_amount / ] => (
69             is => 'ro',
70             isa => Int,
71             );
72              
73             has [ qw/ counterparty metadata / ] => (
74             is => 'ro',
75             isa => HashRef,
76             );
77              
78             has [ qw/ is_load originator / ] => (
79             is => 'ro',
80             isa => Any,
81             );
82              
83             has merchant => (
84             is => 'ro',
85             isa => Maybe[InstanceOf['Business::Mondo::Merchant']],
86             coerce => sub {
87             my ( $args ) = @_;
88              
89             return undef if ! defined $args;
90              
91             if ( ref( $args ) eq 'HASH' ) {
92             return undef if ! keys %{ $args };
93             $args = Business::Mondo::Merchant->new(
94             client => $Business::Mondo::Resource::client,
95             %{ $args },
96             );
97             } elsif ( ! ref( $args ) ) {
98             $args = Business::Mondo::Merchant->new(
99             client => $Business::Mondo::Resource::client,
100             id => $args,
101             );
102             }
103              
104             return $args;
105             },
106             );
107              
108             has attachments => (
109             is => 'ro',
110             isa => Maybe[ArrayRef[InstanceOf['Business::Mondo::Attachment']]],
111             coerce => sub {
112             my ( $args ) = @_;
113              
114             return undef if ! defined $args;
115              
116             my @attachments;
117              
118             foreach my $attachment ( @{ $args } ) {
119             push( @attachments,Business::Mondo::Attachment->new(
120             client => $Business::Mondo::Resource::client,
121             %{ $attachment },
122             ) );
123             }
124              
125             return [ @attachments ];
126             },
127             );
128              
129             has [ qw/ created updated settled / ] => (
130             is => 'ro',
131             isa => Maybe[InstanceOf['DateTime']],
132             coerce => sub {
133             my ( $args ) = @_;
134              
135             if ( ! ref( $args ) ) {
136             $args = DateTime::Format::DateParse->parse_datetime( $args );
137             }
138              
139             return $args;
140             },
141             );
142              
143             =head1 Operations on an transaction
144              
145             =head2 get
146              
147             Returns a new instance of the object populated with the attributes having called
148             the API
149              
150             my $populated_transaction = $transaction->get;
151              
152             This is for when you have instantiated an object with the id, so calling the API
153             will retrieve the full details for the entity.
154              
155             =cut
156              
157             sub get {
158 1     1 1 12 shift->SUPER::get( 'transaction' );
159             }
160              
161             =head2 annotate
162              
163             Returns a new instance of the object with annotated data having called the API
164              
165             my $annotated_transaction = $transaction->annotate(
166             foo => "bar",
167             baz => "boz,
168             );
169              
170             =cut
171              
172             sub annotate {
173 1     1 1 3 my ( $self,%annotations ) = @_;
174              
175 1         6 %annotations = $self->_params_as_array_string( 'metadata',\%annotations );
176              
177 1         26 my $data = $self->client->api_patch( $self->url,\%annotations );
178 1         43 $data = $data->{transaction};
179              
180             return $self->new(
181             client => $self->client,
182 1         3 %{ $data },
  1         19  
183             );
184             }
185              
186             sub annotations {
187 1     1 0 9 return shift->metadata;
188             }
189              
190             sub BUILD {
191 6     6 0 131 my ( $self,$args ) = @_;
192              
193 6         12 foreach my $c ( 'local_','' ) {
194              
195 12         17 my $amount_accessor = "${c}amount";
196 12         15 my $currency_accessor = "${c}currency";
197              
198 12 100       56 if ( my $amount = $self->$amount_accessor ) {
199 5         23 my $decimal_precision = decimal_precision( $self->$currency_accessor->code );
200 5         106 my $value = $amount / ( 10 ** $decimal_precision );
201 5         121 $self->$currency_accessor->value( $value );
202             }
203             }
204              
205             };
206              
207             =head1 SEE ALSO
208              
209             L<Business::Mondo>
210              
211             L<Business::Mondo::Resource>
212              
213             =head1 AUTHOR
214              
215             Lee Johnson - C<leejo@cpan.org>
216              
217             =head1 LICENSE
218              
219             This library is free software; you can redistribute it and/or modify it under
220             the same terms as Perl itself. If you would like to contribute documentation,
221             features, bug fixes, or anything else then please raise an issue / pull request:
222              
223             https://github.com/leejo/business-mondo
224              
225             =cut
226              
227             1;
228              
229             # vim: ts=4:sw=4:et