Subversion Repositories SmartDukaan

Rev

Rev 28493 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
37823 amit 1
// Sellers & Warehouses screen. The server re-checks every rule (WarehouseSetupValidator); these checks only save a
2
// round trip. A refused request comes back as a 400 whose text the ajax helpers show as is.
3
var SellerSetup = (function () {
28300 tejbeer 4
 
37823 amit 5
    var GSTIN = /^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z][0-9A-Z]Z[0-9A-Z]$/;
28300 tejbeer 6
 
37823 amit 7
    function loadPage() {
8
        doGetAjaxRequestHandler(context + "/getAllSeller", function (response) {
9
            $('#main-content').html(response);
10
        });
11
    }
28300 tejbeer 12
 
37823 amit 13
    function loadSeller(sellerId) {
14
        doGetAjaxRequestHandler(context + "/getSellerWareHouse?sellerId=" + sellerId, function (response) {
15
            $('.seller-setup .warehouse-container').html(response);
16
        });
17
    }
28300 tejbeer 18
 
37823 amit 19
    function currentSellerId() {
20
        return $('.seller-warehouses').data('seller-id');
21
    }
28300 tejbeer 22
 
37823 amit 23
    // A page or panel reload replaces the modal's markup, so wait until it has closed and its backdrop is gone.
24
    function closeThen(modalSelector, next) {
25
        $(modalSelector).one('hidden.bs.modal', next).modal('hide');
26
    }
28300 tejbeer 27
 
37823 amit 28
    function value(selector) {
29
        return $.trim($(selector).val() || '');
30
    }
28300 tejbeer 31
 
37823 amit 32
    function postJson(url, payload, onSuccess) {
33
        doPostAjaxRequestWithJsonHandler(url, JSON.stringify(payload), onSuccess);
34
    }
28300 tejbeer 35
 
37823 amit 36
    // Same split as WarehouseAddressMaster.getCustomAddress(): the last comma segment is the city, the rest is
37
    // divided between two lines.
38
    function invoiceLines(address, city) {
39
        var parts = address.split(',').map(function (part) {
40
            return $.trim(part);
41
        }).filter(function (part) {
42
            return part.length > 0;
43
        });
44
        var midpoint = Math.floor(parts.length / 2);
45
        return {
46
            line1: parts.slice(0, midpoint).join(', '),
47
            line2: parts.slice(midpoint).join(', '),
48
            city: city
49
        };
50
    }
28300 tejbeer 51
 
37823 amit 52
    function renderAddressPreview() {
53
        var address = value('#new-address-address');
54
        var city = value('#new-address-city');
55
        if (!address || !city) {
56
            $('#new-address-preview').text('-');
57
            return;
58
        }
59
        var lines = invoiceLines(address, city);
60
        var state = $('#new-address-state option:selected').val() ? $('#new-address-state option:selected').text() : '';
61
        var preview = $('<div>');
62
        [lines.line1, lines.line2, lines.city + ' - ' + value('#new-address-pin'), state].forEach(function (line) {
63
            if (line) {
64
                preview.append($('<div>').text(line));
65
            }
66
        });
67
        $('#new-address-preview').empty().append(preview.children());
68
    }
28300 tejbeer 69
 
37823 amit 70
    function createAddress() {
71
        var payload = {
72
            address: value('#new-address-address'),
73
            city: value('#new-address-city'),
74
            pin: value('#new-address-pin'),
75
            stateId: parseInt($('#new-address-state').val(), 10),
76
            contactPerson: value('#new-address-contact-person'),
77
            contactNumber: value('#new-address-contact-number')
78
        };
79
        if (!payload.address || !payload.city) {
80
            alert('Address and city are required');
81
            return;
82
        }
83
        if (payload.city.indexOf(',') >= 0) {
84
            alert('City must not contain a comma');
85
            return;
86
        }
87
        if (!/^\d{6}$/.test(payload.pin)) {
88
            alert('Pin must be 6 digits');
89
            return;
90
        }
91
        if (!payload.stateId && payload.stateId !== 0) {
92
            alert('Select a state');
93
            return;
94
        }
95
        if (!payload.contactPerson) {
96
            alert('Contact person is required');
97
            return;
98
        }
99
        if (!/^\d{10,15}$/.test(payload.contactNumber)) {
100
            alert('Contact number must be 10 to 15 digits');
101
            return;
102
        }
103
        postJson(context + '/createNewAddress', payload, function (response) {
104
            closeThen('#new-address-modal', loadPage);
105
        });
106
    }
28300 tejbeer 107
 
37823 amit 108
    function renderGstinHint() {
109
        var gstin = value('#new-seller-gstin').toUpperCase();
110
        if (!gstin) {
111
            $('#new-seller-gstin-hint').text('State and PAN are read from the GSTIN.');
112
        } else if (GSTIN.test(gstin)) {
113
            $('#new-seller-gstin-hint').text('State code ' + gstin.substring(0, 2) + ' ยท PAN ' + gstin.substring(2, 12));
114
        } else {
115
            $('#new-seller-gstin-hint').text('Not a valid GSTIN yet');
116
        }
117
    }
28300 tejbeer 118
 
37823 amit 119
    function createSeller() {
120
        var payload = {
121
            label: value('#new-seller-label'),
122
            gstin: value('#new-seller-gstin').toUpperCase(),
123
            address: value('#new-seller-address')
124
        };
125
        if (!payload.label || !payload.address) {
126
            alert('Label and registered address are required');
127
            return;
128
        }
129
        if (!GSTIN.test(payload.gstin)) {
130
            alert('GSTIN is not in the format the GST portal issues');
131
            return;
132
        }
133
        postJson(context + '/createNewSeller', payload, function () {
134
            closeThen('#new-seller-modal', loadPage);
135
        });
136
    }
28300 tejbeer 137
 
37823 amit 138
    function createWarehouse() {
139
        var sellerId = currentSellerId();
140
        var payload = {
141
            sellerId: sellerId,
142
            name: value('#new-warehouse-name'),
143
            prefix: value('#new-warehouse-prefix').toUpperCase(),
144
            addressId: parseInt($('#new-warehouse-address').val(), 10)
145
        };
146
        if (!payload.name) {
147
            alert('Warehouse name is required');
148
            return;
149
        }
150
        if (!/^[A-Z]{2,7}$/.test(payload.prefix)) {
151
            alert('Branch code must be 2 to 7 letters, no digits or symbols');
152
            return;
153
        }
154
        if (!payload.addressId) {
155
            alert('Select an address');
156
            return;
157
        }
158
        if (!confirm('Create warehouse ' + payload.name + ' with branch code ' + payload.prefix + '?')) {
159
            return;
160
        }
161
        postJson(context + '/createNewWarehouse', payload, function () {
162
            closeThen('#new-warehouse-modal', function () {
163
                loadSeller(sellerId);
164
            });
165
        });
166
    }
28300 tejbeer 167
 
37823 amit 168
    function toggleWarehouse(button) {
169
        var warehouseId = button.data('warehouse-id');
170
        var activate = String(button.data('active')) !== 'true';
171
        if (!activate && !confirm('Deactivate warehouse ' + warehouseId + '? It stops being offered as an active'
172
            + ' physical warehouse. Stock, billing and history are not touched.')) {
173
            return;
174
        }
175
        var sellerId = currentSellerId();
176
        doPostAjaxRequestHandler(context + '/physicalWarehouse/' + warehouseId + '/active?active=' + activate,
177
            function () {
178
                loadSeller(sellerId);
179
            });
180
    }
28300 tejbeer 181
 
37823 amit 182
    function remapAddress() {
183
        var sellerId = currentSellerId();
184
        var warehouseId = $('#remap-address-modal').data('warehouse-id');
185
        var addressId = parseInt($('#remap-address').val(), 10);
186
        if (!addressId) {
187
            alert('Select an address');
188
            return;
189
        }
190
        if (!confirm('Change the address of warehouse ' + warehouseId + ' to #' + addressId
191
            + '? Its internal stores move to it as well.')) {
192
            return;
193
        }
194
        doPostAjaxRequestHandler(context + '/physicalWarehouse/' + warehouseId + '/address?addressId=' + addressId,
195
            function () {
196
                closeThen('#remap-address-modal', function () {
197
                    loadSeller(sellerId);
198
                });
199
            });
200
    }
28300 tejbeer 201
 
37823 amit 202
    function toggleAddress(button) {
203
        var addressId = button.data('address-id');
204
        var activate = String(button.data('active')) !== 'true';
205
        if (!activate && !confirm('Deactivate address #' + addressId + '? It will no longer be offered for new'
206
            + ' warehouses. Warehouses already on it keep printing it.')) {
207
            return;
208
        }
209
        doPostAjaxRequestHandler(context + '/warehouseAddress/' + addressId + '/active?active=' + activate, loadPage);
210
    }
28300 tejbeer 211
 
37823 amit 212
    return {
213
        loadPage: loadPage,
214
        loadSeller: loadSeller,
215
        renderAddressPreview: renderAddressPreview,
216
        renderGstinHint: renderGstinHint,
217
        createAddress: createAddress,
218
        createSeller: createSeller,
219
        createWarehouse: createWarehouse,
220
        toggleWarehouse: toggleWarehouse,
221
        remapAddress: remapAddress,
222
        toggleAddress: toggleAddress
223
    };
224
})();
28300 tejbeer 225
 
37823 amit 226
$(document).on('click', '.seller-index', function () {
227
    SellerSetup.loadPage();
28300 tejbeer 228
});
37823 amit 229
$(document).on('click', '.seller-setup .seller-open', function () {
230
    SellerSetup.loadSeller($(this).data('seller-id'));
28300 tejbeer 231
});
37823 amit 232
$(document).on('input change', '.seller-setup .address-preview-source', function () {
233
    SellerSetup.renderAddressPreview();
28300 tejbeer 234
});
37823 amit 235
$(document).on('input', '#new-seller-gstin', function () {
236
    SellerSetup.renderGstinHint();
237
});
238
$(document).on('click', '#new-address-submit', function () {
239
    SellerSetup.createAddress();
240
});
241
$(document).on('click', '#new-seller-submit', function () {
242
    SellerSetup.createSeller();
243
});
244
$(document).on('click', '#new-warehouse-submit', function () {
245
    SellerSetup.createWarehouse();
246
});
247
$(document).on('click', '.seller-setup .warehouse-active-toggle', function () {
248
    SellerSetup.toggleWarehouse($(this));
249
});
250
$(document).on('click', '.seller-setup .address-active-toggle', function () {
251
    SellerSetup.toggleAddress($(this));
252
});
253
$(document).on('click', '.seller-setup .warehouse-remap-open', function () {
254
    var button = $(this);
255
    $('#remap-warehouse-id').text(button.data('warehouse-id'));
256
    $('#remap-address').val(String(button.data('address-id') || ''));
257
    $('#remap-address-modal').data('warehouse-id', button.data('warehouse-id')).modal('show');
258
});
259
$(document).on('click', '#remap-address-submit', function () {
260
    SellerSetup.remapAddress();
261
});