2 use base qw(FS::Record);
5 use FS::Record qw( dbh ); # qsearch qsearchs );
10 FS::vend_bill - Object methods for vend_bill records
16 $record = new FS::vend_bill \%hash;
17 $record = new FS::vend_bill { 'column' => 'value' };
19 $error = $record->insert;
21 $error = $new_record->replace($old_record);
23 $error = $record->delete;
25 $error = $record->check;
29 An FS::vend_bill object represents a vendor invoice or payable. FS::vend_bill
30 inherits from FS::Record. The following fields are currently supported:
59 Creates a new record. To add the record to the database, see L<"insert">.
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to. You can ask the object for a copy with the I<hash> method.
66 # the new method can be inherited from FS::Record, if a table method is defined
68 sub table { 'vend_bill'; }
72 Adds this record to the database. If there is an error, returns the error,
73 otherwise returns false.
80 my $oldAutoCommit = $FS::UID::AutoCommit;
81 local $FS::UID::AutoCommit = 0;
84 my $error = $self->SUPER::insert;
86 $dbh->rollback if $oldAutoCommit;
87 return "inserting vend_bill: $error";
90 #magically auto-inserting for the simple case
91 my $vend_pay = new FS::vend_pay {
92 'vendnum' => $self->vendnum,
93 'vendbillnum' => $self->vendbillnum,
94 '_date' => $self->get('payment_date') || $self->_date,
95 'paid' => $self->charged,
98 $error = $vend_pay->insert;
100 $dbh->rollback if $oldAutoCommit;
101 return "auto-inserting vend_pay: $error";
104 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
111 Delete this record from the database.
118 my $oldAutoCommit = $FS::UID::AutoCommit;
119 local $FS::UID::AutoCommit = 0;
122 foreach my $vend_bill_pay ( $self->vend_bill_pay ) {
123 my $error = $vend_bill_pay->delete;
125 $dbh->rollback if $oldAutoCommit;
130 my $error = $self->SUPER::delete;
132 $dbh->rollback if $oldAutoCommit;
136 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
141 =item replace OLD_RECORD
143 Replaces the OLD_RECORD with this one in the database. If there is an error,
144 returns the error, otherwise returns false.
148 Checks all fields to make sure this is a valid record. If there is
149 an error, returns the error, otherwise returns false. Called by the insert
158 $self->ut_numbern('vendbillnum')
159 || $self->ut_foreign_key('vendnum', 'vend_main', 'vendnum')
160 || $self->ut_numbern('_date')
161 || $self->ut_money('charged')
163 return $error if $error;
177 my ($class, $param) = @_;
183 if ( $param->{_date} ) {
184 my($beginning, $ending) = @{$param->{_date}};
186 push @where, "vend_bill._date >= $beginning",
187 "vend_bill._date < $ending";
191 if ( $param->{payment_date} ) {
192 my($beginning, $ending) = @{$param->{payment_date}};
194 push @where, "vend_pay._date >= $beginning",
195 "vend_pay._date < $ending";
198 if ( $param->{'classnum'} =~ /^(\d+)$/ ) {
199 #also simplistic, but good for now
200 $addl_from .= ' LEFT JOIN vend_main USING (vendnum) ';
201 push @where, "vend_main.classnum = $1";
204 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
206 #simplistic, but how we are for now
207 $addl_from .= ' LEFT JOIN vend_bill_pay USING (vendbillnum) '.
208 ' LEFT JOIN vend_pay USING (vendpaynum) ';
210 my $count_query = "SELECT COUNT(*), SUM(charged) FROM vend_bill $addl_from $extra_sql";
213 'table' => 'vend_bill',
214 'select' => 'vend_bill.*, vend_pay._date as payment_date',
215 'addl_from' => $addl_from,
217 'extra_sql' => $extra_sql,
218 'order_by' => 'ORDER BY _date',
219 'count_query' => $count_query,
220 #'extra_headers' => \@extra_headers,
221 #'extra_fields' => \@extra_fields,
231 L<FS::Record>, schema.html from the base documentation.