summaryrefslogtreecommitdiff
path: root/httemplate/edit/process/change-cust_pkg.html
blob: c8c623641ebc16f61b96535b8f545740e5128274 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
% if ($error) {
%   $cgi->param('error', $error);
%   $cgi->redirect(popurl(3). 'misc/change_pkg.cgi?'. $cgi->query_string );
% } else {

    <& /elements/header-popup.html, emt("Package changed") &>
      <SCRIPT TYPE="text/javascript">
        topreload();
      </SCRIPT>
    </BODY>
    </HTML>

% }
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;

die "access denied"
  unless $curuser->access_right('Change customer package');

my $cust_pkg = qsearchs({
  'table'     => 'cust_pkg',
  'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
  'hashref'   => { 'pkgnum' => scalar($cgi->param('pkgnum')), },
  'extra_sql' => ' AND '. $curuser->agentnums_sql,
});
die 'unknown pkgnum' unless $cust_pkg;

my %change = map { $_ => scalar($cgi->param($_)) }
                 qw( locationnum pkgpart quantity );

$change{'keep_dates'} = 1;

if ( $cgi->param('locationnum') == -1 ) {
  my $cust_location = FS::cust_location->new({
    'custnum' => $cust_pkg->custnum,
    map { $_ => scalar($cgi->param($_)) }
        FS::cust_main->location_fields
  });
  $change{'cust_location'} = $cust_location;
}

my $error;

# Discounts:
# setup_discountnum and change_discountnum may contain one of the following:
# - "-1" Represents the 'other' option.  Results in a new entry to the
#        discount table.
# - "-2" Represents the "waive setup fee" option. Sets cust_pkg.waive_setup = Y
# - int  Represents the id for a discount row: discount.discountnum
# my %discount;
# $change{waive_setup} = '';
# for my $type (qw|setup recur|) {
#   my $dnum = $cgi->param("${type}_discountnum");

#   if ($dnum eq '-2' && $type eq 'setup') {
#     $change{waive_setup} = 'Y';
#   } elsif ($val =~ /^-?\d+$/) {
#     $discount{$type} = {discountnum => $dnum};
#     if ($dnum eq '-1') {
#       $discount{$type}->{amount}  = $cgi->param("${type}_discountnum_amount");
#       $discount{$type}->{percent} = $cgi->param("${type}_discountnum_percent");
#     }
#   } else {
#     # Shouldn't happen without funny business
#     $error = "Bad value ${type}_discountnum ($val)";
#   }
# }


$change{waive_setup} = '';
for my $type (qw|setup_discountnum recur_discountnum|) {
  my $dnum = $cgi->param($type);

  if ($dnum eq '-2' && $type eq 'setup_discountnum') {
    # Waive Discount
    $change{waive_setup} = 'Y';
  } elsif ($dnum =~ /^-?\d+$/) {
    # Set discountnum
    $change{$type} = $dnum;
    $change{"${type}_amount"}  = $cgi->param("${type}_amount");
    $change{"${type}_percent"} = $cgi->param("${type}_percent");
    $change{"${type}_months"}  = $cgi->param("${type}_months");
  } elsif ($dnum eq '') {
    # Set discount as no discount
    $change{"${type}"} = 0;
  } else {
    $error = "Bad value ${type}_discountnum ($dnum)";
  }
}

my $now = time;
if (defined($cgi->param('contract_end'))) {
  $change{'contract_end'} = parse_datetime($cgi->param('contract_end'));
}

unless ($error) {
  if ( $cgi->param('delay') ) {
    my $date = parse_datetime($cgi->param('start_date'));
    if (!$date) {
      $error = "Invalid change date '".$cgi->param('start_date')."'.";
    } elsif ( $date < $now ) {
      $error = "Change date ".$cgi->param('start_date')." is in the past.";
    } else {
      # schedule the change
      $change{'start_date'} = $date;
      $error = $cust_pkg->change_later(\%change);
    }
  } else {

    # for now, can't change usageprice with change_later
    my @old_cust_pkg_usageprice = $cust_pkg->cust_pkg_usageprice;

    # build new usageprice array
    # false laziness with /edit/process/quick-cust_pkg.cgi
    my @cust_pkg_usageprice = ();
    foreach my $quantity_param ( grep { $cgi->param($_) && $cgi->param($_) > 0 }
                                   grep /^usagepricenum(\d+)_quantity$/,
                                     $cgi->param
                               )
    {
      $quantity_param =~ /^usagepricenum(\d+)_quantity$/ or die 'unpossible';
      my $num = $1;
      push @cust_pkg_usageprice, new FS::cust_pkg_usageprice {
        usagepricepart => scalar($cgi->param("usagepricenum${num}_usagepricepart")),
        quantity       => scalar($cgi->param($quantity_param)),
      };
    }

    # Need to figure out if usagepricepart quantities changed
    my %oldup = map { $_->usagepricepart, $_->quantity } @old_cust_pkg_usageprice;
    my %newup = map { $_->usagepricepart, $_->quantity } @cust_pkg_usageprice;
    my $usagechanged = 0;
    foreach my $up (keys %oldup) {
      last if $usagechanged;
      $usagechanged = 1 unless $oldup{$up} == $newup{$up};
    }
    foreach my $up (keys %newup) {
      last if $usagechanged;
      $usagechanged = 1 unless $oldup{$up} == $newup{$up};
    }
    $change{'cust_pkg_usageprice'} = \@cust_pkg_usageprice;

    # special case: if there's a package change scheduled, and it matches
    # the parameters the user requested this time, then change to the existing
    # future package.
    if ( $cust_pkg->change_to_pkgnum ) {
      my $change_to = FS::cust_pkg->by_key($cust_pkg->change_to_pkgnum);
      if (
        $change_to->pkgpart      == $change{'pkgpart'} and
        $change_to->locationnum  == $change{'locationnum'} and
        $change_to->quantity     == $change{'quantity'} and
        $change_to->contract_end == $change{'contract_end'} and
        $change_to->waive_setup  == $change{'waive_setup'} and
        !$usagechanged
      ) {
        %change = ( 'cust_pkg' => $change_to );
      }
    }

    # do a package change right now
    my $pkg_or_error = $cust_pkg->change( \%change );
    $error = ref($pkg_or_error) ? '' : $pkg_or_error;
  }
}

</%init>