From cdfbab7119daab3f32563906a429e096dfd5d94b Mon Sep 17 00:00:00 2001
From: Focuslinkstech <45756999+Focuslinkstech@users.noreply.github.com>
Date: Tue, 18 Mar 2025 16:51:37 +0100
Subject: [PATCH] Refactor invoice handling to use 'invoice' key and improve
 payment link generation

Still in development
---
 system/autoload/Invoice.php               | 34 ++++++++++++++---------
 system/uploads/notifications.default.json | 21 +++++++-------
 ui/ui/admin/settings/notifications.tpl    |  3 +-
 3 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/system/autoload/Invoice.php b/system/autoload/Invoice.php
index 423a626e..96f558e6 100644
--- a/system/autoload/Invoice.php
+++ b/system/autoload/Invoice.php
@@ -7,7 +7,7 @@ class Invoice
     public static function generateInvoice($invoiceData)
     {
         try {
-            if (empty($invoiceData['id'])) {
+            if (empty($invoiceData['invoice'])) {
                 throw new Exception("Invoice ID is required");
             }
 
@@ -45,7 +45,7 @@ class Invoice
             $mpdf->WriteHTML($processedHtml);
 
             // Save PDF
-            $filename = "invoice_{$invoiceData['id']}.pdf";
+            $filename = "invoice_{$invoiceData['invoice']}.pdf";
             $outputPath = "system/uploads/invoices/{$filename}";
             $mpdf->Output($outputPath, 'F');
 
@@ -88,9 +88,9 @@ class Invoice
         }, $template);
     }
 
-    public static function sendInvoice($userId,  $package, $status = "Unpaid")
+    public static function sendInvoice($userId, $status = "Unpaid")
     {
-        global $config;
+        global $config, $root_path, $UPLOAD_PATH;
 
         if (empty($config['currency_code'])) {
             $config['currency_code'] = '$';
@@ -142,19 +142,28 @@ class Invoice
         $tax = $config['enable_tax'] ? Package::tax($subtotal) : 0;
         $total = ($tax > 0) ? $subtotal + $tax : $subtotal + $tax;
 
-        $token = User::generateToken($account['id'], 1);
+        $token = User::generateToken($account->id, 1);
         if (!empty($token['token'])) {
             $tur = ORM::for_table('tbl_user_recharges')
-                ->where('customer_id', $account['id'])
-                ->where('namebp', $package)
-                ->find_one();
-            if ($tur) {
-                $payLink = '?_route=home&recharge=' . $tur['id'] . '&uid=' . urlencode($token['token']);
+                ->where('customer_id', $account->id)
+                ->where('namebp', $invoice->plan_name);
+
+            switch ($status) {
+                case 'Paid':
+                    $tur->where('status', 'on');
+                    break;
+                default:
+                    $tur->where('status', 'off');
+                    break;
             }
+            $turResult = $tur->find_one();
+            $payLink = $turResult ? '?_route=home&recharge=' . $turResult['id'] . '&uid=' . urlencode($token['token']) : '?_route=home';
         } else {
             $payLink = '?_route=home';
         }
 
+        $UPLOAD_URL_PATH = str_replace($root_path, '', $UPLOAD_PATH);
+        $logo = (file_exists($UPLOAD_PATH . DIRECTORY_SEPARATOR . 'logo.png')) ? $UPLOAD_URL_PATH . DIRECTORY_SEPARATOR . 'logo.png?' . time() : $UPLOAD_URL_PATH . DIRECTORY_SEPARATOR . 'logo.default.png';
         $invoiceData = [
             'invoice' => "INV-" . Package::_raid(),
             'fullname' => $account->fullname,
@@ -169,9 +178,8 @@ class Invoice
             'company_address' => $config['address'],
             'company_name' => $config['CompanyName'],
             'company_phone' => $config['phone'],
-            'logo' => $config['logo'],
-            'company_url' => APP_URL,
-            'payment_link' => $payLink,
+            'logo' => $logo,
+            'payment_link' => $payLink
         ];
 
         if (!isset($invoiceData['bill_rows']) || empty($invoiceData['bill_rows'])) {
diff --git a/system/uploads/notifications.default.json b/system/uploads/notifications.default.json
index 45fd9acb..b22efb78 100644
--- a/system/uploads/notifications.default.json
+++ b/system/uploads/notifications.default.json
@@ -1,13 +1,12 @@
 {
-	"expired": "Hello [[name]], your internet package [[package]] has been expired.",
-	"balance_send": "You sent [[balance]] to [[name]].",
-	"balance_received": "You have received [[balance]] from [[name]].",
-	"reminder_7_day": "Hello *[[name]]*, \r\nyour internet package *[[package]]* will be expired in 7 days.",
-	"reminder_3_day": "Hello *[[name]]*, \r\nyour internet package *[[package]]* will be expired in 3 days.",
-	"reminder_1_day": "Hello *[[name]]*,\r\n your internet package *[[package]]* will be expired tomorrow.",
-	"invoice_paid": "*[[company_name]]*\r\n[[address]]\r\n[[phone]]\r\n\r\n\r\nINVOICE: *[[invoice]]*\r\nDate : [[date]]\r\n[[payment_gateway]] [[payment_channel]]\r\n\r\n\r\nType : *[[type]]*\r\nPackage : *[[plan_name]]*\r\nPrice : *[[plan_price]]*\r\n\r\nUsername : *[[user_name]]*\r\nPassword : ***********\r\n\r\nExpired : *[[expired_date]]*\r\n\r\n====================\r\n[[footer]]",
-	"email_invoice": "<!DOCTYPE html>\r\n<html>\r\n\r\n<head>\r\n    <meta charset=\"utf-8\" />\r\n    <title>Invoice No: [[id]]</title>\r\n\r\n    <style>\r\n        .invoice-box {\r\n            max-width: 800px;\r\n            margin: auto;\r\n            padding: 30px;\r\n            border: 1px solid #eee;\r\n            box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);\r\n            font-size: 16px;\r\n            line-height: 24px;\r\n            font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;\r\n            color: #555;\r\n        }\r\n\r\n        .invoice-box table {\r\n            width: 100%;\r\n            line-height: inherit;\r\n            text-align: left;\r\n        }\r\n\r\n        .invoice-box table td {\r\n            padding: 5px;\r\n            vertical-align: top;\r\n        }\r\n\r\n        .invoice-box table tr td:nth-child(2) {\r\n            text-align: right;\r\n        }\r\n\r\n        .invoice-box table tr.top table td {\r\n            padding-bottom: 20px;\r\n        }\r\n\r\n        .invoice-box table tr.top table td.title {\r\n            font-size: 45px;\r\n            line-height: 45px;\r\n            color: #333;\r\n        }\r\n\r\n        .invoice-box table tr.information table td {\r\n            padding-bottom: 40px;\r\n        }\r\n\r\n        .invoice-box table tr.heading td {\r\n            background: #eee;\r\n            border-bottom: 1px solid #ddd;\r\n            font-weight: bold;\r\n        }\r\n\r\n        .invoice-box table tr.details td {\r\n            padding-bottom: 20px;\r\n        }\r\n\r\n        .invoice-box table tr.item td {\r\n            border-bottom: 1px solid #eee;\r\n        }\r\n\r\n        .invoice-box table tr.item.last td {\r\n            border-bottom: none;\r\n        }\r\n\r\n        .invoice-box table tr.total td:nth-child(2) {\r\n            border-top: 2px solid #eee;\r\n            font-weight: bold;\r\n        }\r\n\r\n        @media only screen and (max-width: 600px) {\r\n            .invoice-box table tr.top table td {\r\n                width: 100%;\r\n                display: block;\r\n                text-align: center;\r\n            }\r\n\r\n            .invoice-box table tr.information table td {\r\n                width: 100%;\r\n                display: block;\r\n                text-align: center;\r\n            }\r\n        }\r\n\r\n        /** RTL **/\r\n        .invoice-box.rtl {\r\n            direction: rtl;\r\n            font-family: Tahoma, 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;\r\n        }\r\n\r\n        .invoice-box.rtl table {\r\n            text-align: right;\r\n        }\r\n\r\n        .invoice-box.rtl table tr td:nth-child(2) {\r\n            text-align: left;\r\n        }\r\n    </style>\r\n</head>\r\n\r\n<body>\r\n    <div class=\"invoice-box\">\r\n        <table cellpadding=\"0\" cellspacing=\"0\">\r\n            <tr class=\"top\">\r\n                <td colspan=\"2\">\r\n                    <table>\r\n                        <tr>\r\n                            <td class=\"title\">\r\n                                <img src=\"system/uploads/logo.default.png\" style=\"max-width: 100px\" />\r\n                            </td>\r\n\r\n                            <td>\r\n                                Invoice #: [[invoice]]<br />\r\n                                Created: [[created_at]]<br />\r\n                                Due: [[due_date]]\r\n                            </td>\r\n                        </tr>\r\n                    </table>\r\n                </td>\r\n            </tr>\r\n\r\n            <tr class=\"information\">\r\n                <td colspan=\"2\">\r\n                    <table>\r\n                        <tr>\r\n                            <td>\r\n                                [[company_name]]<br />\r\n                                [[company_address]]<br />\r\n                                [[company_phone]]<br />\r\n                            </td>\r\n\r\n                            <td>\r\n                                [[fullname]]<br />\r\n                                [[address]] <br />\r\n                                [[email]] <br />\r\n                                [[phone]] <br />\r\n                            </td>\r\n                        </tr>\r\n                    </table>\r\n                </td>\r\n            </tr>\r\n\r\n            [[bill_rows]]\r\n        </table>\r\n\r\n        <div style=\"background: #f8f9fa; padding: 15px; border-radius: 5px; margin-top: 20px;\">\r\n            <h4 style=\"margin: 0;\">Payment Options:</h4>\r\n            <p style=\"margin: 0;\">Online Portal: <a href=\"pay.phpnuxbill.com\">pay.phpnuxbill.com</a> <br>\r\n                Bank Transfer: Account # 1234-567890<br>\r\n                Auto Pay: Enabled (Next payment: 2023-11-12)</p>\r\n        </div>\r\n\r\n        <footer\r\n            style=\"margin-top: 30px; padding-top: 20px; border-top: 1px solid #ddd; font-size: 0.9em; color: #666; text-align: center;\">\r\n            <p style=\"margin: 0;\">Thank you for choosing [[company_name]]!<br>\r\n                Late payments may result in service interruption<br>\r\n                Need help? Contact support@[[company_name]].com or call [[company_phone]]</p>\r\n        </footer>\r\n    </div>\r\n</body>\r\n\r\n</html>",
-	"invoice_balance": "*[[company_name]]*\r\n[[address]]\r\n[[phone]]\r\n\r\n\r\nINVOICE: *[[invoice]]*\r\nDate : [[date]]\r\n[[payment_gateway]] [[payment_channel]]\r\n\r\n\r\nType : *[[type]]*\r\nPackage : *[[plan_name]]*\r\nPrice : *[[plan_price]]*\r\n\r\n====================\r\n[[footer]]",
-	"welcome_message": "Welcome aboard, [[name]]! \r\nWe're excited to have you as a new [[company]] customer. \r\nYour account is all set up and ready to go.\r\n\r\nHere's a quick overview:\r\n\r\nPortal: [[url]]\r\nYour login is  [[Username]]\r\nYour temporary password is  [[Password]] (please change this on your first login)\r\n\r\nNeed help? Reach out to our support team at anytime.\r\n\r\nWe're here to ensure you have an amazing experience with our services. Let us know how we can best support you.\r\n\r\nWelcome to the [[company]] family!"
-
+  "expired": "Hello [[name]], your internet package [[package]] has been expired.",
+  "balance_send": "You sent [[balance]] to [[name]].",
+  "balance_received": "You have received [[balance]] from [[name]].",
+  "reminder_7_day": "Hello *[[name]]*, \r\nyour internet package *[[package]]* will be expired in 7 days.",
+  "reminder_3_day": "Hello *[[name]]*, \r\nyour internet package *[[package]]* will be expired in 3 days.",
+  "reminder_1_day": "Hello *[[name]]*,\r\n your internet package *[[package]]* will be expired tomorrow.",
+  "invoice_paid": "*[[company_name]]*\r\n[[address]]\r\n[[phone]]\r\n\r\n\r\nINVOICE: *[[invoice]]*\r\nDate : [[date]]\r\n[[payment_gateway]] [[payment_channel]]\r\n\r\n\r\nType : *[[type]]*\r\nPackage : *[[plan_name]]*\r\nPrice : *[[plan_price]]*\r\n\r\nUsername : *[[user_name]]*\r\nPassword : ***********\r\n\r\nExpired : *[[expired_date]]*\r\n\r\n====================\r\n[[footer]]",
+  "email_invoice": "<!DOCTYPE html>\r\n<html>\r\n\r\n<head>\r\n    <meta charset=\"utf-8\" />\r\n    <title>Invoice No: [[invoice]]</title>\r\n\r\n    <style>\r\n        .invoice-box {\r\n            max-width: 800px;\r\n            margin: auto;\r\n            padding: 30px;\r\n            border: 1px solid #eee;\r\n            box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);\r\n            font-size: 16px;\r\n            line-height: 24px;\r\n            font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;\r\n            color: #555;\r\n        }\r\n\r\n        .invoice-box table {\r\n            width: 100%;\r\n            line-height: inherit;\r\n            text-align: left;\r\n        }\r\n\r\n        .invoice-box table td {\r\n            padding: 5px;\r\n            vertical-align: top;\r\n        }\r\n\r\n        .invoice-box table tr td:nth-child(2) {\r\n            text-align: right;\r\n        }\r\n\r\n        .invoice-box table tr.top table td {\r\n            padding-bottom: 20px;\r\n        }\r\n\r\n        .invoice-box table tr.top table td.title {\r\n            font-size: 45px;\r\n            line-height: 45px;\r\n            color: #333;\r\n        }\r\n\r\n        .invoice-box table tr.information table td {\r\n            padding-bottom: 40px;\r\n        }\r\n\r\n        .invoice-box table tr.heading td {\r\n            background: #eee;\r\n            border-bottom: 1px solid #ddd;\r\n            font-weight: bold;\r\n        }\r\n\r\n        .invoice-box table tr.details td {\r\n            padding-bottom: 20px;\r\n        }\r\n\r\n        .invoice-box table tr.item td {\r\n            border-bottom: 1px solid #eee;\r\n        }\r\n\r\n        .invoice-box table tr.item.last td {\r\n            border-bottom: none;\r\n        }\r\n\r\n        .invoice-box table tr.total td:nth-child(2) {\r\n            border-top: 2px solid #eee;\r\n            font-weight: bold;\r\n        }\r\n\r\n        @media only screen and (max-width: 600px) {\r\n            .invoice-box table tr.top table td {\r\n                width: 100%;\r\n                display: block;\r\n                text-align: center;\r\n            }\r\n\r\n            .invoice-box table tr.information table td {\r\n                width: 100%;\r\n                display: block;\r\n                text-align: center;\r\n            }\r\n        }\r\n\r\n        /** RTL **/\r\n        .invoice-box.rtl {\r\n            direction: rtl;\r\n            font-family: Tahoma, 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;\r\n        }\r\n\r\n        .invoice-box.rtl table {\r\n            text-align: right;\r\n        }\r\n\r\n        .invoice-box.rtl table tr td:nth-child(2) {\r\n            text-align: left;\r\n        }\r\n    </style>\r\n</head>\r\n\r\n<body>\r\n    <div class=\"invoice-box\">\r\n        <table cellpadding=\"0\" cellspacing=\"0\">\r\n            <tr class=\"top\">\r\n                <td colspan=\"2\">\r\n                    <table>\r\n                        <tr>\r\n                            <td class=\"title\">\r\n                                <img src=\"[[logo]]\" style=\"max-width: 100px\" />\r\n                            </td>\r\n\r\n                            <td>\r\n                                Invoice #: [[invoice]]<br />\r\n                                Created: [[created_at]]<br />\r\n                                Due: [[due_date]]\r\n                            </td>\r\n                        </tr>\r\n                    </table>\r\n                </td>\r\n            </tr>\r\n\r\n            <tr class=\"information\">\r\n                <td colspan=\"2\">\r\n                    <table>\r\n                        <tr>\r\n                            <td>\r\n                                [[company_name]]<br />\r\n                                [[company_address]]<br />\r\n                                [[company_phone]]<br />\r\n                            </td>\r\n\r\n                            <td>\r\n                                [[fullname]]<br />\r\n                                [[address]] <br />\r\n                                [[email]] <br />\r\n                                [[phone]] <br />\r\n                            </td>\r\n                        </tr>\r\n                    </table>\r\n                </td>\r\n            </tr>\r\n\r\n            [[bill_rows]]\r\n        </table>\r\n\r\n        <div style=\"background: #f8f9fa; padding: 15px; border-radius: 5px; margin-top: 20px;\">\r\n            <h4 style=\"margin: 0;\">Payment Options:</h4>\r\n            <p style=\"margin: 0;\">Online Portal: <a href=\"\">https://yoursite-domain.com/[[payment_link]]</a> <br>\r\n                Bank Transfer: Account # 1234-567890<br>\r\n                Auto Pay: Enabled (Next payment: 2023-11-12)</p>\r\n        </div>\r\n\r\n        <footer\r\n            style=\"margin-top: 30px; padding-top: 20px; border-top: 1px solid #ddd; font-size: 0.9em; color: #666; text-align: center;\">\r\n            <p style=\"margin: 0;\">Thank you for choosing [[company_name]]!<br>\r\n                Late payments may result in service interruption<br>\r\n                Need help? Contact support@[[company_name]].com or call [[company_phone]]</p>\r\n        </footer>\r\n    </div>\r\n</body>\r\n\r\n</html>\r\n                                                    \r\n                                                    \r\n                                                    \r\n                                                    \r\n                                                    ",
+  "invoice_balance": "*[[company_name]]*\r\n[[address]]\r\n[[phone]]\r\n\r\n\r\nINVOICE: *[[invoice]]*\r\nDate : [[date]]\r\n[[payment_gateway]] [[payment_channel]]\r\n\r\n\r\nType : *[[type]]*\r\nPackage : *[[plan_name]]*\r\nPrice : *[[plan_price]]*\r\n\r\n====================\r\n[[footer]]",
+  "welcome_message": "Welcome aboard, [[name]]! \r\nWe're excited to have you as a new [[company]] customer. \r\nYour account is all set up and ready to go.\r\n\r\nHere's a quick overview:\r\n\r\nPortal: [[url]]\r\nYour login is  [[Username]]\r\nYour temporary password is  [[Password]] (please change this on your first login)\r\n\r\nNeed help? Reach out to our support team at anytime.\r\n\r\nWe're here to ensure you have an amazing experience with our services. Let us know how we can best support you.\r\n\r\nWelcome to the [[company]] family!"
 }
diff --git a/ui/ui/admin/settings/notifications.tpl b/ui/ui/admin/settings/notifications.tpl
index a1c9ce2e..10cc609f 100644
--- a/ui/ui/admin/settings/notifications.tpl
+++ b/ui/ui/admin/settings/notifications.tpl
@@ -210,7 +210,6 @@
                             <b>[[company_name]]</b> {Lang::T('Your Company Name at Settings')}.<br>
                             <b>[[company_address]]</b> {Lang::T('Your Company Address at Settings')}.<br>
                             <b>[[company_phone]]</b> - {Lang::T('Your Company Phone at Settings')}.<br>
-                            <b>[[company_url]]</b> - {Lang::T('Your APP_URL at Settings')}.<br>
                             <b>[[invoice]]</b> - {Lang::T('Invoice number')}.<br>
                             <b>[[created_at]]</b> - {Lang::T('Date invoice created')}.<br>
                             <b>[[payment_gateway]]</b> - {Lang::T('Payment gateway user paid from')}.<br>
@@ -230,7 +229,7 @@
                                 target="_blank">{Lang::T("read documentation")}</a>.
                         </p>
                     </div>
-                </div>*}
+                </div> *}
 
                 <div class="panel-body">
                     <div class="form-group">