invoice voiding, RT#18677
[freeside.git] / FS / FS / cust_bill_void.pm
1 package FS::cust_bill_void;
2 use base qw( FS::Template_Mixin FS::cust_main_Mixin FS::otaker_Mixin FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearch qsearchs );
6 use FS::cust_main;
7 use FS::cust_statement;
8 use FS::access_user;
9 use FS::cust_bill_pkg_void;
10
11 =head1 NAME
12
13 FS::cust_bill_void - Object methods for cust_bill_void records
14
15 =head1 SYNOPSIS
16
17   use FS::cust_bill_void;
18
19   $record = new FS::cust_bill_void \%hash;
20   $record = new FS::cust_bill_void { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::cust_bill_void object represents a voided invoice.  FS::cust_bill_void
33 inherits from FS::Record.  The following fields are currently supported:
34
35 =over 4
36
37 =item invnum
38
39 primary key
40
41 =item custnum
42
43 custnum
44
45 =item _date
46
47 _date
48
49 =item charged
50
51 charged
52
53 =item invoice_terms
54
55 invoice_terms
56
57 =item previous_balance
58
59 previous_balance
60
61 =item billing_balance
62
63 billing_balance
64
65 =item closed
66
67 closed
68
69 =item statementnum
70
71 statementnum
72
73 =item agent_invid
74
75 agent_invid
76
77 =item promised_date
78
79 promised_date
80
81 =item void_date
82
83 void_date
84
85 =item reason
86
87 reason
88
89 =item void_usernum
90
91 void_usernum
92
93
94 =back
95
96 =head1 METHODS
97
98 =over 4
99
100 =item new HASHREF
101
102 Creates a new voided invoice.  To add the voided invoice to the database, see L<"insert">.
103
104 Note that this stores the hash reference, not a distinct copy of the hash it
105 points to.  You can ask the object for a copy with the I<hash> method.
106
107 =cut
108
109 sub table { 'cust_bill_void'; }
110 sub notice_name { 'VOIDED Invoice'; }
111 #XXXsub template_conf { 'quotation_'; }
112
113 =item insert
114
115 Adds this record to the database.  If there is an error, returns the error,
116 otherwise returns false.
117
118 =cut
119
120 # the insert method can be inherited from FS::Record
121
122 =item delete
123
124 Delete this record from the database.
125
126 =cut
127
128 # the delete method can be inherited from FS::Record
129
130 =item replace OLD_RECORD
131
132 Replaces the OLD_RECORD with this one in the database.  If there is an error,
133 returns the error, otherwise returns false.
134
135 =cut
136
137 # the replace method can be inherited from FS::Record
138
139 =item check
140
141 Checks all fields to make sure this is a valid voided invoice.  If there is
142 an error, returns the error, otherwise returns false.  Called by the insert
143 and replace methods.
144
145 =cut
146
147 # the check method should currently be supplied - FS::Record contains some
148 # data checking routines
149
150 sub check {
151   my $self = shift;
152
153   my $error = 
154     $self->ut_number('invnum')
155     || $self->ut_foreign_key('custnum', 'cust_main', 'custnum' )
156     || $self->ut_numbern('_date')
157     || $self->ut_money('charged')
158     || $self->ut_textn('invoice_terms')
159     || $self->ut_moneyn('previous_balance')
160     || $self->ut_moneyn('billing_balance')
161     || $self->ut_enum('closed', [ '', 'Y' ])
162     || $self->ut_foreign_keyn('statementnum', 'cust_statement', 'statementnum')
163     || $self->ut_numbern('agent_invid')
164     || $self->ut_numbern('promised_date')
165     || $self->ut_numbern('void_date')
166     || $self->ut_textn('reason')
167     || $self->ut_numbern('void_usernum')
168   ;
169   return $error if $error;
170
171   $self->void_date(time) unless $self->void_date;
172
173   $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
174     unless $self->void_usernum;
175
176   $self->SUPER::check;
177 }
178
179 =item display_invnum
180
181 Returns the displayed invoice number for this invoice: agent_invid if
182 cust_bill-default_agent_invid is set and it has a value, invnum otherwise.
183
184 =cut
185
186 sub display_invnum {
187   my $self = shift;
188   my $conf = $self->conf;
189   if ( $conf->exists('cust_bill-default_agent_invid') && $self->agent_invid ){
190     return $self->agent_invid;
191   } else {
192     return $self->invnum;
193   }
194 }
195
196 =item void_access_user
197
198 Returns the voiding employee object (see L<FS::access_user>).
199
200 =cut
201
202 sub void_access_user {
203   my $self = shift;
204   qsearchs('access_user', { 'usernum' => $self->void_usernum } );
205 }
206
207 =item cust_main
208
209 =cut
210
211 sub cust_main {
212   my $self = shift;
213   qsearchs('cust_main', { 'custnum' => $self->custnum } );
214 }
215
216 =item cust_bill_pkg
217
218 =cut
219
220 sub cust_bill_pkg { #actually cust_bill_pkg_void objects
221   my $self = shift;
222   qsearch('cust_bill_pkg_void', { invnum=>$self->invnum });
223 }
224
225 =back
226
227 =item enable_previous
228
229 =cut
230
231 sub enable_previous { 0 }
232
233
234 =back
235
236 =head1 BUGS
237
238 =head1 SEE ALSO
239
240 L<FS::Record>, schema.html from the base documentation.
241
242 =cut
243
244 1;
245