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