From a2eda99f887bb4a8e6a867141fb8f7617a631cb7 Mon Sep 17 00:00:00 2001
From: Ibnu Maksum <ibnumaksum@gmail.com>
Date: Tue, 25 Jun 2024 16:08:40 +0700
Subject: [PATCH] Update Multiple Active plan when change pass or username

---
 system/controllers/accounts.php  |  7 +++++--
 system/controllers/customers.php | 11 +++++++----
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/system/controllers/accounts.php b/system/controllers/accounts.php
index 9e050ca3..70443f05 100644
--- a/system/controllers/accounts.php
+++ b/system/controllers/accounts.php
@@ -36,8 +36,8 @@ switch ($action) {
                     r2(U . 'accounts/change-password', 'e', 'Both Password should be same');
                 }
                 $user->password = $npass;
-                $tur = ORM::for_table('tbl_user_recharges')->where('customer_id', $user['id'])->find_one();
-                if ($tur) {
+                $turs = ORM::for_table('tbl_user_recharges')->where('customer_id', $user['id'])->find_many();
+                foreach($turs as $tur) {
                     // if has active plan, change the password to devices
                     if ($tur['status'] == 'on') {
                         $p = ORM::for_table('tbl_plans')->where('id', $tur['plan_id'])->find_one();
@@ -45,7 +45,10 @@ switch ($action) {
                         if ($_app_stage != 'demo') {
                             if (file_exists($dvc)) {
                                 require_once $dvc;
+                                $exp = $p['plan_expired'];
+                                $p['plan_expired'] = 0;
                                 (new $p['device'])->remove_customer($user, $p);
+                                $p['plan_expired'] = $exp;
                                 (new $p['device'])->add_customer($user, $p);
                             } else {
                                 new Exception(Lang::T("Devices Not Found"));
diff --git a/system/controllers/customers.php b/system/controllers/customers.php
index 4a2b25c6..303c25ea 100644
--- a/system/controllers/customers.php
+++ b/system/controllers/customers.php
@@ -564,8 +564,8 @@ switch ($action) {
             }
 
             if ($userDiff || $pppoeDiff || $passDiff) {
-                $tur = ORM::for_table('tbl_user_recharges')->where('customer_id', $c['id'])->find_one();
-                if ($tur) {
+                $turs = ORM::for_table('tbl_user_recharges')->where('customer_id', $c['id'])->findMany();
+                foreach ($turs as $tur) {
                     $tur->username = $username;
                     $tur->save();
                     $p = ORM::for_table('tbl_plans')->find_one($tur['plan_id']);
@@ -576,9 +576,12 @@ switch ($action) {
                             if (file_exists($dvc)) {
                                 require_once $dvc;
                                 if ($userDiff) {
-                                    $c->username = $oldusername;
+                                    $c['username'] = $oldusername;
+                                    $exp = $p['plan_expired'];
+                                    $p['plan_expired'] = 0;
                                     (new $p['device'])->remove_customer($c, $p);
-                                    $c->username = $username;
+                                    $c['username'] = $username;
+                                    $p['plan_expired'] = $exp;
                                 }
                                 (new $p['device'])->add_customer($c, $p);
                             } else {