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