fixed kilo in metals 4
This commit is contained in:
@@ -6,12 +6,14 @@ use App\DB\Te\TableEngine;
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\Order;
|
use App\Models\Order;
|
||||||
use App\Services\BalanceReportService;
|
use App\Services\BalanceReportService;
|
||||||
|
use App\Services\WalletBalanceService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class OrderController extends Controller
|
class OrderController extends Controller
|
||||||
{
|
{
|
||||||
public function table(Request $request){
|
public function table(Request $request){
|
||||||
$balanceReport = app(BalanceReportService::class);
|
$balanceReport = app(BalanceReportService::class);
|
||||||
|
$catalogMetalIds = app(WalletBalanceService::class)->catalogMetalIds();
|
||||||
|
|
||||||
$records = Order::with([
|
$records = Order::with([
|
||||||
'customer',
|
'customer',
|
||||||
@@ -25,16 +27,19 @@ class OrderController extends Controller
|
|||||||
|
|
||||||
if (isset($records['rows'])) {
|
if (isset($records['rows'])) {
|
||||||
foreach ($records['rows'] as &$row) {
|
foreach ($records['rows'] as &$row) {
|
||||||
$balanceReport->enrichOrderTableRow($row);
|
$balanceReport->enrichOrderTableRow($row, $catalogMetalIds);
|
||||||
}
|
}
|
||||||
unset($row);
|
unset($row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$records['catalogMetals'] = $balanceReport->catalogMetalsMeta();
|
||||||
|
|
||||||
return response()->json($records);
|
return response()->json($records);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function singleOrderTable(Request $request){
|
public function singleOrderTable(Request $request){
|
||||||
$balanceReport = app(BalanceReportService::class);
|
$balanceReport = app(BalanceReportService::class);
|
||||||
|
$catalogMetalIds = app(WalletBalanceService::class)->catalogMetalIds();
|
||||||
|
|
||||||
$records = Order::with([
|
$records = Order::with([
|
||||||
'customer',
|
'customer',
|
||||||
@@ -48,11 +53,13 @@ class OrderController extends Controller
|
|||||||
|
|
||||||
if (isset($records['rows'])) {
|
if (isset($records['rows'])) {
|
||||||
foreach ($records['rows'] as &$row) {
|
foreach ($records['rows'] as &$row) {
|
||||||
$balanceReport->enrichOrderTableRow($row);
|
$balanceReport->enrichOrderTableRow($row, $catalogMetalIds);
|
||||||
}
|
}
|
||||||
unset($row);
|
unset($row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$records['catalogMetals'] = $balanceReport->catalogMetalsMeta();
|
||||||
|
|
||||||
return response()->json($records);
|
return response()->json($records);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -355,23 +355,61 @@ class BalanceReportService
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function enrichOrderTableRow(mixed &$row): void
|
public function resolveOrderMetalId(mixed $order): int
|
||||||
|
{
|
||||||
|
$product = $this->resolveOrderProduct($order);
|
||||||
|
if ($product !== null) {
|
||||||
|
$metalId = (int) ($product->metal_id ?? 0);
|
||||||
|
|
||||||
|
return $metalId > 0 ? $metalId : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param list<int> $catalogMetalIds
|
||||||
|
*/
|
||||||
|
public function enrichOrderTableRow(mixed &$row, array $catalogMetalIds = []): void
|
||||||
{
|
{
|
||||||
$product = $this->resolveOrderProduct($row);
|
$product = $this->resolveOrderProduct($row);
|
||||||
$unit = $this->resolveOrderAmountUnit($product);
|
$unit = $this->resolveOrderAmountUnit($product);
|
||||||
$amount = (float) ($this->lineValue($row, 'amount') ?? 0);
|
$amount = (float) ($this->lineValue($row, 'amount') ?? 0);
|
||||||
$displayAmount = $this->resolveOrderDisplayAmount($amount, $product);
|
$displayAmount = $this->resolveOrderDisplayAmount($amount, $product);
|
||||||
|
$metalId = $this->resolveOrderMetalId($row);
|
||||||
|
$metalLabel = $this->metalLabel($metalId);
|
||||||
|
$price = (float) ($this->lineValue($row, 'price') ?? 0);
|
||||||
|
|
||||||
|
$metalPrices = [];
|
||||||
|
foreach ($catalogMetalIds as $mid) {
|
||||||
|
$mid = (int) $mid;
|
||||||
|
$metalPrices[(string) $mid] = $mid === $metalId ? $price : 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$payload = [
|
||||||
|
'amount_unit' => $unit,
|
||||||
|
'amount_display' => $displayAmount,
|
||||||
|
'order_metal_id' => $metalId,
|
||||||
|
'order_metal_label' => $metalLabel,
|
||||||
|
'metal_prices' => $metalPrices,
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($catalogMetalIds as $mid) {
|
||||||
|
$payload['metal_price_' . (int) $mid] = $metalPrices[(string) (int) $mid] ?? 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_array($row)) {
|
if (is_array($row)) {
|
||||||
$row['amount_unit'] = $unit;
|
foreach ($payload as $key => $value) {
|
||||||
$row['amount_display'] = $displayAmount;
|
$row[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_object($row)) {
|
if (is_object($row)) {
|
||||||
$row->amount_unit = $unit;
|
foreach ($payload as $key => $value) {
|
||||||
$row->amount_display = $displayAmount;
|
$row->{$key} = $value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user