Subversion Repositories SmartDukaan

Rev

Rev 34009 | Rev 34230 | 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) {
168
        createCatalogPurchaseSaleBarChart(response, startDate, endDate, supplierId);
33990 tejus.loha 169
    });
170
}
171
 
34018 tejus.loha 172
function getCatalogAgeDetail(startDate, endDate, supplierId, label) {
173
    // /catalog-age-detail
174
    let modelNumber = label;
175
    doGetAjaxRequestHandler(`${context}/pur-sale-ratio/catalog-age-detail?startDate=${startDate}&endDate=${endDate}&modelNumber=${modelNumber}&supplierId=${supplierId}`, function (response) {
176
        $('#catalog-age-div #catalogModalBody').html(response);
177
        $('#catalog-age-div').modal('show');
178
    });
179
}
180
 
181
function createCatalogPurchaseSaleBarChart(response, startDate, endDate, supplierId) {
33990 tejus.loha 182
    let labels = [], totalQuantities = [], soldQuantities = [];
183
    let filterResponse = response.filter(res => res.sale !== res.purchase);
184
    labels = filterResponse.map(sale => sale.model);
185
    totalQuantities = filterResponse.map(sale => sale.purchase);
186
    soldQuantities = filterResponse.map(sale => sale.sale);
187
 
188
    let charContainer = document.getElementById('chartContainer');
189
    //destroy old Catalog Chart Container
190
    $('#catalogChartContainer').remove();
191
    //create new Catalog Chart Container
192
    let newCatalogChartContainer = document.createElement('div');
193
    newCatalogChartContainer.id = 'catalogChartContainer';
194
    newCatalogChartContainer.classList.add('col-md-6');
195
 
196
    let canvas = document.createElement('canvas');
197
 
198
    let catalogChartHeight;
199
    switch (labels.length) {
200
        case 1:
201
            catalogChartHeight = 150;
202
            break;
203
        case 2:
204
            catalogChartHeight = 200;
205
            break;
206
        case 3:
207
            catalogChartHeight = 250;
208
            break;
209
        case 4:
210
            catalogChartHeight = 250;
211
            break;
212
        default:
213
            catalogChartHeight = labels.length * 2 * 25;
214
            break;
215
    }
216
    newCatalogChartContainer.style.height = catalogChartHeight + 'px';
217
    const ctx = canvas.getContext('2d');
218
 
219
    //destroy catalogChart data if available
220
    if (catalogChart) {
221
        catalogChart.destroy();
222
    }
223
 
224
    const percentageData = soldQuantities.map((sale, i) => ((sale / totalQuantities[i]) * 100).toFixed(1));
225
 
226
    // Chart.js configuration
227
    //Chart.register(ChartDataLabels);
228
    catalogChart = new Chart(ctx, {
229
        type: 'bar',
230
        data: {
231
            labels: labels, // Labels for each bar
232
            datasets: [
233
                {
234
                    label: 'Purchase',
235
                    data: totalQuantities,
236
                    backgroundColor: 'rgba(255, 99, 132, 0.6)', // Red bars
237
                    borderColor: 'rgba(255, 99, 132, 1)',
238
                    borderWidth: 1
239
 
240
                },
241
                {
242
                    label: 'Sale',
243
                    data: soldQuantities,
244
                    backgroundColor: 'rgba(144, 238, 144, 0.6)', // Green bars
245
                    borderColor: 'rgba(144, 238, 144, 1)',
246
                    borderWidth: 1,
247
 
248
 
249
                }
250
            ]
251
        },
252
        options: {
253
            responsive: true,
254
            maintainAspectRatio: false,
255
            plugins: {
256
                tooltip: {
257
                    enabled: false,
258
                },
259
                legend: {
260
                    position: 'top',
261
                },
262
                datalabels: {
263
                    display: true,
264
                    color: 'black',
265
                    font: {
266
                        size: 12,
267
                    },
268
                    formatter: function (value, context) {
269
                        const index = context.dataIndex;
270
                        const purchase = totalQuantities[index];
271
                        const sale = soldQuantities[index];
272
                        const percentage = percentageData[index];
273
 
274
                        if (context.dataset.label === 'Purchase') {
275
                            return `${purchase}`;
276
                        } else {
277
                            return `${sale} (${percentage}%)`;
278
                        }
279
 
280
                    },
281
                    align: 'end', // Place labels at the center of the bars
282
                    anchor: 'center', // Align labels to the center of each bar pair
283
                    offset: 5, // Adjust positioning for better visibility if needed
284
                },
34018 tejus.loha 285
            }, onClick: function (event, activeElements) {
286
                if (activeElements.length > 0) {
287
                    const element = activeElements[0]; // Get the clicked element
288
                    const datasetIndex = element.datasetIndex; // Get the dataset (Sale or Purchase)
289
                    const dataIndex = element.index; // Get the index of the clicked bar
290
                    const label = this.data.labels[dataIndex]; // Get the label for the clicked bar
291
                    const value = this.data.datasets[datasetIndex].data[dataIndex]; // Get the value of the clicked bar
292
                    getCatalogAgeDetail(startDate, endDate, supplierId, label);
293
                }
33990 tejus.loha 294
            },
295
            indexAxis: 'y',
296
            scales: {
297
                x: {
298
                    beginAtZero: true,
299
                    title: {
300
                        display: true,
301
                        text: 'Quantity',
302
                        color: '#000000'
303
                    }
304
                },
305
                y: {
306
                    title: {
307
                        display: true,
308
                        text: 'Models',
309
                        color: '#000000'
310
                    }
311
                }
312
            }
313
        }
314
    });
315
    newCatalogChartContainer.appendChild(canvas);
316
    charContainer.appendChild(newCatalogChartContainer);
317
 
318
}
319
 
320
function arrangeLabelsOrder(responseData) {
321
    const requiredOrder = ['HID', 'FASTMOVING', 'SLOWMOVING', 'OTHER'];
322
    const orderMap = {};
323
    requiredOrder.forEach((label, index) => {
324
        orderMap[label] = index;
325
    });
326
    const sortedResponse = responseData.sort((a, b) => {
327
        const indexA = orderMap[a.status] !== undefined ? orderMap[a.status] : Infinity;
328
        const indexB = orderMap[b.status] !== undefined ? orderMap[b.status] : Infinity;
329
        return indexA - indexB;
330
    });
331
    return sortedResponse;
332
}