agent-virtualize credit card surcharge percentage, RT#72961
[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 @cust_bill_batch = sort { $a->invnum <=> $b->invnum }
62                           $self->cust_bill_batch;
63   return "No invoices in batch ".$self->batchnum.'.' if !@cust_bill_batch;
64
65   my $conf = FS::Conf->new;
66   my $duplex = $conf->exists('invoice_print_pdf-duplex');
67
68   my $pdf_out;
69   my $num = 0;
70   foreach my $cust_bill_batch (@cust_bill_batch) {
71     my $part =
72       $cust_bill_batch->cust_bill->print_pdf({$cust_bill_batch->options});
73     die 'Failed creating PDF from invoice '.$cust_bill_batch->invnum.'\n'
74       if !$part;
75
76     if($pdf_out) {
77       $pdf_out->appendPDF(CAM::PDF->new($part));
78     } else {
79       $pdf_out = CAM::PDF->new($part);
80     }
81     if ( $duplex ) {
82       my $n = $pdf_out->numPages;
83       if ( $n % 2 == 1 ) {
84         # then insert a blank page so we end on an even number
85         $pdf_out->duplicatePage($n, 1);
86       }
87     }
88     if ($job) {
89       # update progressbar
90       $num++;
91       my $error = $job->update_statustext(int(100 * $num/scalar(@cust_bill_batch)));
92       die $error if $error;
93     }
94   }
95   $job->update_statustext(100, 'Combining invoices') if $job;
96
97   return $pdf_out->toPDF;
98 }
99
100 =item close
101
102 Set the status of the batch to 'R' (resolved).
103
104 =cut
105
106 sub close {
107   my $self = shift;
108   $self->status('R');
109   return $self->replace;
110 }
111
112 sub check {
113   my $self = shift;
114
115   my $error =
116        $self->ut_numbern('batchnum')
117     || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
118     || $self->ut_enum('status', [ 'O', 'R' ] )
119   ;
120   return $error if $error;
121
122   $self->SUPER::check;
123 }
124
125 =item agent
126
127 Returns the agent (see L<FS::agent>) for this invoice batch.
128
129 =back
130
131 =head1 SUBROUTINES
132
133 =item process_print_pdf
134
135 =cut
136
137 use Data::Dumper;
138
139 sub process_print_pdf {
140   my $job = shift;
141   my $param = shift;
142   warn Dumper($param) if $DEBUG;
143   die "no batchnum specified!\n" if ! exists($param->{batchnum});
144   my $batch = FS::bill_batch->by_key($param->{batchnum});
145   die "batch '$param->{batchnum}' not found!\n" if !$batch;
146
147   if ( $param->{'close'} ) {
148     my $error = $batch->close;
149     die $error if $error;
150   }
151
152   my $pdf = $batch->print_pdf($job);
153   $batch->pdf($pdf);
154   my $error = $batch->replace;
155   die $error if $error;
156 }
157
158 =back
159
160 =head1 BUGS
161
162 =head1 SEE ALSO
163
164 L<FS::Record>, schema.html from the base documentation.
165
166 =cut
167
168 1;
169