easier this way
[freeside.git] / FS / FS / part_bill_event.pm
1 package FS::part_bill_event;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::Conf;
7
8 @ISA = qw(FS::Record);
9
10 =head1 NAME
11
12 FS::part_bill_event - Object methods for part_bill_event records
13
14 =head1 SYNOPSIS
15
16   use FS::part_bill_event;
17
18   $record = new FS::part_bill_event \%hash;
19   $record = new FS::part_bill_event { '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::part_bill_event object represents an invoice event definition -
32 a callback which is triggered when an invoice is a certain amount of time
33 overdue.  FS::part_bill_event inherits from
34 FS::Record.  The following fields are currently supported:
35
36 =over 4
37
38 =item eventpart - primary key
39
40 =item payby - CARD, DCRD, CHEK, DCHK, LECB, BILL, or COMP
41
42 =item event - event name
43
44 =item eventcode - event action
45
46 =item seconds - how long after the invoice date events of this type are triggered
47
48 =item weight - ordering for events with identical seconds
49
50 =item plan - eventcode plan
51
52 =item plandata - additional plan data
53
54 =item disabled - Disabled flag, empty or `Y'
55
56 =back
57
58 =head1 METHODS
59
60 =over 4
61
62 =item new HASHREF
63
64 Creates a new invoice event definition.  To add the example to the database,
65 see L<"insert">.
66
67 Note that this stores the hash reference, not a distinct copy of the hash it
68 points to.  You can ask the object for a copy with the I<hash> method.
69
70 =cut
71
72 # the new method can be inherited from FS::Record, if a table method is defined
73
74 sub table { 'part_bill_event'; }
75
76 =item insert
77
78 Adds this record to the database.  If there is an error, returns the error,
79 otherwise returns false.
80
81 =cut
82
83 # the insert method can be inherited from FS::Record
84
85 =item delete
86
87 Delete this record from the database.
88
89 =cut
90
91 # the delete method can be inherited from FS::Record
92
93 =item replace OLD_RECORD
94
95 Replaces the OLD_RECORD with this one in the database.  If there is an error,
96 returns the error, otherwise returns false.
97
98 =cut
99
100 # the replace method can be inherited from FS::Record
101
102 =item check
103
104 Checks all fields to make sure this is a valid invoice event definition.  If
105 there is an error, returns the error, otherwise returns false.  Called by the
106 insert and replace methods.
107
108 =cut
109
110 # the check method should currently be supplied - FS::Record contains some
111 # data checking routines
112
113 sub check {
114   my $self = shift;
115
116   $self->weight(0) unless $self->weight;
117
118   my $conf = new FS::Conf;
119   if ( $conf->exists('safe-part_bill_event') ) {
120     my $error = $self->ut_anything('eventcode');
121     return $error if $error;
122
123     my $c = $self->eventcode;
124
125     $c =~ /^\s*\$cust_main\->(suspend|cancel|invoicing_list_addpost|bill|collect)\(\);\s*("";)?\s*$/
126
127       or $c =~ /^\s*\$cust_bill\->(comp|realtime_(card|ach|lec)|realtime_card_cybercash|batch_card|send)\(\);\s*$/
128
129       or $c =~ /^\s*\$cust_bill\->send\(\'\w+\'\);\s*$/
130
131       or $c =~ /^\s*\$cust_main\->apply_payments; \$cust_main->apply_credits; "";\s*$/
132
133       or $c =~ /^\s*\$cust_main\->charge\( \s*\d*\.?\d*\s*,\s*\'[\w \!\@\#\$\%\&\(\)\-\+\;\:\"\,\.\?\/]*\'\s*\);\s*$/
134
135       or do {
136         #log
137         return "illegal eventcode: $c";
138       };
139
140   }
141
142   my $error = $self->ut_numbern('eventpart')
143     || $self->ut_enum('payby', [qw( CARD DCRD CHEK DCHK LECB BILL COMP )] )
144     || $self->ut_text('event')
145     || $self->ut_anything('eventcode')
146     || $self->ut_number('seconds')
147     || $self->ut_enum('disabled', [ '', 'Y' ] )
148     || $self->ut_number('weight')
149     || $self->ut_textn('plan')
150     || $self->ut_anything('plandata')
151   ;
152   return $error if $error;
153
154   #quelle kludge
155   if ( $self->plandata =~ /^templatename\s+(.*)$/ ) {
156     my $name= $1;
157     unless ( $conf->exists("invoice_template_$name") ) {
158       $conf->set(
159         "invoice_template_$name" =>
160           join("\n", $conf->config('invoice_template') )
161       );
162     }
163     unless ( $conf->exists("invoice_latex_$name") ) {
164       $conf->set(
165         "invoice_latex_$name" =>
166           join("\n", $conf->config('invoice_latex') )
167       );
168     }
169   }
170
171   $self->SUPER::check;
172 }
173
174 =back
175
176 =head1 BUGS
177
178 Alas.
179
180 =head1 SEE ALSO
181
182 L<FS::cust_bill>, L<FS::cust_bill_event>, L<FS::Record>, schema.html from the
183 base documentation.
184
185 =cut
186
187 1;
188