events should attach reasons
[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 %  'realtime-card' => {
210 %    'name' => 'Run card with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
211 %    'code' => '$cust_bill->realtime_card();',
212 %    'weight' => 30,
213 %  },
214 %
215 %  'realtime-check' => {
216 %    'name' => 'Run check with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
217 %    'code' => '$cust_bill->realtime_ach();',
218 %    'weight' => 30,
219 %  },
220 %
221 %  'realtime-lec' => {
222 %    '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',
223 %    'code' => '$cust_bill->realtime_lec();',
224 %    'weight' => 30,
225 %  },
226 %
227 %  'batch-card' => {
228 %    'name' => 'Add card or check to a pending batch',
229 %    'code' => '$cust_bill->batch_card(%options);',
230 %    'weight' => 40,
231 %  },
232 %
233 %  
234 %  #'retriable' => {
235 %  #  'name' => 'Mark batched card event as retriable',
236 %  #  'code' => '$cust_pay_batch->retriable();',
237 %  #  'weight' => 60,
238 %  #},
239 %
240 %  'send' => {
241 %    'name' => 'Send invoice (email/print/fax)',
242 %    'code' => '$cust_bill->send();',
243 %    'weight' => 50,
244 %  },
245 %
246 %  'send_alternate' => {
247 %    'name' => 'Send invoice (email/print/fax) with alternate template',
248 %    'code' => '$cust_bill->send(\'%%%templatename%%%\');',
249 %    'html' =>
250 %        '<INPUT TYPE="text" NAME="templatename" VALUE="%%%templatename%%%">',
251 %    'weight' => 50,
252 %  },
253 %
254 %  'send_if_newest' => {
255 %    '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)',
256 %    'code' => '$cust_bill->send_if_newest(\'%%%if_newest_templatename%%%\');',
257 %    'html' =>
258 %        '<INPUT TYPE="text" NAME="if_newest_templatename" VALUE="%%%if_newest_templatename%%%">',
259 %    'weight' => 50,
260 %  },
261 %
262 %  'send_agent' => {
263 %    'name' => 'Send invoice (email/print/fax) ',
264 %    'code' => '$cust_bill->send(\'%%%agent_templatename%%%\', [ %%%agentnum%%% ], \'%%%agent_invoice_from%%%\');',
265 %    'html' => sub {
266 %        '<TABLE BORDER=0>
267 %          <TR>
268 %            <TD ALIGN="right">only for agent(s) </TD>
269 %            <TD>'. &select_agentnum(@_). '</TD>
270 %          </TR>
271 %          <TR>
272 %            <TD ALIGN="right">with template </TD>
273 %            <TD>
274 %              <INPUT TYPE="text" NAME="agent_templatename" VALUE="%%%agent_templatename%%%">
275 %            </TD>
276 %          </TR>
277 %          <TR>
278 %            <TD ALIGN="right">email From: </TD>
279 %            <TD>
280 %              <INPUT TYPE="text" NAME="agent_invoice_from" VALUE="%%%agent_invoice_from%%%">
281 %            </TD>
282 %          </TR>
283 %        </TABLE>';
284 %    },
285 %    'weight' => 50,
286 %  },
287 %
288 %  'send_csv_ftp' => {
289 %    'name' => 'Upload CSV invoice data to an FTP server',
290 %    'code' => '$cust_bill->send_csv( protocol   => \'ftp\',
291 %                                     server     => \'%%%ftpserver%%%\',
292 %                                     username   => \'%%%ftpusername%%%\',
293 %                                     password   => \'%%%ftppassword%%%\',
294 %                                     dir        => \'%%%ftpdir%%%\',
295 %                                     \'format\' => \'%%%ftpformat%%%\',
296 %                                   );',
297 %    'html' =>
298 %        '<TABLE BORDER=0>'.
299 %        '<TR><TD ALIGN="right">Format ("default" or "billco"): </TD>'.
300 %          '<TD>'.
301 %            '<!--'.
302 %            '<SELECT NAME="ftpformat">'.
303 %              '<OPTION VALUE="default">Default'.
304 %              '<OPTION VALUE="billco">Billco'.
305 %            '</SELECT>'.
306 %            '-->'.
307 %            '<INPUT TYPE="text" NAME="ftpformat" VALUE="%%%ftpformat%%%">'.
308 %          '</TD></TR>'.
309 %        '<TR><TD ALIGN="right">FTP server: </TD>'.
310 %          '<TD><INPUT TYPE="text" NAME="ftpserver" VALUE="%%%ftpserver%%%">'.
311 %          '</TD></TR>'.
312 %        '<TR><TD ALIGN="right">FTP username: </TD><TD>'.
313 %          '<INPUT TYPE="text" NAME="ftpusername" VALUE="%%%ftpusername%%%">'.
314 %          '</TD></TR>'.
315 %        '<TR><TD ALIGN="right">FTP password: </TD><TD>'.
316 %          '<INPUT TYPE="text" NAME="ftppassword" VALUE="%%%ftppassword%%%">'.
317 %          '</TD></TR>'.
318 %        '<TR><TD ALIGN="right">FTP directory: </TD>'.
319 %          '<TD><INPUT TYPE="text" NAME="ftpdir" VALUE="%%%ftpdir%%%">'.
320 %          '</TD></TR>'.
321 %        '</TABLE>',
322 %    'weight' => 50,
323 %  },
324 %
325 %  'spool_csv' => {
326 %    'name' => 'Spool CSV invoice data',
327 %    'code' => '$cust_bill->spool_csv(
328 %                 \'format\' => \'%%%spoolformat%%%\',
329 %                 \'dest\'   => \'%%%spooldest%%%\',
330 %                 \'balanceover\' => \'%%%spoolbalanceover%%%\',
331 %                 \'agent_spools\' => \'%%%spoolagent_spools%%%\',
332 %               );',
333 %    'html' => sub {
334 %       my $plandata = shift;
335 %
336 %       my $html =
337 %       '<TABLE BORDER=0>'.
338 %       '<TR><TD ALIGN="right">Format: </TD>'.
339 %         '<TD>'.
340 %           '<SELECT NAME="spoolformat">';
341 %
342 %       foreach my $option (qw( default billco )) {
343 %         $html .= qq(<OPTION VALUE="$option");
344 %         $html .= ' SELECTED' if $option eq $plandata->{'spoolformat'};
345 %         $html .= ">\u$option";
346 %       }
347 %
348 %       $html .= 
349 %           '</SELECT>'.
350 %         '</TD></TR>'.
351 %       '<TR><TD ALIGN="right">For destination: </TD>'.
352 %         '<TD>'.
353 %           '<SELECT NAME="spooldest">';
354 %
355 %       tie my %dest, 'Tie::IxHash', 
356 %         ''      => '(all)',
357 %         'POST'  => 'Postal Mail',
358 %         'EMAIL' => 'Email',
359 %         'FAX'   => 'Fax',
360 %       ;
361 %
362 %       foreach my $dest (keys %dest) {
363 %         $html .= qq(<OPTION VALUE="$dest");
364 %         $html .= ' SELECTED' if $dest eq $plandata->{'spooldest'};
365 %         $html .= '>'. $dest{$dest};
366 %       }
367 %
368 %       $html .=
369 %           '</SELECT>'.
370 %         '</TD></TR>'.
371 %
372 %       '<TR>'.
373 %         '<TD ALIGN="right">if balance (this invoice and previous) over </TD>'.
374 %         '<TD>'.
375 %           "$money_char ".
376 %           '<INPUT TYPE="text" SIZE="7" NAME="spoolbalanceover" VALUE="%%%spoolbalanceover%%%">'.
377 %         '</TD>'.
378 %       '<TR><TD ALIGN="right">Individual per-agent spools? </TD>'.
379 %         '<TD><INPUT TYPE="checkbox" NAME="spoolagent_spools" VALUE="1" '.
380 %           ( $plandata->{'spoolagent_spools'} ? 'CHECKED' : '' ).
381 %           '>'.
382 %         '</TD></TR>'.
383 %       '</TABLE>';
384 %
385 %       $html;
386 %    },
387 %    'weight' => 50,
388 %  },
389 %
390 %  'bill' => {
391 %    'name' => 'Generate invoices (normally only used with a <i>Late Fee</i> event)',
392 %    'code' => '$cust_main->bill();',
393 %    'weight'  => 60,
394 %  },
395 %
396 %  'apply' => {
397 %    'name' => 'Apply unapplied payments and credits',
398 %    'code' => '$cust_main->apply_payments; $cust_main->apply_credits; "";',
399 %    'weight'  => 70,
400 %  },
401 %
402 %  'collect' => {
403 %    'name' => 'Collect on invoices (normally only used with a <i>Late Fee</i> and <i>Generate Invoice</i> events)',
404 %    'code' => '$cust_main->collect();',
405 %    'weight'  => 80,
406 %  },
407 %
408 %;
409 %
410 <SCRIPT TYPE="text/javascript">var myreasons = new Array();</SCRIPT>
411 %foreach my $event ( keys %events ) {
412 %  my %plandata = map { /^(\w+) (.*)$/; ($1, $2); }
413 %                   split(/\n/, $part_bill_event->plandata);
414 %  my $html = $events{$event}{html};
415 %  if ( ref($html) eq 'CODE' ) {
416 %    $html = &{$html}(\%plandata);
417 %  }
418 %  while ( $html =~ /%%%(\w+)%%%/ ) {
419 %    my $field = $1;
420 %    $html =~ s/%%%$field%%%/$plandata{$field}/;
421 %  }
422 %
423 <SCRIPT TYPE="text/javascript">myreasons.push('<% $events{$event}{reason} %>');
424 </SCRIPT>
425 %  if ($event eq $part_bill_event->plan){
426 %    $currentreasonclass=$events{$event}{reason};
427 %  }
428 %  print ntable( "#cccccc", 2).
429 %        qq!<TR><TD><INPUT TYPE="radio" NAME="plan_weight_eventcode" !;
430 %  print "CHECKED " if $event eq $part_bill_event->plan;
431 %  print qq!onClick="showhide_table()" !;
432 %  print qq!VALUE="!.  $event. ":". $events{$event}{weight}. ":".
433 %        encode_entities($events{$event}{code}).
434 %        qq!">$events{$event}{name}</TD>!;
435 %  print '<TD>'. $html. '</TD>' if $html;
436 %  print qq!</TR>!;
437 %  print '</TABLE>';
438 %}
439 %
440 %  if ($currentreasonclass eq 'C'){
441 %    if ($cgi->param('creason') =~ /^(-?\d+)$/){
442 %      $creason =  $1;
443 %    }else{
444 %      $creason = $part_bill_event->reason;
445 %    }
446 %    if ($cgi->param('newcreasonT') =~ /^(\d+)$/){
447 %      $newcreasonT =  $1;
448 %    }
449 %    if ($cgi->param('newcreason') =~ /^([\w\s]+)$/){
450 %      $newcreason =  $1;
451 %    }
452 %  }elsif ($currentreasonclass eq 'S'){
453 %    if ($cgi->param('sreason') =~ /^(-?\d+)$/){
454 %      $sreason =  $1;
455 %    }else{
456 %      $sreason = $part_bill_event->reason;
457 %    }
458 %    if ($cgi->param('newsreasonT') =~ /^(\d+)$/){
459 %      $newsreasonT =  $1;
460 %    }
461 %    if ($cgi->param('newsreason') =~ /^([\w\s]+)$/){
462 %      $newsreason =  $1;
463 %    }
464 %  }
465 %
466
467 </TD></TR>
468 </TABLE>
469
470 <SCRIPT TYPE="text/javascript">
471   function showhide_table()
472   {
473     for(i=0;i<document.editEvent.plan_weight_eventcode.length;i++){
474       if (document.editEvent.plan_weight_eventcode[i].checked == true){
475         currentevent=i;
476       }
477     }
478     if(myreasons[currentevent] == 'C'){
479       document.getElementById('Ctable').style.display = 'inline';
480       document.getElementById('Stable').style.display = 'none';
481     }else if(myreasons[currentevent] == 'S'){
482       document.getElementById('Ctable').style.display = 'none';
483       document.getElementById('Stable').style.display = 'inline';
484     }else{
485       document.getElementById('Ctable').style.display = 'none';
486       document.getElementById('Stable').style.display = 'none';
487     }
488   }
489 </SCRIPT>
490
491 <TABLE BGCOLOR="#cccccc" BORDER=0 WIDTH="100%">
492 <TR><TD>
493 <TABLE BORDER=0 id="Ctable" style="display:<% $currentreasonclass eq 'C' ? 'inline' : 'none' %>">
494 <% include('/elements/tr-select-reason.html', 'creason', 'C', $creason, $newcreasonT, $newcreason) %>
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', 'sreason', 'S', $sreason, $newsreasonT, $newsreason) %>
503 </TABLE>
504 </TR></TD>
505 </TABLE>
506     
507 %
508 %print qq!<INPUT TYPE="submit" VALUE="!,
509 %      $hashref->{eventpart} ? "Apply changes" : "Add invoice event",
510 %      qq!">!;
511 %
512
513
514     </FORM>
515   </BODY>
516 </HTML>
517
518