This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple_price_csv_parser.php
168 lines (139 loc) · 5.04 KB
/
multiple_price_csv_parser.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
<?php
/*
The MIT License (MIT)
Copyright (c) 2014-2015 Ng Simon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The program objective:
1. skip the empty sku line
2. add group price for the remaing rows automatically with calculation based on Kinki's formula
*/
function output_browser($context, $flag = true) {
// $flag = ($flag === undefined) ? true : $flag;
if($flag) {
echo $context;
}
}
/*
Row processing
skip row with empty sku column
return row data with sku
echo is for screen preview
*/
function skip_col_and_row($data, $flag) {
$output_row = array(); // output
// skip_column
$skip_column = array(1,2); // name and brand in price edit
$num = count($data);
// $num = 1; // display only first column
for ($c=0; $c < $num; $c++) {
// skip row if first column is empty
// if($data[0] != ""){
if($data[0] != ""){
// var_dump($data[$c]);
// var_dump(count($data[$c]));
// skip all predefined columns in skip_column array
if( !in_array($c, $skip_column, true ) ){
// put column into array
$output_row[] = $data[$c];
output_browser( $data[$c], $flag );
// if it is not the last column, output separator
if( $c !== $num-1 ) {
output_browser(",", $flag);
}
else {
// echo "\n";
output_browser("<br>", $flag);
} // else
}
} // if
} // for
return $output_row;
}
function remap_data($data, $row_no, $flag = true) {
global $fp;
// if($row_no > 5) return;
$output_row = array();
$tmp = $data;
$num = count($data);
$limit = 7; //(0 to $limit-1)
// var_dump($num);
// var_dump($data);
for ($c=0; $c < $num; $c++) {
// remap 5-8 column data into separate row
if( $row_no > 2 ) {
$data_line = array(
',,,'.$tmp[5].',2,all',
',,,'.$tmp[6].',3,all',
',,,'.$tmp[7].',4,all',
',,,'.$tmp[8].',5,all'
);
$data[6] = 1;
} else {
// additional row header
// used row 5 as group heading, without copy and paste again in excel
$data[5] = "_group_price_website";
$data[6] = "status";
}
// echo "row= ".$row_no." c= ".$c."<br>";
// var_dump($data[0]);
if($data[0] != "" && $c < $limit){
// individual row checking, since it is a loop, need to check for exact running $column
if($c === 3 && $data[$c] == "") {$data[3] = 999; } // if price is not defined, set to 999
if($c === 6 && $data[3] == 999) {$data[6] = 2; } // if price is not defined, set to 999
if($c === 5 && $row_no > 2) {$data[5] = "all"; }
echo $data[$c];
$output_row[] = $data[$c];
// if it is not the last column, output separator
// if( $c !== $num-1 ) {
if( $c !== $limit - 1 ) {
echo ",";
}
else {
echo "<br>";
fputcsv($fp, $output_row);
// put the new row into an array for output
if( $row_no > 2 ) {
foreach($data_line as $line){
$line = str_replace("\"", "", $line);
$val = explode(",",$line);
// output line to browser
$total_col = count($val);
for($ac=0; $ac<$total_col; $ac++) {
echo $val[$ac];
if( $ac !== $total_col-1 ) {
echo ",";
} else {
echo "<br>";
fputcsv($fp, $val);
}
}
} // foreach
}
} // else
} // if
} // for
return $output_row;
}
// write
$source = "price_data";
$output = $source . "_import_prepared_test";
$fp = fopen($output.".csv", 'w');
// read
$row = 1;
if (($handle = fopen($source.".csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",", '"')) !== FALSE) {
$num = count($data);
// write the just read data to file
// do something for the $data
if($row > 1){ // skip first row
$data = skip_col_and_row($data, false);
$data = remap_data($data, $row);
}
$row++;
}
fclose($handle);
fclose($fp);
}
?>