From cc8d810d45e4ce6c9809a97b33c1eceb9c9a4df4 Mon Sep 17 00:00:00 2001
From: Ibnu Maksum <ibnumaksum@gmail.com>
Date: Thu, 14 Mar 2024 14:42:20 +0700
Subject: [PATCH] Additional Cost Customer side

---
 system/autoload/Package.php    |  2 ++
 system/controllers/order.php   | 66 ++++++++++++++++++++++++++--------
 system/controllers/voucher.php | 14 ++++++--
 system/lan/english.json        |  4 ++-
 ui/ui/user-orderView.tpl       | 22 +++++++++---
 ui/ui/user-sendPlan.tpl        | 16 +++++++--
 6 files changed, 99 insertions(+), 25 deletions(-)

diff --git a/system/autoload/Package.php b/system/autoload/Package.php
index ea65fb37..83a7415b 100644
--- a/system/autoload/Package.php
+++ b/system/autoload/Package.php
@@ -671,6 +671,8 @@ class Package
             $_admin = Admin::_info($in['admin_id']);
             // if admin not deleted
             if ($_admin) $admin = $_admin;
+        }else{
+            $admin['fullname'] = 'Customer';
         }
         //print
         $invoice = Lang::pad($config['CompanyName'], ' ', 2) . "\n";
diff --git a/system/controllers/order.php b/system/controllers/order.php
index 08ae8067..d80522b6 100644
--- a/system/controllers/order.php
+++ b/system/controllers/order.php
@@ -124,16 +124,26 @@ switch ($action) {
             $trx = ORM::for_table('tbl_payment_gateway')
                 ->where('username', $user['username'])
                 ->find_one($trxid);
-            if ('midtrans' == $trx['gateway']) {
-                //Hapus invoice link
-            }
         }
         if (empty($trx)) {
             r2(U . "order/package", 'e', Lang::T("Transaction Not found"));
         }
-        $router = ORM::for_table('tbl_routers')->find_one($trx['routers_id']);
+        $router = Mikrotik::info($trx['routers']);
         $plan = ORM::for_table('tbl_plans')->find_one($trx['plan_id']);
         $bandw = ORM::for_table('tbl_bandwidth')->find_one($plan['id_bw']);
+
+        $add_cost = 0;
+        $add_rem = User::getAttribute("Additional Remaining", $id_customer);
+        if ($add_rem != 0) {
+            $add_cost = User::getAttribute("Additional Cost", $id_customer);
+            if (empty($add_cost)) {
+                $add_cost = 0;
+            }else{
+                $bills = User::getAttributes("Bill", $id_customer);
+                $ui->assign('bills', $bills);
+            }
+        }
+        $ui->assign('add_cost', $add_cost);
         $ui->assign('trx', $trx);
         $ui->assign('router', $router);
         $ui->assign('plan', $plan);
@@ -157,11 +167,19 @@ switch ($action) {
         } else {
             $router_name = $plan['routers'];
         }
+        $add_cost = 0;
+        $add_rem = User::getAttribute("Additional Remaining", $id_customer);
+        if ($add_rem != 0) {
+            $add_cost = User::getAttribute("Additional Cost", $id_customer);
+            if (empty($add_cost)) {
+                $add_cost = 0;
+            }
+        }
         if ($plan && $plan['enabled'] && $user['balance'] >= $plan['price']) {
             if (Package::rechargeUser($user['id'], $router_name, $plan['id'], 'Customer', 'Balance')) {
                 // if success, then get the balance
-                Balance::min($user['id'], $plan['price']);
-                r2(U . "home", 's', Lang::T("Success to buy package"));
+                Balance::min($user['id'], $plan['price'] + $add_cost);
+                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'] .
@@ -190,6 +208,16 @@ switch ($action) {
         } else {
             $router_name = $plan['routers'];
         }
+        $add_rem = User::getAttribute("Additional Remaining", $id_customer);
+        if ($add_rem != 0) {
+            $add_cost = User::getAttribute("Additional Cost", $id_customer);
+            if (!empty($add_cost)) {
+                $bills = User::getAttributes("Bill", $id_customer);
+                $ui->assign('bills', $bills);
+                $ui->assign('add_cost', $add_cost);
+                $plan['price'] += $add_cost;
+            }
+        }
         if (isset($_POST['send']) && $_POST['send'] == 'plan') {
             $target = ORM::for_table('tbl_customers')->where('username', _post('username'))->find_one();
             if (!$target) {
@@ -267,13 +295,13 @@ switch ($action) {
             r2(U . 'accounts/profile', 'e', Lang::T("Please enter your email address"));
         }
         $pgs = array_values(explode(',', $config['payment_gateway']));
-        if(count($pgs)==0){
+        if (count($pgs) == 0) {
             sendTelegram("Payment Gateway not set, please set it in Settings");
             _log(Lang::T("Payment Gateway not set, please set it in Settings"));
             r2(U . "home", 'e', Lang::T("Failed to create Transaction.."));
         }
-        if(count($pgs)>1){
-            $ui->assign('pgs',$pgs );
+        if (count($pgs) > 1) {
+            $ui->assign('pgs', $pgs);
             //$ui->assign('pgs', $pgs);
             $ui->assign('route2', $routes[2]);
             $ui->assign('route3', $routes[3]);
@@ -281,12 +309,12 @@ switch ($action) {
             //$ui->assign('plan', $plan);
             $ui->display('user-selectGateway.tpl');
             break;
-        }else{
-            if(empty($pgs[0])){
+        } else {
+            if (empty($pgs[0])) {
                 sendTelegram("Payment Gateway not set, please set it in Settings");
                 _log(Lang::T("Payment Gateway not set, please set it in Settings"));
                 r2(U . "home", 'e', Lang::T("Failed to create Transaction.."));
-            }else{
+            } else {
                 $_POST['gateway'] = $pgs[0];
             }
         }
@@ -333,6 +361,16 @@ switch ($action) {
                 }
             }
         }
+        $add_cost = 0;
+        if ($router['name'] != 'balance') {
+            $add_rem = User::getAttribute("Additional Remaining", $id_customer);
+            if ($add_rem != 0) {
+                $add_cost = User::getAttribute("Additional Cost", $id_customer);
+                if (empty($add_cost)) {
+                    $add_cost = 0;
+                }
+            }
+        }
         if (empty($id)) {
             $d = ORM::for_table('tbl_payment_gateway')->create();
             $d->username = $user['username'];
@@ -341,7 +379,7 @@ switch ($action) {
             $d->plan_name = $plan['name_plan'];
             $d->routers_id = $router['id'];
             $d->routers = $router['name'];
-            $d->price = $plan['price'];
+            $d->price = ($plan['price'] + $add_cost);
             $d->created_date = date('Y-m-d H:i:s');
             $d->status = 1;
             $d->save();
@@ -353,7 +391,7 @@ switch ($action) {
             $d->plan_name = $plan['name_plan'];
             $d->routers_id = $router['id'];
             $d->routers = $router['name'];
-            $d->price = $plan['price'];
+            $d->price = ($plan['price'] + $add_cost);
             $d->created_date = date('Y-m-d H:i:s');
             $d->status = 1;
             $d->save();
diff --git a/system/controllers/voucher.php b/system/controllers/voucher.php
index a85c0a26..2f130f6d 100644
--- a/system/controllers/voucher.php
+++ b/system/controllers/voucher.php
@@ -51,9 +51,17 @@ switch ($action) {
         break;
     case 'invoice':
         $id = $routes[2];
-        $in = ORM::for_table('tbl_transactions')->where('username', $user['username'])->where('id', $id)->find_one();
-        Package::createInvoice($in);
-        $ui->display('invoice-customer.tpl');
+        if(empty($id)){
+            $in = ORM::for_table('tbl_transactions')->where('username', $user['username'])->order_by_desc('id')->find_one();
+        }else{
+            $in = ORM::for_table('tbl_transactions')->where('username', $user['username'])->where('id', $id)->find_one();
+        }
+        if($in){
+            Package::createInvoice($in);
+            $ui->display('invoice-customer.tpl');
+        }else{
+            r2(U . 'voucher/list-activated', 'e', Lang::T('Not Found'));
+        }
     default:
         $ui->display('a404.tpl');
 }
diff --git a/system/lan/english.json b/system/lan/english.json
index ccf4128c..b5ffc5ed 100644
--- a/system/lan/english.json
+++ b/system/lan/english.json
@@ -487,5 +487,7 @@
     "Current_Cycle": "Current Cycle",
     "Additional_Cost": "Additional Cost",
     "Remaining": "Remaining",
-    "": ""
+    "": "",
+    "Not_Found": "Not Found",
+    "Cash": "Cash"
 }
\ No newline at end of file
diff --git a/ui/ui/user-orderView.tpl b/ui/ui/user-orderView.tpl
index 2c63ad18..41954048 100644
--- a/ui/ui/user-orderView.tpl
+++ b/ui/ui/user-orderView.tpl
@@ -6,7 +6,7 @@
         <div
             class="panel mb20 {if $trx['status']==1}panel-warning{elseif $trx['status']==2}panel-success{elseif $trx['status']==3}panel-danger{elseif $trx['status']==4}panel-danger{else}panel-primary{/if} panel-hovered">
             <div class="panel-footer">Transaction #{$trx['id']}</div>
-            {if $trx['routers']!='balance'}
+            {if !in_array($trx['routers'],['balance','radius'])}
                 <div class="panel-body">
                     <div class="panel panel-primary panel-hovered">
                         <div class="panel-heading">{$router['name']}</div>
@@ -30,8 +30,8 @@
                                     {date('H:i', strtotime($trx['paid_date']))} </td>
                             </tr>
                             <tr>
-                            {if $trx['plan_name'] == 'Receive Balance'}
-                                <td>{Lang::T('From')}</td>
+                                {if $trx['plan_name'] == 'Receive Balance'}
+                                    <td>{Lang::T('From')}</td>
                                 {else}
                                     <td>{Lang::T('To')}</td>
                                 {/if}
@@ -67,9 +67,21 @@
                                 <td>{Lang::T('Plan Name')}</td>
                                 <td>{$plan['name_plan']}</td>
                             </tr>
+                            {if $add_cost>0}
+                                {foreach $bills as $k => $v}
+                                    <tr>
+                                        <td>{$k}</td>
+                                        <td>{Lang::moneyFormat($v)}</td>
+                                    </tr>
+                                {/foreach}
+                                <tr>
+                                    <td>{Lang::T('Additional Cost')}</td>
+                                    <td>{Lang::moneyFormat($add_cost)}</td>
+                                </tr>
+                            {/if}
                             <tr>
-                                <td>{Lang::T('Plan Price')}</td>
-                                <td>{Lang::moneyFormat($plan['price'])}</td>
+                                <td>{Lang::T('Plan Price')}{if $add_cost>0}<small> + {Lang::T('Additional Cost')}{/if}</small></td>
+                                <td style="font-size: large; font-weight:bolder; font-family: 'Courier New', Courier, monospace; ">{Lang::moneyFormat($trx['price'])}</td>
                             </tr>
                             <tr>
                                 <td>{Lang::T('Type')}</td>
diff --git a/ui/ui/user-sendPlan.tpl b/ui/ui/user-sendPlan.tpl
index 26f9fb7a..8dd9e7bd 100644
--- a/ui/ui/user-sendPlan.tpl
+++ b/ui/ui/user-sendPlan.tpl
@@ -12,9 +12,21 @@
                             <td>{Lang::T('Type')}</td>
                             <td>{$plan['type']}</td>
                         </tr>
+                        {if $add_cost>0}
+                            {foreach $bills as $k => $v}
+                                <tr>
+                                    <td>{$k}</td>
+                                    <td>{Lang::moneyFormat($v)}</td>
+                                </tr>
+                            {/foreach}
+                            <tr>
+                                <td>{Lang::T('Additional Cost')}</td>
+                                <td>{Lang::moneyFormat($add_cost)}</td>
+                            </tr>
+                        {/if}
                         <tr>
-                            <td>{Lang::T('Price')}</td>
-                            <td>{Lang::moneyFormat($plan['price'])}</td>
+                            <td>{Lang::T('Price')}{if $add_cost>0}<small> + {Lang::T('Additional Cost')}{/if}</td>
+                            <td style="font-size: large; font-weight:bolder; font-family: 'Courier New', Courier, monospace; ">{Lang::moneyFormat($plan['price'])}</td>
                         </tr>
                         <tr>
                             <td>{Lang::T('Validity')}</td>