Jump to content

Skip specific Invoices to Xero


pRieStaKos

Recommended Posts

Hello,

I'm using Xero v4.3 module from EdgeHosting and I'm trying to skip specific invoices been pushed to Xero, from WHMCS.

I set these invoices with custom `invoicenum` (not `invoiceid`) and used (after EdgeHosting's suggestion) `custom-hook.php`, from their module folder.

My code is the following:

<?php

namespace EdgeHosting;

use WHMCS\Database\Capsule;

/**
 * Hook point triggered before bulk data is sent to Xero
 *
 * @param string $type The pluralised entity type being sent to Xero (i.e. contacts, invoices, payments)
 * @param array $data An array of entities being sent to Xero
 * @return array You must return the original data array with any modifications
 */
function preXeroSync($type, array $data)
{
    if ($type == 'invoices') {
        foreach ($data as $key => &$invoice) {
            if (isset($invoice['Reference']) && strpos($invoice['Reference'], 'TEST/INV') !== false) {
                unset($data[$key]);
            }
        }
    }
    unset($invoice);

    return $data;
}

All the invoices are been pushed to Xero, no mater what Reference exists (or not), so all invoices are been pushed and skip is not working.

Code was confirmed by EdgeHosting that "it should work" and "If you require further customisation they offer a paid service." (No, thank you).

Do you see something I'm not ? 

Link to comment
Share on other sites

  • 4 months later...

Looks like you left out the add_hook() call to actually cause the hook to be added somewhere.  As you have it, you're defining a function but it never gets called.

add_hook("InvoiceCreation", 9999, "preXeroSync");

except that the hook on which you want this to run probably isn't InvoiceCreation, so you'll need to work out what the correct hook name is for your code to work.

Edited by brianoz
Link to comment
Share on other sites

8 hours ago, brianoz said:

Looks like you left out the add_hook() call to actually cause the hook to be added somewhere.  As you have it, you're defining a function but it never gets called.


add_hook("InvoiceCreation", 9999, "preXeroSync");

except that the hook on which you want this to run probably isn't InvoiceCreation, so you'll need to work out what the correct hook name is for your code to work.

As I mentioned, this is a module hook. So it run with the module. No need to add_hook. The abovementioned code was verified by the module's author, but it's not working.

Although, thank you for your reply. I don't need this anymore.

Link to comment
Share on other sites

  • 2 weeks later...

If you use hook code to define a function, that function will never be called unless you either call it in the same file, or use a module hook.  In your code, you define a function but it is never called.

Pretty sure here that you've misunderstood how module hook code works.  The file is read, and it defines functions which are then connected via add_hook() calls.  No sweat, this is complex if you're not used to it.  🙂

 

Link to comment
Share on other sites

9 minutes ago, brianoz said:

If you use hook code to define a function, that function will never be called unless you either call it in the same file, or use a module hook.  In your code, you define a function but it is never called.

Pretty sure here that you've misunderstood how module hook code works.  The file is read, and it defines functions which are then connected via add_hook() calls.  No sweat, this is complex if you're not used to it.  🙂

 

You cannot misunderstand the encoded module instructions, neither the instructions from the modules support team that you need only to place your code in the custom-hook.php file.

This is a namespace function that is probably triggered in modules function. It’s triggered by the module (on daily cron and invoice creation), but the module is encoded and commercial.

So, either something is wrong in the hook’s code, either support is not helping because they want more money…

Anyway, it’s not my problem anymore 🙂 This was for an old client, back then.

Edited by pRieStaKos
Link to comment
Share on other sites

  • 1 month later...
On 9/25/2022 at 5:47 PM, pRieStaKos said:

You cannot misunderstand the encoded module instructions, neither the instructions from the modules support team that you need only to place your code in the custom-hook.php file.

I'm sorry to partly disagree, but for a WHMCS hook you do need to call the add-hook function to get the code to execute.  The code will be READ and loaded by virtue of it being in the custom-hook file (easy to verify by simply including die("---hook code executed---"); in the file!) but if you define a function that function will never get called.  Alternatively, you could simply call the function directly, although that's considered bad practice, as you generally want hooks to only run at certain points in WHMCS' execution.   

If the module you are loading checks for and then calls a PHP function called "preXeroSync()", this is NOT a WHMCS hook, and you are completely correct that it doesn't need a WHMCS add_hook() call to trigger it.  The reason it's not running would be one of these:

  1. The function isn't defined with the correct name, or namespace, or isn't visible somehow to the module code when it runs.  I'd try commenting out the namespace to see if it works without that.
  2. The function isn't defined early enough to be visible to the Edgehosting code when it needs to see it.  It might be that you need to move the source code to the main hook folder and see if that works.

I realize this is way too late to be helpful, but just want to make sure people don't get confused.  Looks to me like you had the second case, where it wasn't a WHMCS hook at all; apologies for not catching earlier.

Link to comment
Share on other sites

Hook/function or whatever is been triggered by module as it dumps test logs, during my initial checks.

Then I contacted the developer team of the module and confirmed that my code is correct and should work. This until they asked further support ($) to check it from their side.

So all the above discussion is welcome for future reference, but the issue is not solved.

This was for an old client so anyway, thank you all for your messages.

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