Demo Data Population

This commit is contained in:
Thanakorn
2026-08-14 14:10:31 +07:00
parent b2c437426b
commit dd48a8b96d
9 changed files with 2905 additions and 1 deletions
+134
View File
@@ -0,0 +1,134 @@
<?php
/**
* Demo Data Seeder — additional sales orders
* Run once after demo_seed.php + demo_seed_transactions.php: php demo_seed_more_orders.php
*
* demo_seed_transactions.php only creates 1 sales order (via the quotation ->
* order -> invoice cycle). This adds 4 more direct orders (no quotation) to
* give the Orders list page realistic volume and status variety:
* - 2 left in Draft (status=0) — never confirmed
* - 2 Confirmed (status=1) — stock-out auto-approved via FIFO bin selection
*
* All writes go through OrderManager (the same class the app itself uses),
* wrapped in dbTransaction(). Safe to re-run: guarded by order.notes markers.
*/
if (PHP_SAPI !== 'cli') {
http_response_code(403);
exit('Run via CLI only: php demo_seed_more_orders.php');
}
$_SESSION = [];
require __DIR__ . '/app/config.php';
require __DIR__ . '/app/dbconn.php';
require __DIR__ . '/app/assets/utils/db_helpers.php';
require __DIR__ . '/app/assets/utils/classes/WarehouseManager.php';
require __DIR__ . '/app/assets/utils/classes/StockManager.php';
require __DIR__ . '/app/assets/utils/classes/OrderManager.php';
function info(string $msg): void { echo "\033[36m •\033[0m {$msg}\n"; }
function ok(string $msg): void { echo "\033[32m ✓\033[0m {$msg}\n"; }
function skip(string $msg): void { echo "\033[33m –\033[0m {$msg}\n"; }
function buildLineItem(array $products, string $sku, float $qty): array {
$p = $products[$sku];
$unit_price = (float)$p['price'];
$total_price = round($qty * $unit_price, 4);
$tax_rate = 7.0;
$tax_amount = round($total_price * $tax_rate / 100, 4);
return [
'product_sku' => $sku,
'product_name' => $sku,
'quantity' => $qty,
'unit_price' => $unit_price,
'total_price' => $total_price,
'tax_amount' => $tax_amount,
'tax_rate' => $tax_rate,
];
}
echo "\n\033[1m=== mn3wms Demo Data Seeder — more sales orders ===\033[0m\n\n";
// ── Resolve shared context (company, warehouse, products, customers, dept) ────
$sth = $pdo1->prepare("SELECT company_id FROM company_list WHERE channel_name = 'demo' LIMIT 1");
$sth->execute();
$company_id = (int)$sth->fetchColumn();
if (!$company_id) { exit("ERROR: demo company not found — run demo_seed.php first.\n"); }
$sth = $pdo1->prepare("SELECT user_id FROM user WHERE username = 'admin' LIMIT 1");
$sth->execute();
$owner_user_id = (int)$sth->fetchColumn();
$sth = $pdo2->prepare("SELECT id FROM md_warehouse WHERE company_id = :c AND warehouse_name = 'Main Warehouse' LIMIT 1");
$sth->execute([':c' => $company_id]);
$main_wh = (int)$sth->fetchColumn();
$sth = $pdo2->prepare("SELECT id FROM md_department WHERE company_id = :c AND dept_code = 'SALES' LIMIT 1");
$sth->execute([':c' => $company_id]);
$sales_dept_id = (int)$sth->fetchColumn();
if (!$sales_dept_id) { exit("ERROR: SALES department not found — run demo_seed_transactions.php first.\n"); }
$sth = $pdo2->prepare("SELECT sku, price FROM md_product WHERE company_id = :c");
$sth->execute([':c' => $company_id]);
$products = $sth->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_UNIQUE);
$sth = $pdo2->prepare("SELECT id, contact_name FROM md_contact WHERE company_id = :c AND contact_type = 1 ORDER BY id");
$sth->execute([':c' => $company_id]);
$customers = $sth->fetchAll(PDO::FETCH_ASSOC);
$customer_ids = array_column($customers, 'id');
$logging = ['user_id' => $owner_user_id, 'dt' => date('Y-m-d H:i:s'), 'login' => null, 'action' => 'seed_more_orders'];
$orderMgmt = new OrderManager($pdo2, $company_id);
// ── Order definitions ──────────────────────────────────────────────────────
$order_defs = [
['marker' => 'Demo seed extra order 1', 'customer_idx' => 1, 'confirm' => false,
'items' => [['sku' => 'EL-001', 'qty' => 10], ['sku' => 'PK-002', 'qty' => 5]]],
['marker' => 'Demo seed extra order 2', 'customer_idx' => 2, 'confirm' => true,
'items' => [['sku' => 'BV-003', 'qty' => 20], ['sku' => 'OF-002', 'qty' => 8]]],
['marker' => 'Demo seed extra order 3', 'customer_idx' => 3, 'confirm' => true,
'items' => [['sku' => 'EL-004', 'qty' => 5], ['sku' => 'PK-003', 'qty' => 30]]],
['marker' => 'Demo seed extra order 4', 'customer_idx' => 0, 'confirm' => false,
'items' => [['sku' => 'OF-004', 'qty' => 10], ['sku' => 'BV-001', 'qty' => 15]]],
];
echo "--- Sales orders ---\n";
foreach ($order_defs as $d) {
$sth = $pdo2->prepare("SELECT id, status FROM td_order WHERE company_id = :c AND notes = :n LIMIT 1");
$sth->execute([':c' => $company_id, ':n' => $d['marker']]);
$existing = $sth->fetch(PDO::FETCH_ASSOC);
if ($existing) {
skip("{$d['marker']} already exists (id={$existing['id']}, status={$existing['status']})");
continue;
}
$customer_id = $customer_ids[$d['customer_idx']];
$items = array_map(
fn($it) => buildLineItem($products, $it['sku'], $it['qty']) + ['warehouse_id' => $main_wh],
$d['items']
);
$order_id = dbTransaction($pdo2, function ($pdo2) use ($orderMgmt, $customer_id, $sales_dept_id, $items, $logging, $d) {
// No 'source' -> direct WMS order -> starts as Draft (status=0) immediately.
$oid = $orderMgmt->saveOrder([
'id' => 0, 'contact_id' => $customer_id, 'department_id' => $sales_dept_id,
'order_date' => date('Y-m-d'), 'items' => $items, 'discount' => 0,
'shipping_fee' => 0, 'notes' => $d['marker'],
], $logging);
if ($d['confirm']) {
$orderMgmt->confirmOrder($oid, bin2hex(random_bytes(16)), $logging, true); // auto-approve stock-out
}
return $oid;
});
$state = $d['confirm'] ? 'confirmed (stock-out auto-approved)' : 'left as draft';
ok("Created order {$d['marker']} (id={$order_id}), {$state}");
}
echo "\n\033[1m=== Done ===\033[0m\n\n";