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