summaryrefslogtreecommitdiff
path: root/httemplate/misc/xmlhttp-calculate_taxes.html
blob: 2bb1f4cce817f2764a1be890379c9abd57b55e86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<% encode_json($return) %>\
<%init>

my $DEBUG = 0;

my $conf = new FS::Conf;

my $sub = $cgi->param('sub');

my $return = {};

if ( $sub eq 'calculate_taxes' ) {

  {
    my %arg = $cgi->param('arg');
    $return = \%arg;
    warn join('', map "$_: $arg{$_}\n", keys %arg )
      if $DEBUG;

    my $cust_credit = qsearchs( 'cust_credit', { 'crednum' => $arg{crednum} } );
    unless ($cust_credit) {
      $return->{error} = "No such credit: $arg{crednum}";
      last;
    }
  
    my %cust_bill_pkg = ();
    my @items = split(',', $arg{items});
    while ( @items ) {
      my ($billpkgnum, $s_or_r, $amount ) = splice(@items, 0, 3);
      unless ( defined($amount) ) {
        $return->{error} = "Bad items argument list";
        last;
      }
      unless ( $cust_bill_pkg{$billpkgnum} ) {
        my $cust_bill_pkg =
          qsearchs( 'cust_bill_pkg', { 'billpkgnum' => $billpkgnum } );
        unless ($cust_bill_pkg) {
          $return->{error} = "No such line item: $billpkgnum";
          last;
        }
        next unless $cust_bill_pkg->pkgnum > 0; #hmmm @ one shot (-1)
        unless ($cust_bill_pkg->cust_pkg->custnum == $cust_credit->custnum) {
          $return->{error} = "Credit/line item customer mismatch";
          last;
        }
        $cust_bill_pkg{$billpkgnum} = $cust_bill_pkg;
        $cust_bill_pkg->setup(0);
        $cust_bill_pkg->recur(0);
      }

      last if $return->{error};

      my $cust_bill_pkg = $cust_bill_pkg{$billpkgnum};
      $s_or_r = $s_or_r eq 'setup' ? 'setup' : 'recur';
      my $value = sprintf('%.2f', $cust_bill_pkg->$s_or_r + $amount);
      $cust_bill_pkg->$s_or_r($value);
    }

    last if $return->{error};

    my $cust_main = $cust_credit->cust_main;

    my $taxlisthash = {};
    foreach my $cust_bill_pkg (values %cust_bill_pkg) {
      $cust_main->_handle_taxes( $taxlisthash, $cust_bill_pkg );
    }
    my $listref_or_error = 
      $cust_main->calculate_taxes( [ values %cust_bill_pkg ], $taxlisthash, [ values %cust_bill_pkg ]->[0]->cust_bill->_date );

    unless ( ref( $listref_or_error ) ) {
      $return->{error} = "No such credit: $arg{crednum}";
      last;
    }

    my @taxlines = ();
    $return->{taxlines} = \@taxlines;
    foreach my $taxline ( @$listref_or_error ) {
      my $amount = $taxline->setup;
      my $desc = $taxline->desc;
      foreach my $location ( @{$taxline->cust_bill_pkg_tax_location}, @{$taxline->cust_bill_pkg_tax_rate_location} ) {
        my $taxlocnum = $location->locationnum || '';
        my $taxratelocnum = $location->taxratelocationnum || '';
        $location->cust_bill_pkg_desc($taxline->desc); #ugh @ that kludge
        push @taxlines,
          [ $location->desc, $taxline->setup, $taxlocnum, $taxratelocnum ];
        $amount -= $location->amount;
      }
      if ($amount > 0) {
        push @taxlines,
          [ $taxline->itemdesc. ' (default)', sprintf('%.2f', $amount), '', '' ];
      }
    }

    $return->{taxlines} = \@taxlines;

  }
}

</%init>