1.7 sucks. add a "balance over" option to the 1.7 style agent-specific invoice send...
[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%%%\',
253 %                                 [ %%%agentnum%%% ],
254 %                                 \'%%%agent_invoice_from%%%\',
255 %                                 %%%agent_balanceover%%%
256 %                               );',
257 %    'html' => sub {
258 %        '<TABLE BORDER=0>
259 %          <TR>
260 %            <TD ALIGN="right">only for agent(s) </TD>
261 %            <TD>'. &select_agentnum(@_). '</TD>
262 %          </TR>
263 %          <TR>
264 %            <TD ALIGN="right">with template </TD>
265 %            <TD>
266 %              <INPUT TYPE="text" NAME="agent_templatename" VALUE="%%%agent_templatename%%%">
267 %            </TD>
268 %          </TR>
269 %          <TR>
270 %            <TD ALIGN="right">email From: </TD>
271 %            <TD>
272 %              <INPUT TYPE="text" NAME="agent_invoice_from" VALUE="%%%agent_invoice_from%%%">
273 %            </TD>
274 %          </TR>
275 %          <TR>
276 %            <TD ALIGN="right">if balance (this invoice and previous) over
277 %            </TD>
278 %            <TD>
279 %              '. $money_char. '<INPUT TYPE="text" SIZE="7" NAME="agent_balanceover" VALUE="%%%agent_balanceover%%%">
280 %            </TD>
281 %          </TR>
282 %        </TABLE>';
283 %    },
284 %    'weight' => 50,
285 %  },
286 %
287 %  'send_csv_ftp' => {
288 %    'name' => 'Upload CSV invoice data to an FTP server',
289 %    'code' => '$cust_bill->send_csv( protocol   => \'ftp\',
290 %                                     server     => \'%%%ftpserver%%%\',
291 %                                     username   => \'%%%ftpusername%%%\',
292 %                                     password   => \'%%%ftppassword%%%\',
293 %                                     dir        => \'%%%ftpdir%%%\',
294 %                                     \'format\' => \'%%%ftpformat%%%\',
295 %                                   );',
296 %    'html' =>
297 %        '<TABLE BORDER=0>'.
298 %        '<TR><TD ALIGN="right">Format ("default" or "billco"): </TD>'.
299 %          '<TD>'.
300 %            '<!--'.
301 %            '<SELECT NAME="ftpformat">'.
302 %              '<OPTION VALUE="default">Default'.
303 %              '<OPTION VALUE="billco">Billco'.
304 %            '</SELECT>'.
305 %            '-->'.
306 %            '<INPUT TYPE="text" NAME="ftpformat" VALUE="%%%ftpformat%%%">'.
307 %          '</TD></TR>'.
308 %        '<TR><TD ALIGN="right">FTP server: </TD>'.
309 %          '<TD><INPUT TYPE="text" NAME="ftpserver" VALUE="%%%ftpserver%%%">'.
310 %          '</TD></TR>'.
311 %        '<TR><TD ALIGN="right">FTP username: </TD><TD>'.
312 %          '<INPUT TYPE="text" NAME="ftpusername" VALUE="%%%ftpusername%%%">'.
313 %          '</TD></TR>'.
314 %        '<TR><TD ALIGN="right">FTP password: </TD><TD>'.
315 %          '<INPUT TYPE="text" NAME="ftppassword" VALUE="%%%ftppassword%%%">'.
316 %          '</TD></TR>'.
317 %        '<TR><TD ALIGN="right">FTP directory: </TD>'.
318 %          '<TD><INPUT TYPE="text" NAME="ftpdir" VALUE="%%%ftpdir%%%">'.
319 %          '</TD></TR>'.
320 %        '</TABLE>',
321 %    'weight' => 50,
322 %  },
323 %
324 %  'spool_csv' => {
325 %    'name' => 'Spool CSV invoice data',
326 %    'code' => '$cust_bill->spool_csv(
327 %                 \'format\' => \'%%%spoolformat%%%\',
328 %                 \'dest\'   => \'%%%spooldest%%%\',
329 %                 \'balanceover\' => \'%%%spoolbalanceover%%%\',
330 %                 \'agent_spools\' => \'%%%spoolagent_spools%%%\',
331 %               );',
332 %    'html' => sub {
333 %       my $plandata = shift;
334 %
335 %       my $html =
336 %       '<TABLE BORDER=0>'.
337 %       '<TR><TD ALIGN="right">Format: </TD>'.
338 %         '<TD>'.
339 %           '<SELECT NAME="spoolformat">';
340 %
341 %       foreach my $option (qw( default billco )) {
342 %         $html .= qq(<OPTION VALUE="$option");
343 %         $html .= ' SELECTED' if $option eq $plandata->{'spoolformat'};
344 %         $html .= ">\u$option";
345 %       }
346 %
347 %       $html .= 
348 %           '</SELECT>'.
349 %         '</TD></TR>'.
350 %       '<TR><TD ALIGN="right">For destination: </TD>'.
351 %         '<TD>'.
352 %           '<SELECT NAME="spooldest">';
353 %
354 %       tie my %dest, 'Tie::IxHash', 
355 %         ''      => '(all)',
356 %         'POST'  => 'Postal Mail',
357 %         'EMAIL' => 'Email',
358 %         'FAX'   => 'Fax',
359 %       ;
360 %
361 %       foreach my $dest (keys %dest) {
362 %         $html .= qq(<OPTION VALUE="$dest");
363 %         $html .= ' SELECTED' if $dest eq $plandata->{'spooldest'};
364 %         $html .= '>'. $dest{$dest};
365 %       }
366 %
367 %       $html .=
368 %           '</SELECT>'.
369 %         '</TD></TR>'.
370 %
371 %       '<TR>'.
372 %         '<TD ALIGN="right">if balance (this invoice and previous) over </TD>'.
373 %         '<TD>'.
374 %           "$money_char ".
375 %           '<INPUT TYPE="text" SIZE="7" NAME="spoolbalanceover" VALUE="%%%spoolbalanceover%%%">'.
376 %         '</TD>'.
377 %       '<TR><TD ALIGN="right">Individual per-agent spools? </TD>'.
378 %         '<TD><INPUT TYPE="checkbox" NAME="spoolagent_spools" VALUE="1" '.
379 %           ( $plandata->{'spoolagent_spools'} ? 'CHECKED' : '' ).
380 %           '>'.
381 %         '</TD></TR>'.
382 %       '</TABLE>';
383 %
384 %       $html;
385 %    },
386 %    'weight' => 50,
387 %  },
388 %
389 %  'bill' => {
390 %    'name' => 'Generate invoices (normally only used with a <i>Late Fee</i> event)',
391 %    'code' => '$cust_main->bill();',
392 %    'weight'  => 60,
393 %  },
394 %
395 %  'apply' => {
396 %    'name' => 'Apply unapplied payments and credits',
397 %    'code' => '$cust_main->apply_payments_and_credits; "";',
398 %    'weight'  => 70,
399 %  },
400 %
401 %;
402 %
403 <SCRIPT TYPE="text/javascript">var myreasons = new Array();</SCRIPT>
404 %foreach my $event ( keys %events ) {
405 %  my %plandata = map { /^(\w+) (.*)$/; ($1, $2); }
406 %                   split(/\n/, $part_bill_event->plandata);
407 %  my $html = $events{$event}{html};
408 %  if ( ref($html) eq 'CODE' ) {
409 %    $html = &{$html}(\%plandata);
410 %  }
411 %  while ( $html =~ /%%%(\w+)%%%/ ) {
412 %    my $field = $1;
413 %    $html =~ s/%%%$field%%%/$plandata{$field}/;
414 %  }
415 %
416 <SCRIPT TYPE="text/javascript">myreasons.push('<% $events{$event}{reason} %>');
417 </SCRIPT>
418 %  if ($event eq $part_bill_event->plan){
419 %    $currentreasonclass=$events{$event}{reason};
420 %  }
421 %  print ntable( "#cccccc", 2).
422 %        qq!<TR><TD><INPUT TYPE="radio" NAME="plan_weight_eventcode" !;
423 %  print "CHECKED " if $event eq $part_bill_event->plan;
424 %  print qq!onClick="showhide_table()" !;
425 %  print qq!VALUE="!.  $event. ":". $events{$event}{weight}. ":".
426 %        encode_entities($events{$event}{code}).
427 %        qq!">$events{$event}{name}</TD>!;
428 %  print '<TD>'. $html. '</TD>' if $html;
429 %  print qq!</TR>!;
430 %  print '</TABLE>';
431 %}
432 %
433 %  if ($currentreasonclass eq 'C'){
434 %    if ($cgi->param('creason') =~ /^(-?\d+)$/){
435 %      $creason =  $1;
436 %    }else{
437 %      $creason = $part_bill_event->reason;
438 %    }
439 %    if ($cgi->param('newcreasonT') =~ /^(\d+)$/){
440 %      $newcreasonT =  $1;
441 %    }
442 %    if ($cgi->param('newcreason') =~ /^([\w\s]+)$/){
443 %      $newcreason =  $1;
444 %    }
445 %  }elsif ($currentreasonclass eq 'S'){
446 %    if ($cgi->param('sreason') =~ /^(-?\d+)$/){
447 %      $sreason =  $1;
448 %    }else{
449 %      $sreason = $part_bill_event->reason;
450 %    }
451 %    if ($cgi->param('newsreasonT') =~ /^(\d+)$/){
452 %      $newsreasonT =  $1;
453 %    }
454 %    if ($cgi->param('newsreason') =~ /^([\w\s]+)$/){
455 %      $newsreason =  $1;
456 %    }
457 %  }
458 %
459
460 </TD></TR>
461 </TABLE>
462
463 <SCRIPT TYPE="text/javascript">
464   function showhide_table()
465   {
466     for(i=0;i<document.editEvent.plan_weight_eventcode.length;i++){
467       if (document.editEvent.plan_weight_eventcode[i].checked == true){
468         currentevent=i;
469       }
470     }
471     if(myreasons[currentevent] == 'C'){
472       document.getElementById('Ctable').style.display = 'inline';
473       document.getElementById('Stable').style.display = 'none';
474     }else if(myreasons[currentevent] == 'S'){
475       document.getElementById('Ctable').style.display = 'none';
476       document.getElementById('Stable').style.display = 'inline';
477     }else{
478       document.getElementById('Ctable').style.display = 'none';
479       document.getElementById('Stable').style.display = 'none';
480     }
481   }
482 </SCRIPT>
483
484 <TABLE BGCOLOR="#cccccc" BORDER=0 WIDTH="100%">
485 <TR><TD>
486 <TABLE BORDER=0 id="Ctable" style="display:<% $currentreasonclass eq 'C' ? 'inline' : 'none' %>">
487 <% include('/elements/tr-select-reason.html',
488              'field'          => 'creason',
489              'reason_class'   => 'C',
490              'curr_value'     => $creason,
491              'init_type'      => $newcreasonT,
492              'init_newreason' => $newcreason
493           )
494 %>
495 </TABLE>
496 </TR></TD>
497 </TABLE>
498
499 <TABLE BGCOLOR="#cccccc" BORDER=0 WIDTH="100%">
500 <TR><TD>
501 <TABLE BORDER=0 id="Stable" style="display:<% $currentreasonclass eq 'S' ? 'inline' : 'none' %>">
502 <% include('/elements/tr-select-reason.html',
503              'field'          => 'sreason',
504              'reason_class'   => 'S',
505              'curr_value'     => $sreason,
506              'init_type'      => $newsreasonT,
507              'init_newreason' => $newsreason
508           )
509 %>
510 </TABLE>
511 </TR></TD>
512 </TABLE>
513     
514 %
515 %print qq!<INPUT TYPE="submit" VALUE="!,
516 %      $hashref->{eventpart} ? "Apply changes" : "Add invoice event",
517 %      qq!">!;
518 %
519
520
521     </FORM>
522
523 <% include('/elements/footer.html') %>
524
525 <%init>
526
527 die "access denied"
528   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
529
530 if ( $cgi->param('eventpart') && $cgi->param('eventpart') =~ /^(\d+)$/ ) {
531   $cgi->param('eventpart', $1);
532 } else {
533   $cgi->param('eventpart', '');
534 }
535
536 my ($creason, $newcreasonT, $newcreason);
537 my ($sreason, $newsreasonT, $newsreason);
538
539 my ($query) = $cgi->keywords;
540 my $action = '';
541 my $part_bill_event = '';
542 my $currentreasonclass = '';
543 if ( $cgi->param('error') ) {
544   $part_bill_event = new FS::part_bill_event ( {
545     map { $_, scalar($cgi->param($_)) } fields('part_bill_event')
546   } );
547 }
548 if ( $query && $query =~ /^(\d+)$/ ) {
549   $part_bill_event ||= qsearchs('part_bill_event',{'eventpart'=>$1});
550 } else {
551   $part_bill_event ||= new FS::part_bill_event {};
552 }
553 $action ||= $part_bill_event->eventpart ? 'Edit' : 'Add';
554 my $hashref = $part_bill_event->hashref;
555
556 </%init>