Jump to content

Custom Invoice Prefix based on Payment method


Eugene

Recommended Posts

Hello,

Can I somehow segregate invoice prefixes and/or Pay-to-text based on the payment method selected by the client? 

For example

All PayPal paid invoices should come up like PP/123  PP/124  PP/125.. and so on, with Pay To text  "Paid to Company 1"
All other invoices paid using another payment method should come up like "INV/123  iNV/124  INV/125 and so on, with "Pay To" text "Paid to Company 2"

Link to comment
Share on other sites

Eugene, 
          for this, you need to modify the Invoice number generation code. But you can't do this in WHMCS because  WHMCS code is encoded and no hooks for this.

You can do this by creating a custom hook before this disable auto sequence invoice number from Setup -> General Settings and in Setup->Tax Configuration.
Then in the custom hook use the WHMCS InvoicePaid hook. this hook is called when an invoice is marked paid. 
in this hook, you have the invoice ID which is just marked paid.
Here follow these step 

  1. First, fetch the invoice information by using GetInvoice WHMCS API. From this API you receive all information about this invoice including payment method.
  2. Now you have a payment method. Save it in a variable as  $paymethod.
  3. Now get the last paid invoice with the same method as the code below
    $stmtx = Capsule::table('tblinvoices')->where('paymentmethod',$paymethod)->where('id','!=',$invoiceid)->orderBy('datepaid', 'desc')->first();

    here you got the last paid invoice with the same payment method.  generate the next invoice number by adding 1 to your last paid invoice number. for example as per your format  INV/125
    Note: maybe you not receive any invoice details only in case you do not have any paid invoice with this payment method. So we cover both scenarios in the below code

    if($stmtx){
      $last_inv_num = $stmtx->invoicenum;
      $inv_parts = explode('/',$last_inv_num);
      $increase_one = $inv_parts[1] + 1;
      $next_invoice_number = $inv_parts[0]."/".$increase_one;  
    }else{
      $inv_format_array = array("paypal"=>"Pay","Debit_Card"=>"INV");
      $next_invoice_number = $inv_format_array[$paymethod].'/1';    //it gernerate invoice number as per paymentmethod like INV/1
    }

     

  4. Now Update invoice number to invoice ID like 
    Capsule::table('tblinvoices')->where('id',$invoiceid)->update(array('invoicenum'=>$next_invoice_number));
  5. save your hook file in your WHMCS install directory->includes->hooks
  6. that it. all set

Note:  we do not recommend that you generate two different invoice number sequences.

Link to comment
Share on other sites

On 2/27/2024 at 8:19 AM, Saroha said:

Note:  we do not recommend that you generate two different invoice number sequences.

Any specific reason why? WHMCS itself has the option to format the invoice numbers but not two different ones.

Is it going to create problems in the database somewhere? 
Is it because of a compatibility issue? 

Edited by Eugene
typo correction
Link to comment
Share on other sites

WHMCS has the option to format the invoice number because every user wants a different format option. but it does not give two different sequence options because it's a different requirement. I think you are the first who demands this. 
if you do this with your custom hooks I did not create any problem in the database because WHMCS works on InvoiceID 
and has no compatibility issue
we just recommend it only in case of server slowness or case two invoices are raised at the same time with the same payment method then maybe you have a duplicate invoice number. But the chance of this is only 0.001%. but make sure auto sequencing should be disabled.

Out of this topic but important for you I checked your website I found that you using our SEO SMO Manager modules but you have not updated it with its latest version 4.1 autopilot. You have a valid support addon means you can free update it. Please download the latest version and enjoy its autopilot SEO for WHMCS product pages and Knowledgebase bases with schema markup.

Link to comment
Share on other sites

1 hour ago, twhiting9275 said:

Before you consider doing this, please make sure you understand the legalities in your area. As you're trying to modify an existing (paid) invoice, this may not exactly be legal in your area.

 

I'm not modifying existing invoices. The new ones going forward

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