Subversion Repositories SmartDukaan

Rev

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