1 <% encode_json($return) %>\
4 my $curuser = $FS::CurrentUser::CurrentUser;
5 die "access denied" unless $curuser->access_right('Credit line items');
9 my $conf = new FS::Conf;
11 my $sub = $cgi->param('sub');
15 if ( $sub eq 'calculate_taxes' ) {
19 my %arg = $cgi->param('arg');
21 warn join('', map "$_: $arg{$_}\n", keys %arg )
24 #some false laziness w/cust_credit::credit_lineitems
26 my $cust_main = qsearchs({
27 'table' => 'cust_main',
28 'hashref' => { 'custnum' => $arg{custnum} },
29 'extra_sql' => ' AND '. $curuser->agentnums_sql,
30 }) or die 'unknown customer';
32 my @billpkgnums = split(',', $arg{billpkgnums});
33 my @setuprecurs = split(',', $arg{setuprecurs});
34 my @amounts = split(',', $arg{amounts});
36 my @cust_bill_pkg = ();
38 while ( @billpkgnums ) {
39 my $billpkgnum = shift @billpkgnums;
40 my $setuprecur = shift @setuprecurs;
41 my $amount = shift @amounts;
43 my $cust_bill_pkg = qsearchs({
44 'table' => 'cust_bill_pkg',
45 'hashref' => { 'billpkgnum' => $billpkgnum },
46 'addl_from' => 'LEFT JOIN cust_bill USING (invnum)',
47 'extra_sql' => 'AND custnum = '. $cust_main->custnum,
48 }) or die "unknown billpkgnum $billpkgnum";
50 #shouldn't be passed# next if $cust_bill_pkg->pkgnum == 0;
52 if ( $setuprecur eq 'setup' ) {
53 $cust_bill_pkg->setup($amount);
54 $cust_bill_pkg->recur(0);
55 $cust_bill_pkg->unitrecur(0);
56 $cust_bill_pkg->type('');
58 $cust_bill_pkg->recur($amount);
59 $cust_bill_pkg->setup(0);
60 $cust_bill_pkg->unitsetup(0);
63 push @cust_bill_pkg, $cust_bill_pkg;
65 $cust_main->_handle_taxes( $taxlisthash, $cust_bill_pkg );
68 if ( @cust_bill_pkg ) {
70 my $listref_or_error =
71 $cust_main->calculate_taxes( \@cust_bill_pkg, $taxlisthash, $cust_bill_pkg[0]->cust_bill->_date );
73 unless ( ref( $listref_or_error ) ) {
74 $return->{error} = $listref_or_error;
80 $return->{taxlines} = \@taxlines;
81 foreach my $taxline ( @$listref_or_error ) {
82 my $amount = $taxline->setup;
83 my $desc = $taxline->desc;
84 foreach my $location (
85 @{$taxline->get('cust_bill_pkg_tax_location')},
86 @{$taxline->get('cust_bill_pkg_tax_rate_location')} )
88 my $taxlocnum = $location->locationnum || '';
89 my $taxratelocnum = $location->taxratelocationnum || '';
90 $location->cust_bill_pkg_desc($taxline->desc); #ugh @ that kludge
91 $taxtotal += $location->amount;
93 #[ $location->desc, $taxline->setup, $taxlocnum, $taxratelocnum ];
94 [ $location->desc, $location->amount, $taxlocnum, $taxratelocnum ];
95 $amount -= $location->amount;
100 [ $taxline->itemdesc. ' (default)', sprintf('%.2f', $amount), '', '' ];
104 $return->{taxlines} = \@taxlines;
105 $return->{taxtotal} = sprintf('%.2f', $taxtotal);
109 $return->{taxlines} = [];
110 $return->{taxtotal} = '0.00';