simple A/P
[freeside.git] / FS / FS / vend_bill.pm
1 package FS::vend_bill;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( dbh qsearch qsearchs );
6 use FS::vend_main;
7 use FS::vend_pay;
8 use FS::vend_bill_pay;
9
10 =head1 NAME
11
12 FS::vend_bill - Object methods for vend_bill records
13
14 =head1 SYNOPSIS
15
16   use FS::vend_bill;
17
18   $record = new FS::vend_bill \%hash;
19   $record = new FS::vend_bill { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
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:
33
34 =over 4
35
36 =item vendbillnum
37
38 primary key
39
40 =item vendnum
41
42 vendnum
43
44 =item _date
45
46 _date
47
48 =item charged
49
50 charged
51
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new record.  To add the record to the database, see L<"insert">.
62
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.
65
66 =cut
67
68 # the new method can be inherited from FS::Record, if a table method is defined
69
70 sub table { 'vend_bill'; }
71
72 =item insert
73
74 Adds this record to the database.  If there is an error, returns the error,
75 otherwise returns false.
76
77 =cut
78
79 sub insert {
80   my $self = shift;
81
82   my $oldAutoCommit = $FS::UID::AutoCommit;
83   local $FS::UID::AutoCommit = 0;
84   my $dbh = dbh;
85
86   my $error = $self->SUPER::insert;
87   if ( $error ) {
88     $dbh->rollback if $oldAutoCommit;
89     return "inserting vend_bill: $error";
90   }
91
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,
98   };
99
100   $error = $vend_pay->insert;
101   if ( $error ) {
102     $dbh->rollback if $oldAutoCommit;
103     return "auto-inserting vend_pay: $error";
104   }
105
106   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
107   '';
108
109 }
110
111 =item delete
112
113 Delete this record from the database.
114
115 =cut
116
117 sub delete {
118   my $self = shift;
119
120   my $oldAutoCommit = $FS::UID::AutoCommit;
121   local $FS::UID::AutoCommit = 0;
122   my $dbh = dbh;
123
124   foreach my $vend_bill_pay ( $self->vend_bill_pay ) {
125     my $error = $vend_bill_pay->delete;
126     if ( $error ) {
127       $dbh->rollback if $oldAutoCommit;
128       return $error;
129     }
130   }
131
132   my $error = $self->SUPER::delete;
133   if ( $error ) {
134     $dbh->rollback if $oldAutoCommit;
135     return $error;
136   }
137
138   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
139   '';
140
141 }
142
143 =item replace OLD_RECORD
144
145 Replaces the OLD_RECORD with this one in the database.  If there is an error,
146 returns the error, otherwise returns false.
147
148 =item check
149
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
152 and replace methods.
153
154 =cut
155
156 sub check {
157   my $self = shift;
158
159   my $error = 
160     $self->ut_numbern('vendbillnum')
161     || $self->ut_foreign_key('vendnum', 'vend_main', 'vendnum')
162     || $self->ut_numbern('_date')
163     || $self->ut_money('charged')
164   ;
165   return $error if $error;
166
167   $self->SUPER::check;
168 }
169
170 =item vend_main
171
172 =cut
173
174 sub vend_main {
175   my $self = shift;
176   qsearchs('vend_main', { 'vendnum', $self->vendnum });
177 }
178
179 =item vend_bill_pay
180
181 =cut
182
183 sub vend_bill_pay {
184   my $self = shift;
185   qsearch('vend_bill_pay', { 'vendbillnum', $self->vendbillnum });
186 }
187
188 =item search
189
190 =cut
191
192 sub search {
193   my ($class, $param) = @_;
194
195   my @where = ();
196
197   #_date
198   if ( $param->{_date} ) {
199     my($beginning, $ending) = @{$param->{_date}};
200
201     push @where, "vend_bill._date >= $beginning",
202                  "vend_bill._date <  $ending";
203   }
204
205   my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
206
207   #my $count_query = "SELECT COUNT(*) FROM vend_bill $addl_from $extra_sql";
208   my $count_query = "SELECT COUNT(*), SUM(charged) FROM vend_bill $extra_sql";
209
210   +{
211     'table'         => 'vend_bill',
212     #'select'        => $select,
213     #'addl_from'     => $addl_from,
214     'hashref'       => {},
215     'extra_sql'     => $extra_sql,
216     'order_by'      => 'ORDER BY _date',
217     'count_query'   => $count_query,
218     #'extra_headers' => \@extra_headers,
219     #'extra_fields'  => \@extra_fields,
220   };
221 }
222
223 =back
224
225 =head1 BUGS
226
227 =head1 SEE ALSO
228
229 L<FS::Record>, schema.html from the base documentation.
230
231 =cut
232
233 1;
234