-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckUpdate.php
190 lines (175 loc) · 7.55 KB
/
checkUpdate.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
require_once "API/modulConnect.php";
require_once "API/alfaConnect.php";
require_once "API/dbConnect.php";
require_once "API/Middleware/Logs.php";
require_once "API/config.php";
//Connect to the database to check if there any uncompleted cheques
$connect = new dbConnect();
$mysqli = $connect->connect();
$query = $mysqli->query("SELECT `modul`.`status` as status, `modul`.`attempt` as attempt, `modul`.`id` as id,
`orders`.`orderId` as orderId
FROM `modul` JOIN `orders`
WHERE `status` <> 'COMPLETED' AND `orders`.`id` = `modul`.`idOrder`");
if ($query->field_count !== 0) { // If we have any uncompleted order than...
while ($getStat = $query->fetch_array(MYSQLI_ASSOC)) { // We select each item and check for updates on the Modul Kassa side
$status = $getStat['status'];
$attempt = $getStat['attempt'] + 1;
$id = $getStat['id'];
$orderId = $getStat['orderId'];
$modul = new modulConnect();
$newState = $modul->getChequeStatus($orderId);
$newStatus = $newState['status'];
$newTime = $newState['timeStatusChanged'];
if ($newStatus !== $status) { // If status changed we update an appropriate row in the table
$mysqli->query("UPDATE `modul` SET `status` = '$newStatus', `time` = '$newTime', `attempt` = $attempt
WHERE `id` = $id LIMIT 1");
}
}
} else {
$error = "ChequeUpdate script: There is no entries in DB 8(";
Logs::saveToLogs($error);
}
// Return message if we have an error
if ($mysqli->error) {
$error = "ChequeUpdate script: Error : " . $mysqli->error;
Logs::saveToLogs($error);
}
$mysqli->close();
// Check if status of the order has been changed during prescribed period of time
function getOrderStatus() : array
{
$changedStatus = [];
$mysqli = new dbConnect();
$db = $mysqli->connect();
$query = $db->query("SELECT `orderId`, `orderStatus`, `date`, `amount`, `expired` FROM `orders` WHERE `expired` = '0'");
while ($status = $query->fetch_assoc()) {
$orderTime = $status['date'] / 1000;
$currentTime = mktime();
$currentOrderStatus = (new alfaConnect())->getExtOrderStatus($status['orderId']);
$orderIdFromALfa = $currentOrderStatus['attributes'][0]['value'];
$statusFromAlfa = $currentOrderStatus['orderStatus'];
if ($currentTime - $orderTime < TIME_LIMIT * 24 * 3600) {
if ($statusFromAlfa != $status['orderStatus'] && $orderIdFromALfa === $status['orderId']) {
$changedStatus[$orderIdFromALfa] = $statusFromAlfa;
$db->query("UPDATE orders SET `orderStatus` = '$statusFromAlfa' WHERE orderId = '$orderIdFromALfa' LIMIT 1");
}
} elseif ($currentTime - $orderTime > TIME_LIMIT * 24 * 3600) {
echo "\n" .$orderIdFromALfa . "\n";
$db->query("UPDATE orders SET `expired` = '1' WHERE orderId = '$orderIdFromALfa' LIMIT 1");
}
}
$db->close();
return $changedStatus;
}
//Unique check number generator
function chequeId()
{
$firstPart = preg_replace("/[\.]{1,}/", "-", uniqid("", true));
$secondPart = explode(".", uniqid("1", true));
$lastPart = implode("-", array_map(function ($part) {
return str_shuffle($part);
}, $secondPart));
return $chequeId = $firstPart . "-" . $lastPart;
}
// Upload RETURN cheque onto Modul server
function chequeUpload($orderId, $taxMode = "SIMPLIFIED", $vatTag = "1105", $paymentMethod = "full_payment") {
$dbConn = new dbConnect();
$mysqli = $dbConn->connect();
$query = $mysqli->query("SELECT * FROM orders WHERE `orderId` = '$orderId' LIMIT 1");
$get = $query->fetch_array(MYSQLI_ASSOC);
if ($mysqli->error) {
$error = "chequeUpload: Error reading orders table: " . $mysqli->error;
Logs::saveToLogs($error);
$mysqli->close();
return null;
}
if($get['orderStatus'] == 2) {
$error = "chequeUpload: We can't print return cheque without returning money from the bank account.";
Logs::saveToLogs($error);
return false;
}
// Now we generate a new chequeId especially for ModulKassa
$chequeId = chequeId();
// Create json request for check upload
$jsonQuery = array (
"docNum" => $get['orderNumber'],
"docType" => "RETURN",
"checkoutDateTime" => date("c"),
"email" => $get['email'],
"printReceipt" => true,
"id" => $chequeId,
"taxMode" => $taxMode,
"inventPositions" => array ( array (
"name" => $get['orderName'],
"price" => ($get['itemAmount'] / $get['quantity'] )/ 100,
"quantity" => $get['quantity'],
"vatTag" => $vatTag,
"paymentObject" => "service",
"paymentMethod" => $paymentMethod,
) ),
"moneyPositions" => array (array (
"paymentType" => "CARD",
"sum" => $get['amount'] / 100,
)),
);
$idOrder = $get['id'];
$mysqli->close();
$cheque = json_encode($jsonQuery); // Convert to json
$upload = new modulConnect(); // Create new object
$response = $upload->uploadCheque($cheque); // Upload cheque onto Modul server
$dbConn = new dbConnect();
$status = $response['status'];
$time = $response['timeStatusChanged'];
$mysqli = $dbConn->connect();
$mysqli->query("INSERT INTO `returns` (`id`, `idOrder`, `status`, `time`, `chequeId`) VALUES (NULL, '$idOrder', '$status', '$time', '$chequeId')");
if ($mysqli->error) {
$error = "chequeUpload: Modul status storage in the database failed: " . $mysqli->error;
Logs::saveToLogs($error);
$mysqli->close();
return null;
}
$mysqli->close();
return true;
}
function sendReturnCheque(array $changedStatus)
{
foreach ($changedStatus as $orderId => $status) {
if ($status === 4 || $status === 3 || $status === 6){
chequeUpload($orderId);
}
}
}
// Run process of fiscalization of the RETURNED money
sendReturnCheque(getOrderStatus());
//Connect to the database to check if there any uncompleted cheques in RETURNS
$connect = new dbConnect();
$mysqli = $connect->connect();
$query = $mysqli->query("SELECT `returns`.`chequeId` as orderId, `returns`.`status` as status, `returns`.`attempt` as attempt, `returns`.`id` as id
FROM `returns`
WHERE `returns`.`status` <> 'COMPLETED'");
if ($query->field_count > 0) { // If we have any uncompleted order than...
while ($getStat = $query->fetch_array(MYSQLI_ASSOC)) { // We select each item and check for updates on the Modul Kassa side
$status = $getStat['status'];
$attempt = $getStat['attempt'] + 1;
$id = $getStat['id'];
$orderId = $getStat['orderId'];
$modul = new modulConnect();
$newState = $modul->getChequeStatus($orderId);
$newStatus = $newState['status'];
$newTime = $newState['timeStatusChanged'];
if ($newStatus !== $status) { // If status changed we update an appropriate row in the table
$mysqli->query("UPDATE `returns` SET `status` = '$newStatus', `time` = '$newTime', `attempt` = $attempt
WHERE `id` = $id LIMIT 1");
}
}
} else {
$error = "checkUpdate in a row: There is no entries in DB 8(";
Logs::saveToLogs($error);
}
// Return message if we have an error
if ($mysqli->error) {
$error = "checkUpdate in a row: Error : " . $mysqli->error;
Logs::saveToLogs($error);
}
$mysqli->close();