-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNewPriceImport.php
More file actions
148 lines (134 loc) · 5.86 KB
/
NewPriceImport.php
File metadata and controls
148 lines (134 loc) · 5.86 KB
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
<?php
$dbconfig = require_once dirname(__FILE__)."/../app/config/parameters.php";
$mysqli = new mysqli(
$dbconfig['parameters']['database_host'],
$dbconfig['parameters']['database_user'],
$dbconfig['parameters']['database_password'],
$dbconfig['parameters']['database_name']
);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$prefix = $dbconfig['parameters']['database_prefix'];
$message = '';
$countUpdates = 0;
function floatvalue($val) {
$val = str_replace('.', '', $val);
$val = str_replace(',', '.', $val);
return floatval($val);
}
if (isset($_POST["upload"])) {
if ($_FILES['product_file']['name']) {
if(isset($_POST['seperator_selector'])){
$seperator = ";";
}else{
$seperator = ",";
}
$filename = explode(".", $_FILES['product_file']['name']);
if (end($filename) == "csv") {
$handle = fopen($_FILES['product_file']['tmp_name'], "r");
while ($data = fgetcsv($handle,0,$seperator)) {
$reference = mysqli_real_escape_string($mysqli, $data[0]);
$new_price_bad_format = mysqli_real_escape_string($mysqli, $data[1]);
$new_price = floatvalue($new_price_bad_format);
// SIMPLE PRODUCTS
$simpleProduct = "
UPDATE ".$prefix."product
SET price = $new_price
WHERE reference = '$reference' ";
$mysqli->query($simpleProduct);
$getProdInfo = "SELECT id_product FROM ".$prefix."product WHERE reference = '$reference' ";
if ($mysqli->affected_rows > 0) {
$countUpdates += 1;
$prodInfo = $mysqli->query($getProdInfo);
//Succesfully update a simple price now for multistores
while ($row = $prodInfo->fetch_assoc()) {
$tmpId = $row["id_product"];
$simpleProductMultistore = "
UPDATE ".$prefix."product_shop
SET price = $new_price
WHERE id_product = '$tmpId' ";
$mysqli->query($simpleProductMultistore);
}
}
// COMBINATION PRODUCTS
$combiIdProd = "SELECT id_product, id_product_attribute FROM ".$prefix."product_attribute WHERE reference = '$reference' ";
$idProd = $mysqli->query($combiIdProd)->fetch_assoc()["id_product"];
$getProdInfo = "SELECT price FROM ".$prefix."product WHERE id_product = '$idProd' ";
$SimplePrice = $mysqli->query($getProdInfo)->fetch_assoc()['price'];
if ($SimplePrice) {
$priceDif = $new_price - $SimplePrice;
if ($priceDif != 0) {
$combinationProduct = "
UPDATE ".$prefix."product_attribute
SET price = $priceDif
WHERE reference = '$reference' ";
$mysqli->query($combinationProduct);
if ($mysqli->affected_rows > 0) {
$idProdAttr = $mysqli->query($combiIdProd)->fetch_assoc()["id_product_attribute"];
$combinationProductShop = "
UPDATE ".$prefix."product_attribute_shop
SET price = $priceDif
WHERE id_product_attribute = '$idProdAttr' ";
$mysqli->query($combinationProductShop);
$countUpdates += 1;
}
}
}
}
$mysqli->close();
header("location: NewPriceImport.php?DONE=".(int)$countUpdates);
} else {
$message = '<label class="text-danger">Please Select CSV File only</label>';
}
} else {
$message = '<label class="text-danger">Please Select File</label>';
}
}
if (isset($_GET["DONE"])) {
$message = '<label class="text-success">Price Update Done!</label><br>'.'There where '.$_GET["DONE"].' prices changed.';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Update simple and combination prices on reference</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br/>
<div class="container">
<h2 align="center">Update simple and combination prices on reference<br><small class="text-muted">Comma seperated
csv file</small></h2>
<br/>
<form method="post" enctype='multipart/form-data'>
<p><label>Please Select a csv File</label>
<input type="file" name="product_file"/> <br>
<label>File is semicolon</label>
<input type="checkbox" name="seperator_selector" value="seperator_selector">
</p>
<br/>
<input type="submit" name="upload" class="btn btn-info" value="Upload"/>
</form>
<center>
<h1><?php echo $message; ?></h1>
</center>
<center style="padding-top: 250px;">
<div class="card" style="width: 18rem;">
<img src="https://inform-all.nl/img/logo.png" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Developed by</h5>
<p class="card-text"><a href="https://inform-all.nl">Inform-All</a></p>
<a href="https://www.paypal.me/buymecoffee" class="btn btn-primary">Donate</a>
</div>
<br><br>
V.1.1
</div>
</center>
</div>
</body>
</html>