summaryrefslogtreecommitdiff
path: root/httemplate/edit/part_bill_event.cgi
blob: 7e27bd400c7c2cf2ca670ee08a674cf3cad1f76f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!-- mason kludge -->
<%

if ( $cgi->param('eventpart') && $cgi->param('eventpart') =~ /^(\d+)$/ ) {
  $cgi->param('eventpart', $1);
} else {
  $cgi->param('eventpart', '');
}

my ($query) = $cgi->keywords;
my $action = '';
my $part_bill_event = '';
if ( $cgi->param('error') ) {
  $part_bill_event = new FS::part_bill_event ( {
    map { $_, scalar($cgi->param($_)) } fields('part_bill_event')
  } );
}
if ( $query && $query =~ /^(\d+)$/ ) {
  $part_bill_event ||= qsearchs('part_bill_event',{'eventpart'=>$1});
} else {
  $part_bill_event ||= new FS::part_bill_event {};
}
$action ||= $part_bill_event->eventpart ? 'Edit' : 'Add';
my $hashref = $part_bill_event->hashref;

print header("$action Invoice Event Definition", menubar(
  'Main Menu' => popurl(2),
  'View all invoice events' => popurl(2). 'browse/part_bill_event.cgi',
));

print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
      "</FONT>"
  if $cgi->param('error');

print '<FORM ACTION="', popurl(1), 'process/part_bill_event.cgi" METHOD=POST>'.
      '<INPUT TYPE="hidden" NAME="eventpart" VALUE="'.
      $part_bill_event->eventpart  .'">';
print "Invoice Event #", $hashref->{eventpart} ? $hashref->{eventpart} : "(NEW)";

print ntable("#cccccc",2), <<END;
<TR><TD ALIGN="right">Payby</TD><TD><SELECT NAME="payby">
END

for (qw(CARD DCRD CHEK DCHK LECB BILL COMP)) {
  print qq!<OPTION VALUE="$_"!;
  if ($part_bill_event->payby eq $_) {
    print " SELECTED>$_</OPTION>";
  } else {
    print ">$_</OPTION>";
  }
}

my $days = $hashref->{seconds}/86400;

print <<END;
</SELECT></TD></TR>
<TR><TD ALIGN="right">Event</TD><TD><INPUT TYPE="text" NAME="event" VALUE="$hashref->{event}"></TD></TR>
<TR><TD ALIGN="right">After</TD><TD><INPUT TYPE="text" NAME="days" VALUE="$days"> days</TD></TR>
END

print '<TR><TD ALIGN="right">Disabled</TD><TD>';
print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
print ' CHECKED' if $hashref->{disabled} eq "Y";
print '>';
print '</TD></TR>';

print '<TR><TD ALIGN="right">Action</TD><TD>';

#print ntable();

sub select_pkgpart {
  my $label = shift;
  my $plandata = shift;
  my %selected = map { $_=>1 } split(/,\s*/, $plandata->{$label});
  qq(<SELECT NAME="$label" MULTIPLE>).
  join("\n", map {
    '<OPTION VALUE="'. $_->pkgpart. '"'.
    ( $selected{$_->pkgpart} ? ' SELECTED' : '' ).
    '>'. $_->pkg. ' - '. $_->comment
  } qsearch('part_pkg', { 'disabled' => '' } ) ).
  '</SELECT>';
}

sub select_agentnum {
  my $plandata = shift;
  #my $agentnum = $plandata->{'agentnum'};
  my %agentnums = map { $_=>1 } split(/,\s*/, $plandata->{'agentnum'});
  '<SELECT NAME="agentnum" MULTIPLE>'.
  join("\n", map {
    '<OPTION VALUE="'. $_->agentnum. '"'.
    ( $agentnums{$_->agentnum} ? ' SELECTED' : '' ).
    '>'. $_->agent
  } qsearch('agent', { 'disabled' => '' } ) ).
  '</SELECT>';
}

#this is pretty kludgy right here.
tie my %events, 'Tie::IxHash',

  'fee' => {
    'name'   => 'Late fee',
    'code'   => '$cust_main->charge( %%%charge%%%, \'%%%reason%%%\' );',
    'html'   => 
      'Amount <INPUT TYPE="text" SIZE="7" NAME="charge" VALUE="%%%charge%%%">'.
      '<BR>Reason <INPUT TYPE="text" NAME="reason" VALUE="%%%reason%%%">',
    'weight' => 10,
  },
  'suspend' => {
    'name'   => 'Suspend',
    'code'   => '$cust_main->suspend();',
    'weight' => 10,
  },
  'suspend-if-pkgpart' => {
    'name'   => 'Suspend packages',
    'code'   => '$cust_main->suspend_if_pkgpart(%%%if_pkgpart%%%);',
    'html'   => sub { &select_pkgpart('if_pkgpart', @_) },
    'weight' => 10,
  },
  'suspend-unless-pkgpart' => {
    'name'   => 'Suspend packages except',
    'code'   => '$cust_main->suspend_unless_pkgpart(%%%unless_pkgpart%%%);',
    'html'   => sub { &select_pkgpart('unless_pkgpart', @_) },
    'weight' => 10,
  },
  'cancel' => {
    'name'   => 'Cancel',
    'code'   => '$cust_main->cancel();',
    'weight' => 10,
  },

  'addpost' => {
    'name' => 'Add postal invoicing',
    'code' => '$cust_main->invoicing_list_addpost(); "";',
    'weight'  => 20,
  },

  'comp' => {
    'name' => 'Pay invoice with a complimentary "payment"',
    'code' => '$cust_bill->comp();',
    'weight' => 30,
  },

  'realtime-card' => {
    'name' => 'Run card with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
    'code' => '$cust_bill->realtime_card();',
    'weight' => 30,
  },

  'realtime-check' => {
    'name' => 'Run check with a <a href="http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment">Business::OnlinePayment</a> realtime gateway',
    'code' => '$cust_bill->realtime_ach();',
    'weight' => 30,
  },

  'realtime-lec' => {
    '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',
    'code' => '$cust_bill->realtime_lec();',
    'weight' => 30,
  },

  'batch-card' => {
    'name' => 'Add card to the pending credit card batch',
    'code' => '$cust_bill->batch_card();',
    'weight' => 40,
  },

  'send' => {
    'name' => 'Send invoice (email/print)',
    'code' => '$cust_bill->send();',
    'weight' => 50,
  },

  'send_alternate' => {
    'name' => 'Send invoice (email/print) with alternate template',
    'code' => '$cust_bill->send(\'%%%templatename%%%\');',
    'html' =>
        '<INPUT TYPE="text" NAME="templatename" VALUE="%%%templatename%%%">',
    'weight' => 50,
  },

  'send_if_newest' => {
    '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)',
    'code' => '$cust_bill->send_if_newest(\'%%%if_newest_templatename%%%\');',
    'html' =>
        '<INPUT TYPE="text" NAME="if_newest_templatename" VALUE="%%%if_newest_templatename%%%">',
    'weight' => 50,
  },

  'send_agent' => {
    'name' => 'Send invoice (email/print) ',
    'code' => '$cust_bill->send(\'%%%agent_templatename%%%\', [ %%%agentnum%%% ], \'%%%agent_invoice_from%%%\');',
    'html' => sub {
        '<TABLE BORDER=0>
          <TR>
            <TD ALIGN="right">only for agent(s) </TD>
            <TD>'. &select_agentnum(@_). '</TD>
          </TR>
          <TR>
            <TD ALIGN="right">with template </TD>
            <TD>
              <INPUT TYPE="text" NAME="agent_templatename" VALUE="%%%agent_templatename%%%">
            </TD>
          </TR>
          <TR>
            <TD ALIGN="right">email From: </TD>
            <TD>
              <INPUT TYPE="text" NAME="agent_invoice_from" VALUE="%%%agent_invoice_from%%%">
            </TD>
          </TR>
        </TABLE>';
    },
    'weight' => 50,
  },

  'send_csv_ftp' => {
    'name' => 'Upload CSV invoice data to an FTP server',
    'code' => '$cust_bill->send_csv( protocol   => \'ftp\',
                                     server     => \'%%%ftpserver%%%\',
                                     username   => \'%%%ftpusername%%%\',
                                     password   => \'%%%ftppassword%%%\',
                                     dir        => \'%%%ftpdir%%%\',
                                     \'format\' => \'%%%ftpformat%%%\',
                                   );',
    'html' =>
        '<TABLE BORDER=0>'.
        '<TR><TD align="right">Format ("default" or "billco"): </TD>'.
          '<TD>'.
            '<!--'.
            '<SELECT NAME="ftpformat">'.
              '<OPTION VALUE="default">Default'.
              '<OPTION VALUE="billco">Billco'.
            '</SELECT>'.
            '-->'.
            '<INPUT TYPE="text" NAME="ftpformat" VALUE="%%%ftpformat%%%">'.
          '</TD></TR>'.
        '<TR><TD ALIGN="right">FTP server: </TD>'.
          '<TD><INPUT TYPE="text" NAME="ftpserver" VALUE="%%%ftpserver%%%">'.
          '</TD></TR>'.
        '<TR><TD ALIGN="right">FTP username: </TD><TD>'.
          '<INPUT TYPE="text" NAME="ftpusername" VALUE="%%%ftpusername%%%">'.
          '</TD></TR>'.
        '<TR><TD ALIGN="right">FTP password: </TD><TD>'.
          '<INPUT TYPE="text" NAME="ftppassword" VALUE="%%%ftppassword%%%">'.
          '</TD></TR>'.
        '<TR><TD ALIGN="right">FTP directory: </TD>'.
          '<TD><INPUT TYPE="text" NAME="ftpdir" VALUE="%%%ftpdir%%%">'.
          '</TD></TR>'.
        '</TABLE>',
    'weight' => 50,
  },

  'spool_csv' => {
    'name' => 'Spool CSV invoice data',
    'code' => '$cust_bill->spool_csv( \'format\' => \'%%%spoolformat%%%\',
                                      \'dest\'   => \'%%%spooldest%%%\',
                                    );',
    'html' =>
        '<TABLE BORDER=0>'.
        '<TR><TD align="right">Format ("default" or "billco"): </TD>'.
          '<TD>'.
            '<!--'.
            '<SELECT NAME="spoolformat">'.
              '<OPTION VALUE="default">Default'.
              '<OPTION VALUE="billco">Billco'.
            '</SELECT>'.
            '-->'.
            '<INPUT TYPE="text" NAME="spoolformat" VALUE="%%%spoolformat%%%">'.
          '</TD></TR>'.
        '<TR><TD align="right">For destination: </TD>'.
          '<TD>'.
            '<SELECT NAME="spooldest">'.
              '<OPTION VALUE="">(all)'.
              '<OPTION VALUE="POST">Postal mail'.
              '<OPTION VALUE="EMAIL">Email'.
              '<OPTION VALUE="Fax">Fax'.
            '</SELECT>'.
          '</TD></TR>'.
        '</TABLE>',
    'weight' => 50,
  },

  'bill' => {
    'name' => 'Generate invoices (normally only used with a <i>Late Fee</i> event)',
    'code' => '$cust_main->bill();',
    'weight'  => 60,
  },

  'apply' => {
    'name' => 'Apply unapplied payments and credits',
    'code' => '$cust_main->apply_payments; $cust_main->apply_credits; "";',
    'weight'  => 70,
  },

  'collect' => {
    'name' => 'Collect on invoices (normally only used with a <i>Late Fee</i> and <i>Generate Invoice</i> events)',
    'code' => '$cust_main->collect();',
    'weight'  => 80,
  },

;

foreach my $event ( keys %events ) {
  my %plandata = map { /^(\w+) (.*)$/; ($1, $2); }
                   split(/\n/, $part_bill_event->plandata);
  my $html = $events{$event}{html};
  if ( ref($html) eq 'CODE' ) {
    $html = &{$html}(\%plandata);
  }
  while ( $html =~ /%%%(\w+)%%%/ ) {
    my $field = $1;
    $html =~ s/%%%$field%%%/$plandata{$field}/;
  }

  print ntable( "#cccccc", 2).
        qq!<TR><TD><INPUT TYPE="radio" NAME="plan_weight_eventcode" !;
  print "CHECKED " if $event eq $part_bill_event->plan;
  print qq!VALUE="!.  $event. ":". $events{$event}{weight}. ":".
        encode_entities($events{$event}{code}).
        qq!">$events{$event}{name}</TD>!;
  print '<TD>'. $html. '</TD>' if $html;
  print qq!</TR>!;
  print '</TABLE>';
}

#print '</TABLE>';

print <<END;
</TD></TR>
</TABLE>
END

print qq!<INPUT TYPE="submit" VALUE="!,
      $hashref->{eventpart} ? "Apply changes" : "Add invoice event",
      qq!">!;
%>

    </FORM>
  </BODY>
</HTML>