Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / httemplate / misc / xmlhttp-cust_bill_pkg-calculate_taxes.html
1 <% encode_json($return) %>\
2 <%init>
3
4 my $curuser = $FS::CurrentUser::CurrentUser;
5 die "access denied" unless $curuser->access_right('Credit line items');
6
7 my $DEBUG = 0;
8
9 my $conf = new FS::Conf;
10
11 my $sub = $cgi->param('sub');
12
13 my $return = {};
14
15 if ( $sub eq 'calculate_taxes' ) {
16
17   {
18
19     my %arg = $cgi->param('arg');
20     $return = \%arg;
21     warn join('', map "$_: $arg{$_}\n", keys %arg )
22       if $DEBUG;
23
24     #some false laziness w/cust_credit::credit_lineitems
25
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';
31
32     my @billpkgnums = split(',', $arg{billpkgnums});
33     my @setuprecurs = split(',', $arg{setuprecurs});
34     my @amounts =     split(',', $arg{amounts});
35
36     my @cust_bill_pkg = ();
37     my $taxlisthash = {};
38     while ( @billpkgnums ) {
39       my $billpkgnum = shift @billpkgnums;
40       my $setuprecur = shift @setuprecurs;
41       my $amount     = shift @amounts;
42
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";
49
50       #shouldn't be passed# next if $cust_bill_pkg->pkgnum == 0;
51
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('');
57       } else {
58         $cust_bill_pkg->recur($amount);
59         $cust_bill_pkg->setup(0);
60         $cust_bill_pkg->unitsetup(0);
61       }
62
63       push @cust_bill_pkg, $cust_bill_pkg;
64
65       $cust_main->_handle_taxes( $taxlisthash, $cust_bill_pkg );
66     }
67
68     if ( @cust_bill_pkg ) {
69
70       my $listref_or_error = 
71         $cust_main->calculate_taxes( \@cust_bill_pkg, $taxlisthash, $cust_bill_pkg[0]->cust_bill->_date );
72
73       unless ( ref( $listref_or_error ) ) {
74         $return->{error} = $listref_or_error;
75         last;
76       }
77
78       my @taxlines = ();
79       my $taxtotal = 0;
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')} )
87         {
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;
92           push @taxlines,
93             #[ $location->desc, $taxline->setup, $taxlocnum, $taxratelocnum ];
94             [ $location->desc, $location->amount, $taxlocnum, $taxratelocnum ];
95           $amount -= $location->amount;
96         }
97         if ($amount > 0) {
98           $taxtotal += $amount;
99           push @taxlines,
100             [ $taxline->itemdesc. ' (default)', sprintf('%.2f', $amount), '', '' ];
101         }
102       }
103
104       $return->{taxlines} = \@taxlines;
105       $return->{taxtotal} = sprintf('%.2f', $taxtotal);
106
107     } else {
108
109       $return->{taxlines} = [];
110       $return->{taxtotal} = '0.00';
111
112     }
113
114   }
115
116 }
117
118 </%init>