*** empty log message ***
[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 ($query) = $cgi->keywords;
11 my $action = '';
12 my $part_bill_event = '';
13 if ( $cgi->param('error') ) {
14   $part_bill_event = new FS::part_bill_event ( {
15     map { $_, scalar($cgi->param($_)) } fields('part_bill_event')
16   } );
17 }
18 if ( $query && $query =~ /^(\d+)$/ ) {
19   $part_bill_event ||= qsearchs('part_bill_event',{'eventpart'=>$1});
20 } else {
21   $part_bill_event ||= new FS::part_bill_event {};
22 }
23 $action ||= $part_bill_event->eventpart ? 'Edit' : 'Add';
24 my $hashref = $part_bill_event->hashref;
25
26 %>
27
28 <%= include('/elements/header.html',
29       "$action Invoice Event Definition",
30       menubar(
31         'Main Menu' => popurl(2),
32         'View all invoice events' => popurl(2). 'browse/part_bill_event.cgi',
33       )
34     )
35 %>
36
37 <% if ( $cgi->param('error') ) { %>
38   <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $cgi->param('error') %></FONT>
39 <% } %>
40
41 <FORM ACTION="<%= popurl(1) %>process/part_bill_event.cgi" METHOD=POST>
42 <INPUT TYPE="hidden" NAME="eventpart" VALUE="<%= $part_bill_event->eventpart %>">
43 Invoice Event #<%= $hashref->{eventpart} ? $hashref->{eventpart} : "(NEW)" %>
44
45 <%=  ntable("#cccccc",2) %>
46
47   <TR>
48     <TD ALIGN="right">Event name </TD>
49     <TD><INPUT TYPE="text" NAME="event" VALUE="<%= $hashref->{event} %>"></TD>
50   </TR>
51
52   <TR>
53     <TD ALIGN="right">For </TD>
54     <TD>
55       <SELECT NAME="payby">
56
57         <% tie my %payby, 'Tie::IxHash', FS::payby->cust_payby2longname;
58            foreach my $payby ( keys %payby ) {
59         %>
60
61           <OPTION VALUE="<%= $payby %>"<%= ($part_bill_event->payby eq $payby) ? ' SELECTED' : '' %>><%= $payby{$payby} %></OPTION>
62
63         <% } %>
64
65       </SELECT> customers
66     </TD>
67   </TR>
68
69   <% my $days = $hashref->{seconds}/86400; %>
70
71   <TR>
72     <TD ALIGN="right">After</TD>
73     <TD><INPUT TYPE="text" NAME="days" VALUE="<%= $days %>"> days</TD>
74   </TR>
75
76   <TR>
77     <TD ALIGN="right">Test event</TD>
78     <TD>
79       <SELECT NAME="freq">
80
81         <% tie my %freq, 'Tie::IxHash', '1d' => 'daily', '1m' => 'monthly';
82            foreach my $freq ( keys %freq ) {
83         %>
84
85           <OPTION VALUE="<%= $freq %>"<%= ($part_bill_event->freq eq $freq) ? ' SELECTED' : '' %>><%= $freq{$freq} %></OPTION>
86
87         <% } %>
88
89       </SELECT>
90     </TD>
91   </TR>
92
93
94   <TR>
95     <TD ALIGN="right">Disabled</TD>
96     <TD>
97       <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<%= $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>>
98     </TD>
99   </TR>
100
101   <TR>
102     <TD VALIGN="top" ALIGN="right">Action</TD>
103     <TD>
104
105 <%
106
107 #print ntable();
108
109 sub select_pkgpart {
110   my $label = shift;
111   my $plandata = shift;
112   my %selected = map { $_=>1 } split(/,\s*/, $plandata->{$label});
113   qq(<SELECT NAME="$label" MULTIPLE>).
114   join("\n", map {
115     '<OPTION VALUE="'. $_->pkgpart. '"'.
116     ( $selected{$_->pkgpart} ? ' SELECTED' : '' ).
117     '>'. $_->pkg. ' - '. $_->comment
118   } qsearch('part_pkg', { 'disabled' => '' } ) ).
119   '</SELECT>';
120 }
121
122 sub select_agentnum {
123   my $plandata = shift;
124   #my $agentnum = $plandata->{'agentnum'};
125   my %agentnums = map { $_=>1 } split(/,\s*/, $plandata->{'agentnum'});
126   '<SELECT NAME="agentnum" MULTIPLE>'.
127   join("\n", map {
128     '<OPTION VALUE="'. $_->agentnum. '"'.
129     ( $agentnums{$_->agentnum} ? ' SELECTED' : '' ).
130     '>'. $_->agent
131   } qsearch('agent', { 'disabled' => '' } ) ).
132   '</SELECT>';
133 }
134
135 my $conf = new FS::Conf;
136 my $money_char = $conf->config('money_char') || '$';
137
138 #this is pretty kludgy right here.
139 tie my %events, 'Tie::IxHash',
140
141   'fee' => {
142     'name'   => 'Late fee',
143     'code'   => '$cust_main->charge( %%%charge%%%, \'%%%reason%%%\' );',
144     'html'   => 
145       'Amount <INPUT TYPE="text" SIZE="7" NAME="charge" VALUE="%%%charge%%%">'.
146       '<BR>Reason <INPUT TYPE="text" NAME="reason" VALUE="%%%reason%%%">',
147     'weight' => 10,
148   },
149   'suspend' => {
150     'name'   => 'Suspend',
151     'code'   => '$cust_main->suspend();',
152     'weight' => 10,
153   },
154   'suspend-if-balance' => {
155     'name'   => 'Suspend if balance (this invoice and previous) over',
156     'code'   => '$cust_bill->cust_suspend_if_balance_over( %%%balanceover%%% );',
157     'html'   => " $money_char ". '<INPUT TYPE="text" SIZE="7" NAME="balanceover" VALUE="%%%balanceover%%%">',
158     'weight' => 10,
159   },
160   'suspend-if-pkgpart' => {
161     'name'   => 'Suspend packages',
162     'code'   => '$cust_main->suspend_if_pkgpart(%%%if_pkgpart%%%);',
163     'html'   => sub { &select_pkgpart('if_pkgpart', @_) },
164     'weight' => 10,
165   },
166   'suspend-unless-pkgpart' => {
167     'name'   => 'Suspend packages except',
168     'code'   => '$cust_main->suspend_unless_pkgpart(%%%unless_pkgpart%%%);',
169     'html'   => sub { &select_pkgpart('unless_pkgpart', @_) },
170     'weight' => 10,
171   },
172   'cancel' => {
173     'name'   => 'Cancel',
174     'code'   => '$cust_main->cancel();',
175     'weight' => 10,
176   },
177
178   'addpost' => {
179     'name' => 'Add postal invoicing',
180     'code' => '$cust_main->invoicing_list_addpost(); "";',
181     'weight'  => 20,
182   },
183
184   'comp' => {
185     'name' => 'Pay invoice with a complimentary "payment"',
186     'code' => '$cust_bill->comp();',
187     'weight' => 30,
188   },
189
190   'realtime-card' => {
191     'name' => 'Run card with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
192     'code' => '$cust_bill->realtime_card();',
193     'weight' => 30,
194   },
195
196   'realtime-check' => {
197     'name' => 'Run check with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
198     'code' => '$cust_bill->realtime_ach();',
199     'weight' => 30,
200   },
201
202   'realtime-lec' => {
203     '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',
204     'code' => '$cust_bill->realtime_lec();',
205     'weight' => 30,
206   },
207
208   'batch-card' => {
209     'name' => 'Add card to the pending credit card batch',
210     'code' => '$cust_bill->batch_card();',
211     'weight' => 40,
212   },
213
214   'send' => {
215     'name' => 'Send invoice (email/print/fax)',
216     'code' => '$cust_bill->send();',
217     'weight' => 50,
218   },
219
220   'send_alternate' => {
221     'name' => 'Send invoice (email/print/fax) with alternate template',
222     'code' => '$cust_bill->send(\'%%%templatename%%%\');',
223     'html' =>
224         '<INPUT TYPE="text" NAME="templatename" VALUE="%%%templatename%%%">',
225     'weight' => 50,
226   },
227
228   'send_if_newest' => {
229     '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)',
230     'code' => '$cust_bill->send_if_newest(\'%%%if_newest_templatename%%%\');',
231     'html' =>
232         '<INPUT TYPE="text" NAME="if_newest_templatename" VALUE="%%%if_newest_templatename%%%">',
233     'weight' => 50,
234   },
235
236   'send_agent' => {
237     'name' => 'Send invoice (email/print/fax) ',
238     'code' => '$cust_bill->send(\'%%%agent_templatename%%%\', [ %%%agentnum%%% ], \'%%%agent_invoice_from%%%\');',
239     'html' => sub {
240         '<TABLE BORDER=0>
241           <TR>
242             <TD ALIGN="right">only for agent(s) </TD>
243             <TD>'. &select_agentnum(@_). '</TD>
244           </TR>
245           <TR>
246             <TD ALIGN="right">with template </TD>
247             <TD>
248               <INPUT TYPE="text" NAME="agent_templatename" VALUE="%%%agent_templatename%%%">
249             </TD>
250           </TR>
251           <TR>
252             <TD ALIGN="right">email From: </TD>
253             <TD>
254               <INPUT TYPE="text" NAME="agent_invoice_from" VALUE="%%%agent_invoice_from%%%">
255             </TD>
256           </TR>
257         </TABLE>';
258     },
259     'weight' => 50,
260   },
261
262   'send_csv_ftp' => {
263     'name' => 'Upload CSV invoice data to an FTP server',
264     'code' => '$cust_bill->send_csv( protocol   => \'ftp\',
265                                      server     => \'%%%ftpserver%%%\',
266                                      username   => \'%%%ftpusername%%%\',
267                                      password   => \'%%%ftppassword%%%\',
268                                      dir        => \'%%%ftpdir%%%\',
269                                      \'format\' => \'%%%ftpformat%%%\',
270                                    );',
271     'html' =>
272         '<TABLE BORDER=0>'.
273         '<TR><TD ALIGN="right">Format ("default" or "billco"): </TD>'.
274           '<TD>'.
275             '<!--'.
276             '<SELECT NAME="ftpformat">'.
277               '<OPTION VALUE="default">Default'.
278               '<OPTION VALUE="billco">Billco'.
279             '</SELECT>'.
280             '-->'.
281             '<INPUT TYPE="text" NAME="ftpformat" VALUE="%%%ftpformat%%%">'.
282           '</TD></TR>'.
283         '<TR><TD ALIGN="right">FTP server: </TD>'.
284           '<TD><INPUT TYPE="text" NAME="ftpserver" VALUE="%%%ftpserver%%%">'.
285           '</TD></TR>'.
286         '<TR><TD ALIGN="right">FTP username: </TD><TD>'.
287           '<INPUT TYPE="text" NAME="ftpusername" VALUE="%%%ftpusername%%%">'.
288           '</TD></TR>'.
289         '<TR><TD ALIGN="right">FTP password: </TD><TD>'.
290           '<INPUT TYPE="text" NAME="ftppassword" VALUE="%%%ftppassword%%%">'.
291           '</TD></TR>'.
292         '<TR><TD ALIGN="right">FTP directory: </TD>'.
293           '<TD><INPUT TYPE="text" NAME="ftpdir" VALUE="%%%ftpdir%%%">'.
294           '</TD></TR>'.
295         '</TABLE>',
296     'weight' => 50,
297   },
298
299   'spool_csv' => {
300     'name' => 'Spool CSV invoice data',
301     'code' => '$cust_bill->spool_csv(
302                  \'format\' => \'%%%spoolformat%%%\',
303                  \'dest\'   => \'%%%spooldest%%%\',
304                  \'balanceover\' => \'%%%spoolbalanceover%%%\',
305                  \'agent_spools\' => \'%%%spoolagent_spools%%%\',
306                );',
307     'html' => sub {
308        my $plandata = shift;
309
310        my $html =
311        '<TABLE BORDER=0>'.
312        '<TR><TD ALIGN="right">Format: </TD>'.
313          '<TD>'.
314            '<SELECT NAME="spoolformat">';
315
316        foreach my $option (qw( default billco )) {
317          $html .= qq(<OPTION VALUE="$option");
318          $html .= ' SELECTED' if $option eq $plandata->{'spoolformat'};
319          $html .= ">\u$option";
320        }
321
322        $html .= 
323            '</SELECT>'.
324          '</TD></TR>'.
325        '<TR><TD ALIGN="right">For destination: </TD>'.
326          '<TD>'.
327            '<SELECT NAME="spooldest">';
328
329        tie my %dest, 'Tie::IxHash', 
330          ''      => '(all)',
331          'POST'  => 'Postal Mail',
332          'EMAIL' => 'Email',
333          'FAX'   => 'Fax',
334        ;
335
336        foreach my $dest (keys %dest) {
337          $html .= qq(<OPTION VALUE="$dest");
338          $html .= ' SELECTED' if $dest eq $plandata->{'spooldest'};
339          $html .= '>'. $dest{$dest};
340        }
341
342        $html .=
343            '</SELECT>'.
344          '</TD></TR>'.
345
346        '<TR>'.
347          '<TD ALIGN="right">if balance (this invoice and previous) over </TD>'.
348          '<TD>'.
349            "$money_char ".
350            '<INPUT TYPE="text" SIZE="7" NAME="spoolbalanceover" VALUE="%%%spoolbalanceover%%%">'.
351          '</TD>'.
352        '<TR><TD ALIGN="right">Individual per-agent spools? </TD>'.
353          '<TD><INPUT TYPE="checkbox" NAME="spoolagent_spools" VALUE="1" '.
354            ( $plandata->{'spoolagent_spools'} ? 'CHECKED' : '' ).
355            '>'.
356          '</TD></TR>'.
357        '</TABLE>';
358
359        $html;
360     },
361     'weight' => 50,
362   },
363
364   'bill' => {
365     'name' => 'Generate invoices (normally only used with a <i>Late Fee</i> event)',
366     'code' => '$cust_main->bill();',
367     'weight'  => 60,
368   },
369
370   'apply' => {
371     'name' => 'Apply unapplied payments and credits',
372     'code' => '$cust_main->apply_payments; $cust_main->apply_credits; "";',
373     'weight'  => 70,
374   },
375
376   'collect' => {
377     'name' => 'Collect on invoices (normally only used with a <i>Late Fee</i> and <i>Generate Invoice</i> events)',
378     'code' => '$cust_main->collect();',
379     'weight'  => 80,
380   },
381
382 ;
383
384 foreach my $event ( keys %events ) {
385   my %plandata = map { /^(\w+) (.*)$/; ($1, $2); }
386                    split(/\n/, $part_bill_event->plandata);
387   my $html = $events{$event}{html};
388   if ( ref($html) eq 'CODE' ) {
389     $html = &{$html}(\%plandata);
390   }
391   while ( $html =~ /%%%(\w+)%%%/ ) {
392     my $field = $1;
393     $html =~ s/%%%$field%%%/$plandata{$field}/;
394   }
395
396   print ntable( "#cccccc", 2).
397         qq!<TR><TD><INPUT TYPE="radio" NAME="plan_weight_eventcode" !;
398   print "CHECKED " if $event eq $part_bill_event->plan;
399   print qq!VALUE="!.  $event. ":". $events{$event}{weight}. ":".
400         encode_entities($events{$event}{code}).
401         qq!">$events{$event}{name}</TD>!;
402   print '<TD>'. $html. '</TD>' if $html;
403   print qq!</TR>!;
404   print '</TABLE>';
405 }
406
407 #print '</TABLE>';
408
409 print <<END;
410 </TD></TR>
411 </TABLE>
412 END
413
414 print qq!<INPUT TYPE="submit" VALUE="!,
415       $hashref->{eventpart} ? "Apply changes" : "Add invoice event",
416       qq!">!;
417 %>
418
419     </FORM>
420   </BODY>
421 </HTML>
422