Files
boursecu_server/app/Http/Controllers/Customer/ProductController.php
T
2026-07-18 15:04:08 +03:30

211 lines
9.0 KiB
PHP

<?php
namespace App\Http\Controllers\Customer;
use App\DB\Te\TableEngine;
use App\Http\Controllers\Controller;
use App\Http\Controllers\SuperController;
use App\Models\Fund;
use App\Models\Product;
use App\Services\AssetSummaryService;
use App\Services\WalletBalanceService;
use Illuminate\Http\Request;
use Rooyesh\Wallet\interfaces\TransactionInterface;
use Rooyesh\Wallet\interfaces\WalletInterface;
use Rooyesh\Wallet\models\Transaction;
use Rooyesh\Wallet\models\Wallet;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class ProductController extends SuperController
{
public function __construct(Request $request)
{
$model = Product::class;
$this->with=['wallet'];
$select = ['*'];
$search=['name'];
$request->request->add(['customer_id'=>auth('customer')->id()]);
$query = Product::with($this->with);
parent::__construct($model, $this->with, $select, null,$query,$search);
}
public function pTable(Request $request, AssetSummaryService $assetSummaryService, WalletBalanceService $balanceService)
{
$user = auth('customer')->user();
$mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds();
$inventoryByProduct = collect(
$assetSummaryService->buildForCustomer((int) $user->id, (int) $user->type)['inventory_cards'] ?? []
)->keyBy('product_id');
$products = Product::where(function ($query) use ($user, $mainWalletProductIds) {
$query->whereHas('wallet', function ($q) use ($user) {
$q->where('owner', $user->id);
});
if ($mainWalletProductIds !== []) {
$query->orWhereIn('id', $mainWalletProductIds);
}
})->with(['wallet' => function ($query) use ($user) {
$query->where('owner', $user->id);
}, 'file'])->get();
// محصولات متصل به کیف اصلی (main_wallet_products): فقط نمایندهٔ لیست با موجودی کیف اصلی
$representativeId = null;
$mainWalletGold = 0.0;
if ($mainWalletProductIds !== []) {
foreach ($mainWalletProductIds as $pid) {
if ($products->firstWhere('id', $pid)) {
$representativeId = (int) $pid;
break;
}
}
if ($representativeId !== null) {
$mainWalletGold = $balanceService->effectiveMainWalletGold((int) $user->id);
}
$products = $products->filter(function ($product) use ($mainWalletProductIds, $representativeId) {
if (in_array((int) $product->id, array_map('intval', $mainWalletProductIds), true)) {
return (int) $product->id === $representativeId;
}
return true;
})->values();
}
// Prepare the response
$user['rows'] = $products->map(function ($product) use ($user, $representativeId, $mainWalletGold, $inventoryByProduct, $assetSummaryService) {
$product->unit = self::productQuantityUnit([
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
'group_type' => $product->group_type ?? null,
]);
if ($product->relationLoaded('file') && $product->file) {
$product->file->setAttribute('full_src', $product->file->src);
}
$card = $inventoryByProduct->get((int) $product->id);
$displayQty = $card !== null
? (float) ($card['quantity'] ?? 0)
: $assetSummaryService->productDisplayQuantity((int) $user->id, (int) $user->type, (int) $product->id);
if ($representativeId !== null && (int) $product->id === $representativeId) {
// کیف محصول فقط نقش هندل را دارد؛ موجودی واقعی از کیف اصلی (+ legacy) است
$walletInterface = new WalletInterface($user->id, $user->type, null, $product->id);
$handleWallet = $walletInterface->wallet;
$handleWallet->gold = $displayQty > 0 ? $displayQty : $mainWalletGold;
$product->setRelation('wallet', $handleWallet);
} elseif ($displayQty !== null) {
if ($product->wallet) {
$product->wallet->gold = $displayQty;
} else {
$walletInterface = new WalletInterface($user->id, $user->type, null, $product->id);
$handleWallet = $walletInterface->wallet;
$handleWallet->gold = $displayQty;
$product->setRelation('wallet', $handleWallet);
}
}
return $product;
});
return response()->json($user);
}
public function deposit(Request $request){
$data = $request->all();
$wallet_interface = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type,null);
$wallet = $wallet_interface->wallet;
if($wallet->gold < $data['value']){
throw new UnprocessableEntityHttpException('موجودی کمتر از مقدار وارد شده است');
}
$transaction = new TransactionInterface($wallet_interface, $_inp = [
"gold" => $data['value'],
"fund_id"=>$data["fund_id"],
'expire_at'=>now(),
'active'=>true
], 6, false);
$wallet_interface_fund = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type,$data["fund_id"]);
$transaction = new TransactionInterface($wallet_interface_fund, $_inp = [
"gold" => $data['value'],
"fund_id"=>$data["fund_id"],
'expire_at'=>now(),
'active'=>true
], 5, true);
return response()->json(true);
}
public function withdraw(Request $request){
$data = $request->all();
$wallet_interface_fund = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type,$data["fund_id"]);
$wallet = $wallet_interface_fund->wallet;
if($wallet->gold < $data['value']){
throw new UnprocessableEntityHttpException('موجودی کمتر از مقدار وارد شده است');
}
$transaction = new TransactionInterface($wallet_interface_fund, $_inp = [
"gold" => $data['value'],
"fund_id"=>$data["fund_id"],
'expire_at'=>now(),
'active'=>true
], 6, false);
$wallet_interface = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type);
$transaction = new TransactionInterface($wallet_interface, $_inp = [
"gold" => $data['value'],
"fund_id"=>$data["fund_id"],
'expire_at'=>now(),
'active'=>true
], 5, true);
return response()->json(true);
}
public function show($id, AssetSummaryService $assetSummaryService){
$customer = auth('customer')->user();
$query = Product::/*with(['walletMy as wallet'])->*/findOrFail($id);
$wallet = $query->walletMy;
// محصول متصل به کیف اصلی: موجودی مؤثر (کیف اصلی + legacy) — نه فقط ردیف product handle
if (self::isMainWalletProduct((int) $id)) {
$mainWalletGold = $assetSummaryService->productDisplayQuantity(
(int) $customer->id,
(int) $customer->type,
(int) $id
);
if ($wallet) {
$wallet->gold = $mainWalletGold;
} else {
$wallet = Wallet::where('owner', $customer->id)
->whereNull('fund_id')
->whereNull('product_id')
->first() ?? new Wallet(['gold' => $mainWalletGold]);
$wallet->gold = $mainWalletGold;
}
} elseif ($wallet) {
$wallet->gold = $assetSummaryService->productDisplayQuantity(
(int) $customer->id,
(int) $customer->type,
(int) $id
);
}
$query['wallet'] = $wallet;
return response()->json($query);
}
public function destroy($id){
$wallet_interface_fund = Wallet::where('owner',auth('customer')->id())->where('fund_id',$id)->first();
if (!is_null($wallet_interface_fund)){
if ($wallet_interface_fund->gold > 0){
throw new UnprocessableEntityHttpException('به علت داشتن موجودی امکان حذف صندوق وجود ندارد');
}
}
Fund::destroy($id);
return response()->json(true);
}
public function transactionTable(Request $request){
$records = Transaction::where('wallet',$request->wallet_id);
TableEngine::create($records, $request->all())
->initSearch([])
->select([
'*',Transaction::$SELECT_CAJ,Transaction::$SELECT_TYPE_STR
])->export($records);
return response()->json($records);
}
}