1 <% encode_json($return) %>\
6 my $conf = new FS::Conf;
8 my $sub = $cgi->param('sub');
12 if ( $sub eq 'calculate_taxes' ) {
15 my %arg = $cgi->param('arg');
17 warn join('', map "$_: $arg{$_}\n", keys %arg )
20 my $cust_credit = qsearchs( 'cust_credit', { 'crednum' => $arg{crednum} } );
21 unless ($cust_credit) {
22 $return->{error} = "No such credit: $arg{crednum}";
26 my %cust_bill_pkg = ();
27 my @items = split(',', $arg{items});
29 my ($billpkgnum, $s_or_r, $amount ) = splice(@items, 0, 3);
30 unless ( defined($amount) ) {
31 $return->{error} = "Bad items argument list";
34 unless ( $cust_bill_pkg{$billpkgnum} ) {
36 qsearchs( 'cust_bill_pkg', { 'billpkgnum' => $billpkgnum } );
37 unless ($cust_bill_pkg) {
38 $return->{error} = "No such line item: $billpkgnum";
41 next unless $cust_bill_pkg->pkgnum > 0; #hmmm @ one shot (-1)
42 unless ($cust_bill_pkg->cust_pkg->custnum == $cust_credit->custnum) {
43 $return->{error} = "Credit/line item customer mismatch";
46 $cust_bill_pkg{$billpkgnum} = $cust_bill_pkg;
47 $cust_bill_pkg->setup(0);
48 $cust_bill_pkg->recur(0);
51 last if $return->{error};
53 my $cust_bill_pkg = $cust_bill_pkg{$billpkgnum};
54 $s_or_r = $s_or_r eq 'setup' ? 'setup' : 'recur';
55 my $value = sprintf('%.2f', $cust_bill_pkg->$s_or_r + $amount);
56 $cust_bill_pkg->$s_or_r($value);
59 last if $return->{error};
61 my $cust_main = $cust_credit->cust_main;
64 foreach my $cust_bill_pkg (values %cust_bill_pkg) {
65 $cust_main->_handle_taxes( $taxlisthash, $cust_bill_pkg );
67 my $listref_or_error =
68 $cust_main->calculate_taxes( [ values %cust_bill_pkg ], $taxlisthash, [ values %cust_bill_pkg ]->[0]->cust_bill->_date );
70 unless ( ref( $listref_or_error ) ) {
71 $return->{error} = "No such credit: $arg{crednum}";
76 $return->{taxlines} = \@taxlines;
77 foreach my $taxline ( @$listref_or_error ) {
78 my $amount = $taxline->setup;
79 my $desc = $taxline->desc;
80 foreach my $location ( @{$taxline->cust_bill_pkg_tax_location}, @{$taxline->cust_bill_pkg_tax_rate_location} ) {
81 my $taxlocnum = $location->locationnum || '';
82 my $taxratelocnum = $location->taxratelocationnum || '';
83 $location->cust_bill_pkg_desc($taxline->desc); #ugh @ that kludge
85 [ $location->desc, $taxline->setup, $taxlocnum, $taxratelocnum ];
86 $amount -= $location->amount;
90 [ $taxline->itemdesc. ' (default)', sprintf('%.2f', $amount), '', '' ];
94 $return->{taxlines} = \@taxlines;