Jump to content

Overdue Invoices - Prevent Client Access to Client Area


Recommended Posts

So this fantastic hook which I got when trying to download the limit unverefied hook by @sentq

from his website. Been working flawlessy until that is updating whmce 😞 the hook code is:

<?php
/**
 * Prevent Client Access to Client Area, Except to My Invoices page if they have Overdue invoices
 *
 * @author     SENTQ <support@whmcms.com>
 * @copyright  Copyright (c) SENTQ 2016
 * @link       http://www.whmcms.com/
 */

if (!defined("WHMCS")){
    die("This file cannot be accessed directly");
}

use Illuminate\Database\Capsule\Manager as Capsule;
use WHMCS\View\Menu\Item as MenuItem;

add_hook("ClientAreaPage", 1, function($vars){
    
    /**
     * Overdue Invoices Limit
     *
     * If client has overdue invoices more than this number a restriction will be applied
     * Default is 0, mean if client has 1 or more overdue invoices
     */
    $overdueInvoicesLimit = 0;
    
    /**
     * Allow Access To Client Area Homepage
     *
     * True  = Allowed
     * False = Redirect To My Invoices
     */
    $allowAccessToCAHomePage = false;
    
    # Admin Logged-in as Client
    if (isset($_SESSION['adminid'])){
        return;
    }
    
    # ~
    $client = Menu::context('client');
    
    # Client Logged in
    if (is_null($client)===false){
        
        # Get Overdue Invoices
        $getInvoices = Capsule::table("tblinvoices")
        ->where("status", "UnPaid")
        ->where("userid", $client->id)
        ->where("duedate", "<", date("Y-m-d"))
        ->count();
        
        # Client Has 1 or more Overdue Invoices
        if ($getInvoices>intval($overdueInvoicesLimit)){
            
            $redirect = true;
            
            # Pages Where The Redirect Action Will Not Applied
            if (in_array($vars['filename'], array("viewinvoice", "creditcard"))){
                $redirect = false;
            }
            if ($vars['filename']=="clientarea" && in_array($_REQUEST['action'], array("invoices", "addfunds", "masspay", "checkout"))){
                $redirect = false;
            }
            
            if ($vars['filename']=="clientarea" && empty($_REQUEST['action']) && $allowAccessToCAHomePage===true){
                $redirect = false;
            }
            
            # Now Redirect Client to My Invoices
            if ($redirect===true){
                
                header("Location: clientarea.php?action=invoices");
                
				
    return [
        '<div class="alert alert-success"><div class="row"><div class="col-sm-1"><i class="fab fa-whmcs fa-3x"></i></div><div class="col-sm-11">Welcome to WHMCS!<br><small>buy the addon!</small></div></div></div>',
    ];
                exit;
                
            }
        }
        
    }
});

been working great.

Only problem now is that it breaks after the pay invoice page!

So when you view an invoice it takes you to the view invoice page. When you click on the button it takes you to a url similar to this: yourwebsite/clients/invoice/28/pay

So as the hook contains restrictions it breaks. I figure that adding into either one of the arrays here:

 # Pages Where The Redirect Action Will Not Applied
            if (in_array($vars['filename'], array("viewinvoice", "creditcard"))){
                $redirect = false;
            }
            if ($vars['filename']=="clientarea" && in_array($_REQUEST['action'], array("invoices", "addfunds", "masspay", "checkout"))){
                $redirect = false;
            }

would fix the issue BUT I have tried all the different names avaliabe inserted into the array and cannot seem to figure out what the page name, file name (or anyting else for that matter) would be to add into the array for it to work.

At least if that part works I can then test the payment out. I am assuming before hand that it will break somewhere else donw the line due to way whmcs is not using different urls.

Seeing as @sentq was the original dev (credit to you for a fantastic hook) would you know what name needs going into the array? or as @brian! is another valued contributer maybe he could input a little?

Link to comment
Share on other sites

1 hour ago, brian! said:

try adding the code below to that list of if statements

Thank you @brian! will give that a go hopefully with your addition it will work flawlessly again!

Should help others who are using it too and prob don't know its not fully working with the new version.

Link to comment
Share on other sites

Oh forgot to mention a glaring obvious missing behaviour! Although the hook does what it should it misses a message to advise the customer. Something on the lines of: you need to pay overdue/outstanding invoice before tou can access services. I tried adding in a return div message but it broke the hook. @brian! where and how would that go into existing hook?

Link to comment
Share on other sites

21 hours ago, Manchester Web Hosting said:

where and how would that go into existing hook?

personally, I wouldn't use the existing hook for that, i'd use a second hook.

also, it's worth remembering that not all hook points work in the same way - some return html, others return variables, some are used to return errors etc - so don't assume that because you can return html in one hook, that another hook will work the same way.

what you really need to think about is exactly where and when will this message be displayed - and only when you know both of those, can you work out how to do it - it might be a ClientAreaFooterOutput hook that is triggered on the same conditions as the primary hook, e.g they have overdue invoices and they're currently on the invoices page... or you might need to edit the appropriate template if that's easier (e.g if you want the output to be in a position that is awkward to code for in a hook).

the point being that if the template isn't expecting to output a variable at a particular location, you can't natively define where to output it in a hook - you can often do it using jQuery, but that's dependent on being able to locate that intended spot with the current template.

for example, the alert below is generated by a CAFO hook triggered when the above two conditions are met.

aP7LYv0.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated