4b9cffd4e172dcafe3fd9bca81f9a3852b6ed9e5
[freeside.git] / FS / FS / cust_bill_pkg_void.pm
1 package FS::cust_bill_pkg_void;
2 use base qw( FS::TemplateItem_Mixin FS::reason_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 =item reason 
108
109 freeform string (deprecated)
110
111 =item reasonnum 
112
113 reason for voiding the payment (see L<FS::reson>)
114
115 =back
116
117 =head1 METHODS
118
119 =over 4
120
121 =item new HASHREF
122
123 Creates a new record.  To add the record to the database, see L<"insert">.
124
125 Note that this stores the hash reference, not a distinct copy of the hash it
126 points to.  You can ask the object for a copy with the I<hash> method.
127
128 =cut
129
130 sub table { 'cust_bill_pkg_void'; }
131
132 sub detail_table            { 'cust_bill_pkg_detail_void'; }
133 sub display_table           { 'cust_bill_pkg_display_void'; }
134 sub discount_table          { 'cust_bill_pkg_discount_void'; }
135 #sub tax_location_table      { 'cust_bill_pkg_tax_location'; }
136 #sub tax_rate_location_table { 'cust_bill_pkg_tax_rate_location'; }
137 #sub tax_exempt_pkg_table    { 'cust_tax_exempt_pkg'; }
138
139 =item insert
140
141 Adds this record to the database.  If there is an error, returns the error,
142 otherwise returns false.
143
144 =item reason
145
146 Returns the text of the associated void reason (see L<FS::reason>) for this.
147
148 =item unvoid 
149
150 "Un-void"s this line item: Deletes the voided line item from the database and
151 adds back a normal line item (and related tables).
152
153 =cut
154
155 sub unvoid {
156   my $self = shift;
157
158   local $SIG{HUP} = 'IGNORE';
159   local $SIG{INT} = 'IGNORE';
160   local $SIG{QUIT} = 'IGNORE';
161   local $SIG{TERM} = 'IGNORE';
162   local $SIG{TSTP} = 'IGNORE';
163   local $SIG{PIPE} = 'IGNORE';
164
165   my $oldAutoCommit = $FS::UID::AutoCommit;
166   local $FS::UID::AutoCommit = 0;
167   my $dbh = dbh;
168
169   my $cust_bill_pkg = new FS::cust_bill_pkg ( {
170     map { $_ => $self->get($_) } fields('cust_bill_pkg')
171   } );
172   my $error = $cust_bill_pkg->insert;
173   if ( $error ) {
174     $dbh->rollback if $oldAutoCommit;
175     return $error;
176   }
177
178   foreach my $table (qw(
179     cust_bill_pkg_detail
180     cust_bill_pkg_display
181     cust_bill_pkg_discount
182     cust_bill_pkg_tax_location
183     cust_bill_pkg_tax_rate_location
184     cust_tax_exempt_pkg
185     cust_bill_pkg_fee
186   )) {
187
188     foreach my $voided (
189       qsearch($table.'_void', { billpkgnum=>$self->billpkgnum })
190     ) {
191
192       my $class = 'FS::'.$table;
193       my $unvoid = $class->new( {
194         map { $_ => $voided->get($_) } fields($table)
195       });
196       my $error = $unvoid->insert || $voided->delete;
197       if ( $error ) {
198         $dbh->rollback if $oldAutoCommit;
199         return $error;
200       }
201
202     }
203
204   }
205
206   $error = $self->delete;
207   if ( $error ) {
208     $dbh->rollback if $oldAutoCommit;
209     return $error;
210   }
211
212   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
213
214   '';
215
216 }
217
218 =item delete
219
220 Delete this record from the database.
221
222 =item replace OLD_RECORD
223
224 Replaces the OLD_RECORD with this one in the database.  If there is an error,
225 returns the error, otherwise returns false.
226
227 =item check
228
229 Checks all fields to make sure this is a valid record.  If there is
230 an error, returns the error, otherwise returns false.  Called by the insert
231 and replace methods.
232
233 =cut
234
235 sub check {
236   my $self = shift;
237
238   my $error = 
239     $self->ut_number('billpkgnum')
240     || $self->ut_snumber('pkgnum')
241     || $self->ut_number('invnum') #cust_bill or cust_bill_void, if we ever support line item voiding
242     || $self->ut_numbern('pkgpart_override')
243     || $self->ut_money('setup')
244     || $self->ut_money('recur')
245     || $self->ut_numbern('sdate')
246     || $self->ut_numbern('edate')
247     || $self->ut_textn('itemdesc')
248     || $self->ut_textn('itemcomment')
249     || $self->ut_textn('section')
250     || $self->ut_textn('freq')
251     || $self->ut_numbern('quantity')
252     || $self->ut_moneyn('unitsetup')
253     || $self->ut_moneyn('unitrecur')
254     || $self->ut_enum('hidden', [ '', 'Y' ])
255     || $self->ut_numbern('feepart')
256     || $self->ut_textn('reason')
257     || $self->ut_foreign_keyn('reasonnum', 'reason', 'reasonnum')
258   ;
259   return $error if $error;
260
261   $self->SUPER::check;
262 }
263
264 =item cust_bill
265
266 Returns the voided invoice (see L<FS::cust_bill_void>) for this voided line
267 item.
268
269 =cut
270
271 sub cust_bill {
272   my $self = shift;
273   #cust_bill or cust_bill_void, if we ever support line item voiding
274   qsearchs( 'cust_bill_void', { 'invnum' => $self->invnum } );
275 }
276
277 sub cust_bill_pkg_fee {
278   my $self = shift;
279   qsearch( 'cust_bill_pkg_fee_void', { 'billpkgnum' => $self->billpkgnum } );
280 }
281
282 =back
283
284 =head1 BUGS
285
286 =head1 SEE ALSO
287
288 L<FS::Record>, schema.html from the base documentation.
289
290 =cut
291
292 1;
293