summaryrefslogtreecommitdiff
path: root/httemplate/edit/process/cust_main_county-add.cgi
blob: fcd6be48f56b19078fa813186baecbeef2819009 (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
<% include('/elements/header-popup.html', 'Addition successful' ) %>

<SCRIPT TYPE="text/javascript">
  topreload();
</SCRIPT>

</BODY>
</HTML>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');

$cgi->param('taxnum') =~ /^(\d+)$/ or die "Illegal taxnum!";
my $taxnum = $1;
my $cust_main_county = qsearchs('cust_main_county',{'taxnum'=>$taxnum})
  or die ("Unknown taxnum!");

my @expansion = split /[\n\r]{1,2}/, $cgi->param('expansion');

@expansion=map {
  unless ( /^\s*([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)\s*$/ ) {
    $cgi->param('error', "Illegal item in expansion: $_");
    print $cgi->redirect(popurl(2). "cust_main_county-expand.cgi?". $cgi->query_string );
    myexit();
  }
  $1;
} @expansion;

my $what = $cgi->param('what');
foreach my $new_tax_area ( @expansion ) {

  # Clone specific tax columns from original tax row
  #
  # UI Note:  Preserving original behavior, of cloning
  #   tax amounts into new tax record, against better
  #   judgement.  If the new city/county/state has a
  #   different tax value than the one being populated
  #   (rather likely?) now the user must remember to
  #   revisit each newly created tax row, and correct
  #   the possibly incorrect tax values that were populated.
  #   Values would be easier to identify and correct if
  #   they were initially populated with 0% tax rates
  # District Note: The 'district' column is NOT cloned
  #   to the new tax row.   Manually entered taxes
  #   are not be divided into road maintenance districts
  #   like Washington state sales taxes
  my $new = FS::cust_main_county->new({
    map { $_ => $cust_main_county->getfield($_) }
    qw/
      charge_prediscount
      exempt_amount
      exempt_amount_currency
      recurtax
      setuptax
      tax
      taxname
    /
  });

  # Clone additional location columns, based on the $what value
  my %clone_cols_for = (
    state  => [qw/country /],
    county => [qw/country state/],
    city   => [qw/country state county/],
  );

  die "unknown what: $what"
    unless grep { $_ eq $what } keys %clone_cols_for;

  $new->setfield( $_ => $cust_main_county->getfield($_) )
    for @{ $clone_cols_for{ $cgi->param('what') } };

  # In the US, store cities upper case for USPS validation
  $new_tax_area = uc($new_tax_area)
    if $what eq 'city'
    && $new->country eq 'US';

  $new->setfield( $what, $new_tax_area );
  if ( my $error = $new->insert ) {
    die $error;
  }
}

</%init>