/var/www/enzatesting.onesolution.hk/master_customer_importpricelist.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
<?php
$formid 
"Product";
require_once 
"inc/configure.php";
require_once 
"inc/ord_dtl_func.php";
require_once 
"inc/phpexcel_1.7.7/PHPExcel.php";    

$refid setDefaultReqVar("refid"'');

if(
havePermission("PPu")==false){
    
myerror("Invalid Permission");    
}

if(
$_FILES['f']['name']){    
    
$prefix "customer_pricelist_"date('Y-m-d_'). hash('md5'rand()) . '_'$_FILES['f']['name'];
    
$prefix getNormalizedName($prefix);
    
$f IMPORT_PATH.$prefix.".xls";

    
move_uploaded_file($_FILES['f']['tmp_name'], $f)
            or die(
"Could not copy - ".$_FILES["f"]["error"]);
}

if( 
$_POST['action']=='master_customer_import_pricelist' && $f && 
    
$refid && $customer=getDB("master_customer"$refid) )
{
    
}else{
    
//vdump( $_FILES['f'], $_POST['action']=='master_customer_import_pricelist' , $f , 
    //$refid , $customer);
    
myerror(INVALID.WS.REQUEST);
    exit;
}

//echo "start reading xls";
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setReadDataOnly(true);
$objPHPExcel $objReader->load$f );
//echo "xls is loaded";

$rowIterator $objPHPExcel->getSheetByName('Pricelist')->getRowIterator();

$array_data = array();
$array_sql = array();

$prod_row 0;
foreach(
$rowIterator as $row){
    
$cellIterator $row->getCellIterator();
    
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
    
if( $row->getRowIndex() <= ) continue;//skip first 4 rows
    
$rowIndex $row->getRowIndex ();
    
$array_data[$rowIndex] = array('A'=>'''B'=>'''Y'=>'','Z'=>'');
    
    foreach (
$cellIterator as $cell) {
        if( 
'A' == $cell->getColumn() || 
            
'B' == $cell->getColumn() || 
            
'Y' == $cell->getColumn() || //L->Y
            
'Z' == $cell->getColumn() || //M->Z
            
'AA' == $cell->getColumn() ){ 
             
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
        } 
    }
 
/*
 A:prod_refid    int
 B:prod_itemno    varchar
 Y:new_price    float
 Z:UpdateFlag    char
 AA:remarks        varchar
 */

    
if( !empty($array_data[$rowIndex]['A']) && !empty($array_data[$rowIndex]['Y']) ){ //not empty row
    
        
$prod_refid    = (int)filter_var($array_data[$rowIndex]['A'], FILTER_SANITIZE_STRING);
        
$prod_itemnofilter_var($array_data[$rowIndex]['B'], FILTER_SANITIZE_STRING);
        
$new_price    = (float)filter_var($array_data[$rowIndex]['Y'], FILTER_SANITIZE_STRING);
        
$remark filter_var($array_data[$rowIndex]['AA'], FILTER_SANITIZE_STRING);

        
//validate product refid and itemno
        
$prod getDB("inv_product"$prod_refid);
        if(
$prod['itemno']==$prod_itemno){
            
            if( 
strtoupper($array_data[$rowIndex]['Z'])=='Y'){
                
$updateOrderFlag true;
            }else{
                
$updateOrderFlag false;
            }
            
            
$tmp['prod_refid'] = $prod_refid;
            
$tmp['prod_itemno'] = $prod_itemno;
            
$tmp['new_price'] = $new_price;
            
$tmp['updateOrderFlag'] = $updateOrderFlag;
            
$tmp['org_price'] = $prod['sellingprice'];
            
$tmp['remark'] = $remark;
            
            
$cmd[] = $tmp;
            
            
$prod_row++;            
            
        }
    }
}

//vdump($cmd);


$dbh->beginTransaction();

//modify product selling price
$sql "UPDATE inv_product SET 
            sellingprice = :sellingprice,
            lastupby = :lastupby, 
            lastupdate = GETDATE()
        WHERE refid = :refid"
;
$sth $dbh->prepare($sql);    

$sql "SELECT
            dbo.ord_main.refid,
            dbo.ord_main.order_nbr,
            dbo.ord_main.custcode,
            dbo.ord_dtl.unitprice,
            dbo.ord_dtl.uom_cy,
            dbo.ord_dtl.refid AS orddtl_refid
        FROM
            dbo.ord_dtl
        INNER JOIN dbo.ord_main ON dbo.ord_dtl.main_refid = dbo.ord_main.refid
        LEFT JOIN dbo.ord_txpako ON dbo.ord_txpako.orddtl_refid = dbo.ord_dtl.refid
        WHERE
            dbo.ord_dtl.product_refid = :product_refid
        AND dbo.ord_dtl.uom_cy = :uom_cy    
        AND dbo.ord_txpako.invoiced_time IS NULL
        GROUP BY
            dbo.ord_main.refid,
            dbo.ord_main.order_nbr,
            dbo.ord_main.custcode,
            dbo.ord_dtl.unitprice,
            dbo.ord_dtl.uom_cy,
            dbo.ord_dtl.refid
        ORDER BY
            dbo.ord_main.order_nbr ASC"
;            
$sth2 $dbh->prepare($sql);

                

            
if( 
is_array($cmd) ){
    foreach(
$cmd as $task){
        if(empty(
$task['new_price'])){
            break;    
        }
        
        
$sql_param[':sellingprice'] = $task['new_price'];
        
$sql_param[':refid'] = $task['prod_refid'];
        
$sql_param[':lastupby'] = filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
        
        
updateProductPriceHistory($task['prod_refid'], $task['new_price'], falsenull$task['remark']);                            
        
        
$inv_product=getDB("inv_product"$task['prod_refid']);
        
//echo $sth->getSQL( $sql_param ).HTML_EOL;
        
$q $sth->execute$sql_param );
        
pdo_showerror($sth$q);
        
        
$msg[] = UPDATED.WS.PRODUCT.WS.PRICE.WS.$task['prod_itemno'].': '.numfs($task['org_price']).' -> '.numfs($task['new_price']);
        
        if(
$task['updateOrderFlag']){
            
//echo "Update order price".HTML_EOL;            
            
            
$q $sth2->execute(  array(':product_refid' => $task['prod_refid'], ':uom_cy' => $inv_product['uom_cy']) );
            
//echo $sth2->getSQL( array(':product_refid' => $task['prod_refid'], ':uom_cy' => $orddtl['uom_cy']) ).HTML_EOL;
            
pdo_showerror($sth2$q);
            while (
$row $sth2->fetch(PDO::FETCH_ASSOC) ){
                
update_orddtl_sellingprice($row['orddtl_refid'], $task['new_price']);
                
//vdump($row['orddtl_refid'], $task['new_price']);
                
$msg[] = UPDATED.WS.ORDER.WS.PRICE.WS.$row['order_nbr'].'('.generateJCBarcode($row['orddtl_refid']).'): '.numfs($row['unitprice']).' -> '.numfs($task['new_price']);
                            
                
            }
        }
    }
}else{
    
$msg[] = INVALID.WS.FILES;    
}
//echo implode(HTML_EOL, $msg);
//exit;
$dbh->commit();

$msg[] = '==='.FINISHED.'===';

if( 
is_array($msg) ){
    
$html_content implode(HTML_EOL$msg);
}
//unlink($f);
//echo "SAVED"; exit;
require "sys_console.php";
?>