Subversion Repositories SmartDukaan

Rev

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