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
|
<?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_currencypricelist_import' && $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();
$currency_allowed = array('USD','CNY','EUR'); $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() <= 5 ) continue;//skip first 4 rows $rowIndex = $row->getRowIndex (); $array_data[$rowIndex] = array('A'=>'', 'B'=>'', 'G'=>'','H'=>''); foreach ($cellIterator as $cell) { if( 'A' == $cell->getColumn() || 'B' == $cell->getColumn() || 'G' == $cell->getColumn() || 'H' == $cell->getColumn() ){ $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue(); } } /* A:prod_refid int B:prod_itemno varchar G:new_currency char H:new_price float */
if( !empty($array_data[$rowIndex]['A']) && !empty($array_data[$rowIndex]['G']) && !empty($array_data[$rowIndex]['H']) ){ //not empty row $prod_refid = (int)filter_var($array_data[$rowIndex]['A'], FILTER_SANITIZE_STRING); $prod_itemno= filter_var($array_data[$rowIndex]['B'], FILTER_SANITIZE_STRING); $new_uom_cy = filter_var($array_data[$rowIndex]['G'], FILTER_SANITIZE_STRING); $new_price = (float)filter_var($array_data[$rowIndex]['H'], FILTER_SANITIZE_STRING); //validate product refid and itemno $prod = getDB("inv_product", $prod_refid); if($prod['itemno']==$prod_itemno && in_array(strtoupper($new_uom_cy), $currency_allowed)){ $tmp['prod_refid'] = $prod_refid; $tmp['prod_itemno'] = $prod_itemno; $tmp['new_uom_cy'] = $new_uom_cy; $tmp['org_uom_cy'] = $prod['uom_cy']; $tmp['new_price'] = $new_price; $tmp['org_price'] = $prod['sellingprice']; $cmd[] = $tmp; $prod_row++; } } }
//vdump($cmd); exit;
$dbh->beginTransaction();
//modify product selling price $sql = "UPDATE inv_product SET uom_cy = :uom_cy, sellingprice = :sellingprice, lastupby = :lastupby, lastupdate = GETDATE() WHERE refid = :refid"; $sth = $dbh->prepare($sql);
if( is_array($cmd) ){ foreach($cmd as $task){ if(empty($task['new_price']) || empty($task['new_uom_cy'])){ vdump('b'); break; } $sql_param[':uom_cy'] = $task['new_uom_cy']; $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'], false, $task['new_uom_cy'], $task['new_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'].': '.$task['org_uom_cy'].' '.numfs($task['org_price']).' -> '.$task['new_uom_cy'].' '.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"; ?>
|