151 lines
5.5 KiB
PHP
151 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\DB\Te\TableEngine;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ConditionalOrder;
|
|
use App\Models\Customer;
|
|
use App\Models\Product;
|
|
use App\Services\ConditionalOrderService;
|
|
use App\Services\ConfigService;
|
|
use App\Support\ConditionalOrderValidation;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
class ConditionalOrderController extends Controller
|
|
{
|
|
public function table(Request $request)
|
|
{
|
|
$query = ConditionalOrder::query()->with(['customer', 'product']);
|
|
|
|
TableEngine::create($query, $request->all())
|
|
->initSearch(['customer_id', 'product_id', 'status', 'direction', 'price_condition'])
|
|
->select(['*'])
|
|
->export($query);
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$data = $this->validatedPayload($request);
|
|
$customer = Customer::findOrFail($data['customer_id']);
|
|
|
|
$service = app(ConditionalOrderService::class);
|
|
if (! app(ConfigService::class)->isConditionalOrderEnabled()) {
|
|
throw new UnprocessableEntityHttpException('قابلیت سفارش شرطی سراسری خاموش است.');
|
|
}
|
|
if (! $customer->conditional_order_active) {
|
|
throw new UnprocessableEntityHttpException('سفارش شرطی برای این مشتری مجاز نیست.');
|
|
}
|
|
$service->assertWithinLimit($customer);
|
|
|
|
$fee = 0;
|
|
DB::beginTransaction();
|
|
try {
|
|
$fee = $service->chargeRegistrationFee($customer);
|
|
$order = ConditionalOrder::create([
|
|
...$data,
|
|
'is_active' => $data['is_active'] ?? true,
|
|
'status' => ConditionalOrder::STATUS_PENDING,
|
|
'fee_charged' => $fee,
|
|
]);
|
|
DB::commit();
|
|
} catch (\Throwable $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
|
|
return response()->json($order->load(['customer', 'product']), 201);
|
|
}
|
|
|
|
public function update(Request $request, int $id)
|
|
{
|
|
$order = ConditionalOrder::findOrFail($id);
|
|
$data = $this->validatedPayload($request, true);
|
|
unset($data['customer_id']);
|
|
$order->update($data);
|
|
|
|
return response()->json(true);
|
|
}
|
|
|
|
public function toggleActive(int $id)
|
|
{
|
|
$order = ConditionalOrder::findOrFail($id);
|
|
if ($order->status !== ConditionalOrder::STATUS_PENDING) {
|
|
throw new UnprocessableEntityHttpException('فقط سفارشهای در انتظار قابل فعال/غیرفعالسازی هستند.');
|
|
}
|
|
|
|
$order->is_active = ! $order->is_active;
|
|
$order->save();
|
|
|
|
return response()->json(['is_active' => (bool) $order->is_active]);
|
|
}
|
|
|
|
public function destroy(int $id)
|
|
{
|
|
ConditionalOrder::findOrFail($id)->delete();
|
|
|
|
return response()->json(true);
|
|
}
|
|
|
|
private function validatedPayload(Request $request, bool $partial = false): array
|
|
{
|
|
$rules = [
|
|
'customer_id' => ($partial ? 'sometimes|' : '') . 'required|integer|exists:customers,id',
|
|
'product_id' => ($partial ? 'sometimes|' : '') . 'required|integer|exists:products,id',
|
|
'direction' => ($partial ? 'sometimes|' : '') . 'required|integer|in:1,2',
|
|
'price_condition' => ($partial ? 'sometimes|' : '') . 'required|in:gte,lte',
|
|
'trigger_price' => ($partial ? 'sometimes|' : '') . 'required|numeric|min:1',
|
|
'amount' => 'nullable|numeric|min:0',
|
|
'number' => 'nullable|numeric|min:0',
|
|
'expires_at' => 'nullable|date',
|
|
'description' => 'nullable|string|max:2000',
|
|
'is_active' => 'sometimes|boolean',
|
|
];
|
|
|
|
$data = $request->validate(
|
|
$rules,
|
|
ConditionalOrderValidation::messages(),
|
|
ConditionalOrderValidation::attributes()
|
|
);
|
|
|
|
$productId = (int) ($data['product_id'] ?? $request->input('product_id') ?? 0);
|
|
$product = $productId > 0 ? Product::find($productId) : null;
|
|
|
|
$amount = isset($data['amount']) ? (float) $data['amount'] : null;
|
|
$number = isset($data['number']) ? (float) $data['number'] : null;
|
|
|
|
if (! $partial) {
|
|
if (($amount === null || $amount <= 0) && ($number === null || $number <= 0)) {
|
|
throw new UnprocessableEntityHttpException('مقدار یا تعداد را وارد کنید.');
|
|
}
|
|
}
|
|
|
|
if (! empty($data['expires_at'])) {
|
|
try {
|
|
$expires = \Illuminate\Support\Carbon::parse($data['expires_at']);
|
|
} catch (\Throwable $e) {
|
|
throw new UnprocessableEntityHttpException('زمان اعتبار تاریخ معتبری نیست.');
|
|
}
|
|
if ($expires->lte(now())) {
|
|
throw new UnprocessableEntityHttpException('زمان اعتبار باید بعد از الان باشد.');
|
|
}
|
|
$data['expires_at'] = $expires->format('Y-m-d H:i:s');
|
|
}
|
|
|
|
if ($product && $amount !== null && $amount > 0 && ($number === null || $number <= 0)) {
|
|
$unit = (float) ($product->amount ?? 0);
|
|
if ($unit > 0 && ! in_array(trim((string) ($product->group_type ?? '')), ['آبشده', 'کاتد مس'], true)
|
|
&& ! (bool) ($product->trade_by_quantity ?? false)) {
|
|
$data['number'] = $amount;
|
|
$data['amount'] = null;
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|