better error message
[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   if ( $@ ) {
58     die "CAM::PDF not installed\n" if $@ =~ /^Can't locate/;
59     die "Failed to load CAM::PDF: '$@'\n";
60   }
61
62   my $self = shift;
63   my $job = shift;
64   $job->update_statustext(0) if $job;
65   my @invoices = sort { $a->invnum <=> $b->invnum }
66                  qsearch('cust_bill_batch', { batchnum => $self->batchnum });
67   return "No invoices in batch ".$self->batchnum.'.' if !@invoices;
68
69   my $pdf_out;
70   my $num = 0;
71   foreach my $invoice (@invoices) {
72     my $part = $invoice->cust_bill->print_pdf({$invoice->options});
73     die 'Failed creating PDF from invoice '.$invoice->invnum.'\n' if !$part;
74
75     if($pdf_out) {
76       $pdf_out->appendPDF(CAM::PDF->new($part));
77     }
78     else {
79       $pdf_out = CAM::PDF->new($part);
80     }
81     if($job) {
82       # update progressbar
83       $num++;
84       my $error = $job->update_statustext(int(100 * $num/scalar(@invoices)));
85       die $error if $error;
86     }
87   }
88
89   return $pdf_out->toPDF;
90 }
91
92 =item close
93
94 Set the status of the batch to 'R' (resolved).
95
96 =cut
97
98 sub close {
99   my $self = shift;
100   $self->status('R');
101   return $self->replace;
102 }
103
104 =back
105
106 =head1 CLASS METHODS
107
108 =item get_open_batch
109
110 Returns the currently open batch.  There should only be one at a time.
111
112 =cut
113
114 sub get_open_batch {
115   my $class = shift;
116   my $batch = qsearchs('bill_batch', { status => 'O' });
117   return $batch if $batch;
118   $batch = FS::bill_batch->new({status => 'O'});
119   my $error = $batch->insert;
120   die $error if $error;
121   return $batch;
122 }
123
124 use Storable 'thaw';
125 use Data::Dumper;
126 use MIME::Base64;
127
128 sub process_print_pdf {
129   my $job = shift;
130   my $param = thaw(decode_base64(shift));
131   warn Dumper($param) if $DEBUG;
132   die "no batchnum specified!\n" if ! exists($param->{batchnum});
133   my $batch = FS::bill_batch->by_key($param->{batchnum});
134   die "batch '$param->{batchnum}' not found!\n" if !$batch;
135
136   my $pdf = $batch->print_pdf($job);
137   $batch->pdf($pdf);
138   my $error = $batch->replace;
139   die $error if $error;
140 }
141
142
143 =back
144
145 =head1 BUGS
146
147 =head1 SEE ALSO
148
149 L<FS::Record>, schema.html from the base documentation.
150
151 =cut
152
153 1;
154