1 package FS::cust_bill_void;
2 use base qw( FS::Template_Mixin FS::cust_main_Mixin FS::otaker_Mixin FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh fields );
6 use FS::cust_statement;
8 use FS::cust_bill_pkg_void;
13 FS::cust_bill_void - Object methods for cust_bill_void records
17 use FS::cust_bill_void;
19 $record = new FS::cust_bill_void \%hash;
20 $record = new FS::cust_bill_void { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
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:
57 =item previous_balance
102 Creates a new voided invoice. To add the voided invoice to the database, see L<"insert">.
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.
109 sub table { 'cust_bill_void'; }
110 sub notice_name { 'VOIDED Invoice'; }
111 sub template_conf { 'invoice_'; }
115 my $agentnum = $self->cust_main->agentnum;
116 my $tc = $self->template_conf;
118 $self->conf->exists($tc.'sections', $agentnum) ||
119 $self->conf->exists($tc.'sections_by_location', $agentnum);
125 Adds this record to the database. If there is an error, returns the error,
126 otherwise returns false.
132 "Un-void"s this invoice: Deletes the voided invoice from the database and adds
133 back a normal invoice (and related tables).
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';
147 my $oldAutoCommit = $FS::UID::AutoCommit;
148 local $FS::UID::AutoCommit = 0;
151 my $cust_bill = new FS::cust_bill ( {
152 map { $_ => $self->get($_) } fields('cust_bill')
154 my $error = $cust_bill->insert;
156 $dbh->rollback if $oldAutoCommit;
160 foreach my $cust_bill_pkg_void ( $self->cust_bill_pkg ) {
161 my $error = $cust_bill_pkg_void->unvoid;
163 $dbh->rollback if $oldAutoCommit;
168 $error = $self->delete;
170 $dbh->rollback if $oldAutoCommit;
174 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
182 Delete this record from the database.
186 =item replace OLD_RECORD
188 Replaces the OLD_RECORD with this one in the database. If there is an error,
189 returns the error, otherwise returns false.
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
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')
220 return $error if $error;
222 $self->void_date(time) unless $self->void_date;
224 $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
225 unless $self->void_usernum;
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.
239 my $conf = $self->conf;
240 if ( $conf->exists('cust_bill-default_agent_invid') && $self->agent_invid ){
241 return $self->agent_invid;
243 return $self->invnum;
247 =item void_access_user
249 Returns the voiding employee object (see L<FS::access_user>).
253 sub void_access_user {
255 qsearchs('access_user', { 'usernum' => $self->void_usernum } );
264 sub cust_bill_pkg { #actually cust_bill_pkg_void objects
266 qsearch('cust_bill_pkg_void', { invnum=>$self->invnum });
273 Returns the packages (see L<FS::cust_pkg>) corresponding to the line items for
280 my @cust_pkg = map { $_->pkgnum > 0 ? $_->cust_pkg : () }
281 $self->cust_bill_pkg;
283 grep { ! $saw{$_->pkgnum}++ } @cust_pkg;
286 =item search_sql_where HASHREF
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:
298 Arrayref of start and end date to find invoices voided in a date range.
302 User identifier (L<FS::access_user> key) that voided the invoice.
308 sub search_sql_where {
309 my($class, $param) = @_;
311 my $cust_bill_param = {
312 map { $_ => $param->{$_} }
313 grep { exists($param->{$_}) }
314 qw( _date invnum_min invnum_max agentnum custnum cust_classnum
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);
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";
327 if ( $param->{void_usernum} =~ /^(\d+)$/ ) {
329 push @search, "cust_bill_void.void_usernum = $1";
332 join(" AND ", @search);
336 =item enable_previous
340 sub enable_previous { 0 }
348 L<FS::Record>, schema.html from the base documentation.