fix profit value after each order

This commit is contained in:
2026-07-18 16:38:46 +03:30
parent 45f2d8bf70
commit b66cd5e8f9
5 changed files with 56 additions and 6 deletions
@@ -170,6 +170,8 @@ class SingleOrderController extends Controller
$data['difference'] = $this->resolveCashPriceDiff($config, $data['product'], 2);
$data['service_charge_percentage'] = $data['product']['percent_fee_sell'];
}
// fee اولیه از orderPriceDiffRial ممکن است با اختلاف واقعی محصول فرق کند
$data['fee'] = $data['difference'];
$data['total_fee'] = $data['amount'] * $data['difference'];
$data['product_id'] = $data['product']->id;
// Check if the user wants to buy
@@ -296,6 +298,7 @@ class SingleOrderController extends Controller
$this->assertProviderOrderAccepted($data);
$this->syncOrderProfitFields($data);
$data['order'] = $order = Order::create($data);
$abshode = $this->resolveAbshodeProductContext($data);
$productId = (int) ($data['product_id'] ?? $abshode['id'] ?? 0);
@@ -370,6 +373,7 @@ class SingleOrderController extends Controller
if (!$data['product']['open_buy']) {
throw new UnprocessableEntityHttpException('خرید این محصول در این لحظه مقدور نمیباشد');
}
$this->syncOrderProfitFields($data);
$data['order'] = $order = Order::create($data);
$data['transaction'][] = new TransactionInterface($wallet_interface, $_inp = [
"number" => $data['number'],
@@ -437,6 +441,7 @@ class SingleOrderController extends Controller
if (!$data['product']['open_buy']) {
throw new UnprocessableEntityHttpException('خرید این محصول در این لحظه مقدور نمیباشد');
}
$this->syncOrderProfitFields($data);
$data['order'] = $order = Order::create($data);
$paymentSellTitle = $paymentProduct
@@ -547,6 +552,7 @@ class SingleOrderController extends Controller
$this->assertProviderOrderAccepted($data);
$this->syncOrderProfitFields($data);
$data['order'] = $order = Order::create($data);
if ($usesProductWallet) {
@@ -607,6 +613,7 @@ class SingleOrderController extends Controller
if (!$data['product']['open_sell']) {
throw new UnprocessableEntityHttpException('فروش این محصول در این لحظه مقدور نمیباشد');
}
$this->syncOrderProfitFields($data);
$data['order'] = $order = Order::create($data);
$this->normalizeProductWalletGold($wallet_interface_product);
new TransactionInterface($wallet_interface_product, [
@@ -649,6 +656,7 @@ class SingleOrderController extends Controller
if (!$data['product']['open_sell']) {
throw new UnprocessableEntityHttpException('فروش این محصول در این لحظه مقدور نمیباشد');
}
$this->syncOrderProfitFields($data);
$data['order'] = $order = Order::create($data);
$data['transaction'][] = new TransactionInterface($wallet_interface, $_inp = [
"number" => $data['number'],
@@ -873,6 +881,7 @@ class SingleOrderController extends Controller
if ($wallet->cash < $data['total_price']) {
throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است');
}
$this->syncOrderProfitFields($data);
$order = Order::create($data);//buy
new TransactionInterface($wallet_interface, $_inp = [
"cash" => $data['total_price'],
@@ -889,6 +898,7 @@ class SingleOrderController extends Controller
if ($wallet->gold < $data['amount']) {
throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است');
}
$this->syncOrderProfitFields($data);
$order = Order::create($data);
new TransactionInterface($wallet_interface, $_inp = [
"gold" => $data['amount'],
@@ -1191,6 +1201,21 @@ class SingleOrderController extends Controller
return (int) (($product->price ?? 0) + ($product->price_profit ?? 0));
}
/**
* سود هر گرم / سود کل را از قیمت نهایی و قیمت بدون سود همگام کن
* (بعد از نهایی شدن amount و اعمال difference روی price).
*/
private function syncOrderProfitFields(array &$data): void
{
$price = (float) ($data['price'] ?? 0);
$priceOriginal = (float) ($data['price_original'] ?? 0);
$amount = (float) ($data['amount'] ?? 0);
$data['fee'] = abs($price - $priceOriginal);
$data['total_price_original'] = $amount * $priceOriginal;
$data['total_fee'] = $amount * $data['fee'];
}
private function bonusGift($customer, $referrer)
{
$bonus = Setting::where('key', 'referral_bonus_amount')->where('is_active', 1)->first();