fix attribute importing bugs that borked the passwords
[freeside.git] / FS / FS / cust_bill_pkg.pm
1 package FS::cust_bill_pkg;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs dbdef dbh );
6 use FS::cust_pkg;
7 use FS::cust_bill;
8 use FS::cust_bill_pkg_detail;
9
10 @ISA = qw( FS::Record );
11
12 =head1 NAME
13
14 FS::cust_bill_pkg - Object methods for cust_bill_pkg records
15
16 =head1 SYNOPSIS
17
18   use FS::cust_bill_pkg;
19
20   $record = new FS::cust_bill_pkg \%hash;
21   $record = new FS::cust_bill_pkg { 'column' => 'value' };
22
23   $error = $record->insert;
24
25   $error = $new_record->replace($old_record);
26
27   $error = $record->delete;
28
29   $error = $record->check;
30
31 =head1 DESCRIPTION
32
33 An FS::cust_bill_pkg object represents an invoice line item.
34 FS::cust_bill_pkg inherits from FS::Record.  The following fields are currently
35 supported:
36
37 =over 4
38
39 =item invnum - invoice (see L<FS::cust_bill>)
40
41 =item pkgnum - package (see L<FS::cust_pkg>) or 0 for the special virtual sales tax package
42
43 =item setup - setup fee
44
45 =item recur - recurring fee
46
47 =item sdate - starting date of recurring fee
48
49 =item edate - ending date of recurring fee
50
51 =item itemdesc - Line item description (currentlty used only when pkgnum is 0)
52
53 =back
54
55 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">.  Also
56 see L<Time::Local> and L<Date::Parse> for conversion functions.
57
58 =head1 METHODS
59
60 =over 4
61
62 =item new HASHREF
63
64 Creates a new line item.  To add the line item to the database, see
65 L<"insert">.  Line items are normally created by calling the bill method of a
66 customer object (see L<FS::cust_main>).
67
68 =cut
69
70 sub table { 'cust_bill_pkg'; }
71
72 =item insert
73
74 Adds this line item 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   local $SIG{HUP} = 'IGNORE';
83   local $SIG{INT} = 'IGNORE';
84   local $SIG{QUIT} = 'IGNORE';
85   local $SIG{TERM} = 'IGNORE';
86   local $SIG{TSTP} = 'IGNORE';
87   local $SIG{PIPE} = 'IGNORE';
88
89   my $oldAutoCommit = $FS::UID::AutoCommit;
90   local $FS::UID::AutoCommit = 0;
91   my $dbh = dbh;
92
93   my $error = $self->SUPER::insert;
94   if ( $error ) {
95     $dbh->rollback if $oldAutoCommit;
96     return $error;
97   }
98
99   unless ( defined dbdef->table('cust_bill_pkg_detail') && $self->get('details') ) {
100     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
101     return '';
102   }
103
104   foreach my $detail ( @{$self->get('details')} ) {
105     my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail {
106       'pkgnum' => $self->pkgnum,
107       'invnum' => $self->invnum,
108       'detail' => $detail,
109     };
110     $error = $cust_bill_pkg_detail->insert;
111     if ( $error ) {
112       $dbh->rollback if $oldAutoCommit;
113       return $error;
114     }
115   }
116
117   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
118   '';
119
120 }
121
122 =item delete
123
124 Currently unimplemented.  I don't remove line items because there would then be
125 no record the items ever existed (which is bad, no?)
126
127 =cut
128
129 sub delete {
130   return "Can't delete cust_bill_pkg records!";
131 }
132
133 =item replace OLD_RECORD
134
135 Currently unimplemented.  This would be even more of an accounting nightmare
136 than deleteing the items.  Just don't do it.
137
138 =cut
139
140 sub replace {
141   return "Can't modify cust_bill_pkg records!";
142 }
143
144 =item check
145
146 Checks all fields to make sure this is a valid line item.  If there is an
147 error, returns the error, otherwise returns false.  Called by the insert
148 method.
149
150 =cut
151
152 sub check {
153   my $self = shift;
154
155   my $error =
156     $self->ut_number('pkgnum')
157       || $self->ut_number('invnum')
158       || $self->ut_money('setup')
159       || $self->ut_money('recur')
160       || $self->ut_numbern('sdate')
161       || $self->ut_numbern('edate')
162       || $self->ut_textn('itemdesc')
163   ;
164   return $error if $error;
165
166   if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
167     return "Unknown pkgnum ". $self->pkgnum
168       unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
169   }
170
171   return "Unknown invnum"
172     unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
173
174   $self->SUPER::check;
175 }
176
177 =item cust_pkg
178
179 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
180
181 =cut
182
183 sub cust_pkg {
184   my $self = shift;
185   qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
186 }
187
188 =item details
189
190 Returns an array of detail information for the invoice line item.
191
192 =cut
193
194 sub details {
195   my $self = shift;
196   return () unless defined dbdef->table('cust_bill_pkg_detail');
197   map { $_->detail }
198     qsearch ( 'cust_bill_pkg_detail', { 'pkgnum' => $self->pkgnum,
199                                         'invnum' => $self->invnum, } );
200     #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
201 }
202
203 =back
204
205 =head1 BUGS
206
207 =head1 SEE ALSO
208
209 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
210 from the base documentation.
211
212 =cut
213
214 1;
215