fix certain problems with third-party payment, #23579
[freeside.git] / FS / FS / bill_batch.pm
1 package FS::bill_batch;
2 use base qw(FS::Record);
3
4 use strict;
5 use vars qw( $me $DEBUG );
6 use CAM::PDF;
7 use FS::Conf;
8
9 $me = '[ FS::bill_batch ]';
10 $DEBUG = 0;
11
12 sub table { 'bill_batch' }
13
14 sub nohistory_fields { 'pdf' }
15
16 =head1 NAME
17
18 FS::bill_batch - Object methods for bill_batch records
19
20 =head1 SYNOPSIS
21
22   use FS::bill_batch;
23
24   $open_batch = FS::bill_batch->get_open_batch;
25   
26   my $pdf = $open_batch->print_pdf;
27   
28   $error = $open_batch->close;
29   
30 =head1 DESCRIPTION
31
32 An FS::bill_batch object represents a batch of invoices.  FS::bill_batch 
33 inherits from FS::Record.  The following fields are currently supported:
34
35 =over 4
36
37 =item batchnum - primary key
38
39 =item agentnum - empty for global batches or agent (see L<FS::agent>)
40
41 =item status - either 'O' (open) or 'R' (resolved/closed).
42
43 =item pdf - blob field for temporarily storing the invoice as a PDF.
44
45 =back
46
47 =head1 METHODS
48
49 =over 4
50
51 =item print_pdf
52
53 Typeset the entire batch as a PDF file.  Returns the PDF as a string.
54
55 =cut
56
57 sub print_pdf {
58   my $self = shift;
59   my $job = shift;
60   $job->update_statustext(0) if $job;
61   my @invoices = sort { $a->invnum <=> $b->invnum } $self->cust_bill_batch;
62   return "No invoices in batch ".$self->batchnum.'.' if !@invoices;
63
64   my $pdf_out;
65   my $num = 0;
66   foreach my $invoice (@invoices) {
67     my $part = $invoice->cust_bill->print_pdf({$invoice->options});
68     die 'Failed creating PDF from invoice '.$invoice->invnum.'\n' if !$part;
69
70     if($pdf_out) {
71       $pdf_out->appendPDF(CAM::PDF->new($part));
72     }
73     else {
74       $pdf_out = CAM::PDF->new($part);
75     }
76     if($job) {
77       # update progressbar
78       $num++;
79       my $error = $job->update_statustext(int(100 * $num/scalar(@invoices)));
80       die $error if $error;
81     }
82   }
83   $job->update_statustext(100, 'Combining invoices') if $job;
84
85   return $pdf_out->toPDF;
86 }
87
88 =item close
89
90 Set the status of the batch to 'R' (resolved).
91
92 =cut
93
94 sub close {
95   my $self = shift;
96   $self->status('R');
97   return $self->replace;
98 }
99
100 sub check {
101   my $self = shift;
102
103   my $error =
104        $self->ut_numbern('batchnum')
105     || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
106     || $self->ut_enum('status', [ 'O', 'R' ] )
107   ;
108   return $error if $error;
109
110   $self->SUPER::check;
111 }
112
113 =item agent
114
115 Returns the agent (see L<FS::agent>) for this invoice batch.
116
117 =back
118
119 =head1 SUBROUTINES
120
121 =item process_print_pdf
122
123 =cut
124
125 use Storable 'thaw';
126 use Data::Dumper;
127 use MIME::Base64;
128
129 sub process_print_pdf {
130   my $job = shift;
131   my $param = thaw(decode_base64(shift));
132   warn Dumper($param) if $DEBUG;
133   die "no batchnum specified!\n" if ! exists($param->{batchnum});
134   my $batch = FS::bill_batch->by_key($param->{batchnum});
135   die "batch '$param->{batchnum}' not found!\n" if !$batch;
136
137   if ( $param->{'close'} ) {
138     my $error = $batch->close;
139     die $error if $error;
140   }
141
142   my $pdf = $batch->print_pdf($job);
143   $batch->pdf($pdf);
144   my $error = $batch->replace;
145   die $error if $error;
146 }
147
148 =back
149
150 =head1 BUGS
151
152 =head1 SEE ALSO
153
154 L<FS::Record>, schema.html from the base documentation.
155
156 =cut
157
158 1;
159