fix profit value after each order 2

This commit is contained in:
2026-07-18 17:37:41 +03:30
parent b66cd5e8f9
commit 285a159762
8 changed files with 512 additions and 25 deletions
+89
View File
@@ -605,6 +605,31 @@ class BalanceReportService
$metalId = $this->resolveOrderMetalId($row);
$metalLabel = $this->metalLabel($metalId);
$price = (float) ($this->lineValue($row, 'price') ?? 0);
$fee = (float) ($this->lineValue($row, 'fee') ?? 0);
$totalFee = (float) ($this->lineValue($row, 'total_fee') ?? 0);
$direction = (int) ($this->lineValue($row, 'direction') ?? 0);
$productId = (int) ($this->lineValue($row, 'product_id') ?? 0);
if ($productId <= 0 && $product !== null) {
$productId = (int) ($product->id ?? 0);
}
$productTitle = $product !== null ? trim((string) ($product->title ?? '')) : '';
$storedSource = trim((string) ($this->lineValue($row, 'fee_source') ?? ''));
$storedLabel = trim((string) ($this->lineValue($row, 'fee_source_label') ?? ''));
$storedKey = trim((string) ($this->lineValue($row, 'fee_source_key') ?? ''));
$storedBreakdown = $this->lineValue($row, 'fee_breakdown');
if (is_string($storedBreakdown) && $storedBreakdown !== '') {
$decoded = json_decode($storedBreakdown, true);
$storedBreakdown = is_array($decoded) ? $decoded : null;
} elseif (! is_array($storedBreakdown)) {
$storedBreakdown = null;
}
$feeSource = $storedSource !== '' ? $storedSource : $this->resolveOrderFeeSourceKey($product);
$feeSourceLabel = $storedLabel !== ''
? $storedLabel
: $this->resolveOrderFeeSourceLabel($direction, $feeSource);
$feeSourceKey = $storedKey !== '' ? $storedKey : null;
$metalPrices = [];
foreach ($catalogMetalIds as $mid) {
@@ -618,6 +643,14 @@ class BalanceReportService
'order_metal_id' => $metalId,
'order_metal_label' => $metalLabel,
'metal_prices' => $metalPrices,
'product_id' => $productId > 0 ? $productId : null,
'product_title' => $productTitle !== '' ? $productTitle : null,
'fee_source' => $feeSource,
'fee_source_key' => $feeSourceKey,
'fee_source_label' => $feeSourceLabel,
'fee_breakdown' => $storedBreakdown,
'fee_display' => abs($fee),
'total_fee_display' => abs($totalFee),
];
foreach ($catalogMetalIds as $mid) {
@@ -639,6 +672,44 @@ class BalanceReportService
}
}
/** settings | product | app+product — هم‌تراز resolveCashPriceDiffMeta در SingleOrderController */
public function resolveOrderFeeSourceKey(?object $product = null): string
{
if ($product instanceof Product) {
if (app(ProductDisplayPriceService::class)->usesAppPrice($product)) {
return 'app+product';
}
return 'product';
}
$provider = $this->configService->priceProvider();
if (in_array($provider, ['zaarbox', 'main_product'], true)) {
return 'settings';
}
return 'product';
}
public function resolveOrderFeeSourceLabel(int $direction, ?string $feeSource = null): string
{
$source = $feeSource ?? $this->resolveOrderFeeSourceKey();
if ($source === 'app+product') {
return $direction === 2
? 'قیمت اپ (sell_fee) + تب اختلاف محصول'
: 'قیمت اپ (buy_fee) + تب اختلاف محصول';
}
if ($source === 'settings') {
return $direction === 2
? 'تنظیمات سراسری (sell_fee / fee_single)'
: 'تنظیمات سراسری (buy_fee / fee_single)';
}
return $direction === 2
? 'هزینه فروش محصول (cash_fee_sell)'
: 'هزینه خرید محصول (cash_fee_buy)';
}
public function resolveOrderAmountUnit(?object $product): string
{
if ($product === null) {
@@ -670,6 +741,24 @@ class BalanceReportService
private function resolveOrderProduct(mixed $order): ?object
{
$embedded = $this->lineValue($order, 'product');
if (is_object($embedded) && ! empty($embedded->title ?? null)) {
return $embedded;
}
if (is_array($embedded) && ! empty($embedded['title'])) {
return (object) $embedded;
}
$productId = (int) ($this->lineValue($order, 'product_id') ?? 0);
if ($productId > 0) {
$byId = Product::query()
->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price'])
->find($productId);
if ($byId) {
return $byId;
}
}
$transactions = $this->lineValue($order, 'transactions');
if ($transactions instanceof \Illuminate\Support\Collection) {
$transactions = $transactions->all();