Subversion Repositories SmartDukaan

Rev

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