RT# 24643, added waive setup fee option to change package screen.
[freeside.git] / httemplate / edit / process / change-cust_pkg.html
1 % if ($error) {
2 %   $cgi->param('error', $error);
3 %   $cgi->redirect(popurl(3). 'misc/change_pkg.cgi?'. $cgi->query_string );
4 % } else {
5
6     <& /elements/header-popup.html, emt("Package changed") &>
7       <SCRIPT TYPE="text/javascript">
8         topreload();
9       </SCRIPT>
10     </BODY>
11     </HTML>
12
13 % }
14 <%init>
15
16 my $curuser = $FS::CurrentUser::CurrentUser;
17
18 die "access denied"
19   unless $curuser->access_right('Change customer package');
20
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,
26 });
27 die 'unknown pkgnum' unless $cust_pkg;
28
29 my %change = map { $_ => scalar($cgi->param($_)) }
30                  qw( locationnum pkgpart quantity );
31
32 $change{'keep_dates'} = 1;
33
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
39   });
40   $change{'cust_location'} = $cust_location;
41 }
42
43 if ( $cgi->param('setup_discountnum') =~ /^(-?\d+)$/ ) { 
44   if ( $1 == -2 ) {
45     $change{waive_setup} = 'Y';
46   } else {
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');
50   }
51 }
52
53 my $error;
54 my $now = time;
55 if (defined($cgi->param('contract_end'))) {
56   $change{'contract_end'} = parse_datetime($cgi->param('contract_end'));
57 }
58
59 unless ($error) {
60   if ( $cgi->param('delay') ) {
61     my $date = parse_datetime($cgi->param('start_date'));
62     if (!$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.";
66     } else {
67       # schedule the change
68       $change{'start_date'} = $date;
69       $error = $cust_pkg->change_later(\%change);
70     }
71   } else {
72
73     # for now, can't change usageprice with change_later
74     my @old_cust_pkg_usageprice = $cust_pkg->cust_pkg_usageprice;
75
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$/,
81                                      $cgi->param
82                                )
83     {
84       $quantity_param =~ /^usagepricenum(\d+)_quantity$/ or die 'unpossible';
85       my $num = $1;
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)),
89       };
90     }
91
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;
95     my $usagechanged = 0;
96     foreach my $up (keys %oldup) {
97       last if $usagechanged;
98       $usagechanged = 1 unless $oldup{$up} == $newup{$up};
99     }
100     foreach my $up (keys %newup) {
101       last if $usagechanged;
102       $usagechanged = 1 unless $oldup{$up} == $newup{$up};
103     }
104     $change{'cust_pkg_usageprice'} = \@cust_pkg_usageprice;
105
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
108     # future package.
109     if ( $cust_pkg->change_to_pkgnum ) {
110       my $change_to = FS::cust_pkg->by_key($cust_pkg->change_to_pkgnum);
111       if (
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
117         !$usagechanged
118       ) {
119         %change = ( 'cust_pkg' => $change_to );
120       }
121     }
122
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;
126   }
127 }
128
129 </%init>