fixed kilo in metals 3
This commit is contained in:
@@ -5,26 +5,54 @@ namespace App\Http\Controllers\Admin;
|
|||||||
use App\DB\Te\TableEngine;
|
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 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){
|
||||||
$records = Order::with(['customer','transactions']);
|
$balanceReport = app(BalanceReportService::class);
|
||||||
|
|
||||||
|
$records = Order::with([
|
||||||
|
'customer',
|
||||||
|
'transactions.product:id,title,unit,metal_id,group_type,amount,trade_by_quantity,direct_price',
|
||||||
|
]);
|
||||||
TableEngine::create($records, $request->all())
|
TableEngine::create($records, $request->all())
|
||||||
->initSearch(['customer.name'])
|
->initSearch(['customer.name'])
|
||||||
->select([
|
->select([
|
||||||
'*',Order::$SELECT_STS_STR
|
'*',Order::$SELECT_STS_STR
|
||||||
])->export($records);
|
])->export($records);
|
||||||
|
|
||||||
|
if (isset($records['rows'])) {
|
||||||
|
foreach ($records['rows'] as &$row) {
|
||||||
|
$balanceReport->enrichOrderTableRow($row);
|
||||||
|
}
|
||||||
|
unset($row);
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json($records);
|
return response()->json($records);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function singleOrderTable(Request $request){
|
public function singleOrderTable(Request $request){
|
||||||
$records = Order::with(['customer'])->where('type',2);
|
$balanceReport = app(BalanceReportService::class);
|
||||||
|
|
||||||
|
$records = Order::with([
|
||||||
|
'customer',
|
||||||
|
'transactions.product:id,title,unit,metal_id,group_type,amount,trade_by_quantity,direct_price',
|
||||||
|
])->where('type',2);
|
||||||
TableEngine::create($records, $request->all())
|
TableEngine::create($records, $request->all())
|
||||||
->initSearch(['customer.name'])
|
->initSearch(['customer.name'])
|
||||||
->select([
|
->select([
|
||||||
'*',Order::$SELECT_STS_STR
|
'*',Order::$SELECT_STS_STR
|
||||||
])->export($records);
|
])->export($records);
|
||||||
|
|
||||||
|
if (isset($records['rows'])) {
|
||||||
|
foreach ($records['rows'] as &$row) {
|
||||||
|
$balanceReport->enrichOrderTableRow($row);
|
||||||
|
}
|
||||||
|
unset($row);
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json($records);
|
return response()->json($records);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -355,6 +355,76 @@ class BalanceReportService
|
|||||||
return null;
|
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
|
private function lineValue(mixed $line, string $key): mixed
|
||||||
{
|
{
|
||||||
if (is_array($line)) {
|
if (is_array($line)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user