RT# 82949 - changes section name from fees to pricing, better opiton
[freeside.git] / httemplate / edit / process / quick-cust_pkg.cgi
1 %if ($error) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(3). 'misc/order_pkg.html?'. $cgi->query_string ) %>
4 %} else {
5 %  my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
6 %               ? ''
7 %               : ';show=packages';
8 %
9 %  my $redir_url = popurl(3);
10 %  if ( $svcpart ) { # for going straight to service provisining after ordering
11 %    $redir_url .= 'edit/'.$part_svc->svcdb.'.cgi?'.
12 %                    'pkgnum='.$cust_pkg->pkgnum. ";svcpart=$svcpart";
13 %    $redir_url .= ";qualnum=$qualnum" if $qualnum;
14 %  } elsif ( $quotationnum ) {
15 %    $redir_url .= "view/quotation.html?quotationnum=$quotationnum";
16 %  } else {
17 %    my $custnum = $cust_main->custnum;
18 %    my $frag = "cust_pkg". $cust_pkg->pkgnum;
19 %    $redir_url .=
20 %      "view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#$frag";
21 %  }
22
23 <& /elements/header-popup.html, 'Package ordered' &>
24   <SCRIPT TYPE="text/javascript">
25     // XXX fancy ajax rebuild table at some point, but a page reload will do for now
26
27     // XXX chop off trailing #target and replace... ?
28     window.top.location = '<% $redir_url %>';
29
30   </SCRIPT>
31
32   </BODY></HTML>
33 %}
34 <%init>
35
36 my $curuser = $FS::CurrentUser::CurrentUser;
37
38 die "access denied"
39   unless $curuser->access_right('Order customer package');
40
41 my $cust_main;
42 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
43   my $custnum = $1;
44   $cust_main = qsearchs({
45     'table'     => 'cust_main',
46     'hashref'   => { 'custnum' => $custnum },
47     'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
48   });
49 }
50
51 my $prospect_main;
52 if ( $cgi->param('prospectnum') =~ /^(\d+)$/ ) {
53   my $prospectnum = $1;
54   $prospect_main = qsearchs({
55     'table'     => 'prospect_main',
56     'hashref'   => { 'prospectnum' => $prospectnum },
57     'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
58   });
59 }
60
61 die 'no custnum or prospectnum' unless $cust_main || $prospect_main;
62
63 #probably not necessary, taken care of by cust_pkg::check
64 $cgi->param('pkgpart') =~ /^(\d+)$/
65   or die 'illegal pkgpart '. $cgi->param('pkgpart');
66 my $pkgpart = $1;
67 $cgi->param('quantity') =~ /^(\d*)$/
68   or die 'illegal quantity '. $cgi->param('quantity');
69 my $quantity = $1 || 1;
70 $cgi->param('refnum') =~ /^(\d*)$/
71   or die 'illegal refnum '. $cgi->param('refnum');
72 my $refnum = $1;
73 $cgi->param('salesnum') =~ /^(\d*)$/
74   or die 'illegal salesnum '. $cgi->param('salesnum');
75 my $salesnum = $1;
76 $cgi->param('contactnum') =~ /^(\-?\d*)$/
77   or die 'illegal contactnum '. $cgi->param('contactnum');
78 my $contactnum = $1;
79 $cgi->param('locationnum') =~ /^(\-?\d*)$/
80   or die 'illegal locationnum '. $cgi->param('locationnum');
81 my $locationnum = $1;
82
83 # for going right to a provision service after ordering a package
84 my( $svcpart, $part_svc ) = ( '', '' );
85 if ( $cgi->param('svcpart') ) {
86   $cgi->param('svcpart') =~ /^(\-?\d*)$/
87      or die 'illegal svcpart '. $cgi->param('svcpart');
88   $svcpart = $1;
89   $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } )
90     or die "unknown svcpart $svcpart";
91 }
92
93 my $qualnum = '';
94 if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
95   $qualnum = $1;
96 }
97 my $quotationnum = '';
98 if ( $cgi->param('quotationnum') =~ /^(\d+)$/ ) {
99   $quotationnum = $1;
100 }
101 # verify this quotation is visible to this user
102
103 my $cust_pkg = '';
104 my $quotation_pkg = '';
105 my $error = '';
106
107 my %hash = (
108     'pkgpart'              => $pkgpart,
109     'quantity'             => $quantity,
110     'salesnum'             => $salesnum,
111     'refnum'               => $refnum,
112     'contactnum'           => $contactnum,
113     'locationnum'          => $locationnum,
114     'contract_end'         => ( scalar($cgi->param('contract_end'))
115                                   ? parse_datetime($cgi->param('contract_end'))
116                                   : ''
117                               ),
118 );
119
120 if ( $cgi->param('setup_discountnum') =~ /^(-?\d+)$/ ) { 
121   if ( $1 == -2 ) {
122     $hash{waive_setup} = 'Y';
123   } else {
124     $hash{setup_discountnum} = $1;
125     $hash{setup_discountnum_amount} = $cgi->param('setup_discountnum_amount');
126     $hash{setup_discountnum_percent} = $cgi->param('setup_discountnum_percent');
127   }
128 }
129
130 if ( $cgi->param('recur_discountnum') =~ /^(-?\d+)$/ ) { 
131   $hash{recur_discountnum} = $1;
132   $hash{recur_discountnum_amount} = $cgi->param('recur_discountnum_amount');
133   $hash{recur_discountnum_percent} = $cgi->param('recur_discountnum_percent');
134   $hash{recur_discountnum_months} = $cgi->param('recur_discountnum_months');
135 }
136
137 $hash{'custnum'} = $cust_main->custnum if $cust_main;
138
139 if ( $cgi->param('start') eq 'on_hold' ) {
140   $hash{'susp'} = 'now';
141 } elsif ( $cgi->param('start') eq 'on_date' ) {
142   $hash{'start_date'} = scalar($cgi->param('start_date'))
143                           ? parse_datetime($cgi->param('start_date'))
144                           : '';
145 }
146
147 my @cust_pkg_usageprice = ();
148 foreach my $quantity_param ( grep { $cgi->param($_) && $cgi->param($_) > 0 }
149                                grep /^usagepricenum(\d+)_quantity$/,
150                                  $cgi->param
151                            )
152 {
153   $quantity_param =~ /^usagepricenum(\d+)_quantity$/ or die 'unpossible';
154   my $num = $1;
155   push @cust_pkg_usageprice, new FS::cust_pkg_usageprice {
156     usagepricepart => scalar($cgi->param("usagepricenum${num}_usagepricepart")),
157     quantity       => scalar($cgi->param($quantity_param)),
158   };
159 }
160 $hash{cust_pkg_usageprice} = \@cust_pkg_usageprice;
161
162 # extract details (false laziness with /misc/order_pkg.html)
163 my $details = {
164   'invoice_detail' => [],
165   'package_comment' => [],
166   'quotation_detail' => [],
167 };
168 foreach my $field ( $cgi->param ) {
169   foreach my $detailtype ( keys %$details ) {
170     if ($field =~ /^$detailtype(\d+)$/) {
171       $details->{$detailtype}->[$1] = $cgi->param($field);
172     }
173   }
174 }
175 foreach my $detailtype ( keys %$details ) {
176   @{ $details->{$detailtype} } = grep { length($_) } @{ $details->{$detailtype} };
177 }
178
179 if ( $quotationnum ) {
180
181   $quotation_pkg = new FS::quotation_pkg \%hash;
182   $quotation_pkg->quotationnum($quotationnum);
183   $quotation_pkg->prospectnum($prospect_main->prospectnum) if $prospect_main;
184
185   my %opt = @{ $details->{'quotation_detail'} }
186   ? (
187     quotation_details => $details->{'quotation_detail'},
188     copy_on_order     => scalar($cgi->param('copy_on_order')) ? 'Y' : '',
189   )
190   : ();
191
192   if ( $locationnum == -1 ) {
193     my $cust_location = FS::cust_location->new({
194       'custnum'     => $cust_main ? $cust_main->custnum : '',
195       'prospectnum' => $prospect_main ? $prospect_main->prospectnum : '',
196       map { $_ => scalar($cgi->param($_)) }
197         FS::cust_main->location_fields
198     });
199     $opt{'cust_location'} = $cust_location;
200   } else {
201     $opt{'locationnum'} = $locationnum;
202   }
203
204   $error = $quotation_pkg->insert(%opt);
205
206 } else {
207
208   $cust_pkg = new FS::cust_pkg \%hash;
209
210   $cust_pkg->no_auto( scalar($cgi->param('no_auto')) );
211
212   my %opt = ( 'cust_pkg' => $cust_pkg );
213
214   if ( $contactnum == -1 ) {
215     my $contact = FS::contact->new({
216       'custnum' => scalar($cgi->param('custnum')),
217       map { $_ => scalar($cgi->param("contactnum_$_")) } qw( first last )
218     });
219     $opt{'contact'} = $contact;
220   }
221
222   if ( $locationnum == -1 ) {
223     my $cust_location = FS::cust_location->new({
224       map { $_ => scalar($cgi->param($_)) }
225           ('custnum', FS::cust_main->location_fields)
226     });
227     $opt{'cust_location'} = $cust_location;
228   } else {
229     $opt{'locationnum'} = $locationnum;
230   }
231
232   $opt{'invoice_details'} = $details->{'invoice_detail'} if @{ $details->{'invoice_detail'} };
233   $opt{'package_comments'} = $details->{'package_comment'} if @{ $details->{'package_comment'} };
234
235   $error = $cust_main->order_pkg( \%opt );
236
237 }
238
239 </%init>