Subversion Repositories SmartDukaan

Rev

Rev 36215 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
33990 tejus.loha 1
$(document).on('click', '.mk-pur-sale-ratio-panel', function () {
34018 tejus.loha 2
    doGetAjaxRequestHandler(`${context}/pur-sale-ratio/panel`, function (response) {
33990 tejus.loha 3
        $('#main-content').html(response);
4
    });
5
});
6
 
34427 tejus.loha 7
$(document).on('change', '#purSaleStartDate, #purSaleEndDate, #purSaleBrand', function () {
34009 tejus.loha 8
    console.log("change clicked...");
33990 tejus.loha 9
    let startDate = $('input[name="startDate"]').val();
10
    let endDate = $('input[name="endDate"]').val();
34009 tejus.loha 11
    let brand = $('select[name="brand"]').val();
12
    let supplierId = $('select[name="supplier"]').val();
13
 
14
    if (!startDate) {
15
        alert("Please select start date");
16
        return;
17
    }
18
    if (!endDate) {
19
        alert("Please select end date");
20
        return;
21
    }
22
    if (!brand) {
23
        alert("Please select brand");
24
        return;
25
    }
26
 
27
    if (!supplierId) {
28
        supplierId = 0;
29
    }
36215 vikas 30
 
34018 tejus.loha 31
    doGetAjaxRequestHandler(`${context}/pur-sale-ratio/brandSuppliers?startDate=${startDate}&endDate=${endDate}&brand=${brand}&supplierId=${supplierId}`, function (response) {
34009 tejus.loha 32
        if (response.hasOwnProperty('purchaseSaleQuantities') && response.purchaseSaleQuantities.length) {
33
            createPurchaseSaleBarChart(response.purchaseSaleQuantities, startDate, endDate, brand, supplierId);
36215 vikas 34
        } else {
35
            if (salesChart) salesChart.destroy();
36
            if (catalogChart) catalogChart.destroy();
34009 tejus.loha 37
        }
36215 vikas 38
        console.log('Purchase sale ratio data fetched successfully',response);
33990 tejus.loha 39
        if (response.hasOwnProperty('brandWiseSuppliers') && response.brandWiseSuppliers.length) {
40
            $('.suppliersList').empty();
41
            $('.suppliersList').append(`<option selected disabled>Select Supplier</option>`);
34240 tejus.loha 42
            $('.suppliersList').append(`<option value="0">All Supplier</option>`);
33990 tejus.loha 43
            response.brandWiseSuppliers.forEach(function (supplier) {
44
                $('.suppliersList').append(`<option value="${supplier.supplierId}">${supplier.supplierName}</option>`);
45
            });
46
        }
47
    });
48
});
49
 
34427 tejus.loha 50
$(document).on('change', '#purSaleSupplier', function () {
33990 tejus.loha 51
    let startDate = $('input[name="startDate"]').val();
52
    let endDate = $('input[name="endDate"]').val();
53
    let brand = $('select[name="brand"]').val();
54
    let supplierId = $(this).val();
34018 tejus.loha 55
    doGetAjaxRequestHandler(`${context}/pur-sale-ratio/data?startDate=${startDate}&endDate=${endDate}&brand=${brand}&supplierId=${supplierId}`, function (response) {
33990 tejus.loha 56
        if (response.length) {
34009 tejus.loha 57
            createPurchaseSaleBarChart(response, startDate, endDate, brand, supplierId);
33990 tejus.loha 58
        }
59
    });
60
});
61
 
62
let salesChart, catalogChart;
36215 vikas 63
let currentPurchaseSaleData = [];
64
let currentCatalogData = [];
33990 tejus.loha 65
 
66
Chart.register(ChartDataLabels);
67
 
36215 vikas 68
$(document).on('click', '#exportCsvBtn', function () {
69
    exportPurchaseSaleCSV();
70
});
71
 
72
function exportPurchaseSaleCSV() {
36217 vikas 73
    if (!currentPurchaseSaleData.length) {
36215 vikas 74
        alert('No data to export');
75
        return;
76
    }
77
 
36217 vikas 78
    let startDate = $('input[name="startDate"]').val();
79
    let endDate = $('input[name="endDate"]').val();
80
    let brand = $('select[name="brand"]').val() || '';
81
    let supplierId = $('select[name="supplier"]').val() || 0;
36215 vikas 82
 
36217 vikas 83
    doGetAjaxRequestHandler(`${context}/pur-sale-ratio/catalog-export?startDate=${startDate}&endDate=${endDate}&brand=${brand}&supplierId=${supplierId}`, function (response) {
84
        let csvContent = 'Model,Status,Purchase,Sale,Sale %,Unsold (0-5 Days),Unsold (6-15 Days),Unsold (16-30 Days),Unsold (31-45 Days),Unsold (>45 Days)\n';
85
        response.forEach(row => {
36215 vikas 86
            let pct = row.purchase > 0 ? ((row.sale / row.purchase) * 100).toFixed(1) : '0.0';
36217 vikas 87
            csvContent += `${row.model},${row.status},${row.purchase},${row.sale},${pct}%,${row.unsold5},${row.unsold15},${row.unsold30},${row.unsold45},${row.unsoldAbove45}\n`;
36215 vikas 88
        });
89
 
36217 vikas 90
        let fileName = `purchase_sale_ratio_${brand}_${startDate}_to_${endDate}.csv`;
91
        let blob = new Blob([csvContent], {type: 'text/csv;charset=utf-8;'});
92
        let link = document.createElement('a');
93
        link.href = URL.createObjectURL(blob);
94
        link.download = fileName;
95
        link.click();
96
        URL.revokeObjectURL(link.href);
97
    });
36215 vikas 98
}
99
 
33990 tejus.loha 100
function createPurchaseSaleBarChart(response, startDate, endDate, brand, supplierId) {
101
    response = arrangeLabelsOrder(response);
36215 vikas 102
    currentPurchaseSaleData = response;
103
    currentCatalogData = [];
36217 vikas 104
    $('#exportCsvBtn').show();
33990 tejus.loha 105
    let labels = [], totalQuantities = [], soldQuantities = [];
106
    labels = response.map(sale => sale.status);
107
    totalQuantities = response.map(sale => sale.totalQuantity);
108
    soldQuantities = response.map(sale => sale.soldQuantity);
109
    const ctx = document.getElementById('salesChart').getContext('2d');
110
 
111
    //destroy salesChart data if available
112
    if (salesChart) {
113
        salesChart.destroy();
114
    }
115
    if (catalogChart) {
116
        catalogChart.destroy();
117
    }
118
    const percentageData = soldQuantities.map((sale, i) => ((sale / totalQuantities[i]) * 100).toFixed(1));
119
    // Chart.js configuration
120
    salesChart = new Chart(ctx, {
121
        type: 'bar',
122
        data: {
123
            labels: labels, // Labels for each bar
124
            datasets: [
125
                {
126
                    label: 'Purchase',
127
                    data: totalQuantities,
128
                    backgroundColor: 'rgba(255, 99, 132, 0.6)', // Red bars
129
                    borderColor: 'rgba(255, 99, 132, 1)',
130
                    borderWidth: 1
131
                },
132
                {
133
                    label: 'Sale',
134
                    data: soldQuantities,
135
                    backgroundColor: 'rgba(144, 238, 144, 0.6)', // Green bars
136
                    borderColor: 'rgba(144, 238, 144, 1)',
137
                    borderWidth: 1
138
                }
139
            ]
140
        },
141
        options: {
142
            responsive: true,
143
            plugins: {
144
                tooltip: {
145
                    enabled: false,
146
                },
147
                legend: {
148
                    position: 'top',
149
                },
150
                datalabels: {
151
                    display: true,
152
                    color: 'black',
153
                    font: {
154
                        size: 12,
155
                    },
156
                    formatter: function (value, context) {
157
                        const index = context.dataIndex;
158
                        const purchase = totalQuantities[index];
159
                        const sale = soldQuantities[index];
160
                        const percentage = percentageData[index];
161
 
162
                        if (context.dataset.label === 'Purchase') {
163
                            return `${purchase}`;
164
                        } else {
165
                            return `${sale} (${percentage}%)`;
166
                        }
167
 
168
                    },
169
                    align: 'end', // Place labels at the center of the bars
170
                    anchor: 'center', // Align labels to the center of each bar pair
171
                    offset: 5, // Adjust positioning for better visibility if needed
172
                },
173
            }, onClick: function (event, activeElements) {
174
                if (activeElements.length > 0) {
175
                    const element = activeElements[0]; // Get the clicked element
176
                    const datasetIndex = element.datasetIndex; // Get the dataset (Sale or Purchase)
177
                    const dataIndex = element.index; // Get the index of the clicked bar
178
                    const label = this.data.labels[dataIndex]; // Get the label for the clicked bar
179
                    const value = this.data.datasets[datasetIndex].data[dataIndex]; // Get the value of the clicked bar
180
                    // Show an alert with the label and value
181
                    catalogPurchaseSaleChart(startDate, endDate, brand, supplierId, label);
182
                }
183
            },
184
            indexAxis: 'y',
185
            scales: {
186
                x: {
187
                    beginAtZero: true,
188
                    title: {
189
                        display: true,
190
                        text: 'Quantity',
191
                        color: '#000000'
192
                    }
193
                },
194
                y: {
195
                    title: {
196
                        display: true,
197
                        text: 'Status',
198
                        color: '#000000'
199
                    }
200
                }
201
            }
202
        }
203
    });
204
 
205
}
206
 
207
function catalogPurchaseSaleChart(startDate, endDate, brand, supplierId, label) {
34018 tejus.loha 208
    doGetAjaxRequestHandler(`${context}/pur-sale-ratio/catalog-quantity?startDate=${startDate}&endDate=${endDate}&brand=${brand}&supplierId=${supplierId}&label=${label}`, function (response) {
34230 tejus.loha 209
        console.log("response to check status - ",response);
34018 tejus.loha 210
        createCatalogPurchaseSaleBarChart(response, startDate, endDate, supplierId);
33990 tejus.loha 211
    });
212
}
213
 
34230 tejus.loha 214
function getCatalogAgeDetail(startDate, endDate, supplierId, label,status) {
34018 tejus.loha 215
    // /catalog-age-detail
216
    let modelNumber = label;
34230 tejus.loha 217
    doGetAjaxRequestHandler(`${context}/pur-sale-ratio/catalog-age-detail?startDate=${startDate}&endDate=${endDate}&modelNumber=${modelNumber}&supplierId=${supplierId}&status=${status}`, function (response) {
34018 tejus.loha 218
        $('#catalog-age-div #catalogModalBody').html(response);
219
        $('#catalog-age-div').modal('show');
220
    });
221
}
222
 
223
function createCatalogPurchaseSaleBarChart(response, startDate, endDate, supplierId) {
33990 tejus.loha 224
    let labels = [], totalQuantities = [], soldQuantities = [];
34230 tejus.loha 225
 
33990 tejus.loha 226
    let filterResponse = response.filter(res => res.sale !== res.purchase);
36215 vikas 227
    currentCatalogData = filterResponse;
34230 tejus.loha 228
 
33990 tejus.loha 229
    labels = filterResponse.map(sale => sale.model);
230
    totalQuantities = filterResponse.map(sale => sale.purchase);
231
    soldQuantities = filterResponse.map(sale => sale.sale);
34230 tejus.loha 232
    let status = filterResponse.map(sale => sale.status)[0];
233
    let charContainer = document.getElementById('chartContainer');
33990 tejus.loha 234
 
235
    //destroy old Catalog Chart Container
236
    $('#catalogChartContainer').remove();
34230 tejus.loha 237
 
33990 tejus.loha 238
    //create new Catalog Chart Container
239
    let newCatalogChartContainer = document.createElement('div');
240
    newCatalogChartContainer.id = 'catalogChartContainer';
241
    newCatalogChartContainer.classList.add('col-md-6');
242
 
243
    let canvas = document.createElement('canvas');
244
 
245
    let catalogChartHeight;
246
    switch (labels.length) {
247
        case 1:
248
            catalogChartHeight = 150;
249
            break;
250
        case 2:
251
            catalogChartHeight = 200;
252
            break;
253
        case 3:
254
            catalogChartHeight = 250;
255
            break;
256
        case 4:
257
            catalogChartHeight = 250;
258
            break;
259
        default:
260
            catalogChartHeight = labels.length * 2 * 25;
261
            break;
262
    }
263
    newCatalogChartContainer.style.height = catalogChartHeight + 'px';
264
    const ctx = canvas.getContext('2d');
265
 
266
    //destroy catalogChart data if available
267
    if (catalogChart) {
268
        catalogChart.destroy();
269
    }
270
 
271
    const percentageData = soldQuantities.map((sale, i) => ((sale / totalQuantities[i]) * 100).toFixed(1));
272
 
273
    // Chart.js configuration
274
    //Chart.register(ChartDataLabels);
275
    catalogChart = new Chart(ctx, {
276
        type: 'bar',
277
        data: {
278
            labels: labels, // Labels for each bar
279
            datasets: [
280
                {
281
                    label: 'Purchase',
282
                    data: totalQuantities,
283
                    backgroundColor: 'rgba(255, 99, 132, 0.6)', // Red bars
284
                    borderColor: 'rgba(255, 99, 132, 1)',
285
                    borderWidth: 1
286
 
287
                },
288
                {
289
                    label: 'Sale',
290
                    data: soldQuantities,
291
                    backgroundColor: 'rgba(144, 238, 144, 0.6)', // Green bars
292
                    borderColor: 'rgba(144, 238, 144, 1)',
293
                    borderWidth: 1,
294
 
295
 
296
                }
297
            ]
298
        },
299
        options: {
300
            responsive: true,
301
            maintainAspectRatio: false,
302
            plugins: {
303
                tooltip: {
304
                    enabled: false,
305
                },
306
                legend: {
307
                    position: 'top',
308
                },
309
                datalabels: {
310
                    display: true,
311
                    color: 'black',
312
                    font: {
313
                        size: 12,
314
                    },
315
                    formatter: function (value, context) {
316
                        const index = context.dataIndex;
317
                        const purchase = totalQuantities[index];
318
                        const sale = soldQuantities[index];
319
                        const percentage = percentageData[index];
320
 
321
                        if (context.dataset.label === 'Purchase') {
322
                            return `${purchase}`;
323
                        } else {
324
                            return `${sale} (${percentage}%)`;
325
                        }
326
 
327
                    },
328
                    align: 'end', // Place labels at the center of the bars
329
                    anchor: 'center', // Align labels to the center of each bar pair
330
                    offset: 5, // Adjust positioning for better visibility if needed
331
                },
34018 tejus.loha 332
            }, onClick: function (event, activeElements) {
333
                if (activeElements.length > 0) {
334
                    const element = activeElements[0]; // Get the clicked element
335
                    const datasetIndex = element.datasetIndex; // Get the dataset (Sale or Purchase)
336
                    const dataIndex = element.index; // Get the index of the clicked bar
34230 tejus.loha 337
                    const modelNumber = this.data.labels[dataIndex]; // Get the label for the clicked bar that is modelNumber this time
34018 tejus.loha 338
                    const value = this.data.datasets[datasetIndex].data[dataIndex]; // Get the value of the clicked bar
34230 tejus.loha 339
                    getCatalogAgeDetail(startDate, endDate, supplierId, modelNumber,status);
34018 tejus.loha 340
                }
33990 tejus.loha 341
            },
342
            indexAxis: 'y',
343
            scales: {
344
                x: {
345
                    beginAtZero: true,
346
                    title: {
347
                        display: true,
348
                        text: 'Quantity',
349
                        color: '#000000'
350
                    }
351
                },
352
                y: {
353
                    title: {
354
                        display: true,
355
                        text: 'Models',
356
                        color: '#000000'
357
                    }
358
                }
359
            }
360
        }
361
    });
362
    newCatalogChartContainer.appendChild(canvas);
363
    charContainer.appendChild(newCatalogChartContainer);
364
 
365
}
366
 
367
function arrangeLabelsOrder(responseData) {
368
    const requiredOrder = ['HID', 'FASTMOVING', 'SLOWMOVING', 'OTHER'];
369
    const orderMap = {};
370
    requiredOrder.forEach((label, index) => {
371
        orderMap[label] = index;
372
    });
373
    const sortedResponse = responseData.sort((a, b) => {
374
        const indexA = orderMap[a.status] !== undefined ? orderMap[a.status] : Infinity;
375
        const indexB = orderMap[b.status] !== undefined ? orderMap[b.status] : Infinity;
376
        return indexA - indexB;
377
    });
378
    return sortedResponse;
379
}