fix trigger bug
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Models\Customer;
|
|||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
use App\Services\ConditionalOrderService;
|
use App\Services\ConditionalOrderService;
|
||||||
use App\Services\ConfigService;
|
use App\Services\ConfigService;
|
||||||
|
use App\Support\ConditionalOrderValidation;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||||
@@ -105,7 +106,11 @@ class ConditionalOrderController extends Controller
|
|||||||
'is_active' => 'sometimes|boolean',
|
'is_active' => 'sometimes|boolean',
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = $request->validate($rules);
|
$data = $request->validate(
|
||||||
|
$rules,
|
||||||
|
ConditionalOrderValidation::messages(),
|
||||||
|
ConditionalOrderValidation::attributes()
|
||||||
|
);
|
||||||
|
|
||||||
$productId = (int) ($data['product_id'] ?? $request->input('product_id') ?? 0);
|
$productId = (int) ($data['product_id'] ?? $request->input('product_id') ?? 0);
|
||||||
$product = $productId > 0 ? Product::find($productId) : null;
|
$product = $productId > 0 ? Product::find($productId) : null;
|
||||||
@@ -119,6 +124,18 @@ class ConditionalOrderController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)) {
|
if ($product && $amount !== null && $amount > 0 && ($number === null || $number <= 0)) {
|
||||||
$unit = (float) ($product->amount ?? 0);
|
$unit = (float) ($product->amount ?? 0);
|
||||||
if ($unit > 0 && ! in_array(trim((string) ($product->group_type ?? '')), ['آبشده', 'کاتد مس'], true)
|
if ($unit > 0 && ! in_array(trim((string) ($product->group_type ?? '')), ['آبشده', 'کاتد مس'], true)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use App\Models\Customer;
|
|||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
use App\Services\ConditionalOrderService;
|
use App\Services\ConditionalOrderService;
|
||||||
use App\Services\ConfigService;
|
use App\Services\ConfigService;
|
||||||
|
use App\Support\ConditionalOrderValidation;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||||
@@ -128,12 +129,16 @@ class ConditionalOrderController extends Controller
|
|||||||
'trigger_price' => ($partial ? 'sometimes|' : '') . 'required|numeric|min:1',
|
'trigger_price' => ($partial ? 'sometimes|' : '') . 'required|numeric|min:1',
|
||||||
'amount' => 'nullable|numeric|min:0',
|
'amount' => 'nullable|numeric|min:0',
|
||||||
'number' => 'nullable|numeric|min:0',
|
'number' => 'nullable|numeric|min:0',
|
||||||
'expires_at' => 'nullable|date|after:now',
|
'expires_at' => 'nullable|date',
|
||||||
'description' => 'nullable|string|max:2000',
|
'description' => 'nullable|string|max:2000',
|
||||||
'is_active' => 'sometimes|boolean',
|
'is_active' => 'sometimes|boolean',
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = $request->validate($rules);
|
$data = $request->validate(
|
||||||
|
$rules,
|
||||||
|
ConditionalOrderValidation::messages(),
|
||||||
|
ConditionalOrderValidation::attributes()
|
||||||
|
);
|
||||||
|
|
||||||
$amount = isset($data['amount']) ? (float) $data['amount'] : null;
|
$amount = isset($data['amount']) ? (float) $data['amount'] : null;
|
||||||
$number = isset($data['number']) ? (float) $data['number'] : null;
|
$number = isset($data['number']) ? (float) $data['number'] : null;
|
||||||
@@ -142,6 +147,18 @@ class ConditionalOrderController extends Controller
|
|||||||
throw new UnprocessableEntityHttpException('مقدار گرم یا تعداد را وارد کنید.');
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
$productId = (int) ($data['product_id'] ?? $request->input('product_id') ?? 0);
|
$productId = (int) ($data['product_id'] ?? $request->input('product_id') ?? 0);
|
||||||
$product = $productId > 0 ? Product::find($productId) : null;
|
$product = $productId > 0 ? Product::find($productId) : null;
|
||||||
if ($product && $amount !== null && $amount > 0 && ($number === null || $number <= 0)) {
|
if ($product && $amount !== null && $amount > 0 && ($number === null || $number <= 0)) {
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Support;
|
||||||
|
|
||||||
|
class ConditionalOrderValidation
|
||||||
|
{
|
||||||
|
public static function messages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'required' => ':attribute الزامی است.',
|
||||||
|
'integer' => ':attribute باید عدد صحیح باشد.',
|
||||||
|
'numeric' => ':attribute باید عدد باشد.',
|
||||||
|
'min.numeric' => ':attribute نباید کمتر از :min باشد.',
|
||||||
|
'min.string' => ':attribute نباید کمتر از :min کاراکتر باشد.',
|
||||||
|
'max.string' => ':attribute نباید بیشتر از :max کاراکتر باشد.',
|
||||||
|
'in' => ':attribute انتخابشده نامعتبر است.',
|
||||||
|
'boolean' => ':attribute باید صحیح یا غلط باشد.',
|
||||||
|
'date' => ':attribute تاریخ معتبری نیست.',
|
||||||
|
'after' => ':attribute باید بعد از الان باشد.',
|
||||||
|
'exists' => ':attribute انتخابشده معتبر نیست.',
|
||||||
|
'string' => ':attribute باید متن باشد.',
|
||||||
|
'nullable' => '',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function attributes(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'customer_id' => 'مشتری',
|
||||||
|
'product_id' => 'محصول',
|
||||||
|
'direction' => 'جهت معامله',
|
||||||
|
'price_condition' => 'شرط اجرا',
|
||||||
|
'trigger_price' => 'مظنه هدف',
|
||||||
|
'amount' => 'مقدار',
|
||||||
|
'number' => 'تعداد',
|
||||||
|
'expires_at' => 'زمان اعتبار',
|
||||||
|
'description' => 'توضیحات',
|
||||||
|
'is_active' => 'وضعیت فعال',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user