This commit is contained in:
gholak tala
2026-01-24 14:03:11 +00:00
parent 7dcd83715a
commit eb26afbcb3
224 changed files with 32652 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace App\Http\Controllers\Customer;
use App\Http\Controllers\Controller;
use App\Http\Controllers\SuperController;
use App\Models\Address;
use Illuminate\Http\Request;
class AddressController extends SuperController
{
public function __construct(Request $request)
{
$request->request->add(['customer_id'=>auth('customer')->id()]);
$address = Address::where('customer_id',auth('customer')->id());
parent::__construct(Address::class, [], ['*'],null,$address);
}
}
+267
View File
@@ -0,0 +1,267 @@
<?php
namespace App\Http\Controllers\Customer;
use App\DB\Te\TableEngine;
use App\Http\Controllers\Admin\CustomerController as adm_cu;
use App\Http\Controllers\Controller;
use App\Models\Address;
use App\Models\Customer;
use App\Models\User;
use App\Models\TransactionsAdmin;
use App\Models\Withdraw;
use Illuminate\Http\Request;
use Rooyesh\Wallet\interfaces\TransactionInterface;
use Rooyesh\Wallet\interfaces\WalletInterface;
use Rooyesh\Wallet\models\Wallet;
use Rooyesh\Wallet\models\Transaction;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Http;
//use App\Models\Price;
//use Carbon\Carbon;
//use Illuminate\Support\Facades\Http;
class CustomerController extends Controller
{
public function me()
{
$user = Customer::findOrFail(auth('customer')->id());
$wallet = Wallet::where('owner', $user->id)
->where('owner_type', $user->type)
->whereNull('fund_id')
->first() ?? new Wallet();
$wallet->gold_ab = round(Wallet::where('owner', $user->id)->where('product_id', null)->sum('gold'), 2);
$wallet->wp = Wallet::with('product')->where('owner', $user->id)->get();
foreach ($wallet->wp as $row){
if ($row->product_id != null){
$wallet->treasure +=$row->product->amount*$row->gold;
}
$wallet->treasure_sum =$user->gold+$wallet->treasure;
}
$wallet->gold_sum = $wallet->treasure_sum+$wallet->gold_ab;
$user->wallets = Wallet::with(['product'])->where('owner', $user->id)->get() ?? new Wallet();
$count_gold =0;
$count_cash =0;
$count_product =0;
foreach ($user->wallets as $walletSingle) {
if ($walletSingle->product_id) {
if (!($walletSingle->product->group_type == "سکه")){
(float)$count_gold += $walletSingle->product->amount*$walletSingle->gold;
}
$count_cash += $walletSingle->product->price*$walletSingle->gold;
$count_product += $walletSingle->gold;
} else {
(float)$count_gold += $walletSingle->gold;
(float)$count_cash += $walletSingle->cash;
$wallet->count_cash_wallet = $walletSingle->cash;
$wallet->count_gold_wallet = $walletSingle->gold;
}
}
$wallet->count_cash_wallets = $count_cash-$wallet->count_cash_wallet;
$wallet->count_gold_wallets = $count_gold-$wallet->count_gold_wallet;
$wallet->count_gold = $count_gold;
$wallet->count_cash = $count_cash;
$wallet->count_product = $count_product;
$user->wallet = $wallet;
$user->pwallet = Wallet::with("product")->where('owner', $user->id)
->where('owner_type', $user->type) // Ensures the wallet's owner type matches the user's type
->whereNotNull('product_id') // Filters wallets that have a non-null product_id
->get() ?? new Wallet(); // If no wallet is found, assigns a new Wallet instance to the attribute
return response()->json($user);
}
public function updateMe(Request $request)
{
try {
$customer = Customer::findOrFail(auth('customer')->id());
$updateData = $request->except(['referral_id', 'referral_to_id']);
if (isset($request->fname) && isset($request->lname)) {
$updateData['name'] = "$request->fname $request->lname";
}
if ($request->has('credential_id')) {
$updateData['credential_id'] = $request->credential_id;
}
$customer->update($updateData);
if (!$customer->is_legal) {
$response = Http::withHeaders([
'Authorization' => 'Bearer mob83EBzqU7GWBmnc-6gXIp6r2G57P_94',
'Content-Type' => 'application/json',
])->post('http://185.97.116.24:20205/check_user', [
'number' => $customer->mobile,
'national' => $customer->n_code,
'host' => $_SERVER['HTTP_HOST'],
]);
if (isset($response['matched']) and $response['matched'] == true) {
$customer->is_legal = 1;
$customer->save();
if ($this->Config('has_page_earn_money') == true) {
$gift_for_inviter = 0.002;
$gift_for_invited = 0.003;
$companyName = $this->Config('name_fa');
$companyurl = $this->Config('app_url');
$referrer = $customer->referral;
if (Customer::where('referral_to_id', $referrer->referral_id)->get()->count() <= $referrer->add_user_limit) {
$Withdraw = Withdraw::create([
'user_id' => $customer->id,
'customer_id' => $customer->referral->id,
"cash" => 0,
"gold" => $gift_for_invited,
'sts' => 5,
]);
$wallet_interface = new WalletInterface($customer->referral->id, 2);
new TransactionInterface($wallet_interface, $_inp = [
"cash" => 0,
"gold" => $gift_for_invited,
'expire_at' => now(),
'active' => true,
'title' => 'پاداش بابت معرفی',
'withdraw_id' => $Withdraw->id,
'start_at' => now(),
], 8, true, false);
$transaction = Transaction::where('customer_id', $customer->referral->id)->latest()->first();
TransactionsAdmin::create([
'customer_id' => $customer->id,
'customer_to_id' => $customer->referral->id,
'user_id' => $customer->id,
'wallet' => null, // یا مقدار مناسب دیگری
'transaction_id' => $transaction->id,
'cash' => 0,
'note' => 'پاداش بابت معرفی',
'type' => 1, // یا مقدار مناسب دیگری
'gold' => $gift_for_invited,
'balance_cash' => 0, // یا مقدار مناسب دیگری
'withdraw_id' => $Withdraw->id,
]);
$referrerWallet = Wallet::where('owner', $customer->referral->id)->first();
$message = "سلام {$referrer->name} عزیز،\n\n{$gift_for_invited} گرم طلا بابت معرفی {$customer->name} به حساب شما واریز شد.\nموجودی فعلی: {$referrerWallet->gold} گرم.\n\nاطلاعات بیشتر: $companyurl\n\nبا احترام،\n$companyName";
$this->Send($referrer->mobile, $message);
}
$Withdraw = Withdraw::create([
'user_id' => $customer->id,
'customer_id' => $customer->id,
"cash" => 0,
"gold" => $gift_for_inviter,
'sts' => 5,
]);
$wallet_interface = new WalletInterface($customer->id, 2);
new TransactionInterface($wallet_interface, $_inp = [
"cash" => 0,
"gold" => $gift_for_inviter,
'expire_at' => now(),
'active' => true,
'title' => 'پاداش بابت ثبت نام',
'withdraw_id' => $Withdraw->id,
'start_at' => now(),
], 8, true, false);
$transaction = Transaction::where('customer_id', $customer->id)->latest()->first();
TransactionsAdmin::create([
'customer_id' => $customer->id,
'customer_to_id' => $customer->id,
'user_id' => $customer->id,
'wallet' => null, // یا مقدار مناسب دیگری
'transaction_id' => $transaction->id,
'cash' => 0,
'note' => 'پاداش بابت ثبت نام',
'type' => 1, // یا مقدار مناسب دیگری
'gold' => $gift_for_inviter,
'balance_cash' => 0, // یا مقدار مناسب دیگری
'withdraw_id' => $Withdraw->id,
]);
$referrerWallet = Wallet::where('owner', $customer->id)->first();
$message = "سلام " . $customer->name . " عزیز،\n\n" . $gift_for_invited . " گرم طلا بابت ثبت نام " . " به حساب شما واریز شد.\n" . "موجودی فعلی: " . $referrerWallet->gold . " گرم.\n\n" . "اطلاعات بیشتر: " . $companyurl . "\n\n" . "با احترام،\n" . $companyName;
$this->Send($customer->mobile, $message);
}
} else {
// This block will handle the case where the code doesn't match
// if (isset($response['code']) and $response['code'] === 'mobileNumber.not_valid') {
// return response()->json(['message' => "کد ملی تطابق ندارد",'response' =>$response], 422);
// } else {
// // This block will handle other errors from the $response variable
// return response()->json(['response' => $response], 422);
// }
return response()->json(['message' => "کد ملی ثبت شد، اما در حال حاضر سیستم نتوانست آن را تأیید کند. لطفاً منتظر بررسی و تأیید مدیر بمانید.",'response' =>$response], 422);
}
}
return response()->json(true);
} catch (\Exception $e) {
// نمایش 400 کاراکتر اول از پیام خطا
$errorMessage = substr($e->getMessage(), 0, 400);
return response()->json(['error' => $errorMessage], 500);
}
}
public function active_finger()
{
$customer = Customer::findOrFail(auth('customer')->id());
if ($customer->finger_print) {
$customer->finger_print = false; // Set to false if currently true
} else {
$customer->finger_print = true; // Set to true if currently false
}
$customer->save();
return response()->json($customer);
}
public function imFrom($code)
{
$referrer = Customer::where('referral_id', $code)->first();
if ($referrer) {
// Define the reward amount
$rewardAmount = 200000; // Adjusted reward amount as per the new requirement
// Update the referred customer with the referral code
$newCustomer = auth('customer')->user(); // Assuming the new customer is authenticated
$newCustomer->referral_id = $code;
$newCustomer->save();
// Construct the SMS message
$message = sprintf(
"%s با کد معرف شما ثبت نام کرد.\nکیف پول شما %d ریال به عنوان پاداش شارژ شد.\n%s",
$newCustomer->name,
$rewardAmount,
$this->Config('name_fa')
);
// Send SMS to the referrer
$this->Send($referrer->mobile, $message);
return response()->json(['message' => 'Referral reward processed and SMS sent to referrer.'], 200);
} else {
return response()->json(['error' => 'کد نامعتبر میباشد'], 422);
}
}
public function i_have(Request $request)
{
$user = auth('customer')->user();
$customer_referral_id = Customer::where("id", $user['id'])->first()->referral_id;
$return['users'] = Customer::where("referral_to_id", $customer_referral_id)->get();
$return['gifts'] = TransactionsAdmin::with(['withdraw', 'order', "user", 'customerTo', 'customer'])->where("customer_to_id", $user->id)->get();
$return['usersSum'] = $return['users']->count();
$return['giftsSum'] = number_format($return['gifts']->sum('gold'), 3);
return response()->json($return);
}
}
+97
View File
@@ -0,0 +1,97 @@
<?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 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 FundController extends SuperController
{
public function __construct(Request $request)
{
$model = Fund::class;
$this->with=['wallet'];
$select = ['*'];
$search=['name'];
$request->request->add(['customer_id'=>auth('customer')->id()]);
$query = Fund::with($this->with)->where('customer_id',auth('customer')->id());
parent::__construct($model, $this->with, $select, null,$query,$search);
}
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 = Fund::with($this->with)->findOrFail($id);
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);
}
}
+125
View File
@@ -0,0 +1,125 @@
<?php
namespace App\Http\Controllers\Customer;
use App\DB\Te\TableEngine;
use App\Http\Controllers\Controller;
use App\Jobs\CheckOrder;
use App\Models\Order;
use App\Models\Price;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Rooyesh\Wallet\interfaces\TransactionInterface;
use Rooyesh\Wallet\interfaces\WalletInterface;
use Rooyesh\Wallet\models\Wallet;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class OrderController extends Controller
{
public function store(Request $request){
$user= auth()->user();
if ($user->is_legal == 0 or $user->id==4) {
return response()->json(['massage' => 'شما باید اول خود را احراز هویت کنید'], 422);
}
// if ()
$data = $request->all();
$data['customer_id'] = auth('customer')->id();
$data['sts'] = 2;
$data['payment_sts'] = 4;
$PriceMain = Price::orderBy('id','desc')->first();
$data['price'] = $PriceMain->price;
$add_sell_price = config('add_sell_price.major');
$customer = auth('customer')->user();
if (!auth('customer')->user()->trade_active){
throw new UnprocessableEntityHttpException('امکان سفارش برای شما غیرفعال است.');
}
$wallet = Wallet::where('owner',auth('customer')->id())->whereNull('fund_id')
->first();
$wallet_interface = new WalletInterface($data['customer_id'], 1);
if ($data['direction'] == 1){
// $order = Order::where('customer_id',auth('customer')->id())->where('sts',1)->first();
// if (!is_null($order)){
// throw new UnprocessableEntityHttpException('شما یک سفارش باز دارید,ابتدا نسبت به تسویه آن اقدام نمایید و مجددا تلاش کنید');
// }
$gold_block = 0;
$data['price_original'] = $data['price'];
$data['total_price_original'] = $data['amount']* $data['price_original'];
$data['price'] += $add_sell_price;
$data['total_price'] = $data['amount']* $data['price'];
$data['payment_remind'] = 0;
$data['payment_amount'] = $data['total_price'];
$data['fee'] = $add_sell_price;
$data['total_fee'] = $data['amount']* $data['fee'];
if ($wallet->cash < $data['total_price']){
if ($data['amount'] > $customer->gram){
throw new UnprocessableEntityHttpException('مقدار وارد شده بیش از سقف اعتبار شماست');
}
$data['sts'] = 1;
$data['payment_remind'] = $data['total_price'];
$data['payment_amount'] = 0;
$gold_block = $data['amount'];
}
$data['price_id'] = $PriceMain->id;
$order = Order::create($data);
new TransactionInterface($wallet_interface, $_inp = [
"cash" => $data['total_price'],
"gold"=> $data['amount'],
'expire_at'=>now(),
'active'=>true,
'order_id'=>$order->id,
'start_at'=>now(),
'end_at'=>now()->addDay()
], 1, true,false,$gold_block);
if ($data['sts'] == 1){
CheckOrder::dispatch($order->id)->onConnection('database')->delay(now()->addMinutes(5));
}
$message = auth('customer')->user()->name." عزیز" ."\n"
.'در تاریخ '.verta()->format('Y/n/j H:i:s')." کیف طلایی شما "."\n"
.$data['amount']." گرم شارژ شد"."\n"
."با تشکر".$this->Config('name_fa');
}else{
$data['price_original'] = $data['price'];
$data['total_price_original'] = $data['amount']* $data['price_original'];
$data['price'] -= $add_sell_price;
$data['total_price'] = $data['amount']* $data['price'];
$data['payment_remind'] = 0;
$data['payment_amount'] = $data['total_price'];
$data['fee'] = $add_sell_price;
$data['total_fee'] = $data['amount']* $data['fee'];
if (($wallet->gold - $wallet->gold_block) < $data['amount']){
throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است');
}
$order = Order::create($data);
new TransactionInterface($wallet_interface, $_inp = [
"gold" => $data['amount'],
"cash" => $data['total_price'],
'expire_at'=>now(),
'active'=>true,
'order_id'=>$order->id
], 2, false,true);
$message = auth('customer')->user()->name." عزیز" ."\n"
.'در تاریخ '.verta()->format('Y/n/j H:i:s')." کیف طلایی شما "."\n"
.$data['amount']." گرم کم شد"."\n"
."با تشکر".$this->Config('name_fa');
}
$this->Send(auth('customer')->user()->mobile,$message);
return response()->json($order);
}
public function table(Request $request){
$records = Order::where('customer_id',auth('customer')->id())
->where('type',1);
TableEngine::create($records, $request->all())
->initSearch([])
->select([
'*',Order::$SELECT_STS_STR
])->export($records);
return response()->json($records);
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace App\Http\Controllers\Customer;
use App\Http\Controllers\SuperController;
use Illuminate\Http\Request;
use App\Models\PriceTrigger; // اطمینان حاصل کنید که این مدل به درستی وارد شده است
class PriceTriggerController extends SuperController
{
public function __construct(Request $request)
{
$request->request->add(['customer_id'=>auth('customer')->id()]);
$model = PriceTrigger::class;
$this->with = ['customer'];
$this->select = ['*'];
$this->search = ['*'];
parent::__construct($model, $this->with, $this->select);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
$data=PriceTrigger::where("customer_id",auth()->id())->get();
return response()->json($data);
}
}
+117
View File
@@ -0,0 +1,117 @@
<?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;
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);
}
}
+319
View File
@@ -0,0 +1,319 @@
<?php
namespace App\Http\Controllers\Customer;
use App\DB\Te\TableEngine;
use App\Http\Controllers\Controller;
use App\Models\Address;
use App\Models\Customer;
use App\Models\Order;
use App\Models\User;
use App\Models\Withdraw;
use App\Models\BankAccount;
use App\Models\Price;
use App\Http\Controllers\Admin\TahesabController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
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 WalletController extends Controller
{
public function withdrawTable(Request $request)
{
$records = Withdraw::with(['transaction'])->where('customer_id', auth('customer')->id())->whereIn('sts', [2,1])->orderBy('id','desc');
TableEngine::create($records, $request->all())
->initSearch(['customer.name'])
->select([
'*',Withdraw::$SELECT_STS_STR,Withdraw::$SELECT_DAJ
])->export($records);
return response()->json($records);
}
public function walletMe()
{
$wallet = Wallet::select(['owner', 'id', 'gold', 'cash'])->where('owner', auth('customer')->id())
->first();
// $wallet['gold_sum'] = Wallet::where('owner', auth('customer')->id())->get();
$wallet['gold_sum'] = round(Wallet::where('owner', auth('customer')->id())->where('product_id', null)->sum('gold'), 2);
return response()->json($wallet);
}
public function priceData(Request $request)
{
return $response = Http::get('https://milli.gold/api/v1/public/milli-price/widget/external');
$prise = Price::with('order');
TableEngine::create($prise, $request)
->initSearch(['price'])
->select(['*'])
->export($prise);
return response()->json($prise);
}
public function transactionTable(Request $request)
{
if (isset($request->wallet_id)){
$wallet = Wallet::with('product')->where('id', $request->wallet_id)->where('owner', auth('customer')->id())->get();
}else{
$wallet = Wallet::with('product')->where('owner', auth('customer')->id())->get();
}
if ($wallet->isEmpty()) {
$records = ['query' => ['total' => 0], 'rows' => []];
} else {
$records = Transaction::with([
'order' => function ($q) {
$q->select(['*', Order::$SELECT_STS_STR]);
},
'withdraw' => function ($q) {
$q->select(['*', Order::$SELECT_STS_STR]);
},
'wallet.product'
])->whereIn('wallet', $wallet->pluck('id'))->whereNull('fund_id');
TableEngine::create($records, $request->all())
->initSearch(['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();
}
}
return response()->json($records);
}
public function transactionTableb(Request $request)
{
$wallet = Wallet::with('product')->where('owner', auth('customer')->id())->whereNull('fund_id')->get();
if (is_null($wallet)) {
$records = ['query' => ['total' => 0], 'rows' => []];
} else {
$records = Transaction::with(['order' => function ($q) {
return $q->select(['*', Order::$SELECT_STS_STR]);
}, 'withdraw' => function ($q) {
return $q->select(['*', Order::$SELECT_STS_STR]);
}, 'wallet.product'])->whereIn('wallet', $wallet->pluck('id'))->whereNull('fund_id');
TableEngine::create($records, $request->all())
->initSearch(['customer.name'])
->select([
'*', Transaction::$SELECT_CAJ, Transaction::$SELECT_TYPE_STR, Transaction::$SELECT_SAJ, Transaction::$SELECT_EAJ
])->export($records);
}
foreach ($records['rows'] as &$record) {
return $record['wallet'];
if (isset($record->wallet->product->title)) {
$record->title = $record->wallet->product->title."22";
$record->save();
} else {
$record->title = $record->type_str."22";
$record->save();
}
}
return response()->json($records);
}
public function withdraw(Request $request)
{
$customer = auth('customer')->user();
$data = $request->all();
if (!isset($data['card_id'])) {
return response()->json(['message' => 'کارت انتخاب نشده'], 422);
}
$bCart = BankAccount::where('id', $data['card_id'])->first();
if ($bCart->sts != 2) {
return response()->json(['message' => 'کارت انتخاب شده غیر فعال است یا ردشده'], 422);
}
$wallet_interface_fund = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type);
$wallet = $wallet_interface_fund->wallet;
if ($data['value'] < 0) {
throw new UnprocessableEntityHttpException('مقدار برداشت باید مثبت باشد');
}
if ($data['value'] > $wallet->cash) {
throw new UnprocessableEntityHttpException('مقدار برداشت بیشتر از موجودی است');
}
$Withdraw = Withdraw::create([
'customer_id' => auth('customer')->id(),
"cash" => $data['value'],
"note" => "برداشت ریالی",
"bank_account_id" => $data['card_id'],
'sts' => 1,
'type' => 2
]);
$transaction = new TransactionInterface($wallet_interface_fund, $_inp = [
"cash" => $data['value'],
'expire_at' => now(),
'withdraw_id' => $Withdraw->id,
'active' => true
], 4, false, false);
$userMobile = $customer->mobile; // User's mobile number
$userName = $customer->name; // User's name
$withdrawAmount = $data['value']; // amount
$tahesab = new TahesabController();
$tahesab->wallet([
"customer_id" => auth('customer')->id(),
"value" => $data['value'],
"direction" => 1,
]);
$companyName = $this->Config('name_fa'); // Company name from configuration
$message = "کاربر $userName با شماره موبایل $userMobile درخواست برداشت مبلغ " . number_format($withdrawAmount) . " ریال را دارد. با تشکر، $companyName";
if (isset($request->otp)) {
if (Customer::where("id", $customer->id)->first()->token_sms != $request->otp) {
return response()->json(['message' => 'رمز دوم اشتباه میباشد'], 422);
}
try {
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $this->Config('liman_token')
])->post('https://limangold.ir/kernel/customer/fin', [
'receive_bank_shaba' => $bCart->shaba_number,
'receive_bank_card' => $bCart->cart_number,
'receive_name' => $customer->name,
'receive_account_number' => $bCart->account_number,
'description' => $this->Config('name_fa') . ' | ' . $customer->mobile . ' | ' . $customer->name . ' | ' . $Withdraw->id,
'amount' => '1000'
])->json();
if (isset($response['id'])) {
$Withdraw->note = $response['id'];
$Withdraw->save();
$message = "کاربر $userName با شماره موبایل $userMobile درخواست برداشت مبلغ " . number_format($withdrawAmount) . " ثبت کرد و به لیمان منتقل شد. با تشکر، $companyName";
} else {
$message = "کاربر $userName با شماره موبایل $userMobile درخواست برداشت مبلغ " . number_format($withdrawAmount) . " ریال را دارد (مشکل در اتصال به لیمان). با تشکر، $companyName";
}
} catch (Exception $e) {
$message = "کاربر $userName با شماره موبایل $userMobile درخواست برداشت مبلغ " . number_format($withdrawAmount) . " ریال را دارد (مشکل در اتصال به لیمان). با تشکر، $companyName";
}
}
User::where('send_sms', 1)->get()->each(function ($admin) use ($message) {
$this->Send($admin->mobile, $message . ' ' . now()->format('H:i:s'));
});
return response()->json($Withdraw);
}
public function withdraw_physical(Request $request)
{
$data = $request->all();
$data['customer'] = Customer::with([/*"wallet"*/])->where('id', auth('customer')->id())->first();
$data['customer']['wallet_interface'] = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type);
if (!isset($data['address_id'])) {
return response()->json(['message' => 'آدرس انتخاب نشده است'], 422);
}
$data['adress'] = Address::find($data['address_id']);
if (!$data['adress'] || $data['adress']->customer_id != $data['customer']->id) {
return response()->json(['message' => 'آدرس معتبر نیست یا متعلق به شما نمی‌باشد'], 403);
}
foreach ($data['wallets'] as $key => $wallet) {
$data['wallets'][$key]['wallet'] = Wallet::with(["product"])->find($wallet['wallet_id']);
if (!$data['wallets'][$key]['wallet']) {
return response()->json(['message' => 'ولت مورد نظر یافت نشد.'], 422);
}
if ($data['wallets'][$key]['wallet']->owner != $data['customer']['id']) {
return response()->json(['message' => 'ولت انتخاب شده متعلق به شما نمی‌باشد.'], 422);
}
// بررسی وجود محصول در ولت
if (!$data['wallets'][$key]['wallet']['product']) {
return response()->json(['message' => 'محصولی در ولت مورد نظر یافت نشد.'], 422);
}
if ($data['wallets'][$key]['value'] > $data['wallets'][$key]['wallet']['gold']){
return response()->json([
'message' => "شما به اندازه کافی از {$data['wallets'][$key]['wallet']['product']['title']} ندارید"
], 422);
}
if ($data['wallets'][$key]['value'] == 0) {
return response()->json([
'message' => "مقدار انتخابی نمی‌تواند صفر باشد. لطفاً مقدار صحیحی را برای {$data['wallets'][$key]['wallet']['product']['title']} وارد کنید."
], 422);
}
$data['wallets'][$key]['wallet_interface'] = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type, null, $data['wallets'][$key]['wallet']["product_id"]);
}
$data['withdraw'] = new Withdraw(); // Create a new instance of the Withdraw model
$data['withdraw']->note = "برداشت فیزیکی"; // 50 for phisicals
$data['withdraw']->address_id = $data['address_id'];
$data['withdraw']->customer_id = $data['customer']['id'];
$data['withdraw']->sts = 50;
$data['withdraw']->save();
foreach ($data['wallets'] as $key => $wallet) {
$data['wallets'][$key]['transaction'][] = new TransactionInterface($data['wallets'][$key]['wallet_interface'], $_inp = [
"gold" => $data['wallets'][$key]['value'],
"number" => $data['wallets'][$key]['wallet']['product']['amount']*$data['wallets'][$key]['value'],
"product_id" => $data['wallets'][$key]['wallet']['product']['id'],
'expire_at' => now(),
'active' => true,
'title' => " برداشت {$data['wallets'][$key]['wallet']['product']['title']}",
'note' => " برداشت محصول {$data['wallets'][$key]['wallet']['product']['title']} شماره سند {$data['withdraw']->id}",
'withdraw_id' => $data['withdraw']->id,
], 6, false);
}
return response()->json($data['withdraw']['id']);
}
public function indexWithdraw(Request $request){
$records = Withdraw::with(['customer','bank_account','Address',"Transaction"])->where("customer_id",auth('customer')->id());
TableEngine::create($records, $request->all())
->initSearch(['customer.name'])
->select([
'*',Withdraw::$SELECT_STS_STR,Withdraw::$SELECT_DAJ
])->export($records);
return response()->json($records);
}
public function index_withdraw_physical(Request $request){
$records = Withdraw::with(['customer','bank_account','Address',"Transaction.product"])->where("customer_id",auth('customer')->id())->whereBetween('sts', [49, 70]);
TableEngine::create($records, $request->all())
->initSearch(['customer.name'])
->select([
'*',Withdraw::$SELECT_STS_STR,Withdraw::$SELECT_DAJ
])->export($records);
return response()->json($records);
}
public function withdrawOtp(Request $request)
{
$customer = auth('customer')->user();
$otp = rand(1000, 9999);
$customer->token_sms = $otp;
$customer->save();
$this->Send(
$customer->mobile,
'رمز عبور یکبار مصرف شما جهت برداشت پول از اپلیکیشن ' . $this->Config('name_fa') . ' می باشد: ' . $otp
);
return response()->json(true);
}
}