-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbol_upload.php
146 lines (136 loc) · 5.3 KB
/
bol_upload.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
<?php
/*
* Read SQLite and find everything we need to send.
*/
require __DIR__ . "/core/init.php";
require __DIR__ . "/core/db.php";
require __DIR__ . "/core/bol.php";
require __DIR__ . "/core/strings.php";
$db = new core\Db(sprintf("sqlite:%s/db.sqlite", CACHE), "", "");
$now = time();
$del = 0;
// 0.Del prods in bol_del where tm_synced is null
foreach ($db->getAll("select * from bol_del where tm_synced is null") as $prod) {
if ($prod["bol_id"] === null) {
var_dump($prod);
user_error("bol_id is null.");
}
list($res, $head) = bol_http("DELETE", "/offers/".$prod["bol_id"], []);
if (! in_array($res["status"], ["PENDING", "SUCCESS"])) {
var_dump($res);
if (isset($res["violations"]) && $res["violations"][0]["name"] === "offer-id") {
echo "WARN: Skip invalid offerid.\n";
} else {
user_error("DELETE offer err.");
}
}
$db->exec("UPDATE `bol_del` SET `tm_synced` = ? WHERE `bol_id` = ?", [$now, $prod["bol_id"]]);
//$db->exec("DELETE from prods where ean = ?", [$prod["ean"]]);
if (VERBOSE) echo sprintf("bol_del %s\n", $prod["bol_id"]);
$del++;
ratelimit($head);
}
$added = 0;
// 1.Sync prods not in Bol
foreach ($db->getAll("select id, ean, title, calc_price_bol, price_me, price, stock from prods where bol_id is null and bol_pending is null and bol_error is null") as $prod) {
if (in_array($prod["stock"], [null])) die("ERR: Stock amount empty!");
if (intval($prod["stock"]) >= 1000) $prod["stock"] = 999;
$price = $prod["calc_price_bol"];
if (VERBOSE) echo sprintf("bol_add ean=%s stock=%s\n", $prod["ean"], $prod["stock"]);
list($res, $head) = bol_http("POST", "/offers", [
"ean" => $prod["ean"],
"condition" => [
"name" => "NEW"
],
"referenceCode" => $prod["id"],
"onHoldByRetailer" => false,
"unknownProductTitle" => $prod["title"],
"pricing" => [
"bundlePrices" => [[
"quantity" => "1",
"price" => $price
]]
],
"stock" => [
"amount" => $prod["stock"],
"managedByRetailer" => true
],
"fulfilment" => [
"type" => "FBR",
"deliveryCode" => "24uurs-20" // EDC=24uurs-23 but this change gives more space?
]
]);
if ($head["status"] !== 202) {
$db->exec("update prods set bol_error=? where id=?", [time(), $prod["id"]]);
var_dump($res);
if ($res["status"] === 401) die("FAIL");
ratelimit($head);
continue;
}
$stmt = $db->exec("update prods set bol_pending=? where id=?", [$res["id"], $prod["id"]]);
if ($stmt->rowCount() !== 1) {
user_error("ERR: Failed updating DB with ean=" . $prod["ean"]);
}
$added++;
ratelimit($head);
}
// 2.Sync prods that have a different stock amount or price compared to bol
// TODO: Horrible complex SQL-logic
$prods = $db->getAll("select calc_price_bol, bol_id, id, ean, title, price, stock, bol_stock, bol_price from prods where bol_id is not null");
$update = 0;
foreach ($prods as $prod) {
$bol_id = $prod["bol_id"];
// make them comparable
if (intval($prod["stock"]) >= 1000) $prod["stock"] = 999;
//$prod["bol_stock"] = intval($prod["bol_stock"])-5;
$prod["stock"] = bcsub($prod["stock"], "5", 0);
if ($prod["stock"] < 0) $prod["stock"] = "0";
if ($prod["bol_stock"] < 0) $prod["bol_stock"] = "0";
if ($prod["bol_stock"] !== $prod["stock"]) {
if (VERBOSE) echo sprintf("bol.stock_update %s %s=>%s\n", $prod["ean"], $prod["bol_stock"], $prod["stock"]);
list($res, $head) = bol_http("PUT", "/offers/$bol_id/stock", [
"amount" => $prod["stock"],
"managedByRetailer" => true
]);
if ($head["status"] !== 202) {
$db->exec("update prods set bol_error=? where id=?", [time(), $prod["id"]]);
var_dump($res);
ratelimit($head);
continue;
}
ratelimit($head);
$stmt = $db->exec("update prods set bol_pending=? where id=?", [$res["id"], $prod["id"]]);
if ($stmt->rowCount() !== 1) {
user_error("ERR: Failed updating DB with ean=" . $prod["ean"]);
}
$update++;
} else {
if (VERBOSE) echo sprintf("bol.stock same %s %s=>%s\n", $prod["ean"], $prod["bol_stock"], $prod["stock"]);
}
if ($prod["bol_price"] !== $prod["calc_price_bol"]) {
$bundle = [[
"quantity" => 1,
"price" => $prod["calc_price_bol"]
]];
if (VERBOSE) echo sprintf("bol.price_update %s %s=>%s\n", $prod["ean"], $prod["bol_price"], $prod["calc_price_bol"]);
list($res, $head) = bol_http("PUT", "/offers/$bol_id/price", [
"pricing" => ["bundlePrices" => $bundle]
]);
if ($head["status"] !== 202) {
$db->exec("update prods set bol_error=? where id=?", [time(), $prod["id"]]);
var_dump($res);
continue;
}
ratelimit($head);
$stmt = $db->exec("update prods set bol_pending=? where id=?", [$res["id"], $prod["id"]]);
if ($stmt->rowCount() !== 1) {
user_error("ERR: Failed updating DB with ean=" . $prod["ean"]);
}
$update++;
}
}
if (VERBOSE) {
echo "del=$del\n";
echo "added=$added\n";
echo "update=$update\n";
}