address standardization, part 2
[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 = ();
9 # 'standardize_locations');
10
11 push @fixups, 'confirm_censustract_bill', 'confirm_censustract_ship'
12     if $conf->exists('cust_main-require_censustract');
13
14 my $uniqueness = $conf->config('cust_main-check_unique');
15 push @fixups, 'check_unique'
16     if $uniqueness and !$opt{'custnum'};
17
18 push @fixups, 'do_submit'; # always last
19 </%init>
20 var fixups = <% encode_json(\@fixups) %>;
21 var fixup_position;
22 var running = false;
23
24 <&| /elements/onload.js &>
25 submit_abort();
26 </&>
27
28 %# state machine to deal with all the asynchronous stuff we're doing
29 %# call this after each fixup on success:
30 function submit_continue() {
31   if ( running ) {
32     window[ fixups[fixup_position++] ].call();
33   }
34 }
35
36 %# or on failure:
37 function submit_abort() {
38   running = false;
39   fixup_position = 0;
40   document.CustomerForm.submitButton.disabled = false;
41   cClick();
42 }
43
44 function bottomfixup(what) {
45   fixup_position = 0;
46   document.CustomerForm.submitButton.disabled = true;
47   running = true;
48   submit_continue();
49 }
50
51 function do_submit() {
52   document.CustomerForm.submit();
53 }
54
55 <& /elements/standardize_locations.js,
56   'callback' => 'submit_continue();',
57   'billship' => 1,
58   'with_census' => 1, # no with_firm, apparently
59 &>
60
61 % # the value in pre+'censustract' is the confirmed censustract (either from
62 % # the previous saved record, or from address standardization (if the backend
63 % # supports it), or from an aborted previous submit. only need to reconfirm
64 % # if it's empty.
65 function confirm_censustract(pre) {
66   var cf = document.CustomerForm;
67   if ( cf.elements[pre+'censustract'].value == '' ) {
68     var address_info = form_address_info();
69     address_info[pre+'latitude']  = cf.elements[pre+'latitude'].value;
70     address_info[pre+'longitude'] = cf.elements[pre+'longitude'].value;
71     address_info['prefix'] = pre;
72     OLpostAJAX(
73         '<%$p%>/misc/confirm-censustract.html',
74         'q=' + encodeURIComponent(JSON.stringify(address_info)),
75         function() {
76           if ( OLresponseAJAX ) {
77             overlib( OLresponseAJAX, CAPTION, 'Confirm censustract', STICKY,
78               AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
79               576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
80               TEXTSIZE, 3 );
81           } else
82             submit_continue();
83         },
84         0);
85   } else submit_continue();
86 }
87 function confirm_censustract_bill() {
88   confirm_censustract('bill_');
89 }
90
91 function confirm_censustract_ship() {
92   var cf = document.CustomerForm;
93   if ( cf.elements['same'].checked ) {
94     submit_continue();
95   } else {
96     confirm_censustract('ship_');
97   }
98 }
99
100 %# called from confirm-censustract.html
101 function set_censustract(tract, year, pre) {
102   var cf = document.CustomerForm;
103   cf.elements[pre + 'censustract'].value = tract;
104   cf.elements[pre + 'censusyear'].value = year;
105   submit_continue();
106 }
107
108 function check_unique() {
109   var search_hash = {};
110 % if ($uniqueness eq 'address') {
111   search_hash['address'] = [
112     document.CustomerForm.elements['bill_address1'].value,
113     document.CustomerForm.elements['ship_address1'].value
114   ];
115 % }
116 %# no other options yet
117
118 %# supported in IE8+, Firefox 3.5+, WebKit, Opera 10.5+
119   duplicates_form(JSON.stringify(search_hash), confirm_unique);
120 }
121
122 function confirm_unique(arg) {
123   if ( arg.match(/\S/) ) {
124 %# arg contains a complete form to choose an existing customer, or not
125   overlib( arg, CAPTION, 'Duplicate customer', STICKY, AUTOSTATUSCAP, 
126       CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 
127       268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
128   } else { // no duplicates
129     submit_continue();
130   }
131 }
132