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

126 lines
4.9 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 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)
{
$user = auth('customer')->user();
// $products = Product::with(['wallet' => function ($query) use ($user) {
// $query->where('owner', $user->id);
// }])->get();
$products = Product::whereHas('wallet', function ($query) use ($user) {
$query->where('owner', $user->id);
})->with(['wallet' => function ($query) use ($user) {
$query->where('owner', $user->id);
}])->get();
// Prepare the response
$user['rows'] = $products->map(function ($product) {
$product->unit = self::productQuantityUnit([
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
'group_type' => $product->group_type ?? null,
]);
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){
$query = Product::/*with(['walletMy as wallet'])->*/findOrFail($id);
$query['wallet']= $query->walletMy;
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);
}
}