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