Added processor_id functionality
[Business-BatchPayment.git] / BatchPayment / Batch.pm
1 =head1 NAME
2
3 Business::BatchPayment::Batch
4
5 =head1 DESCRIPTION
6
7 A Business::BatchPayment::Batch object represents a group of payment 
8 requests or confirmations (L<Business::BatchPayment::Item>) submitted
9 to a bank (or returned from a bank) as a batch.
10
11 =head1 ATTRIBUTES
12
13 =over 4
14
15 =item incoming - Flag for one-way batches.  The processor must set
16 this if the batch was originated by the gateway.
17
18 =item batch_id - Batch identifier.  The format is processor-specific
19 but usually must be a positive integer, if it's used at all.
20
21 Processor modules may include C<batch_id> in a reply batch ONLY if 
22 it is guaranteed to match the batch_id of a request batch AND all 
23 the items in the reply batch come from that request batch.  Otherwise, 
24 C<batch_id> must be undef.  It must always be undef when using one-way
25 (receive-only) workflow, since there are no request batches.
26
27 =item process_date - The intended processing date for a request batch.
28 If not set, it will default to the start of the next day; if that's  
29 not what you want, set it explicitly.
30
31 =item items - An arrayref of L<Business::BatchPayment::Item> objects
32 included in the batch.
33
34 =item num
35
36 If your processor uses C<format_header> and C<format_item>, this will 
37 be set to 0 by C<format_header> and incremented every time C<format_item>
38 is called.  Convenient for formats that require record numbers.
39
40 =item processor_id
41
42 Processor-specific value to be set by the processor during submit and
43 passed to receive, useful for processors that require their own identifiers 
44 for download.
45
46 =back
47
48 =head1 METHODS
49
50 =over 4
51
52 =item totals
53
54 Returns a hash containing 'credit_count', 'credit_sum', 'payment_count', 
55 and 'payment_sum'.  These are the number of credits, sum of credit amounts,
56 number of payments, and sum of payment amounts.
57
58 =cut
59
60 package Business::BatchPayment::Batch;
61
62 use strict;
63 use Moose;
64 use Moose::Util::TypeConstraints;
65 use DateTime;
66
67 has incoming => (
68   is => 'rw',
69   isa => 'Bool',
70 );
71
72 has batch_id => (
73   is => 'rw',
74   isa => 'Str',
75   default => '',
76 );
77
78 has processor_id => (
79   is => 'rw',
80   isa => 'Str',
81   default => '',
82 );
83
84 has items => (
85   traits => ['Array'],
86   is => 'rw',
87   isa => 'ArrayRef[Business::BatchPayment::Item]',
88   handles =>  {
89     count     => 'count',
90     elements  => 'elements',
91     push      => 'push',
92   },
93   default => sub { [] },
94 );
95
96 class_type 'DateTime';
97
98 has process_date    => (
99   is => 'rw',
100   isa => 'DateTime',
101   coerce => 1,
102   default => sub {
103 #    warn "No batch process date set; assuming tomorrow.\n";
104     DateTime->today->add(days => 1);
105   },
106 );
107
108 has num => (
109   is => 'rw',
110   isa => 'Maybe[Int]',
111 );
112
113 sub totals {
114   my $self = shift;
115   my %totals = map {$_ => 0}
116     qw(credit_count credit_sum payment_count payment_sum);
117   foreach ($self->elements) {
118     if ($_->action eq 'credit') {
119       $totals{credit_count}++;
120       $totals{credit_sum} += $_->amount;
121     } elsif ( $_->action eq 'payment') {
122       $totals{payment_count}++;
123       $totals{payment_sum} += $_->amount;
124     }
125   }
126   %totals;
127 }
128
129 1;