/var/www/enzatesting.onesolution.hk/xls_stats.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
require_once "inc/phpexcel_1.7.7/PHPExcel.php";
set_time_limit(300);
ini_set('memory_limit''512M');

error_log("Données postées reçues : " print_r($_POSTtrue));

// Retrieve the data sent from JavaScript
$selectedData json_decode($_POST['selectedData'], true);
$totalData json_decode($_POST['totalData'], true);
$dataType $_POST['dataType'];
$detailsData json_decode($_POST['detailsData'], true);

$itemNumber $_POST['itemnoInput'];
$itemType $_POST['itemtypeInput'];
$custCode $_POST['customerInput'];


$objPHPExcel = new PHPExcel();

if (empty(
$custCode) && (empty($itemNumber) || $itemType != 'product')) {



    
$objPHPExcel->getActiveSheet()->setTitle("Global");

    
// Determine the headers to use based on the dataType value
    
$headers = array();
    
$dataColumns = array();

    
$headers = array(
        
"Customer Code",
        
"Customer Name",
        
"Total Quantity",
        
"Total Unit Cost",
        
"Total Unit Factory Cost",
        
"Total Unit Price",
    );
    
$dataColumns = array('A''B''C''D''E''F');


    
// Set the headers
    
$column 'A';
    foreach (
$headers as $header) {
        
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($column '1'$header);
        
$column++;
    }

    
// Define the column widths
    
$columnWidths = array(
        
'A' => 20// Customer Code
        
'B' => 20// Customer Name
        
'C' => 20// Total Quantity
        
'D' => 20// Total Unit Cost
        
'E' => 20// Total Unit Factory Cost
        
'F' => 20// Total Unit Price
    
);

    
// Set the column widths
    
foreach ($columnWidths as $column => $width) {
        
$objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth($width);
    }

    
// for the first sheet we have the global data of all customers
    
$rowNumber 2// start from the second row
    
foreach ($selectedData as $rowData) {

        
$objPHPExcel->setActiveSheetIndex(0)
            ->
setCellValue('A' $rowNumber$rowData['custcode'])
            ->
setCellValue('B' $rowNumber$rowData['custname'])
            ->
setCellValue('C' $rowNumberfloatval(str_replace(','''$rowData['total_qty'])))
            ->
setCellValue('D' $rowNumberfloatval(str_replace(','''$rowData['total_unitcost'])))
            ->
setCellValue('E' $rowNumberfloatval(str_replace(','''$rowData['total_unitfactcost'])))
            ->
setCellValue('F' $rowNumberfloatval(str_replace(','''$rowData['total_unitprice'])));

        
$objPHPExcel->getActiveSheet()->getStyle('C' $rowNumber)->getNumberFormat()->setFormatCode('#,##0.00');
        
$objPHPExcel->getActiveSheet()->getStyle('D' $rowNumber)->getNumberFormat()->setFormatCode('#,##0.00');
        
$objPHPExcel->getActiveSheet()->getStyle('E' $rowNumber)->getNumberFormat()->setFormatCode('#,##0.00');
        
$objPHPExcel->getActiveSheet()->getStyle('F' $rowNumber)->getNumberFormat()->setFormatCode('#,##0.00');

        
$rowNumber++;
    }

    
// for the other sheets we have the data of each customer total + details of all products selected by the user
    
foreach ($selectedData as $rowData) {
        
// Create a new sheet
        
$objPHPExcel->createSheet();
        
// Set the active sheet to the last sheet
        
$objPHPExcel->setActiveSheetIndex($objPHPExcel->getSheetCount() - 1);
        
// Set the title of the active sheet to the customer name
        
$objPHPExcel->getActiveSheet()->setTitle($rowData['custcode']);

        
// Define the headers for the active sheet
        
$headers = array(
            
"Item Number",
            
"Item Name",
            
"Total Quantity",
            
"Total Unit Cost",
            
"Total Unit Factory Cost",
            
"Total Unit Price",
        );

        
// Set the headers
        
$column 'A';
        foreach (
$headers as $header) {
            
$objPHPExcel->getActiveSheet()->setCellValue($column '1'$header);
            
$column++;
        }



        
// Define the column widths
        
$columnWidths = array(
            
'A' => 20// Customer Code
            
'B' => 20// Customer Name
            
'C' => 20// Total Quantity
            
'D' => 20// Total Unit Cost
            
'E' => 20// Total Unit Factory Cost
            
'F' => 20// Total Unit Price
        
);

        
// Set the column widths
        
foreach ($columnWidths as $column => $width) {
            
$objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth($width);
        }

        
// Add the headers to the active sheet
        
$column 'A';
        foreach (
$headers as $header) {
            
$objPHPExcel->getActiveSheet()->setCellValue($column '1'$header);
            
$column++;
        }

        
// Get the details data for this customer
        
$detailsDataForThisCustomer = isset($detailsData[$rowData['custcode']]) ? $detailsData[$rowData['custcode']] : array();

        
// Add the details data to the active sheet
        
$row 2// Start from the second row
        
if (is_array($detailsDataForThisCustomer)) {
            foreach (
$detailsDataForThisCustomer as $detail) {
                
$objPHPExcel->getActiveSheet()
                    ->
setCellValue('A' $row$detail['itemno'])
                    ->
setCellValue('B' $row$detail['name_en'])
                    ->
setCellValue('C' $row$detail['total_qty'])
                    ->
setCellValue('D' $row$detail['total_unitcost'])
                    ->
setCellValue('E' $row$detail['total_unitfactcost'])
                    ->
setCellValue('F' $row$detail['total_unitprice']);

                
// Set the number format for the data
                
$objPHPExcel->getActiveSheet()->getStyle('C' $row)->getNumberFormat()->setFormatCode('#,##0.00');
                
$objPHPExcel->getActiveSheet()->getStyle('D' $row)->getNumberFormat()->setFormatCode('#,##0.00');
                
$objPHPExcel->getActiveSheet()->getStyle('E' $row)->getNumberFormat()->setFormatCode('#,##0.00');
                
$objPHPExcel->getActiveSheet()->getStyle('F' $row)->getNumberFormat()->setFormatCode('#,##0.00');
                
$row++;
            }
        }
    }
} else {

    
$objPHPExcel->getActiveSheet()->setTitle("Global");

    
// Determine the headers to use based on the dataType value
    
$headers = array();
    
$dataColumns = array();

    
$headers = array(
        
"Customer Code",
        
"Item Number - Item Name",
        
"Total Quantity",
        
"Total Unit Cost",
        
"Total Unit Factory Cost",
        
"Total Unit Price",
    );
    
$dataColumns = array('A''B''C''D''E''F');


    
// Set the headers
    
$column 'A';
    foreach (
$headers as $header) {
        
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($column '1'$header);
        
$column++;
    }

    
// Define the column widths
    
$columnWidths = array(
        
'A' => 20// Customer Code
        
'B' => 50// Customer Name
        
'C' => 20// Total Quantity
        
'D' => 20// Total Unit Cost
        
'E' => 20// Total Unit Factory Cost
        
'F' => 20// Total Unit Price
    
);

    
// Set the column widths
    
foreach ($columnWidths as $column => $width) {
        
$objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth($width);
    }

    
// for the first sheet we have the global data of all customers
    
$rowNumber 2// start from the second row
    
foreach ($selectedData as $rowData) {

        
$objPHPExcel->setActiveSheetIndex(0)
            ->
setCellValue('A' $rowNumber$rowData['custcode'])
            ->
setCellValue('B' $rowNumber$rowData['custname'])
            ->
setCellValue('C' $rowNumberfloatval(str_replace(','''$rowData['total_qty'])))
            ->
setCellValue('D' $rowNumberfloatval(str_replace(','''$rowData['total_unitcost'])))
            ->
setCellValue('E' $rowNumberfloatval(str_replace(','''$rowData['total_unitfactcost'])))
            ->
setCellValue('F' $rowNumberfloatval(str_replace(','''$rowData['total_unitprice'])));

        
$objPHPExcel->getActiveSheet()->getStyle('C' $rowNumber)->getNumberFormat()->setFormatCode('#,##0.00');
        
$objPHPExcel->getActiveSheet()->getStyle('D' $rowNumber)->getNumberFormat()->setFormatCode('#,##0.00');
        
$objPHPExcel->getActiveSheet()->getStyle('E' $rowNumber)->getNumberFormat()->setFormatCode('#,##0.00');
        
$objPHPExcel->getActiveSheet()->getStyle('F' $rowNumber)->getNumberFormat()->setFormatCode('#,##0.00');

        
$rowNumber++;
    }
}


// Pour chaque feuille de calcul

foreach ($objPHPExcel->getAllSheets() as $sheet) {
    
// Obtenez le numéro de la dernière ligne de la feuille de calcul
    
$lastRow $sheet->getHighestRow();

    
// Ajoutez une nouvelle ligne pour le total
    
$totalRow $lastRow 1;

    
// Vérifiez s'il y a plus d'une ligne (en excluant l'en-tête)
    
if ($lastRow 2) {
        
// Définissez les valeurs pour la ligne "Total"
        
$sheet->setCellValue('A' $totalRow'Total');
        
$sheet->setCellValue('C' $totalRow'=SUM(C2:C' $lastRow ')');
        
$sheet->setCellValue('D' $totalRow'=SUM(D2:D' $lastRow ')');
        
$sheet->setCellValue('E' $totalRow'=SUM(E2:E' $lastRow ')');
        
$sheet->setCellValue('F' $totalRow'=SUM(F2:F' $lastRow ')');
    } else if (
$lastRow == 2) {
        
// Si une seule ligne de données, le total est la valeur de cette ligne
        
$sheet->setCellValue('A' $totalRow'Total');
        
$sheet->setCellValue('C' $totalRow'=C2');
        
$sheet->setCellValue('D' $totalRow'=D2');
        
$sheet->setCellValue('E' $totalRow'=E2');
        
$sheet->setCellValue('F' $totalRow'=F2');
    }

    
// Appliquez le format de nombre à la ligne "Total"
    
$sheet->getStyle('C' $totalRow)->getNumberFormat()->setFormatCode('#,##0.00');
    
$sheet->getStyle('D' $totalRow)->getNumberFormat()->setFormatCode('#,##0.00');
    
$sheet->getStyle('E' $totalRow)->getNumberFormat()->setFormatCode('#,##0.00');
    
$sheet->getStyle('F' $totalRow)->getNumberFormat()->setFormatCode('#,##0.00');
}


// // Add the "Total" row to the sheet.
// $totalRow = array(
//     "Total",
//     "",
//     $totalData['totalqty'],
//     $totalData['totalcost'],
//     $totalData['totalfcost'],
//     $totalData['totalprice'],
// );



// $column = 'A';
// foreach ($totalRow as $cellValue) {
//     if (in_array($column, $dataColumns)) {
//         $objPHPExcel->setActiveSheetIndex(0)->setCellValue($column . $rowNumber, $cellValue);
//         // If the column is one of the numeric columns, set the number format.
//         if (is_numeric($cellValue)) {
//             $objPHPExcel->getActiveSheet()->getStyle($column . $rowNumber)->getNumberFormat()->setFormatCode('#,##0.00');
//         }
//         if ($column === 'G') {
//             $objPHPExcel->getActiveSheet()->getStyle($column . $rowNumber)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00);
//         }
//     }
//     $column++;
// }

// Determine the filename based on the condition value
$fileNamePrefix 'Global';
$fileNameSuffix '';

if (!empty(
$custCode)) {
    
$fileNamePrefix $custCode;
}

if (!empty(
$itemNumber)) {
    
$fileNamePrefix $itemNumber;
}

if (!empty(
$itemType)) {
    
$fileNameSuffix '-' $itemType;
}

// Add "stats" to the end of the filename
$fileNameSuffix .= '-stats';

// Set the headers for the Excel file
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' $fileNamePrefix $fileNameSuffix '.xls"');
header('Cache-Control: max-age=0');

$objPHPExcel->setActiveSheetIndex(0);
$objWriter PHPExcel_IOFactory::createWriter($objPHPExcel'Excel5');
$objWriter->save('php://output');

// Send a response back to JavaScript
echo json_encode(array('success' => true'message' => 'File has been saved.'));