fixed kilo in metals 3
This commit is contained in:
@@ -355,6 +355,76 @@ class BalanceReportService
|
||||
return null;
|
||||
}
|
||||
|
||||
public function enrichOrderTableRow(mixed &$row): void
|
||||
{
|
||||
$product = $this->resolveOrderProduct($row);
|
||||
$unit = $this->resolveOrderAmountUnit($product);
|
||||
$amount = (float) ($this->lineValue($row, 'amount') ?? 0);
|
||||
$displayAmount = $this->resolveOrderDisplayAmount($amount, $product);
|
||||
|
||||
if (is_array($row)) {
|
||||
$row['amount_unit'] = $unit;
|
||||
$row['amount_display'] = $displayAmount;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_object($row)) {
|
||||
$row->amount_unit = $unit;
|
||||
$row->amount_display = $displayAmount;
|
||||
}
|
||||
}
|
||||
|
||||
public function resolveOrderAmountUnit(?object $product): string
|
||||
{
|
||||
if ($product === null) {
|
||||
return Controller::tradeWeightUnit();
|
||||
}
|
||||
|
||||
return Controller::productQuantityUnit(Controller::productUnitPayload($product));
|
||||
}
|
||||
|
||||
private function resolveOrderDisplayAmount(float $amount, ?object $product): float
|
||||
{
|
||||
if ($product === null || abs($amount) < 0.0000001) {
|
||||
return round($amount, 3);
|
||||
}
|
||||
|
||||
$payload = Controller::productUnitPayload($product);
|
||||
$productUnit = trim((string) ($payload['unit'] ?? ''));
|
||||
if (!Controller::isWeightBasedProductUnit($productUnit)) {
|
||||
return round($amount, 3);
|
||||
}
|
||||
|
||||
$tradeUnit = Controller::tradeWeightUnit();
|
||||
if ($productUnit !== $tradeUnit) {
|
||||
return round(Controller::convertWeight($amount, $tradeUnit, $productUnit), 3);
|
||||
}
|
||||
|
||||
return round($amount, 3);
|
||||
}
|
||||
|
||||
private function resolveOrderProduct(mixed $order): ?object
|
||||
{
|
||||
$transactions = $this->lineValue($order, 'transactions');
|
||||
if ($transactions instanceof \Illuminate\Support\Collection) {
|
||||
$transactions = $transactions->all();
|
||||
}
|
||||
|
||||
if (!is_array($transactions)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($transactions as $tx) {
|
||||
$product = is_object($tx) ? ($tx->product ?? null) : ($tx['product'] ?? null);
|
||||
if ($product) {
|
||||
return is_object($product) ? $product : (object) $product;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function lineValue(mixed $line, string $key): mixed
|
||||
{
|
||||
if (is_array($line)) {
|
||||
|
||||
Reference in New Issue
Block a user