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
+22
View File
@@ -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();
@@ -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(
@@ -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();
}
}
@@ -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();
+1 -3
View File
@@ -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,