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