From ea51fa24d0e0e1fca509506426e138dccee79717 Mon Sep 17 00:00:00 2001 From: Focuslinkstech <45756999+Focuslinkstech@users.noreply.github.com> Date: Fri, 17 May 2024 14:47:12 +0100 Subject: [PATCH] update fixed tax logic --- system/controllers/order.php | 97 ++++++++++++++++++++++++++------- system/controllers/settings.php | 28 ---------- ui/ui/app-settings.tpl | 4 +- ui/ui/user-sendPlan.tpl | 45 +++++++++------ 4 files changed, 106 insertions(+), 68 deletions(-) diff --git a/system/controllers/order.php b/system/controllers/order.php index c5b7a897..0e25677d 100644 --- a/system/controllers/order.php +++ b/system/controllers/order.php @@ -148,9 +148,6 @@ switch ($action) { r2(U . "voucher/invoice/"); die(); } - if($user['status'] != 'Active'){ - _alert(Lang::T('This account status').' : '.Lang::T($user['status']),'danger', ""); - } $plan = ORM::for_table('tbl_plans')->where('enabled', '1')->find_one($routes['3']); if (empty($plan)) { r2(U . "order/package", 'e', Lang::T("Plan Not found")); @@ -163,30 +160,48 @@ switch ($action) { } else { $router_name = $plan['routers']; } + list($bills, $add_cost) = User::getBills($id_customer); - if ($plan && $plan['enabled'] && $user['balance'] >= $plan['price']) { + + // Tax calculation start + $tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no'; + $tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null; + $custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null; + + if ($tax_rate_setting === 'custom') { + $tax_rate = $custom_tax_rate; + } else { + $tax_rate = $tax_rate_setting; + } + + if ($tax_enable === 'yes') { + $tax = Package::tax($plan['price'], $tax_rate); + } else { + $tax = 0; + } + // Tax calculation stop + + if ($plan && $plan['enabled'] && $user['balance'] >= $plan['price'] + $tax) { if (Package::rechargeUser($user['id'], $router_name, $plan['id'], 'Customer', 'Balance')) { // if success, then get the balance - Balance::min($user['id'], $plan['price'] + $add_cost); + Balance::min($user['id'], $plan['price'] + $add_cost + $tax); App::setToken($_GET['stoken'], "success"); r2(U . "voucher/invoice/", 's', Lang::T("Success to buy package")); } else { r2(U . "order/package", 'e', Lang::T("Failed to buy package")); Message::sendTelegram("Buy Package with Balance Failed\n\n#u$c[username] #buy \n" . $plan['name_plan'] . "\nRouter: " . $router_name . - "\nPrice: " . $p['price']); + "\nPrice: " . $plan['price'] + $tax); } } else { r2(U . "home", 'e', 'Plan is not exists'); } break; + case 'send': if ($config['enable_balance'] != 'yes') { r2(U . "order/package", 'e', Lang::T("Balance not enabled")); } - if($user['status'] != 'Active'){ - _alert(Lang::T('This account status').' : '.Lang::T($user['status']),'danger', ""); - } $ui->assign('_title', Lang::T('Buy for friend')); $ui->assign('_system_menu', 'package'); $plan = ORM::for_table('tbl_plans')->find_one($routes['3']); @@ -201,6 +216,27 @@ switch ($action) { } else { $router_name = $plan['routers']; } + $tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null; + $custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null; + + if ($tax_rate_setting === 'custom') { + $tax_rate = $custom_tax_rate; + } else { + $tax_rate = $tax_rate_setting; + } + + $tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no'; + + if ($tax_enable === 'yes') { + $tax = Package::tax($plan['price'], $tax_rate); + $ui->assign('tax', $tax); + } else { + $tax = 0; + } + + // Add tax to plan price + $plan['price'] += $tax; + if (isset($_POST['send']) && $_POST['send'] == 'plan') { $target = ORM::for_table('tbl_customers')->where('username', _post('username'))->find_one(); list($bills, $add_cost) = User::getBills($target['id']); @@ -209,6 +245,7 @@ switch ($action) { $ui->assign('add_cost', $add_cost); $plan['price'] += $add_cost; } + if (!$target) { r2(U . 'home', 'd', Lang::T('Username not found')); } @@ -269,15 +306,22 @@ switch ($action) { $d->save(); r2(U . "order/view/$trx_id", 's', Lang::T("Success to send package")); } else { - r2(U . "order/package", 'e', Lang::T("Failed to Send package")); - Message::sendTelegram("Send Package with Balance Failed\n\n#u$user[username] #send \n" . $plan['name_plan'] . + $errorMessage = "Send Package with Balance Failed\n\n#u$user[username] #send \n" . $plan['name_plan'] . "\nRouter: " . $router_name . - "\nPrice: " . $plan['price']); + "\nPrice: " . $plan['price']; + + if ($tax_enable === 'yes') { + $errorMessage .= "\nTax: " . $tax; + } + + r2(U . "order/package", 'e', Lang::T("Failed to Send package")); + Message::sendTelegram($errorMessage); } } $ui->assign('username', $_GET['u']); $ui->assign('router', $router_name); $ui->assign('plan', $plan); + $ui->assign('tax', $tax); $ui->display('user-sendPlan.tpl'); break; case 'gateway': @@ -328,9 +372,6 @@ switch ($action) { } else if (!empty($gateway)) { $_SESSION['gateway'] = $gateway; } - if($user['status'] != 'Active'){ - _alert(Lang::T('This account status').' : '.Lang::T($user['status']),'danger', ""); - } if (empty($gateway)) { r2(U . 'order/gateway/' . $routes[2] . '/' . $routes[3], 'w', Lang::T("Please select Payment Gateway")); } @@ -368,9 +409,23 @@ switch ($action) { } } $add_cost = 0; + $tax = 0; if ($router['name'] != 'balance') { list($bills, $add_cost) = User::getBills($id_customer); } + // Tax calculation start + $tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no'; + $tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null; + $custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null; + if ($tax_rate_setting === 'custom') { + $tax_rate = $custom_tax_rate; + } else { + $tax_rate = $tax_rate_setting; + } + if ($tax_enable === 'yes') { + $tax = Package::tax($plan['price'], $tax_rate); + } + // Tax calculation stop if (empty($id)) { $d = ORM::for_table('tbl_payment_gateway')->create(); $d->username = $user['username']; @@ -383,12 +438,12 @@ switch ($action) { // Postpaid price from field $add_inv = User::getAttribute("Invoice", $id_customer); if (empty($add_inv) or $add_inv == 0) { - $d->price = ($plan['price'] + $add_cost); + $d->price = ($plan['price'] + $add_cost + $tax); } else { - $d->price = ($add_inv + $add_cost); + $d->price = ($add_inv + $add_cost + $tax); } } else { - $d->price = ($plan['price'] + $add_cost); + $d->price = ($plan['price'] + $add_cost + $tax); } //$d->price = ($plan['price'] + $add_cost); $d->created_date = date('Y-m-d H:i:s'); @@ -406,12 +461,12 @@ switch ($action) { // Postpaid price from field $add_inv = User::getAttribute("Invoice", $id_customer); if (empty($add_inv) or $add_inv == 0) { - $d->price = ($plan['price'] + $add_cost); + $d->price = ($plan['price'] + $add_cost + $tax); } else { - $d->price = ($add_inv + $add_cost); + $d->price = ($add_inv + $add_cost + $tax); } } else { - $d->price = ($plan['price'] + $add_cost); + $d->price = ($plan['price'] + $add_cost + $tax); } //$d->price = ($plan['price'] + $add_cost); $d->created_date = date('Y-m-d H:i:s'); diff --git a/system/controllers/settings.php b/system/controllers/settings.php index 5575ebdd..4419720f 100644 --- a/system/controllers/settings.php +++ b/system/controllers/settings.php @@ -132,34 +132,6 @@ switch ($action) { $d->save(); } } - - // Handle tax system separately - $enable_tax = isset($_POST['enable_tax']) ? $_POST['enable_tax'] : 'no'; - $tax_rate = isset($_POST['tax_rate']) ? $_POST['tax_rate'] : '0.01'; // Default tax rate 1% - - // Save or update tax system settings - $d_tax_enable = ORM::for_table('tbl_appconfig')->where('setting', 'enable_tax')->find_one(); - if ($d_tax_enable) { - $d_tax_enable->value = $enable_tax; - $d_tax_enable->save(); - } else { - $d_tax_enable = ORM::for_table('tbl_appconfig')->create(); - $d_tax_enable->setting = 'enable_tax'; - $d_tax_enable->value = $enable_tax; - $d_tax_enable->save(); - } - - $d_tax_rate = ORM::for_table('tbl_appconfig')->where('setting', 'tax_rate')->find_one(); - if ($d_tax_rate) { - $d_tax_rate->value = $tax_rate; - $d_tax_rate->save(); - } else { - $d_tax_rate = ORM::for_table('tbl_appconfig')->create(); - $d_tax_rate->setting = 'tax_rate'; - $d_tax_rate->value = $tax_rate; - $d_tax_rate->save(); - } - //checkbox $checks = ['hide_mrc', 'hide_tms', 'hide_aui', 'hide_al', 'hide_uet', 'hide_vs', 'hide_pg']; foreach ($checks as $check) { diff --git a/ui/ui/app-settings.tpl b/ui/ui/app-settings.tpl index a182ef21..30ab4c1f 100644 --- a/ui/ui/app-settings.tpl +++ b/ui/ui/app-settings.tpl @@ -604,7 +604,7 @@ - {*
+

{Lang::T('Enter the custom tax rate (e.g., 3.75 for 3.75%)')}

-
*} + {*
diff --git a/ui/ui/user-sendPlan.tpl b/ui/ui/user-sendPlan.tpl index c2ab0e4b..fb3ac4e3 100644 --- a/ui/ui/user-sendPlan.tpl +++ b/ui/ui/user-sendPlan.tpl @@ -13,20 +13,29 @@ {$plan['type']} {if $add_cost>0} - {foreach $bills as $k => $v} - - {$k} - {Lang::moneyFormat($v)} - - {/foreach} - - {Lang::T('Additional Cost')} - {Lang::moneyFormat($add_cost)} - + {foreach $bills as $k => $v} + + {$k} + {Lang::moneyFormat($v)} + + {/foreach} + + {Lang::T('Additional Cost')} + {Lang::moneyFormat($add_cost)} + + {/if} + {if $tax > 0} + + {Lang::T('Tax')} + {Lang::moneyFormat($tax)} + {/if} - {Lang::T('Price')}{if $add_cost>0} + {Lang::T('Additional Cost')}{/if} - {Lang::moneyFormat($plan['price'])} + {Lang::T('Price')}{if $add_cost>0} + {Lang::T('Additional Cost')}{/if}{if + $tax>0} + {Lang::T('Tax')}{/if} + + {Lang::moneyFormat($plan['price'])} {Lang::T('Validity')} @@ -39,15 +48,17 @@
- +
- +
-

{Lang::T('If your friend have Additional Cost, you will pay for that too')}

+

{Lang::T('If your friend have Additional Cost, you will pay for + that too')}