stray closing /TABLE in the no-ticket case
[freeside.git] / FS / FS / invoice_conf.pm
1 package FS::invoice_conf;
2
3 use strict;
4 use base qw( FS::Record FS::Conf );
5 use FS::Record qw( qsearch qsearchs );
6
7 =head1 NAME
8
9 FS::invoice_conf - Object methods for invoice_conf records
10
11 =head1 SYNOPSIS
12
13   use FS::invoice_conf;
14
15   $record = new FS::invoice_conf \%hash;
16   $record = new FS::invoice_conf { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::invoice_conf object represents a set of localized invoice 
29 configuration values.  FS::invoice_conf inherits from FS::Record and FS::Conf,
30 and supports the FS::Conf interface.  The following fields are supported:
31
32 =over 4
33
34 =item confnum - primary key
35
36 =item modenum - L<FS::invoice_mode> foreign key
37
38 =item locale - locale string (see L<FS::Locales>)
39
40 =item notice_name - the title to display on the invoice
41
42 =item subject - subject line of the email
43
44 =item htmlnotes - "notes" section (HTML)
45
46 =item htmlfooter - footer (HTML)
47
48 =item htmlsummary - summary header, for invoices in summary format (HTML)
49
50 =item htmlreturnaddress - return address (HTML)
51
52 =item htmlwatermark - watermark to show in background (HTML)
53
54 =item latexnotes - "notes" section (LaTeX)
55
56 =item latexfooter - footer (LaTeX)
57
58 =item latexsummary - summary header, for invoices in summary format (LaTeX)
59
60 =item latexreturnaddress - return address (LaTeX)
61
62 =item latexsmallfooter - footer for pages after the first (LaTeX)
63
64 =item latexwatermark - watermark to show in background (LaTeX)
65
66 =item with_latexcoupon - 'Y' to print the payment coupon (LaTeX)
67
68 =item lpr - command to print the invoice (passed on stdin as a PDF)
69
70 =back
71
72 =head1 METHODS
73
74 =over 4
75
76 =item new HASHREF
77
78 Creates a new invoice configuration.  To add it to the database, see 
79 L<"insert">.
80
81 =cut
82
83 # the new method can be inherited from FS::Record, if a table method is defined
84
85 sub table { 'invoice_conf'; }
86
87 # fields (prefixed with 'with_') that turn on certain conf variables 
88 # (set them to their conf values, rather than to null)
89 my %flags = (
90   latexcoupon => 1
91 );
92
93 =item insert
94
95 Adds this record to the database.  If there is an error, returns the error,
96 otherwise returns false.
97
98 =cut
99
100 # slightly special: you can insert/replace the invoice mode this way
101
102 sub insert {
103   my $self = shift;
104   if (!$self->modenum) {
105     my $invoice_mode = FS::invoice_mode->new({
106         'modename' => $self->modename,
107         'agentnum' => $self->agentnum,
108     });
109     my $error = $invoice_mode->insert;
110     return $error if $error;
111     $self->set('modenum' => $invoice_mode->modenum);
112   } else {
113     my $invoice_mode = FS::invoice_mode->by_key($self->modenum);
114     my $changed = 0;
115     foreach (qw(agentnum modename)) {
116       $changed ||= ($invoice_mode->get($_) eq $self->get($_));
117       $invoice_mode->set($_, $self->get($_));
118     }
119     my $error = $invoice_mode->replace if $changed;
120     return $error if $error;
121   }
122   $self->SUPER::insert(@_);
123 }
124
125 =item delete
126
127 Delete this record from the database.
128
129 =cut
130
131 sub delete {
132   my $self = shift;
133   my $error = $self->FS::Record::delete; # not Conf::delete
134   return $error if $error;
135   my $invoice_mode = FS::invoice_mode->by_key($self->modenum);
136   if ( $invoice_mode and
137        FS::invoice_conf->count('modenum = '.$invoice_mode->modenum) == 0 ) {
138     $error = $invoice_mode->delete;
139     return $error if $error;
140   }
141   '';
142 }
143
144 =item replace OLD_RECORD
145
146 Replaces the OLD_RECORD with this one in the database.  If there is an error,
147 returns the error, otherwise returns false.
148
149 =cut
150
151 sub replace {
152   my $self = shift;
153   my $error = $self->SUPER::replace(@_);
154   return $error if $error;
155
156   my $invoice_mode = FS::invoice_mode->by_key($self->modenum);
157   my $changed = 0;
158   foreach (qw(agentnum modename)) {
159     $changed ||= ($invoice_mode->get($_) eq $self->get($_));
160     $invoice_mode->set($_, $self->get($_));
161   }
162   $error = $invoice_mode->replace if $changed;
163   return $error if $error;
164 }
165
166 =item check
167
168 Checks all fields to make sure this is a valid example.  If there is
169 an error, returns the error, otherwise returns false.  Called by the insert
170 and replace methods.
171
172 =cut
173
174 # the check method should currently be supplied - FS::Record contains some
175 # data checking routines
176
177 sub check {
178   my $self = shift;
179
180   my $error = 
181     $self->ut_numbern('confnum')
182     # core properties
183     || $self->ut_number('modenum')
184     || $self->ut_textn('locale')
185     # direct overrides of conf variables
186     || $self->ut_anything('notice_name')
187     || $self->ut_anything('subject')
188     || $self->ut_anything('htmlnotes')
189     || $self->ut_anything('htmlfooter')
190     || $self->ut_anything('htmlsummary')
191     || $self->ut_anything('htmlreturnaddress')
192     || $self->ut_anything('htmlwatermark')
193     || $self->ut_anything('latexnotes')
194     || $self->ut_anything('latexfooter')
195     || $self->ut_anything('latexsummary')
196     || $self->ut_anything('latexsmallfooter')
197     || $self->ut_anything('latexreturnaddress')
198     || $self->ut_anything('latexwatermark')
199     # flags
200     || $self->ut_flag('with_latexcoupon')
201   ;
202   return $error if $error;
203
204   $self->SUPER::check;
205 }
206
207 # hook _config to substitute our own values; let FS::Conf do the rest of 
208 # the interface
209
210 sub _config {
211   my $self = shift;
212   # if we fall back, we still want FS::Conf to respect our locale
213   $self->{locale} = $self->get('locale');
214   my ($key, $agentnum, $nodefault) = @_;
215   # some fields, but not all, start with invoice_
216   my $colname = $key;
217   if ( $key =~ /^invoice_(.*)$/ ) {
218     $colname = $1;
219   }
220   if ( $flags{$colname} and !$self->get("with_$colname") ) {
221     # then a flag field is defined, and the flag is off, so act as though
222     # the config entry doesn't exist
223     # (currently only used for "latexcoupon", to allow invoice modes
224     # where the coupon is not printed)
225     return undef;
226   }
227   if ( length($self->get($colname)) ) {
228     return FS::conf->new({ 'name'   => $key,
229                            'value'  => $self->get($colname) });
230   } else {
231     return $self->FS::Conf::_config(@_);
232   }
233 }
234
235 # disambiguation
236 sub set {
237   my $self = shift;
238   $self->FS::Record::set(@_);
239 }
240
241 sub exists {
242   my $self = shift;
243   $self->FS::Conf::exists(@_);
244 }
245
246 =back
247
248 =head1 SEE ALSO
249
250 L<FS::Template_Mixin>, L<FS::Record>, schema.html from the base documentation.
251
252 =cut
253
254 1;
255