<?php
include "../../db.php";
$user_id = 0;
$_POST = (json_decode(file_get_contents('php://input'), true));
$currentDate = date("Y/m/d");
$backDate = urlencode(date('Y-m-d\TH:i:sP', strtotime('-0 day', strtotime($currentDate))));
$stat=0;
if (isset($_GET["status"])) {
$stat=$_GET["status"];
}
$storesStmt = $db->prepare("SELECT * FROM wildberries_store order by id desc");
if($storesStmt->execute()){
$storesResult = $storesStmt->get_result();
while($rowStore = $storesResult->fetch_object()){
$supplyStmt = $db->prepare("SELECT * FROM wildberries_supply where store_id = ? order by id desc");
$supplyStmt->bind_param("i", $rowStore->id);
$supplyStmt->execute();
$supplyResult = $supplyStmt->get_result();
$rowSupply = $supplyResult->fetch_object();
$deal = execRest("https://suppliers-api.wildberries.ru/api/v3/orders/new", $rowStore->api_key);
for ($i=0; $i < count($deal["orders"]); $i++) {
$code = $deal["orders"][$i]["id"];
$firstName = "";
$secondName = "";
$totalPrice = substr($deal["orders"][$i]["price"], 0, -2);
$currencyCode = $deal["orders"][$i]["currencyCode"];
$paymentMode = "wildberries";
$address = $deal["orders"][$i]["address"]["street"];
$town = $deal["orders"][$i]["address"]["city"];
$status = 0;
$phone = "";
$chrtId = $deal["orders"][$i]["chrtId"];
$warehouseId = $deal["orders"][$i]["warehouseId"];
$userStatus = 0;
echo "code: ".$code. "\n";
echo $deal["orders"][$i]["skus"][0]."\n";
echo $deal["orders"][$i]["supplyId"]."\n";
echo $deal["orders"][$i]["warehouseId"]."\n";
echo $deal["orders"][$i]["nmId"]."\n";
$sticker = "";
$stickerQr = execRestWithParams("https://suppliers-api.wildberries.ru/api/v3/orders/stickers?type=svg&width=58&height=40", array(
"orders" => array(
$code
)
), $rowStore->api_key);
$freeManager = 1;
$stock = execRestWithParams(
"https://suppliers-api.wildberries.ru/api/v3/stocks/".$deal["orders"][$i]["warehouseId"],
array(
"skus" => array(
$deal["orders"][$i]["skus"][0]
)
),
$rowStore->api_key
);
echo "<br>";echo "<br>";
print_r(json_encode($stock));
$amount = isset($stock["amount"]) ? $stock["amount"] : 0;
$managerCountStmt = $db->prepare("SELECT `manager_id`, count(manager_id) AS ct
FROM orders
WHERE id > 0
GROUP BY `manager_id`
HAVING COUNT(manager_id) > 0
ORDER BY ct ASC");
if($managerCountStmt->execute()){
$managerCountResult = $managerCountStmt->get_result();
$rowManagerCount = $managerCountResult->fetch_object();
if (isset($rowManagerCount->manager_id)) {
$freeManager = $rowManagerCount->manager_id;
} else {
$usersStmt = $db->prepare("SELECT * FROM users");
if($usersStmt->execute()) {
$usersResult = $usersStmt->get_result();
while($usersRow = $usersResult->fetch_object()){
$arr[] = array();
$arr = array("id"=>$usersRow->id);
}
for ($l=0; $l < count($arr); $l++) {
// echo "dop - ".$arr[$l];
}
}
}
echo "<br> free - $freeManager";echo "<br>";
}
$stocks = execRest("https://suppliers-api.wildberries.ru/api/v2/stocks?skip=0&take=10&search=".$deal["orders"][$i]["skus"][0], $rowStore->api_key);
// $cardResponse["result"]["cards"][0]["nomenclatures"][0]["nmId"]["variations"][0];
$color = "";
$photo = "";
// $cardNumber = $cardResponse["result"]["cards"][0]["nomenclatures"];
// for ($s=0; $s < count($cardNumber); $s++) {
// if ($cardNumber[$s]["variations"][0]["chrtId"] == $chrtId) {
// for ($p=0; $p < count($cardNumber[$s]["addin"]); $p++) {
// if ($cardNumber[$s]["addin"][$p]["type"] == "Фото") {
// $photo = $cardNumber[$s]["addin"][$p]["params"][0]["value"];
// } else {
// // $photo = "";
// }
// if ($cardNumber[$s]["addin"][$p]["type"] == "Основной цвет") {
// $color = $cardNumber[$s]["addin"][$p]["params"][0]["value"];
// } else {
// // $color = "";
// }
// }
// $nmId = 0;
// $nmId = $cardNumber[$s]["nmId"];
// break;
// } else {
// // $color = $cardResponse["result"]["cards"][0]["nomenclatures"][0]["addin"][1]["params"][0]["value"];
// // $photo = $cardResponse["result"]["cards"][0]["nomenclatures"][0]["addin"][2]["params"][0]["value"];
// }
// }
if (!isset($photo)) {
$photo = "";
}
if (!isset($color)) {
$color = "";
}
$orderProvider = 2;
// echo "<br>";
// echo "<br>";
// echo $nmId;
// echo "<br>";
// echo "<br>";
$orderStmt = $db->prepare("SELECT * FROM orders WHERE code = ? group by id DESC");
$orderStmt->bind_param("s", $deal["orders"][$i]["id"]);
if($orderStmt->execute()){
$orderResult = $orderStmt->get_result();
$rowOrder = $orderResult->fetch_object();
if (!isset($rowOrder->id)) {
$bar = $deal["orders"][$i]["skus"][0];
$sortBody = array(
"vendorCodes" => array(
$bar
)
);
$subOrderAnswer = execRestWithParams("https://suppliers-api.wildberries.ru/content/v1/cards/filter",
array(
"vendorCodes" => array(
$deal["orders"][$i]["article"]
)),
$rowStore->api_key);
$name="Нет названия";
// echo (json_encode($subOrderAnswer["data"][$i]["characteristics"]));
$color="";
for ($k=0; $k < count($subOrderAnswer["data"]); $k++) {
foreach ($subOrderAnswer["data"][$k]["mediaFiles"] as $key => $value) {
if(strlen($value) == 0) {
continue;
}
echo "got photo ".$value;
$photo = $value;
break;
}
// print_r($subOrderAnswer["data"][$i]["characteristics"]);
for ($j=0; $j < count($subOrderAnswer["data"][$k]["characteristics"]); $j++) {
foreach ($subOrderAnswer["data"][$k]["characteristics"][$j] as $key => $value) {
// print_r($value);
// print_r($key);
if($key == "Наименование") {
$name = $value;
}
}
}
}
$nmId = $deal["orders"][$i]["nmId"];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$offersStmt = $db->prepare("
INSERT INTO orders(code, firstName, lastName, price, paymentMethod, address, city, status, phone, manager_id, orderProvider, unique_id, barcode, sticker, nmId, stock, store_id, photo, color, chrtId, userStatus, currencyCode, qr, supply_id, warehouse_id)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?, ?)");
$offersStmt->bind_param("sssisssssiisssiiissiiisss",
$code,
$firstName,
$secondName,
$totalPrice,
$paymentMode,
$address,
$town,
$status,
$phone,
$freeManager,
$orderProvider,
$nmId,
$deal["orders"][$i]["skus"][0],
$phone,
$nmId,
$amount,//change this its stocks
$rowStore->id,
$photo,
$color,
$chrtId,
$userStatus,
$currencyCode,
$stickerQr["stickers"][0]["file"],
$nmId,
$warehouseId
);
if($offersStmt->execute()){
echo true;
addSubOrder($name, $code, $rowStore->api_key);
} else {
http_response_code(500);
die("Error: can't add your offer to database");
}
$photo="";
$nmId=0;
} else {
// if ($rowOrder->photo == "" || $rowOrder->photo == null) {
// }
echo "Dublicate order";
$bar =$deal["orders"][$i]["skus"][0];
// $subOrderAnswer = execRest("https://suppliers-api.wildberries.ru/api/v2/stocks?skip=0&take=10&search=$bar", $rowStore->api_key)["stocks"][0]["article"];
$sortBody = array(
"vendorCodes" => array(
$bar
)
);
$nmId = $deal["orders"][$i]["nmId"];
echo "art ". $deal["orders"][$i]["article"];
$stickQr = $stickerQr["stickers"][0]["file"];
// echo $stickQr;
$stick = "";
echo "sdddd\n\n\n";
// echo $stickQr;
// if(strlen($rowOrder->qr) != 0) {
// $stickQr = $rowOrder->qr;
// }
// if(strlen($rowOrder->sticker) != 0) {
// $stick = $rowOrder->sticker;
// }
$offersStmt = $db->prepare("
UPDATE orders SET status = ?, barcode = ?, sticker = ?, qr = ?, nmId = ?, stock = ?, photo = ?, color = ?, userStatus = ?, currencyCode = ?, article = ? WHERE code = ?");
$offersStmt->bind_param("sssssissiisi",
$status,
$bar,
$stickQr,
$stickQr,
$nmId, //real one
$amount,
$photo,
$color,
$userStatus,
$currencyCode,
$deal["orders"][$i]["article"],
$code,
);
if($offersStmt->execute()){
echo "updated!";
} else {
http_response_code(500);
die("Error: can't update your offer!");
}
}
$photo="";
$nmId = 0;
}
}
}
}
function addSubOrder($barcode, $ouid, $token) {
include "../../db.php";
$quantity = 1;
$subPrice = 0;
$subStmt = $db->prepare("
INSERT INTO subOrder(name, related_code, price, quantity) VALUES(?,?,?,?)");
$subStmt->bind_param("ssii",
$barcode,
$ouid,
$subPrice,
$quantity,
);
if($subStmt->execute()){
echo "Added suborder";
// return $subOrderAnswer;
} else {
echo $barcode;
http_response_code(500);
die("Error: can't add suborder to database");
return null;
}
}
function execRestWithParams($url, $params, $token) {
// echo json_encode($params);
$queryUrl = $url;
$queryData = http_build_query($params);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl,
CURLOPT_POSTFIELDS => json_encode($params, JSON_NUMERIC_CHECK)
));
$headers = array(
"Authorization: ".$token,
"Content-Type: application/json; charset=utf-8",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec($curl);
curl_close($curl);
return json_decode($res, true);
}
function execRest($url, $token) {
$queryUrl = $url;
// $queryData = http_build_query($params);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 0,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl,
// CURLOPT_POSTFIELDS => json_encode($params)
));
$headers = array(
"Authorization: ".$token,
"Content-Type: application/vnd.api+json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec($curl);
curl_close($curl);
return json_decode($res, true);
}
function execPostRest($url, $data, $token) {
$queryUrl = $url;
// $queryData = http_build_query($params);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl,
CURLOPT_POSTFIELDS => json_encode($data)
));
$headers = array(
"Authorization: ".$token,
"Content-Type: application/vnd.api+json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec($curl);
curl_close($curl);
return json_decode($res, true);
}
?>