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