diff --git a/app/Http/Controllers/Single/SingleOrderController.php b/app/Http/Controllers/Single/SingleOrderController.php index 56aa6b5..cdd7320 100644 --- a/app/Http/Controllers/Single/SingleOrderController.php +++ b/app/Http/Controllers/Single/SingleOrderController.php @@ -52,15 +52,6 @@ class SingleOrderController extends Controller $transactionType = $data['direction'] == 1 ? "خرید" : "فروش"; $customer = Customer::find(auth('customer')->id() ?? ($data['customer_id'] ?? null)); - if ($min_sell = Setting::where('key', 'minimum_sell')->where('is_active', 1)->first()) { - if ((int) $data['direction'] === 2) { - $sellAmount = $this->resolveOrderAmount($data); - if ($sellAmount !== null && (float) $min_sell->value > $sellAmount) { - throw new UnprocessableEntityHttpException('حداقل میزان فروش ' . $min_sell->value . ' گرم میباشد'); - } - } - } - $data['first_buy'] = auth('customer')->id() && $customer && $customer->first_buy_order !== 1; if (auth('customer')->id()) { @@ -146,6 +137,8 @@ class SingleOrderController extends Controller $data['price_original'] = $data['product']['price'] + $data['product']['price_profit']; $data['total_price_original'] = $data['product']['amount'] * ($data['price_original'] ?? 0); + $this->assertMinimumSellCash($data); + // Check if the method is cash if ($data['method'] == "cash") { $response['sell'] = $this->processSellTransactionProduct($data, $wallet, $wallet_interface); @@ -186,6 +179,7 @@ class SingleOrderController extends Controller if (!$data['product']['open_sell']) { throw new UnprocessableEntityHttpException('خرید این محصول در این لحظه مقدور نمیباشد'); } + $this->assertMinimumSellCash($data); $response['sell'] = $this->processSellTransaction($data, $wallet, $wallet_interface); } } @@ -983,6 +977,61 @@ class SingleOrderController extends Controller return null; } + private function assertMinimumSellCash(array $data): void + { + $minSell = Setting::where('key', 'minimum_sell')->where('is_active', 1)->first(); + if (!$minSell) { + return; + } + + $minRial = (float) $minSell->value; + if ($minRial <= 0) { + return; + } + + $sellCash = $this->resolveSellOrderCashRial($data); + if ($sellCash === null || $sellCash >= $minRial) { + return; + } + + throw new UnprocessableEntityHttpException( + 'حداقل مبلغ فروش ' . number_format((int) round($minRial)) . ' ریال می‌باشد' + ); + } + + private function resolveSellOrderCashRial(array $data): ?float + { + $targetCash = $this->resolveTargetCash($data); + if ($targetCash !== null) { + return (float) $targetCash; + } + + if (!empty($data['product_id']) && isset($data['number'])) { + $product = $data['product'] ?? Product::find($data['product_id']); + if ($product) { + $unitPrice = (float) ($product->price + $product->price_profit); + $difference = (float) ($data['difference'] ?? 0); + $amount = (float) $data['number'] * (float) $product->amount; + $sellUnitPrice = max(0.0, $unitPrice - $difference); + + return $amount * $sellUnitPrice; + } + } + + if (isset($data['amount'])) { + $amount = (float) $data['amount']; + if ($amount > 0) { + $price = (float) ($data['price'] ?? 0); + $difference = (float) ($data['difference'] ?? 0); + $sellUnitPrice = max(0.0, $price - $difference); + + return $amount * $sellUnitPrice; + } + } + + return null; + } + private function resolveTargetCash(array $data): ?int { if (!isset($data['target_cash'])) {