diff --git a/app/Http/Controllers/Single/SingleOrderController.php b/app/Http/Controllers/Single/SingleOrderController.php index 0a13104..92e7d11 100755 --- a/app/Http/Controllers/Single/SingleOrderController.php +++ b/app/Http/Controllers/Single/SingleOrderController.php @@ -41,6 +41,15 @@ class SingleOrderController extends Controller $data['difference'] = Setting::where('key', 'fee_cash')->first()->value; // $data['service_charge_percentage'] = config('fee.service_charge_percentage'); $data['service_charge_percentage'] = Setting::where('key', 'fee_service_charge_percentage')->first()->value; + + $fee_free = Setting::where('key', 'fee_free')->where('is_active', true)->value('value'); + + if($fee_free && $this->feeFree($data['customer_id'], $fee_free)){ + $data['service_charge_percentage'] = 0; + $data['difference'] = 0; + $data['fee_free'] = true; + } + $data['pay_at'] = (new \DateTime())->format('Y-m-d H:i:s'); } else if (auth('user')->id()) {//admin $data['service_charge_percentage'] = 0; @@ -65,6 +74,13 @@ class SingleOrderController extends Controller $data['difference'] = $data['fee'] = $data['product']['cash_fee_buy']; $data['total_price_original'] = $data['product']['amount'] * ($data['price_original'] ?? 0); + $fee_free = Setting::where('key', 'fee_free')->where('is_active', true)->value('value'); + + if($fee_free && $this->feeFree($data['customer_id'], $fee_free)){ + $data['service_charge_percentage'] = 0; + $data['difference'] = 0; + } + // Check if the method is cash if ($data['method'] == "cash") { $response['buy'] = $this->processBuyTransactionProduct($data, $wallet, $wallet_interface); @@ -79,6 +95,12 @@ class SingleOrderController extends Controller $data['difference'] = $data['fee'] = $data['product']['cash_fee_sell']; $data['total_price_original'] = $data['product']['amount'] * ($data['price_original'] ?? 0); + $fee_free = Setting::where('key', 'fee_free')->where('is_active', true)->value('value'); + + if($fee_free && $this->feeFree($data['customer_id'], $fee_free)){ + $data['service_charge_percentage'] = 0; + $data['difference'] = 0; + } // Check if the method is cash if ($data['method'] == "cash") { $response['sell'] = $this->processSellTransactionProduct($data, $wallet, $wallet_interface); @@ -90,7 +112,7 @@ class SingleOrderController extends Controller } else { // Calculate total price and fee for non-product (gold) transaction $data['total_price_original'] = $data['amount'] * ($data['price_original'] ?? 0); - $data['total_fee'] = $data['amount'] * $data['difference']; + $data['total_fee'] = $data['amount'] * ($data['difference'] == 0 ? 1 : $data['difference']); $data['product'] = Product::find(13); // Check if the user wants to buy if ($data['direction'] == 1) { @@ -163,7 +185,7 @@ class SingleOrderController extends Controller private function processBuyTransactionProduct(&$data, $wallet, $wallet_interface) { - + $data['price'] = $data['product']["price"]; $data['price'] += $data['difference']; $data['amount'] = $data['number'] * $data['product']['amount']; $data['total_amount'] = $data['amount']+($data['product']['percent_fee_buy']*$data['amount']); @@ -270,6 +292,7 @@ class SingleOrderController extends Controller private function processSellTransactionProduct(&$data, $wallet, $wallet_interface) { + $data['price'] = $data['product']["price"]; $data['price'] -= $data['difference']; $data['amount'] = $data['number'] * $data['product']['amount']; $data['total_amount'] = $data['amount']-($data['product']['percent_fee_sell']*$data['amount']) ; @@ -542,4 +565,13 @@ class SingleOrderController extends Controller ])->export($records); return response()->json($records); } + + private function feeFree($customer_id, $count) + { + return Order::where('customer_id', $customer_id) + ->where('type', 2) + ->where('sts', 2) + ->where('payment_sts', 4) + ->count() < (int)$count; + } }