summaryrefslogtreecommitdiff
path: root/BatchPayment/Batch.pm
blob: b05eb80b3d5dc65499c27b149336498860e5433e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
=head1 NAME

Business::BatchPayment::Batch

=head1 DESCRIPTION

A Business::BatchPayment::Batch object represents a group of payment 
requests or confirmations (L<Business::BatchPayment::Item>) submitted
to a bank (or returned from a bank) as a batch.

=head1 ATTRIBUTES

=over 4

=item incoming - Flag for one-way batches.  The processor must set
this if the batch was originated by the gateway.

=item batch_id - Batch identifier.  The format is processor-specific
but usually must be a positive integer, if it's used at all.

Processor modules may include C<batch_id> in a reply batch ONLY if 
it is guaranteed to match the batch_id of a request batch AND all 
the items in the reply batch come from that request batch.  Otherwise, 
C<batch_id> must be undef.  It must always be undef when using one-way
(receive-only) workflow, since there are no request batches.

=item process_date - The intended processing date for a request batch.
If not set, it will default to the start of the next day; if that's  
not what you want, set it explicitly.

=item items - An arrayref of L<Business::BatchPayment::Item> objects
included in the batch.

=item num

If your processor uses C<format_header> and C<format_item>, this will 
be set to 0 by C<format_header> and incremented every time C<format_item>
is called.  Convenient for formats that require record numbers.

=back

=head1 METHODS

=over 4

=item totals

Returns a hash containing 'credit_count', 'credit_sum', 'payment_count', 
and 'payment_sum'.  These are the number of credits, sum of credit amounts,
number of payments, and sum of payment amounts.

=cut

package Business::BatchPayment::Batch;

use strict;
use Moose;
use Moose::Util::TypeConstraints;
use DateTime;

has incoming => (
  is => 'rw',
  isa => 'Bool',
);

has batch_id => (
  is => 'rw',
  isa => 'Str',
  default => '',
);

has items => (
  traits => ['Array'],
  is => 'rw',
  isa => 'ArrayRef[Business::BatchPayment::Item]',
  handles =>  {
    count     => 'count',
    elements  => 'elements',
    push      => 'push',
  },
  default => sub { [] },
);

class_type 'DateTime';

has process_date    => (
  is => 'rw',
  isa => 'DateTime',
  coerce => 1,
  default => sub {
#    warn "No batch process date set; assuming tomorrow.\n";
    DateTime->today->add(days => 1);
  },
);

has num => (
  is => 'rw',
  isa => 'Maybe[Int]',
);

sub totals {
  my $self = shift;
  my %totals = map {$_ => 0}
    qw(credit_count credit_sum payment_count payment_sum);
  foreach ($self->elements) {
    if ($_->action eq 'credit') {
      $totals{credit_count}++;
      $totals{credit_sum} += $_->amount;
    } elsif ( $_->action eq 'payment') {
      $totals{payment_count}++;
      $totals{payment_sum} += $_->amount;
    }
  }
  %totals;
}

1;