Subversion Repositories SmartDukaan

Rev

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