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;
43 if ( $cgi->param('setup_discountnum') =~ /^(-?\d+)$/ ) {
45 $change{waive_setup} = 'Y';
47 $change{setup_discountnum} = $1;
48 $change{setup_discountnum_amount} = $cgi->param('setup_discountnum_amount');
49 $change{setup_discountnum_percent} = $cgi->param('setup_discountnum_percent');
55 if (defined($cgi->param('contract_end'))) {
56 $change{'contract_end'} = parse_datetime($cgi->param('contract_end'));
60 if ( $cgi->param('delay') ) {
61 my $date = parse_datetime($cgi->param('start_date'));
63 $error = "Invalid change date '".$cgi->param('start_date')."'.";
64 } elsif ( $date < $now ) {
65 $error = "Change date ".$cgi->param('start_date')." is in the past.";
68 $change{'start_date'} = $date;
69 $error = $cust_pkg->change_later(\%change);
73 # for now, can't change usageprice with change_later
74 my @old_cust_pkg_usageprice = $cust_pkg->cust_pkg_usageprice;
76 # build new usageprice array
77 # false laziness with /edit/process/quick-cust_pkg.cgi
78 my @cust_pkg_usageprice = ();
79 foreach my $quantity_param ( grep { $cgi->param($_) && $cgi->param($_) > 0 }
80 grep /^usagepricenum(\d+)_quantity$/,
84 $quantity_param =~ /^usagepricenum(\d+)_quantity$/ or die 'unpossible';
86 push @cust_pkg_usageprice, new FS::cust_pkg_usageprice {
87 usagepricepart => scalar($cgi->param("usagepricenum${num}_usagepricepart")),
88 quantity => scalar($cgi->param($quantity_param)),
92 # Need to figure out if usagepricepart quantities changed
93 my %oldup = map { $_->usagepricepart, $_->quantity } @old_cust_pkg_usageprice;
94 my %newup = map { $_->usagepricepart, $_->quantity } @cust_pkg_usageprice;
96 foreach my $up (keys %oldup) {
97 last if $usagechanged;
98 $usagechanged = 1 unless $oldup{$up} == $newup{$up};
100 foreach my $up (keys %newup) {
101 last if $usagechanged;
102 $usagechanged = 1 unless $oldup{$up} == $newup{$up};
104 $change{'cust_pkg_usageprice'} = \@cust_pkg_usageprice;
106 # special case: if there's a package change scheduled, and it matches
107 # the parameters the user requested this time, then change to the existing
109 if ( $cust_pkg->change_to_pkgnum ) {
110 my $change_to = FS::cust_pkg->by_key($cust_pkg->change_to_pkgnum);
112 $change_to->pkgpart == $change{'pkgpart'} and
113 $change_to->locationnum == $change{'locationnum'} and
114 $change_to->quantity == $change{'quantity'} and
115 $change_to->contract_end == $change{'contract_end'} and
116 $change_to->waive_setup == $change{'waive_setup'} and
119 %change = ( 'cust_pkg' => $change_to );
123 # do a package change right now
124 my $pkg_or_error = $cust_pkg->change( \%change );
125 $error = ref($pkg_or_error) ? '' : $pkg_or_error;