From b66cd5e8f9d186a693098b6dcbac2274eed1fec4 Mon Sep 17 00:00:00 2001 From: adonis Date: Sat, 18 Jul 2026 16:38:46 +0330 Subject: [PATCH] fix profit value after each order --- app/Http/Controllers/Controller.php | 22 ++++++++++++++++ .../Customer/ProductController.php | 6 ++++- .../Pargar/PargarInboundController.php | 5 ++-- .../Single/SingleOrderController.php | 25 +++++++++++++++++++ app/Services/PargarSyncService.php | 4 +-- 5 files changed, 56 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 5bbb3c3..b27312b 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -476,6 +476,28 @@ class Controller extends BaseController return 'فروش ' . self::meltedGoldLabel(); } + /** + * شرح معامله از دید فروشگاه برای حسابداری (پرگار و …). + * direction اپ: 1 = خرید مشتری → فروش به مشتری | 2 = فروش مشتری → خرید از مشتری + * اپ مشتری همچنان از productBuyTitle / productSellTitle استفاده می‌کند. + */ + public static function accountingShopTradeDesc(int $customerDirection, bool $fromCart = false): string + { + if ($fromCart) { + return 'فروش سبد به مشتری'; + } + + if ($customerDirection === 1) { + return 'فروش به مشتری'; + } + + if ($customerDirection === 2) { + return 'خرید از مشتری'; + } + + return 'معامله'; + } + public static function meltedGoldDepositTitle(): string { return 'موجودی ' . self::meltedGoldLabel(); diff --git a/app/Http/Controllers/Customer/ProductController.php b/app/Http/Controllers/Customer/ProductController.php index 2293e66..1616088 100644 --- a/app/Http/Controllers/Customer/ProductController.php +++ b/app/Http/Controllers/Customer/ProductController.php @@ -158,9 +158,13 @@ class ProductController extends SuperController public function show($id){ $customer = auth('customer')->user(); $assetSummaryService = app(AssetSummaryService::class); - $query = Product::/*with(['walletMy as wallet'])->*/findOrFail($id); + $query = Product::with('file')->findOrFail($id); $wallet = $query->walletMy; + if ($query->relationLoaded('file') && $query->file) { + $query->file->setAttribute('full_src', $query->file->src); + } + // محصول متصل به کیف اصلی: موجودی مؤثر (کیف اصلی + legacy) — نه فقط ردیف product handle if (self::isMainWalletProduct((int) $id)) { $mainWalletGold = $assetSummaryService->productDisplayQuantity( diff --git a/app/Http/Controllers/Pargar/PargarInboundController.php b/app/Http/Controllers/Pargar/PargarInboundController.php index 48ef38c..2be29e2 100644 --- a/app/Http/Controllers/Pargar/PargarInboundController.php +++ b/app/Http/Controllers/Pargar/PargarInboundController.php @@ -176,6 +176,7 @@ class PargarInboundController extends Controller // direction اپ: 1=خرید مشتری، 2=فروش مشتری // OrderType پرگار از دید فروشگاه است: خرید مشتری = فروش فروشگاه (-1)، فروش مشتری = خرید فروشگاه (1) $isCustomerBuy = (int) ($order->direction ?? 1) === 1; + $customerDirection = (int) ($order->direction ?? 0); return [ 'OrderId' => (string) $order->id, @@ -192,7 +193,7 @@ class PargarInboundController extends Controller 'Weight' => (float) ($order->amount ?? 0), 'Price' => (float) ($order->total_price ?? 0), 'Carat' => 750, - 'Description' => (string) ($order->description ?? ''), + 'Description' => self::accountingShopTradeDesc($customerDirection, $this->isCartOrder($order)), ]; })->values(); @@ -481,7 +482,7 @@ class PargarInboundController extends Controller } return PargarSyncLog::where('order_id', $order->id) - ->where('desc', 'سبد خرید') + ->whereIn('desc', ['سبد خرید', 'فروش سبد به مشتری']) ->exists(); } } diff --git a/app/Http/Controllers/Single/SingleOrderController.php b/app/Http/Controllers/Single/SingleOrderController.php index adcabfb..0f5cdbc 100644 --- a/app/Http/Controllers/Single/SingleOrderController.php +++ b/app/Http/Controllers/Single/SingleOrderController.php @@ -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(); diff --git a/app/Services/PargarSyncService.php b/app/Services/PargarSyncService.php index 60dffa5..a2b19d7 100644 --- a/app/Services/PargarSyncService.php +++ b/app/Services/PargarSyncService.php @@ -144,9 +144,7 @@ class PargarSyncService } $direction = (int) ($order->direction ?? 0); - $desc = $fromCart - ? 'سبد خرید' - : ($direction === 1 ? 'خرید آبشده' : 'فروش آبشده'); + $desc = \App\Http\Controllers\Controller::accountingShopTradeDesc($direction, $fromCart); return PargarSyncLog::create([ 'customer_id' => $order->customer_id,