308ea8ffd845b1543f3b405832bc0876b91e2b6a
[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     <% header(emt("Package changed")) %>
7       <SCRIPT TYPE="text/javascript">
8         window.top.location.reload();
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 my $error;
44 my $now = time;
45 if (defined($cgi->param('contract_end'))) {
46   $change{'contract_end'} = parse_datetime($cgi->param('contract_end'));
47 }
48
49 unless ($error) {
50   if ( $cgi->param('delay') ) {
51     my $date = parse_datetime($cgi->param('start_date'));
52     if (!$date) {
53       $error = "Invalid change date '".$cgi->param('start_date')."'.";
54     } elsif ( $date < $now ) {
55       $error = "Change date ".$cgi->param('start_date')." is in the past.";
56     } else {
57       # schedule the change
58       $change{'start_date'} = $date;
59       $error = $cust_pkg->change_later(\%change);
60     }
61   } else {
62
63     # for now, can't change usageprice with change_later
64     my @old_cust_pkg_usageprice = $cust_pkg->cust_pkg_usageprice;
65
66     # build new usageprice array
67     # false laziness with /edit/process/quick-cust_pkg.cgi
68     my @cust_pkg_usageprice = ();
69     foreach my $quantity_param ( grep { $cgi->param($_) && $cgi->param($_) > 0 }
70                                    grep /^usagepricenum(\d+)_quantity$/,
71                                      $cgi->param
72                                )
73     {
74       $quantity_param =~ /^usagepricenum(\d+)_quantity$/ or die 'unpossible';
75       my $num = $1;
76       push @cust_pkg_usageprice, new FS::cust_pkg_usageprice {
77         usagepricepart => scalar($cgi->param("usagepricenum${num}_usagepricepart")),
78         quantity       => scalar($cgi->param($quantity_param)),
79       };
80     }
81
82     # Need to figure out if usagepricepart quantities changed
83     my %oldup = map { $_->usagepricepart, $_->quantity } @old_cust_pkg_usageprice;
84     my %newup = map { $_->usagepricepart, $_->quantity } @cust_pkg_usageprice;
85     my $usagechanged = 0;
86     foreach my $up (keys %oldup) {
87       last if $usagechanged;
88       $usagechanged = 1 unless $oldup{$up} == $newup{$up};
89     }
90     foreach my $up (keys %newup) {
91       last if $usagechanged;
92       $usagechanged = 1 unless $oldup{$up} == $newup{$up};
93     }
94     $change{'cust_pkg_usageprice'} = \@cust_pkg_usageprice;
95
96     # special case: if there's a package change scheduled, and it matches
97     # the parameters the user requested this time, then change to the existing
98     # future package.
99     if ( $cust_pkg->change_to_pkgnum ) {
100       my $change_to = FS::cust_pkg->by_key($cust_pkg->change_to_pkgnum);
101       if (
102         $change_to->pkgpart      == $change{'pkgpart'} and
103         $change_to->locationnum  == $change{'locationnum'} and
104         $change_to->quantity     == $change{'quantity'} and
105         $change_to->contract_end == $change{'contract_end'} and
106         !$usagechanged
107       ) {
108         %change = ( 'cust_pkg' => $change_to );
109       }
110     }
111
112     # do a package change right now
113     my $pkg_or_error = $cust_pkg->change( \%change );
114     $error = ref($pkg_or_error) ? '' : $pkg_or_error;
115   }
116 }
117
118 </%init>