fixed kilo in metals 2

This commit is contained in:
2026-06-23 21:21:04 +03:30
parent 0bc690591f
commit 6c15ff1830
2 changed files with 137 additions and 24 deletions
+119
View File
@@ -2,6 +2,7 @@
namespace App\Services;
use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\Product;
@@ -248,4 +249,122 @@ class BalanceReportService
$buckets[$metalId]['gold_abs'] += $gold;
$buckets[$metalId]['cash_abs'] += $cash;
}
public function resolveTransactionMetalId(mixed $transaction): int
{
$product = $this->transactionProduct($transaction);
if ($product !== null) {
$metalId = (int) ($product->metal_id ?? 0);
return $metalId > 0 ? $metalId : 1;
}
return 1;
}
public function transactionGoldGrams(mixed $transaction): float
{
$gold = (float) ($this->lineValue($transaction, 'gold') ?? 0);
if (abs($gold) < 0.0000001) {
return 0.0;
}
$product = $this->transactionProduct($transaction);
if ($product !== null) {
return Controller::productLineWeightToGrams($product, $gold);
}
return Controller::weightToGrams($gold);
}
/**
* @param list<int> $catalogMetalIds
* @return array<string, float>
*/
public function buildTransactionMetalDisplayMap(mixed $transaction, array $catalogMetalIds): array
{
$map = [];
foreach ($catalogMetalIds as $metalId) {
$map[(string) (int) $metalId] = 0.0;
}
$type = (int) ($this->lineValue($transaction, 'type') ?? 0);
if (in_array($type, [3, 4], true)) {
return $map;
}
$gold = (float) ($this->lineValue($transaction, 'gold') ?? 0);
if (abs($gold) < 0.0000001) {
return $map;
}
$metalId = $this->resolveTransactionMetalId($transaction);
$grams = abs($this->transactionGoldGrams($transaction));
$display = $this->gramsToMetalDisplay($grams, $metalId);
if ($gold < 0) {
$display *= -1;
}
$map[(string) $metalId] = round($display, 3);
return $map;
}
/**
* @param list<int> $catalogMetalIds
*/
public function enrichTransactionTableRow(mixed &$row, array $catalogMetalIds): void
{
$metalMap = $this->buildTransactionMetalDisplayMap($row, $catalogMetalIds);
$metalId = $this->resolveTransactionMetalId($row);
$metalLabel = $this->metalLabel($metalId);
if (is_array($row)) {
$row['metal_gold'] = $metalMap;
$row['transaction_metal_id'] = $metalId;
$row['transaction_metal_label'] = $metalLabel;
foreach ($catalogMetalIds as $mid) {
$row['metal_gold_' . $mid] = $metalMap[(string) $mid] ?? 0.0;
}
return;
}
if (is_object($row)) {
$row->metal_gold = $metalMap;
$row->transaction_metal_id = $metalId;
$row->transaction_metal_label = $metalLabel;
foreach ($catalogMetalIds as $mid) {
$row->{'metal_gold_' . $mid} = $metalMap[(string) $mid] ?? 0.0;
}
}
}
private function transactionProduct(mixed $transaction): ?object
{
if (is_object($transaction) && isset($transaction->product)) {
return $transaction->product;
}
if (is_array($transaction) && isset($transaction['product'])) {
$product = $transaction['product'];
return is_object($product) ? $product : (object) $product;
}
return null;
}
private function lineValue(mixed $line, string $key): mixed
{
if (is_array($line)) {
return $line[$key] ?? null;
}
if (is_object($line)) {
return $line->{$key} ?? null;
}
return null;
}
}