fix "monthly recurring" display w/quantities, 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.  ideally we'd like
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 %       my $pkg_amount = 0;
65 %
66 %       #add recurring amounts for this package and its billing add-ons
67 %       foreach my $l_part_pkg ( $part_pkg->self_and_bill_linked ) {
68 %         $pkg_amount += $l_part_pkg->base_recur;
69 %       }
70 %
71 %       #subtract amounts for any active discounts
72 %       #(there should only be one at the moment, otherwise this makes no sense)
73 %       foreach my $cust_pkg_discount ( $cust_pkg->cust_pkg_discount_active ) {
74 %         my $discount = $cust_pkg_discount->discount;
75 %         #and only one of these for each
76 %         $pkg_amount -= $discount->amount;
77 %         $pkg_amount -= $amount * $discount->percent/100;
78 %       }
79 %
80 %       $pkg_amount *= ( $cust_pkg->quantity || 1 );
81 %
82 %       $amount += $pkg_amount;
83 %
84 %     }
85    
86       <TR>
87         <TD ALIGN="right"><% emt( ucfirst($freq_pretty). ' recurring' ) %></TD>
88         <TD BGCOLOR="#ffffff"><% $money_char. sprintf('%.2f', $amount) %></TD>
89         </TD>
90       </TR>
91 %   }
92
93 % }
94
95 % if ( $conf->exists('cust_main-select-prorate_day') ) {
96 <TR>
97   <TD ALIGN="right"><% mt('Prorate day of month') |h %></TD>
98   <TD BGCOLOR="#ffffff"><% $cust_main->prorate_day %>
99   </TD>
100 </TR>
101 % }
102
103 % if ( $conf->exists('cust_main-select-billday') 
104 %    && ($cust_main->payby eq 'CARD' || $cust_main->payby eq 'CHEK') ) {
105 <TR>
106   <TD ALIGN="right"><% mt('Billing day of month') |h %></TD>
107   <TD BGCOLOR="#ffffff"><% $cust_main->billday %>
108   </TD>
109 </TR>
110 % }
111
112 <TR>
113   <TD ALIGN="right"><% mt('Billing type') |h %></TD>
114   <TD BGCOLOR="#ffffff">
115 % if ( $cust_main->payby eq 'CARD' || $cust_main->payby eq 'DCRD' ) { 
116
117 %   my $autodemand = $cust_main->payby eq 'CARD' ? 'automatic' : 'on-demand';
118     <% mt("Credit card ([_1])",$autodemand) |h %>
119   </TD>
120 </TR>
121 <TR>
122   <TD ALIGN="right"><% mt('Card number') |h %></TD>
123   <TD BGCOLOR="#ffffff"><% $cust_main->paymask %></TD>
124 </TR>
125 %
126 %#false laziness w/elements/select-month_year.html & edit/cust_main/billing.html
127 %my( $mon, $year );
128 %my $date = $cust_main->paydate || '12-2037';
129 %if ( $date  =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
130 %  ( $mon, $year ) = ( $2, $1 );
131 %} elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
132 %  ( $mon, $year ) = ( $1, $3 );
133 %} else {
134 %  warn "unrecognized expiration date format: $date";
135 %  ( $mon, $year ) = ( '', '' );
136 %}
137 %
138
139 <TR>
140   <TD ALIGN="right"><% mt('Expiration') |h %></TD>
141   <TD BGCOLOR="#ffffff"><% "$mon/$year" %></TD>
142 </TR>
143 % if ( $cust_main->paystart_month ) { 
144
145   <TR>
146     <TD ALIGN="right"><% mt('Start date') |h %></TD>
147     <TD BGCOLOR="#ffffff"><% $cust_main->paystart_month. '/'. $cust_main->paystart_year %>
148   </TR>
149 % } elsif ( $cust_main->payissue ) { 
150
151   <TR>
152     <TD ALIGN="right"><% mt('Issue #') |h %></TD>
153     <TD BGCOLOR="#ffffff"><% $cust_main->payissue %>
154   </TR>
155 % } 
156
157
158 <TR>
159   <TD ALIGN="right"><% mt('Name on card') |h %></TD>
160   <TD BGCOLOR="#ffffff"><% $cust_main->payname %></TD>
161 </TR>
162 % } elsif ( $cust_main->payby eq 'CHEK' || $cust_main->payby eq 'DCHK') {
163 %     my( $account, $aba ) = split('@', $cust_main->paymask );
164 %  my $branch = '';
165 %  ($branch,$aba) = split('\.',$aba) if $conf->config('echeck-country') eq 'CA';
166
167
168 % my $autodemand = $cust_main->payby eq 'CHEK' ? 'automatic' : 'on-demand';
169     <% mt("Electronic check ([_1])",$autodemand) |h %>
170   </TD>
171 </TR>
172
173 %  #false laziness w/edit/cust_main/billing.html and misc/payment.cgi
174 %  my $routing_label = $conf->config('echeck-country') eq 'US'
175 %                        ? 'ABA/Routing number'
176 %                        : 'Routing number';
177
178 <TR>
179   <TD ALIGN="right"><% mt($routing_label) |h %></TD>
180   <TD BGCOLOR="#ffffff"><% $aba %></TD>
181 </TR>
182
183 % if ( $conf->config('echeck-country') eq 'CA' ) {
184 <TR>
185   <TD ALIGN="right"><% mt('Branch number') |h %></TD>
186   <TD BGCOLOR="#ffffff"><% $branch %></TD>
187 <TR>
188 % }
189
190   <TD ALIGN="right"><% mt('Account number') |h %></TD>
191   <TD BGCOLOR="#ffffff"><% $account %></TD>
192 </TR>
193 <TR>
194   <TD ALIGN="right"><% mt('Account type') |h %></TD>
195   <TD BGCOLOR="#ffffff"><% $cust_main->paytype %></TD>
196 </TR>
197 <TR>
198   <TD ALIGN="right"><% mt('Bank name') |h %></TD>
199   <TD BGCOLOR="#ffffff"><% $cust_main->payname %></TD>
200 </TR>
201 % if ( $conf->exists('show_bankstate') ) {
202 <TR>
203   <TD ALIGN="right"><% $paystate_label %></TD>
204   <TD BGCOLOR="#ffffff"><% $cust_main->paystate || '&nbsp;&nbsp;&nbsp;' %></TD>
205 </TR>
206 % }
207 % } elsif ( $cust_main->payby eq 'LECB' ) {
208 %     $cust_main->payinfo =~ /^(\d{3})(\d{3})(\d{4})$/;
209 %     my $payinfo = "$1-$2-$3";
210
211     <% mt('Phone bill billing') |h %> 
212   </TD>
213 </TR>
214 <TR>
215   <TD ALIGN="right"><% mt('Phone number') |h %></TD>
216   <TD BGCOLOR="#ffffff"><% $payinfo %></TD>
217 </TR>
218 % } elsif ( $cust_main->payby eq 'BILL' ) { 
219
220     <% mt('Billing') |h %> 
221   </TD>
222 </TR>
223 % if ( $cust_main->payinfo ) { 
224
225 <TR>
226   <TD ALIGN="right"><% mt('P.O.') |h %></TD>
227   <TD BGCOLOR="#ffffff"><% $cust_main->payinfo %></TD>
228 </TR>
229 % } 
230
231
232 <TR>
233   <TD ALIGN="right"><% mt('Attention') |h %></TD>
234   <TD BGCOLOR="#ffffff"><% $cust_main->payname |h %></TD>
235 </TR>
236 % } elsif ( $cust_main->payby eq 'COMP' ) { 
237
238     <% mt('Complimentary') |h %> 
239   </TD>
240 </TR>
241 <TR>
242   <TD ALIGN="right"><% mt('Authorized by') |h %></TD>
243   <TD BGCOLOR="#ffffff"><% $cust_main->payinfo %></TD>
244 </TR>
245 %
246 %#false laziness w/above etc.
247 %my( $mon, $year );
248 %my $date = $cust_main->paydate || '12-2037';
249 %if ( $date  =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
250 %  ( $mon, $year ) = ( $2, $1 );
251 %} elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
252 %  ( $mon, $year ) = ( $1, $3 );
253 %} else {
254 %  warn "unrecognized expiration date format: $date";
255 %  ( $mon, $year ) = ( '', '' );
256 %}
257 %
258
259 <TR>
260   <TD ALIGN="right"><% mt('Expiration') |h %></TD>
261   <TD BGCOLOR="#ffffff"><% "$mon/$year" %></TD>
262 </TR>
263 % } 
264
265 % my $yes = emt('yes');
266 % my $no = emt('no');
267
268 % my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
269
270 % unless (    $conf->exists('cust_class-tax_exempt')
271 %          || $conf->exists('tax-cust_exempt-groups-require_individual_nums')
272 %        )
273 % {
274     <TR>
275       <TD ALIGN="right"><% mt('Tax exempt') |h %><% @exempt_groups ? ' ('.emt('all taxes').')' : '' %></TD>
276       <TD BGCOLOR="#ffffff"><% $cust_main->tax ? $yes : $no %></TD>
277     </TR>
278 % }
279
280 % foreach my $exempt_group ( @exempt_groups ) {
281 %   my $cust_main_exemption = $cust_main->tax_exemption($exempt_group);
282     <TR>
283       <TD ALIGN="right"><% mt('Tax exempt') |h %> (<% $exempt_group %> taxes)</TD>
284       <TD BGCOLOR="#ffffff"><% $cust_main_exemption ? $yes : $no %>
285         <% $cust_main_exemption ? $cust_main_exemption->exempt_number : '' |h %>
286       </TD>
287     </TR>
288 % }
289
290 % if ( $conf->exists('enable_taxproducts') ) {
291 <TR>
292   <TD ALIGN="right"><% mt('Tax location') |h %></TD>
293 % my $tax_location = $conf->exists('tax-ship_address')
294 %                    ? $cust_main->ship_location
295 %                    : $cust_main->bill_location;
296   <TD BGCOLOR="#ffffff"><% $tax_location->geocode('cch') %></TD>
297 </TR>
298 % }
299 <TR>
300   <TD ALIGN="right"><% mt('Postal mail invoices') |h %></TD>
301   <TD BGCOLOR="#ffffff">
302     <% ( grep { $_ eq 'POST' } @invoicing_list ) ? $yes : $no %>
303   </TD>
304 </TR>
305 <TR>
306   <TD ALIGN="right"><% mt('Fax invoices') |h %></TD>
307   <TD BGCOLOR="#ffffff">
308     <% ( grep { $_ eq 'FAX' } @invoicing_list ) ? $yes : $no %>
309   </TD>
310 </TR>
311 <TR>
312   <TD ALIGN="right"><% mt('Email invoices') |h %></TD>
313   <TD BGCOLOR="#ffffff">
314     <% $cust_main->invoice_noemail ? $no : $yes %>
315   </TD>
316 </TR>
317 % unless ( $conf->exists('cust-email-high-visibility')) {
318 <TR>
319   <TD ALIGN="right"><% mt('Email address(es)') |h %></TD>
320   <TD BGCOLOR="#ffffff">
321     <% join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list ) || $no %>
322 %   if ( $cust_main->message_noemail ) {
323     <BR>
324     <SPAN STYLE="font-size: small"><% emt('(do not send notices)') %></SPAN>
325 %   }
326   </TD>
327 </TR>
328 % }
329 <TR>
330   <TD ALIGN="right"><% mt('Invoice terms') |h %></TD>
331   <TD BGCOLOR="#ffffff">
332     <% $cust_main->invoice_terms
333          || emt('Default').' ('. ( $conf->config('invoice_default_terms', $cust_main->agentnum)
334                                      || emt('Payable upon receipt')
335                                  ).
336                            ')'
337     %>
338   </TD>
339 </TR>
340 <TR>
341   <TD ALIGN="right"><% mt('Credit limit') |h %></TD>
342   <TD BGCOLOR="#ffffff">
343 %   my $default_credit_limit = $conf->config('default_credit_limit');
344     <% length($cust_main->credit_limit)
345          ? $money_char. sprintf("%.2f", $cust_main->credit_limit)
346          : $default_credit_limit
347            ? "Default ($money_char". sprintf("%.2f", $default_credit_limit). ")"
348            : emt('Unlimited')
349     %>
350 %   if ( $cust_main->num_cust_main_credit_limit ) {
351       <A HREF="<% $p %>search/cust_main_credit_limit.html?custnum=<% $cust_main->custnum %>">(incidents)</A>
352 %   }
353   </TD>
354 </TR>
355
356 % if ( $conf->exists('voip-cust_cdr_spools') ) { 
357   <TR>
358     <TD ALIGN="right"><% mt('Spool CDRs') |h %></TD>
359     <TD BGCOLOR="#ffffff"><% $cust_main->spool_cdr ? $yes : $no %></TD>
360   </TR>
361 % } 
362
363 % if ( $conf->exists('voip-cust_cdr_squelch') ) { 
364   <TR>
365     <TD ALIGN="right"><% mt($conf->exists('voip-cdr_email') ? 'Print CDRs' : 'Show CDRs') |h %></TD>
366     <TD BGCOLOR="#ffffff"><% $cust_main->squelch_cdr ? $no : $yes %></TD>
367   </TR>
368 % } 
369
370 % if ( $conf->exists('voip-cust_accountcode_cdr') ) { 
371    <TR>
372      <TD ALIGN="right"><% mt('Breakdown CDRs by accountcode') |h %></TD>
373      <TD BGCOLOR="#ffffff"><% $cust_main->accountcode_cdr ? $yes : $no %></TD>
374    </TR>
375 % }
376
377 % if ( $conf->exists('voip-cust_email_csv_cdr') ) { 
378   <TR>
379     <TD ALIGN="right"><% mt('Email CDRs as CSV') |h %></TD>
380     <TD BGCOLOR="#ffffff"><% $cust_main->email_csv_cdr ? $yes : $no %></TD>
381   </TR>
382 % } 
383
384 % if ( $show_term || $cust_main->cdr_termination_percentage ) {
385   <TR>
386     <TD ALIGN="right"><% mt('CDR termination settlement') |h %></TD>
387     <TD BGCOLOR="#ffffff"><% $cust_main->cdr_termination_percentage %><% $cust_main->cdr_termination_percentage =~ /\d/ ? '%' : '' %></TD>
388   </TR>
389 % }
390
391 % if ( $cust_main->locale ) {
392 % my %locale_info = FS::Locales->locale_info($cust_main->locale);
393   <TR>
394     <TD ALIGN="right"><% mt('Invoicing locale') |h %></TD>
395     <TD BGCOLOR="#ffffff"><% $locale_info{name} . " (" . $locale_info{country} .")" %></TD>
396   </TR>
397 % }
398
399
400 </TABLE>
401 <%once>
402
403 my $paystate_label = FS::Msgcat::_gettext('paystate');
404 $paystate_label = 'Bank state' if $paystate_label =~/^paystate$/;
405
406 </%once>
407 <%init>
408
409 my( $cust_main ) = @_;
410 my @invoicing_list = $cust_main->invoicing_list;
411 my $conf = new FS::Conf;
412 my $money_char = $conf->config('money_char') || '$';
413
414 #false laziness w/edit/cust_main/billing.html
415 my $term_sql = "SELECT COUNT(*) FROM cust_pkg LEFT JOIN part_pkg USING ( pkgpart ) WHERE custnum = ? AND plan = 'cdr_termination' LIMIT 1";
416 my $term_sth = dbh->prepare($term_sql)  or die dbh->errstr;
417 $term_sth->execute($cust_main->custnum) or die $term_sth->errstr;
418 my $show_term = $term_sth->fetchrow_arrayref->[0];
419
420 </%init>