RT#40215: OFM - Separate permissions for edit dates and contract dates [v3 only]
[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       or $curuser->access_right('Change package contract end date');
19
20 my $contract_only = $curuser->access_right('Edit customer package dates') ? 0 : 1;
21 $contract_only = 1 if $cgi->param('contract_only');
22
23 my $pkgnum = $cgi->param('pkgnum') or die;
24 my $old = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
25 my %hash = $old->hash;
26 foreach ( $contract_only ? qw( contract_end ) : qw( start_date setup bill last_bill contract_end ) ) {
27   if ( $cgi->param($_) =~ /^(\d+)$/ ) {
28     $hash{$_} = $1;
29   } else {
30     $hash{$_} = '';
31   }
32   # adjourn, expire, resume not editable this way
33 }
34
35 my $new;
36 my $error;
37 $new = new FS::cust_pkg \%hash;
38 $error = $new->replace($old);
39
40 if (!$error) {
41   my @supp_pkgs = $old->supplemental_pkgs;
42   foreach $new (@supp_pkgs) {
43     foreach ( $contract_only ? qw( contract_end ) : qw( start_date setup contract_end ) ) {
44       # propagate these to supplementals
45       $new->set($_, $hash{$_});
46     }
47     if (( $hash{'bill'} ne $old->get('bill') ) && !$contract_only ) {
48       if ( $hash{'bill'} and $old->get('bill') ) {
49         # adjust by the same interval
50         my $diff = $hash{'bill'} - $old->get('bill');
51         $new->set('bill', $new->get('bill') + $diff);
52       } else {
53         # absolute date
54         $new->set('bill', $hash{'bill'});
55       }
56     }
57     $error = $new->replace;
58     $error .= ' (supplemental package '.$new->pkgnum.')' if $error; 
59     last if $error;
60   }
61 }
62
63 </%init>