/var/www/enzatesting.onesolution.hk/sys_pakocalc.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
$formid 
"Order";
require_once 
"inc/configure.php";

if(
havePermission("SUu")==false){
    
//echo $permissionstr;
    
print INVALID.WS.PERMISSION;
    exit;
}

function 
gettxpako($txrefid){
    global 
$dbh;
    
$sql="SELECT
            dbo.ord_txpako.*,
            dbo.ord_txmain.orddtl_refid

            FROM
            dbo.ord_txpako
            INNER JOIN dbo.ord_txmain ON dbo.ord_txpako.txmain_refid = dbo.ord_txmain.refid
            WHERE
            dbo.ord_txpako.refid = :refid"
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':refid' => $txrefid) );
    
$tx=$sth2->fetch();
    return 
$tx;
}
//===================================functions for packing summary=================================================
function getOrddtl($orddtl_refid){
    global 
$dbh;
    
$sql="SELECT
            *
            FROM
            dbo.ord_dtl
            WHERE
            dbo.ord_dtl.refid = :orddtl_refid"
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid) );
    
$row $sth2->fetch();
    if(
$row['qty']<1){
        
$row['qty']=1;
    }
    return 
$row;
}

function 
getDiaWt($orddtl_refid){
    global 
$dbh;
    
$sql="SELECT
            Sum(dbo.ord_item_bom.actual_qty) AS wt
            FROM
            dbo.ord_item_bom
            WHERE
            dbo.ord_item_bom.orddtl_refid = :orddtl_refid AND
            dbo.ord_item_bom.bomcategy = 'diamond'"
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid) );
    
$row $sth2->fetch();
    return 
$row['wt'];
}

function 
getStoneWt($orddtl_refid){
    global 
$dbh;
    
$sql="SELECT
            Sum(dbo.ord_item_bom.actual_qty) AS wt
            FROM
            dbo.ord_item_bom
            WHERE
            dbo.ord_item_bom.orddtl_refid = :orddtl_refid AND
            dbo.ord_item_bom.bomcategy = 'stone'"
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid) );
    
$row $sth2->fetch();
    return 
$row['wt'];
}

function 
getOtherAccWt($orddtl_refid){
    global 
$dbh;
    
//find the main material
    
$sql="SELECT
            dbo.inv_material.mattype
            FROM
            dbo.ord_item_bom
            INNER JOIN dbo.inv_material ON dbo.ord_item_bom.itemnoid = dbo.inv_material.refid
            WHERE
            dbo.ord_item_bom.orddtl_refid = :orddtl_refid AND
            dbo.ord_item_bom.bomcategy = 'material'
            ORDER BY
            dbo.ord_item_bom.qty DESC
            "
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid) );
    
$material $sth2->fetch();

    
//find the total weight of the accessory that is not using the main material
    
$sql="SELECT
            Sum(dbo.ord_item_bom.actual_qty) AS wt
            FROM
            dbo.ord_item_bom
            INNER JOIN dbo.inv_accessory ON dbo.ord_item_bom.itemnoid = dbo.inv_accessory.refid
            WHERE
            dbo.ord_item_bom.orddtl_refid = :orddtl_refid AND
            dbo.ord_item_bom.bomcategy = 'accessory' AND
            dbo.inv_accessory.mattype <> :mattype"
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid':mattype' => $material['mattype']) );
    
$row $sth2->fetch();
    return 
$row['wt'];
}


function 
getMatAccWt($orddtl_refid){
    global 
$dbh;
    
//find the main material
    
$sql="SELECT
            dbo.inv_material.mattype
            FROM
            dbo.ord_item_bom
            INNER JOIN dbo.inv_material ON dbo.ord_item_bom.itemnoid = dbo.inv_material.refid
            WHERE
            dbo.ord_item_bom.orddtl_refid = :orddtl_refid AND
            dbo.ord_item_bom.bomcategy = 'material'
            ORDER BY
            dbo.ord_item_bom.qty DESC
            "
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid) );
    
$material $sth2->fetch();

    
//find the total weight of the accessory that is not using the main material
    
$sql="SELECT
            Sum(dbo.ord_item_bom.actual_qty) AS wt
            FROM
            dbo.ord_item_bom
            INNER JOIN dbo.inv_accessory ON dbo.ord_item_bom.itemnoid = dbo.inv_accessory.refid
            WHERE
            dbo.ord_item_bom.orddtl_refid = :orddtl_refid AND
            dbo.ord_item_bom.bomcategy = 'accessory' AND
            dbo.inv_accessory.mattype = :mattype"
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid':mattype' => $material['mattype']) );
    
$row $sth2->fetch();
    return 
$row['wt'];
}

function 
getFiloAccCheckoutWeight($orddtl_refid){
    global 
$dbh;
    
//find the main material
    
$sql="SELECT
            dbo.ord_txfilo.*
            FROM
            dbo.ord_txfilo
            INNER JOIN dbo.ord_txmain ON dbo.ord_txfilo.txmain_refid = dbo.ord_txmain.refid
            WHERE
            dbo.ord_txmain.orddtl_refid = :orddtl_refid
            ORDER BY
            checkoutgold_wt DESC
            "
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid) );
    
//echo $sth2->getSQL(  array(':orddtl_refid' => $orddtl_refid) ).HTML_EOL;
    
$material $sth2->fetch();
    if(
$material['checkoutgold_wt']>0){
        
$mat_acc_wt $material['checkout_wt'] - $material['checkoutgold_wt'];
    }else {
        
$mat_acc_wt 0;

        
//find splitted FILO
        
$sql="SELECT
                SUM(dbo.ord_txfilo.checkinacc_wt) as checkinacc_wt

                FROM
                dbo.ord_txmain
                INNER JOIN dbo.ord_txfilo ON dbo.ord_txmain.refid = dbo.ord_txfilo.txmain_refid
                WHERE
                dbo.ord_txmain.orddtl_refid = :orddtl_refid"
;
        
$sth2 $dbh->prepare($sql);
        
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid) );
        
//echo $sth2->getSQL(  array(':orddtl_refid' => $orddtl_refid) ).HTML_EOL;
        
if($accessory $sth2->fetch()){
            
$mat_acc_wt $accessory['checkinacc_wt'];
        }
    }
    
//echo " getFiloAccCheckoutWeight=".$mat_acc_wt.PHP_EOL;
    
return $mat_acc_wt;
}

function 
calcProdSummary($txpako){
    
//global $dbh;  //vdump($txpako);
    
global $system_var;
    
$orddtl                        getOrddtl($txpako['orddtl_refid']);

    
$summary['product_wt']         = $txpako['product_wt'];
    
$summary['dia_wt']             = getDiaWt($txpako['orddtl_refid']) / $orddtl['qty'];
    
$summary['stone_wt']         = getStoneWt($txpako['orddtl_refid']) / $orddtl['qty'];
    
$summary['main_acc_wt']        = getFiloAccCheckoutWeight($txpako['orddtl_refid']) / $orddtl['qty'];
    
$summary['other_acc_wt']    = getOtherAccWt($txpako['orddtl_refid']) / $orddtl['qty'];

    
$summary['metal_wt']         = $summary['product_wt'] - $summary['other_acc_wt'] - weightConv($summary['dia_wt']+$summary['stone_wt'], 'ct''gr');    //vdump($summary);
    
$summary['net_metal_wt']    = $summary['metal_wt']  - $summary['main_acc_wt']    ;

    
$summary['unitcost']         = getUnitCostwNetMetalWT($txpako['orddtl_refid'], $orddtl['qty'], $summary['net_metal_wt']) / $orddtl['qty'];  //calculate using actual value
    
$summary['unitfactcost']     = $summary['unitcost'] * $system_var['FMU'];  //default markup
    
$summary['unitprice']         = $orddtl['unitprice'];
    if(
$summary['unitprice']>0){
        
$summary['margin']        = ($summary['unitprice']-$summary['unitcost'])*100/$summary['unitprice'];
    }
    
//vdump($summary);
    
return $summary;
}



function 
updateProdSummary($tx){
    global 
$dbh;

    
$summary calcProdSummary($tx); //calculate summary after update BOM
    
$sql "UPDATE ord_txpako SET
                orddtl_refid = :orddtl_refid,
                unitcost = :unitcost,
                unitfactcost = :unitfactcost,
                unitprice = :unitprice,
                margin = :margin,
                metal_wt = :metal_wt,
                dia_wt = :dia_wt,
                stone_wt = :stone_wt,
                uom_stonewt = :uom_stonewt,
                net_metal_wt = :net_metal_wt,
                serialno = :serialno,
                lastupby = :lastupby,
                lastupdate = GETDATE()
            WHERE refid = :refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':refid'         => (int)$tx['refid'],
                            
':orddtl_refid'     => (int)$tx['orddtl_refid'],
                            
':unitcost'         => (float)filter_var($summary['unitcost'], FILTER_SANITIZE_STRING),
                            
':unitfactcost'     => (float)filter_var($summary['unitfactcost'], FILTER_SANITIZE_STRING),
                            
':unitprice'         => (float)filter_var($summary['unitprice'], FILTER_SANITIZE_STRING),
                            
':margin'             => (float)filter_var($summary['margin'], FILTER_SANITIZE_STRING),
                            
':metal_wt'         => (float)filter_var($summary['metal_wt'], FILTER_SANITIZE_STRING),
                            
':dia_wt'             => (float)filter_var($summary['dia_wt'], FILTER_SANITIZE_STRING),
                            
':stone_wt'         => (float)filter_var($summary['stone_wt'], FILTER_SANITIZE_STRING),
                            
':net_metal_wt'     => (float)filter_var($summary['net_metal_wt'], FILTER_SANITIZE_STRING),
                            
':uom_stonewt'         => 'ct',
                            
':serialno'         => generateSN($tx['refid']),
                            
':lastupby'         => filter_var($_SESSION['user'], FILTER_SANITIZE_STRING)) );
/*    echo $sth->getSQL( array(':refid'         => (int)$tx['refid'],
                            ':orddtl_refid'     => (int)$tx['orddtl_refid'],
                            ':unitcost'         => (float)filter_var($summary['unitcost'], FILTER_SANITIZE_STRING),
                            ':unitfactcost'     => (float)filter_var($summary['unitfactcost'], FILTER_SANITIZE_STRING),
                            ':unitprice'         => (float)filter_var($summary['unitprice'], FILTER_SANITIZE_STRING),
                            ':margin'             => (float)filter_var($summary['margin'], FILTER_SANITIZE_STRING),
                            ':metal_wt'         => (float)filter_var($summary['metal_wt'], FILTER_SANITIZE_STRING),
                            ':dia_wt'             => (float)filter_var($summary['dia_wt'], FILTER_SANITIZE_STRING),
                            ':stone_wt'         => (float)filter_var($summary['stone_wt'], FILTER_SANITIZE_STRING),
                            ':net_metal_wt'     => (float)filter_var($summary['net_metal_wt'], FILTER_SANITIZE_STRING),
                            ':uom_stonewt'         => 'ct',
                            ':serialno'         => generateSN($tx['refid']),
                            ':lastupby'         => filter_var($_SESSION['user'], FILTER_SANITIZE_STRING)) ).HTML_EOL;*/
    
pdo_showerror($sth$q);

    
//vdump($summary);
    //$summary = calcProdSummary($tx);

    //BOM material will not affect on the cost, because we use txpako[net_metal_wt] as metal qty
    
updateBOMMaterial($tx['orddtl_refid']);

}

function 
updateBOMMaterial($orddtl_refid){
    global 
$dbh;

    
//find the main material
    
$sql="SELECT
            dbo.inv_material.mattype,
            dbo.ord_item_bom.refid
            FROM
            dbo.ord_item_bom
            INNER JOIN dbo.inv_material ON dbo.ord_item_bom.itemnoid = dbo.inv_material.refid
            WHERE
            dbo.ord_item_bom.orddtl_refid = :orddtl_refid AND
            dbo.ord_item_bom.bomcategy = 'material'
            ORDER BY
            dbo.ord_item_bom.qty DESC
            "
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid) );
    
$material $sth2->fetch();

    
//get the total of net metal wt in the JC
    
$sql="SELECT
            sum(dbo.ord_txpako.net_metal_wt) as ttl_metal_wt
            FROM
            dbo.ord_txpako
            WHERE
            dbo.ord_txpako.orddtl_refid = :orddtl_refid"
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute(  array(':orddtl_refid' => $orddtl_refid) );
    
//echo $sth2->getSQL(  array(':orddtl_refid' => $orddtl_refid) ).HTML_EOL;
    
$sumpako $sth2->fetch();

    
//update Main Material weight in the BOM
    
$sql "UPDATE ord_item_bom SET
                actual_qty = :actual_qty,
                lastupby = :lastupby,
                lastupdate = GETDATE()
            WHERE refid = :refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':actual_qty' => $sumpako['ttl_metal_wt'],
                                
':refid' => (int)$material['refid'],
                                
':lastupby' => filter_var($_SESSION['user'], FILTER_SANITIZE_STRING)) );
/*    echo $sth->getSQL(  array(':actual_qty' => $net_metal_wt,
                                ':refid' => (int)$material['refid'],
                                ':lastupby' => filter_var($_SESSION['user'], FILTER_SANITIZE_STRING))   );        */

}

//=================================== end of functions for packing summary=================================================


$dbh->beginTransaction();
    
$sql="SELECT
            dbo.ord_txpako.*,
            dbo.ord_txmain.orddtl_refid

            FROM
            dbo.ord_txpako
            INNER JOIN dbo.ord_txmain ON dbo.ord_txpako.txmain_refid = dbo.ord_txmain.refid
"
;
    
$sth2 $dbh->prepare($sql);
    
$sth2->execute();


    while(
$tx=$sth2->fetch()){
        
// vdump($tx['refid']);
        //$tx = gettxpako($row['refid']);
        
$orddtl_refid $tx['orddtl_refid'];
        
$orddtl getOrddtl($orddtl_refid);
        
$ordmain_refid ord_getOrdmain_refid($orddtl['refid']);

        
updateProdSummary($tx);
    
//exit;
    // vdump(ord_isallpacked($orddtl_refid));

        
if(ord_isallpacked($orddtl_refid)){
            
//update
            
require_once "inc/ord_dtl_func.php";

            print 
"ALL PACKED $orddtl_refid <br/>";
            
ord_updateOrddtlSummary($orddtl_refid);
            
updateMainValue($ordmain_refid);
            
ord_updateInvProductForFirstItem($orddtl_refid);
        }

    }
//exit;
$dbh->commit();

    print 
"DONE";
    exit;

//vdump($_REQUEST);
print "Invalid Request";