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