49 lines
2.1 KiB
PHP
49 lines
2.1 KiB
PHP
<?php
|
|
// food/admin/merchant_create.php — إنشاء مطعم جديد بحالة pending_approval + حساب مالكه
|
|
require_once __DIR__ . '/../connect_admin.php';
|
|
|
|
requireFoodFields(['name_ar', 'city', 'address', 'latitude', 'longitude', 'owner_name', 'owner_phone', 'owner_password']);
|
|
|
|
$nameAr = filterRequest('name_ar');
|
|
$nameEn = filterRequest('name_en');
|
|
$city = filterRequest('city');
|
|
$address = filterRequest('address');
|
|
$lat = filterRequest('latitude', 'float');
|
|
$lng = filterRequest('longitude', 'float');
|
|
$category = filterRequest('category');
|
|
$commission = filterRequest('commission_percent', 'float') ?? (float)(getenv('FOOD_COMMISSION_PERCENT') ?: 15);
|
|
|
|
$ownerName = filterRequest('owner_name');
|
|
$ownerPhone = normalizePhone(filterRequest('owner_phone'));
|
|
$ownerPassword = filterRequest('owner_password');
|
|
|
|
if (strlen($ownerPassword) < 8) jsonError('owner_password must be at least 8 characters');
|
|
|
|
$dupSt = $food_con->prepare("SELECT id FROM food_merchant_users WHERE phone=? LIMIT 1");
|
|
$dupSt->execute([$ownerPhone]);
|
|
if ($dupSt->fetch()) jsonError('A merchant account already uses this phone', 409);
|
|
|
|
$food_con->beginTransaction();
|
|
try {
|
|
$food_con->prepare(
|
|
"INSERT INTO food_merchants
|
|
(name_ar, name_en, city, address, latitude, longitude, category, commission_percent, status)
|
|
VALUES (?,?,?,?,?,?,?,?,'pending_approval')"
|
|
)->execute([$nameAr, $nameEn, $city, $address, $lat, $lng, $category, $commission]);
|
|
|
|
$merchantId = (int)$food_con->lastInsertId();
|
|
|
|
$food_con->prepare(
|
|
"INSERT INTO food_merchant_users (merchant_id, name, phone, role, password_hash)
|
|
VALUES (?,?,?,'owner',?)"
|
|
)->execute([$merchantId, $ownerName, $ownerPhone, password_hash($ownerPassword, PASSWORD_DEFAULT)]);
|
|
|
|
$food_con->commit();
|
|
} catch (Throwable $e) {
|
|
$food_con->rollBack();
|
|
appLog('[FOOD][ADMIN][merchant_create] ' . $e->getMessage(), 'ERROR');
|
|
jsonError('Failed to create merchant', 500);
|
|
}
|
|
|
|
jsonSuccess(['merchant_id' => $merchantId, 'status' => 'pending_approval'], 'Merchant created — awaiting approval');
|