Jump to content

InvoiceCreationPreEmail hook not working in WHMCS 8.1


webtechwhiz

Recommended Posts

Dear Team,

I have created following hook for invoice number formatting:


use Illuminate\Database\Capsule\Manager as Capsule;

function generate_custom_invoice_number_hook($vars) {
   $invoiceid = $vars['invoiceid'];
   $customnumber = 'WTW'.date("Y").str_pad($invoiceid,5,"0",STR_PAD_LEFT);
   
   if (isset($customnumber)) {
       try {
           $updatedInvoiceNumber = Capsule::table('tblinvoices')
                                   ->where('id', $invoiceid)
                                   ->update(['invoicenum' => $customnumber,]);
       }
       catch (\Exception $e) {
           // Deal with error
       }
   }
}
add_hook("InvoiceCreationPreEmail",1,"generate_custom_invoice_number_hook");

But there is  no impact on Invoice Number. Invoice is generated with default invoice number like 1,2,3,...

Why invoice number is  not getting updated as expected like WTW202100001, WTW202100002,WTW202100003,... as expected with using above hook?

 

Kindly help to get desired invoice number.

 

Regards,

Shubhajeet Saha

 

 

Link to comment
Share on other sites

On 10/04/2021 at 07:40, webtechwhiz said:

Why invoice number is  not getting updated as expected like WTW202100001, WTW202100002,WTW202100003,... as expected with using above hook?

it works for me...

gWUf5ZA.png

if the hook is failing (assuming it's uploaded to the correct /includes/hooks folder and there are no encoding issues with the file), then that implies that there is something about the customnumber definition that it doesn't like - though I can't see what.

are you sure that the file is uploaded to the correct folder for action hooks ?

if you use one of the original versions of the hook, does it work ??

also, don't forget that the hook would only be called when generating new invoices - as written, it couldn't be used to update existing invoices.

Link to comment
Share on other sites

Hi  Brian,

There is other hooks too there in hooks folder which like customMenu.php is working fine. And custom invoice number formatting hook is also paced there only file name custominvoicenumberformat.php. I have attached screenshot. Also attached hook file in edit mode displaying the location of custominvoicenumberformat.php. I know hook will only work new invoices onwards.

Also attached current Tax Configuration screenshot. Presently Custom invoice numbering is enabled. Should I turn it off?

Awaiting reply...

Thanks

screencapture-server1-webtechwhiz-2083-cpsess6087038601-frontend-paper-lantern-filemanager-index-html-2021-04-12-19_26_32.png

file-in-includes-hooks-folder.png

screencapture-webtechwhiz-wtw-shop-admin-setup-payments-tax-2021-04-12-19_48_16.png

Link to comment
Share on other sites

Hi Brian,

Forgot to mentioned one thing. You have replied on 10th April, 2021 but notification email received just 5 hours ago.  Don't know why there was so much delay. Ideally it should trigger soon a reply is made. There should not be any waiting to get reply approved by moderator.

Thanks kindly help me to find out the issue. I have explained everything in my previous reply.

Regards,

Link to comment
Share on other sites

6 minutes ago, webtechwhiz said:

I have made custom Invoice numbering off. What should be filled in format field only {number} ? Or anything else?

if the feature is disabled, then what you put in that field won't matter - it's the hook that's renaming invoices on creation, not this feature in WHMCS.

Link to comment
Share on other sites

6 minutes ago, brian! said:

if the feature is disabled, then what you put in that field won't matter - it's the hook that's renaming invoices on creation, not this feature in WHMCS.

Hi Brian,

Thanks for quick reply, As per your statement hook should work now. But I just tested with an order though its unpaid.  Its showing invoice number as #3. Would like to know when Invoice number gets formatted with the above hook at the time of placing order or after Payment. 

Unpaid invoice is attached for your reference. Kindly guide me so that I can make this hook work.  Awaiting for your guidance...

 

Regards,

Shubhajeet Saha

 

 

Invoice-3.pdf

Link to comment
Share on other sites

1 hour ago, brian! said:

it might be because proforma invoicing is enabled - i'll have to check this in the morning... or you could disable it and see if the hook runs.

Hi Brian,

Thanks for your reply. I'll wait for your investigation report. I have disabled Proforma Invoicing and after disabling proforma invoicing no invoice is created. Is there any hook which can format invoice number in Proforma Invoice?

Searching in WHMCS hooks references but unable to find any such hook. It will be better if any such is available or can be provided. Awaiting reply...

Regards,

Shubhajeet Saha

Link to comment
Share on other sites

5 hours ago, brian! said:

how important is it to you to have leading zeros in front of the invoice number part e.g WTW202100014 ?? 

Hi Brian, 

Leading 0 is important to me. I want invoice number in my given format and I can create such invoice number using core PHP, Codeigniter and in Laravel too. But here is no option available for custom coding. Hooks given are not working.

If above any option is introduced in next release of WHMCS to check want leading 0 to invoice number and size of invoice then it will be very useful.

 

But my simple question when there is no scope of custom coding and hooks are provided then why it's not working? If any option is given the should work properly.

 

Also I should let know that client who has forgotten his account password is unable to reset his account password. At the reset page client fills his registered email id and a reset link is received. When he clocks the link it again asks for email id. No page appears to enter new password. Though I have created ticket for this but letting you know to check what is the issue with latest version WHMCS and Twenty One theme.

 

Awaiting your reply...

 

Regards,

Shubhajeet Saha

Link to comment
Share on other sites

15 hours ago, webtechwhiz said:

Leading 0 is important to me. I want invoice number in my given format and I can create such invoice number using core PHP, Codeigniter and in Laravel too.

in fairness, you chose the hook and didn't mention your invoicing setup.

15 hours ago, webtechwhiz said:

But here is no option available for custom coding. Hooks given are not working.

as hard as I try, I cannot make this hook fail - it's working with proformas for me...

nKmfcOA.png

15 hours ago, webtechwhiz said:

If above any option is introduced in next release of WHMCS to check want leading 0 to invoice number and size of invoice then it will be very useful.

I wouldn't hold your breath waiting for that.

15 hours ago, webtechwhiz said:

But my simple question when there is no scope of custom coding and hooks are provided then why it's not working? If any option is given the should work properly.

I think either a) the hook isn't actually running on your server and/or b) your tax/invoice settings are overwriting the changes somehow.... I simply cannot reproduce the issue locally.

enabling hook debug mode might be useful, as might tweaking the hook code to add a log entry upon successful running.

15 hours ago, webtechwhiz said:

Also I should let know that client who has forgotten his account password is unable to reset his account password. At the reset page client fills his registered email id and a reset link is received. When he clocks the link it again asks for email id. No page appears to enter new password. Though I have created ticket for this but letting you know to check what is the issue with latest version WHMCS and Twenty One theme.

does your site use 21 ? I thought it was a fully custom theme.... in any event, that's one for Support to deal with... the real test would be if the links fail with a clean version of Six or 21 - that should at least tell you if it's a template issue or something else.

Link to comment
Share on other sites

Hi Brian,

Sorry for late reply. My site using TwentyOne theme and all its functions, only some design customization is done. I have raised issue with Support tkt number is #KQX-559385 and below was their reply:

Quote

Hi Shubhajeet,
Regrettably we are unable to assist with customisations here as part of product technical support.

For further assistance customising WHMCS I'd recommend posting on our Developer Corner community where our friendly and knowledgeable community will I'm sure be pleased to assist in this matter: https://whmcs.community/community/41-developer-corner/

If we can assist with any software technical errors, please don't hesitate to get back in contact.

Kind Regards,

--
John Kipling
Head of Support
WHMCS, Ltd.
www.whmcs.com

 

Now tell me where should I report my issues. Support guys do access our cpanel and admin and fixes the issues. But they redirected me to Developer community. And here you senior developers guides us for any issue, always grateful for your help but you produce instance locally not on our site. Then how this issue will get fixed? The result your are showing is clearly indicating there is no issue with the coding neither with PI or Final Invoice.

Now at least guide me whom I should contact for my issue? Who will test the issue on my server and find out why it is not working?

Awaiting your reply...

 Regards,

Shubhajeet Saha

Link to comment
Share on other sites

Hi Brian,

Would like to update that I have reopened ticket with this community post and as suggested by them I have created a new quote from Admin and then converted it into Invoice. Then the hook worked and created unpaid invoices WTW202100027 and WTW202100028 but when I tried to place and order from user login it generated proforma invoice 29.

Can you please put some light on this issue? I think if it starts working in PI then it will work for final Invoice too.

Request you to make a response. Awaiting your reply...

Regards,

Shubhajeet Saha

Link to comment
Share on other sites

Hi Brian,

Now WHMCS support has given hook AfterShoppingCartCheckout to format invoice number when client makes purchases from fontend:

I had written hook code as below:

function generate_custom_invoice_number_4Client_hook($vars) {
   $invoiceid = $vars['InvoiceID'];
   $customnumber = 'WTW'.date("Y").str_pad($invoiceid,5,"0",STR_PAD_LEFT);
   
   if (isset($customnumber)) {
       try {
           $updatedInvoiceNumber = Capsule::table('tblinvoices')
                                   ->where('id', $invoiceid)
                                   ->update(['invoicenum' => $customnumber,]);
       }
       catch (\Exception $e) {
           // Deal with error
       }
   }
}

add_hook('AfterShoppingCartCheckout', 1, "generate_custom_invoice_number_4Client_hook");

But its not working. As given in hook documentation below parameters should be available:

Parameters

Variable Type Notes
OrderID int The Order ID
OrderNumber int The randomly generated order number
ServiceIDs array An array of Service IDs created by the order
AddonIDs array An array of Addon IDs created by the order
DomainIDs array An array of Domain IDs created by the order
RenewalIDs array An array of Domain Renewal IDs created by the order
PaymentMethod string The payment gateway selected
InvoiceID int The Invoice ID
TotalDue float The total amount due

Hence I have fetched InvoiceId as $invoiceid = $vars['InvoiceID']. But not working.

Awaiting for your reply.

Regards,

Shubhajeet Saha

Link to comment
Share on other sites

I suggest to inspect the content of $vars. E.g.:

function generate_custom_invoice_number_4Client_hook($vars) {
   logActivity (json_encode($vars));
   $invoiceid = $vars['InvoiceID'];
   $customnumber = 'WTW'.date("Y").str_pad($invoiceid,5,"0",STR_PAD_LEFT);
   
   //if (isset($customnumber)) { // would be always true...
   if (!empty($invoiceid)) {
       try {
           $updatedInvoiceNumber = Capsule::table('tblinvoices')
                                   ->where('id', $invoiceid)
                                   ->update(['invoicenum' => $customnumber,]);
       }
       catch (\Exception $e) {
           logActivity ($e->getMessage());
       }
   }
}

add_hook('AfterShoppingCartCheckout', 1, "generate_custom_invoice_number_4Client_hook");

Then check the WHMCS activity log to see what $vars contains. This will also reveal whether the hook is executed at all.

Edited by string
Link to comment
Share on other sites

Dear String,

I have implemented above code, enabled hook debug mode and display errors. But no variable data is logged in tblactivitylog table. And in WHMCS v8.0+ I am unable to find Configuration > System Logs menu from Admin Dashboard.

There are many logs for the hook 1 given below in description column:

Hooks Debug: Hook Defined for Point: AfterShoppingCartCheckout - Priority: 1 - Function Name: generate_custom_invoice_number_4Client_hook

But no log entries have logged any variables. I think logActivity (json_encode($vars)); didn't worked.

Awaiting your reply...

Regards,

Shubhajeet Saha

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