Joywork/cron/addObjectsToZipalOne.php

175 lines
6.4 KiB
PHP
Raw Permalink Normal View History

2026-05-22 20:21:54 +02:00
<?php
set_time_limit(0);
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Отправка объектов на Zipal.ru</title>
</head>
<body>
<?php
$sql = "SELECT * from zipal_objects WHERE sended = 0 AND unpublish = 0 and id=1837";
$rez_obj = mysql_query($sql);
while($obj = mysql_fetch_assoc($rez_obj)) {
$objectId = $obj['object_id'];
$res = Zipal::createObjectForZipal($mdb, $obj['object_id']);
$curUser = new User();
$curUser->get($obj['user_id']);
$requestToSend = [];
$login = $curUser->zipal_login;
$password = $curUser->zipal_password;
if (empty($login) || empty($password)) {
$login = $curUser->agencyZipalLogin;
$password = $curUser->agencyZipalPassword;
}
$requestToSend['login'] = $login;
$requestToSend['password'] = $password;
if ($obj['zipal_id'] == null || !$obj['zipal_id']) {
$requestToSend['method'] = 'CreateProperty';
$requestToSend['request'] = $res;
} else {
$requestToSend['method'] = 'UpdateProperty';
$requestToSend['request']['requestType'] = 'UpdatePropertyRequestType';
$requestToSend['request']['id'] = $obj['zipal_id'];
$requestToSend['request']['request'] = $res;
}
$request = json_encode(utf8ize($requestToSend));
//echo $request;
//echo "<br/>";
var_dump($requestToSend);
$user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; HTC_Touch_Diamond2_T5353; Windows Phone 6.5)';
//делаем запрос к zipal
$result = curl_request_zipal_json("https://zipal.ru/ws/json", $obj['user_id'], $user_agent, $request);
//echo $result;
$jsonResult = json_decode($result, true);
if (!$jsonResult) {
$jsonResult['resultCode'] = "FAIL";
$jsonResult['result'] = "NO RESPONSE";
}
var_dump($jsonResult);
if ($jsonResult['resultCode'] == "FAIL") {
$sql = "UPDATE zipal_objects SET error_text = '$result' WHERE id = $obj[id]";
mysql_query($sql);
if (empty($obj['error_text'])) {
if (empty($curUser->zipal_balance)) {
$newBalance = $curUser->agencyZipalBalance + $obj['price'];
$sql = "UPDATE users SET zipal_balance='$newBalance' WHERE id=" . $curUser->agencyId;
mysql_query($sql);
} else {
$newBalance = $curUser->zipal_balance + $obj['price'];
$sql = "UPDATE users SET zipal_balance='$newBalance' WHERE id=" . $curUser->id;
mysql_query($sql);
}
$sql = "UPDATE objects SET add_to_zipal='0' WHERE id=$objectId";
mysql_query($sql);
}
} else {
if ($jsonResult['result']['status'] === "ErrorPropertyResponseType") {
$sql = "UPDATE zipal_objects SET error_text = '$result' WHERE id = $obj[id]";
mysql_query($sql);
if (empty($obj['error_text'])) {
if (empty($curUser->zipal_balance)) {
$newBalance = $curUser->agencyZipalBalance + $obj['price'];
$sql = "UPDATE users SET zipal_balance='$newBalance' WHERE id=" . $curUser->agencyId;
mysql_query($sql);
} else {
$newBalance = $curUser->zipal_balance + $obj['price'];
$sql = "UPDATE users SET zipal_balance='$newBalance' WHERE id=" . $curUser->id;
mysql_query($sql);
}
$sql = "UPDATE objects SET add_to_zipal='0' WHERE id=$objectId";
mysql_query($sql);
}
} else {
if ($jsonResult['result']['status'] === "SuccessPropertyResponseType" ) {
if (isset($jsonResult['result']['id'])) {
$zipal_id = $jsonResult['result']['id'];
$sql = "UPDATE zipal_objects SET zipal_id = $zipal_id, error_text = NULL, sended = 1 WHERE id = $obj[id]";
mysql_query($sql);
} else {
$sql = "UPDATE zipal_objects SET error_text = NULL, sended = 1 WHERE id = $obj[id]";
mysql_query($sql);
}
if (!empty($obj['error_text'])) {
//если была ошибка перед этим - вычитаем деньги и ставим флаг в JW
if (empty($curUser->zipal_balance)) {
$newBalance = $curUser->agencyZipalBalance - $obj['price'];
$sql = "UPDATE users SET zipal_balance='$newBalance' WHERE id=" . $curUser->agencyId;
mysql_query($sql);
} else {
$newBalance = $curUser->zipal_balance - $obj['price'];
$sql = "UPDATE users SET zipal_balance='$newBalance' WHERE id=" . $curUser->id;
mysql_query($sql);
}
}
$sql = "UPDATE objects SET add_to_zipal='1' WHERE id=$objectId";
mysql_query($sql);
} else {
$sql = "UPDATE zipal_objects SET error_text = '$result' WHERE id = $obj[id]";
mysql_query($sql);
if (empty($obj['error_text'])) {
if (empty($curUser->zipal_balance)) {
$newBalance = $curUser->agencyZipalBalance + $obj['price'];
$sql = "UPDATE users SET zipal_balance='$newBalance' WHERE id=" . $curUser->agencyId;
mysql_query($sql);
} else {
$newBalance = $curUser->zipal_balance + $obj['price'];
$sql = "UPDATE users SET zipal_balance='$newBalance' WHERE id=" . $curUser->id;
mysql_query($sql);
}
$sql = "UPDATE objects SET add_to_zipal='0' WHERE id=$objectId";
mysql_query($sql);
}
}
}
}
}
function utf8ize($mixed) {
if (is_array($mixed)) {
foreach ($mixed as $key => $value) {
$mixed[$key] = utf8ize($value);
}
} else if (is_string ($mixed)) {
return iconv(mb_detect_encoding($mixed, mb_detect_order(), true), "UTF-8", $mixed);
}
return $mixed;
}
?>
</body>
</html>