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