eWay self-signup fixes
[freeside.git] / FS / FS / bill_batch.pm
1 package FS::bill_batch;
2
3 use strict;
4 use vars qw( @ISA $me $DEBUG );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::cust_bill_batch;
7
8 @ISA = qw( FS::Record );
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 status - either 'O' (open) or 'R' (resolved/closed).
40
41 =item pdf - blob field for temporarily storing the invoice as a PDF.
42
43 =back
44
45 =head1 METHODS
46
47 =over 4
48
49 =item print_pdf
50
51 Typeset the entire batch as a PDF file.  Returns the PDF as a string.
52
53 =cut
54
55 sub print_pdf {
56   eval 'use CAM::PDF';
57   warn "Failed to load CAM::PDF: '$@'\n" if $@;
58
59   my $self = shift;
60   my $job = shift;
61   $job->update_statustext(0) if $job;
62   my @invoices = sort { $a->invnum <=> $b->invnum }
63                  qsearch('cust_bill_batch', { batchnum => $self->batchnum });
64   return "No invoices in batch ".$self->batchnum.'.' if !@invoices;
65
66   my $pdf_out;
67   my $num = 0;
68   foreach my $invoice (@invoices) {
69     my $part = $invoice->cust_bill->print_pdf({$invoice->options});
70     die 'Failed creating PDF from invoice '.$invoice->invnum.'\n' if !$part;
71
72     if($pdf_out) {
73       $pdf_out->appendPDF(CAM::PDF->new($part));
74     }
75     else {
76       $pdf_out = CAM::PDF->new($part);
77     }
78     if($job) {
79       # update progressbar
80       $num++;
81       my $error = $job->update_statustext(int(100 * $num/scalar(@invoices)));
82       die $error if $error;
83     }
84   }
85
86   return $pdf_out->toPDF;
87 }
88
89 =item close
90
91 Set the status of the batch to 'R' (resolved).
92
93 =cut
94
95 sub close {
96   my $self = shift;
97   $self->status('R');
98   return $self->replace;
99 }
100
101 =back
102
103 =head1 CLASS METHODS
104
105 =item get_open_batch
106
107 Returns the currently open batch.  There should only be one at a time.
108
109 =cut
110
111 sub get_open_batch {
112   my $class = shift;
113   my $batch = qsearchs('bill_batch', { status => 'O' });
114   return $batch if $batch;
115   $batch = FS::bill_batch->new({status => 'O'});
116   my $error = $batch->insert;
117   die $error if $error;
118   return $batch;
119 }
120
121 use Storable 'thaw';
122 use Data::Dumper;
123 use MIME::Base64;
124
125 sub process_print_pdf {
126   my $job = shift;
127   my $param = thaw(decode_base64(shift));
128   warn Dumper($param) if $DEBUG;
129   die "no batchnum specified!\n" if ! exists($param->{batchnum});
130   my $batch = FS::bill_batch->by_key($param->{batchnum});
131   die "batch '$param->{batchnum}' not found!\n" if !$batch;
132
133   my $pdf = $batch->print_pdf($job);
134   $batch->pdf($pdf);
135   my $error = $batch->replace;
136   die $error if $error;
137 }
138
139
140 =back
141
142 =head1 BUGS
143
144 =head1 SEE ALSO
145
146 L<FS::Record>, schema.html from the base documentation.
147
148 =cut
149
150 1;
151