fd2893487c0aad509c6c0072cf23f1a4e0f8d51c
[freeside.git] / httemplate / edit / process / REAL_cust_pkg.cgi
1 %if ( $error ) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "REAL_cust_pkg.cgi?". $cgi->query_string ) %>
4 %} else { 
5 %  my $custnum = $new->custnum;
6 %  my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
7 %               ? ''
8 %               : ';show=packages';
9 %  my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
10 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#$frag" ) %>
11 %}
12 <%init>
13
14 my $curuser = $FS::CurrentUser::CurrentUser;
15
16 die "access denied"
17   unless $curuser->access_right('Edit customer package dates');
18
19 my $pkgnum = $cgi->param('pkgnum') or die;
20 my $old = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
21 my %hash = $old->hash;
22 foreach ( qw( start_date setup bill last_bill contract_end ) ) {
23   if ( $cgi->param($_) =~ /^(\d+)$/ ) {
24     $hash{$_} = $1;
25   } else {
26     $hash{$_} = '';
27   }
28   # adjourn, expire, resume not editable this way
29 }
30
31 my $new;
32 my $error;
33 $new = new FS::cust_pkg \%hash;
34 $error = $new->replace($old);
35
36 if (!$error) {
37   my @supp_pkgs = $old->supplemental_pkgs;
38   foreach $new (@supp_pkgs) {
39     foreach ( qw( start_date setup contract_end ) ) {
40       # propagate these to supplementals
41       $new->set($_, $hash{$_});
42     }
43     if ( $hash{'bill'} ne $old->get('bill') ) {
44       if ( $hash{'bill'} and $old->get('bill') ) {
45         # adjust by the same interval
46         my $diff = $hash{'bill'} - $old->get('bill');
47         $new->set('bill', $new->get('bill') + $diff);
48       } else {
49         # absolute date
50         $new->set('bill', $hash{'bill'});
51       }
52     }
53     $error = $new->replace;
54     $error .= ' (supplemental package '.$new->pkgnum.')' if $error; 
55     last if $error;
56   }
57 }
58
59 </%init>