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