when address standardization returns a censustract, don't try to look it up through...
[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, 'confirm_censustract_bill', 'confirm_censustract_ship'
11     if $conf->exists('cust_main-require_censustract');
12
13 my $uniqueness = $conf->config('cust_main-check_unique');
14 push @fixups, 'check_unique'
15     if $uniqueness and !$opt{'custnum'};
16
17 push @fixups, 'do_submit'; # always last
18 </%init>
19 var fixups = <% encode_json(\@fixups) %>;
20 var fixup_position;
21 var running = false;
22
23 <&| /elements/onload.js &>
24 submit_abort();
25 </&>
26
27 %# state machine to deal with all the asynchronous stuff we're doing
28 %# call this after each fixup on success:
29 function submit_continue() {
30   if ( running ) {
31     window[ fixups[fixup_position++] ].call();
32   }
33 }
34
35 %# or on failure:
36 function submit_abort() {
37   running = false;
38   fixup_position = 0;
39   document.CustomerForm.submitButton.disabled = false;
40   cClick();
41 }
42
43 function bottomfixup(what) {
44   fixup_position = 0;
45   document.CustomerForm.submitButton.disabled = true;
46   running = true;
47   submit_continue();
48 }
49
50 function do_submit() {
51   document.CustomerForm.submit();
52 }
53
54 function copy_payby_fields() {
55   var layervars = new Array(
56     'payauto', 'billday',
57     'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
58     'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
59     'paystart_month', 'paystart_year', 'payissue',
60     'payip',
61     'paid'
62   );
63
64   var cf = document.CustomerForm;
65   var payby = cf.payby.options[cf.payby.selectedIndex].value;
66   for ( f=0; f < layervars.length; f++ ) {
67     var field = layervars[f];
68     copyelement( cf.elements[payby + '_' + field],
69                  cf.elements[field]
70                );
71   }
72   submit_continue();
73 }
74
75 <& /elements/standardize_locations.js,
76   'callback' => 'submit_continue();',
77   'billship' => 1,
78   'with_census' => 1, # no with_firm, apparently
79 &>
80
81 function copyelement(from, to) {
82   if ( from == undefined ) {
83     to.value = '';
84   } else if ( from.type == 'select-one' ) {
85     to.value = from.options[from.selectedIndex].value;
86     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
87   } else if ( from.type == 'checkbox' ) {
88     if ( from.checked ) {
89       to.value = from.value;
90     } else {
91       to.value = '';
92     }
93   } else {
94     if ( from.value == undefined ) {
95       to.value = '';
96     } else {
97       to.value = from.value;
98     }
99   }
100   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
101 }
102
103 % # the value in pre+'censustract' is the confirmed censustract (either from
104 % # the previous saved record, or from address standardization (if the backend
105 % # supports it), or from an aborted previous submit. only need to reconfirm
106 % # if it's empty.
107 function confirm_censustract(pre) {
108   var cf = document.CustomerForm;
109   if ( cf.elements[pre+'censustract'].value == '' ) {
110     var address_info = form_address_info();
111     address_info[pre+'latitude']  = cf.elements[pre+'latitude'].value;
112     address_info[pre+'longitude'] = cf.elements[pre+'longitude'].value;
113     address_info['prefix'] = pre;
114     OLpostAJAX(
115         '<%$p%>/misc/confirm-censustract.html',
116         'q=' + encodeURIComponent(JSON.stringify(address_info)),
117         function() {
118           if ( OLresponseAJAX ) {
119             overlib( OLresponseAJAX, CAPTION, 'Confirm censustract', STICKY,
120               AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
121               576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
122               TEXTSIZE, 3 );
123           } else
124             submit_continue();
125         },
126         0);
127   } else submit_continue();
128 }
129 function confirm_censustract_bill() {
130   confirm_censustract('bill_');
131 }
132
133 function confirm_censustract_ship() {
134   var cf = document.CustomerForm;
135   if ( cf.elements['same'].checked ) {
136     submit_continue();
137   } else {
138     confirm_censustract('ship_');
139   }
140 }
141
142 %# called from confirm-censustract.html
143 function set_censustract(tract, year, pre) {
144   var cf = document.CustomerForm;
145   cf.elements[pre + 'censustract'].value = tract;
146   cf.elements[pre + 'censusyear'].value = year;
147   submit_continue();
148 }
149
150 function check_unique() {
151   var search_hash = {};
152 % if ($uniqueness eq 'address') {
153   search_hash['address'] = [
154     document.CustomerForm.elements['bill_address1'].value,
155     document.CustomerForm.elements['ship_address1'].value
156   ];
157 % }
158 %# no other options yet
159
160 %# supported in IE8+, Firefox 3.5+, WebKit, Opera 10.5+
161   duplicates_form(JSON.stringify(search_hash), confirm_unique);
162 }
163
164 function confirm_unique(arg) {
165   if ( arg.match(/\S/) ) {
166 %# arg contains a complete form to choose an existing customer, or not
167   overlib( arg, CAPTION, 'Duplicate customer', STICKY, AUTOSTATUSCAP, 
168       CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 
169       268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
170   } else { // no duplicates
171     submit_continue();
172   }
173 }
174