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