| Server IP : 173.209.174.21 / Your IP : 216.73.216.89 Web Server : Apache/2.4.58 (Ubuntu) System : Linux wcfs-server 6.8.0-124-generic #124-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 13:00:45 UTC 2026 x86_64 User : nodor ( 1000) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/wcfs/NAOPOS/api/ |
Upload File : |
<?php
declare(strict_types=1);
require __DIR__ . '/db.php';
require __DIR__ . '/helpers.php';
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$businessDate = require_business_date($_GET['date'] ?? null);
$pdo = db();
$salesStmt = $pdo->prepare(
'SELECT id, business_date, payment_method, subtotal, tax, total, amount_tendered, change_due, sold_at
FROM sales
WHERE business_date = :business_date
ORDER BY sold_at ASC'
);
$salesStmt->execute(['business_date' => $businessDate]);
$salesRows = $salesStmt->fetchAll();
$itemsStmt = $pdo->prepare(
'SELECT sale_id, name, category, report_category, price, quantity, taxable, tax_rate, tax_amount, line_total
FROM sale_items
WHERE sale_id = :sale_id
ORDER BY id ASC'
);
$sales = [];
foreach ($salesRows as $row) {
$itemsStmt->execute(['sale_id' => $row['id']]);
$itemRows = $itemsStmt->fetchAll();
$sales[] = [
'id' => $row['id'],
'businessDate' => $row['business_date'],
'paymentMethod' => $row['payment_method'],
'subtotal' => (float) $row['subtotal'],
'tax' => (float) $row['tax'],
'total' => (float) $row['total'],
'amountTendered' => $row['amount_tendered'] !== null ? (float) $row['amount_tendered'] : null,
'changeDue' => $row['change_due'] !== null ? (float) $row['change_due'] : null,
'timestamp' => date(DATE_ATOM, strtotime($row['sold_at'])),
'items' => array_map(static function (array $item): array {
return [
'name' => $item['name'],
'category' => $item['category'],
'reportCategory' => $item['report_category'],
'price' => (float) $item['price'],
'quantity' => (int) $item['quantity'],
'taxable' => (bool) $item['taxable'],
'taxRate' => $item['tax_rate'] !== null ? (float) $item['tax_rate'] : null,
'taxAmount' => (float) $item['tax_amount'],
'lineTotal' => (float) $item['line_total'],
];
}, $itemRows),
];
}
json_response(['sales' => $sales]);
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
json_response(['error' => 'Method not allowed'], 405);
}
$payload = read_json_body();
$businessDate = require_business_date($payload['businessDate'] ?? null);
if (
!isset(
$payload['id'],
$payload['paymentMethod'],
$payload['subtotal'],
$payload['tax'],
$payload['total'],
$payload['timestamp'],
$payload['items']
) ||
!is_array($payload['items'])
) {
json_response(['error' => 'Missing required sale fields'], 400);
}
$pdo = db();
$pdo->beginTransaction();
try {
$saleStmt = $pdo->prepare(
'INSERT INTO sales (
id, business_date, payment_method, subtotal, tax, total, amount_tendered, change_due, sold_at
) VALUES (
:id, :business_date, :payment_method, :subtotal, :tax, :total, :amount_tendered, :change_due, :sold_at
)'
);
$saleStmt->execute([
'id' => $payload['id'],
'business_date' => $businessDate,
'payment_method' => $payload['paymentMethod'],
'subtotal' => $payload['subtotal'],
'tax' => $payload['tax'],
'total' => $payload['total'],
'amount_tendered' => $payload['amountTendered'] ?? null,
'change_due' => $payload['changeDue'] ?? null,
'sold_at' => date('Y-m-d H:i:s', strtotime((string) $payload['timestamp'])),
]);
$itemStmt = $pdo->prepare(
'INSERT INTO sale_items (
sale_id, name, category, report_category, price, quantity, taxable, tax_rate, tax_amount, line_total
) VALUES (
:sale_id, :name, :category, :report_category, :price, :quantity, :taxable, :tax_rate, :tax_amount, :line_total
)'
);
foreach ($payload['items'] as $item) {
$itemStmt->execute([
'sale_id' => $payload['id'],
'name' => $item['name'] ?? 'Item',
'category' => $item['category'] ?? 'Other',
'report_category' => $item['reportCategory'] ?? 'Other',
'price' => $item['price'] ?? 0,
'quantity' => $item['quantity'] ?? 1,
'taxable' => !empty($item['taxable']) ? 1 : 0,
'tax_rate' => $item['taxRate'] ?? null,
'tax_amount' => $item['taxAmount'] ?? 0,
'line_total' => $item['lineTotal'] ?? 0,
]);
}
$pdo->commit();
json_response(['ok' => true], 201);
} catch (Throwable $error) {
$pdo->rollBack();
json_response(['error' => $error->getMessage()], 500);
}