2026-01-24 14:03:11 +00:00
<? php
namespace App\Http\Controllers\Admin ;
use App\DB\Te\TableEngine ;
use App\Http\Controllers\Controller ;
use App\Models\Customer ;
use App\Models\Order ;
use App\Models\Price ;
use App\Models\Product ;
use Illuminate\Support\Facades\DB ;
use App\Models\Withdraw ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\Log ;
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 ;
use App\Http\Controllers\Admin\TahesabController ;
2026-02-09 14:09:19 +03:30
use App\Models\Setting ;
2026-02-19 16:20:36 +03:30
use App\Models\User ;
2026-01-24 14:03:11 +00:00
class WalletController extends Controller
{
public function charge2 ( Request $request )
{
$data = $request -> all ();
$customer = Customer :: findOrFail ( $data [ 'customer_id' ]);
$deposit = false ;
if ( $data [ 'value' ] > 0 ) {
$deposit = true ;
}
$wallet = Wallet :: where ( 'owner' , $data [ 'customer_id' ]) -> whereNull ( 'fund_id' ) -> first ();
if ( is_null ( $wallet ) && ! $deposit ) {
throw new UnprocessableEntityHttpException ( 'مقدار موجودی کمتر از درخواست شماست' );
}
if ( ! is_null ( $wallet ) && $wallet -> cash < abs ( $data [ 'value' ]) && ! $deposit ) {
throw new UnprocessableEntityHttpException ( 'مقدار موجودی کمتر از درخواست شماست' );
}
$wallet_interface = new WalletInterface ( $data [ 'customer_id' ], $customer -> type );
$orders = Order :: where ( 'customer_id' , $data [ 'customer_id' ]) -> where ( 'sts' , 1 ) -> get ();
$gold_block = 0 ;
if ( count ( $orders ) > 0 ) {
$value = $data [ 'value' ];
foreach ( $orders as $order ) {
if ( $value < $order -> payment_remind ) {
$order -> payment_amount += $value ;
$order -> payment_remind -= $value ;
$order -> save ();
break ;
} else {
$value -= $order -> payment_remind ;
$order -> payment_amount = $order -> total_price ;
$order -> payment_remind = 0 ;
$order -> sts = 2 ;
$gold_block -= $order -> amount ;
$order -> save ();
}
}
}
$user = auth () -> user ();
$customer = Customer :: findOrFail ( $data [ "customer_id" ]);
$Withdraw = Withdraw :: create ([
'user_id' => $user -> id ,
'customer_id' => $customer -> id ,
"cash" => $data [ 'value' ],
'sts' => 4 ,
]);
new TransactionInterface ( $wallet_interface , $_inp = [
'cash' => $data [ 'value' ],
'expire_at' => now (),
'withdraw_id' => $Withdraw -> id ,
'active' => true
], 3 , false , true , $gold_block );
$message = 'کیف پول شما به مبلغ ' . $data [ 'value' ] . " ریال در تاریخ " . verta () -> format ( 'Y/n/j H:i:s' ) . " شارژ شد " . " \n "
. $this -> Config ( 'name_fa' );
$this -> Send ( $customer -> mobile , $message );
return response () -> json ( true );
}
public function charge ( Request $request )
{
$data = $request -> all ();
$customer = Customer :: findOrFail ( $data [ 'customer_id' ]);
$deposit = false ;
if ( $data [ 'value' ] > 0 ) {
$deposit = true ;
}
$wallet = Wallet :: where ( 'owner' , $data [ 'customer_id' ]) -> whereNull ( 'fund_id' ) -> first ();
if ( is_null ( $wallet ) && ! $deposit ) {
throw new UnprocessableEntityHttpException ( 'مقدار موجودی کمتر از درخواست شماست' );
}
if ( ! is_null ( $wallet ) && $wallet -> cash < abs ( $data [ 'value' ]) && ! $deposit ) {
throw new UnprocessableEntityHttpException ( 'مقدار موجودی کمتر از درخواست شماست' );
}
$wallet_interface = new WalletInterface ( $data [ 'customer_id' ], $customer -> type );
$orders = Order :: where ( 'customer_id' , $data [ 'customer_id' ]) -> where ( 'sts' , 1 ) -> get ();
$gold_block = 0 ;
$user = auth () -> user ();
$customer = Customer :: findOrFail ( $data [ "customer_id" ]);
$Withdraw = Withdraw :: create ([
'user_id' => $user -> id ,
'customer_id' => $customer -> id ,
"cash" => $data [ 'value' ],
"note" => $data [ 'note' ],
'sts' => 4 ,
]);
2026-06-07 12:25:32 +03:30
$type = $deposit ? 3 : 4 ;
2026-01-24 14:03:11 +00:00
new TransactionInterface ( $wallet_interface , $_inp = [
'gold' => 0 ,
'cash' => $data [ 'value' ],
'expire_at' => now (),
"note" => $data [ 'note' ],
'withdraw_id' => $Withdraw -> id ,
'active' => true
2026-06-07 12:25:32 +03:30
], $type , true , $deposit , $gold_block );
2026-01-24 14:03:11 +00:00
2026-02-19 16:20:36 +03:30
if ( $this -> Config ( 'send_to_tahesab' ) == "true" ){
2026-02-09 14:09:19 +03:30
try {
2026-03-14 11:08:30 +03:30
$data [ 'direction' ] = $deposit ? 0 : 1 ;
2026-02-09 14:09:19 +03:30
$tahesab = new TahesabController ();
$tahesab -> wallet ( $data );
} catch ( \Exception $e ){
return response ( $e -> getMessage (), 500 );
}
}
2026-02-19 16:20:36 +03:30
//send sms to users and customer
2026-01-25 09:58:32 +00:00
$status = $data [ 'value' ] > 0 ? " شارژ شد " : " کسر شد " ;
2026-02-19 16:20:36 +03:30
if ( $this -> Config ( 'charge_notif' ) == "true" ){
$message = 'کیف پول کاربر ' . $customer -> name . ' به مقدار ' . abs ( $data [ 'value' ]) . " ریال در تاریخ " . verta () -> format ( 'Y/n/j H:i:s' ) . $status . "بابت" . " \n "
. $data [ 'note' ]
. " \n "
. $this -> Config ( 'name_fa' );
User :: where ( 'send_sms' , 1 ) -> get () -> each ( function ( $admin ) use ( $message , $data ) {
$this -> send ( $admin -> mobile , $message );
});
}
2026-01-25 09:58:32 +00:00
$message = 'کیف پول شما به اندازه ' . number_format ( abs ( $data [ 'value' ])) . " ریال در تاریخ " . verta () -> format ( 'Y/n/j H:i:s' ) . $status . "بابت" . " \n "
2026-01-24 14:03:11 +00:00
. $data [ 'note' ]
2026-01-25 09:58:32 +00:00
. " \n "
2026-01-24 14:03:11 +00:00
. $this -> Config ( 'name_fa' );
$this -> Send ( $customer -> mobile , $message );
return response () -> json ( true );
}
public function charge_gold ( Request $request )
{
$data = $request -> all ();
$customer = Customer :: findOrFail ( $data [ 'customer_id' ]);
$deposit = false ;
if ( $data [ 'value' ] > 0 ) {
$deposit = true ;
}
$wallet = Wallet :: where ( 'owner' , $data [ 'customer_id' ]) -> whereNull ( 'fund_id' ) -> first ();
// if (is_null($wallet) && !$deposit) {
// throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست');
// }
// if (!is_null($wallet) && $wallet->cash < abs($data['value']) && !$deposit) {
// throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست');
// }
$wallet_interface = new WalletInterface ( $data [ 'customer_id' ], $customer -> type );
$gold_block = 0 ;
$user = auth () -> user ();
$customer = Customer :: findOrFail ( $data [ "customer_id" ]);
$Withdraw = Withdraw :: create ([
'user_id' => $user -> id ,
'customer_id' => $customer -> id ,
"gold" => $data [ 'value' ],
"note" => $data [ 'note' ],
'sts' => 4 ,
]);
new TransactionInterface ( $wallet_interface , $_inp = [
'cash' => 0 ,
'gold' => $data [ 'value' ],
'expire_at' => now (),
"note" => $data [ 'note' ],
'withdraw_id' => $Withdraw -> id ,
'active' => true
], 7 , $deposit , false , $gold_block );
2026-06-09 20:08:06 +03:30
//send to tahesab
2026-03-14 11:08:30 +03:30
if ( $this -> Config ( 'send_to_tahesab' ) == "true" ){
try {
2026-06-07 14:30:26 +03:30
$data [ 'direction' ] = $deposit ? 1 : 0 ;
2026-03-14 11:08:30 +03:30
$tahesab = new TahesabController ();
$tahesab -> walletGold ( $data );
} catch ( \Exception $e ){
return response ( $e -> getMessage (), 500 );
}
}
2026-02-19 16:20:36 +03:30
//send sms to users and customer
$status = $data [ 'value' ] > 0 ? " شارژ شد " : " کسر شد " ;
if ( $this -> Config ( 'charge_notif' ) == "true" ){
$message = 'کیف پول کاربر ' . $customer -> name . ' به مقدار ' . number_format ( abs ( $data [ 'value' ])) . " گرم در تاریخ " . verta () -> format ( 'Y/n/j H:i:s' ) . $status . "بابت" . " \n "
. $data [ 'note' ]
. " \n "
. $this -> Config ( 'name_fa' );
User :: where ( 'send_sms' , 1 ) -> get () -> each ( function ( $admin ) use ( $message , $data ) {
$this -> send ( $admin -> mobile , $message );
});
}
2026-01-24 14:03:11 +00:00
$message = 'کیف پول شما به اندازه ' . $data [ 'value' ] . " گرم در تاریخ " . verta () -> format ( 'Y/n/j H:i:s' ) . " شارژ شد بابت" . " \n "
. $data [ 'note' ]
. $this -> Config ( 'name_fa' );
$this -> Send ( $customer -> mobile , $message );
return response () -> json ( true );
}
public function index ( Request $request )
{
$records = Transaction :: with ([ 'customer' ]) -> whereNull ( 'fund_id' );
TableEngine :: create ( $records , $request -> all ())
-> initSearch ([ 'order_id' , '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 updateTransaction ( Request $request )
{
$message = 'تراکنش تغیری نکرده.' ;
$transaction = Transaction :: find ( $request -> id );
$product = Product :: find ( $transaction -> product_id );
if ( ! $transaction ) return response () -> json ([ 'error' => 'تراکنش یافت نشد.' ], 404 );
$difference = $request -> input ( 'number' ) - $transaction -> number ;
if ( abs ( $difference ) < 0.0001 ) {
return response () -> json ([ 'message' => $message ], 422 );
}
$transaction -> number = $request -> input ( 'number' );
$wallet_interface = new WalletInterface ( $transaction -> customer_id , 2 );
if ( $wallet_interface -> wallet -> gold < $difference ) {
if ( ! ( isset ( $request -> force ) && $request -> force === true )){
return response () -> json ([ 'message' => "کاربر مورد نظر موجودی ندارد. موجودی فعلی: " . $wallet_interface -> wallet -> gold . " مقدار مورد نیاز: " . $difference , 'type' => 406 ], 422 );
}
}
$isDiposit = $difference > 0 ? 0 : 1 ;
new TransactionInterface ( $wallet_interface , [
"title" => "اصلاح سند " . $transaction -> withdraw_id ,
"note" => $product -> title ,
"product_id" => $product -> id ,
"number" => 0 ,
"gold" => $difference ,
'expire_at' => now (),
'active' => true ,
'withdraw_id' => $transaction -> withdraw_id ,
'start_at' => now (),
], 51 , $isDiposit , false );
$transaction -> save ();
$message = 'تراکنش با موفقیت بهروزرسانی شد.' ;
return response () -> json ([
'message' => $message ,
'transaction' => $transaction ,
// 'previous_amount' => $previousAmount,
// 'new_amount' => $newAmount,
'difference' => $difference ,
'wallet' => $wallet_interface -> wallet ,
]);
}
public function indexWallet ( Request $request )
{
2026-06-09 20:32:27 +03:30
$lastPrice = ( float ) ( Price :: latest () -> first () ?-> price ?? 0 );
// همان فیلتر/جستجوی جدول — بدون صفحهبندی — برای مجموع ردیفهای قابل مشاهده
$sumQuery = Wallet :: query ();
TableEngine :: create ( $sumQuery , $request -> all ())
-> initSearch ([ 'customer.name' ]);
$totalGold = ( float ) ( clone $sumQuery ) -> sum ( 'gold' );
$totalCash = ( float ) ( clone $sumQuery ) -> sum ( 'cash' );
$sums = [
'gold' => round ( $totalGold , 2 ),
'goldToCash' => round ( $totalGold * $lastPrice , 2 ),
'cash' => round ( $totalCash , 2 ),
'cashToGold' => $lastPrice > 0 ? round ( $totalCash / $lastPrice , 2 ) : 0 ,
];
2026-01-24 14:03:11 +00:00
$records = Wallet :: with ( 'customer' );
TableEngine :: create ( $records , $request -> all ())
-> initSearch ([ 'customer.name' ])
-> select ([ '*' ])
-> export ( $records );
$records [ 'sums' ] = $sums ;
return response () -> json ( $records );
}
public function indexWithdraw ( Request $request )
{
$records = Withdraw :: with ([ 'customer' , 'bank_account' , 'Address' , "Transaction" ]);
TableEngine :: create ( $records , $request -> all ())
-> initSearch ([ 'customer.name' ])
-> select ([
'*' , Withdraw :: $SELECT_STS_STR , Withdraw :: $SELECT_DAJ
]) -> export ( $records );
return response () -> json ( $records );
}
public function updateWithdraw ( Request $request , $id )
{
// پیدا کردن رکورد Withdraw با استفاده از شناسه
$withdraw = Withdraw :: find ( $id );
// بررسی وجود رکورد
if ( ! $withdraw ) {
return response () -> json ([ 'error' => 'Withdraw not found' ], 404 );
}
$withdraw -> sts = $request -> sts ;
$withdraw -> save ();
return response () -> json ([ 'message' => 'Withdraw updated successfully' , 'withdraw' => $withdraw ]);
}
public function getWithdraw ( Request $request , $id )
{
2026-06-09 20:08:06 +03:30
$withdraw = Withdraw :: with ([ 'customer' , 'bank_account' , 'Address' , "Transaction" ]) -> find ( $id );
2026-01-24 14:03:11 +00:00
if ( ! $withdraw ) {
return response () -> json ([ 'error' => 'Withdraw not found' ], 404 );
}
2026-06-09 20:08:06 +03:30
return response () -> json ( $withdraw );
2026-01-24 14:03:11 +00:00
}
public function completeWithdraw ( Request $request )
{
2026-06-09 20:08:06 +03:30
$request -> validate ([
'id' => 'required|integer|exists:withdraws,id' ,
'tracked_number' => 'required|string|max:50' ,
'date_at' => 'nullable|date' ,
]);
$data = $request -> only ([ 'tracked_number' , 'date_at' ]);
2026-01-24 14:03:11 +00:00
$data [ 'user_id' ] = auth ( 'user' ) -> id ();
$data [ 'sts' ] = 2 ;
2026-06-09 20:08:06 +03:30
$data [ 'tracked_number' ] = ( string ) $data [ 'tracked_number' ];
if ( empty ( $data [ 'date_at' ])) {
$data [ 'date_at' ] = now ();
}
$withdraw = Withdraw :: findOrFail ( $request -> id );
2026-01-24 14:03:11 +00:00
$withdraw -> update ( $data );
// $tahesab = new TahesabController();
// $tahesab->wallet([
// 'customer_id' => $withdraw->customer_id,
// 'value' => $withdraw->cash,
// 'direction' => 1,
// 'note' => 'برداشت از کیف پول شماره ' . $withdraw->id,
// ]);
return response () -> json ( true );
}
}