changes for cardfortress
[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 coerce 'DateTime', from 'Int', via { DateTime->from_epoch($_) };
86
87 has process_date    => (
88   is => 'rw',
89   isa => 'DateTime',
90   coerce => 1,
91   default => sub {
92 #    warn "No batch process date set; assuming tomorrow.\n";
93     DateTime->today->add(days => 1);
94   },
95 );
96
97 has num => (
98   is => 'rw',
99   isa => 'Maybe[Int]',
100 );
101
102 sub totals {
103   my $self = shift;
104   my %totals = map {$_ => 0}
105     qw(credit_count credit_sum payment_count payment_sum);
106   foreach ($self->elements) {
107     if ($_->action eq 'credit') {
108       $totals{credit_count}++;
109       $totals{credit_sum} += $_->amount;
110     } elsif ( $_->action eq 'payment') {
111       $totals{payment_count}++;
112       $totals{payment_sum} += $_->amount;
113     }
114   }
115   %totals;
116 }
117
118 1;