fixed kilo in metals 2
This commit is contained in:
@@ -20,6 +20,7 @@ use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
use App\Http\Controllers\Admin\TahesabController;
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use App\Services\BalanceReportService;
|
||||
use App\Services\WalletBalanceService;
|
||||
use Hekmatinasser\Verta\Verta;
|
||||
|
||||
@@ -219,35 +220,28 @@ class WalletController extends Controller
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$records = Transaction::with(['customer'])->whereNull('fund_id');
|
||||
$balanceReport = app(BalanceReportService::class);
|
||||
$balanceService = app(WalletBalanceService::class);
|
||||
$catalogMetalIds = $balanceService->catalogMetalIds();
|
||||
|
||||
$records = Transaction::with([
|
||||
'customer',
|
||||
'product:id,title,metal_id,group_type,unit,amount,trade_by_quantity,direct_price',
|
||||
])->whereNull('fund_id');
|
||||
TableEngine::create($records, $request->all())
|
||||
->initSearch(['order_id', 'customer.name'])
|
||||
->select([
|
||||
'*', Transaction::$SELECT_CAJ, Transaction::$SELECT_TYPE_STR, Transaction::$SELECT_SAJ, Transaction::$SELECT_EAJ
|
||||
])->export($records);
|
||||
// foreach ($records['rows'] as &$record) {
|
||||
// if (empty($record->title) or $record->title == "تعریف نشده"){
|
||||
// $record->title = $record->type_str ;
|
||||
// $record->unit = "گرم";
|
||||
// if (isset($record->wallet)) {
|
||||
// $wallet = Wallet::with('product')->where('id',$record->wallet)->first();
|
||||
// if (isset($wallet->product)) {
|
||||
// if ($record->gold > 0){
|
||||
// $record->title = " خرید " ;
|
||||
//
|
||||
// }else{
|
||||
// $record->title = " فروش " ;
|
||||
//
|
||||
// }
|
||||
// $record->title .= $wallet->product->title ;
|
||||
// $record->unit = "عدد";
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// $record->save();
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
if (isset($records['rows'])) {
|
||||
foreach ($records['rows'] as &$row) {
|
||||
$balanceReport->enrichTransactionTableRow($row, $catalogMetalIds);
|
||||
}
|
||||
unset($row);
|
||||
}
|
||||
|
||||
$records['catalogMetals'] = $balanceReport->catalogMetalsMeta();
|
||||
|
||||
return response()->json($records);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user