RT# 82949 - changes section name from fees to pricing, better opiton
[freeside.git] / httemplate / elements / wa_state_tax_district.js
1 <%doc>
2
3 Provide js function in support of a lookup hook for wa_state_tax_districts
4
5 wa_state_tax_district()
6 * Checks form_address_info() to collect address data
7 * If any addresses are in Washington State,
8 * Uses misc/xmlhttp-wa_state-find_district_for_address.html to query
9   wa state tax districting database
10 * Displays error, or updates the district input element for the addresses
11 * Calls submit_continue() upon success
12 * Calls submit_abort() upon error
13
14 </%doc>
15
16 function wa_state_tax_district() {
17   // Queries the WA State API to get a Tax District for this address
18   // upon failure: User can choose to skip or cancel
19   // upon success: set value of district input box and submit_continue()
20
21   address_info = form_address_info();
22   // console.log( address_info );
23
24   if (
25        address_info['state'] != 'WA'
26     && address_info['state'] != 'wa'
27     && address_info['bill_state'] != 'WA'
28     && address_info['bill_state'] != 'wa'
29     && (
30       address_info['same']
31       || (
32         address_info['ship_state'] != 'WA'
33         && address_info['ship_state'] != 'wa'
34       )
35     )
36   ) {
37     // nothing to do, not in Washington state
38     submit_continue();
39     return;
40   }
41
42   wa_state_tax_district_overlib( 'Looking up tax district... please wait...' );
43
44   $.post({
45     url: "<% $fsurl %>misc/xmlhttp-wa_state-find_district_for_address.html",
46     data: address_info,
47     success: function(response) {
48       // console.log(response);
49
50       let error = '';
51       if ( response['error'] ) {
52         error = error + response['error'] + ' ';
53       }
54
55       // populate Billing Address district into form, or record error
56       if ( response['bill'] && response['bill']['district'] ) {
57         $('#bill_district').val( response['bill']['district'] );
58       }
59       else if ( response['bill'] && response['bill']['error'] ) {
60         error = error + 'Cound not set tax district for billing address. ';
61       }
62
63       // populate Shipping Address district into form, or record error
64       if (
65         ! address_info['same']
66         && response['ship']
67         && response['ship']['district']
68       ) {
69         $('#ship_district').val( response['ship']['district'] );
70       }
71       else if (
72         ! address_info['same']
73         && response['ship']
74         && response['ship']['error']
75       ) {
76         error = error + 'Could not set tax district for service address. ';
77       }
78
79       // populate Plain Address district into form, or record error
80       if (
81         response['address']
82         && response['address']['district']
83       ) {
84         $('#district').val( response['address']['district'] );
85       }
86       else if (
87         response['address']
88         && response['address']['error']
89       ) {
90         error = error + 'Could not set tax district for address. ';
91       }
92
93       if ( error ) {
94         wa_state_tax_district_overlib(
95           'An error occured determining Washington state tax district:<br>'
96           + '<br>'
97           + error + '<br>'
98           + '<br>'
99           + 'If you choose to skip this step, taxes will not be calculated '
100           + 'for this customer, unless you enter a tax district manually.'
101           + '<br>'
102           + '<a href="https://webgis.dor.wa.gov/taxratelookup/SalesTax.aspx" target="_blank">See WA Dept of Revenue</a>'
103         );
104       }
105       else {
106         cClick();
107         submit_continue();
108         return;
109       }
110
111     }
112   })
113   .fail(function() {
114     wa_state_tax_district_overlib(
115       'A network error occured determining Washington state tax district:<br>'
116       + '<br>'
117       + 'If you choose to skip this step, taxes will not be calculated '
118       + 'for this customer, unless you enter a tax district manually.'
119       + '<br>'
120       + '<a href="https://webgis.dor.wa.gov/taxratelookup/SalesTax.aspx" target="_blank">See WA Dept of Revenue</a>'
121     );
122   });
123 }
124
125 function wa_state_tax_district_overlib(html) {
126   html =
127       '<div style="text-align: center;">'
128     +   '<h2>Washington State Tax District Lookup</h2>'
129     +   '<p>' + html + '</p>'
130     + '<a href="#" onclick="wa_state_tax_district_skip()">skip</a>'
131     + ' | '
132     + '<a href="#" onclick="wa_state_tax_district_cancel()">cancel</a>'
133     + '</div>';
134
135   overlib(
136     html,
137     CAPTION, 'WA State Tax District',
138     STICKY,
139     CLOSETEXT, '',
140     MIDX, 0,
141     MIDY, 0,
142     WIDTH, 500,
143     BGCOLOR, '#339',
144     CGCOLOR, '#339',
145     TEXTSIZE, 3
146   );
147 }
148
149 function wa_state_tax_district_skip() {
150   // Click target to skip tax district determination
151   cClick()
152   submit_continue();
153 }
154
155 function wa_state_tax_district_cancel() {
156   // Click target to cancel submit from tax district determination
157   cClick()
158   submit_abort();
159 }