diff --git a/app/Http/Controllers/Admin/CustomerController.php b/app/Http/Controllers/Admin/CustomerController.php index 1f880c2..f7d740c 100644 --- a/app/Http/Controllers/Admin/CustomerController.php +++ b/app/Http/Controllers/Admin/CustomerController.php @@ -16,6 +16,7 @@ use Rooyesh\Wallet\interfaces\WalletInterface; use Rooyesh\Wallet\models\Transaction; use Rooyesh\Wallet\models\Wallet; use App\Models\Setting; +use App\Services\AssetSummaryService; use App\Services\BalanceReportService; use App\Services\ConfigService; use App\Services\WalletBalanceService; @@ -23,6 +24,11 @@ use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; class CustomerController extends SuperController { + /** فیلدهای محصول لازم برای محاسبهٔ صحیح وزن موجودی */ + private const WALLET_PRODUCT_COLUMNS = [ + 'id', 'metal_id', 'unit', 'amount', 'group_type', 'trade_by_quantity', 'direct_price', 'title', + ]; + public function __construct() { $this->with = []; @@ -134,7 +140,8 @@ class CustomerController extends SuperController return response()->json(true); } public function tableWallets(Request $request){ - $query_table = Customer::with(['wallet.product:id,metal_id', 'referral'])->where("is_archive",false)/*->where('type',1)*/; + $productSelect = 'product:'.implode(',', self::WALLET_PRODUCT_COLUMNS); + $query_table = Customer::with(['wallet.'.$productSelect, 'referral'])->where('is_archive', false); TableEngine::create($query_table, $request) ->initSearch($this->search) ->select(['*',Customer::$SELECT_TYPE_STR]) @@ -163,6 +170,15 @@ class CustomerController extends SuperController return response()->json($query_table); } + public function assetsSummary($customerId, AssetSummaryService $assetSummaryService) + { + $customer = Customer::findOrFail($customerId); + + return response()->json( + $assetSummaryService->buildForCustomer((int) $customer->id, (int) $customer->type) + ); + } + public function archiveTable(Request $request) { $query_table = Customer::with($this->with)->where("is_archive",true)/*->where('type',1)*/; diff --git a/app/Http/Controllers/Admin/WalletController.php b/app/Http/Controllers/Admin/WalletController.php index a4d649f..5d5aceb 100644 --- a/app/Http/Controllers/Admin/WalletController.php +++ b/app/Http/Controllers/Admin/WalletController.php @@ -490,7 +490,7 @@ class WalletController extends Controller TableEngine::create($sumQuery, $request->all()) ->initSearch(['customer.name']); - $sumRows = (clone $sumQuery)->with('product:id,metal_id')->get(); + $sumRows = (clone $sumQuery)->with('product:id,metal_id,unit,amount,group_type,trade_by_quantity,direct_price,title')->get(); $metalBalances = $balanceService->buildWalletMetalSums($sumRows, $catalogMetalIds); $totalGold = array_sum(array_column($metalBalances, 'gold')); $totalGoldToCash = array_sum(array_column($metalBalances, 'goldToCash')); @@ -504,7 +504,7 @@ class WalletController extends Controller 'metalBalances' => $metalBalances, ]; - $records = Wallet::with(['customer', 'product:id,metal_id']); + $records = Wallet::with(['customer', 'product:id,metal_id,unit,amount,group_type,trade_by_quantity,direct_price,title']); TableEngine::create($records, $request->all()) ->initSearch(['customer.name']) ->select(['*']) diff --git a/routes/admin.php b/routes/admin.php index eb5ffd9..ebdce6d 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -78,6 +78,7 @@ Route::middleware('auth:user')->group(function (){ //--------customer Route::post('/customer/table',[CustomerController::class,'tableWallets']); + Route::get('/customer/{id}/assets',[CustomerController::class,'assetsSummary']); Route::get('/customer/active/{id}',[CustomerController::class,'active']); Route::get('/customer/archive/{id}',[CustomerController::class,'archive']); Route::get('/customer/trade/{id}',[CustomerController::class,'changeTradeActive']);