f3dba9081958bf9a47bee15aec6f91f4dba43bdc
[freeside.git] / FS / FS / cust_bill_void.pm
1 package FS::cust_bill_void;
2 use base qw( FS::Template_Mixin FS::cust_main_Mixin FS::otaker_Mixin FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearch qsearchs dbh fields );
6 use FS::cust_statement;
7 use FS::access_user;
8 use FS::cust_bill_pkg_void;
9 use FS::cust_bill;
10
11 =head1 NAME
12
13 FS::cust_bill_void - Object methods for cust_bill_void records
14
15 =head1 SYNOPSIS
16
17   use FS::cust_bill_void;
18
19   $record = new FS::cust_bill_void \%hash;
20   $record = new FS::cust_bill_void { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::cust_bill_void object represents a voided invoice.  FS::cust_bill_void
33 inherits from FS::Record.  The following fields are currently supported:
34
35 =over 4
36
37 =item invnum
38
39 primary key
40
41 =item custnum
42
43 custnum
44
45 =item _date
46
47 _date
48
49 =item charged
50
51 charged
52
53 =item invoice_terms
54
55 invoice_terms
56
57 =item previous_balance
58
59 previous_balance
60
61 =item billing_balance
62
63 billing_balance
64
65 =item closed
66
67 closed
68
69 =item statementnum
70
71 statementnum
72
73 =item agent_invid
74
75 agent_invid
76
77 =item promised_date
78
79 promised_date
80
81 =item void_date
82
83 void_date
84
85 =item reason
86
87 reason
88
89 =item void_usernum
90
91 void_usernum
92
93
94 =back
95
96 =head1 METHODS
97
98 =over 4
99
100 =item new HASHREF
101
102 Creates a new voided invoice.  To add the voided invoice to the database, see L<"insert">.
103
104 Note that this stores the hash reference, not a distinct copy of the hash it
105 points to.  You can ask the object for a copy with the I<hash> method.
106
107 =cut
108
109 sub table { 'cust_bill_void'; }
110 sub notice_name { 'VOIDED Invoice'; }
111 sub template_conf { 'invoice_'; }
112
113 sub has_sections {
114   my $self = shift;
115   my $agentnum = $self->cust_main->agentnum;
116   my $tc = $self->template_conf;
117
118   $self->conf->exists($tc.'sections', $agentnum) ||
119   $self->conf->exists($tc.'sections_by_location', $agentnum);
120 }
121
122
123 =item insert
124
125 Adds this record to the database.  If there is an error, returns the error,
126 otherwise returns false.
127
128 =cut
129
130 =item unvoid 
131
132 "Un-void"s this invoice: Deletes the voided invoice from the database and adds
133 back a normal invoice (and related tables).
134
135 =cut
136
137 sub unvoid {
138   my $self = shift;
139
140   local $SIG{HUP} = 'IGNORE';
141   local $SIG{INT} = 'IGNORE';
142   local $SIG{QUIT} = 'IGNORE';
143   local $SIG{TERM} = 'IGNORE';
144   local $SIG{TSTP} = 'IGNORE';
145   local $SIG{PIPE} = 'IGNORE';
146
147   my $oldAutoCommit = $FS::UID::AutoCommit;
148   local $FS::UID::AutoCommit = 0;
149   my $dbh = dbh;
150
151   my $cust_bill = new FS::cust_bill ( {
152     map { $_ => $self->get($_) } fields('cust_bill')
153   } );
154   my $error = $cust_bill->insert;
155   if ( $error ) {
156     $dbh->rollback if $oldAutoCommit;
157     return $error;
158   }
159
160   foreach my $cust_bill_pkg_void ( $self->cust_bill_pkg ) {
161     my $error = $cust_bill_pkg_void->unvoid;
162     if ( $error ) {
163       $dbh->rollback if $oldAutoCommit;
164       return $error;
165     }
166   }
167
168   $error = $self->delete;
169   if ( $error ) {
170     $dbh->rollback if $oldAutoCommit;
171     return $error;
172   }
173
174   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
175
176   '';
177
178 }
179
180 =item delete
181
182 Delete this record from the database.
183
184 =cut
185
186 =item replace OLD_RECORD
187
188 Replaces the OLD_RECORD with this one in the database.  If there is an error,
189 returns the error, otherwise returns false.
190
191 =cut
192
193 =item check
194
195 Checks all fields to make sure this is a valid voided invoice.  If there is
196 an error, returns the error, otherwise returns false.  Called by the insert
197 and replace methods.
198
199 =cut
200
201 sub check {
202   my $self = shift;
203
204   my $error = 
205     $self->ut_number('invnum')
206     || $self->ut_foreign_key('custnum', 'cust_main', 'custnum' )
207     || $self->ut_numbern('_date')
208     || $self->ut_money('charged')
209     || $self->ut_textn('invoice_terms')
210     || $self->ut_moneyn('previous_balance')
211     || $self->ut_moneyn('billing_balance')
212     || $self->ut_enum('closed', [ '', 'Y' ])
213     || $self->ut_foreign_keyn('statementnum', 'cust_statement', 'statementnum')
214     || $self->ut_numbern('agent_invid')
215     || $self->ut_numbern('promised_date')
216     || $self->ut_numbern('void_date')
217     || $self->ut_textn('reason')
218     || $self->ut_numbern('void_usernum')
219   ;
220   return $error if $error;
221
222   $self->void_date(time) unless $self->void_date;
223
224   $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
225     unless $self->void_usernum;
226
227   $self->SUPER::check;
228 }
229
230 =item display_invnum
231
232 Returns the displayed invoice number for this invoice: agent_invid if
233 cust_bill-default_agent_invid is set and it has a value, invnum otherwise.
234
235 =cut
236
237 sub display_invnum {
238   my $self = shift;
239   my $conf = $self->conf;
240   if ( $conf->exists('cust_bill-default_agent_invid') && $self->agent_invid ){
241     return $self->agent_invid;
242   } else {
243     return $self->invnum;
244   }
245 }
246
247 =item void_access_user
248
249 Returns the voiding employee object (see L<FS::access_user>).
250
251 =cut
252
253 sub void_access_user {
254   my $self = shift;
255   qsearchs('access_user', { 'usernum' => $self->void_usernum } );
256 }
257
258 =item cust_main
259
260 =item cust_bill_pkg
261
262 =cut
263
264 sub cust_bill_pkg { #actually cust_bill_pkg_void objects
265   my $self = shift;
266   qsearch('cust_bill_pkg_void', { invnum=>$self->invnum });
267 }
268
269 =back
270
271 =item cust_pkg
272
273 Returns the packages (see L<FS::cust_pkg>) corresponding to the line items for
274 this invoice.
275
276 =cut
277
278 sub cust_pkg {
279   my $self = shift;
280   my @cust_pkg = map { $_->pkgnum > 0 ? $_->cust_pkg : () }
281                  $self->cust_bill_pkg;
282   my %saw = ();
283   grep { ! $saw{$_->pkgnum}++ } @cust_pkg;
284 }
285
286 =item search_sql_where HASHREF
287
288 Class method which returns an SQL WHERE fragment to search for parameters
289 specified in HASHREF.  Accepts the following parameters for 
290 L<FS::cust_bill::search_sql_where>: C<_date>, C<invnum_min>, C<invnum_max>,
291 C<agentnum>, C<custnum>, C<cust_classnum>, C<refnum>.  Also 
292 accepts the following:
293
294 =over 4
295
296 =item void_date
297
298 Arrayref of start and end date to find invoices voided in a date range.
299
300 =item void_usernum
301
302 User identifier (L<FS::access_user> key) that voided the invoice.
303
304 =back
305
306 =cut
307
308 sub search_sql_where {
309   my($class, $param) = @_;
310
311   my $cust_bill_param = {
312     map { $_ => $param->{$_} }
313     grep { exists($param->{$_}) }
314     qw( _date invnum_min invnum_max agentnum custnum cust_classnum 
315         refnum )
316   };
317   my $search_sql = FS::cust_bill->search_sql_where($cust_bill_param);
318   $search_sql =~ s/cust_bill/cust_bill_void/g;
319   my @search = ($search_sql);
320
321   if ( $param->{void_date} ) {
322     my($beginning, $ending) = @{$param->{void_date}};
323     push @search, "cust_bill_void.void_date >= $beginning",
324                   "cust_bill_void.void_date <  $ending";
325   }
326
327   if ( $param->{void_usernum} =~ /^(\d+)$/ ) {
328     my $usernum = $1;
329     push @search, "cust_bill_void.void_usernum = $1";
330   }
331
332   join(" AND ", @search);
333 }
334
335
336 =item enable_previous
337
338 =cut
339
340 sub enable_previous { 0 }
341
342 =back
343
344 =head1 BUGS
345
346 =head1 SEE ALSO
347
348 L<FS::Record>, schema.html from the base documentation.
349
350 =cut
351
352 1;
353