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