don't generate invoices for COMP customers
[freeside.git] / FS / FS / cust_pay_void.pm
1 package FS::cust_pay_void; 
2 use strict;
3 use vars qw( @ISA );
4 use Business::CreditCard;
5 use FS::UID qw(getotaker);
6 use FS::Record qw(qsearchs); # dbh qsearch );
7 #use FS::cust_bill;
8 #use FS::cust_bill_pay;
9 #use FS::cust_pay_refund;
10 #use FS::cust_main;
11
12 @ISA = qw( FS::Record );
13
14 =head1 NAME
15
16 FS::cust_pay_void - Object methods for cust_pay_void objects
17
18 =head1 SYNOPSIS
19
20   use FS::cust_pay_void;
21
22   $record = new FS::cust_pay_void \%hash;
23   $record = new FS::cust_pay_void { 'column' => 'value' };
24
25   $error = $record->insert;
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33 =head1 DESCRIPTION
34
35 An FS::cust_pay_void object represents a voided payment.  The following fields
36 are currently supported:
37
38 =over 4
39
40 =item paynum - primary key (assigned automatically for new payments)
41
42 =item custnum - customer (see L<FS::cust_main>)
43
44 =item paid - Amount of this payment
45
46 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
47 L<Time::Local> and L<Date::Parse> for conversion functions.
48
49 =item payby - `CARD' (credit cards), `CHEK' (electronic check/ACH),
50 `LECB' (phone bill billing), `BILL' (billing), or `COMP' (free)
51
52 =item payinfo - card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
53
54 =item paybatch - text field for tracking card processing
55
56 =item closed - books closed flag, empty or `Y'
57
58 =item void_date
59
60 =item reason
61
62 =back
63
64 =head1 METHODS
65
66 =over 4 
67
68 =item new HASHREF
69
70 Creates a new payment.  To add the payment to the databse, see L<"insert">.
71
72 =cut
73
74 sub table { 'cust_pay_void'; }
75
76 =item insert
77
78 Adds this voided payment to the database.
79
80 =item delete
81
82 Currently unimplemented.
83
84 =cut
85
86 sub delete {
87   return "Can't delete voided payments!";
88 }
89
90 =item replace OLD_RECORD
91
92 Currently unimplemented.
93
94 =cut
95
96 sub replace {
97    return "Can't modify voided payments!";
98 }
99
100 =item check
101
102 Checks all fields to make sure this is a valid voided payment.  If there is an
103 error, returns the error, otherwise returns false.  Called by the insert
104 method.
105
106 =cut
107
108 sub check {
109   my $self = shift;
110
111   my $error =
112     $self->ut_numbern('paynum')
113     || $self->ut_numbern('custnum')
114     || $self->ut_money('paid')
115     || $self->ut_number('_date')
116     || $self->ut_textn('paybatch')
117     || $self->ut_enum('closed', [ '', 'Y' ])
118     || $self->ut_numbern('void_date')
119     || $self->ut_textn('reason')
120   ;
121   return $error if $error;
122
123   return "paid must be > 0 " if $self->paid <= 0;
124
125   return "unknown cust_main.custnum: ". $self->custnum
126     unless $self->invnum
127            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
128
129   $self->void_date(time) unless $self->void_date;
130
131   $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP)$/ or return "Illegal payby";
132   $self->payby($1);
133
134   #false laziness with cust_refund::check
135   if ( $self->payby eq 'CARD' ) {
136     my $payinfo = $self->payinfo;
137     $payinfo =~ s/\D//g;
138     $self->payinfo($payinfo);
139     if ( $self->payinfo ) {
140       $self->payinfo =~ /^(\d{13,16})$/
141         or return "Illegal (mistyped?) credit card number (payinfo)";
142       $self->payinfo($1);
143       validate($self->payinfo) or return "Illegal credit card number";
144       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
145     } else {
146       $self->payinfo('N/A');
147     }
148
149   } else {
150     $error = $self->ut_textn('payinfo');
151     return $error if $error;
152   }
153
154   $self->otaker(getotaker);
155
156   $self->SUPER::check;
157 }
158
159 =item cust_main
160
161 Returns the parent customer object (see L<FS::cust_main>).
162
163 =cut
164
165 sub cust_main {
166   my $self = shift;
167   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
168 }
169
170 =item payinfo_masked
171
172 Returns a "masked" payinfo field with all but the last four characters replaced
173 by 'x'es.  Useful for displaying credit cards.
174
175 =cut
176
177 sub payinfo_masked {
178   my $self = shift;
179   my $payinfo = $self->payinfo;
180   'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
181 }
182
183 =back
184
185 =head1 BUGS
186
187 Delete and replace methods.
188
189 =head1 SEE ALSO
190
191 L<FS::cust_pay>, L<FS::Record>, schema.html from the base documentation.
192
193 =cut
194
195 1;
196