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