failure status
[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 =back
41
42 =head1 METHODS
43
44 =over 4
45
46 =item totals
47
48 Returns a hash containing 'credit_count', 'credit_sum', 'payment_count', 
49 and 'payment_sum'.  These are the number of credits, sum of credit amounts,
50 number of payments, and sum of payment amounts.
51
52 =cut
53
54 package Business::BatchPayment::Batch;
55
56 use strict;
57 use Moose;
58 use Moose::Util::TypeConstraints;
59 use DateTime;
60
61 has incoming => (
62   is => 'rw',
63   isa => 'Bool',
64 );
65
66 has batch_id => (
67   is => 'rw',
68   isa => 'Str',
69   default => '',
70 );
71
72 has items => (
73   traits => ['Array'],
74   is => 'rw',
75   isa => 'ArrayRef[Business::BatchPayment::Item]',
76   handles =>  {
77     count     => 'count',
78     elements  => 'elements',
79     push      => 'push',
80   },
81   default => sub { [] },
82 );
83
84 class_type 'DateTime';
85
86 has process_date    => (
87   is => 'rw',
88   isa => 'DateTime',
89   coerce => 1,
90   default => sub {
91 #    warn "No batch process date set; assuming tomorrow.\n";
92     DateTime->today->add(days => 1);
93   },
94 );
95
96 has num => (
97   is => 'rw',
98   isa => 'Maybe[Int]',
99 );
100
101 sub totals {
102   my $self = shift;
103   my %totals = map {$_ => 0}
104     qw(credit_count credit_sum payment_count payment_sum);
105   foreach ($self->elements) {
106     if ($_->action eq 'credit') {
107       $totals{credit_count}++;
108       $totals{credit_sum} += $_->amount;
109     } elsif ( $_->action eq 'payment') {
110       $totals{payment_count}++;
111       $totals{payment_sum} += $_->amount;
112     }
113   }
114   %totals;
115 }
116
117 1;