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