Jump to content

Different invoice numbers for each payment method?


Razva

Recommended Posts

Is there any module that allows me to set a different invoice number for each payment method? For example BankTransfer should generate BT-1, PayPal should generate PP-1 etc.

none that springs to mind - I know that you can add some more tags with the Billing Extension addon, but payment method wouldn't be one of them.

 

but you have to remember that effectively these tags are just values added to the invoicenum value in the database table... therefore, if you write a hook to update that value after the invoice has been created, but before it's been sent to the client, you will have achieved what you want.

 

the hook below should get you closer to what you want...

<?php

/**
* Generate Custom Invoice Number Format Based On Payment Method
* @author brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;

function generate_custom_invoice_number_hook($vars) {
   $invoiceid = $vars['invoiceid'];
   $paymentmethod = Capsule::table('tblinvoices')
               ->where('id',$invoiceid)
               ->value('paymentmethod');
   if ($paymentmethod == 'paypal') {
       $customnumber = "PP-".$invoiceid;
   }
   elseif ($paymentmethod == 'banktransfer') {
       $customnumber = "BT-".$invoiceid;
   }    
   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");
?>

so if invoice uses PayPal, it adds 'PP-{invoice number}' as the invoice number, 'BT' if Bank Transfer is used; if neither, it does nothing and it will just use the invoiceid value instead... obviously, just expand the if statement to add more payment methods.

 

I did toy with querying the db to get a list of active payment methods, but unless you were using a LOT of gateways and had a simple way to abbreviate them all in the invoice numbers, I didn't think it would be worth it - it's quicker to just expand the if statement. :idea:

 

btw - i'm assuming that you're going to use the invoiceID values and you don't want different invoices to use the same number, e.g the first Paypal invoice is PP-1 and the first BankTransfer invoice is BT-1 (and not BT-2 or whatever) ? if not, then you're going to need additional coding to ensure the save invoicenum isn't reused.

Link to comment
Share on other sites

  • 10 months later...
On 9/9/2017 at 6:43 PM, brian! said:

none that springs to mind - I know that you can add some more tags with the Billing Extension addon, but payment method wouldn't be one of them.

 

but you have to remember that effectively these tags are just values added to the invoicenum value in the database table... therefore, if you write a hook to update that value after the invoice has been created, but before it's been sent to the client, you will have achieved what you want.

 

the hook below should get you closer to what you want...

 


<?php

/**
* Generate Custom Invoice Number Format Based On Payment Method
* @author brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;

function generate_custom_invoice_number_hook($vars) {
   $invoiceid = $vars['invoiceid'];
   $paymentmethod = Capsule::table('tblinvoices')
               ->where('id',$invoiceid)
               ->value('paymentmethod');
   if ($paymentmethod == 'paypal') {
       $customnumber = "PP-".$invoiceid;
   }
   elseif ($paymentmethod == 'banktransfer') {
       $customnumber = "BT-".$invoiceid;
   }    
   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");
?>
Hello, Brian can you help me? I use 7,5 version whcms but invoice generate only with number. I am need normal invoice for clients example 201807241. This no working for me Available auto-insert tags are: {YEAR} {MONTH} {DAY} {NUMBER} only invoice have number. Please help with hooks?

 

so if invoice uses PayPal, it adds 'PP-{invoice number}' as the invoice number, 'BT' if Bank Transfer is used; if neither, it does nothing and it will just use the invoiceid value instead... obviously, just expand the if statement to add more payment methods.

 

I did toy with querying the db to get a list of active payment methods, but unless you were using a LOT of gateways and had a simple way to abbreviate them all in the invoice numbers, I didn't think it would be worth it - it's quicker to just expand the if statement. :idea:

 

btw - i'm assuming that you're going to use the invoiceID values and you don't want different invoices to use the same number, e.g the first Paypal invoice is PP-1 and the first BankTransfer invoice is BT-1 (and not BT-2 or whatever) ? if not, then you're going to need additional coding to ensure the save invoicenum isn't reused.

 

Link to comment
Share on other sites

19 hours ago, Darius said:

Hello, Brian can you help me? I use 7,5 version whcms but invoice generate only with number. I am need normal invoice for clients example 201807241. This no working for me Available auto-insert tags are: {YEAR} {MONTH} {DAY} {NUMBER} only invoice have number. Please help with hooks?

{YEAR} {MONTH} {DAY} {NUMBER} would only work if you enabled Sequential Invoicing.

probably a better hook to modify for your needs would be the one I posted in the thread below...

<?php

/**
* Generate Custom Invoice Number Format
* @author brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;

function generate_custom_invoice_number_hook($vars) {
   $invoiceid = $vars['invoiceid'];
   $customnumber = date("Y").date("m").date("d").$invoiceid;
   
   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");
?>

kZouHHg.png

so for new invoices, it will use a format of (YEAR)(MONTH)(DATE)(INVOICE NUM) hardcoded by the hook.

Link to comment
Share on other sites

  • 5 months later...

This doesn't seem to work in 7.6.1.

/includes/hooks]# cat customInvoicePrefix.php
<?php

/**
* Generate Custom Invoice Number Format
* @author brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;

function generate_custom_invoice_number_hook($vars) {
   $invoiceid = $vars['invoiceid'];
   $customnumber = "SI-".$invoiceid;

   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");

image.png.20cf480804519f1cf0d8d7d16476be4a.png

Tried several different variants.

Edited by John Kennedy
Link to comment
Share on other sites

Hi John,

there's something weird about how you've typed in $customnumber... if I re-encode the file to utf-8, that fixes the issue...

<?php

/**
* Generate Custom Invoice Number Format
* @author brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;

function generate_custom_invoice_number_hook($vars) {
   $invoiceid = $vars['invoiceid'];
   $customnumber = "SI-".$invoiceid;
   
   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");
?>

the hook is working for me locally in v7.6.1...

opSy7MM.png

Link to comment
Share on other sites

this is what I see in my hook...

104y9qX.png

and what I saw in yours...

rK8czgA.png

so it's checking that a variable is set - but it's seeing it as different from the one you're defining.... so the db update is never triggered.

I suppose you could check if $invoiceid is empty instead... and I assume there are no other hooks that could be interfering with this ??

Link to comment
Share on other sites

OK, got it working, but now I got a new problem - I need the same to work for manual invoices after having them published.

Sometimes we need to backdate invoices and the only way to do it is to create an invoice, publish it without sending, change the dates and only them email it. This hook doesn't work it you publish without sending it right away.

Can it be changed to something like "InvoiceCreationPostCreate"? What will it break if it can?

 

Link to comment
Share on other sites

14 hours ago, John Kennedy said:

Can it be changed to something like "InvoiceCreationPostCreate"? What will it break if it can? 

Anyone looking to do the same:

update the last call to "InvoiceCreation"

add_hook("InvoiceCreation",1,"generate_custom_invoice_number_hook");

 

@brian!

Thanks for all the help! You're a legend.

Edited by John Kennedy
To say thank you
Link to comment
Share on other sites

  • 1 year later...
On 9/9/2017 at 9:13 PM, brian! said:

none that springs to mind - I know that you can add some more tags with the Billing Extension addon, but payment method wouldn't be one of them.

 

but you have to remember that effectively these tags are just values added to the invoicenum value in the database table... therefore, if you write a hook to update that value after the invoice has been created, but before it's been sent to the client, you will have achieved what you want.

 

the hook below should get you closer to what you want...

 


<?php

/**
* Generate Custom Invoice Number Format Based On Payment Method
* @author brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;

function generate_custom_invoice_number_hook($vars) {
   $invoiceid = $vars['invoiceid'];
   $paymentmethod = Capsule::table('tblinvoices')
               ->where('id',$invoiceid)
               ->value('paymentmethod');
   if ($paymentmethod == 'paypal') {
       $customnumber = "PP-".$invoiceid;
   }
   elseif ($paymentmethod == 'banktransfer') {
       $customnumber = "BT-".$invoiceid;
   }    
   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");
?>
 

 

so if invoice uses PayPal, it adds 'PP-{invoice number}' as the invoice number, 'BT' if Bank Transfer is used; if neither, it does nothing and it will just use the invoiceid value instead... obviously, just expand the if statement to add more payment methods.

 

I did toy with querying the db to get a list of active payment methods, but unless you were using a LOT of gateways and had a simple way to abbreviate them all in the invoice numbers, I didn't think it would be worth it - it's quicker to just expand the if statement. :idea:

 

btw - i'm assuming that you're going to use the invoiceID values and you don't want different invoices to use the same number, e.g the first Paypal invoice is PP-1 and the first BankTransfer invoice is BT-1 (and not BT-2 or whatever) ? if not, then you're going to need additional coding to ensure the save invoicenum isn't reused.

How to achieve this if i want different invoices to use the same number EX: PP-1 and BT-1. I just want to display such format on invoice not in DB update

Link to comment
Share on other sites

1 hour ago, Prahost said:

How to achieve this if i want different invoices to use the same number EX: PP-1 and BT-1.

you would have to check the last number used for that gateway option and add 1 to it.

1 hour ago, Prahost said:

I just want to display such format on invoice not in DB update

it's the db update that saves you having to modify the template outputs... if you're not updating the db, then you're going to have to do more work to display this change.

 

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