Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / cust_bill_pkg_void.pm
1 package FS::cust_bill_pkg_void;
2 use base qw( FS::TemplateItem_Mixin FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearch qsearchs dbh fields );
6 use FS::cust_bill_void;
7 use FS::cust_bill_pkg_detail;
8 use FS::cust_bill_pkg_display;
9 use FS::cust_bill_pkg_discount;
10 use FS::cust_bill_pkg;
11 use FS::cust_bill_pkg_tax_location;
12 use FS::cust_bill_pkg_tax_rate_location;
13 use FS::cust_tax_exempt_pkg;
14
15 =head1 NAME
16
17 FS::cust_bill_pkg_void - Object methods for cust_bill_pkg_void records
18
19 =head1 SYNOPSIS
20
21   use FS::cust_bill_pkg_void;
22
23   $record = new FS::cust_bill_pkg_void \%hash;
24   $record = new FS::cust_bill_pkg_void { 'column' => 'value' };
25
26   $error = $record->insert;
27
28   $error = $new_record->replace($old_record);
29
30   $error = $record->delete;
31
32   $error = $record->check;
33
34 =head1 DESCRIPTION
35
36 An FS::cust_bill_pkg_void object represents a voided invoice line item.
37 FS::cust_bill_pkg_void inherits from FS::Record.  The following fields are
38 currently supported:
39
40 =over 4
41
42 =item billpkgnum
43
44 primary key
45
46 =item invnum
47
48 invnum
49
50 =item pkgnum
51
52 pkgnum
53
54 =item pkgpart_override
55
56 pkgpart_override
57
58 =item setup
59
60 setup
61
62 =item recur
63
64 recur
65
66 =item sdate
67
68 sdate
69
70 =item edate
71
72 edate
73
74 =item itemdesc
75
76 itemdesc
77
78 =item itemcomment
79
80 itemcomment
81
82 =item section
83
84 section
85
86 =item freq
87
88 freq
89
90 =item quantity
91
92 quantity
93
94 =item unitsetup
95
96 unitsetup
97
98 =item unitrecur
99
100 unitrecur
101
102 =item hidden
103
104 hidden
105
106
107 =back
108
109 =head1 METHODS
110
111 =over 4
112
113 =item new HASHREF
114
115 Creates a new record.  To add the record to the database, see L<"insert">.
116
117 Note that this stores the hash reference, not a distinct copy of the hash it
118 points to.  You can ask the object for a copy with the I<hash> method.
119
120 =cut
121
122 sub table { 'cust_bill_pkg_void'; }
123
124 sub detail_table            { 'cust_bill_pkg_detail_void'; }
125 sub display_table           { 'cust_bill_pkg_display_void'; }
126 sub discount_table          { 'cust_bill_pkg_discount_void'; }
127 #sub tax_location_table      { 'cust_bill_pkg_tax_location'; }
128 #sub tax_rate_location_table { 'cust_bill_pkg_tax_rate_location'; }
129 #sub tax_exempt_pkg_table    { 'cust_tax_exempt_pkg'; }
130
131 =item insert
132
133 Adds this record to the database.  If there is an error, returns the error,
134 otherwise returns false.
135
136 =item unvoid 
137
138 "Un-void"s this line item: Deletes the voided line item from the database and
139 adds back a normal line item (and related tables).
140
141 =cut
142
143 sub unvoid {
144   my $self = shift;
145
146   local $SIG{HUP} = 'IGNORE';
147   local $SIG{INT} = 'IGNORE';
148   local $SIG{QUIT} = 'IGNORE';
149   local $SIG{TERM} = 'IGNORE';
150   local $SIG{TSTP} = 'IGNORE';
151   local $SIG{PIPE} = 'IGNORE';
152
153   my $oldAutoCommit = $FS::UID::AutoCommit;
154   local $FS::UID::AutoCommit = 0;
155   my $dbh = dbh;
156
157   my $cust_bill_pkg = new FS::cust_bill_pkg ( {
158     map { $_ => $self->get($_) } fields('cust_bill_pkg')
159   } );
160   my $error = $cust_bill_pkg->insert;
161   if ( $error ) {
162     $dbh->rollback if $oldAutoCommit;
163     return $error;
164   }
165
166   foreach my $table (qw(
167     cust_bill_pkg_detail
168     cust_bill_pkg_display
169     cust_bill_pkg_discount
170     cust_bill_pkg_tax_location
171     cust_bill_pkg_tax_rate_location
172     cust_tax_exempt_pkg
173   )) {
174
175     foreach my $voided (
176       qsearch($table.'_void', { billpkgnum=>$self->billpkgnum })
177     ) {
178
179       my $class = 'FS::'.$table;
180       my $unvoid = $class->new( {
181         map { $_ => $voided->get($_) } fields($table)
182       });
183       my $error = $unvoid->insert || $voided->delete;
184       if ( $error ) {
185         $dbh->rollback if $oldAutoCommit;
186         return $error;
187       }
188
189     }
190
191   }
192
193   $error = $self->delete;
194   if ( $error ) {
195     $dbh->rollback if $oldAutoCommit;
196     return $error;
197   }
198
199   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
200
201   '';
202
203 }
204
205 =item delete
206
207 Delete this record from the database.
208
209 =item replace OLD_RECORD
210
211 Replaces the OLD_RECORD with this one in the database.  If there is an error,
212 returns the error, otherwise returns false.
213
214 =item check
215
216 Checks all fields to make sure this is a valid record.  If there is
217 an error, returns the error, otherwise returns false.  Called by the insert
218 and replace methods.
219
220 =cut
221
222 sub check {
223   my $self = shift;
224
225   my $error = 
226     $self->ut_number('billpkgnum')
227     || $self->ut_snumber('pkgnum')
228     || $self->ut_number('invnum') #cust_bill or cust_bill_void, if we ever support line item voiding
229     || $self->ut_numbern('pkgpart_override')
230     || $self->ut_money('setup')
231     || $self->ut_money('recur')
232     || $self->ut_numbern('sdate')
233     || $self->ut_numbern('edate')
234     || $self->ut_textn('itemdesc')
235     || $self->ut_textn('itemcomment')
236     || $self->ut_textn('section')
237     || $self->ut_textn('freq')
238     || $self->ut_numbern('quantity')
239     || $self->ut_moneyn('unitsetup')
240     || $self->ut_moneyn('unitrecur')
241     || $self->ut_enum('hidden', [ '', 'Y' ])
242   ;
243   return $error if $error;
244
245   $self->SUPER::check;
246 }
247
248 =item cust_bill
249
250 Returns the voided invoice (see L<FS::cust_bill_void>) for this voided line
251 item.
252
253 =cut
254
255 sub cust_bill {
256   my $self = shift;
257   #cust_bill or cust_bill_void, if we ever support line item voiding
258   qsearchs( 'cust_bill_void', { 'invnum' => $self->invnum } );
259 }
260
261 =back
262
263 =head1 BUGS
264
265 =head1 SEE ALSO
266
267 L<FS::Record>, schema.html from the base documentation.
268
269 =cut
270
271 1;
272