2026-01-24 14:03:11 +00:00
<? php
namespace App\Http\Controllers\Single ;
use App\DB\Te\TableEngine ;
use App\Http\Controllers\Controller ;
use App\Http\Controllers\Admin\TahesabController ;
use App\Models\Customer ;
use App\Models\Order ;
use App\Models\Price ;
use App\Models\Product ;
use App\Models\User ;
use App\Mail\YourMailableClass ;
use Illuminate\Support\Facades\Mail ;
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 App\Models\TransactionsAdmin ;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException ;
use Illuminate\Support\Facades\Http ;
2026-02-09 14:09:19 +03:30
use App\Models\Setting ;
2026-03-10 15:39:30 +03:30
use App\Models\Cnt ;
2026-05-25 12:47:04 +03:30
use App\Services\MontzarService ;
use App\Services\ConfigService ;
2026-01-24 14:03:11 +00:00
class SingleOrderController extends Controller
{
public function store ( Request $request )
{
2026-04-04 07:47:17 +00:00
$data = $request -> all ();
2026-06-03 19:53:14 +03:30
$data [ 'is_katod' ] = ( $data [ 'method' ] ?? null ) === 'katod' ;
if ( $data [ 'is_katod' ]) {
2026-06-03 19:45:08 +03:30
$data [ 'method' ] = 'gold' ;
}
2026-05-25 12:47:04 +03:30
$config = app ( ConfigService :: class );
2026-04-04 07:47:17 +00:00
2026-05-25 12:47:04 +03:30
$toBool = function ( $value ) {
return filter_var ( $value , FILTER_VALIDATE_BOOLEAN );
};
2026-05-24 22:29:43 +03:30
2026-06-04 12:18:53 +03:30
if ( $config -> usesMontzarPricing ()) {
2026-05-25 12:47:04 +03:30
$montzar_closed = $toBool ( $config -> Config ( 'montzar_closed' ));
$montzar_buy = ! $toBool ( $config -> Config ( 'montzar_open_buy' )) && ( int ) $data [ 'direction' ] === 1 ;
$montzar_sell = ! $toBool ( $config -> Config ( 'montzar_open_sell' )) && ( int ) $data [ 'direction' ] === 2 ;
2026-05-24 22:29:43 +03:30
2026-05-25 12:47:04 +03:30
if ( $montzar_closed || $montzar_buy || $montzar_sell ) {
2026-04-28 20:15:23 +00:00
throw new UnprocessableEntityHttpException ( 'عدم ارائه سرویس.' );
}
2026-03-15 09:30:06 +00:00
}
2026-05-25 12:47:04 +03:30
if ( $toBool ( $config -> Config ( 'close_price' ))) {
if ( Cnt :: where ( 'key' , 'app_closed' ) -> value ( 'value' ) == "true" ) {
2026-05-11 17:21:56 +00:00
throw new UnprocessableEntityHttpException ( 'عدم ارائه سرویس.' );
}
}
2026-03-15 09:30:06 +00:00
2026-01-24 14:03:11 +00:00
// throw new UnprocessableEntityHttpException('به دلیل نوسانات سفارشات بسته میباشد.');
$transactionType = $data [ 'direction' ] == 1 ? "خرید" : "فروش" ;
if ( auth ( 'customer' ) -> id ()) {
$data [ 'customer_id' ] = auth ( 'customer' ) -> id ();
$latestPrice = Price :: latest () -> first ();
2026-05-02 18:47:02 +00:00
2026-05-28 11:53:27 +03:30
if ( ! $latestPrice || $latestPrice -> price == 0 ) {
2026-05-02 18:47:02 +00:00
throw new UnprocessableEntityHttpException ( 'عدم ارائه سرویس.' );
}
2026-01-24 14:03:11 +00:00
$data [ 'all_price' ] = $latestPrice ;
$data [ 'price' ] = $latestPrice -> price ;
$data [ 'price_id' ] = $latestPrice -> id ;
2026-05-28 11:53:27 +03:30
$feeCash = Setting :: where ( 'key' , 'fee_cash' ) -> first ();
$feeServiceCharge = Setting :: where ( 'key' , 'fee_service_charge_percentage' ) -> first ();
if ( ! $feeCash || ! $feeServiceCharge ) {
throw new UnprocessableEntityHttpException ( 'تنظیمات کارمزد یافت نشد.' );
}
2026-06-04 13:48:37 +03:30
$data [ 'difference' ] = ( float ) $feeCash -> value ;
$data [ 'service_charge_percentage' ] = ( float ) $feeServiceCharge -> value ;
2026-02-10 11:19:51 +03:30
$fee_free = Setting :: where ( 'key' , 'fee_free' ) -> where ( 'is_active' , true ) -> value ( 'value' );
2026-03-10 15:39:30 +03:30
if ( $fee_free && $this -> feeFree ( $data [ 'customer_id' ], $fee_free )) {
2026-02-10 11:19:51 +03:30
$data [ 'service_charge_percentage' ] = 0 ;
2026-02-22 15:06:01 +03:30
// $data['difference'] = 0;
2026-02-10 11:19:51 +03:30
$data [ 'fee_free' ] = true ;
}
2026-01-24 14:03:11 +00:00
$data [ 'pay_at' ] = ( new \DateTime ()) -> format ( 'Y-m-d H:i:s' );
} else if ( auth ( 'user' ) -> id ()) { //admin
$data [ 'service_charge_percentage' ] = 0 ;
}
2026-03-31 18:28:27 +03:30
if ( $data [ 'price' ] === 0 ){
throw new UnprocessableEntityHttpException ( ' سفارشات بسته میباشد.' );
}
2026-01-24 14:03:11 +00:00
$data [ 'sts' ] = 2 ;
$data [ 'type' ] = 2 ;
$data [ 'payment_sts' ] = 4 ;
$data [ 'price_original' ] = $data [ 'price' ];
$data [ 'payment_remind' ] = 0 ;
$data [ 'fee' ] = $data [ 'difference' ];
$wallet_interface = new WalletInterface ( $data [ 'customer_id' ], 2 );
$wallet = Wallet :: where ( 'owner' , $data [ 'customer_id' ]) -> whereNull ( 'fund_id' ) -> first ();
2026-05-28 11:53:27 +03:30
if ( ! $wallet ) {
throw new UnprocessableEntityHttpException ( 'کیف پول یافت نشد.' );
}
2026-01-24 14:03:11 +00:00
$response = [];
// Check if the user wants to transact with a product or non-product (gold)
if ( isset ( $data [ 'product_id' ]) and $data [ 'product' ] = Product :: find ( $data [ 'product_id' ])) {
// Check if the user wants to buy
if ( $data [ 'direction' ] == 1 ) {
2026-02-22 15:06:01 +03:30
$data [ 'service_charge_percentage' ] = isset ( $data [ 'fee_free' ]) && $data [ 'fee_free' ] === true ? 0 : ( $data [ 'product' ][ 'percent_fee_buy' ]) * $data [ 'number' ];
2026-01-24 14:03:11 +00:00
$data [ 'difference' ] = $data [ 'fee' ] = $data [ 'product' ][ 'cash_fee_buy' ];
2026-02-22 15:06:01 +03:30
$data [ 'total_fee' ] = $data [ 'number' ] * $data [ 'fee' ];
$data [ 'price_original' ] = $data [ 'product' ][ 'price' ] + $data [ 'product' ][ 'price_profit' ];
2026-01-24 14:03:11 +00:00
$data [ 'total_price_original' ] = $data [ 'product' ][ 'amount' ] * ( $data [ 'price_original' ] ?? 0 );
// Check if the method is cash
if ( $data [ 'method' ] == "cash" ) {
2026-03-10 15:39:30 +03:30
$response [ 'buy' ] = $this -> processBuyTransactionProduct ( $data , $wallet , $wallet_interface );
2026-01-24 14:03:11 +00:00
} // Check if the method is gold
else if ( $data [ 'method' ] == "gold" ) {
$response [ 'buy' ] = $this -> processBuyTransactionProductByGold ( $data , $wallet , $wallet_interface );
2026-06-03 19:41:00 +03:30
} else {
throw new UnprocessableEntityHttpException ( 'روش پرداخت نامعتبر است.' );
2026-01-24 14:03:11 +00:00
}
} // Check if the user wants to sell
else if ( $data [ 'direction' ] == 2 ) {
2026-02-22 15:06:01 +03:30
$data [ 'service_charge_percentage' ] = isset ( $data [ 'fee_free' ]) && $data [ 'fee_free' ] === true ? 0 : ( $data [ 'product' ][ 'percent_fee_sell' ]) * $data [ 'number' ];
2026-01-24 14:03:11 +00:00
$data [ 'difference' ] = $data [ 'fee' ] = $data [ 'product' ][ 'cash_fee_sell' ];
2026-02-22 15:06:01 +03:30
$data [ 'total_fee' ] = $data [ 'number' ] * $data [ 'fee' ];
$data [ 'price_original' ] = $data [ 'product' ][ 'price' ] + $data [ 'product' ][ 'price_profit' ];
2026-01-24 14:03:11 +00:00
$data [ 'total_price_original' ] = $data [ 'product' ][ 'amount' ] * ( $data [ 'price_original' ] ?? 0 );
// Check if the method is cash
if ( $data [ 'method' ] == "cash" ) {
$response [ 'sell' ] = $this -> processSellTransactionProduct ( $data , $wallet , $wallet_interface );
} // Check if the method is gold
else if ( $data [ 'method' ] == "gold" ) {
$response [ 'sell' ] = $this -> processSellTransactionProductByGold ( $data , $wallet , $wallet_interface );
2026-06-03 19:41:00 +03:30
} else {
throw new UnprocessableEntityHttpException ( 'روش پرداخت نامعتبر است.' );
2026-01-24 14:03:11 +00:00
}
}
} else {
// Calculate total price and fee for non-product (gold) transaction
$data [ 'total_price_original' ] = $data [ 'amount' ] * ( $data [ 'price_original' ] ?? 0 );
2026-05-28 11:53:27 +03:30
$mainProductId = Cnt :: where ( 'key' , 'main_product' ) -> value ( 'value' );
2026-06-04 13:48:37 +03:30
$productId = $data [ 'abshode_id' ] ?? $data [ 'product_id' ] ?? $mainProductId ;
$data [ 'product' ] = $productId ? Product :: find ( $productId ) : null ;
2026-05-28 11:53:27 +03:30
if ( ! $data [ 'product' ]) {
throw new UnprocessableEntityHttpException ( 'محصول آبشده یافت نشد.' );
}
2026-06-04 13:48:37 +03:30
if ( $data [ 'direction' ] == 1 ) {
$data [ 'difference' ] = $data [ 'product' ][ 'cash_fee_buy' ];
$data [ 'service_charge_percentage' ] = $data [ 'product' ][ 'percent_fee_buy' ];
} elseif ( $data [ 'direction' ] == 2 ) {
$data [ 'difference' ] = $data [ 'product' ][ 'cash_fee_sell' ];
$data [ 'service_charge_percentage' ] = $data [ 'product' ][ 'percent_fee_sell' ];
}
$data [ 'total_fee' ] = $data [ 'amount' ] * $data [ 'difference' ];
2026-05-28 11:53:27 +03:30
$data [ 'product_id' ] = $data [ 'product' ] -> id ;
2026-01-24 14:03:11 +00:00
// Check if the user wants to buy
if ( $data [ 'direction' ] == 1 ) {
if ( ! $data [ 'product' ][ 'open_buy' ]) {
throw new UnprocessableEntityHttpException ( 'خرید این محصول در این لحظه مقدور نمیباشد' );
}
$response [ 'buy' ] = $this -> processBuyTransaction ( $data , $wallet , $wallet_interface );
} // Check if the user wants to sell
else if ( $data [ 'direction' ] == 2 ) {
if ( ! $data [ 'product' ][ 'open_sell' ]) {
throw new UnprocessableEntityHttpException ( 'خرید این محصول در این لحظه مقدور نمیباشد' );
}
$response [ 'sell' ] = $this -> processSellTransaction ( $data , $wallet , $wallet_interface );
}
}
2026-06-04 13:48:37 +03:30
if ( $config -> getString ( 'send_to_tahesab' , 'false' ) === 'true' ) {
2026-02-09 14:09:19 +03:30
try {
$tahesab = new TahesabController ();
$data [ 'tahesab' ] = $tahesab -> storeTrade ( $data );
2026-03-10 15:39:30 +03:30
} catch ( \Exception $e ) {
2026-02-09 14:09:19 +03:30
return response ( $e -> getMessage (), 500 );
}
2026-02-08 08:02:11 +00:00
}
2026-01-24 14:03:11 +00:00
// $response['reward'] = $this->give_order_reward($data, $wallet, $wallet_interface);
2026-06-03 19:41:00 +03:30
if ( isset ( $data [ 'order' ])) {
$response [ 'Notifications' ] = $this -> sendTransactionNotifications ( $data , $transactionType );
}
2026-01-24 14:03:11 +00:00
return response () -> json ([ $response , $data ]);
}
private function processBuyTransaction ( & $data , $wallet , $wallet_interface )
{
$response = [];
$data [ 'price' ] += $data [ 'difference' ];
$data [ 'total_price' ] = $data [ 'amount' ] * $data [ 'price' ];
$data [ 'payment_amount' ] = $data [ 'total_price' ];
2026-02-22 15:06:01 +03:30
$data [ 'service_charge' ] = isset ( $data [ 'fee_free' ]) && $data [ 'fee_free' ] === true ? 0 : $data [ 'service_charge_percentage' ] * $data [ 'amount' ];
2026-01-24 14:03:11 +00:00
$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" => $data [ 'amount' ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
'start_at' => now (),
2026-06-03 21:37:05 +03:30
'title' => self :: meltedGoldBuyTitle (),
2026-01-24 14:03:11 +00:00
], 1 , true , false );
$response [ '1' ] = 2 ;
2026-05-25 12:47:04 +03:30
//send to provider
2026-05-24 22:29:43 +03:30
$this -> sendProviderOrder ( $data , $wallet_interface , ( int ) $data [ 'direction' ]);
2026-01-24 14:03:11 +00:00
return $response ;
}
private function processBuyTransactionProduct ( & $data , $wallet , $wallet_interface )
{
2026-03-10 15:39:30 +03:30
$data [ 'price' ] = $data [ 'product' ][ 'price' ] + $data [ 'product' ][ 'price_profit' ];
2026-01-24 14:03:11 +00:00
$data [ 'price' ] += $data [ 'difference' ];
$data [ 'amount' ] = $data [ 'number' ] * $data [ 'product' ][ 'amount' ];
2026-03-10 15:39:30 +03:30
$data [ 'total_amount' ] = $data [ 'amount' ] + ( $data [ 'product' ][ 'percent_fee_buy' ] * $data [ 'amount' ]);
2026-01-24 14:03:11 +00:00
$data [ 'total_price' ] = $data [ 'total_amount' ] * $data [ 'price' ];
$data [ 'payment_amount' ] = $data [ 'total_price' ];
2026-03-10 15:39:30 +03:30
$data [ 'service_charge' ] = isset ( $data [ 'fee_free' ]) && $data [ 'fee_free' ] === true ? 0 : ( $data [ 'product' ][ 'percent_fee_buy' ] * $data [ 'amount' ]);
2026-01-24 14:03:11 +00:00
$data [ 'service_charge_price' ] = $data [ 'service_charge' ] * $data [ 'price_original' ];
$data [ 'wallet' ] = $wallet ;
if ( $wallet -> cash < $data [ 'payment_amount' ]) {
throw new UnprocessableEntityHttpException ( 'موجودی شما کمتر از میزان سفارش است' );
}
if ( ! $data [ 'product' ][ 'open_buy' ]) {
throw new UnprocessableEntityHttpException ( 'خرید این محصول در این لحظه مقدور نمیباشد' );
}
$data [ 'order' ] = $order = Order :: create ( $data );
$data [ 'transaction' ][] = new TransactionInterface ( $wallet_interface , $_inp = [
"number" => $data [ 'number' ],
"cash" => $data [ 'total_price' ],
"product_id" => $data [ "product_id" ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
'title' => "برداشت وجه نقد"
2026-03-10 15:39:30 +03:30
], 12 , false , false );
2026-01-24 14:03:11 +00:00
$wallet_interface_product = new WalletInterface ( auth ( 'customer' ) -> id (), auth ( 'customer' ) -> user () -> type , null , $data [ "product_id" ]);
$wallet_interface_product -> wallet -> gold = intval ( $wallet_interface_product -> wallet -> gold );
$wallet_interface_product -> wallet -> save ();
new TransactionInterface ( $wallet_interface_product , [
"gold" => $data [ 'number' ],
"product_id" => $data [ "product_id" ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
'start_at' => now (),
2026-06-03 21:37:05 +03:30
'title' => self :: productBuyTitle ( $data [ 'product' ][ 'title' ] ?? '' ),
2026-01-24 14:03:11 +00:00
], 1 , true , false );
}
private function processBuyTransactionProductByGold ( & $data , $wallet , $wallet_interface )
{
$data [ 'price' ] += $data [ 'difference' ];
$data [ 'amount' ] = $data [ 'number' ] * $data [ 'product' ][ 'amount' ];
2026-03-10 15:39:30 +03:30
$data [ 'total_amount' ] = $data [ 'amount' ] + ( $data [ 'product' ][ 'percent_fee_buy' ] * $data [ 'amount' ]);
2026-01-24 14:03:11 +00:00
$data [ 'total_price' ] = $data [ 'total_amount' ] * $data [ 'price' ];
$data [ 'payment_amount' ] = $data [ 'total_price' ];
2026-03-10 15:39:30 +03:30
$data [ 'service_charge' ] = isset ( $data [ 'fee_free' ]) && $data [ 'fee_free' ] === true ? 0 : ( $data [ 'product' ][ 'percent_fee_buy' ] * $data [ 'amount' ]);
2026-01-24 14:03:11 +00:00
$data [ 'total_fee' ] = 0 ;
$data [ 'service_charge_price' ] = $data [ 'service_charge' ] * $data [ 'price_original' ];
$data [ 'wallet' ] = $wallet ;
if ( $wallet -> gold < $data [ 'total_amount' ]) {
2026-06-03 19:53:14 +03:30
throw new UnprocessableEntityHttpException ( $this -> insufficientGoldBalanceMessage ( $data ));
2026-01-24 14:03:11 +00:00
}
if ( ! $data [ 'product' ][ 'open_buy' ]) {
throw new UnprocessableEntityHttpException ( 'خرید این محصول در این لحظه مقدور نمیباشد' );
}
$data [ 'order' ] = $order = Order :: create ( $data );
$data [ 'transaction' ][] = new TransactionInterface ( $wallet_interface , $_inp = [
"number" => $data [ 'number' ],
"gold" => $data [ 'total_amount' ],
"product_id" => $data [ "product_id" ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
2026-06-03 21:37:05 +03:30
'title' => self :: productBuyTitle ( $data [ 'product' ][ 'title' ] ?? '' )
2026-03-10 15:39:30 +03:30
], 12 , false , true );
2026-01-24 14:03:11 +00:00
$wallet_interface_product = new WalletInterface ( auth ( 'customer' ) -> id (), auth ( 'customer' ) -> user () -> type , null , $data [ "product_id" ]);
$wallet_interface_product -> wallet -> gold = intval ( $wallet_interface_product -> wallet -> gold );
$wallet_interface_product -> wallet -> save ();
new TransactionInterface ( $wallet_interface_product , [
"gold" => $data [ 'number' ],
"product_id" => $data [ "product_id" ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
'start_at' => now (),
2026-06-03 21:37:05 +03:30
'title' => self :: productBuyTitle ( $data [ 'product' ][ 'title' ] ?? '' ),
2026-01-24 14:03:11 +00:00
], 1 , true , false );
}
private function processSellTransaction ( & $data , $wallet , $wallet_interface )
{
$response = [];
$data [ 'price' ] -= $data [ 'difference' ];
2026-02-22 15:06:01 +03:30
$data [ 'service_charge' ] = isset ( $data [ 'fee_free' ]) && $data [ 'fee_free' ] === true ? 0 : $data [ 'service_charge_percentage' ] * $data [ 'amount' ];
2026-01-24 14:03:11 +00:00
$data [ 'service_charge_price' ] = $data [ 'service_charge' ] * $data [ 'price_original' ];
2026-03-10 15:39:30 +03:30
$data [ 'total_price' ] = $data [ 'amount' ] * $data [ 'price' ] - $data [ 'service_charge_price' ];
2026-01-24 14:03:11 +00:00
$data [ 'payment_amount' ] = $data [ 'total_price' ];
$data [ 'payment_amount' ] -= $data [ 'service_charge_price' ];
if ( $wallet -> gold < $data [ 'amount' ]) {
2026-06-03 19:53:14 +03:30
throw new UnprocessableEntityHttpException ( $this -> insufficientGoldBalanceMessage ( $data ));
2026-01-24 14:03:11 +00:00
}
$data [ 'order' ] = $order = Order :: create ( $data );
new TransactionInterface ( $wallet_interface , [
"gold" => $data [ 'amount' ],
"cash" => $data [ 'total_price' ],
'expire_at' => now (),
'active' => true ,
2026-06-03 21:37:05 +03:30
'order_id' => $order -> id ,
'title' => self :: meltedGoldSellTitle (),
2026-01-24 14:03:11 +00:00
], 2 , false , true );
2026-03-10 15:39:30 +03:30
2026-03-15 09:30:06 +00:00
//send to Goldir
2026-05-24 22:29:43 +03:30
$this -> sendProviderOrder ( $data , $wallet_interface , ( int ) $data [ 'direction' ]);
2026-03-10 15:39:30 +03:30
2026-01-24 14:03:11 +00:00
}
private function processSellTransactionProduct ( & $data , $wallet , $wallet_interface )
{
2026-03-10 15:39:30 +03:30
$data [ 'price' ] = $data [ 'product' ][ 'price' ] + $data [ 'product' ][ 'price_profit' ];
2026-01-24 14:03:11 +00:00
$data [ 'price' ] -= $data [ 'difference' ];
$data [ 'amount' ] = $data [ 'number' ] * $data [ 'product' ][ 'amount' ];
2026-03-10 15:39:30 +03:30
$data [ 'total_amount' ] = $data [ 'amount' ] - ( $data [ 'product' ][ 'percent_fee_sell' ] * $data [ 'amount' ]);
2026-01-24 14:03:11 +00:00
$data [ 'total_price' ] = $data [ 'total_amount' ] * $data [ 'price' ];
$data [ 'payment_amount' ] = $data [ 'total_price' ];
2026-03-10 15:39:30 +03:30
$data [ 'service_charge' ] = isset ( $data [ 'fee_free' ]) && $data [ 'fee_free' ] === true ? 0 : ( $data [ 'product' ][ 'percent_fee_sell' ] * $data [ 'amount' ]);
2026-01-24 14:03:11 +00:00
$data [ 'service_charge_price' ] = $data [ 'service_charge' ] * $data [ 'price_original' ];
$wallet_interface_product = new WalletInterface ( auth ( 'customer' ) -> id (), auth ( 'customer' ) -> user () -> type , null , $data [ "product_id" ]);
if ( $wallet_interface_product -> wallet -> gold < $data [ 'number' ]) {
throw new UnprocessableEntityHttpException ( 'موجودی شما کمتر از میزان سفارش است' );
}
if ( ! $data [ 'product' ][ 'open_sell' ]) {
throw new UnprocessableEntityHttpException ( 'فروش این محصول در این لحظه مقدور نمیباشد' );
}
$data [ 'order' ] = $order = Order :: create ( $data );
$wallet_interface_product -> wallet -> gold = intval ( $wallet_interface_product -> wallet -> gold );
$wallet_interface_product -> wallet -> save ();
new TransactionInterface ( $wallet_interface_product , [
"gold" => $data [ 'number' ],
"product_id" => $data [ "product_id" ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
'start_at' => now (),
2026-06-03 21:37:05 +03:30
'title' => self :: productSellTitle ( $data [ 'product' ][ 'title' ] ?? '' ),
2026-01-24 14:03:11 +00:00
], 1 , false , false );
$data [ 'transaction' ][] = new TransactionInterface ( $wallet_interface , $_inp = [
"number" => $data [ 'number' ],
"cash" => $data [ 'total_price' ],
"product_id" => $data [ "product_id" ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
'title' => "واریز وجه نقد"
2026-03-10 15:39:30 +03:30
], 12 , false , true );
2026-01-24 14:03:11 +00:00
}
2026-03-10 15:39:30 +03:30
2026-01-24 14:03:11 +00:00
private function processSellTransactionProductByGold ( & $data , $wallet , $wallet_interface )
{
$data [ 'price' ] -= $data [ 'difference' ];
$data [ 'amount' ] = $data [ 'number' ] * $data [ 'product' ][ 'amount' ];
2026-03-10 15:39:30 +03:30
$data [ 'total_amount' ] = $data [ 'amount' ] - ( $data [ 'product' ][ 'percent_fee_sell' ] * $data [ 'amount' ]);
2026-01-24 14:03:11 +00:00
$data [ 'total_price' ] = $data [ 'total_amount' ] * $data [ 'price' ];
$data [ 'payment_amount' ] = $data [ 'total_price' ];
2026-03-10 15:39:30 +03:30
$data [ 'service_charge' ] = isset ( $data [ 'fee_free' ]) && $data [ 'fee_free' ] === true ? 0 : ( $data [ 'product' ][ 'percent_fee_sell' ] * $data [ 'amount' ]);
2026-01-24 14:03:11 +00:00
$data [ 'total_fee' ] = 0 ;
$data [ 'service_charge_price' ] = $data [ 'service_charge' ] * $data [ 'price_original' ];
$wallet_interface_product = new WalletInterface ( auth ( 'customer' ) -> id (), auth ( 'customer' ) -> user () -> type , null , $data [ "product_id" ]);
if ( $wallet_interface_product -> wallet -> gold < $data [ 'number' ]) {
throw new UnprocessableEntityHttpException ( 'موجودی شما کمتر از میزان سفارش است' );
}
if ( ! $data [ 'product' ][ 'open_sell' ]) {
throw new UnprocessableEntityHttpException ( 'فروش این محصول در این لحظه مقدور نمیباشد' );
}
$data [ 'order' ] = $order = Order :: create ( $data );
$data [ 'transaction' ][] = new TransactionInterface ( $wallet_interface , $_inp = [
"number" => $data [ 'number' ],
"gold" => $data [ 'total_amount' ],
"product_id" => $data [ "product_id" ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
2026-06-03 21:37:05 +03:30
'title' => self :: productSellTitle ( $data [ 'product' ][ 'title' ] ?? '' )
2026-03-10 15:39:30 +03:30
], 12 , true , false );
2026-01-24 14:03:11 +00:00
$wallet_interface_product -> wallet -> gold = intval ( $wallet_interface_product -> wallet -> gold );
$wallet_interface_product -> wallet -> save ();
new TransactionInterface ( $wallet_interface_product , [
"gold" => $data [ 'number' ],
"product_id" => $data [ "product_id" ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
'start_at' => now (),
2026-06-03 21:37:05 +03:30
'title' => self :: productSellTitle ( $data [ 'product' ][ 'title' ] ?? '' ),
2026-01-24 14:03:11 +00:00
], 1 , false , false );
}
2026-03-10 15:39:30 +03:30
2026-01-24 14:03:11 +00:00
private function give_order_reward ( & $data , $wallet , $wallet_interface )
{
$data [ 'aCustomers' ] = [];
$i = 0 ;
$data [ 'aCustomers' ][ $i ] = Customer :: findOrFail ( $data [ 'customer_id' ]);
while ( $data [ 'aCustomers' ][ $i + 1 ] = Customer :: where ( 'referral_id' , $data [ 'aCustomers' ][ $i ] -> referral_to_id ) -> first ()) {
if ( $data [ 'aCustomers' ][ $i + 1 ] -> profit_amount_by_lvl ) {
$levels = json_decode ( str_replace ([ '{' , '}' ], [ '[' , ']' ], $data [ 'aCustomers' ][ $i + 1 ] -> profit_amount_by_lvl ), true );
foreach ( $levels as $level ) {
if ( $level [ 0 ] == $i + 1 ) {
// [$level[1],$data['aCustomers'][$i+1]];
if ( $this -> Config ( 'has_page_earn_money' ) == true ) {
$gift_for_invite = ( $data [ 'order' ][ 'service_charge' ] * $level [ 1 ]) / 100 ;
$profit_receiver = $data [ 'aCustomers' ][ $i + 1 ] -> id ;
$profit_sender = $data [ 'aCustomers' ][ 0 ] -> id ;
$Withdraw = Withdraw :: create ([
'user_id' => $profit_sender ,
'customer_id' => $profit_receiver ,
"cash" => 0 ,
"gold" => $gift_for_invite ,
'sts' => 9 ,
]);
$wallet_interface = new WalletInterface ( $profit_receiver , 2 );
new TransactionInterface ( $wallet_interface , $_inp = [
"cash" => 0 ,
"gold" => $gift_for_invite ,
'expire_at' => now (),
'active' => true ,
'withdraw_id' => $Withdraw -> id ,
'start_at' => now (),
], 9 , true , false );
$transaction = Transaction :: where ( 'customer_id' , $profit_receiver ) -> latest () -> first ();
TransactionsAdmin :: create ([
'customer_id' => $profit_sender ,
'customer_to_id' => $profit_receiver ,
'user_id' => $profit_sender ,
'order_id' => $data [ 'order' ][ 'id' ],
'wallet' => null , // یا مقدار مناسب دیگری
'transaction_id' => $transaction -> id ,
'cash' => 0 ,
'note' => ' سود بابت سفارش' ,
'type' => 1 , // یا مقدار مناسب دیگری
'gold' => $gift_for_invite ,
'balance_cash' => 0 , // یا مقدار مناسب دیگری
'balance_gold' => $gift_for_invite , // مقدار صحیح برای `balance_gold`
'withdraw_id' => $Withdraw -> id ,
]);
}
}
}
}
if ( $i == 100 ) break ;
$i ++ ; // برای جلوگیری از حلقه بینهایت
}
return $data ;
}
private function sendTransactionNotifications ( array $data , string $transactionType )
{
$customer = Customer :: findOrFail ( $data [ 'customer_id' ]);
$companyName = $this -> Config ( 'name_fa' );
2026-06-03 19:41:00 +03:30
$amount = $data [ 'amount' ] ?? (
isset ( $data [ 'number' ], $data [ 'product' ][ 'amount' ])
? $data [ 'number' ] * $data [ 'product' ][ 'amount' ]
: 0
);
$totalPrice = $data [ 'total_price' ] ?? ( $data [ 'payment_amount' ] ?? 0 );
2026-06-03 21:37:05 +03:30
$subjectTitle = $this -> transactionSubjectTitle ( $data );
2026-01-24 14:03:11 +00:00
2026-06-03 21:37:05 +03:30
$message = "کاربر { $customer -> name } با شماره موبایل { $customer -> mobile } اقدام به $transactionType { $subjectTitle } به مقدار " .
2026-06-03 19:41:00 +03:30
" { $amount } گرم به مبلغ " . number_format ( $totalPrice ) .
2026-01-24 14:03:11 +00:00
" ریال کرد. با تشکر، $companyName " ;
User :: where ( 'send_sms' , 1 ) -> get () -> each ( function ( $admin ) use ( $message , $data , $transactionType , $companyName ) {
$this -> send ( $admin -> mobile , $message );
});
$wallet = Wallet :: where ( 'owner' , $data [ 'customer_id' ]) -> whereNull ( 'fund_id' ) -> first ();
2026-06-03 21:37:05 +03:30
$balanceLabel = self :: metalTypeLabel ();
2026-06-03 19:41:00 +03:30
// $pricePerGram = $totalPrice / $amount;
2026-06-03 21:37:05 +03:30
$customerMessage = " $companyName \n $transactionType { $subjectTitle } \n گرم: { $amount } \n " .
2026-06-03 19:41:00 +03:30
"مبلغ کل: " . number_format ( $totalPrice ) . " \n\n " .
2026-06-03 21:37:05 +03:30
"موجودی { $balanceLabel } : { $wallet -> gold } \n " .
2026-01-24 14:03:11 +00:00
"مانده ریالی: " . number_format ( $wallet -> cash ) . " \n " .
$this -> Config ( 'app_url' );
// ارسال SMS به مشتری
$this -> send ( $customer -> mobile , $customerMessage );
}
2026-05-24 22:29:43 +03:30
private function sendProviderOrder ( array & $data , $wallet_interface , int $direction )
{
2026-06-04 12:18:53 +03:30
if ( app ( \App\Services\ConfigService :: class ) -> usesMontzarPricing ()) {
2026-05-24 22:29:43 +03:30
try {
2026-05-25 12:47:04 +03:30
return app ( MontzarService :: class ) -> sendOrder ( $data );
2026-05-24 22:29:43 +03:30
} catch ( UnprocessableEntityHttpException $e ) {
$this -> rollbackProviderFailedOrder ( $data , $wallet_interface , $direction );
throw $e ;
} catch ( \Throwable $e ) {
$this -> rollbackProviderFailedOrder ( $data , $wallet_interface , $direction );
throw new UnprocessableEntityHttpException (
2026-05-25 12:47:04 +03:30
'خطا در ثبت سفارش ارائه دهنده: ' . $e -> getMessage ()
2026-05-24 22:29:43 +03:30
);
}
}
2026-05-25 12:47:04 +03:30
return null ;
2026-05-24 22:29:43 +03:30
}
private function rollbackProviderFailedOrder ( array $data , $wallet_interface , int $direction ) : void
{
$order = $data [ 'order' ] ?? null ;
if ( ! $order instanceof Order ) {
return ;
}
if ( $direction === 1 ) {
new TransactionInterface ( $wallet_interface , [
'cash' => $data [ 'payment_amount' ],
'gold' => $data [ 'amount' ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
'start_at' => now (),
], 2 , false , true );
} else {
new TransactionInterface ( $wallet_interface , [
'gold' => $data [ 'amount' ],
'cash' => $data [ 'total_price' ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
], 1 , true , false );
}
$order -> delete ();
}
2026-01-24 14:03:11 +00:00
public function storeBackUp ( Request $request )
{
$data = $request -> all ();
if ( auth ( 'customer' ) -> id ()) { // متد در پنل کاربر
$data [ 'customer_id' ] = auth ( 'customer' ) -> id ();
$data [ 'price' ] = Price :: orderBy ( 'id' , 'desc' ) -> first () -> price ;
} elseif ( auth ( 'user' ) -> user ()) { // متد در پنل ادمین
$data [ 'customer_id' ] = $request -> customer_id ;
$data [ 'price' ] = $request -> price ;
}
$data [ 'sts' ] = 2 ;
$data [ 'type' ] = 2 ;
$data [ 'payment_sts' ] = 4 ;
$data [ 'price_original' ] = $data [ 'price' ];
$add_sell_price = config ( 'add_sell_price.single' );
$wallet_interface = new WalletInterface ( $data [ 'customer_id' ], 2 );
$wallet = Wallet :: where ( 'owner' , $data [ 'customer_id' ]) -> whereNull ( 'fund_id' ) -> first ();
$data [ 'total_price_original' ] = $data [ 'amount' ] * $data [ 'price_original' ];
$data [ 'payment_remind' ] = 0 ;
$data [ 'fee' ] = $add_sell_price ;
$data [ 'total_fee' ] = $data [ 'amount' ] * $data [ 'fee' ];
if ( $data [ 'direction' ] == 1 ) {
$data [ 'price' ] += $add_sell_price ;
$data [ 'total_price' ] = $data [ 'amount' ] * $data [ 'price' ];
$data [ 'payment_amount' ] = $data [ 'total_price' ];
if ( $wallet -> cash < $data [ 'total_price' ]) {
throw new UnprocessableEntityHttpException ( 'موجودی شما کمتر از میزان سفارش است' );
}
$order = Order :: create ( $data ); //buy
new TransactionInterface ( $wallet_interface , $_inp = [
"cash" => $data [ 'total_price' ],
"gold" => $data [ 'amount' ],
'expire_at' => now (),
'active' => true ,
'order_id' => $order -> id ,
'start_at' => now (),
], 1 , true , false );
} else { //sell
$data [ 'price' ] -= $add_sell_price ;
$data [ 'total_price' ] = $data [ 'amount' ] * $data [ 'price' ];
$data [ 'payment_amount' ] = $data [ 'total_price' ];
if ( $wallet -> gold < $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 );
}
//----------------------------------------------------------------------------sms
// Determine transaction type
$transactionType = ( $data [ 'direction' ] == 1 ) ? "خرید" : "فروش" ;
// User information
$customer = Customer :: where ( "id" , $data [ 'customer_id' ]) -> firstOrFail ();
$userMobile = $customer -> mobile ; // User's mobile number
$userName = $customer -> name ; // User's name
$chargedAmount = $data [ 'total_price' ]; // Total price in Rial
$chargedAmountGold = $data [ 'amount' ]; // Amount of gold
// Company name
$companyName = $this -> Config ( 'name_fa' ); // Company name from configuration
// Constructing the SMS message
$message = "کاربر $userName با شماره موبایل $userMobile اقدام به $transactionType طلا به مقدار " . $chargedAmountGold . " گرم به مبلغ " . number_format ( $chargedAmount ) . " ریال کرد. با تشکر، $companyName " ;
// Getting admin mobile numbers and sending the SMS
User :: where ( 'active' , 1 ) -> get () -> each ( function ( $admin ) use ( $message ) {
$this -> Send ( $admin -> mobile , $message );
});
$Walet = Wallet :: where ( 'owner' , $data [ 'customer_id' ]) -> whereNull ( 'fund_id' ) -> first ();
$companyName = $this -> Config ( 'name_fa' );
$companyurl = $this -> Config ( 'app_url' );
$weight = $data [ 'amount' ];
$totalAmount = $data [ 'total_price' ];
$pricePerGram = $totalAmount / $weight ;
$goldBalance = $Walet -> gold ;
$remainingBalance = $Walet -> cash ;
$message = " $companyName \n " ;
$message .= "خرید آبشده \n " ;
$message .= "گرم: $weight \n " ;
$message .= "قیمت گرم: " . number_format ( $pricePerGram ) . " \n " ;
$message .= "مبلغ کل: " . number_format ( $totalAmount ) . " \n\n " ;
$message .= "موجودی طلایی: $goldBalance \n " ;
$message .= "مانده ریالی: " . number_format ( $remainingBalance ) . " \n " ;
$message .= " $companyurl " . " \n " ;
$this -> Send ( $customer -> mobile , $message );
return response () -> json ( $order );
}
public function table ( Request $request )
{
$records = Order :: where ( 'customer_id' , auth ( 'customer' ) -> id ())
-> where ( 'type' , 2 );
TableEngine :: create ( $records , $request -> all ())
-> initSearch ([])
-> select ([
'*' , Order :: $SELECT_STS_STR
]) -> export ( $records );
return response () -> json ( $records );
}
2026-02-10 11:19:51 +03:30
private function feeFree ( $customer_id , $count )
{
return Order :: where ( 'customer_id' , $customer_id )
2026-03-10 15:39:30 +03:30
-> where ( 'type' , 2 )
-> where ( 'sts' , 2 )
-> where ( 'payment_sts' , 4 )
-> count () < ( int ) $count ;
2026-02-10 11:19:51 +03:30
}
2026-06-03 19:53:14 +03:30
2026-06-03 21:37:05 +03:30
private function isProductOrder ( array $data ) : bool
2026-06-03 19:53:14 +03:30
{
2026-06-03 21:37:05 +03:30
return ! empty ( $data [ 'product_id' ]) && ! empty ( $data [ 'product' ][ 'title' ]);
2026-06-03 19:53:14 +03:30
}
2026-06-03 21:37:05 +03:30
private function transactionSubjectTitle ( array $data ) : string
2026-06-03 19:53:14 +03:30
{
2026-06-03 21:37:05 +03:30
if ( $this -> isProductOrder ( $data )) {
return $data [ 'product' ][ 'title' ];
}
return self :: meltedGoldLabel ();
2026-06-03 19:53:14 +03:30
}
private function insufficientGoldBalanceMessage ( array $data ) : string
{
2026-06-03 20:24:07 +03:30
return 'موجودی ' . self :: metalTypeLabel () . ' شما کمتر از میزان سفارش است' ;
2026-06-03 19:53:14 +03:30
}
2026-01-24 14:03:11 +00:00
}