2026-06-04 12:18:53 +03:30
<? php
namespace App\Http\Controllers\Site ;
use App\Http\Controllers\Controller ;
use App\Models\Cnt ;
use App\Models\Customer ;
use App\Models\Order ;
use App\Models\Price ;
use App\Models\TransactionsAdmin ;
use App\Models\User ;
use App\Models\Withdraw ;
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 RequestsController extends Controller
{
public function order ( Request $request )
{
$data = $request -> all ();
$transactionType = $data [ 'method' ] == 'cash' ? 'ریالی' : 'طلایی' ;
$customer_id = Customer :: where ( 'mobile' , $request -> mobile ) -> first () ?-> id ;
if ( ! $customer_id ) {
throw new UnprocessableEntityHttpException ( 'کاربری با این شماره موبایل یافت نشد.' );
}
$data [ 'customer_id' ] = $customer_id ;
$latestPrice = Price :: latest () -> first ();
$data [ 'price' ] = $latestPrice -> price ;
$data [ 'price_id' ] = $latestPrice -> id ;
$data [ 'difference' ] = 0 ;
$data [ 'service_charge_percentage' ] = 0 ;
$data [ 'pay_at' ] = now ();
$data [ 'sts' ] = 2 ;
$data [ 'type' ] = 2 ;
$data [ 'payment_sts' ] = 4 ;
$data [ 'price_original' ] = $data [ 'price' ];
$data [ 'total_price_original' ] = $data [ 'amount' ] * ( $data [ 'price_original' ] ?? 0 );
$data [ 'payment_remind' ] = 0 ;
$data [ 'fee' ] = $data [ 'difference' ];
$data [ 'total_fee' ] = $data [ 'amount' ] * $data [ 'difference' ];
$myCustomer = Customer :: find ( $customer_id );
2026-06-15 11:25:10 +03:30
$ownerType = ( int ) ( $myCustomer ?-> type ?? 2 );
$wallet_interface = new WalletInterface ( $customer_id , $ownerType );
$wallet = $wallet_interface -> wallet ;
2026-06-04 12:18:53 +03:30
$response = [];
// === اینجا قبلاً return بود و همه چیز قطع میشد ===
if ( $data [ 'method' ] == 'cash' ) {
$response [ 'cash' ] = $this -> processCashTransaction ( $data , $wallet , $wallet_interface , $myCustomer );
} else if ( $data [ 'method' ] == 'gold' ) {
$response [ 'gold' ] = $this -> processGoldTransaction ( $data , $wallet , $wallet_interface , $myCustomer );
}
$response [ 'Notifications' ] = $this -> sendTransactionNotifications ( $data , $transactionType );
return response () -> json ( $response );
}
public function getWallet ( Request $request )
{
$customer_id = Customer :: where ( 'mobile' , $request -> mobile ) -> first () ?-> id ;
if ( $customer_id && $customer_id !== null ){
2026-06-26 14:27:50 +03:30
$wallet = Wallet :: select ([ 'owner' , 'gold' , 'cash' ])
-> where ( 'owner' , $customer_id )
-> whereNull ( 'fund_id' )
-> whereNull ( 'product_id' )
-> first ();
2026-06-04 12:18:53 +03:30
} else {
throw new UnprocessableEntityHttpException ( 'کاربری با این شماره موبایل یافت نشد.' );
}
return $wallet ;
}
//for orders
private function processCashTransaction ( & $data , $wallet , $wallet_interface , $myCustomer )
{
$response = [];
// $data['price'] += $data['difference'];
$data [ 'total_price' ] = $data [ 'amount' ] /** $data['price']*/ ;
$data [ 'payment_amount' ] = $data [ 'total_price' ];
// $data['service_charge'] = $data['service_charge_percentage'] * $data['amount'];
// $data['service_charge_price'] = $data['service_charge'] * $data['price_original'];
// $data['amount'] -= $data['service_charge'];
if ( $wallet -> cash < $data [ 'payment_amount' ]) {
throw new UnprocessableEntityHttpException ( 'موجودی شما کمتر از میزان سفارش است' );
}
// $data['order'] = $order = Order::create($data);
new TransactionInterface ( $wallet_interface , [
"cash" => $data [ 'payment_amount' ],
"gold" => 0 ,
'expire_at' => now (),
'active' => true ,
// 'order_id' => $order->id,
'start_at' => now (),
], 4 , true , false );
$response [ '1' ] = 2 ;
return $response ;
}
private function processGoldTransaction ( & $data , $wallet , $wallet_interface , $myCustomer )
{
// if ($data['amount'] < 0.06){
// throw new UnprocessableEntityHttpException('کمترین مقدار مجاز برای فروش 60 سوت است.');
//
// }
// $data['price'] -= $data['difference'];
$montifyCompanyName = $this -> Config ( 'montify_name' );
$data [ 'total_price' ] = $data [ 'amount' ] * $data [ 'price' ];
$data [ 'payment_amount' ] = $data [ 'total_price' ];
$data [ 'service_charge' ] = $data [ 'service_charge_percentage' ] * $data [ 'amount' ];
$data [ 'service_charge_price' ] = $data [ 'service_charge' ] * $data [ 'price_original' ];
$data [ 'total_price' ] = $data [ 'amount' ] * $data [ 'price' ] - $data [ 'service_charge_price' ];
$data [ 'payment_amount' ] = $data [ 'total_price' ];
$data [ 'payment_amount' ] -= $data [ 'service_charge_price' ];
if ( $wallet -> gold < $data [ 'amount' ]) {
throw new UnprocessableEntityHttpException ( 'موجودی شما کمتر از میزان سفارش است' );
}
$data [ 'direction' ] = 2 ;
2026-11-18 18:25:34 +03:30
$data [ 'description' ] = "فروش { $data [ 'amount' ] } کیلوگرم طلا برای $montifyCompanyName توسط کاربر { $myCustomer -> name } با شماره موبایل { $myCustomer -> mobile } " ;
2026-06-04 12:18:53 +03:30
$data [ 'order' ] = $order = Order :: create ( $data );
new TransactionInterface ( $wallet_interface , [
"gold" => $data [ 'amount' ],
"cash" => 0 ,
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id
], 10 , false , true );
}
private function sendTransactionNotifications ( array $data , string $transactionType )
{
$customer = Customer :: findOrFail ( $data [ 'customer_id' ]);
$companyName = $this -> Config ( 'name_fa' );
$montifyCompanyName = $this -> Config ( 'montify_name' );
if ( $data [ 'method' ] == 'cash' ) {
$message = "کاربر { $customer -> name } با شماره موبایل { $customer -> mobile } اقدام به برداشت موجودی از طرف $montifyCompanyName به مقدار " .
" { $data [ 'amount' ] } ریال " . " کرد. با تشکر، $companyName " ;
} elseif ( $data [ 'method' ] == 'gold' ) {
2026-08-10 16:48:53 +03:30
$amountLabel = self :: formatSmsGoldWeight ( $data [ 'amount' ]);
2026-06-04 12:18:53 +03:30
$message = "کاربر { $customer -> name } با شماره موبایل { $customer -> mobile } اقدام به $transactionType طلا به مقدار " .
2026-11-18 18:25:34 +03:30
" { $amountLabel } کیلوگرم به مبلغ " . number_format ( $data [ 'total_price' ]) .
2026-06-04 12:18:53 +03:30
" ریال کرد. با تشکر، $companyName " ;
}
User :: where ( 'send_sms' , 1 ) -> get () -> each ( function ( $admin ) use ( $message , $data , $transactionType , $companyName ) {
2026-07-18 22:22:01 +03:30
if ( self :: isSmsNotifEnabled ( 'sms_notif_fund_withdraw_admin' )) {
$this -> send ( $admin -> mobile , $message );
}
2026-06-04 12:18:53 +03:30
});
// User::whereNotNull('telegram')->get()->each(function ($admin) use ($message, $data, $transactionType, $companyName) {
// $this->SendTelegram($admin->telegram, $message);
// });
// $messageold = "کاربر {$customer->name} با شماره موبایل {$customer->mobile} اقدام به $transactionType طلا به مقدار " .
2026-11-18 18:25:34 +03:30
// "{$data['amount']} کیلوگرم به مبلغ " . number_format($data['total_price']) .
2026-06-04 12:18:53 +03:30
// " ریال کرد. با تشکر، $companyName";
// $azOrTa = $data['direction'] == 1 ? 'از' : 'به';
// $messageExam = "
// {$transactionType} قلک طلا $azOrTa شما
2026-11-18 18:25:34 +03:30
// {$data['amount']} کیلوگرم
2026-06-04 12:18:53 +03:30
// در مظنه
// " . number_format($data['price_original']) . " ریال";
//
//
//// User::whereNotNull('telegram')->get()->each(function ($admin) use ($messageExam) {
//// $this->SendTelegram($admin->telegram, $messageExam);
//// });
// User::where('send_sms', 1)->get()->each(function ($admin) use ($messageExam) {
// $this->send($admin->mobile, $messageExam);
// });
2026-06-14 21:09:43 +03:30
$wallet = Wallet :: where ( 'owner' , $data [ 'customer_id' ])
-> whereNull ( 'fund_id' )
-> whereNull ( 'product_id' )
-> first ();
2026-06-04 12:18:53 +03:30
if ( $data [ 'method' ] == 'cash' ) {
$pricePerGram = $data [ 'total_price' ];
} elseif ( $data [ 'method' ] == 'gold' ) {
$pricePerGram = $data [ 'total_price' ] / $data [ 'amount' ];
}
2026-06-14 21:09:43 +03:30
$goldBalance = $wallet ? ( $wallet -> gold ?? 0 ) : 0 ;
$cashBalance = $wallet ? ( $wallet -> cash ?? 0 ) : 0 ;
2026-06-04 12:18:53 +03:30
if ( $data [ 'method' ] == 'cash' ) {
$customerMessage = " $companyName \n برداشت ریالی \n مبلغ: { $data [ 'amount' ] } \n " .
2026-06-14 21:09:43 +03:30
" \n موجودی طلایی: { $goldBalance } \n " .
2026-07-21 13:25:18 +03:30
"مانده ریالی: " . number_format ( $cashBalance ) . " \n " . self :: publicSiteUrl ( request ());
2026-06-04 12:18:53 +03:30
} elseif ( $data [ 'method' ] == 'gold' ) {
2026-11-18 18:25:34 +03:30
$customerMessage = " $companyName \n برداشت طلایی \n کیلوگرم: { $data [ 'amount' ] } \n قیمت کیلوگرم: " . number_format ( $pricePerGram ) .
2026-06-14 21:09:43 +03:30
" \n مبلغ کل: " . number_format ( $data [ 'total_price' ]) . " \n موجودی طلایی: { $goldBalance } \n " .
2026-07-21 13:25:18 +03:30
"مانده ریالی: " . number_format ( $cashBalance ) . " \n " . self :: publicSiteUrl ( request ());
2026-06-04 12:18:53 +03:30
}
2026-11-18 18:25:34 +03:30
// $customerMessage = "$companyName\n$transactionType آبشده\nکیلوگرم: {$data['amount']}\nقیمت کیلوگرم: " . number_format($pricePerGram) .
2026-06-04 12:18:53 +03:30
// "\nمبلغ کل: " . number_format($data['total_price']) . "\n\nموجودی طلایی: {$wallet->gold}\n" .
// "مانده ریالی: " . number_format($wallet->cash) . "\n" . $this->Config('app_url');
2026-07-18 22:22:01 +03:30
if ( self :: isSmsNotifEnabled ( 'sms_notif_fund_withdraw_customer' )) {
$this -> send ( $customer -> mobile , $customerMessage );
}
2026-07-27 22:09:08 +03:30
try {
$title = ( $data [ 'method' ] ?? '' ) === 'gold' ? 'برداشت طلایی' : 'برداشت ریالی' ;
$body = ( $data [ 'method' ] ?? '' ) === 'gold'
2026-11-18 18:25:34 +03:30
? 'کیلوگرم: ' . ( $data [ 'amount' ] ?? '' ) . " \n مبلغ کل: " . number_format (( float ) ( $data [ 'total_price' ] ?? 0 )) . ' ریال'
2026-07-27 22:09:08 +03:30
: 'مبلغ: ' . number_format (( float ) ( $data [ 'amount' ] ?? 0 )) . ' ریال' ;
app ( \App\Services\CustomerAppNotifyService :: class ) -> notifyCustomer (
( int ) $customer -> id ,
$title ,
$body ,
'info'
);
} catch ( \Throwable $e ) {
report ( $e );
}
2026-06-04 12:18:53 +03:30
}
}