-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplete_task.php
171 lines (147 loc) · 6.64 KB
/
complete_task.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
<?php
include 'access.php';
header('Content-Type: application/json');
session_start();
// Έλεγχος αν ο χρήστης είναι συνδεδεμένος και αν είναι rescuer
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'rescuer') {
echo json_encode(['success' => false, 'message' => 'User not authorized']);
exit();
}
// Λήψη δεδομένων από το αίτημα
$data = json_decode(file_get_contents('php://input'), true);
// Καταγραφή των δεδομένων που λαμβάνονται
error_log("Received data: " . json_encode($data));
// Έλεγχος αν τα δεδομένα είναι σωστά
if (!isset($data['id']) || !isset($data['type']) || !isset($data['quantity'])) {
echo json_encode(['success' => false, 'message' => 'Invalid input data']);
exit();
}
$id = $data['id'];
$type = $data['type'];
$quantity = intval($data['quantity']);
$now = date('Y-m-d H:i:s');
// Συνάρτηση για την ενημέρωση ή προσθήκη αντικειμένου στο rescuer.json
function updateOrAddItem(&$rescuerData, $id, $quantity, $userId, $itemToAdd = null, $type) {
$itemFound = false;
error_log("Starting updateOrAddItem function with ID: $id, Quantity: $quantity, Type: $type");
foreach ($rescuerData['items'] as &$rescuerItem) {
error_log("Checking rescuer item: " . json_encode($rescuerItem));
if ($rescuerItem['id'] == $id && $rescuerItem['rescuerId'] == $userId) {
$itemFound = true;
foreach ($rescuerItem['details'] as &$detail) {
if ($detail['detail_name'] == 'Quantity') {
if ($type == 'request') {
if ($detail['detail_value'] < $quantity) {
error_log("Insufficient quantity: have " . $detail['detail_value'] . ", need $quantity");
return ['success' => false, 'message' => 'Insufficient quantity'];
} else {
$detail['detail_value'] -= $quantity;
error_log("Quantity after reduction: " . $detail['detail_value']);
}
} else if ($type == 'announcement') {
$detail['detail_value'] += $quantity;
error_log("Quantity after addition: " . $detail['detail_value']);
}
return ['success' => true];
}
}
}
}
if (!$itemFound && $type == 'announcement' && $itemToAdd) {
$itemToAdd['rescuerId'] = intval($userId); // Μετατροπή του rescuerId σε ακέραιο αριθμό
foreach ($itemToAdd['details'] as &$detail) {
if ($detail['detail_name'] == 'Quantity') {
$detail['detail_value'] = $quantity;
break;
}
}
$rescuerData['items'][] = $itemToAdd;
error_log("Added new item to rescuerData: " . json_encode($itemToAdd));
return ['success' => true];
}
error_log("Item not found for ID: $id and rescuerId: $userId");
return ['success' => false, 'message' => 'Item not found'];
}
// Συνάρτηση για την ολοκλήρωση του request
function completeRequest($id, $quantity, $now) {
$jsonFile = 'requests.json';
$rescuerFile = 'rescuer.json';
$tasks = json_decode(file_get_contents($jsonFile), true);
$rescuerData = json_decode(file_get_contents($rescuerFile), true);
// Εύρεση του itemId από το requests.json
$itemId = null;
foreach ($tasks as $task) {
if ($task['request_id'] == $id && $task['rescuer_id'] == $_SESSION['user_id']) {
$itemId = $task['item_id'];
break;
}
}
if (!$itemId) {
error_log("Request ID not found or not associated with rescuer ID: $id");
return ['success' => false, 'message' => 'Request ID not found'];
}
$response = updateOrAddItem($rescuerData, $itemId, $quantity, $_SESSION['user_id'], null, 'request');
if ($response['success']) {
foreach ($tasks as &$task) {
if ($task['request_id'] == $id) {
if ($task['status'] == 'Completed') {
return ['success' => false, 'message' => 'Request already completed'];
}
$task['status'] = 'Completed';
$task['completed_at'] = $now;
break;
}
}
file_put_contents($jsonFile, json_encode($tasks, JSON_PRETTY_PRINT));
file_put_contents($rescuerFile, json_encode($rescuerData, JSON_PRETTY_PRINT));
return ['success' => true];
} else {
return $response;
}
}
// Συνάρτηση για την ολοκλήρωση της ανακοίνωσης
function completeAnnouncement($id, $now) {
$jsonFile = 'announcements.json';
$itemsFile = 'items.json';
$tasks = json_decode(file_get_contents($jsonFile), true);
$itemsData = json_decode(file_get_contents($itemsFile), true);
$quantity = 0;
foreach ($tasks as &$announcement) {
foreach ($announcement['items'] as &$item) {
if ($item['item_id'] == $id && empty($item['delivery_completion_date'])) {
$item['delivery_completion_date'] = $now;
$quantity = isset($item['quantity']) ? intval($item['quantity']) : 0;
break 2;
}
}
}
file_put_contents($jsonFile, json_encode($tasks, JSON_PRETTY_PRINT));
$itemToAdd = null;
foreach ($itemsData['items'] as $item) {
if ($item['id'] == $id) {
$itemToAdd = $item;
break;
}
}
if ($itemToAdd) {
$rescuerFile = 'rescuer.json';
$rescuerData = json_decode(file_get_contents($rescuerFile), true);
$response = updateOrAddItem($rescuerData, $id, $quantity, $_SESSION['user_id'], $itemToAdd, 'announcement');
if ($response['success']) {
file_put_contents($rescuerFile, json_encode($rescuerData, JSON_PRETTY_PRINT));
return ['success' => true];
} else {
return $response;
}
} else {
return ['success' => false, 'message' => 'Item not found in items.json'];
}
}
$response = ['success' => false, 'message' => 'Invalid task type'];
if ($type == 'request') {
$response = completeRequest($id, $quantity, $now);
} else if ($type == 'announcement') {
$response = completeAnnouncement($id, $now);
}
echo json_encode($response);
?>