customer bill/ship location refactoring, #940
[freeside.git] / httemplate / edit / cust_main / bottomfixup.js
1 <%init>
2 my %opt = @_; # custnum
3 my $conf = new FS::Conf;
4
5 my $company_latitude  = $conf->config('company_latitude');
6 my $company_longitude = $conf->config('company_longitude');
7
8 my @fixups = ('copy_payby_fields', 'standardize_locations');
9
10 push @fixups, 'fetch_censustract'
11     if $conf->exists('cust_main-require_censustract');
12
13 push @fixups, 'check_unique'
14     if $conf->exists('cust_main-check_unique') and !$opt{'custnum'};
15
16 push @fixups, 'do_submit'; # always last
17 </%init>
18
19 var fixups = <% encode_json(\@fixups) %>;
20 var fixup_position;
21
22 %# state machine to deal with all the asynchronous stuff we're doing
23 %# call this after each fixup on success:
24 function submit_continue() {
25   window[ fixups[fixup_position++] ].call();
26 }
27
28 %# or on failure:
29 function submit_abort() {
30   fixup_position = 0;
31   document.CustomerForm.submitButton.disabled = false;
32   cClick();
33 }
34
35 function bottomfixup(what) {
36   fixup_position = 0;
37   document.CustomerForm.submitButton.disabled = true;
38   submit_continue();
39 }
40
41 function do_submit() {
42   document.CustomerForm.submit();
43 }
44
45 function copy_payby_fields() {
46   var layervars = new Array(
47     'payauto', 'billday',
48     'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
49     'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
50     'paystart_month', 'paystart_year', 'payissue',
51     'payip',
52     'paid'
53   );
54
55   var cf = document.CustomerForm;
56   var payby = cf.payby.options[cf.payby.selectedIndex].value;
57   for ( f=0; f < layervars.length; f++ ) {
58     var field = layervars[f];
59     copyelement( cf.elements[payby + '_' + field],
60                  cf.elements[field]
61                );
62   }
63   submit_continue();
64 }
65
66 %# call submit_continue() on completion...
67 %# otherwise not touching standardize_locations for now
68 <% include( '/elements/standardize_locations.js',
69             'callback' => 'submit_continue();',
70             'main_prefix' => 'bill_',
71             'no_company' => 1,
72           )
73 %>
74
75 var prefix;
76 function fetch_censustract() {
77
78   //alert('fetch census tract data');
79   prefix = document.getElementById('same').checked ? 'bill_' : 'ship_';
80   var cf = document.CustomerForm;
81   var state_el = cf.elements[prefix + 'state'];
82   var census_data = new Array(
83     'year',     <% $conf->config('census_year') || '2012' %>,
84     'address1', cf.elements[prefix + 'address1'].value,
85     'city',     cf.elements[prefix + 'city'].value,
86     'state',    state_el.options[ state_el.selectedIndex ].value,
87     'zip',      cf.elements[prefix + 'zip'].value
88   );
89
90   censustract( census_data, update_censustract );
91
92 }
93
94 var set_censustract;
95
96 function update_censustract(arg) {
97
98   var argsHash = eval('(' + arg + ')');
99
100   var cf = document.CustomerForm;
101
102 /*  var msacode    = argsHash['msacode'];
103   var statecode  = argsHash['statecode'];
104   var countycode = argsHash['countycode'];
105   var tractcode  = argsHash['tractcode'];
106   
107   var newcensus = 
108     new String(statecode)  +
109     new String(countycode) +
110     new String(tractcode).replace(/\s$/, '');  // JSON 1 workaround */
111   var error      = argsHash['error'];
112   var newcensus  = argsHash['censustract'];
113
114   set_censustract = function () {
115
116     cf.elements[prefix + 'censustract'].value = newcensus;
117     submit_continue();
118
119   }
120
121   if (error || cf.elements[prefix + 'censustract'].value != newcensus) {
122     // popup an entry dialog
123
124     if (error) { newcensus = error; }
125     newcensus.replace(/.*ndefined.*/, 'Not found');
126
127     var latitude = cf.elements[prefix + 'latitude'].value 
128                    || '<% $company_latitude %>';
129     var longitude= cf.elements[prefix + 'longitude'].value 
130                    || '<% $company_longitude %>';
131
132     var choose_censustract =
133       '<CENTER><BR><B>Confirm censustract</B><BR>' +
134       '<A href="http://maps.ffiec.gov/FFIECMapper/TGMapSrv.aspx?' +
135       'census_year=<% $conf->config('census_year') || '2012' %>' +
136       '&latitude=' + latitude +
137       '&longitude=' + longitude +
138       '" target="_blank">Map service module location</A><BR>' +
139       '<A href="http://maps.ffiec.gov/FFIECMapper/TGMapSrv.aspx?' +
140       'census_year=<% $conf->config('census_year') || '2012' %>' +
141       '&zip_code=' + cf.elements[prefix + 'zip'].value +
142       '" target="_blank">Map zip code center</A><BR><BR>' +
143       '<TABLE>';
144     
145     choose_censustract = choose_censustract + 
146       '<TR><TH style="width:50%">Entered census tract</TH>' +
147         '<TH style="width:50%">Calculated census tract</TH></TR>' +
148       '<TR><TD>' + cf.elements[prefix + 'censustract'].value +
149         '</TD><TD>' + newcensus + '</TD></TR>' +
150         '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
151
152     choose_censustract = choose_censustract +
153       '<TR><TD ALIGN="center">' +
154         '<BUTTON TYPE="button" onClick="submit_continue();"><IMG SRC="<%$p%>images/error.png" ALT=""> Use entered census tract </BUTTON>' + 
155       '</TD><TD ALIGN="center">' +
156         '<BUTTON TYPE="button" onClick="set_censustract();"><IMG SRC="<%$p%>images/tick.png" ALT=""> Use calculated census tract </BUTTON>' + 
157       '</TD></TR>' +
158       '<TR><TD COLSPAN=2 ALIGN="center">' +
159         '<BUTTON TYPE="button" onClick="submit_abort();"><IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel submission</BUTTON></TD></TR>' +
160         
161       '</TABLE></CENTER>';
162
163     overlib( choose_censustract, CAPTION, 'Confirm censustract', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
164
165   } else {
166
167     submit_continue();
168
169   }
170
171 }
172
173 function copyelement(from, to) {
174   if ( from == undefined ) {
175     to.value = '';
176   } else if ( from.type == 'select-one' ) {
177     to.value = from.options[from.selectedIndex].value;
178     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
179   } else if ( from.type == 'checkbox' ) {
180     if ( from.checked ) {
181       to.value = from.value;
182     } else {
183       to.value = '';
184     }
185   } else {
186     if ( from.value == undefined ) {
187       to.value = '';
188     } else {
189       to.value = from.value;
190     }
191   }
192   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
193 }
194
195 function check_unique() {
196   var search_hash = new Object;
197 % foreach ($conf->config('cust_main-check_unique')) {
198   search_hash['<% $_ %>'] = document.CustomerForm.elements['<% $_ %>'].value;
199 % }
200
201 %# supported in IE8+, Firefox 3.5+, WebKit, Opera 10.5+
202   duplicates_form(JSON.stringify(search_hash), confirm_unique);
203 }
204
205 function confirm_unique(arg) {
206   if ( arg.match(/\S/) ) {
207 %# arg contains a complete form to choose an existing customer, or not
208   overlib( arg, CAPTION, 'Duplicate customer', STICKY, AUTOSTATUSCAP, 
209       CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 
210       268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
211   } else { // no duplicates
212     submit_continue();
213   }
214 }
215