62 lines
2.1 KiB
PHP
62 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1\Resident;
|
|
|
|
use App\Http\Controllers\Api\V1\ApiController;
|
|
use App\Models\QrPurchaseOrder;
|
|
use App\Models\PartnerStore;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class QrPurchaseController extends ApiController
|
|
{
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$orders = QrPurchaseOrder::with(['store.storePartnerProfile'])
|
|
->where('resident_id', $request->user()->id)
|
|
->orderByDesc('created_at')
|
|
->get();
|
|
|
|
return $this->ok($orders);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'store_id' => 'required|exists:partner_stores,uuid',
|
|
'quantity' => 'sometimes|integer|min:1|max:100',
|
|
'payment_method' => 'sometimes|string|in:cash,online',
|
|
]);
|
|
|
|
$partnerStore = PartnerStore::where('uuid', $request->store_id)->firstOrFail();
|
|
|
|
// Enforce LGU constraint: Resident and Store must belong to the same tenant
|
|
if ($partnerStore->tenant_id !== $request->user()->tenant_id) {
|
|
return $this->fail('The selected store does not belong to your LGU.', null, 403);
|
|
}
|
|
|
|
$quantity = (int) $request->input('quantity', 1);
|
|
$mop = $request->input('payment_method', 'cash');
|
|
|
|
// Check LGU specific price or fallback to system config
|
|
$tenant = $request->user()->tenant;
|
|
$pricePerCode = $tenant && $tenant->qr_retail_price_centavos > 0
|
|
? $tenant->qr_retail_price_centavos
|
|
: config('qr.default_retail_price_per_code_centavos', 1000);
|
|
|
|
$amount = ($quantity * $pricePerCode) / 100.0;
|
|
|
|
$order = QrPurchaseOrder::create([
|
|
'resident_id' => $request->user()->id,
|
|
'store_id' => $partnerStore->owner_user_id,
|
|
'tenant_id' => $request->user()->tenant_id,
|
|
'status' => 'pending_payment',
|
|
'quantity' => $quantity,
|
|
'payment_method' => $mop,
|
|
'amount' => $amount,
|
|
]);
|
|
|
|
return $this->created($order->load('store.storePartnerProfile'), 'QR Reservation created successfully');
|
|
}
|
|
}
|