RT# 79284 Option to set discount at Change Package
[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 waive_setup);
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 %discount = (discountnum => $cgi->param('discountnum'));
44 if (%discount) {
45   $discount{$_} = $cgi->param("discountnum_$_")
46     for qw(_type amount months percent setup);
47 }
48
49 my $error;
50 my $now = time;
51 if (defined($cgi->param('contract_end'))) {
52   $change{'contract_end'} = parse_datetime($cgi->param('contract_end'));
53 }
54
55 unless ($error) {
56   if ( $cgi->param('delay') ) {
57     my $date = parse_datetime($cgi->param('start_date'));
58     if (!$date) {
59       $error = "Invalid change date '".$cgi->param('start_date')."'.";
60     } elsif ( $date < $now ) {
61       $error = "Change date ".$cgi->param('start_date')." is in the past.";
62     } else {
63       # schedule the change
64       $change{'start_date'} = $date;
65       $change{discount} = \%discount if %discount;
66       $error = $cust_pkg->change_later(\%change);
67     }
68   } else {
69     # special case: if there's a package change scheduled, and it matches
70     # the parameters the user requested this time, then change to the existing
71     # future package.
72     if ( $cust_pkg->change_to_pkgnum ) {
73       my $change_to = FS::cust_pkg->by_key($cust_pkg->change_to_pkgnum);
74       if (
75         $change_to->pkgpart      == $change{'pkgpart'} and
76         $change_to->locationnum  == $change{'locationnum'} and
77         $change_to->quantity     == $change{'quantity'} and
78         $change_to->contract_end == $change{'contract_end'} and
79         $change_to->waive_setup  == $change{'waive_setup'}
80       ) {
81         %change = ( 'cust_pkg' => $change_to );
82       }
83     }
84
85     # do a package change right now
86     my $pkg_or_error = $cust_pkg->change( \%change );
87     $pkg_or_error->change_discount(\%discount)
88       if ref $pkg_or_error && $discount{discountnum} =~ /$-?\d+$/;
89     $error = ref($pkg_or_error) ? '' : $pkg_or_error;
90   }
91 }
92
93 </%init>