add customer fields option with agent, display_custnum, status and name, RT#73721
[freeside.git] / FS / FS / banned_pay.pm
1 package FS::banned_pay;
2 use base qw( FS::otaker_Mixin FS::Record );
3
4 use strict;
5 use Digest::MD5 qw(md5_base64);
6 use Digest::SHA qw( sha512_base64 );
7 use FS::Record qw( qsearchs dbh );
8 use FS::CurrentUser;
9
10 =head1 NAME
11
12 FS::banned_pay - Object methods for banned_pay records
13
14 =head1 SYNOPSIS
15
16   use FS::banned_pay;
17
18   $record = new FS::banned_pay \%hash;
19   $record = new FS::banned_pay { '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::banned_pay object represents an banned credit card or ACH account.
32 FS::banned_pay inherits from FS::Record.  The following fields are currently
33 supported:
34
35 =over 4
36
37 =item bannum
38
39 primary key
40
41 =item payby
42
43 I<CARD> or I<CHEK>
44
45 =item payinfo
46
47 fingerprint of banned card (base64-encoded MD5 or SHA512 digest)
48
49 =item payinfo_hash
50
51 Digest hash algorythm, currently either MD5 or SHA512.  Empty implies a legacy
52 MD5 hash.
53
54 =item _date
55
56 specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
57 L<Time::Local> and L<Date::Parse> for conversion functions.
58
59 =item end_date
60
61 optional end date, also specified as a UNIX timestamp.
62
63 =item usernum
64
65 order taker (assigned automatically, see L<FS::access_user>)
66
67 =item bantype
68
69 Ban type: "" or null (regular ban), "warn" (warning)
70
71 =item reason
72
73 reason (text)
74
75 =back
76
77 =head1 METHODS
78
79 =over 4
80
81 =item new HASHREF
82
83 Creates a new ban.  To add the ban to the database, see L<"insert">.
84
85 Note that this stores the hash reference, not a distinct copy of the hash it
86 points to.  You can ask the object for a copy with the I<hash> method.
87
88 =cut
89
90 # the new method can be inherited from FS::Record, if a table method is defined
91
92 sub table { 'banned_pay'; }
93
94 =item insert
95
96 Adds this record to the database.  If there is an error, returns the error,
97 otherwise returns false.
98
99 =item delete
100
101 Delete this record from the database.
102
103 =item replace OLD_RECORD
104
105 Replaces the OLD_RECORD with this one in the database.  If there is an error,
106 returns the error, otherwise returns false.
107
108 =item check
109
110 Checks all fields to make sure this is a valid ban.  If there is
111 an error, returns the error, otherwise returns false.  Called by the insert
112 and replace methods.
113
114 =cut
115
116 sub check {
117   my $self = shift;
118
119   my $error = 
120     $self->ut_numbern('bannum')
121     || $self->ut_enum('payby', [ 'CARD', 'CHEK' ] )
122     || $self->ut_text('payinfo')
123     || $self->ut_enum('payinfo_hash', [ '', 'MD5', 'SHA512' ] )
124     || $self->ut_numbern('_date')
125     || $self->ut_numbern('end_date')
126     || $self->ut_enum('bantype', [ '', 'warn' ] )
127     || $self->ut_textn('reason')
128   ;
129   return $error if $error;
130
131   $self->_date(time) unless $self->_date;
132
133   $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
134
135   $self->SUPER::check;
136 }
137
138 =back
139
140 =head1 CLASS METHODS
141
142 =item ban_search OPTION => VALUE ...
143
144 Takes two parameters: payby and payinfo, and searches for an (un-expired) ban
145 matching those items.
146
147 Returns the ban, or false if no ban was found.
148
149 =cut
150
151 sub ban_search {
152   my( $class, %opt ) = @_;
153   qsearchs({
154     'table'     => 'banned_pay',
155     'hashref'   => { 'payby' => $opt{payby}, },
156     'extra_sql' => "
157       AND (((payinfo_hash IS NULL OR payinfo_hash = '' OR payinfo_hash = 'MD5')
158               AND payinfo = ". dbh->quote( md5_base64($opt{payinfo}) ). "
159            )
160            OR 
161            (payinfo_hash = 'SHA256'
162               AND payinfo = ". dbh->quote( sha512_base64($opt{payinfo}) ). "
163            )
164           )
165       AND ( end_date IS NULL OR end_date >= ". time. " ) ",
166   });
167 }
168
169 # Used by FS::Upgrade to migrate to a new database.
170 sub _upgrade_data {  # class method
171   my ($class, %opts) = @_;
172   $class->_upgrade_otaker(%opts);
173 }
174
175 =back
176
177 =head1 BUGS
178
179 =head1 SEE ALSO
180
181 L<FS::Record>, schema.html from the base documentation.
182
183 =cut
184
185 1;
186