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