add monthly (and other) recurring to billing info, RT#36813
[freeside.git] / httemplate / view / cust_main / billing.html
1 <FONT CLASS="fsinnerbox-title"><% mt('Billing information') |h %></FONT>
2 %# If we can't see the unencrypted card, then bill now is an exercise in
3 %# frustration (without some sort of job queue magic to send it to a secure
4 %# machine, anyway)
5 %if (  $FS::CurrentUser::CurrentUser->access_right('Bill customer now')
6 %      && ! $cust_main->is_encrypted($cust_main->payinfo)
7 %   ) { 
8 %#  (<A HREF="<% $p %>misc/bill.cgi?<% $cust_main->custnum %>"><% mt('Bill now') |h %></A>)
9   <& /elements/bill.html,
10        custnum   => $cust_main->custnum,
11        label     => emt('Bill now'),
12        url       => $p.'view/cust_main.cgi?'.$cust_main->custnum,
13   &>
14 % } 
15
16 <TABLE CLASS="fsinnerbox">
17
18 %( my $balance = $cust_main->balance )
19 %  =~ s/^(\-?)(.*)$/<FONT SIZE=+1>$1<\/FONT>$money_char$2/;
20
21 <TR>
22   <TD ALIGN="right"><% mt('Balance due') |h %></TD>
23   <TD BGCOLOR="#ffffff"><B><% $balance %></B></TD>
24 </TR>
25
26 % #54: just an arbitrary number i pulled out of my goober.  in reality we'd want
27 % # to consider e.g. a histogram of num_ncancelled_packages for the entire
28 % # customer base, and compare it to a graph of the overhead for generating this
29 % # information.  (and optimize it better, we could get it more from SQL)
30 % if ( $cust_main->num_ncancelled_pkgs < 54 ) {
31 %   my $sth = dbh->prepare("
32 %     SELECT DISTINCT freq FROM cust_pkg LEFT JOIN part_pkg USING (pkgpart)
33 %       WHERE freq IS NOT NULL AND freq != '0'
34 %         AND ( cancel IS NULL OR cancel = 0 )
35 %         AND custnum = ?
36 %   ") or die $DBI::errstr;
37
38 %   $sth->execute($cust_main->custnum) or die $sth->errstr;
39
40 %   #not really a numeric sort because freqs can actually be all sorts of things
41 %   # but good enough for the 99% cases of ordering monthly quarterly annually
42 %   my @freqs = sort { $a <=> $b } map { $_->[0] } @{ $sth->fetchall_arrayref };
43
44 %   foreach my $freq (@freqs) {
45 %     my @cust_pkg = qsearch({
46 %       'table'     => 'cust_pkg',
47 %       'addl_from' => 'LEFT JOIN part_pkg USING (pkgpart)',
48 %       'hashref'   => { 'custnum' => $cust_main->custnum, },
49 %       'extra_sql' => 'AND ( cancel IS NULL OR cancel = 0 )
50 %                       AND freq = '. dbh->quote($freq),
51 %     }) or next;
52
53 %     my $freq_pretty = $cust_pkg[0]->part_pkg->freq_pretty;
54 %
55 %     my $amount = 0;
56 %     foreach my $cust_pkg (@cust_pkg) {
57 %       my $part_pkg = $cust_pkg->part_pkg;
58 %       next if $cust_pkg->susp
59 %            && ! $cust_pkg->option('suspend_bill')
60 %            && ( ! $part_pkg->option('suspend_bill')
61 %                 || $cust_pkg->option('no_suspend_bill')
62 %               );
63 %
64 %       #add recurring amounts for this package and its billing add-ons
65 %       foreach my $l_part_pkg ( $part_pkg->self_and_bill_linked ) {
66 %         $amount += $l_part_pkg->option('recur_fee');
67 %       }
68 %
69 %       #subtract amounts for any active discounts
70 %       #(there should only be one at the moment, otherwise this makes no sense)
71 %       foreach my $cust_pkg_discount ( $cust_pkg->cust_pkg_discount_active ) {
72 %         my $discount = $cust_pkg_discount->discount;
73 %         #and only one of these for each
74 %         $amount -= $discount->amount;
75 %         $amount -= $amount * $discount->percent/100;
76 %       }
77 %
78 %     }
79    
80       <TR>
81         <TH ALIGN="right"><% emt( ucfirst($freq_pretty). ' recurring' ) %></TH>
82         <TD><% $money_char. sprintf('%.2f', $amount) %></TD>
83         </TD>
84       </TR>
85 %   }
86
87 % }
88
89 % if ( $conf->exists('cust_main-select-prorate_day') ) {
90 <TR>
91   <TD ALIGN="right"><% mt('Prorate day of month') |h %></TD>
92   <TD BGCOLOR="#ffffff"><% $cust_main->prorate_day %>
93   </TD>
94 </TR>
95 % }
96
97 % if ( $conf->exists('cust_main-select-billday') 
98 %    && ($cust_main->payby eq 'CARD' || $cust_main->payby eq 'CHEK') ) {
99 <TR>
100   <TD ALIGN="right"><% mt('Billing day of month') |h %></TD>
101   <TD BGCOLOR="#ffffff"><% $cust_main->billday %>
102   </TD>
103 </TR>
104 % }
105
106 <TR>
107   <TD ALIGN="right"><% mt('Billing type') |h %></TD>
108   <TD BGCOLOR="#ffffff">
109 % if ( $cust_main->payby eq 'CARD' || $cust_main->payby eq 'DCRD' ) { 
110
111 %   my $autodemand = $cust_main->payby eq 'CARD' ? 'automatic' : 'on-demand';
112     <% mt("Credit card ([_1])",$autodemand) |h %>
113   </TD>
114 </TR>
115 <TR>
116   <TD ALIGN="right"><% mt('Card number') |h %></TD>
117   <TD BGCOLOR="#ffffff"><% $cust_main->paymask %></TD>
118 </TR>
119 %
120 %#false laziness w/elements/select-month_year.html & edit/cust_main/billing.html
121 %my( $mon, $year );
122 %my $date = $cust_main->paydate || '12-2037';
123 %if ( $date  =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
124 %  ( $mon, $year ) = ( $2, $1 );
125 %} elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
126 %  ( $mon, $year ) = ( $1, $3 );
127 %} else {
128 %  warn "unrecognized expiration date format: $date";
129 %  ( $mon, $year ) = ( '', '' );
130 %}
131 %
132
133 <TR>
134   <TD ALIGN="right"><% mt('Expiration') |h %></TD>
135   <TD BGCOLOR="#ffffff"><% "$mon/$year" %></TD>
136 </TR>
137 % if ( $cust_main->paystart_month ) { 
138
139   <TR>
140     <TD ALIGN="right"><% mt('Start date') |h %></TD>
141     <TD BGCOLOR="#ffffff"><% $cust_main->paystart_month. '/'. $cust_main->paystart_year %>
142   </TR>
143 % } elsif ( $cust_main->payissue ) { 
144
145   <TR>
146     <TD ALIGN="right"><% mt('Issue #') |h %></TD>
147     <TD BGCOLOR="#ffffff"><% $cust_main->payissue %>
148   </TR>
149 % } 
150
151
152 <TR>
153   <TD ALIGN="right"><% mt('Name on card') |h %></TD>
154   <TD BGCOLOR="#ffffff"><% $cust_main->payname %></TD>
155 </TR>
156 % } elsif ( $cust_main->payby eq 'CHEK' || $cust_main->payby eq 'DCHK') {
157 %     my( $account, $aba ) = split('@', $cust_main->paymask );
158 %  my $branch = '';
159 %  ($branch,$aba) = split('\.',$aba) if $conf->config('echeck-country') eq 'CA';
160
161
162 % my $autodemand = $cust_main->payby eq 'CHEK' ? 'automatic' : 'on-demand';
163     <% mt("Electronic check ([_1])",$autodemand) |h %>
164   </TD>
165 </TR>
166
167 %  #false laziness w/edit/cust_main/billing.html and misc/payment.cgi
168 %  my $routing_label = $conf->config('echeck-country') eq 'US'
169 %                        ? 'ABA/Routing number'
170 %                        : 'Routing number';
171
172 <TR>
173   <TD ALIGN="right"><% mt($routing_label) |h %></TD>
174   <TD BGCOLOR="#ffffff"><% $aba %></TD>
175 </TR>
176
177 % if ( $conf->config('echeck-country') eq 'CA' ) {
178 <TR>
179   <TD ALIGN="right"><% mt('Branch number') |h %></TD>
180   <TD BGCOLOR="#ffffff"><% $branch %></TD>
181 <TR>
182 % }
183
184   <TD ALIGN="right"><% mt('Account number') |h %></TD>
185   <TD BGCOLOR="#ffffff"><% $account %></TD>
186 </TR>
187 <TR>
188   <TD ALIGN="right"><% mt('Account type') |h %></TD>
189   <TD BGCOLOR="#ffffff"><% $cust_main->paytype %></TD>
190 </TR>
191 <TR>
192   <TD ALIGN="right"><% mt('Bank name') |h %></TD>
193   <TD BGCOLOR="#ffffff"><% $cust_main->payname %></TD>
194 </TR>
195 % if ( $conf->exists('show_bankstate') ) {
196 <TR>
197   <TD ALIGN="right"><% $paystate_label %></TD>
198   <TD BGCOLOR="#ffffff"><% $cust_main->paystate || '&nbsp;&nbsp;&nbsp;' %></TD>
199 </TR>
200 % }
201 % } elsif ( $cust_main->payby eq 'LECB' ) {
202 %     $cust_main->payinfo =~ /^(\d{3})(\d{3})(\d{4})$/;
203 %     my $payinfo = "$1-$2-$3";
204
205     <% mt('Phone bill billing') |h %> 
206   </TD>
207 </TR>
208 <TR>
209   <TD ALIGN="right"><% mt('Phone number') |h %></TD>
210   <TD BGCOLOR="#ffffff"><% $payinfo %></TD>
211 </TR>
212 % } elsif ( $cust_main->payby eq 'BILL' ) { 
213
214     <% mt('Billing') |h %> 
215   </TD>
216 </TR>
217 % if ( $cust_main->payinfo ) { 
218
219 <TR>
220   <TD ALIGN="right"><% mt('P.O.') |h %></TD>
221   <TD BGCOLOR="#ffffff"><% $cust_main->payinfo %></TD>
222 </TR>
223 % } 
224
225
226 <TR>
227   <TD ALIGN="right"><% mt('Attention') |h %></TD>
228   <TD BGCOLOR="#ffffff"><% $cust_main->payname |h %></TD>
229 </TR>
230 % } elsif ( $cust_main->payby eq 'COMP' ) { 
231
232     <% mt('Complimentary') |h %> 
233   </TD>
234 </TR>
235 <TR>
236   <TD ALIGN="right"><% mt('Authorized by') |h %></TD>
237   <TD BGCOLOR="#ffffff"><% $cust_main->payinfo %></TD>
238 </TR>
239 %
240 %#false laziness w/above etc.
241 %my( $mon, $year );
242 %my $date = $cust_main->paydate || '12-2037';
243 %if ( $date  =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
244 %  ( $mon, $year ) = ( $2, $1 );
245 %} elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
246 %  ( $mon, $year ) = ( $1, $3 );
247 %} else {
248 %  warn "unrecognized expiration date format: $date";
249 %  ( $mon, $year ) = ( '', '' );
250 %}
251 %
252
253 <TR>
254   <TD ALIGN="right"><% mt('Expiration') |h %></TD>
255   <TD BGCOLOR="#ffffff"><% "$mon/$year" %></TD>
256 </TR>
257 % } 
258
259 % my $yes = emt('yes');
260 % my $no = emt('no');
261
262 % my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
263
264 % unless (    $conf->exists('cust_class-tax_exempt')
265 %          || $conf->exists('tax-cust_exempt-groups-require_individual_nums')
266 %        )
267 % {
268     <TR>
269       <TD ALIGN="right"><% mt('Tax exempt') |h %><% @exempt_groups ? ' ('.emt('all taxes').')' : '' %></TD>
270       <TD BGCOLOR="#ffffff"><% $cust_main->tax ? $yes : $no %></TD>
271     </TR>
272 % }
273
274 % foreach my $exempt_group ( @exempt_groups ) {
275 %   my $cust_main_exemption = $cust_main->tax_exemption($exempt_group);
276     <TR>
277       <TD ALIGN="right"><% mt('Tax exempt') |h %> (<% $exempt_group %> taxes)</TD>
278       <TD BGCOLOR="#ffffff"><% $cust_main_exemption ? $yes : $no %>
279         <% $cust_main_exemption ? $cust_main_exemption->exempt_number : '' |h %>
280       </TD>
281     </TR>
282 % }
283
284 % if ( $conf->exists('enable_taxproducts') ) {
285 <TR>
286   <TD ALIGN="right"><% mt('Tax location') |h %></TD>
287 % my $tax_location = $conf->exists('tax-ship_address')
288 %                    ? $cust_main->ship_location
289 %                    : $cust_main->bill_location;
290   <TD BGCOLOR="#ffffff"><% $tax_location->geocode('cch') %></TD>
291 </TR>
292 % }
293 <TR>
294   <TD ALIGN="right"><% mt('Postal mail invoices') |h %></TD>
295   <TD BGCOLOR="#ffffff">
296     <% ( grep { $_ eq 'POST' } @invoicing_list ) ? $yes : $no %>
297   </TD>
298 </TR>
299 <TR>
300   <TD ALIGN="right"><% mt('Fax invoices') |h %></TD>
301   <TD BGCOLOR="#ffffff">
302     <% ( grep { $_ eq 'FAX' } @invoicing_list ) ? $yes : $no %>
303   </TD>
304 </TR>
305 <TR>
306   <TD ALIGN="right"><% mt('Email invoices') |h %></TD>
307   <TD BGCOLOR="#ffffff">
308     <% $cust_main->invoice_noemail ? $no : $yes %>
309   </TD>
310 </TR>
311 % unless ( $conf->exists('cust-email-high-visibility')) {
312 <TR>
313   <TD ALIGN="right"><% mt('Email address(es)') |h %></TD>
314   <TD BGCOLOR="#ffffff">
315     <% join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list ) || $no %>
316 %   if ( $cust_main->message_noemail ) {
317     <BR>
318     <SPAN STYLE="font-size: small"><% emt('(do not send notices)') %></SPAN>
319 %   }
320   </TD>
321 </TR>
322 % }
323 <TR>
324   <TD ALIGN="right"><% mt('Invoice terms') |h %></TD>
325   <TD BGCOLOR="#ffffff">
326     <% $cust_main->invoice_terms
327          || emt('Default').' ('. ( $conf->config('invoice_default_terms', $cust_main->agentnum)
328                                      || emt('Payable upon receipt')
329                                  ).
330                            ')'
331     %>
332   </TD>
333 </TR>
334 <TR>
335   <TD ALIGN="right"><% mt('Credit limit') |h %></TD>
336   <TD BGCOLOR="#ffffff">
337 %   my $default_credit_limit = $conf->config('default_credit_limit');
338     <% length($cust_main->credit_limit)
339          ? $money_char. sprintf("%.2f", $cust_main->credit_limit)
340          : $default_credit_limit
341            ? "Default ($money_char". sprintf("%.2f", $default_credit_limit). ")"
342            : emt('Unlimited')
343     %>
344 %   if ( $cust_main->num_cust_main_credit_limit ) {
345       <A HREF="<% $p %>search/cust_main_credit_limit.html?custnum=<% $cust_main->custnum %>">(incidents)</A>
346 %   }
347   </TD>
348 </TR>
349
350 % if ( $conf->exists('voip-cust_cdr_spools') ) { 
351   <TR>
352     <TD ALIGN="right"><% mt('Spool CDRs') |h %></TD>
353     <TD BGCOLOR="#ffffff"><% $cust_main->spool_cdr ? $yes : $no %></TD>
354   </TR>
355 % } 
356
357 % if ( $conf->exists('voip-cust_cdr_squelch') ) { 
358   <TR>
359     <TD ALIGN="right"><% mt($conf->exists('voip-cdr_email') ? 'Print CDRs' : 'Show CDRs') |h %></TD>
360     <TD BGCOLOR="#ffffff"><% $cust_main->squelch_cdr ? $no : $yes %></TD>
361   </TR>
362 % } 
363
364 % if ( $conf->exists('voip-cust_accountcode_cdr') ) { 
365    <TR>
366      <TD ALIGN="right"><% mt('Breakdown CDRs by accountcode') |h %></TD>
367      <TD BGCOLOR="#ffffff"><% $cust_main->accountcode_cdr ? $yes : $no %></TD>
368    </TR>
369 % }
370
371 % if ( $conf->exists('voip-cust_email_csv_cdr') ) { 
372   <TR>
373     <TD ALIGN="right"><% mt('Email CDRs as CSV') |h %></TD>
374     <TD BGCOLOR="#ffffff"><% $cust_main->email_csv_cdr ? $yes : $no %></TD>
375   </TR>
376 % } 
377
378 % if ( $show_term || $cust_main->cdr_termination_percentage ) {
379   <TR>
380     <TD ALIGN="right"><% mt('CDR termination settlement') |h %></TD>
381     <TD BGCOLOR="#ffffff"><% $cust_main->cdr_termination_percentage %><% $cust_main->cdr_termination_percentage =~ /\d/ ? '%' : '' %></TD>
382   </TR>
383 % }
384
385 % if ( $cust_main->locale ) {
386 % my %locale_info = FS::Locales->locale_info($cust_main->locale);
387   <TR>
388     <TD ALIGN="right"><% mt('Invoicing locale') |h %></TD>
389     <TD BGCOLOR="#ffffff"><% $locale_info{name} . " (" . $locale_info{country} .")" %></TD>
390   </TR>
391 % }
392
393
394 </TABLE>
395 <%once>
396
397 my $paystate_label = FS::Msgcat::_gettext('paystate');
398 $paystate_label = 'Bank state' if $paystate_label =~/^paystate$/;
399
400 </%once>
401 <%init>
402
403 my( $cust_main ) = @_;
404 my @invoicing_list = $cust_main->invoicing_list;
405 my $conf = new FS::Conf;
406 my $money_char = $conf->config('money_char') || '$';
407
408 #false laziness w/edit/cust_main/billing.html
409 my $term_sql = "SELECT COUNT(*) FROM cust_pkg LEFT JOIN part_pkg USING ( pkgpart ) WHERE custnum = ? AND plan = 'cdr_termination' LIMIT 1";
410 my $term_sth = dbh->prepare($term_sql)  or die dbh->errstr;
411 $term_sth->execute($cust_main->custnum) or die $term_sth->errstr;
412 my $show_term = $term_sth->fetchrow_arrayref->[0];
413
414 </%init>