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 #XXXsub template_conf { 'quotation_'; }
115 Adds this record to the database. If there is an error, returns the error,
116 otherwise returns false.
122 "Un-void"s this invoice: Deletes the voided invoice from the database and adds
123 back a normal invoice (and related tables).
130 local $SIG{HUP} = 'IGNORE';
131 local $SIG{INT} = 'IGNORE';
132 local $SIG{QUIT} = 'IGNORE';
133 local $SIG{TERM} = 'IGNORE';
134 local $SIG{TSTP} = 'IGNORE';
135 local $SIG{PIPE} = 'IGNORE';
137 my $oldAutoCommit = $FS::UID::AutoCommit;
138 local $FS::UID::AutoCommit = 0;
141 my $cust_bill = new FS::cust_bill ( {
142 map { $_ => $self->get($_) } fields('cust_bill')
144 my $error = $cust_bill->insert;
146 $dbh->rollback if $oldAutoCommit;
150 foreach my $cust_bill_pkg_void ( $self->cust_bill_pkg ) {
151 my $error = $cust_bill_pkg_void->unvoid;
153 $dbh->rollback if $oldAutoCommit;
158 $error = $self->delete;
160 $dbh->rollback if $oldAutoCommit;
164 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
172 Delete this record from the database.
176 =item replace OLD_RECORD
178 Replaces the OLD_RECORD with this one in the database. If there is an error,
179 returns the error, otherwise returns false.
185 Checks all fields to make sure this is a valid voided invoice. If there is
186 an error, returns the error, otherwise returns false. Called by the insert
195 $self->ut_number('invnum')
196 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum' )
197 || $self->ut_numbern('_date')
198 || $self->ut_money('charged')
199 || $self->ut_textn('invoice_terms')
200 || $self->ut_moneyn('previous_balance')
201 || $self->ut_moneyn('billing_balance')
202 || $self->ut_enum('closed', [ '', 'Y' ])
203 || $self->ut_foreign_keyn('statementnum', 'cust_statement', 'statementnum')
204 || $self->ut_numbern('agent_invid')
205 || $self->ut_numbern('promised_date')
206 || $self->ut_numbern('void_date')
207 || $self->ut_textn('reason')
208 || $self->ut_numbern('void_usernum')
210 return $error if $error;
212 $self->void_date(time) unless $self->void_date;
214 $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
215 unless $self->void_usernum;
222 Returns the displayed invoice number for this invoice: agent_invid if
223 cust_bill-default_agent_invid is set and it has a value, invnum otherwise.
229 my $conf = $self->conf;
230 if ( $conf->exists('cust_bill-default_agent_invid') && $self->agent_invid ){
231 return $self->agent_invid;
233 return $self->invnum;
237 =item void_access_user
239 Returns the voiding employee object (see L<FS::access_user>).
243 sub void_access_user {
245 qsearchs('access_user', { 'usernum' => $self->void_usernum } );
254 sub cust_bill_pkg { #actually cust_bill_pkg_void objects
256 qsearch('cust_bill_pkg_void', { invnum=>$self->invnum });
263 Returns the packages (see L<FS::cust_pkg>) corresponding to the line items for
270 my @cust_pkg = map { $_->pkgnum > 0 ? $_->cust_pkg : () }
271 $self->cust_bill_pkg;
273 grep { ! $saw{$_->pkgnum}++ } @cust_pkg;
276 =item search_sql_where HASHREF
278 Class method which returns an SQL WHERE fragment to search for parameters
279 specified in HASHREF. Accepts the following parameters for
280 L<FS::cust_bill::search_sql_where>: C<_date>, C<invnum_min>, C<invnum_max>,
281 C<agentnum>, C<custnum>, C<cust_classnum>, C<refnum>. Also
282 accepts the following:
288 Arrayref of start and end date to find invoices voided in a date range.
292 User identifier (L<FS::access_user> key) that voided the invoice.
298 sub search_sql_where {
299 my($class, $param) = @_;
301 my $cust_bill_param = {
302 map { $_ => $param->{$_} }
303 grep { exists($param->{$_}) }
304 qw( _date invnum_min invnum_max agentnum custnum cust_classnum
307 my $search_sql = FS::cust_bill->search_sql_where($cust_bill_param);
308 $search_sql =~ s/cust_bill/cust_bill_void/g;
309 my @search = ($search_sql);
311 if ( $param->{void_date} ) {
312 my($beginning, $ending) = @{$param->{void_date}};
313 push @search, "cust_bill_void.void_date >= $beginning",
314 "cust_bill_void.void_date < $ending";
317 if ( $param->{void_usernum} =~ /^(\d+)$/ ) {
319 push @search, "cust_bill_void.void_usernum = $1";
322 join(" AND ", @search);
326 =item enable_previous
330 sub enable_previous { 0 }
338 L<FS::Record>, schema.html from the base documentation.