4 use base qw( FS::Record );
5 use FS::Record qw( dbh qsearch qsearchs );
12 FS::vend_bill - Object methods for vend_bill records
18 $record = new FS::vend_bill \%hash;
19 $record = new FS::vend_bill { 'column' => 'value' };
21 $error = $record->insert;
23 $error = $new_record->replace($old_record);
25 $error = $record->delete;
27 $error = $record->check;
31 An FS::vend_bill object represents a vendor invoice or payable. FS::vend_bill
32 inherits from FS::Record. The following fields are currently supported:
61 Creates a new record. To add the record to the database, see L<"insert">.
63 Note that this stores the hash reference, not a distinct copy of the hash it
64 points to. You can ask the object for a copy with the I<hash> method.
68 # the new method can be inherited from FS::Record, if a table method is defined
70 sub table { 'vend_bill'; }
74 Adds this record to the database. If there is an error, returns the error,
75 otherwise returns false.
82 my $oldAutoCommit = $FS::UID::AutoCommit;
83 local $FS::UID::AutoCommit = 0;
86 my $error = $self->SUPER::insert;
88 $dbh->rollback if $oldAutoCommit;
89 return "inserting vend_bill: $error";
92 #magically auto-inserting for the simple case
93 my $vend_pay = new FS::vend_pay {
94 'vendnum' => $self->vendnum,
95 'vendbillnum' => $self->vendbillnum,
96 '_date' => $self->get('payment_date') || $self->_date,
97 'paid' => $self->charged,
100 $error = $vend_pay->insert;
102 $dbh->rollback if $oldAutoCommit;
103 return "auto-inserting vend_pay: $error";
106 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
113 Delete this record from the database.
120 my $oldAutoCommit = $FS::UID::AutoCommit;
121 local $FS::UID::AutoCommit = 0;
124 foreach my $vend_bill_pay ( $self->vend_bill_pay ) {
125 my $error = $vend_bill_pay->delete;
127 $dbh->rollback if $oldAutoCommit;
132 my $error = $self->SUPER::delete;
134 $dbh->rollback if $oldAutoCommit;
138 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
143 =item replace OLD_RECORD
145 Replaces the OLD_RECORD with this one in the database. If there is an error,
146 returns the error, otherwise returns false.
150 Checks all fields to make sure this is a valid record. If there is
151 an error, returns the error, otherwise returns false. Called by the insert
160 $self->ut_numbern('vendbillnum')
161 || $self->ut_foreign_key('vendnum', 'vend_main', 'vendnum')
162 || $self->ut_numbern('_date')
163 || $self->ut_money('charged')
165 return $error if $error;
176 qsearchs('vend_main', { 'vendnum', $self->vendnum });
185 qsearch('vend_bill_pay', { 'vendbillnum', $self->vendbillnum });
193 my ($class, $param) = @_;
199 if ( $param->{_date} ) {
200 my($beginning, $ending) = @{$param->{_date}};
202 push @where, "vend_bill._date >= $beginning",
203 "vend_bill._date < $ending";
207 if ( $param->{payment_date} ) {
208 my($beginning, $ending) = @{$param->{payment_date}};
210 push @where, "vend_pay._date >= $beginning",
211 "vend_pay._date < $ending";
214 if ( $param->{'classnum'} =~ /^(\d+)$/ ) {
215 #also simplistic, but good for now
216 $addl_from .= ' LEFT JOIN vend_main USING (vendnum) ';
217 push @where, "vend_main.classnum = $1";
220 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
222 #simplistic, but how we are for now
223 $addl_from .= ' LEFT JOIN vend_bill_pay USING (vendbillnum) '.
224 ' LEFT JOIN vend_pay USING (vendpaynum) ';
226 my $count_query = "SELECT COUNT(*), SUM(charged) FROM vend_bill $addl_from $extra_sql";
229 'table' => 'vend_bill',
230 'select' => 'vend_bill.*, vend_pay._date as payment_date',
231 'addl_from' => $addl_from,
233 'extra_sql' => $extra_sql,
234 'order_by' => 'ORDER BY _date',
235 'count_query' => $count_query,
236 #'extra_headers' => \@extra_headers,
237 #'extra_fields' => \@extra_fields,
247 L<FS::Record>, schema.html from the base documentation.