2 % $cgi->param('error', $error);
3 % $cgi->redirect(popurl(3). 'misc/change_pkg.cgi?'. $cgi->query_string );
6 <& /elements/header-popup.html, emt("Package changed") &>
7 <SCRIPT TYPE="text/javascript">
16 my $curuser = $FS::CurrentUser::CurrentUser;
19 unless $curuser->access_right('Change customer package');
21 my $cust_pkg = qsearchs({
22 'table' => 'cust_pkg',
23 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
24 'hashref' => { 'pkgnum' => scalar($cgi->param('pkgnum')), },
25 'extra_sql' => ' AND '. $curuser->agentnums_sql,
27 die 'unknown pkgnum' unless $cust_pkg;
29 my %change = map { $_ => scalar($cgi->param($_)) }
30 qw( locationnum pkgpart quantity );
32 $change{'keep_dates'} = 1;
34 if ( $cgi->param('locationnum') == -1 ) {
35 my $cust_location = FS::cust_location->new({
36 'custnum' => $cust_pkg->custnum,
37 map { $_ => scalar($cgi->param($_)) }
38 FS::cust_main->location_fields
40 $change{'cust_location'} = $cust_location;
46 # setup_discountnum and change_discountnum may contain one of the following:
47 # - "-1" Represents the 'other' option. Results in a new entry to the
49 # - "-2" Represents the "waive setup fee" option. Sets cust_pkg.waive_setup = Y
50 # - int Represents the id for a discount row: discount.discountnum
52 # $change{waive_setup} = '';
53 # for my $type (qw|setup recur|) {
54 # my $dnum = $cgi->param("${type}_discountnum");
56 # if ($dnum eq '-2' && $type eq 'setup') {
57 # $change{waive_setup} = 'Y';
58 # } elsif ($val =~ /^-?\d+$/) {
59 # $discount{$type} = {discountnum => $dnum};
60 # if ($dnum eq '-1') {
61 # $discount{$type}->{amount} = $cgi->param("${type}_discountnum_amount");
62 # $discount{$type}->{percent} = $cgi->param("${type}_discountnum_percent");
65 # # Shouldn't happen without funny business
66 # $error = "Bad value ${type}_discountnum ($val)";
71 $change{waive_setup} = '';
72 for my $type (qw|setup_discountnum recur_discountnum|) {
73 my $dnum = $cgi->param($type);
75 if ($dnum eq '-2' && $type eq 'setup_discountnum') {
77 $change{waive_setup} = 'Y';
78 } elsif ($dnum =~ /^-?\d+$/) {
80 $change{$type} = $dnum;
81 $change{"${type}_amount"} = $cgi->param("${type}_amount");
82 $change{"${type}_percent"} = $cgi->param("${type}_percent");
83 $change{"${type}_months"} = $cgi->param("${type}_months");
84 } elsif ($dnum eq '') {
85 # Set discount as no discount
86 $change{"${type}"} = 0;
88 $error = "Bad value ${type}_discountnum ($dnum)";
93 if (defined($cgi->param('contract_end'))) {
94 $change{'contract_end'} = parse_datetime($cgi->param('contract_end'));
98 if ( $cgi->param('delay') ) {
99 my $date = parse_datetime($cgi->param('start_date'));
101 $error = "Invalid change date '".$cgi->param('start_date')."'.";
102 } elsif ( $date < $now ) {
103 $error = "Change date ".$cgi->param('start_date')." is in the past.";
105 # schedule the change
106 $change{'start_date'} = $date;
107 $error = $cust_pkg->change_later(\%change);
111 # for now, can't change usageprice with change_later
112 my @old_cust_pkg_usageprice = $cust_pkg->cust_pkg_usageprice;
114 # build new usageprice array
115 # false laziness with /edit/process/quick-cust_pkg.cgi
116 my @cust_pkg_usageprice = ();
117 foreach my $quantity_param ( grep { $cgi->param($_) && $cgi->param($_) > 0 }
118 grep /^usagepricenum(\d+)_quantity$/,
122 $quantity_param =~ /^usagepricenum(\d+)_quantity$/ or die 'unpossible';
124 push @cust_pkg_usageprice, new FS::cust_pkg_usageprice {
125 usagepricepart => scalar($cgi->param("usagepricenum${num}_usagepricepart")),
126 quantity => scalar($cgi->param($quantity_param)),
130 # Need to figure out if usagepricepart quantities changed
131 my %oldup = map { $_->usagepricepart, $_->quantity } @old_cust_pkg_usageprice;
132 my %newup = map { $_->usagepricepart, $_->quantity } @cust_pkg_usageprice;
133 my $usagechanged = 0;
134 foreach my $up (keys %oldup) {
135 last if $usagechanged;
136 $usagechanged = 1 unless $oldup{$up} == $newup{$up};
138 foreach my $up (keys %newup) {
139 last if $usagechanged;
140 $usagechanged = 1 unless $oldup{$up} == $newup{$up};
142 $change{'cust_pkg_usageprice'} = \@cust_pkg_usageprice;
144 # special case: if there's a package change scheduled, and it matches
145 # the parameters the user requested this time, then change to the existing
147 if ( $cust_pkg->change_to_pkgnum ) {
148 my $change_to = FS::cust_pkg->by_key($cust_pkg->change_to_pkgnum);
150 $change_to->pkgpart == $change{'pkgpart'} and
151 $change_to->locationnum == $change{'locationnum'} and
152 $change_to->quantity == $change{'quantity'} and
153 $change_to->contract_end == $change{'contract_end'} and
154 $change_to->waive_setup == $change{'waive_setup'} and
157 %change = ( 'cust_pkg' => $change_to );
161 # do a package change right now
162 my $pkg_or_error = $cust_pkg->change( \%change );
163 $error = ref($pkg_or_error) ? '' : $pkg_or_error;