Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / view / cust_main / billing.html
1 <FONT CLASS="fsinnerbox-title"><% mt('Billing information') |h %></FONT>
2 % my $yes = emt('yes');
3 % my $no = emt('no');
4
5 <TABLE CLASS="fsinnerbox">
6
7 % if ( $cust_main->complimentary ) {
8   <TR>
9     <TH ALIGN="right"><% mt('Complimentary') |h %></TH>
10     <TD><% $yes %></TD>
11   </TR>
12 % }
13
14 %( my $balance = $cust_main->balance )
15 %  =~ s/^(\-?)(.*)$/<FONT SIZE=+1>$1<\/FONT>$money_char$2/;
16
17 <TR>
18   <TH ALIGN="right"><% mt('Balance due') |h %></TH>
19   <TD><B><% $balance %></B></TD>
20 </TR>
21
22 % #54: just an arbitrary number i pulled out of my goober.  ideally we'd like
23 % # to consider e.g. a histogram of num_ncancelled_packages for the entire
24 % # customer base, and compare it to a graph of the overhead for generating this
25 % # information.  (and optimize it better, we could get it more from SQL)
26 % if ( $cust_main->num_ncancelled_pkgs < 54 ) {
27 %   my $sth = dbh->prepare("
28 %     SELECT DISTINCT freq FROM cust_pkg LEFT JOIN part_pkg USING (pkgpart)
29 %       WHERE freq IS NOT NULL AND freq != '0'
30 %         AND ( cancel IS NULL OR cancel = 0 )
31 %         AND custnum = ?
32 %   ") or die $DBI::errstr;
33
34 %   $sth->execute($cust_main->custnum) or die $sth->errstr;
35
36 %   #not really a numeric sort because freqs can actually be all sorts of things
37 %   # but good enough for the 99% cases of ordering monthly quarterly annually
38 %   my @freqs = sort { $a <=> $b } map { $_->[0] } @{ $sth->fetchall_arrayref };
39
40 %   foreach my $freq (@freqs) {
41 %     my @cust_pkg = qsearch({
42 %       'table'     => 'cust_pkg',
43 %       'addl_from' => 'LEFT JOIN part_pkg USING (pkgpart)',
44 %       'hashref'   => { 'custnum' => $cust_main->custnum, },
45 %       'extra_sql' => 'AND ( cancel IS NULL OR cancel = 0 )
46 %                       AND freq = '. dbh->quote($freq),
47 %     }) or next;
48
49 %     my $freq_pretty = $cust_pkg[0]->part_pkg->freq_pretty;
50 %
51 %     my $amount = 0;
52 %     foreach my $cust_pkg (@cust_pkg) {
53 %       my $part_pkg = $cust_pkg->part_pkg;
54 %       next if $cust_pkg->susp
55 %            && ! $cust_pkg->option('suspend_bill')
56 %            && ( ! $part_pkg->option('suspend_bill')
57 %                 || $cust_pkg->option('no_suspend_bill')
58 %               );
59 %
60 %       my $pkg_amount = 0;
61 %
62 %       #add recurring amounts for this package and its billing add-ons
63 %       foreach my $l_part_pkg ( $part_pkg->self_and_bill_linked ) {
64 %         $pkg_amount += $l_part_pkg->base_recur($cust_pkg);
65 %       }
66 %
67 %       #subtract amounts for any active discounts
68 %       #(there should only be one at the moment, otherwise this makes no sense)
69 %       foreach my $cust_pkg_discount ( $cust_pkg->cust_pkg_discount_active ) {
70 %         my $discount = $cust_pkg_discount->discount;
71 %         #and only one of these for each
72 %         $pkg_amount -= $discount->amount;
73 %         $pkg_amount -= $amount * $discount->percent/100;
74 %       }
75 %
76 %       $pkg_amount *= ( $cust_pkg->quantity || 1 );
77 %
78 %       $amount += $pkg_amount;
79 %
80 %     }
81    
82       <TR>
83         <TH ALIGN="right"><% emt( ucfirst($freq_pretty). ' recurring' ) %></TH>
84         <TD><% $money_char. sprintf('%.2f', $amount) %></TD>
85         </TD>
86       </TR>
87 %   }
88
89 % }
90
91 % if ( $conf->exists('cust_main-select-prorate_day') ) {
92 <TR>
93   <TH ALIGN="right"><% mt('Prorate day of month') |h %></TH>
94   <TD><% $cust_main->prorate_day %>
95   </TD>
96 </TR>
97 % }
98
99 % if ( $conf->exists('cust_main-select-billday') 
100 %        && qsearch({ 'table'     => 'cust_payby',
101 %                     'hashref'   => { 'custnum' => $cust_main->custnum, },
102 %                     'extra_sql' => "AND payby IN ( 'CARD', 'CHEK' )",
103 %                     'order_by'  => 'LIMIT 1',
104 %                  })
105 %    )
106 % {
107     <TR>
108       <TH ALIGN="right"><% mt('Payment day of month') |h %></TH>
109       <TD><% $cust_main->billday %>
110       </TD>
111     </TR>
112 % }
113
114 % if ( $cust_main->po_number ) { 
115     <TR>
116       <TH ALIGN="right"><% mt('Purchase Order #') |h %></TH>
117       <TD><% $cust_main->po_number %></TD>
118     </TR>
119 % } 
120
121 % my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
122
123 % unless (    $conf->exists('cust_class-tax_exempt')
124 %          || $conf->exists('tax-cust_exempt-groups-require_individual_nums')
125 %        )
126 % {
127     <TR>
128       <TH ALIGN="right"><% mt('Tax exempt') |h %><% @exempt_groups ? ' ('.emt('all taxes').')' : '' %></TH>
129       <TD><% $cust_main->tax ? $yes : $no %></TD>
130     </TR>
131 % }
132
133 % foreach my $exempt_group ( @exempt_groups ) {
134 %   my $cust_main_exemption = $cust_main->tax_exemption($exempt_group);
135     <TR>
136       <TH ALIGN="right"><% mt('Tax exempt') |h %> (<% $exempt_group %> taxes)</TH>
137       <TD><% $cust_main_exemption ? $yes : $no %>
138         <% $cust_main_exemption ? $cust_main_exemption->exempt_number : '' |h %>
139       </TD>
140     </TR>
141 % }
142
143 % if ( $conf->config('tax_data_vendor') eq 'cch' ) {
144 <TR>
145   <TH ALIGN="right"><% mt('Tax location') |h %></TH>
146 % my $tax_location = $conf->exists('tax-ship_address')
147 %                    ? $cust_main->ship_location
148 %                    : $cust_main->bill_location;
149   <TD><% $tax_location->geocode('cch') %></TD>
150 </TR>
151 % }
152 <TR>
153   <TH ALIGN="right"><% mt('Postal mail invoices') |h %></TH>
154   <TD>
155     <% ( grep { $_ eq 'POST' } @invoicing_list )
156          ? $yes. ( $cust_main->invoice_attn
157                      ? ', attn: '. $cust_main->invoice_attn
158                      : ''
159                  )
160          : $no
161     %>
162   </TD>
163 </TR>
164 <TR>
165   <TH ALIGN="right"><% mt('Email invoices') |h %></TH>
166   <TD>
167     <% $cust_main->invoice_noemail ? $no : $yes %>
168   </TD>
169 </TR>
170 % unless ( $conf->exists('cust-email-high-visibility')) {
171 <TR>
172   <TH ALIGN="right"><% mt('Email address(es)') |h %></TH>
173   <TD>
174     <% join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list ) || $no %>
175 %   if ( $cust_main->message_noemail ) {
176     <BR>
177     <SPAN STYLE="font-size: small"><% emt('(do not send notices)') %></SPAN>
178 %   }
179   </TD>
180 </TR>
181 % }
182 <TR>
183   <TH ALIGN="right"><% mt('Invoice terms') |h %></TH>
184   <TD>
185     <% $cust_main->invoice_terms
186          || emt('Default').' ('. ( $conf->config('invoice_default_terms', $cust_main->agentnum)
187                                      || emt('Payable upon receipt')
188                                  ).
189                            ')'
190     %>
191   </TD>
192 </TR>
193 <TR>
194   <TH ALIGN="right"><% mt('Credit limit') |h %></TH>
195   <TD>
196 %   my $default_credit_limit = $conf->config('default_credit_limit');
197     <% length($cust_main->credit_limit)
198          ? $money_char. sprintf("%.2f", $cust_main->credit_limit)
199          : $default_credit_limit
200            ? "Default ($money_char". sprintf("%.2f", $default_credit_limit). ")"
201            : emt('Unlimited')
202     %>
203 %   if ( $cust_main->num_cust_main_credit_limit ) {
204       <A HREF="<% $p %>search/cust_main_credit_limit.html?custnum=<% $cust_main->custnum %>">(incidents)</A>
205 %   }
206   </TD>
207 </TR>
208
209 % if ( $conf->exists('voip-cust_cdr_spools') ) { 
210   <TR>
211     <TH ALIGN="right"><% mt('Spool CDRs') |h %></TH>
212     <TD><% $cust_main->spool_cdr ? $yes : $no %></TD>
213   </TR>
214 % } 
215
216 % if ( $conf->exists('voip-cust_cdr_squelch') ) { 
217   <TR>
218     <TH ALIGN="right"><% mt($conf->exists('voip-cdr_email') ? 'Print CDRs' : 'Show CDRs') |h %></TH>
219     <TD><% $cust_main->squelch_cdr ? $no : $yes %></TD>
220   </TR>
221 % } 
222
223 % if ( $conf->exists('voip-cust_accountcode_cdr') ) { 
224    <TR>
225      <TH ALIGN="right"><% mt('Breakdown CDRs by accountcode') |h %></TH>
226      <TD><% $cust_main->accountcode_cdr ? $yes : $no %></TD>
227    </TR>
228 % }
229
230 % if ( $conf->exists('voip-cust_email_csv_cdr') ) { 
231   <TR>
232     <TH ALIGN="right"><% mt('Email CDRs as CSV') |h %></TH>
233     <TD><% $cust_main->email_csv_cdr ? $yes : $no %></TD>
234   </TR>
235 % } 
236
237 % if ( $show_term || $cust_main->cdr_termination_percentage ) {
238   <TR>
239     <TH ALIGN="right"><% mt('CDR termination settlement') |h %></TH>
240     <TD><% $cust_main->cdr_termination_percentage %><% $cust_main->cdr_termination_percentage =~ /\d/ ? '%' : '' %></TD>
241   </TR>
242 % }
243
244 % if ( $cust_main->currency ) {
245   <TR>
246     <TH ALIGN="right"><% mt('Invoicing currency') |h %></TH>
247     <TD><% $cust_main->currency. ': '. code2currency($cust_main->currency) %></TD>
248   </TR>
249 % }
250
251 % if ( $cust_main->locale ) {
252 % my %locale_info = FS::Locales->locale_info($cust_main->locale);
253   <TR>
254     <TH ALIGN="right"><% mt('Invoicing locale') |h %></TH>
255     <TD><% $locale_info{name} . " (" . $locale_info{country} .")" %></TD>
256   </TR>
257 % }
258
259
260 </TABLE>
261 <%init>
262
263 my( $cust_main ) = @_;
264 my @invoicing_list = $cust_main->invoicing_list;
265 my $conf = new FS::Conf;
266 my $money_char = $conf->config('money_char') || '$';
267
268 #false laziness w/edit/cust_main/billing.html
269 my $term_sql = "SELECT COUNT(*) FROM cust_pkg LEFT JOIN part_pkg USING ( pkgpart ) WHERE custnum = ? AND plan = 'cdr_termination' LIMIT 1";
270 my $term_sth = dbh->prepare($term_sql)  or die dbh->errstr;
271 $term_sth->execute($cust_main->custnum) or die $term_sth->errstr;
272 my $show_term = $term_sth->fetchrow_arrayref->[0];
273
274 </%init>