-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
244 lines (209 loc) · 8.99 KB
/
api.php
File metadata and controls
244 lines (209 loc) · 8.99 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
require 'vendor/autoload.php';
require 'config.php';
use OdxProxy\Odx;
use OdxProxy\Model\KeywordRequest;
header('Content-Type: application/json');
try {
Odx::init([
'url' => ODOO_URL,
'db' => ODOO_DB,
'api_key' => ODOO_API_KEY,
'user_id' => 2,
'gateway_api_key' => ODX_API_KEY
]);
} catch (Exception $e) {
die(json_encode(['error' => 'Initialization failed: ' . $e->getMessage()]));
}
$action = $_GET['action'] ?? '';
try {
switch ($action) {
case 'fetchProducts':
$request = (new KeywordRequest())
->setFields(['id', 'name', 'list_price', 'default_code', 'qty_available'])
->setLimit(50)
->setContext([
'lang' => 'en_US',
'tz' => 'Asia/Jakarta',
'allowed_company_ids' => [1],
'default_company_id' => 1
]);
echo json_encode(Odx::searchRead('product.product', [], $request));
break;
case 'getSession':
// 1. Get Active Config
$configReq = (new KeywordRequest())->setFields(['id'])->setLimit(1)->setContext([
'lang' => 'en_US',
'tz' => 'Asia/Jakarta',
'allowed_company_ids' => [1],
'default_company_id' => 1
]);
$config = Odx::searchRead('pos.config', [["active", "=", true]], $configReq);
if (empty($config) || !isset($config[0]['id'])) {
throw new Exception("No active POS Configuration found or ID is missing.");
}
$configId = (int)$config[0]['id'];
// 2. Search for open session
$sessionReq = (new KeywordRequest())->setFields(['id', 'state'])->setLimit(1)->setContext([
'lang' => 'en_US',
'tz' => 'Asia/Jakarta',
'allowed_company_ids' => [1],
'default_company_id' => 1
]);
$session = Odx::searchRead('pos.session', [
['config_id', '=', $configId],
['state', 'in', ['opened', 'opening_control']]
], $sessionReq);
if (empty($session)) {
echo json_encode(['ok' => true, 'session_id' => null, 'config_id' => $configId]);
} else {
echo json_encode(['ok' => true, 'session_id' => $session[0]['id'], 'state' => $session[0]['state']]);
}
break;
case 'openStore':
$logs = [];
try {
$configId = isset($_GET['config_id']) ? (int)$_GET['config_id'] : 0;
if ($configId <= 0) {
throw new Exception("Config ID is missing or invalid.");
}
$sessionReqOS1 = (new KeywordRequest())->setFields(['id', 'state'])->setLimit(1)->setContext([
'lang' => 'en_US',
'tz' => 'Asia/Jakarta',
'allowed_company_ids' => [1],
'default_company_id' => 1
]);
$existing = Odx::searchRead('pos.session', [
['config_id', '=', $configId],
['state', '=', 'opening_control']
], $sessionReqOS1);
if (!empty($existing)) {
$finalSid = (int)$existing[0]['id'];
$logs[] = "Found existing opening session: " . $finalSid;
} else {
$sessionReqOS2 = (new KeywordRequest())->setContext([
'lang' => 'en_US',
'tz' => 'Asia/Jakarta',
'allowed_company_ids' => [1],
'default_company_id' => 1
]);
$sid = Odx::create('pos.session',
['config_id' => $configId, 'name' => 'PHP POS ' . date('H:i')]
, $sessionReqOS2);
$finalSid = is_array($sid) ? (int)$sid[0] : (int)$sid;
$logs[] = "New Session ID: " . $finalSid;
}
// STEP 3: Open the session
$logs[] = "Executing action_pos_session_open...";
$sessionReqOS3 = (new KeywordRequest())->setContext([
'lang' => 'en_US',
'tz' => 'Asia/Jakarta',
'allowed_company_ids' => [1],
'default_company_id' => 1
]);
Odx::call('pos.session', 'action_pos_session_open', [$finalSid], $sessionReqOS3);
echo json_encode(['ok' => true, 'session_id' => $finalSid, 'php_logs' => $logs]);
} catch (Exception $e) {
echo json_encode([
'ok' => false,
'error' => $e->getMessage(),
'php_logs' => $logs
]);
}
break;
case 'closeStore':
$sid = (int)$_GET['session_id'];
// 1. Set to closing_control
$sessionReqCS2 = (new KeywordRequest())->setContext([
'lang' => 'en_US',
'tz' => 'Asia/Jakarta',
'allowed_company_ids' => [1],
'default_company_id' => 1
]);
Odx::write('pos.session', [$sid], ['state' => 'closing_control'], $sessionReqCS2);
// 2. Trigger closing action
Odx::call('pos.session', 'action_pos_session_closing_control', [$sid], $sessionReqCS2);
echo json_encode(['ok' => true]);
break;
case 'createOrder':
// 1. Get the JSON data from the request body
$json = file_get_contents('php://input');
$data = json_decode($json, true);
$cart = $data['cart'] ?? [];
$sessionId = $data['session_id'] ?? null;
if (empty($cart) || !$sessionId) {
throw new Exception("Cart is empty or Session ID is missing.");
}
// 2. Prepare Order Lines and Calculate Total
$total = 0;
$orderLines = [];
foreach ($cart as $item) {
$price = (float)($item['list_price'] ?? 0);
$qty = (float)($item['qty'] ?? 1.0); // Now using the qty from JS
$subtotal = $price * $qty;
$total += $subtotal;
$orderLines[] = [0, 0, [
'product_id' => (int)$item['id'],
'qty' => $qty,
'price_unit' => $price,
'name' => $item['name'] ?? 'POS item',
'price_subtotal' => $subtotal,
'price_subtotal_incl' => $subtotal,
]];
}
$sessionPayment = (new KeywordRequest())->setFields(['name'])->setLimit(1)->setContext([
'lang' => 'en_US',
'tz' => 'Asia/Jakarta',
'allowed_company_ids' => [1],
'default_company_id' => 1
]);
$paymentMethod = Odx::searchRead('pos.payment.method', [
['active', '=', true]
], $sessionPayment);
// 3. Prepare Payment (Corrected PHP Array Syntax)
$payments = [[0, 0, [
'amount' => $total,
'payment_method_id' => $paymentMethod[0]["id"],
'payment_date' => date('Y-m-d H:i:s')
]]];
// 4. Build full Order Data
$orderData = [
'session_id' => (int)$sessionId,
'lines' => $orderLines,
'payment_ids' => $payments,
'amount_total' => $total,
'amount_paid' => $total,
'amount_tax' => 0,
'amount_return' => 0,
'state' => 'paid',
'name' => 'PHP POS Order ' . date('H:i:s')
];
// 5. Create the Order in Odoo
try {
$request = (new KeywordRequest())->setContext([
'lang' => 'en_US',
'tz' => 'Asia/Jakarta',
'allowed_company_ids' => [1],
'default_company_id' => 1
]);
$result = Odx::create('pos.order', $orderData, $request);
$orderId = is_array($result) ? ($result[0] ?? $result) : $result;
echo json_encode([
'ok' => true,
'order_id' => $orderId,
'total' => $total
]);
} catch (Exception $e) {
echo json_encode([
'ok' => false,
'error' => $e
]);
}
break;
default:
throw new Exception("Unknown action: $action");
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}