Merge branch 'patch-3' of https://github.com/gjones2/Freeside (fix closing tag)
[freeside.git] / httemplate / misc / xmlhttp-cust_bill_pkg-calculate_taxes.html
1 <% to_json($return) %>
2 <%init>
3
4 my $curuser = $FS::CurrentUser::CurrentUser;
5 die "access denied" unless $curuser->access_right('Post credit');
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       my $part_pkg = $cust_bill_pkg->part_pkg;
66       $cust_main->_handle_taxes( $part_pkg,
67                                  $taxlisthash,
68                                  $cust_bill_pkg,
69                                  $cust_bill_pkg->cust_pkg,
70                                  $cust_bill_pkg->cust_bill->_date,
71                                  $cust_bill_pkg->cust_pkg->pkgpart,
72                                );
73
74     }
75
76     if ( @cust_bill_pkg ) {
77
78       my $listref_or_error = 
79         $cust_main->calculate_taxes( \@cust_bill_pkg, $taxlisthash, $cust_bill_pkg[0]->cust_bill->_date );
80
81       unless ( ref( $listref_or_error ) ) {
82         $return->{error} = $listref_or_error;
83         last;
84       }
85
86       my @taxlines = ();
87       my $taxtotal = 0;
88       $return->{taxlines} = \@taxlines;
89       foreach my $taxline ( @$listref_or_error ) {
90         my $amount = $taxline->setup;
91         my $desc = $taxline->desc;
92         foreach my $location ( @{$taxline->cust_bill_pkg_tax_location}, @{$taxline->cust_bill_pkg_tax_rate_location} ) {
93           my $taxlocnum = $location->locationnum || '';
94           my $taxratelocnum = $location->taxratelocationnum || '';
95           $location->cust_bill_pkg_desc($taxline->desc); #ugh @ that kludge
96           $taxtotal += $location->amount;
97           push @taxlines,
98             #[ $location->desc, $taxline->setup, $taxlocnum, $taxratelocnum ];
99             [ $location->desc, $location->amount, $taxlocnum, $taxratelocnum ];
100           $amount -= $location->amount;
101         }
102         if ($amount > 0) {
103           $taxtotal += $amount;
104           push @taxlines,
105             [ $taxline->itemdesc. ' (default)', sprintf('%.2f', $amount), '', '' ];
106         }
107       }
108
109       $return->{taxlines} = \@taxlines;
110       $return->{taxtotal} = sprintf('%.2f', $taxtotal);
111
112     } else {
113
114       $return->{taxlines} = [];
115       $return->{taxtotal} = '0.00';
116
117     }
118
119   }
120
121 }
122
123 </%init>