/var/www/enzatesting.onesolution.hk/01_20240711_full_backup/dgn_master_modify.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
<?php $formid "Master";
require_once 
"inc/configure.php";

function 
copy_dgn_master_BOM($last_refid$new_refid){
    global 
$dbh;
    
//copy info from last version
    
$sql "insert into dgn_master_bom(
                mold_refid, bomcategy, itemnoid, qty, uom_qty, price, uom_price, unitprice, createby, createdate, lastupby, lastupdate)
            select :new_refid as mold_refid, bomcategy, itemnoid, qty, uom_qty, price, uom_price, unitprice, createby, createdate, lastupby, lastupdate
             from dgn_master_bom
            where mold_refid = :last_refid"
;
    
$sth $dbh->prepare($sql);
    
$q$sth->execute(  array(':new_refid' => $new_refid,
                            
':last_refid' => $last_refid) );
/*    echo $sth->getSQL(  array(':new_refid' => $new_refid,
                            ':last_refid' => $last_refid) );*/
    
pdo_showerror($sth$q);
}

function 
copy_dgn_master_submold($last_refid$new_refid){
    global 
$dbh;
    
//copy info from last version
    
$sql "insert into dgn_master_submold(
                mold_refid, mold_no, version, qty, img_src, img_path, createby, createdate, lastupby, lastupdate)
            select :new_refid as mold_refid, mold_no, version, qty, img_src, img_path, createby, createdate, lastupby, lastupdate
             from dgn_master_submold
            where mold_refid = :last_refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':new_refid' => $new_refid,
                            
':last_refid' => $last_refid) );
/*    echo $sth->getSQL(  array(':new_refid' => $new_refid,
                            ':last_refid' => $last_refid) );*/
    
pdo_showerror($sth$q);
}

function 
copy_dgn_master_mold($refid){
    
    global 
$dbh;
    
//copy info from last version
    
$sql "insert into dgn_master_mold(
                status, response_date, launch_date, model_maker_name, pdt_type,
                custcode, request_origine, target_wt, uom_wt, target_price,
                qtn_price, est_price, remark, client_remark, fmproduct,
                uom_cy, target_date, request_approval_date, drawing_approval_date, req_no,
                drawing_no, designer_name, producttype, moldno, mattype,
                matcolor, finishing, diatype, stonetype, productstonecolor,
                size, version, pdt_moldno, itemno, wax_wt, uom_waxwt, formolding,
                companyid, finished,
                silver_wt, uom_silverwt, remark_mold, target_wt_ref, productsize, 
                mastersize, isproductsize_range, productsize_fm, productsize_to,
                createby, createdate, lastupby, lastupdate)
            select         
                status, response_date, launch_date, model_maker_name, pdt_type,
                custcode, request_origine, target_wt, uom_wt, target_price,
                qtn_price, est_price, remark, client_remark, fmproduct,
                uom_cy, target_date, request_approval_date, drawing_approval_date, req_no,
                drawing_no, designer_name, producttype, moldno, mattype,
                matcolor, finishing, diatype, stonetype, productstonecolor,
                size, version, pdt_moldno, itemno, wax_wt, uom_waxwt, formolding,
                companyid, finished,
                silver_wt, uom_silverwt, remark_mold, target_wt_ref, productsize, 
                mastersize, isproductsize_range, productsize_fm, productsize_to,
                createby, createdate, lastupby, lastupdate from dgn_master_mold where             
            refid = :refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':refid' => $refid) );    
    
//echo $sth->getSQL(  array(':refid' => $refid) );    

    
pdo_showerror($sth$q);
    
$new_refid $dbh->lastInsertId();
    
    
copy_dgn_master_BOM($refid$new_refid);
    
copy_dgn_master_submold($refid$new_refid);
    
//copy route
    
bnr_copyRoute('MASTER'$refid$new_refid);

    return 
$new_refid;
}

$refid                 = (int)filter_var($_REQUEST['refid'], FILTER_SANITIZE_NUMBER_INT);
$mold_refid         $refid;    
/*print"<pre>";
print_r($_FILES);
print"</pre>";*/
/*vdump($_REQUEST); exit;*/
if( $_POST['action']=="dgn_master_modifyform" && !empty($refid) ){
    

    
    
//generals
    
$status             filter_var($_REQUEST['status'], FILTER_SANITIZE_STRING);    
    
$last_status         filter_var($_REQUEST['last_status'], FILTER_SANITIZE_STRING);    
    
    
$response_date         filter_var($_REQUEST['response_date'], FILTER_SANITIZE_STRING);
        
    
$launch_date         filter_var($_REQUEST['launch_date'], FILTER_SANITIZE_STRING);            
    
$model_maker_name     filter_var($_REQUEST['model_maker_name'], FILTER_SANITIZE_STRING);    
    
$pdt_type             filter_var($_REQUEST['pdt_type'], FILTER_SANITIZE_STRING);    
    
    
$custcode             filter_var($_REQUEST['custcode'], FILTER_SANITIZE_STRING);
    
$request_origine    filter_var($_REQUEST['request_origine'], FILTER_SANITIZE_STRING);
    
$target_wt             = (float)filter_var($_REQUEST['target_wt'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$uom_wt                 = (float)filter_var($_REQUEST['uom_wt'], FILTER_SANITIZE_STRING);    
    
$target_price         = (float)filter_var($_REQUEST['target_price'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);    
    
$est_price             = (float)filter_var($_REQUEST['est_price'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);    
    
$qtn_price             = (float)filter_var($_REQUEST['qtn_price'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);    
    
    
$createby            filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
$lastupby            filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
    
//changed to english remark
    
$remark_new            filter_var($_REQUEST['remark_new'], FILTER_SANITIZE_STRING);    
    
$remark                filter_var($_REQUEST['remark_record'], FILTER_SANITIZE_STRING);    
    if(!empty(
$remark_new)){
        
//$remark_new         = "--" . $createby . " [" . date("Y-m-d h:i:s") . "]\n" . $remark_new;
        
$remark             $remark_new "\n" $remark//append old remarks
    
}
        
//changed to chinese remark
    
$client_remark_new    filter_var($_REQUEST['client_remark_new'], FILTER_SANITIZE_STRING);    
    
$client_remark        filter_var($_REQUEST['client_remark_record'], FILTER_SANITIZE_STRING);
    if(
$client_remark_new){
        
//$client_remark_new         = "--" . $createby . " [" . date("Y-m-d h:i:s") . "]\n" . $client_remark_new;
        
$client_remark         $client_remark_new "\n" $client_remark//append old remarks 
    
}
    
    
$uom_cy                 filter_var($_REQUEST['uom_cy'], FILTER_SANITIZE_STRING);    
    
$target_date         filter_var($_REQUEST['target_date'], FILTER_SANITIZE_STRING);    
    
    
$request_approval_date     filter_var($_REQUEST['request_approval_date'], FILTER_SANITIZE_STRING);    
    
$drawing_approval_date     filter_var($_REQUEST['drawing_approval_date'], FILTER_SANITIZE_STRING);    
    
$req_no                filter_var($_REQUEST['req_no'], FILTER_SANITIZE_STRING);
    
$drawing_no            filter_var($_REQUEST['drawing_no'], FILTER_SANITIZE_STRING);
    
$designer_name         filter_var($_REQUEST['designer_name'], FILTER_SANITIZE_STRING);
    
    
$wax_wt                 = (float)filter_var($_REQUEST['wax_wt'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$uom_waxwt             filter_var($_REQUEST['uom_waxwt'], FILTER_SANITIZE_STRING);    
    
$formolding            = empty($_REQUEST['formolding'])?"false":"true";    
    
$silver_wt             = (float)filter_var($_REQUEST['silver_wt'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$uom_silverwt        filter_var($_REQUEST['uom_silverwt'], FILTER_SANITIZE_STRING);
    
$remark_mold        filter_var($_REQUEST['remark_mold'], FILTER_SANITIZE_STRING);    
    
    
//segments
    
$producttype         filter_var($_REQUEST['producttype'], FILTER_SANITIZE_STRING);    
    
$moldno             filter_var($_REQUEST['moldno'], FILTER_SANITIZE_STRING);
    
$moldno                substrstr_pad$moldno    4'0'STR_PAD_LEFT) , 04);    
    
$mattype            filter_var($_REQUEST['mattype'], FILTER_SANITIZE_STRING);    
    
$matcolor             filter_var($_REQUEST['matcolor'], FILTER_SANITIZE_STRING);    
    
$finishing             filter_var($_REQUEST['finishing'], FILTER_SANITIZE_STRING);    
    
$diatype            filter_var($_REQUEST['diatype'], FILTER_SANITIZE_STRING);    
    
$stonetype             filter_var($_REQUEST['stonetype'], FILTER_SANITIZE_STRING);
    
$productstonecolor    filter_var($_REQUEST['productstonecolor'], FILTER_SANITIZE_STRING);    
    
$size                 filter_var($_REQUEST['size'], FILTER_SANITIZE_STRING);    
    
$itemno             getItemNo($producttype$moldno$mattype$matcolor$finishing$diatype$stonetype$productstonecolor$size);    
    
    
$pdt_moldno            filter_var($_REQUEST['pdt_moldno'], FILTER_SANITIZE_STRING);

    
$fmproduct             filter_var($_REQUEST['fmproduct'], FILTER_SANITIZE_STRING);    
    
$productrefid         filter_var($_REQUEST['productrefid'], FILTER_SANITIZE_STRING);    
        
    
$target_wt_ref         filter_var($_REQUEST['target_wt_ref'], FILTER_SANITIZE_STRING);
    
$productsize         filter_var($_REQUEST['productsize'], FILTER_SANITIZE_STRING);
    
$mastersize         filter_var($_REQUEST['mastersize'], FILTER_SANITIZE_STRING);
    
$isproductsize_range = empty($_REQUEST['isproductsize_range'])? '0''1';
    
$productsize_fm        filter_var($_REQUEST['productsize_fm'], FILTER_SANITIZE_STRING);
    
$productsize_to        filter_var($_REQUEST['productsize_to'], FILTER_SANITIZE_STRING);
    
//echo "pdt_moldno = $pdt_moldno";
    //echo "<br/> new = $producttype, $moldno";
    // check existance of moldno before insert    
    
if( $pdt_moldno != $producttype $moldno){
        if( 
pdtmold_exist($producttype$moldno$size$drawing_no) ){
            
$msg="$producttype$moldno ($size) is already existed";
            
header("Location: dgn_master_modifyform.php?refid=$refid&edit=1&error=$msg");
            print 
$msg;
            exit;    
        }
    }

$dbh->beginTransaction();
    
    
$last_refid $refid;    
    
$refid copy_dgn_master_mold($last_refid);
    
    
//if(empty($pdt_moldno)){
    
$pdt_moldno $producttype $moldno;
    
//}
    
/*    if($status=="AMEND" && $last_status=="REJECT"){ //new status -> new version
        $drawing_no            = getNextVersionNum($drawing_no); 
    }*/
    //$seq                = parseSeqNum($drawing_no);
    
$version                 = (int)filter_var($_REQUEST['version'], FILTER_SANITIZE_NUMBER_INT);

    
//changed to validation date
    
if($status == "CUS-QAP" && empty($response_date) ){
        
$response_date         date("Y-m-d");
    }
    
    if(
$last_status=="REJECT" && 
        (
$status<>"REJECT" && $status<>"CANCEL" && $status<>"P&DAP")   ){ //new status -> new version
        
$version++; 
        
$removeTD true;
    }
    elseif(
$last_status=="PDM-REJ" &&
        (
$status<>"PDM-REJ" && $status<>"CUS-QAP" && $status<>"CANCEL")  ){ //new status -> new version
        
$version++; 
        
$removeTD true;    
    }
    elseif(
$last_status=="CAD-REV" &&
        (
$status<>"CAD-REV" && $status<>"CUS-QAP" && $status<>"CANCEL")  ){ //new status -> new version
        
$version++; 
        
$removeTD true;    
    }
    elseif(
$last_status=="PDM-MTR" &&
        (
$status<>"PDM-MTR" && $status<>"CUS-QAP" && $status<>"CANCEL")  ){ //new status -> new version
        
$version++; 
        
$removeTD true;    
    }
    elseif(
$status=="CANCEL"){
        
$sql "UPDATE dgn_master_mold SET 
                    finished = :finished,
                    lastupby = :lastupby,
                    lastupdate = GETDATE()
                WHERE refid = :refid"
;    
    
        
$sth $dbh->prepare($sql);
        
$q $sth->execute( array(':finished' => 1,
                                    
':lastupby' => $lastupby,
                                    
':refid' => $refid) );
        
pdo_showerror($sth$q);        
    }
    
    
$sql_param = array(':status' => $status,
                            
':response_date' => $response_date,
                            
':launch_date' => $launch_date,
                            
':model_maker_name' => $model_maker_name,
                            
':pdt_type' => $pdt_type,
                            
':custcode' => $custcode,
                            
':request_origine' => $request_origine,
                            
                            
':uom_wt' => $uom_wt,
                            
':target_price' => $target_price,
                            
                            
':client_remark' => $client_remark,
                            
':fmproduct' => $fmproduct,
                            
':uom_cy' => $uom_cy,
                            
':target_date' => $target_date,
                            
':request_approval_date' => $request_approval_date,
                            
':drawing_approval_date' => $drawing_approval_date,
                            
':req_no' => $req_no,
                            
':drawing_no' => $drawing_no,
                            
':designer_name' => $designer_name,
                            
':producttype' => $producttype,
                            
':moldno' => $moldno,
                            
':mattype' => $mattype,
                            
':matcolor' => $matcolor,
                            
':finishing' => $finishing,
                            
':diatype' => $diatype,
                            
':stonetype' => $stonetype,
                            
':productstonecolor' => $productstonecolor,
                            
':size' => $size,
                            
':itemno' => $itemno,
                            
':wax_wt' => $wax_wt,
                            
':uom_waxwt' => $uom_waxwt,
                            
':formolding' => $formolding,
                            
':silver_wt' => $silver_wt,
                            
':uom_silverwt' => $uom_silverwt,
                            
':remark_mold' => $remark_mold,
                            
':version' => $version,
                            
':pdt_moldno' => $pdt_moldno,
                            
':target_wt_ref' => $target_wt_ref,                            
                            
':isproductsize_range' => $isproductsize_range,
                            
':productsize_fm' => $productsize_fm,
                            
':productsize_to' => $productsize_to,                            
                            
':createby' => $createby,
                            
':lastupby' => $lastupby,
                            
':refid' => $refid,                            
                            
':target_wt' => $target_wt,
                            
':productsize' => $productsize,
                            
':mastersize' => $mastersize,
                            
':remark' => $remark);
    
    if(
havePermission("PPu")){ 
        
$sql_PPu_field="qtn_price = :qtn_price,
                        est_price = :est_price,"
;
                                            
        
$sql_param[':est_price'] = $est_price;
        
$sql_param[':qtn_price'] = $qtn_price;
    }
    
    
//new request record with same request_no
    
$sql "UPDATE dgn_master_mold SET
                status = :status,
                response_date = :response_date,
                launch_date = :launch_date,
                model_maker_name = :model_maker_name,
                pdt_type = :pdt_type,
                custcode = :custcode,
                request_origine = :request_origine,
                target_wt = :target_wt,
                uom_wt = :uom_wt,
                target_price = :target_price,
                
                
$sql_PPu_field
                
                remark = :remark,
                client_remark = :client_remark,
                fmproduct = :fmproduct,
                uom_cy = :uom_cy,
                target_date = :target_date,
                request_approval_date = :request_approval_date,
                drawing_approval_date = :drawing_approval_date,
                req_no = :req_no,
                drawing_no = :drawing_no,
                designer_name = :designer_name,
                producttype = :producttype,
                moldno = :moldno,
                mattype = :mattype,
                matcolor = :matcolor,
                finishing = :finishing,
                diatype = :diatype,
                stonetype = :stonetype,
                productstonecolor = :productstonecolor,
                size = :size,
                version = :version,
                pdt_moldno = :pdt_moldno,
                itemno = :itemno,
                wax_wt = :wax_wt,
                uom_waxwt = :uom_waxwt,
                formolding = :formolding,
                silver_wt = :silver_wt, 
                uom_silverwt = :uom_silverwt,
                remark_mold = :remark_mold,
                target_wt_ref = :target_wt_ref,
                productsize = :productsize,
                mastersize = :mastersize,
                isproductsize_range = :isproductsize_range,
                productsize_fm = :productsize_fm,
                productsize_to = :productsize_to,
                createby = :createby,
                createdate = getdate(),
                lastupby = :lastupby,
                lastupdate = getdate()
        WHERE refid = :refid "
;

    
$sth $dbh->prepare($sql);
    
$q $sth->execute$sql_param );                    
    
//echo $sth->getSQL( $sql_param  ) . HTML_EOL;        
    
pdo_showerror($sth$q);                

if(
havePermission("BMu")){ 
    
//update tmp BOM------------------------------------------------
    
$sql "DELETE from dgn_master_bom WHERE mold_refid=:mold_refid";
    
$sth $dbh->prepare($sql);
    
$q$sth->execute( array(':mold_refid'=> $refid) );
    
pdo_showerror($sth$q);
    
    
    if(!empty(
$_REQUEST['bom_detail'])){
        
$sql "INSERT INTO dgn_master_bom (mold_refid, bomcategy, itemnoid, qty, uom_qty, price, uom_price, unitprice, createby, createdate, lastupby, lastupdate)
                Values(:mold_refid, :bcategy, :itemnoid, :qty, :uom_qty, :price, :uom_price, :unitprice, :createby, GETDATE(), :lastupby, GETDATE())"
;
        
$sth $dbh->prepare($sql);    
        foreach(
$_REQUEST['bom_detail'] as $bom_item){
            
//vdump($bom_item);    
            
$q$sth->execute( array(':mold_refid' => $refid,
                                    
':bcategy' => $bom_item['bomcategy'],
                                    
':itemnoid' => $bom_item['itemnoid'], 
                                    
':qty' => (float)$bom_item['qty'], 
                                    
':uom_qty' => $bom_item['uom_qty'],
                                    
':price' => $bom_item['price'], 
                                    
':uom_price' => (float)$bom_item['uom_price'],
                                    
':unitprice' => (float)$bom_item['unitprice'],
                                    
':createby' => $createby,
                                    
':lastupby' => $lastupby) );
            
pdo_showerror($sth$q);                        
    
/*        echo $sth->getSQL( array(':mold_refid' => $refid,
                                    ':bcategy' => $bom_item['bomcategy'],
                                    ':itemnoid' => $bom_item['itemnoid'], 
                                    ':qty' => $bom_item['qty'], 
                                    ':uom_qty' => $bom_item['uom_qty'],
                                    ':price' => $bom_item['price'], 
                                    ':uom_price' => $bom_item['uom_price'],
                                    ':unitprice' => $bom_item['unitprice'],
                                    ':createby' => $createby,
                                    ':lastupby' => $lastupby) ) . HTML_EOL;*/
        
}
    }
}

// drawing ---------------------------------------------------------
if(!empty($_REQUEST['old_img'])){
    
// copy drawing file detail to new record
    
$sql "INSERT INTO dgn_master_moldimg (
                    mold_refid,
                    img_src, img_path,
                    createby, createdate,
                    lastupby, lastupdate
                ) VALUES (
                    :mold_refid,
                    :img_src, :img_path,
                    :createby, "
.NOW."(),
                    :lastupby, "
.NOW."()
                )"
;
    
$sth $dbh->prepare($sql);
    
    foreach (
$_REQUEST['old_img'] as $dwg_refid){        
        
$row getDB_dgn_master_img($dwg_refid);
        
//vdump($row);
            
        
$q$sth->execute( array(':mold_refid' => $refid,
        
':img_src' => $row['img_src'],
        
':img_path' => $row['img_path'],
        
':createby' => $createby,
        
':lastupby' => $lastupby) );
/*        echo $sth->getSQL( array(':drawing_refid' => 100,
        ':img_src' => $row['img_src'],
        ':img_path' => $row['img_path'],
        ':createby' => $createby,
        ':lastupby' => $lastupby) );*/    
        
pdo_showerror($sth$q);        
    }
}

foreach(
$_FILES['d_file']['name'] as $key => $tmpf){
    
    
//upload image\
    //var_dump($tmpf);
    
if ($_FILES['d_file']['name'][$key] <> '')
    {
        
$f $_FILES['d_file']['name'][$key];
        
$filename getImgName("MASTER"$f$refid);
        
//echo INV_PRODUCT_IMG_PATH.$filename;
        
copy ($_FILES['d_file']['tmp_name'][$key], DGN_MASTER_IMG_PATH.$filename)  
                or die (
"Could not copy image");
        
createthumb(DGN_MASTER_IMG_PATH.$filenameDGN_MASTER_IMG_PATH.THUMB_PATH.$filename300300);
    }
    else{
        
//tbc
        
$filename="";
    }
    
    if(
$filename){
    
        
$sql "INSERT INTO dgn_master_moldimg (
                    mold_refid,
                    img_src, img_path,
                    createby, createdate,
                    lastupby, lastupdate
                ) VALUES (
                    :mold_refid,
                    :img_src, :img_path,
                    :createby, "
.NOW."(),
                    :lastupby, "
.NOW."()
                )"
;
        
$sth $dbh->prepare($sql);    
        
        
$q$sth->execute( array(':mold_refid' => $refid,
                                
':img_src' => $filename,
                                
':img_path' => DGN_MASTER_IMG_PATH
                                
':createby' => $createby,
                                
':lastupby' => $lastupby) );    
        
pdo_showerror($sth$q);
/*        echo $sth->getSQL( array(':drawing_refid' => $refid,
                                ':img_src' => $filename,
                                ':img_path' => DGN_DRAWING_IMG_PATH, 
                                ':createby' => $createby,
                                ':lastupby' => $lastupby) );*/
    
}
}    

// technical drawing---------------------------------------------------
    
foreach($_FILES['td_file']['name'] as $key => $tmpf){
        
        
//upload image\
        //var_dump($tmpf);
        
if ($_FILES['td_file']['name'][$key] <> '')
        {
            
$f $_FILES['td_file']['name'][$key];
            
$filename getImgName("TECH_DRAWING"$f$refid);
            
//echo INV_PRODUCT_IMG_PATH.$filename;
            
copy ($_FILES['td_file']['tmp_name'][$key], DGN_DRAWING_IMG_PATH.$filename)  
                    or die (
"Could not copy image");
            
createthumb(DGN_DRAWING_IMG_PATH.$filenameDGN_DRAWING_IMG_PATH.THUMB_PATH.$filename300300);
        }
        else{
            
//tbc
            
$filename="";
        }
        
        if(
$filename){
        
            
$sql "INSERT INTO dgn_techdrawing_img (
                    mold_refid,
                    img_src, img_path,
                    createby, createdate,
                    lastupby, lastupdate
                ) VALUES (
                    :mold_refid,
                    :img_src, :img_path,
                    :createby, "
.NOW."(),
                    :lastupby, "
.NOW."()
                )    "
;
            
$sth $dbh->prepare($sql);    
            
            
$q$sth->execute( array(':mold_refid' => $refid,
                                    
':img_src' => $filename,
                                    
':img_path' => DGN_DRAWING_IMG_PATH
                                    
':createby' => $createby,
                                    
':lastupby' => $lastupby) );    
            
pdo_showerror($sth$q);
    
/*        echo $sth->getSQL( array(':drawing_refid' => $refid,
                                    ':img_src' => $filename,
                                    ':img_path' => DGN_DRAWING_IMG_PATH, 
                                    ':createby' => $createby,
                                    ':lastupby' => $lastupby) );*/
            //unset($_REQUEST['old_tdimg']);                        
        
}
    }

if(!
$removeTD && !empty($_REQUEST['old_tdimg'])){
    
// copy drawing file detail to new record
    
$sql "INSERT INTO dgn_techdrawing_img (
                mold_refid,
                img_src, img_path,
                createby, createdate,
                lastupby, lastupdate
            ) VALUES (
                :mold_refid,
                :img_src, :img_path,
                :createby, "
.NOW."(),
                :lastupby, "
.NOW."()
            )    "
;
    
$sth $dbh->prepare($sql);
    
    foreach (
$_REQUEST['old_tdimg'] as $dwg_refid){        
        
$row getDB_dgn_tddrawing_img($dwg_refid);
        
//vdump($row);
            
        
$q$sth->execute( array(':mold_refid' => $refid,
        
':img_src' => $row['img_src'],
        
':img_path' => $row['img_path'],
        
':createby' => $createby,
        
':lastupby' => $lastupby) );
/*        echo $sth->getSQL( array(':mold_refid' => $refid,
        ':img_src' => $row['img_src'],
        ':img_path' => $row['img_path'],
        ':createby' => $createby,
        ':lastupby' => $lastupby) ).HTML_EOL;*/    
        
pdo_showerror($sth$q);        
    }
}

//if(havePermission("TDu")){

//}
//exit;

// pdf drawing---------------------------------------------------
foreach($_FILES['pdf_file']['name'] as $key => $tmpf){
    
    
//upload image\
    //var_dump($tmpf);
    
if ($_FILES['pdf_file']['name'][$key] <> '')
    {
        
$f $_FILES['pdf_file']['name'][$key];
        
$filename getImgName("Request_file"$f$refid);
        
//echo INV_PRODUCT_IMG_PATH.$filename;
        
copy ($_FILES['pdf_file']['tmp_name'][$key], DGN_DRAWING_IMG_PATH.$filename)  
                or die (
"Could not copy image");
        
//createthumb(DGN_REQUEST_IMG_PATH.$filename, DGN_REQUEST_IMG_PATH.THUMB_PATH.$filename, 300, 300);
    
}
    else{
        
//tbc
        
$filename="";
    }
    
    if(
$filename){
    
    
$sql "INSERT INTO dgn_techdrawing_pdf (
                mold_refid,
                img_src, img_path,
                createby, createdate,
                lastupby, lastupdate
            ) VALUES (
                :mold_refid,
                :img_src, :img_path,
                :createby, "
.NOW."(),
                :lastupby, "
.NOW."()
            )    "
;
        
$sth $dbh->prepare($sql);    
        
        
$q$sth->execute( array(':mold_refid' => $refid,
                                
':img_src' => $filename,
                                
':img_path' => DGN_DRAWING_IMG_PATH
                                
':createby' => $createby,
                                
':lastupby' => $lastupby) );    
        
pdo_showerror($sth$q);
/*        echo $sth->getSQL( array(':drawing_refid' => $refid,
                                ':img_src' => $filename,
                                ':img_path' => DGN_REQUEST_IMG_PATH, 
                                ':createby' => $createby,
                                ':lastupby' => $lastupby) );*/
        //unset($_REQUEST['old_pdf']); //only one drawing is allowed                                
    
}
}

if(!empty(
$_REQUEST['old_pdf'])){
    
// copy drawing file detail to new record
    
$sql "INSERT INTO dgn_techdrawing_pdf (
                mold_refid,
                img_src, img_path,
                createby, createdate,
                lastupby, lastupdate
            ) VALUES (
                :mold_refid,
                :img_src, :img_path,
                :createby, "
.NOW."(),
                :lastupby, "
.NOW."()
            )    "
;
    
$sth $dbh->prepare($sql);
    
    foreach (
$_REQUEST['old_pdf'] as $dwg_refid){        
        
$row getDB_dgn_drawing_file($dwg_refid);
        
//vdump($row);
            
        
$q$sth->execute( array(':mold_refid' => $refid,
        
':img_src' => $row['img_src'],
        
':img_path' => $row['img_path'],
        
':createby' => $createby,
        
':lastupby' => $lastupby) );
/*        echo $sth->getSQL( array(':mold_refid' => $refid,
        ':img_src' => $row['img_src'],
        ':img_path' => $row['img_path'],
        ':createby' => $createby,
        ':lastupby' => $lastupby) ).HTML_EOL;*/
        
pdo_showerror($sth$q);        
    }
}

/*
//update route ------------------------------------------------
//TODO: update route/subroute/subsubroute records to new mold_refid
    $sql = "UPDATE dgn_master_route SET mold_refid = :newid WHERE mold_refid = :oldid";
    $sth = $dbh->prepare($sql);    
    $q= $sth->execute( array(':newid' => $refid,
                            ':oldid' => $mold_refid) );
    pdo_showerror($sth, $q);
                                
    $sql = "UPDATE dgn_master_subroute SET mold_refid = :newid WHERE mold_refid = :oldid";
    $sth = $dbh->prepare($sql);    
    $q= $sth->execute( array(':newid' => $refid,
                            ':oldid' => $mold_refid) );
    pdo_showerror($sth, $q);
                            
    $sql = "UPDATE dgn_master_subsubroute SET mold_refid = :newid WHERE mold_refid = :oldid";
    $sth = $dbh->prepare($sql);    
    $q= $sth->execute( array(':newid' => $refid,
                            ':oldid' => $mold_refid) );
    pdo_showerror($sth, $q);

*/


//update Submold information------------------------------------------------
$sql "DELETE from dgn_master_submold WHERE mold_refid=:mold_refid";
$sth $dbh->prepare($sql);
$q$sth->execute( array(':mold_refid'=> $refid) );
//echo $sth->getSQL( array(':mold_refid'=> $refid) );
pdo_showerror($sth$q);


if(!empty(
$_REQUEST['submold_detail'])){

    foreach(
$_REQUEST['submold_detail'] as $submold_no => $submold){
        
//vdump($submold);    
        
$sql "INSERT INTO dgn_master_submold (
                mold_refid, mold_no, mold_subno, qty, 
                img_src, img_path,
                createby, createdate, lastupby, lastupdate)
                Values(
                :mold_refid, :mold_no, :mold_subno, :qty, 
                :img_src, :img_path,
                :createby, GETDATE(), :lastupby, GETDATE())"
;
        
$sth $dbh->prepare($sql);            
        
$q$sth->execute( array(':mold_refid' => $refid,
                                
':mold_no' => $pdt_moldno,
                                
':mold_subno' => $submold['mold_subno'], 
                                
':qty' => (int)$submold['qty'], 
                                
':img_src' => $submold['img_src'], 
                                
':img_path' => $submold['img_path'], 
                                
':createby' => $createby,
                                
':lastupby' => $lastupby) );
/*        echo $sth->getSQL( array(':mold_refid' => $refid,
                                ':mold_no' => $pdt_moldno,
                                ':mold_subno' => $submold['mold_subno'], 
                                ':qty' => $submold['qty'], 
                                ':createby' => $createby,
                                ':lastupby' => $lastupby) ) . HTML_EOL;        */                    
        
pdo_showerror($sth$q);
        
$submold_refid $dbh->lastInsertId();                            
        
        
//upload submold img 
        //$submold_no = $submold['mold_subno'];
        //vdump($_FILES['submold_detail']['name'][$submold_no]);
        
if ($_FILES['submold_detail']['name'][$submold_no]['file'] <> '')
        {
            
$f $_FILES['submold_detail']['name'][$submold_no]['file'];
            
$filename getImgName("MASTERMOLD"$f$refid);
            
//echo DGN_MASTERMOLD_IMG_PATH.$filename;
            
copy ($_FILES['submold_detail']['tmp_name'][$submold_no]['file'], DGN_MASTERMOLD_IMG_PATH.$filename)  
                    or die (
"Could not copy image");
            
createthumb(DGN_MASTERMOLD_IMG_PATH.$filenameDGN_MASTERMOLD_IMG_PATH.THUMB_PATH.$filename300300);
        }
        else{
            
//tbc
            
$filename="";
        }
        
        if(
$filename){
        
            
$sql "UPDATE dgn_master_submold SET
                            img_src = :img_src,
                            img_path = :img_path
                        where refid = :refid"
;
            
$sth $dbh->prepare($sql);    
            
            
$q$sth->execute( array(':refid' => $submold_refid,
                                    
':img_src' => $filename,
                                    
':img_path' => DGN_MASTERMOLD_IMG_PATH) );    
            
pdo_showerror($sth$q);
/*            echo $sth->getSQL( array(':refid' => $submold_refid,
                                    ':img_src' => $filename,
                                    ':img_path' => DGN_MASTERMOLD_IMG_PATH) ).HTML_EOL;*/
        
}
    }
}
    
// update product to mastermold reference---------------------------

/*$sql = "SELECT mold_refid from inv_product where refid = :refid";
$sth = $dbh->prepare($sql);
$q= $sth->execute( array(':refid'=> $productrefid) );
pdo_showerror($sth, $q);
echo $sth->getSQL( array(':refid'=> $productrefid) );
pdo_showerror($sth, $q);
$oldmold = $sth->fetch();*/

$sql "SELECT refid from dgn_master_mold where refid = :refid";
$sth $dbh->prepare($sql);
$q$sth->execute( array(':refid'=> $last_refid) );
//echo $sth->getSQL( array(':refid'=> $last_refid) );
pdo_showerror($sth$q);
$oldmold $sth->fetch();
//var_dump($oldmold);

//update mastermold link of all related product
$sql "UPDATE inv_product SET mold_refid = :newrefid where mold_refid = :oldrefid";
$sth $dbh->prepare($sql);
$q$sth->execute( array(':newrefid'=> $refid':oldrefid'=> (int)$oldmold['refid']) );
//echo $sth->getSQL( array(':newrefid'=> $refid, ':oldrefid'=> $oldmold['refid']) );
pdo_showerror($sth$q);
//exit;

$dbh->commit();
    
            
//header("Location: dgn_master_index.php?act=resume&refid=$refid&msg=Saved.");
            
    
form_dest($_REQUEST['godest'], $_REQUEST['formdest']);
    
header("Location: dgn_master_modifyform.php?edit=1&refid=$refid&fmproduct=$fmproduct&productrefid=$productrefid&msg=Saved."); 
    print 
"Saved.";
    exit;
    
    
}
myerror("Invaild request");


?>