Merge branch 'master' of ssh://git.freeside.biz/home/git/freeside
[freeside.git] / httemplate / misc / xmlhttp-calculate_taxes.html
1 <% encode_json($return) %>\
2 <%init>
3
4 my $DEBUG = 0;
5
6 my $conf = new FS::Conf;
7
8 my $sub = $cgi->param('sub');
9
10 my $return = {};
11
12 if ( $sub eq 'calculate_taxes' ) {
13
14   {
15     my %arg = $cgi->param('arg');
16     $return = \%arg;
17     warn join('', map "$_: $arg{$_}\n", keys %arg )
18       if $DEBUG;
19
20     my $cust_credit = qsearchs( 'cust_credit', { 'crednum' => $arg{crednum} } );
21     unless ($cust_credit) {
22       $return->{error} = "No such credit: $arg{crednum}";
23       last;
24     }
25   
26     my %cust_bill_pkg = ();
27     my @items = split(',', $arg{items});
28     while ( @items ) {
29       my ($billpkgnum, $s_or_r, $amount ) = splice(@items, 0, 3);
30       unless ( defined($amount) ) {
31         $return->{error} = "Bad items argument list";
32         last;
33       }
34       unless ( $cust_bill_pkg{$billpkgnum} ) {
35         my $cust_bill_pkg =
36           qsearchs( 'cust_bill_pkg', { 'billpkgnum' => $billpkgnum } );
37         unless ($cust_bill_pkg) {
38           $return->{error} = "No such line item: $billpkgnum";
39           last;
40         }
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";
44           last;
45         }
46         $cust_bill_pkg{$billpkgnum} = $cust_bill_pkg;
47         $cust_bill_pkg->setup(0);
48         $cust_bill_pkg->recur(0);
49       }
50
51       last if $return->{error};
52
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);
57     }
58
59     last if $return->{error};
60
61     my $cust_main = $cust_credit->cust_main;
62
63     my $taxlisthash = {};
64     foreach my $cust_bill_pkg (values %cust_bill_pkg) {
65       $cust_main->_handle_taxes( $taxlisthash, $cust_bill_pkg );
66     }
67     my $listref_or_error = 
68       $cust_main->calculate_taxes( [ values %cust_bill_pkg ], $taxlisthash, [ values %cust_bill_pkg ]->[0]->cust_bill->_date );
69
70     unless ( ref( $listref_or_error ) ) {
71       $return->{error} = "No such credit: $arg{crednum}";
72       last;
73     }
74
75     my @taxlines = ();
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
84         push @taxlines,
85           [ $location->desc, $taxline->setup, $taxlocnum, $taxratelocnum ];
86         $amount -= $location->amount;
87       }
88       if ($amount > 0) {
89         push @taxlines,
90           [ $taxline->itemdesc. ' (default)', sprintf('%.2f', $amount), '', '' ];
91       }
92     }
93
94     $return->{taxlines} = \@taxlines;
95
96   }
97 }
98
99 </%init>