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 );
7 use FS::cust_statement;
9 use FS::cust_bill_pkg_void;
14 FS::cust_bill_void - Object methods for cust_bill_void records
18 use FS::cust_bill_void;
20 $record = new FS::cust_bill_void \%hash;
21 $record = new FS::cust_bill_void { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::cust_bill_void object represents a voided invoice. FS::cust_bill_void
34 inherits from FS::Record. The following fields are currently supported:
58 =item previous_balance
103 Creates a new voided invoice. To add the voided invoice to the database, see L<"insert">.
105 Note that this stores the hash reference, not a distinct copy of the hash it
106 points to. You can ask the object for a copy with the I<hash> method.
110 sub table { 'cust_bill_void'; }
111 sub notice_name { 'VOIDED Invoice'; }
112 #XXXsub template_conf { 'quotation_'; }
116 Adds this record to the database. If there is an error, returns the error,
117 otherwise returns false.
123 "Un-void"s this invoice: Deletes the voided invoice from the database and adds
124 back a normal invoice (and related tables).
131 local $SIG{HUP} = 'IGNORE';
132 local $SIG{INT} = 'IGNORE';
133 local $SIG{QUIT} = 'IGNORE';
134 local $SIG{TERM} = 'IGNORE';
135 local $SIG{TSTP} = 'IGNORE';
136 local $SIG{PIPE} = 'IGNORE';
138 my $oldAutoCommit = $FS::UID::AutoCommit;
139 local $FS::UID::AutoCommit = 0;
142 my $cust_bill = new FS::cust_bill ( {
143 map { $_ => $self->get($_) } fields('cust_bill')
145 my $error = $cust_bill->insert;
147 $dbh->rollback if $oldAutoCommit;
151 foreach my $cust_bill_pkg_void ( $self->cust_bill_pkg ) {
152 my $error = $cust_bill_pkg_void->unvoid;
154 $dbh->rollback if $oldAutoCommit;
159 $error = $self->delete;
161 $dbh->rollback if $oldAutoCommit;
165 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
173 Delete this record from the database.
177 =item replace OLD_RECORD
179 Replaces the OLD_RECORD with this one in the database. If there is an error,
180 returns the error, otherwise returns false.
186 Checks all fields to make sure this is a valid voided invoice. If there is
187 an error, returns the error, otherwise returns false. Called by the insert
196 $self->ut_number('invnum')
197 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum' )
198 || $self->ut_numbern('_date')
199 || $self->ut_money('charged')
200 || $self->ut_textn('invoice_terms')
201 || $self->ut_moneyn('previous_balance')
202 || $self->ut_moneyn('billing_balance')
203 || $self->ut_enum('closed', [ '', 'Y' ])
204 || $self->ut_foreign_keyn('statementnum', 'cust_statement', 'statementnum')
205 || $self->ut_numbern('agent_invid')
206 || $self->ut_numbern('promised_date')
207 || $self->ut_numbern('void_date')
208 || $self->ut_textn('reason')
209 || $self->ut_numbern('void_usernum')
211 return $error if $error;
213 $self->void_date(time) unless $self->void_date;
215 $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
216 unless $self->void_usernum;
223 Returns the displayed invoice number for this invoice: agent_invid if
224 cust_bill-default_agent_invid is set and it has a value, invnum otherwise.
230 my $conf = $self->conf;
231 if ( $conf->exists('cust_bill-default_agent_invid') && $self->agent_invid ){
232 return $self->agent_invid;
234 return $self->invnum;
238 =item void_access_user
240 Returns the voiding employee object (see L<FS::access_user>).
244 sub void_access_user {
246 qsearchs('access_user', { 'usernum' => $self->void_usernum } );
255 qsearchs('cust_main', { 'custnum' => $self->custnum } );
262 sub cust_bill_pkg { #actually cust_bill_pkg_void objects
264 qsearch('cust_bill_pkg_void', { invnum=>$self->invnum });
271 Returns the packages (see L<FS::cust_pkg>) corresponding to the line items for
278 my @cust_pkg = map { $_->pkgnum > 0 ? $_->cust_pkg : () }
279 $self->cust_bill_pkg;
281 grep { ! $saw{$_->pkgnum}++ } @cust_pkg;
284 =item search_sql_where HASHREF
286 Class method which returns an SQL WHERE fragment to search for parameters
287 specified in HASHREF. Accepts the following parameters for
288 L<FS::cust_bill::search_sql_where>: C<_date>, C<invnum_min>, C<invnum_max>,
289 C<agentnum>, C<custnum>, C<cust_classnum>, C<refnum>, C<payby>. Also
290 accepts the following:
296 Arrayref of start and end date to find invoices voided in a date range.
300 User identifier (L<FS::access_user> key) that voided the invoice.
306 sub search_sql_where {
307 my($class, $param) = @_;
309 my $cust_bill_param = {
310 map { $_ => $param->{$_} }
311 grep { exists($param->{$_}) }
312 qw( _date invnum_min invnum_max agentnum custnum cust_classnum
315 my $search_sql = FS::cust_bill->search_sql_where($cust_bill_param);
316 $search_sql =~ s/cust_bill/cust_bill_void/g;
317 my @search = ($search_sql);
319 if ( $param->{void_date} ) {
320 my($beginning, $ending) = @{$param->{void_date}};
321 push @search, "cust_bill_void.void_date >= $beginning",
322 "cust_bill_void.void_date < $ending";
325 if ( $param->{void_usernum} =~ /^(\d+)$/ ) {
327 push @search, "cust_bill_void.void_usernum = $1";
330 join(" AND ", @search);
334 =item enable_previous
338 sub enable_previous { 0 }
346 L<FS::Record>, schema.html from the base documentation.