7dcd232bcf2966b98ea112945a32f73da9687d60
[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 <% header('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 $cgi->param('discountnum') =~ /^(\-?\d*)$/
83   or die 'illegal discountnum '. $cgi->param('discountnum');
84 my $discountnum = $1;
85
86 # for going right to a provision service after ordering a package
87 my( $svcpart, $part_svc ) = ( '', '' );
88 if ( $cgi->param('svcpart') ) {
89   $cgi->param('svcpart') =~ /^(\-?\d*)$/
90      or die 'illegal svcpart '. $cgi->param('svcpart');
91   $svcpart = $1;
92   $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } )
93     or die "unknown svcpart $svcpart";
94 }
95
96 my $qualnum = '';
97 if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
98   $qualnum = $1;
99 }
100 my $quotationnum = '';
101 if ( $cgi->param('quotationnum') =~ /^(\d+)$/ ) {
102   $quotationnum = $1;
103 }
104 # verify this quotation is visible to this user
105
106 my $cust_pkg = '';
107 my $quotation_pkg = '';
108 my $error = '';
109
110 my %hash = (
111     'pkgpart'              => $pkgpart,
112     'quantity'             => $quantity,
113     'start_date'           => ( scalar($cgi->param('start_date'))
114                                   ? parse_datetime($cgi->param('start_date'))
115                                   : ''
116                               ),
117     'salesnum'             => $salesnum,
118     'refnum'               => $refnum,
119     'contactnum'           => $contactnum,
120     'locationnum'          => $locationnum,
121     'discountnum'          => $discountnum,
122     #for the create a new discount case
123     'discountnum__type'    => scalar($cgi->param('discountnum__type')),
124     'discountnum_amount'   => scalar($cgi->param('discountnum_amount')),
125     'discountnum_percent'  => scalar($cgi->param('discountnum_percent')),
126     'discountnum_months'   => scalar($cgi->param('discountnum_months')),
127     'discountnum_setup'    => scalar($cgi->param('discountnum_setup')),
128     'contract_end'         => ( scalar($cgi->param('contract_end'))
129                                   ? parse_datetime($cgi->param('contract_end'))
130                                   : ''
131                               ),
132      'waive_setup'         => ( $cgi->param('waive_setup') eq 'Y' ? 'Y' : '' ),
133 );
134 $hash{'custnum'} = $cust_main->custnum if $cust_main;
135
136 if ( $quotationnum ) {
137
138   $quotation_pkg = new FS::quotation_pkg \%hash;
139   $quotation_pkg->quotationnum($quotationnum);
140   $quotation_pkg->prospectnum($prospect_main->prospectnum) if $prospect_main;
141
142   #XXX handle new location
143   $error = $quotation_pkg->insert;
144
145 } else {
146
147   $cust_pkg = new FS::cust_pkg \%hash;
148
149   $cust_pkg->no_auto( scalar($cgi->param('no_auto')) );
150
151   my %opt = ( 'cust_pkg' => $cust_pkg );
152
153   if ( $contactnum == -1 ) {
154     my $contact = FS::contact->new({
155       'custnum' => scalar($cgi->param('custnum')),
156       map { $_ => scalar($cgi->param("contactnum_$_")) } qw( first last )
157     });
158     $opt{'contact'} = $contact;
159   }
160
161   if ( $locationnum == -1 ) {
162     my $cust_location = FS::cust_location->new({
163       map { $_ => scalar($cgi->param($_)) }
164           ('custnum', FS::cust_main->location_fields)
165     });
166     $opt{'cust_location'} = $cust_location;
167   } else {
168     $opt{'locationnum'} = $locationnum;
169   }
170
171   $error = $cust_main->order_pkg( \%opt );
172
173 }
174
175 </%init>