/var/www/enzatesting.onesolution.hk/xls_inv_stocktake_report.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
<?php 
error_reporting
(E_ALL E_NOTICE E_WARNING);
ini_set('display_errors'1);

@
set_time_limit(3600);
ini_set('max_execution_time''3600'); 

ini_set('post_max_size''10M');
ini_set('memory_limit','1024M');    
ignore_user_abort(false);

//-------------------Try to lock the file to ensure only one instance is running---------------
// $fp = fopen('./tmp/inv_stocktake_report.lock', 'w+');
// if (!flock($fp, LOCK_EX | LOCK_NB)) {
//     exit('Someone is generating the report.');
// }
//-------------------Try to lock the file to ensure only one instance is running---------------

$start_time MICROTIME(TRUE);

$formid "Inventory";
require_once 
"inc/configure.php";
require_once 
"inc/phpexcel_1.7.7/PHPExcel.php";    

if(
havePermission("STr")==false){
    
myerror(INVALID.WS.PERMISSION);    
}

function 
excel_writeRow($objWorksheet, &$productrow, Array $data, Array $settings=array()){
    
// vdump($productrow, $data);
    // var_dump($data['itemno']);    
    
$data['lastQty'] = numf(inv_getSafeQty($settings['companyid'], $data['itemtype'], $data['refid'], $settings['startdate']));
    
$data['lastWt'] = numf(inv_getSafeWt($settings['companyid'], $data['itemtype'], $data['refid'], $settings['startdate']), 3);
    
$data['currQty'] = numf(inv_getSafeQty($settings['companyid'], $data['itemtype'], $data['refid'], $settings['finishdate']));
    
$data['currWt'] = numf(inv_getSafeWt($settings['companyid'], $data['itemtype'], $data['refid'], $settings['finishdate']), 3);
    
$data['gapQty'] = numf($data['currQty'] - $data['lastQty']);
    
$data['gapWt'] = numf($data['currWt'] - $data['lastWt'], 3);

    if(empty(
$data['gapQty']) && empty($data['gapWt'])){ return; }
    
    
$objWorksheet->insertNewRowBefore($productrow+11);
    
$i 0;
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['itemtype'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['itemno'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['name_en'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['name_sc'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['lastQty'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['uom_inventory'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['lastWt'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['uom_wt'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['currQty'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['currWt'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['gapQty'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow$data['gapWt'] );
    
$objWorksheet->setCellValueByColumnAndRow($i++, $productrow'' );
    
$productrow++;
}

function 
csv_createworksheet(){
    return 
fopen('php://output''w');
}

function 
csv_header($filename){
    
header('Content-Type: application/text/csv');
    
header('Content-Disposition: attachment;filename="'.$filename);
    
header('Cache-Control: max-age=0');
}

function 
csv_writeRow($objWorksheet, &$productrow, Array $data, Array $settings=array()){
    
// vdump($productrow, $data);
    // var_dump($data['itemno']);    
    
$data['lastQty'] = numf(inv_getSafeQty($settings['companyid'], $data['itemtype'], $data['refid'], $settings['startdate']));
    
$data['lastWt'] = numf(inv_getSafeWt($settings['companyid'], $data['itemtype'], $data['refid'], $settings['startdate']), 3);
    
$data['currQty'] = numf(inv_getSafeQty($settings['companyid'], $data['itemtype'], $data['refid'], $settings['finishdate']));
    
$data['currWt'] = numf(inv_getSafeWt($settings['companyid'], $data['itemtype'], $data['refid'], $settings['finishdate']), 3);
    
$data['gapQty'] = numf($data['currQty'] - $data['lastQty']);
    
$data['gapWt'] = numf($data['currWt'] - $data['lastWt'], 3);

    if(empty(
$data['gapQty']) && empty($data['gapWt'])){ return; }
    
    
fputcsv($objWorksheet, array( 
                    
$data['itemtype'], $data['itemno'], $data['name_en'],    $data['name_sc'],
                    
$data['lastQty'], $data['uom_inventory'], $data['lastWt'], $data['uom_wt'],
                    
$data['currQty'], $data['currWt'], $data['gapQty'], $data['gapWt'])
    ); 
}

/*--------------------------------END OF FUNCTIONS--------------------------------------*/

$refid = (int)setDefaultReqVar("refid"'');
$companyid setDefaultReqVar("companyid"$system_var['COMAPNY_FACTORY']);
$bomcategy setDefaultReqVar("bomcategy"'NA');

if(
$refid){
    
$hdr getDB('inv_stocktake'$refid);
} else {
    exit;
}

$objPHPexcel PHPExcel_IOFactory::load('templates/inv_stocktake_report.xls');    
$objWorksheet $objPHPexcel->getSheetByName('STGR');
/*
#title             C1
#printdate        M1
*/
$objWorksheet->getCell('C1')->setValue'Stock take gap report (' .datef($hdr['createdate'],10) . ')' );
$objWorksheet->getCell('M1')->setValue'Printed: ' date('Y-m-d H:i:s') );

$excel_row=4;

if(
$bomcategy=='stone'){
    
$sql "SELECT
        'Stone' as itemtype,
        dbo.inv_stone.refid,
        dbo.inv_stone.itemno,
        dbo.inv_stone.name_en,
        dbo.inv_stone.name_sc,
        dbo.inv_stone.uom_inventory,
        dbo.inv_stone.uom_wt
        FROM
        dbo.inv_stone
        WHERE verified='1'
        ORDER BY itemno"
;        
}elseif(
$bomcategy=='diamond'){
    
$sql "SELECT 
        'Diamond' as itemtype,
        dbo.inv_diamond.refid,
        dbo.inv_diamond.itemno,
        dbo.inv_diamond.name_en,
        dbo.inv_diamond.name_sc,
        dbo.inv_diamond.uom_inventory,
        dbo.inv_diamond.uom_wt
        FROM
        dbo.inv_diamond
        WHERE verified='1'
        -- AND itemno like 'H1WTRD%'
        ORDER BY itemno"
;        
}elseif(
$bomcategy=='accessory'){
    
$sql "SELECT 
        'Accessory' as itemtype,
        dbo.inv_accessory.refid,
        dbo.inv_accessory.itemno,
        dbo.inv_accessory.name_en,
        dbo.inv_accessory.name_sc,
        dbo.inv_accessory.uom_inventory,
        dbo.inv_accessory.uom_wt
        FROM
        dbo.inv_accessory
        WHERE verified='1'
        ORDER BY itemno"
;        
}elseif(
$bomcategy=='all'){
    
$sql "SELECT 
            'Stone' as itemtype,
            dbo.inv_stone.refid,
            dbo.inv_stone.itemno,
            dbo.inv_stone.name_en,
            dbo.inv_stone.name_sc,
            dbo.inv_stone.uom_inventory,
            dbo.inv_stone.uom_wt
            FROM
            dbo.inv_stone
            WHERE verified='1'
        UNION ALL
            (SELECT 
            'Diamond' as itemtype,
            dbo.inv_diamond.refid,
            dbo.inv_diamond.itemno,
            dbo.inv_diamond.name_en,
            dbo.inv_diamond.name_sc,
            dbo.inv_diamond.uom_inventory,
            dbo.inv_diamond.uom_wt
            FROM
            dbo.inv_diamond
            WHERE verified='1')
        UNION ALL
            (SELECT 
            'Accessory' AS itemtype,
            dbo.inv_accessory.refid,
            dbo.inv_accessory.itemno,
            dbo.inv_accessory.name_en,
            dbo.inv_accessory.name_sc,
            dbo.inv_accessory.uom_inventory,
            dbo.inv_accessory.uom_wt
            FROM
            dbo.inv_accessory
            WHERE verified='1')
        ORDER BY itemtype, itemno"
;
}else{
    exit(
'Invalid Request');
}

$finishdate = new DateTime($hdr['finishdate']);

$settings = array('companyid'=>$companyid,
                    
'startdate'=>$hdr['startdate'],
                    
'finishdate'=>$finishdate->add(new DateInterval('PT1S'))->format('Y-m-d H:i:s')); // 1 second after stocktake

$sth $dbh->prepare($sql);
$sth->execute(  );
// echo $sth->getSQL(   );
while($dtl $sth->fetch(PDO::FETCH_ASSOC) ){
    
excel_writeRow($objWorksheet$excel_row$dtl$settings);
}

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Stocktake_report_'.$bomcategy.'_'.datef($hdr['createdate']).'.xls"');
header('Cache-Control: max-age=0');

$objPHPexcel->setActiveSheetIndex(0);
$objWriter PHPExcel_IOFactory::createWriter($objPHPexcel'Excel5');
$objWriter->setPreCalculateFormulas(false);
$objWriter->save('php://output');
 
// $stop_time = MICROTIME(TRUE);
// $time = $stop_time - $start_time;
// PRINT "Elapsed time was $time seconds.";

flush();