simple A/P
[freeside.git] / FS / FS / vend_bill_pay.pm
1 package FS::vend_bill_pay;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( dbh qsearch ); #qsearchs );
6 use FS::vend_bill;
7 use FS::vend_pay;
8
9 =head1 NAME
10
11 FS::vend_bill_pay - Object methods for vend_bill_pay records
12
13 =head1 SYNOPSIS
14
15   use FS::vend_bill_pay;
16
17   $record = new FS::vend_bill_pay \%hash;
18   $record = new FS::vend_bill_pay { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::vend_bill_pay object represents the application of a vendor payment to a
31 specific invoice or payment. FS::vend_bill_pay inherits from FS::Record.  The
32 following fields are currently supported:
33
34 =over 4
35
36 =item vendbillpaynum
37
38 primary key
39
40 =item vendbillnum
41
42 vendbillnum
43
44 =item vendpaynum
45
46 vendpaynum
47
48 =item amount
49
50 amount
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 sub table { 'vend_bill_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
75 =item delete
76
77 Delete this record from the database.
78
79 =cut
80
81 sub delete {
82   my $self = shift;
83
84   my $oldAutoCommit = $FS::UID::AutoCommit;
85   local $FS::UID::AutoCommit = 0;
86   my $dbh = dbh;
87
88   #magically auto-deleting for the simple case
89   foreach my $vend_pay ( $self->vend_pay ) {
90     my $error = $vend_pay->delete;
91     if ( $error ) {
92       $dbh->rollback if $oldAutoCommit;
93       return $error;
94     }
95   }
96
97   my $error = $self->SUPER::delete;
98   if ( $error ) {
99     $dbh->rollback if $oldAutoCommit;
100     return $error;
101   }
102
103   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
104   '';
105
106 }
107
108
109 =item replace OLD_RECORD
110
111 Replaces the OLD_RECORD with this one in the database.  If there is an error,
112 returns the error, otherwise returns false.
113
114 =item check
115
116 Checks all fields to make sure this is a valid record.  If there is
117 an error, returns the error, otherwise returns false.  Called by the insert
118 and replace methods.
119
120 =cut
121
122 sub check {
123   my $self = shift;
124
125   my $error = 
126     $self->ut_numbern('vendbillpaynum')
127     || $self->ut_foreign_key('vendbillnum', 'vend_bill', 'vendbillnum')
128     || $self->ut_foreign_key('vendpaynum', 'vend_pay', 'vendpaynum')
129     || $self->ut_money('amount')
130   ;
131   return $error if $error;
132
133   $self->SUPER::check;
134 }
135
136 =item vend_pay
137
138 =cut
139
140 sub vend_pay {
141   my $self = shift;
142   qsearch('vend_pay', { 'vendpaynum', $self->vendpaynum });
143 }
144
145 =back
146
147 =head1 BUGS
148
149 =head1 SEE ALSO
150
151 L<FS::Record>, schema.html from the base documentation.
152
153 =cut
154
155 1;
156