make sure to specify a money_char default
[freeside.git] / httemplate / edit / part_bill_event.cgi
1 <!-- mason kludge -->
2 <%
3
4 if ( $cgi->param('eventpart') && $cgi->param('eventpart') =~ /^(\d+)$/ ) {
5   $cgi->param('eventpart', $1);
6 } else {
7   $cgi->param('eventpart', '');
8 }
9
10 my ($query) = $cgi->keywords;
11 my $action = '';
12 my $part_bill_event = '';
13 if ( $cgi->param('error') ) {
14   $part_bill_event = new FS::part_bill_event ( {
15     map { $_, scalar($cgi->param($_)) } fields('part_bill_event')
16   } );
17 }
18 if ( $query && $query =~ /^(\d+)$/ ) {
19   $part_bill_event ||= qsearchs('part_bill_event',{'eventpart'=>$1});
20 } else {
21   $part_bill_event ||= new FS::part_bill_event {};
22 }
23 $action ||= $part_bill_event->eventpart ? 'Edit' : 'Add';
24 my $hashref = $part_bill_event->hashref;
25
26 print header("$action Invoice Event Definition", menubar(
27   'Main Menu' => popurl(2),
28   'View all invoice events' => popurl(2). 'browse/part_bill_event.cgi',
29 ));
30
31 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
32       "</FONT>"
33   if $cgi->param('error');
34
35 print '<FORM ACTION="', popurl(1), 'process/part_bill_event.cgi" METHOD=POST>'.
36       '<INPUT TYPE="hidden" NAME="eventpart" VALUE="'.
37       $part_bill_event->eventpart  .'">';
38 print "Invoice Event #", $hashref->{eventpart} ? $hashref->{eventpart} : "(NEW)";
39
40 print ntable("#cccccc",2), <<END;
41 <TR><TD ALIGN="right">Payby</TD><TD><SELECT NAME="payby">
42 END
43
44 for (qw(CARD DCRD CHEK DCHK LECB BILL COMP)) {
45   print qq!<OPTION VALUE="$_"!;
46   if ($part_bill_event->payby eq $_) {
47     print " SELECTED>$_</OPTION>";
48   } else {
49     print ">$_</OPTION>";
50   }
51 }
52
53 my $days = $hashref->{seconds}/86400;
54
55 print <<END;
56 </SELECT></TD></TR>
57 <TR><TD ALIGN="right">Event</TD><TD><INPUT TYPE="text" NAME="event" VALUE="$hashref->{event}"></TD></TR>
58 <TR><TD ALIGN="right">After</TD><TD><INPUT TYPE="text" NAME="days" VALUE="$days"> days</TD></TR>
59 END
60
61 print '<TR><TD ALIGN="right">Disabled</TD><TD>';
62 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
63 print ' CHECKED' if $hashref->{disabled} eq "Y";
64 print '>';
65 print '</TD></TR>';
66
67 print '<TR><TD ALIGN="right">Action</TD><TD>';
68
69 #print ntable();
70
71 sub select_pkgpart {
72   my $label = shift;
73   my $plandata = shift;
74   my %selected = map { $_=>1 } split(/,\s*/, $plandata->{$label});
75   qq(<SELECT NAME="$label" MULTIPLE>).
76   join("\n", map {
77     '<OPTION VALUE="'. $_->pkgpart. '"'.
78     ( $selected{$_->pkgpart} ? ' SELECTED' : '' ).
79     '>'. $_->pkg. ' - '. $_->comment
80   } qsearch('part_pkg', { 'disabled' => '' } ) ).
81   '</SELECT>';
82 }
83
84 sub select_agentnum {
85   my $plandata = shift;
86   #my $agentnum = $plandata->{'agentnum'};
87   my %agentnums = map { $_=>1 } split(/,\s*/, $plandata->{'agentnum'});
88   '<SELECT NAME="agentnum" MULTIPLE>'.
89   join("\n", map {
90     '<OPTION VALUE="'. $_->agentnum. '"'.
91     ( $agentnums{$_->agentnum} ? ' SELECTED' : '' ).
92     '>'. $_->agent
93   } qsearch('agent', { 'disabled' => '' } ) ).
94   '</SELECT>';
95 }
96
97 my $conf = new FS::Conf;
98 my $money_char = $conf->config('money_char') || '$';
99
100 #this is pretty kludgy right here.
101 tie my %events, 'Tie::IxHash',
102
103   'fee' => {
104     'name'   => 'Late fee',
105     'code'   => '$cust_main->charge( %%%charge%%%, \'%%%reason%%%\' );',
106     'html'   => 
107       'Amount <INPUT TYPE="text" SIZE="7" NAME="charge" VALUE="%%%charge%%%">'.
108       '<BR>Reason <INPUT TYPE="text" NAME="reason" VALUE="%%%reason%%%">',
109     'weight' => 10,
110   },
111   'suspend' => {
112     'name'   => 'Suspend',
113     'code'   => '$cust_main->suspend();',
114     'weight' => 10,
115   },
116   'suspend' => {
117     'name'   => 'Suspend if balance (this invoice and previous) over',
118     'code'   => '$cust_bill->cust_suspend_if_balance_over( %%%balanceover%%% );',
119     'html'   => " $money_char ". '<INPUT TYPE="text" SIZE="7" NAME="balanceover" VALUE="%%%balanceover%%%">',
120     'weight' => 10,
121   },
122   'suspend-if-pkgpart' => {
123     'name'   => 'Suspend packages',
124     'code'   => '$cust_main->suspend_if_pkgpart(%%%if_pkgpart%%%);',
125     'html'   => sub { &select_pkgpart('if_pkgpart', @_) },
126     'weight' => 10,
127   },
128   'suspend-unless-pkgpart' => {
129     'name'   => 'Suspend packages except',
130     'code'   => '$cust_main->suspend_unless_pkgpart(%%%unless_pkgpart%%%);',
131     'html'   => sub { &select_pkgpart('unless_pkgpart', @_) },
132     'weight' => 10,
133   },
134   'cancel' => {
135     'name'   => 'Cancel',
136     'code'   => '$cust_main->cancel();',
137     'weight' => 10,
138   },
139
140   'addpost' => {
141     'name' => 'Add postal invoicing',
142     'code' => '$cust_main->invoicing_list_addpost(); "";',
143     'weight'  => 20,
144   },
145
146   'comp' => {
147     'name' => 'Pay invoice with a complimentary "payment"',
148     'code' => '$cust_bill->comp();',
149     'weight' => 30,
150   },
151
152   'realtime-card' => {
153     'name' => 'Run card with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
154     'code' => '$cust_bill->realtime_card();',
155     'weight' => 30,
156   },
157
158   'realtime-check' => {
159     'name' => 'Run check with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
160     'code' => '$cust_bill->realtime_ach();',
161     'weight' => 30,
162   },
163
164   'realtime-lec' => {
165     'name' => 'Run phone bill ("LEC") billing with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
166     'code' => '$cust_bill->realtime_lec();',
167     'weight' => 30,
168   },
169
170   'batch-card' => {
171     'name' => 'Add card to the pending credit card batch',
172     'code' => '$cust_bill->batch_card();',
173     'weight' => 40,
174   },
175
176   'send' => {
177     'name' => 'Send invoice (email/print)',
178     'code' => '$cust_bill->send();',
179     'weight' => 50,
180   },
181
182   'send_alternate' => {
183     'name' => 'Send invoice (email/print) with alternate template',
184     'code' => '$cust_bill->send(\'%%%templatename%%%\');',
185     'html' =>
186         '<INPUT TYPE="text" NAME="templatename" VALUE="%%%templatename%%%">',
187     'weight' => 50,
188   },
189
190   'send_if_newest' => {
191     'name' => 'Send invoice (email/print) with alternate template, if it is still the newest invoice (useful for late notices - set to 31 days or later)',
192     'code' => '$cust_bill->send_if_newest(\'%%%if_newest_templatename%%%\');',
193     'html' =>
194         '<INPUT TYPE="text" NAME="if_newest_templatename" VALUE="%%%if_newest_templatename%%%">',
195     'weight' => 50,
196   },
197
198   'send_agent' => {
199     'name' => 'Send invoice (email/print) ',
200     'code' => '$cust_bill->send(\'%%%agent_templatename%%%\', [ %%%agentnum%%% ], \'%%%agent_invoice_from%%%\');',
201     'html' => sub {
202         '<TABLE BORDER=0>
203           <TR>
204             <TD ALIGN="right">only for agent(s) </TD>
205             <TD>'. &select_agentnum(@_). '</TD>
206           </TR>
207           <TR>
208             <TD ALIGN="right">with template </TD>
209             <TD>
210               <INPUT TYPE="text" NAME="agent_templatename" VALUE="%%%agent_templatename%%%">
211             </TD>
212           </TR>
213           <TR>
214             <TD ALIGN="right">email From: </TD>
215             <TD>
216               <INPUT TYPE="text" NAME="agent_invoice_from" VALUE="%%%agent_invoice_from%%%">
217             </TD>
218           </TR>
219         </TABLE>';
220     },
221     'weight' => 50,
222   },
223
224   'send_csv_ftp' => {
225     'name' => 'Upload CSV invoice data to an FTP server',
226     'code' => '$cust_bill->send_csv( protocol   => \'ftp\',
227                                      server     => \'%%%ftpserver%%%\',
228                                      username   => \'%%%ftpusername%%%\',
229                                      password   => \'%%%ftppassword%%%\',
230                                      dir        => \'%%%ftpdir%%%\',
231                                      \'format\' => \'%%%ftpformat%%%\',
232                                    );',
233     'html' =>
234         '<TABLE BORDER=0>'.
235         '<TR><TD ALIGN="right">Format ("default" or "billco"): </TD>'.
236           '<TD>'.
237             '<!--'.
238             '<SELECT NAME="ftpformat">'.
239               '<OPTION VALUE="default">Default'.
240               '<OPTION VALUE="billco">Billco'.
241             '</SELECT>'.
242             '-->'.
243             '<INPUT TYPE="text" NAME="ftpformat" VALUE="%%%ftpformat%%%">'.
244           '</TD></TR>'.
245         '<TR><TD ALIGN="right">FTP server: </TD>'.
246           '<TD><INPUT TYPE="text" NAME="ftpserver" VALUE="%%%ftpserver%%%">'.
247           '</TD></TR>'.
248         '<TR><TD ALIGN="right">FTP username: </TD><TD>'.
249           '<INPUT TYPE="text" NAME="ftpusername" VALUE="%%%ftpusername%%%">'.
250           '</TD></TR>'.
251         '<TR><TD ALIGN="right">FTP password: </TD><TD>'.
252           '<INPUT TYPE="text" NAME="ftppassword" VALUE="%%%ftppassword%%%">'.
253           '</TD></TR>'.
254         '<TR><TD ALIGN="right">FTP directory: </TD>'.
255           '<TD><INPUT TYPE="text" NAME="ftpdir" VALUE="%%%ftpdir%%%">'.
256           '</TD></TR>'.
257         '</TABLE>',
258     'weight' => 50,
259   },
260
261   'spool_csv' => {
262     'name' => 'Spool CSV invoice data',
263     'code' => '$cust_bill->spool_csv(
264                  \'format\' => \'%%%spoolformat%%%\',
265                  \'dest\'   => \'%%%spooldest%%%\',
266                  \'agent_spools\' => \'%%%spoolagent_spools%%%\',
267                );',
268     'html' => sub {
269        my $plandata = shift;
270
271        my $html =
272        '<TABLE BORDER=0>'.
273        '<TR><TD ALIGN="right">Format: </TD>'.
274          '<TD>'.
275            '<SELECT NAME="spoolformat">';
276
277        foreach my $option (qw( default billco )) {
278          $html .= qq(<OPTION VALUE="$option");
279          $html .= ' SELECTED' if $option eq $plandata->{'spoolformat'};
280          $html .= ">\u$option";
281        }
282
283        $html .= 
284            '</SELECT>'.
285          '</TD></TR>'.
286        '<TR><TD ALIGN="right">For destination: </TD>'.
287          '<TD>'.
288            '<SELECT NAME="spooldest">';
289
290        tie my %dest, 'Tie::IxHash', 
291          ''      => '(all)',
292          'POST'  => 'Postal Mail',
293          'EMAIL' => 'Email',
294          'FAX'   => 'Fax',
295        ;
296
297        foreach my $dest (keys %dest) {
298          $html .= qq(<OPTION VALUE="$dest");
299          $html .= ' SELECTED' if $dest eq $plandata->{'spooldest'};
300          $html .= '>'. $dest{$dest};
301        }
302
303        $html .=
304            '</SELECT>'.
305          '</TD></TR>'.
306        '<TR><TD ALIGN="right">Individual per-agent spools? </TD>'.
307          '<TD><INPUT TYPE="checkbox" NAME="spoolagent_spools" VALUE="1" '.
308            ( $plandata->{'spoolagent_spools'} ? 'CHECKED' : '' ).
309            '>'.
310          '</TD></TR>'.
311        '</TABLE>';
312
313        $html;
314     },
315     'weight' => 50,
316   },
317
318   'bill' => {
319     'name' => 'Generate invoices (normally only used with a <i>Late Fee</i> event)',
320     'code' => '$cust_main->bill();',
321     'weight'  => 60,
322   },
323
324   'apply' => {
325     'name' => 'Apply unapplied payments and credits',
326     'code' => '$cust_main->apply_payments; $cust_main->apply_credits; "";',
327     'weight'  => 70,
328   },
329
330   'collect' => {
331     'name' => 'Collect on invoices (normally only used with a <i>Late Fee</i> and <i>Generate Invoice</i> events)',
332     'code' => '$cust_main->collect();',
333     'weight'  => 80,
334   },
335
336 ;
337
338 foreach my $event ( keys %events ) {
339   my %plandata = map { /^(\w+) (.*)$/; ($1, $2); }
340                    split(/\n/, $part_bill_event->plandata);
341   my $html = $events{$event}{html};
342   if ( ref($html) eq 'CODE' ) {
343     $html = &{$html}(\%plandata);
344   }
345   while ( $html =~ /%%%(\w+)%%%/ ) {
346     my $field = $1;
347     $html =~ s/%%%$field%%%/$plandata{$field}/;
348   }
349
350   print ntable( "#cccccc", 2).
351         qq!<TR><TD><INPUT TYPE="radio" NAME="plan_weight_eventcode" !;
352   print "CHECKED " if $event eq $part_bill_event->plan;
353   print qq!VALUE="!.  $event. ":". $events{$event}{weight}. ":".
354         encode_entities($events{$event}{code}).
355         qq!">$events{$event}{name}</TD>!;
356   print '<TD>'. $html. '</TD>' if $html;
357   print qq!</TR>!;
358   print '</TABLE>';
359 }
360
361 #print '</TABLE>';
362
363 print <<END;
364 </TD></TR>
365 </TABLE>
366 END
367
368 print qq!<INPUT TYPE="submit" VALUE="!,
369       $hashref->{eventpart} ? "Apply changes" : "Add invoice event",
370       qq!">!;
371 %>
372
373     </FORM>
374   </BODY>
375 </HTML>
376