fix some bugs 5

This commit is contained in:
2026-07-21 22:23:09 +03:30
parent 55ce5e83b0
commit 3f8c47b1fc
6 changed files with 215 additions and 36 deletions
+104 -22
View File
@@ -257,20 +257,13 @@ class BalanceReportService
public function resolveTransactionMetalId(mixed $transaction): int
{
$product = $this->transactionProduct($transaction);
$product = $this->resolveTransactionProduct($transaction);
if ($product !== null) {
$metalId = (int) ($product->metal_id ?? 0);
return $metalId > 0 ? $metalId : 1;
}
$walletProduct = $this->transactionWalletProduct($transaction);
if ($walletProduct !== null) {
$metalId = (int) ($walletProduct->metal_id ?? 0);
return $metalId > 0 ? $metalId : 1;
}
if ($this->isMainWalletMetalTransaction($transaction)) {
return $this->configService->mainWalletMetalId();
}
@@ -387,16 +380,11 @@ class BalanceReportService
public function resolveTransactionProductLabel(mixed $transaction): ?string
{
$product = $this->transactionProduct($transaction);
$product = $this->resolveTransactionProduct($transaction);
if ($product !== null && !empty($product->title)) {
return trim((string) $product->title);
}
$walletProduct = $this->transactionWalletProduct($transaction);
if ($walletProduct !== null && !empty($walletProduct->title)) {
return trim((string) $walletProduct->title);
}
if ($this->isMainWalletMetalTransaction($transaction)) {
$labels = $this->configService->mainWalletProductLabels();
@@ -487,17 +475,37 @@ class BalanceReportService
$metalId = $this->resolveTransactionMetalId($row);
$metalLabel = $this->metalLabel($metalId);
$currentTypeStr = trim((string) ($this->lineValue($row, 'type_str') ?? ''));
$displayType = $this->resolveTransactionTitleLabel($row)
?? $this->resolveTransactionDisplayTypeStr($row, $metalId);
if ($displayType === null && $this->shouldReplaceGenericTransactionLabel($currentTypeStr)) {
$displayType = $this->resolveTransactionDisplayTypeStr($row, $metalId);
$titleLabel = $this->resolveTransactionTitleLabel($row);
$productAware = $this->resolveTransactionDisplayTypeStr($row, $metalId);
$product = $this->resolveTransactionProduct($row);
$productTitle = $this->resolveTransactionProductLabel($row);
$productId = (int) ($this->lineValue($row, 'product_id') ?? 0);
if ($productId <= 0 && $product !== null) {
$productId = (int) ($product->id ?? 0);
}
// عنوان ژنریک مثل «خرید طلا» را با «خرید {نام محصول}» عوض کن
if ($productAware !== null && (
$titleLabel === null
|| $this->shouldReplaceGenericTransactionLabel($titleLabel)
|| $this->shouldReplaceGenericTransactionLabel($currentTypeStr)
)) {
$displayType = $productAware;
} else {
$displayType = $titleLabel;
}
$productPayload = $this->productTablePayload($product);
if (is_array($row)) {
$row['metal_gold'] = $metalMap;
$row['transaction_metal_id'] = $metalId;
$row['transaction_metal_label'] = $metalLabel;
$row['product_id'] = $productId > 0 ? $productId : null;
$row['product_title'] = $productTitle;
if ($productPayload !== null && empty($row['product'])) {
$row['product'] = $productPayload;
}
if ($displayType !== null) {
$row['type_str'] = $displayType;
}
@@ -512,6 +520,11 @@ class BalanceReportService
$row->metal_gold = $metalMap;
$row->transaction_metal_id = $metalId;
$row->transaction_metal_label = $metalLabel;
$row->product_id = $productId > 0 ? $productId : null;
$row->product_title = $productTitle;
if ($product instanceof Product && empty($this->lineValue($row, 'product'))) {
$row->setRelation('product', $product);
}
if ($displayType !== null) {
$row->setAttribute('type_str', $displayType);
}
@@ -523,11 +536,11 @@ class BalanceReportService
private function transactionProduct(mixed $transaction): ?object
{
if (is_object($transaction) && isset($transaction->product)) {
if (is_object($transaction) && isset($transaction->product) && $transaction->product) {
return $transaction->product;
}
if (is_array($transaction) && isset($transaction['product'])) {
if (is_array($transaction) && isset($transaction['product']) && $transaction['product']) {
$product = $transaction['product'];
return is_object($product) ? $product : (object) $product;
@@ -536,6 +549,54 @@ class BalanceReportService
return null;
}
private function resolveTransactionProduct(mixed $transaction): ?object
{
$product = $this->transactionProduct($transaction);
if ($product !== null && ! empty($product->title ?? null)) {
return $product;
}
$walletProduct = $this->transactionWalletProduct($transaction);
if ($walletProduct !== null && ! empty($walletProduct->title ?? null)) {
return $walletProduct;
}
$productId = (int) ($this->lineValue($transaction, 'product_id') ?? 0);
if ($productId > 0) {
return Product::withTrashed()
->select(['id', 'title', 'group_type', 'unit', 'direct_price', 'amount', 'trade_by_quantity', 'metal_id', 'file_id'])
->find($productId);
}
return $product ?? $walletProduct;
}
/**
* @return array<string, mixed>|null
*/
private function productTablePayload(?object $product): ?array
{
if ($product === null) {
return null;
}
$id = (int) ($product->id ?? 0);
if ($id <= 0) {
return null;
}
return [
'id' => $id,
'title' => $product->title ?? null,
'group_type' => $product->group_type ?? null,
'unit' => $product->unit ?? null,
'amount' => $product->amount ?? null,
'trade_by_quantity' => (bool) ($product->trade_by_quantity ?? false),
'direct_price' => $product->direct_price ?? null,
'metal_id' => isset($product->metal_id) ? (int) $product->metal_id : null,
];
}
private function transactionWalletProduct(mixed $transaction): ?object
{
$wallet = $this->lineValue($transaction, 'wallet');
@@ -614,6 +675,7 @@ class BalanceReportService
$productId = (int) ($product->id ?? 0);
}
$productTitle = $product !== null ? trim((string) ($product->title ?? '')) : '';
$productPayload = $this->productTablePayload($product);
$storedSource = trim((string) ($this->lineValue($row, 'fee_source') ?? ''));
$storedLabel = trim((string) ($this->lineValue($row, 'fee_source_label') ?? ''));
@@ -661,6 +723,9 @@ class BalanceReportService
foreach ($payload as $key => $value) {
$row[$key] = $value;
}
if ($productPayload !== null && empty($row['product'])) {
$row['product'] = $productPayload;
}
return;
}
@@ -669,6 +734,13 @@ class BalanceReportService
foreach ($payload as $key => $value) {
$row->{$key} = $value;
}
if ($productPayload !== null && empty($this->lineValue($row, 'product'))) {
if ($product instanceof Product) {
$row->setRelation('product', $product);
} else {
$row->product = $productPayload;
}
}
}
}
@@ -751,7 +823,7 @@ class BalanceReportService
$productId = (int) ($this->lineValue($order, 'product_id') ?? 0);
if ($productId > 0) {
$byId = Product::query()
$byId = Product::withTrashed()
->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price'])
->find($productId);
if ($byId) {
@@ -773,6 +845,16 @@ class BalanceReportService
if ($product) {
return is_object($product) ? $product : (object) $product;
}
$txProductId = (int) ($this->lineValue($tx, 'product_id') ?? 0);
if ($txProductId > 0) {
$byTx = Product::withTrashed()
->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price'])
->find($txProductId);
if ($byTx) {
return $byTx;
}
}
}
return null;