summaryrefslogtreecommitdiff
path: root/httemplate/edit/process/quick-charge.cgi
blob: 07797d0c4346b6dbf56651ccb5ecd806087c58d5 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
% if ( $error ) {
%   $cgi->param('error', $error );
<% $cgi->redirect($p.'quick-charge.html?'. $cgi->query_string) %>
% } else {
<% header(emt($message)) %>
  <SCRIPT TYPE="text/javascript">
    window.top.location.reload();
  </SCRIPT>
  </BODY></HTML>
% }
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;
die "access denied"
  unless $curuser->access_right('One-time charge');

my $error = '';
my $conf = new FS::conf;
my $param = $cgi->Vars;

my @description = ();
for ( my $row = 0; exists($param->{"description$row"}); $row++ ) {
  push @description, $param->{"description$row"}
    if ($param->{"description$row"} =~ /\S/);
}

$param->{"custnum"} =~ /^(\d+)$/
  or $error .= "Illegal customer number " . $param->{"custnum"} . "  ";
my $custnum = $1;

my $cust_main = FS::cust_main->by_key($custnum)
  or die "custnum $custnum not found";

exists($curuser->agentnums_href->{$cust_main->agentnum})
  or die "access denied";

my $message;

if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) {
  $message = "One-time charge changed";
  my $pkgnum = $1;
  die "access denied"
    unless $curuser->access_right('Modify one-time charge');

  my $cust_pkg = FS::cust_pkg->by_key($1)
    or die "pkgnum $pkgnum not found";

  my $part_pkg = $cust_pkg->part_pkg;
  die "pkgnum $pkgnum is not a one-time charge" unless $part_pkg->freq eq '0';

  my ($amount, $quantity, $start_date);
  if ( $cgi->param('amount') =~ /^\s*(\d*(\.\d{1,2})*)\s*$/ ) {
    $amount = sprintf('%.2f', $1);
  }
  if ( $cgi->param('quantity') =~ /^\s*(\d*)\s*$/ ) {
    $quantity = $1 || 1;
  }
  if ( $cgi->param('start_date') ) {
    $start_date = parse_datetime($cgi->param('start_date'));
  } else {
    $start_date = time;
  }

  $error = $cust_pkg->modify_charge(
      'pkg'               => scalar($cgi->param('pkg')),
      'classnum'          => scalar($cgi->param('classnum')),
      'additional'        => \@description,
      'adjust_commission' => ($cgi->param('adjust_commission') ? 1 : 0),
      'amount'            => $amount,
      'quantity'          => $quantity,
      'start_date'        => $start_date,
  );

} else {
  $message = "One-time charge added";
  # the usual case: new one-time charge
  $param->{"amount"} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/
    or $error .= "Illegal amount " . $param->{"amount"} . "  ";
  my $amount = $1;

  my $quantity = 1;
  if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
    $quantity = $1;
  }

  $param->{'tax_override'} =~ /^\s*([,\d]*)\s*$/
    or $error .= "Illegal tax override " . $param->{"tax_override"} . "  ";
  my $override = $1;

  if ( $param->{'taxclass'} eq '(select)' ) {
    $error .= "Must select a tax class.  "
      unless ($conf->exists('enable_taxproducts') &&
               ( $override || $param->{taxproductnum} )
             );
    $cgi->param('taxclass', '');
  }

  unless ( $error ) {
    my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
      or $error .= "Unknown customer number $custnum.  ";

    $error ||= $cust_main->charge( {
      'amount'        => $amount,
      'quantity'      => $quantity,
      'bill_now'      => scalar($cgi->param('bill_now')),
      'invoice_terms' => scalar($cgi->param('invoice_terms')),
      'start_date'    => ( scalar($cgi->param('start_date'))
                             ? parse_datetime($cgi->param('start_date'))
                             : ''
                         ),
      'no_auto'       => scalar($cgi->param('no_auto')),
      'pkg'           => scalar($cgi->param('pkg')),
      'setuptax'      => scalar($cgi->param('setuptax')),
      'taxclass'      => scalar($cgi->param('taxclass')),
      'taxproductnum' => scalar($cgi->param('taxproductnum')),
      'tax_override'  => $override,
      'classnum'      => scalar($cgi->param('classnum')),
      'additional'    => \@description,
    } );
  }
}

</%init>