97816aad76dfa5011be474523feb5a5d18a048dd
[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, '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 <& /elements/standardize_locations.js,
55   'callback' => 'submit_continue();',
56   'billship' => 1,
57   'with_census' => 1, # no with_firm, apparently
58 &>
59
60 % # the value in pre+'censustract' is the confirmed censustract (either from
61 % # the previous saved record, or from address standardization (if the backend
62 % # supports it), or from an aborted previous submit. only need to reconfirm
63 % # if it's empty.
64 function confirm_censustract(pre) {
65   var cf = document.CustomerForm;
66   if ( cf.elements[pre+'censustract'].value == '' ) {
67     var address_info = form_address_info();
68     address_info[pre+'latitude']  = cf.elements[pre+'latitude'].value;
69     address_info[pre+'longitude'] = cf.elements[pre+'longitude'].value;
70     address_info['prefix'] = pre;
71     OLpostAJAX(
72         '<%$p%>/misc/confirm-censustract.html',
73         'q=' + encodeURIComponent(JSON.stringify(address_info)),
74         function() {
75           if ( OLresponseAJAX ) {
76             overlib( OLresponseAJAX, CAPTION, 'Confirm censustract', STICKY,
77               AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
78               576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
79               TEXTSIZE, 3 );
80           } else
81             submit_continue();
82         },
83         0);
84   } else submit_continue();
85 }
86 function confirm_censustract_bill() {
87   confirm_censustract('bill_');
88 }
89
90 function confirm_censustract_ship() {
91   var cf = document.CustomerForm;
92   if ( cf.elements['same'].checked ) {
93     submit_continue();
94   } else {
95     confirm_censustract('ship_');
96   }
97 }
98
99 %# called from confirm-censustract.html
100 function set_censustract(tract, year, pre) {
101   var cf = document.CustomerForm;
102   cf.elements[pre + 'censustract'].value = tract;
103   cf.elements[pre + 'censusyear'].value = year;
104   submit_continue();
105 }
106
107 function check_unique() {
108   var search_hash = {};
109 % if ($uniqueness eq 'address') {
110   search_hash['address'] = [
111     document.CustomerForm.elements['bill_address1'].value,
112     document.CustomerForm.elements['ship_address1'].value
113   ];
114 % }
115 %# no other options yet
116
117 %# supported in IE8+, Firefox 3.5+, WebKit, Opera 10.5+
118   duplicates_form(JSON.stringify(search_hash), confirm_unique);
119 }
120
121 function confirm_unique(arg) {
122   if ( arg.match(/\S/) ) {
123 %# arg contains a complete form to choose an existing customer, or not
124   overlib( arg, CAPTION, 'Duplicate customer', STICKY, AUTOSTATUSCAP, 
125       CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 
126       268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
127   } else { // no duplicates
128     submit_continue();
129   }
130 }
131