summaryrefslogtreecommitdiff
path: root/httemplate/elements/wa_state_tax_district.js
blob: 03f262b52f6e8271924b7d185193030be8805f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<%doc>

Provide js function in support of a lookup hook for wa_state_tax_districts

wa_state_tax_district()
* Checks form_address_info() to collect address data
* If any addresses are in Washington State,
* Uses misc/xmlhttp-wa_state-find_district_for_address.html to query
  wa state tax districting database
* Displays error, or updates the district input element for the addresses
* Calls submit_continue() upon success
* Calls submit_abort() upon error

</%doc>

function wa_state_tax_district() {
  // Queries the WA State API to get a Tax District for this address
  // upon failure: User can choose to skip or cancel
  // upon success: set value of district input box and submit_continue()

  address_info = form_address_info();
  // console.log( address_info );

  if (
       address_info['state'] != 'WA'
    && address_info['state'] != 'wa'
    && address_info['bill_state'] != 'WA'
    && address_info['bill_state'] != 'wa'
    && (
      address_info['same']
      || (
        address_info['ship_state'] != 'WA'
        && address_info['ship_state'] != 'wa'
      )
    )
  ) {
    // nothing to do, not in Washington state
    submit_continue();
    return;
  }

  wa_state_tax_district_overlib( 'Looking up tax district... please wait...' );

  $.post({
    url: "<% $fsurl %>misc/xmlhttp-wa_state-find_district_for_address.html",
    data: address_info,
    success: function(response) {
      // console.log(response);

      let error = '';
      if ( response['error'] ) {
        error = error + response['error'] + ' ';
      }

      // populate Billing Address district into form, or record error
      if ( response['bill'] && response['bill']['district'] ) {
        $('#bill_district').val( response['bill']['district'] );
      }
      else if ( response['bill'] && response['bill']['error'] ) {
        error = error + 'Cound not set tax district for billing address. ';
      }

      // populate Shipping Address district into form, or record error
      if (
        ! address_info['same']
        && response['ship']
        && response['ship']['district']
      ) {
        $('#ship_district').val( response['ship']['district'] );
      }
      else if (
        ! address_info['same']
        && response['ship']
        && response['ship']['error']
      ) {
        error = error + 'Could not set tax district for service address. ';
      }

      // populate Plain Address district into form, or record error
      if (
        response['address']
        && response['address']['district']
      ) {
        $('#district').val( response['address']['district'] );
      }
      else if (
        response['address']
        && response['address']['error']
      ) {
        error = error + 'Could not set tax district for address. ';
      }

      if ( error ) {
        wa_state_tax_district_overlib(
          'An error occured determining Washington state tax district:<br>'
          + '<br>'
          + error + '<br>'
          + '<br>'
          + 'If you choose to skip this step, taxes will not be calculated '
          + 'for this customer, unless you enter a tax district manually.'
          + '<br>'
          + '<a href="https://webgis.dor.wa.gov/taxratelookup/SalesTax.aspx" target="_blank">See WA Dept of Revenue</a>'
        );
      }
      else {
        cClick();
        submit_continue();
        return;
      }

    }
  })
  .fail(function() {
    wa_state_tax_district_overlib(
      'A network error occured determining Washington state tax district:<br>'
      + '<br>'
      + 'If you choose to skip this step, taxes will not be calculated '
      + 'for this customer, unless you enter a tax district manually.'
      + '<br>'
      + '<a href="https://webgis.dor.wa.gov/taxratelookup/SalesTax.aspx" target="_blank">See WA Dept of Revenue</a>'
    );
  });
}

function wa_state_tax_district_overlib(html) {
  html =
      '<div style="text-align: center;">'
    +   '<h2>Washington State Tax District Lookup</h2>'
    +   '<p>' + html + '</p>'
    + '<a href="#" onclick="wa_state_tax_district_skip()">skip</a>'
    + ' | '
    + '<a href="#" onclick="wa_state_tax_district_cancel()">cancel</a>'
    + '</div>';

  overlib(
    html,
    CAPTION, 'WA State Tax District',
    STICKY,
    CLOSETEXT, '',
    MIDX, 0,
    MIDY, 0,
    WIDTH, 500,
    BGCOLOR, '#339',
    CGCOLOR, '#339',
    TEXTSIZE, 3
  );
}

function wa_state_tax_district_skip() {
  // Click target to skip tax district determination
  cClick()
  submit_continue();
}

function wa_state_tax_district_cancel() {
  // Click target to cancel submit from tax district determination
  cClick()
  submit_abort();
}