pick/enter a location when ordering a package, RT#4499
[freeside.git] / httemplate / misc / order_pkg.html
1 <% include('/elements/header-popup.html', 'Order new package' ) %>
2
3 <% include('/elements/xmlhttp.html',
4               'url'  => $p.'misc/location.cgi',
5               'subs' => [ 'get_location' ],
6            )
7 %>
8
9 <SCRIPT TYPE="text/javascript">
10
11   function locationnum_changed(what) {
12     var locationnum = what.options[what.selectedIndex].value;
13     if ( locationnum == -1 ) {
14
15 %     for (@location_fields) { 
16         what.form.<%$_%>.disabled = false;
17         what.form.<%$_%>.style.backgroundColor = '#ffffff';
18 %     } 
19
20       what.form.address1.value = '';
21       what.form.address2.value = '';
22       what.form.city.value = '';
23       what.form.zip.value = '';
24       changeSelect(what.form.country, <% $countrydefault |js_string %>);
25 %#shouldn't we sleep/wait here until the state dropdown is updated?
26 %#(is it even triggered???)
27       changeSelect(what.form.state, <% $statedefault |js_string %>);
28       what.form.county.selectedIndex = 0;
29
30     } else {
31
32       if ( locationnum == 0 ) {
33         what.form.address1.value = <% $cust_main->address1 |js_string %>;
34         what.form.address2.value = <% $cust_main->address2 |js_string %>;
35         what.form.city.value = <% $cust_main->city |js_string %>;
36         what.form.zip.value = <% $cust_main->zip |js_string %>;
37         changeSelect(what.form.country, <% $cust_main->country | js_string %> );
38 %#shouldn't we sleep/wait here until the state dropdown is updated?
39 %#(is it even triggered???)
40         changeSelect(what.form.state, <% $cust_main->state | js_string %> );
41 %#shouldn't we sleep/wait here until the county dropdown is updated?
42 %#(is it even triggered???)
43         changeSelect(what.form.county, <% $cust_main->county | js_string %> );
44       } else {
45         get_location( locationnum, update_location );
46       } 
47
48 %#sleep/wait until dropdowns are updated?
49 %     for (@location_fields) { 
50         what.form.<%$_%>.disabled = true;
51         what.form.<%$_%>.style.backgroundColor = '#dddddd';
52 %     } 
53
54     }
55   }
56
57   function changeSelect(what, value) {
58     for ( var i=0; i<what.length; i++) {
59       if ( what.options[i].value == value ) {
60         what.selectedIndex = i;
61       }
62     }
63   }
64
65   function update_location( hash ) {
66     alert(hash);
67   }
68
69   function enable_order_pkg () {
70     if ( document.OrderPkgForm.pkgpart.selectedIndex > 0 ) {
71       document.OrderPkgForm.submit.disabled = false;
72     } else {
73       document.OrderPkgForm.submit.disabled = true;
74     }
75   }
76
77 </SCRIPT>
78
79 <% include('/elements/error.html') %>
80
81 <FORM NAME="OrderPkgForm" ACTION="<% $p %>edit/process/quick-cust_pkg.cgi" METHOD="POST">
82
83 <INPUT TYPE="hidden" NAME="custnum" VALUE="<% $cust_main->custnum %>">
84
85 <% ntable("#cccccc", 2) %>
86 <TR>
87   <TH ALIGN="right">Package</TH>
88   <TD COLSPAN=7>
89     <% include('/elements/select-cust-part_pkg.html',
90                  'curr_value' => $pkgpart,
91                  'cust_main'  => $cust_main,
92                  'onchange'   => 'enable_order_pkg',
93               )
94     %>
95   </TD>
96 </TR>
97
98 % if ( $conf->exists('pkg_referral') ) {
99   <% include('/elements/tr-select-part_referral.html',
100                'curr_value'    => scalar( $cgi->param('refnum') ), #get rid of empty_label first# || $cust_main->refnum,
101                'disable_empty' => 1,
102                'multiple'      => $conf->exists('pkg_referral-multiple'),
103                'colspan'       => 7,
104             )
105   %>
106 % }
107
108 <TR>
109   <TH ALIGN="right">Service location</TH>
110   <TD COLSPAN=7>
111     <SELECT NAME="locationnum" onChange="locationnum_changed(this);">
112       <OPTION VALUE="">(default service address)
113 %     foreach my $loc ( $cust_main->cust_location ) {
114         <OPTION VALUE="<% $loc->locationnum %>"
115                 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
116         ><% $loc->line |h %>
117 %     }
118       <OPTION VALUE="-1"
119               <% $locationnum == -1 ? 'SELECTED' : '' %>
120       >Add new location
121     </SELECT>
122   </TD>
123 </TR>
124
125 <% include('/elements/location.html',
126              'object'       => $cust_location,
127              #'onchange' ?  probably not
128              'disabled'     => ( $locationnum == -1 ? '' : 'DISABLED' ),
129              'no_asterisks' => 1,
130           )
131 %>
132
133 </TABLE>
134
135 <BR>
136 <INPUT NAME="submit" TYPE="submit" VALUE="Order Package" <% $pkgpart ? '' : 'DISABLED' %>>
137
138 </FORM>
139 </BODY>
140 </HTML>
141 <%once>
142
143 my @location_fields = qw( address1 address2 city county state zip country );
144
145 </%once>
146 <%init>
147
148 die "access denied"
149   unless $FS::CurrentUser::CurrentUser->access_right('Order customer package');
150
151 my $conf = new FS::Conf;
152 my $countrydefault = $conf->config('countrydefault') || 'US';
153 my $statedefault = $conf->config('statedefault')
154                    || ($countrydefault eq 'US' ? 'CA' : '');
155
156 $cgi->param('custnum') =~ /^(\d+)$/ or die "no custnum";
157 my $custnum = $1;
158 my $cust_main = qsearchs({
159   'table'     => 'cust_main',
160   'hashref'   => { 'custnum' => $custnum },
161   'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
162 });
163
164 my $pkgpart = scalar($cgi->param('pkgpart'));
165
166 $cgi->param('locationnum') =~ /^(\d*)$/ or die "illegal locationnum";
167 my $locationnum = $1;
168 my $cust_location;
169 if ( $locationnum ) {
170   $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
171     or die "unknown locationnum";
172 } else {
173   $cust_location = new FS::cust_location;
174   if ( $cgi->param('error') && $locationnum == -1 ) {
175     $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
176   } else {
177     $cust_location->$_( $cust_main->$_() ) foreach @location_fields;
178   }
179 }
180
181 </%init>