Files
boursecu_server/app/Http/Controllers/Customer/ProductController.php
T

202 lines
8.4 KiB
PHP
Raw Normal View History

2026-01-24 14:03:11 +00:00
<?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;
2026-07-06 17:06:03 +03:30
use App\Services\AssetSummaryService;
2026-01-24 14:03:11 +00:00
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);
}
2026-07-06 17:06:03 +03:30
public function pTable(Request $request, AssetSummaryService $assetSummaryService)
2026-01-24 14:03:11 +00:00
{
$user = auth('customer')->user();
2026-06-10 17:31:38 +03:30
$mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds();
2026-07-06 17:06:03 +03:30
$inventoryByProduct = collect(
$assetSummaryService->buildForCustomer((int) $user->id, (int) $user->type)['inventory_cards'] ?? []
)->keyBy('product_id');
2026-06-10 17:31:38 +03:30
$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);
}
2026-01-24 14:03:11 +00:00
})->with(['wallet' => function ($query) use ($user) {
$query->where('owner', $user->id);
2026-06-09 16:44:02 +03:30
}, 'file'])->get();
2026-01-24 14:03:11 +00:00
2026-06-10 17:31:38 +03:30
// محصولات متصل به کیف اصلی (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) {
$mainWallet = Wallet::where('owner', $user->id)
->whereNull('fund_id')
->whereNull('product_id')
->first();
$mainWalletGold = (float) ($mainWallet->gold ?? 0);
}
$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();
}
2026-01-24 14:03:11 +00:00
// Prepare the response
2026-07-06 17:06:03 +03:30
$user['rows'] = $products->map(function ($product) use ($user, $representativeId, $mainWalletGold, $inventoryByProduct) {
2026-06-03 21:52:37 +03:30
$product->unit = self::productQuantityUnit([
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
'group_type' => $product->group_type ?? null,
]);
2026-06-09 16:44:02 +03:30
if ($product->relationLoaded('file') && $product->file) {
2026-06-29 20:02:20 +03:30
$product->file->setAttribute('full_src', $product->file->src);
2026-06-09 16:44:02 +03:30
}
2026-07-06 17:06:03 +03:30
$card = $inventoryByProduct->get((int) $product->id);
$displayQty = $card !== null
? (float) ($card['quantity'] ?? 0)
: null;
2026-06-10 17:31:38 +03:30
if ($representativeId !== null && (int) $product->id === $representativeId) {
// کیف محصول فقط نقش هندل را دارد؛ موجودی واقعی از کیف اصلی است
$walletInterface = new WalletInterface($user->id, $user->type, null, $product->id);
$handleWallet = $walletInterface->wallet;
2026-07-06 17:06:03 +03:30
$handleWallet->gold = $displayQty ?? $mainWalletGold;
2026-06-10 17:31:38 +03:30
$product->setRelation('wallet', $handleWallet);
2026-07-06 17:06:03 +03:30
} 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);
}
2026-06-10 17:31:38 +03:30
}
2026-06-03 21:52:37 +03:30
return $product;
});
2026-01-24 14:03:11 +00:00
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){
$query = Product::/*with(['walletMy as wallet'])->*/findOrFail($id);
2026-06-10 17:31:38 +03:30
$wallet = $query->walletMy;
// محصول متصل به کیف اصلی: موجودی از کیف اصلی خوانده می‌شود
if (self::isMainWalletProduct((int) $id)) {
$mainWallet = Wallet::where('owner', auth('customer')->id())
->whereNull('fund_id')
->whereNull('product_id')
->first();
if ($wallet) {
$wallet->gold = (float) ($mainWallet->gold ?? 0);
} else {
$wallet = $mainWallet;
}
}
$query['wallet'] = $wallet;
2026-01-24 14:03:11 +00:00
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);
}
}