event refactor, landing on HEAD!
[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 ($creason, $newcreasonT, $newcreason);
11 %my ($sreason, $newsreasonT, $newsreason);
12 %
13 %
14 %my ($query) = $cgi->keywords;
15 %my $action = '';
16 %my $part_bill_event = '';
17 %my $currentreasonclass = '';
18 %if ( $cgi->param('error') ) {
19 %  $part_bill_event = new FS::part_bill_event ( {
20 %    map { $_, scalar($cgi->param($_)) } fields('part_bill_event')
21 %  } );
22 %}
23 %if ( $query && $query =~ /^(\d+)$/ ) {
24 %  $part_bill_event ||= qsearchs('part_bill_event',{'eventpart'=>$1});
25 %} else {
26 %  $part_bill_event ||= new FS::part_bill_event {};
27 %}
28 %$action ||= $part_bill_event->eventpart ? 'Edit' : 'Add';
29 %my $hashref = $part_bill_event->hashref;
30 %
31 %
32
33
34 <% include('/elements/header.html',
35       "$action Invoice Event Definition",
36       menubar(
37         'Main Menu' => popurl(2),
38         'View all invoice events' => popurl(2). 'browse/part_bill_event.cgi',
39       )
40     )
41 %>
42 % if ( $cgi->param('error') ) { 
43
44   <FONT SIZE="+1" COLOR="#ff0000">Error: <% $cgi->param('error') %></FONT>
45 % } 
46
47
48 <FORM ACTION="<% popurl(1) %>process/part_bill_event.cgi" NAME="editEvent" METHOD=POST>
49 <INPUT TYPE="hidden" NAME="eventpart" VALUE="<% $part_bill_event->eventpart %>">
50 Invoice Event #<% $hashref->{eventpart} ? $hashref->{eventpart} : "(NEW)" %>
51
52 <%  ntable("#cccccc",2) %>
53
54   <TR>
55     <TD ALIGN="right">Event name </TD>
56     <TD><INPUT TYPE="text" NAME="event" VALUE="<% $hashref->{event} %>"></TD>
57   </TR>
58
59   <TR>
60     <TD ALIGN="right">For </TD>
61     <TD>
62       <SELECT NAME="payby">
63 % tie my %payby, 'Tie::IxHash', FS::payby->cust_payby2longname;
64 %           foreach my $payby ( keys %payby ) {
65 %        
66
67
68           <OPTION VALUE="<% $payby %>"<% ($part_bill_event->payby eq $payby) ? ' SELECTED' : '' %>><% $payby{$payby} %></OPTION>
69 % } 
70
71
72       </SELECT> customers
73     </TD>
74   </TR>
75 % my $days = $hashref->{seconds}/86400; 
76
77
78   <TR>
79     <TD ALIGN="right">After</TD>
80     <TD><INPUT TYPE="text" NAME="days" VALUE="<% $days %>"> days</TD>
81   </TR>
82
83   <TR>
84     <TD ALIGN="right">Test event</TD>
85     <TD>
86       <SELECT NAME="freq">
87 % tie my %freq, 'Tie::IxHash', '1d' => 'daily', '1m' => 'monthly';
88 %           foreach my $freq ( keys %freq ) {
89 %        
90
91
92           <OPTION VALUE="<% $freq %>"<% ($part_bill_event->freq eq $freq) ? ' SELECTED' : '' %>><% $freq{$freq} %></OPTION>
93 % } 
94
95
96       </SELECT>
97     </TD>
98   </TR>
99
100
101   <TR>
102     <TD ALIGN="right">Disabled</TD>
103     <TD>
104       <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<% $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>>
105     </TD>
106   </TR>
107
108   <TR>
109     <TD VALIGN="top" ALIGN="right">Action</TD>
110     <TD>
111 %
112 %
113 %#print ntable();
114 %
115 %sub select_pkgpart {
116 %  my $label = shift;
117 %  my $plandata = shift;
118 %  my %selected = map { $_=>1 } split(/,\s*/, $plandata->{$label});
119 %  qq(<SELECT NAME="$label" MULTIPLE>).
120 %  join("\n", map {
121 %    '<OPTION VALUE="'. $_->pkgpart. '"'.
122 %    ( $selected{$_->pkgpart} ? ' SELECTED' : '' ).
123 %    '>'. $_->pkg. ' - '. $_->comment
124 %  } qsearch('part_pkg', { 'disabled' => '' } ) ).
125 %  '</SELECT>';
126 %}
127 %
128 %sub select_agentnum {
129 %  my $plandata = shift;
130 %  #my $agentnum = $plandata->{'agentnum'};
131 %  my %agentnums = map { $_=>1 } split(/,\s*/, $plandata->{'agentnum'});
132 %  '<SELECT NAME="agentnum" MULTIPLE>'.
133 %  join("\n", map {
134 %    '<OPTION VALUE="'. $_->agentnum. '"'.
135 %    ( $agentnums{$_->agentnum} ? ' SELECTED' : '' ).
136 %    '>'. $_->agent
137 %  } qsearch('agent', { 'disabled' => '' } ) ).
138 %  '</SELECT>';
139 %}
140 %
141 %my $conf = new FS::Conf;
142 %my $money_char = $conf->config('money_char') || '$';
143 %
144 %#this is pretty kludgy right here.
145 %tie my %events, 'Tie::IxHash',
146 %
147 %  'fee' => {
148 %    'name'   => 'Late fee (flat)',
149 %    'code'   => '$cust_main->charge( %%%charge%%%, \'%%%reason%%%\' );',
150 %    'html'   => 
151 %      'Amount <INPUT TYPE="text" SIZE="7" NAME="charge" VALUE="%%%charge%%%">'.
152 %      '<BR>Reason <INPUT TYPE="text" NAME="reason" VALUE="%%%reason%%%">',
153 %    'weight' => 10,
154 %  },
155 %  'fee_percent' => {
156 %    'name'   => 'Late fee (percentage)',
157 %    'code'   => '$cust_main->charge( sprintf(\'%.2f\', $cust_bill->owed * %%%percent%%% / 100 ), \'%%%reason%%%\' );',
158 %    'html'   => 
159 %      'Percent <INPUT TYPE="text" SIZE="2" NAME="percent" VALUE="%%%percent%%%">%'.
160 %      '<BR>Reason <INPUT TYPE="text" NAME="reason" VALUE="%%%reason%%%">',
161 %    'weight' => 10,
162 %  },
163 %  'suspend' => {
164 %    'name'   => 'Suspend',
165 %    'code'   => '$cust_main->suspend(reason => %%%sreason%%%);',
166 %    'weight' => 10,
167 %    'reason' => 'S',
168 %  },
169 %  'suspend-if-balance' => {
170 %    'name'   => 'Suspend if balance (this invoice and previous) over',
171 %    'code'   => '$cust_bill->cust_suspend_if_balance_over( %%%balanceover%%%, reason => %%%sreason%%%, );',
172 %    'html'   => " $money_char ". '<INPUT TYPE="text" SIZE="7" NAME="balanceover" VALUE="%%%balanceover%%%">',
173 %    'weight' => 10,
174 %    'reason' => 'S',
175 %  },
176 %  'suspend-if-pkgpart' => {
177 %    'name'   => 'Suspend packages',
178 %    'code'   => '$cust_main->suspend_if_pkgpart({pkgparts => [%%%if_pkgpart%%%,], reason => %%%sreason%%%,});',
179 %    'html'   => sub { &select_pkgpart('if_pkgpart', @_) },
180 %    'weight' => 10,
181 %    'reason' => 'S',
182 %  },
183 %  'suspend-unless-pkgpart' => {
184 %    'name'   => 'Suspend packages except',
185 %    'code'   => '$cust_main->suspend_unless_pkgpart({unless_pkgpart => [%%%unless_pkgpart%%%], reason => %%%sreason%%%,});',
186 %    'html'   => sub { &select_pkgpart('unless_pkgpart', @_) },
187 %    'weight' => 10,
188 %    'reason' => 'S',
189 %  },
190 %  'cancel' => {
191 %    'name'   => 'Cancel',
192 %    'code'   => '$cust_main->cancel(reason => %%%creason%%%);',
193 %    'weight' => 10,
194 %    'reason' => 'C',
195 %  },
196 %
197 %  'addpost' => {
198 %    'name' => 'Add postal invoicing',
199 %    'code' => '$cust_main->invoicing_list_addpost(); "";',
200 %    'weight'  => 20,
201 %  },
202 %
203 %  'comp' => {
204 %    'name' => 'Pay invoice with a complimentary "payment"',
205 %    'code' => '$cust_bill->comp();',
206 %    'weight' => 30,
207 %  },
208 %
209 %  'credit' => {
210 %    'name'   => "Create and apply a credit for the customer's balance (i.e. write off as bad debt)",
211 %    'code'   => '$cust_main->credit( $cust_main->balance, \'%%%reason%%%\' );',
212 %    'html'   => '<INPUT TYPE="text" NAME="reason" VALUE="%%%reason%%%">',
213 %    'weight' => 30,
214 %  },
215 %
216 %  'realtime-card' => {
217 %    'name' => 'Run card with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
218 %    'code' => '$cust_bill->realtime_card();',
219 %    'weight' => 30,
220 %  },
221 %
222 %  'realtime-check' => {
223 %    'name' => 'Run check with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
224 %    'code' => '$cust_bill->realtime_ach();',
225 %    'weight' => 30,
226 %  },
227 %
228 %  'realtime-lec' => {
229 %    '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',
230 %    'code' => '$cust_bill->realtime_lec();',
231 %    'weight' => 30,
232 %  },
233 %
234 %  'batch-card' => {
235 %    'name' => 'Add card or check to a pending batch',
236 %    'code' => '$cust_bill->batch_card(%options);',
237 %    'weight' => 40,
238 %  },
239 %
240 %  
241 %  #'retriable' => {
242 %  #  'name' => 'Mark batched card event as retriable',
243 %  #  'code' => '$cust_pay_batch->retriable();',
244 %  #  'weight' => 60,
245 %  #},
246 %
247 %  'send' => {
248 %    'name' => 'Send invoice (email/print/fax)',
249 %    'code' => '$cust_bill->send();',
250 %    'weight' => 50,
251 %  },
252 %
253 %  'send_email' => {
254 %    'name' => 'Send invoice (email only)',
255 %    'code' => '$cust_bill->email();',
256 %    'weight' => 50,
257 %  },
258 %
259 %  'send_alternate' => {
260 %    'name' => 'Send invoice (email/print/fax) with alternate template',
261 %    'code' => '$cust_bill->send(\'%%%templatename%%%\');',
262 %    'html' =>
263 %        '<INPUT TYPE="text" NAME="templatename" VALUE="%%%templatename%%%">',
264 %    'weight' => 50,
265 %  },
266 %
267 %  'send_if_newest' => {
268 %    '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)',
269 %    'code' => '$cust_bill->send_if_newest(\'%%%if_newest_templatename%%%\');',
270 %    'html' =>
271 %        '<INPUT TYPE="text" NAME="if_newest_templatename" VALUE="%%%if_newest_templatename%%%">',
272 %    'weight' => 50,
273 %  },
274 %
275 %  'send_agent' => {
276 %    'name' => 'Send invoice (email/print/fax) ',
277 %    'code' => '$cust_bill->send(\'%%%agent_templatename%%%\', [ %%%agentnum%%% ], \'%%%agent_invoice_from%%%\');',
278 %    'html' => sub {
279 %        '<TABLE BORDER=0>
280 %          <TR>
281 %            <TD ALIGN="right">only for agent(s) </TD>
282 %            <TD>'. &select_agentnum(@_). '</TD>
283 %          </TR>
284 %          <TR>
285 %            <TD ALIGN="right">with template </TD>
286 %            <TD>
287 %              <INPUT TYPE="text" NAME="agent_templatename" VALUE="%%%agent_templatename%%%">
288 %            </TD>
289 %          </TR>
290 %          <TR>
291 %            <TD ALIGN="right">email From: </TD>
292 %            <TD>
293 %              <INPUT TYPE="text" NAME="agent_invoice_from" VALUE="%%%agent_invoice_from%%%">
294 %            </TD>
295 %          </TR>
296 %        </TABLE>';
297 %    },
298 %    'weight' => 50,
299 %  },
300 %
301 %  'send_csv_ftp' => {
302 %    'name' => 'Upload CSV invoice data to an FTP server',
303 %    'code' => '$cust_bill->send_csv( protocol   => \'ftp\',
304 %                                     server     => \'%%%ftpserver%%%\',
305 %                                     username   => \'%%%ftpusername%%%\',
306 %                                     password   => \'%%%ftppassword%%%\',
307 %                                     dir        => \'%%%ftpdir%%%\',
308 %                                     \'format\' => \'%%%ftpformat%%%\',
309 %                                   );',
310 %    'html' =>
311 %        '<TABLE BORDER=0>'.
312 %        '<TR><TD ALIGN="right">Format ("default" or "billco"): </TD>'.
313 %          '<TD>'.
314 %            '<!--'.
315 %            '<SELECT NAME="ftpformat">'.
316 %              '<OPTION VALUE="default">Default'.
317 %              '<OPTION VALUE="billco">Billco'.
318 %            '</SELECT>'.
319 %            '-->'.
320 %            '<INPUT TYPE="text" NAME="ftpformat" VALUE="%%%ftpformat%%%">'.
321 %          '</TD></TR>'.
322 %        '<TR><TD ALIGN="right">FTP server: </TD>'.
323 %          '<TD><INPUT TYPE="text" NAME="ftpserver" VALUE="%%%ftpserver%%%">'.
324 %          '</TD></TR>'.
325 %        '<TR><TD ALIGN="right">FTP username: </TD><TD>'.
326 %          '<INPUT TYPE="text" NAME="ftpusername" VALUE="%%%ftpusername%%%">'.
327 %          '</TD></TR>'.
328 %        '<TR><TD ALIGN="right">FTP password: </TD><TD>'.
329 %          '<INPUT TYPE="text" NAME="ftppassword" VALUE="%%%ftppassword%%%">'.
330 %          '</TD></TR>'.
331 %        '<TR><TD ALIGN="right">FTP directory: </TD>'.
332 %          '<TD><INPUT TYPE="text" NAME="ftpdir" VALUE="%%%ftpdir%%%">'.
333 %          '</TD></TR>'.
334 %        '</TABLE>',
335 %    'weight' => 50,
336 %  },
337 %
338 %  'spool_csv' => {
339 %    'name' => 'Spool CSV invoice data',
340 %    'code' => '$cust_bill->spool_csv(
341 %                 \'format\' => \'%%%spoolformat%%%\',
342 %                 \'dest\'   => \'%%%spooldest%%%\',
343 %                 \'balanceover\' => \'%%%spoolbalanceover%%%\',
344 %                 \'agent_spools\' => \'%%%spoolagent_spools%%%\',
345 %               );',
346 %    'html' => sub {
347 %       my $plandata = shift;
348 %
349 %       my $html =
350 %       '<TABLE BORDER=0>'.
351 %       '<TR><TD ALIGN="right">Format: </TD>'.
352 %         '<TD>'.
353 %           '<SELECT NAME="spoolformat">';
354 %
355 %       foreach my $option (qw( default billco )) {
356 %         $html .= qq(<OPTION VALUE="$option");
357 %         $html .= ' SELECTED' if $option eq $plandata->{'spoolformat'};
358 %         $html .= ">\u$option";
359 %       }
360 %
361 %       $html .= 
362 %           '</SELECT>'.
363 %         '</TD></TR>'.
364 %       '<TR><TD ALIGN="right">For destination: </TD>'.
365 %         '<TD>'.
366 %           '<SELECT NAME="spooldest">';
367 %
368 %       tie my %dest, 'Tie::IxHash', 
369 %         ''      => '(all)',
370 %         'POST'  => 'Postal Mail',
371 %         'EMAIL' => 'Email',
372 %         'FAX'   => 'Fax',
373 %       ;
374 %
375 %       foreach my $dest (keys %dest) {
376 %         $html .= qq(<OPTION VALUE="$dest");
377 %         $html .= ' SELECTED' if $dest eq $plandata->{'spooldest'};
378 %         $html .= '>'. $dest{$dest};
379 %       }
380 %
381 %       $html .=
382 %           '</SELECT>'.
383 %         '</TD></TR>'.
384 %
385 %       '<TR>'.
386 %         '<TD ALIGN="right">if balance (this invoice and previous) over </TD>'.
387 %         '<TD>'.
388 %           "$money_char ".
389 %           '<INPUT TYPE="text" SIZE="7" NAME="spoolbalanceover" VALUE="%%%spoolbalanceover%%%">'.
390 %         '</TD>'.
391 %       '<TR><TD ALIGN="right">Individual per-agent spools? </TD>'.
392 %         '<TD><INPUT TYPE="checkbox" NAME="spoolagent_spools" VALUE="1" '.
393 %           ( $plandata->{'spoolagent_spools'} ? 'CHECKED' : '' ).
394 %           '>'.
395 %         '</TD></TR>'.
396 %       '</TABLE>';
397 %
398 %       $html;
399 %    },
400 %    'weight' => 50,
401 %  },
402 %
403 %  'bill' => {
404 %    'name' => 'Generate invoices (normally only used with a <i>Late Fee</i> event)',
405 %    'code' => '$cust_main->bill();',
406 %    'weight'  => 60,
407 %  },
408 %
409 %  'apply' => {
410 %    'name' => 'Apply unapplied payments and credits',
411 %    'code' => '$cust_main->apply_payments_and_credits; "";',
412 %    'weight'  => 70,
413 %  },
414 %
415 %  'collect' => {
416 %    'name' => 'Collect on invoices (normally only used with a <i>Late Fee</i> and <i>Generate Invoice</i> events)',
417 %    'code' => '$cust_main->collect();',
418 %    'weight'  => 80,
419 %  },
420 %
421 %;
422 %
423 <SCRIPT TYPE="text/javascript">var myreasons = new Array();</SCRIPT>
424 %foreach my $event ( keys %events ) {
425 %  my %plandata = map { /^(\w+) (.*)$/; ($1, $2); }
426 %                   split(/\n/, $part_bill_event->plandata);
427 %  my $html = $events{$event}{html};
428 %  if ( ref($html) eq 'CODE' ) {
429 %    $html = &{$html}(\%plandata);
430 %  }
431 %  while ( $html =~ /%%%(\w+)%%%/ ) {
432 %    my $field = $1;
433 %    $html =~ s/%%%$field%%%/$plandata{$field}/;
434 %  }
435 %
436 <SCRIPT TYPE="text/javascript">myreasons.push('<% $events{$event}{reason} %>');
437 </SCRIPT>
438 %  if ($event eq $part_bill_event->plan){
439 %    $currentreasonclass=$events{$event}{reason};
440 %  }
441 %  print ntable( "#cccccc", 2).
442 %        qq!<TR><TD><INPUT TYPE="radio" NAME="plan_weight_eventcode" !;
443 %  print "CHECKED " if $event eq $part_bill_event->plan;
444 %  print qq!onClick="showhide_table()" !;
445 %  print qq!VALUE="!.  $event. ":". $events{$event}{weight}. ":".
446 %        encode_entities($events{$event}{code}).
447 %        qq!">$events{$event}{name}</TD>!;
448 %  print '<TD>'. $html. '</TD>' if $html;
449 %  print qq!</TR>!;
450 %  print '</TABLE>';
451 %}
452 %
453 %  if ($currentreasonclass eq 'C'){
454 %    if ($cgi->param('creason') =~ /^(-?\d+)$/){
455 %      $creason =  $1;
456 %    }else{
457 %      $creason = $part_bill_event->reason;
458 %    }
459 %    if ($cgi->param('newcreasonT') =~ /^(\d+)$/){
460 %      $newcreasonT =  $1;
461 %    }
462 %    if ($cgi->param('newcreason') =~ /^([\w\s]+)$/){
463 %      $newcreason =  $1;
464 %    }
465 %  }elsif ($currentreasonclass eq 'S'){
466 %    if ($cgi->param('sreason') =~ /^(-?\d+)$/){
467 %      $sreason =  $1;
468 %    }else{
469 %      $sreason = $part_bill_event->reason;
470 %    }
471 %    if ($cgi->param('newsreasonT') =~ /^(\d+)$/){
472 %      $newsreasonT =  $1;
473 %    }
474 %    if ($cgi->param('newsreason') =~ /^([\w\s]+)$/){
475 %      $newsreason =  $1;
476 %    }
477 %  }
478 %
479
480 </TD></TR>
481 </TABLE>
482
483 <SCRIPT TYPE="text/javascript">
484   function showhide_table()
485   {
486     for(i=0;i<document.editEvent.plan_weight_eventcode.length;i++){
487       if (document.editEvent.plan_weight_eventcode[i].checked == true){
488         currentevent=i;
489       }
490     }
491     if(myreasons[currentevent] == 'C'){
492       document.getElementById('Ctable').style.display = 'inline';
493       document.getElementById('Stable').style.display = 'none';
494     }else if(myreasons[currentevent] == 'S'){
495       document.getElementById('Ctable').style.display = 'none';
496       document.getElementById('Stable').style.display = 'inline';
497     }else{
498       document.getElementById('Ctable').style.display = 'none';
499       document.getElementById('Stable').style.display = 'none';
500     }
501   }
502 </SCRIPT>
503
504 <TABLE BGCOLOR="#cccccc" BORDER=0 WIDTH="100%">
505 <TR><TD>
506 <TABLE BORDER=0 id="Ctable" style="display:<% $currentreasonclass eq 'C' ? 'inline' : 'none' %>">
507 <% include('/elements/tr-select-reason.html',
508              'field'          => 'creason',
509              'reason_class'   => 'C',
510              'curr_value'     => $creason,
511              'init_type'      => $newcreasonT,
512              'init_newreason' => $newcreason
513           )
514 %>
515 </TABLE>
516 </TR></TD>
517 </TABLE>
518
519 <TABLE BGCOLOR="#cccccc" BORDER=0 WIDTH="100%">
520 <TR><TD>
521 <TABLE BORDER=0 id="Stable" style="display:<% $currentreasonclass eq 'S' ? 'inline' : 'none' %>">
522 <% include('/elements/tr-select-reason.html',
523              'field'          => 'sreason',
524              'reason_class'   => 'S',
525              'curr_value'     => $sreason,
526              'init_type'      => $newsreasonT,
527              'init_newreason' => $newsreason
528           )
529 %>
530 </TABLE>
531 </TR></TD>
532 </TABLE>
533     
534 %
535 %print qq!<INPUT TYPE="submit" VALUE="!,
536 %      $hashref->{eventpart} ? "Apply changes" : "Add invoice event",
537 %      qq!">!;
538 %
539
540
541     </FORM>
542   </BODY>
543 </HTML>
544
545