default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / FS / vend_pay.pm
1 package FS::vend_pay;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( dbh ); #qsearch qsearchs );
6 use FS::vend_bill_pay;
7
8 =head1 NAME
9
10 FS::vend_pay - Object methods for vend_pay records
11
12 =head1 SYNOPSIS
13
14   use FS::vend_pay;
15
16   $record = new FS::vend_pay \%hash;
17   $record = new FS::vend_pay { '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_pay object represents a vendor payment.  FS::vend_pay inherits from
30 FS::Record.  The following fields are currently supported:
31
32 =over 4
33
34 =item vendpaynum
35
36 primary key
37
38 =item vendnum
39
40 vendnum
41
42 =item _date
43
44 _date
45
46 =item paid
47
48 paid
49
50
51 =back
52
53 =head1 METHODS
54
55 =over 4
56
57 =item new HASHREF
58
59 Creates a new payment.  To add the payment 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_pay'; }
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 =cut
75
76 sub insert {
77   my $self = shift;
78
79   my $oldAutoCommit = $FS::UID::AutoCommit;
80   local $FS::UID::AutoCommit = 0;
81   my $dbh = dbh;
82
83   my $error = $self->SUPER::insert;
84   if ( $error ) {
85     $dbh->rollback if $oldAutoCommit;
86     return "inserting vend_pay: $error";
87   }
88
89   if ( $self->get('vendbillnum') ) {
90
91     my $vend_bill_pay = new FS::vend_bill_pay {
92       'vendpaynum'  => $self->vendpaynum,
93       'vendbillnum' => $self->get('vendbillnum'),
94       'amount'      => $self->paid,
95     };
96
97     $error = $vend_bill_pay->insert;
98     if ( $error ) {
99       $dbh->rollback if $oldAutoCommit;
100       return "auto-inserting vend_bill_pay: $error";
101     }
102
103   }
104
105   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
106   '';
107
108 }
109
110 =item delete
111
112 Delete this record from the database.
113
114 =item replace OLD_RECORD
115
116 Replaces the OLD_RECORD with this one in the database.  If there is an error,
117 returns the error, otherwise returns false.
118
119 =item check
120
121 Checks all fields to make sure this is a valid payment.  If there is
122 an error, returns the error, otherwise returns false.  Called by the insert
123 and replace methods.
124
125 =cut
126
127 sub check {
128   my $self = shift;
129
130   my $error = 
131     $self->ut_numbern('vendpaynum')
132     || $self->ut_foreign_key('vendnum', 'vend_main', 'vendnum')
133     || $self->ut_numbern('_date')
134     || $self->ut_money('paid')
135   ;
136   return $error if $error;
137
138   $self->SUPER::check;
139 }
140
141 =back
142
143 =head1 BUGS
144
145 =head1 SEE ALSO
146
147 L<FS::Record>, schema.html from the base documentation.
148
149 =cut
150
151 1;
152