Wolfie6610 Posted February 22, 2022 Share Posted February 22, 2022 Hi All, not sure I'm in the right place but I'm really not figuring this part out. My Invoices are showing dates in the line items as shown below VOIP Business Line Business Line DID: 023*******(22/12/2021 - 21/01/2022) I want the Dates gone in the Per line field can you please assist 🙂 I know i need to edit the invoicepdf.tpl and viewinvoice.tpl Ive searched for the file and got a lot of results. I hope this clears the question but i use Lagom V2 and Clean PDF to showcase invoices, i need the dates removed in the description when logged into whmcs aswell as wyhen thy download the invoice Thanks 🙂 0 Quote Link to comment Share on other sites More sharing options...
leew87 Posted May 6, 2022 Share Posted May 6, 2022 i am also looking for a solution for this 0 Quote Link to comment Share on other sites More sharing options...
leew87 Posted May 9, 2022 Share Posted May 9, 2022 i am looking for assistance to learn this myself and direction on resolve this issue rather than just outsourcing to custom development such little task which it was i thought this community was about not just paid work. 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 9, 2022 Share Posted May 9, 2022 I should have an action hook that removes dates from invoice items. If I remember correctly, it works with all date formats supported by WHMCS. It should be on my old PC but it is not here with me. Btw all it takes is a... SELECT id, description FROM tblinvoiceitems WHERE invoiceid = {ID} ...on InvoiceCreated hook point followed by... SELECT value FROM tblconfiguration WHERE setting = 'DateFormat' LIMIT 1 ... to get DateFormat used by your WHMCS. Depending on DateFormat you'll need 3 different regex to match all 6 formats supported by WHMCS. Once you have your regex, run a foreach on every invoice item description to preg_replace your patternt (eg. (10/10/2020 - 09/11/2020)) with "" (nothing) then... UPDATE tblinvoiceitems SET description = {YOUR_PREG_REPLACED_DESCRIPTION} WHERE id = {YOUR_INVOICE_ITEM_ID} LIMIT 1 Done. 0 Quote Link to comment Share on other sites More sharing options...
leew87 Posted May 9, 2022 Share Posted May 9, 2022 (edited) 10 minutes ago, Kian said: I should have an action hook that removes dates from invoice items. If I remember correctly, it works with all date formats supported by WHMCS. It should be on my old PC but it is not here with me. Btw all it takes is a... SELECT id, description FROM tblinvoiceitems WHERE invoiceid = {ID} ...on InvoiceCreated hook point followed by... SELECT value FROM tblconfiguration WHERE setting = 'DateFormat' LIMIT 1 ... to get DateFormat used by your WHMCS. Depending on DateFormat you'll need 3 different regex to match all 6 formats supported by WHMCS. Once you have your regex, run a foreach on every invoice item description to preg_replace your patternt (eg. (10/10/2020 - 09/11/2020)) with "" (nothing) then... UPDATE tblinvoiceitems SET description = {YOUR_PREG_REPLACED_DESCRIPTION} WHERE id = {YOUR_INVOICE_ITEM_ID} LIMIT 1 Done. thank you i will try and get my head around this and hopefully get it work new to development so all abit confusing for me where would i enter this hook code? sorry if it seems like a silly question i apologies. if you had this already written of course i would be very very greatful if you wished to share then one you have tested. Edited May 9, 2022 by leew87 added to post 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted May 10, 2022 Share Posted May 10, 2022 @leew87 hooks are to be placed in /includes/hooks/ - unless they are part of a module. I don't think the code should run on InvoiceCreated, though - that hook runs after the invoice has been sent to the client, and as such the client would see the dates on the invoice. Executing on InvoiceCreation should work for this. Hook documentation can be found here: https://developers.whmcs.com/hooks-reference This should do the trick for you: <?php use WHMCS\Billing\Invoice; add_hook('InvoiceCreation', 1, function($vars) { // $vars allows us to use get the specific invoice id $invoiceID = $vars['invoiceid']; // Get data related to the invoice. See this for documentation on models: https://docs.whmcs.com/Using_Models // It would also be possible to use Capsule to query the database instead of using the Invoice model. See information on database queries here: https://developers.whmcs.com/advanced/db-interaction/ $invoiceItems = Invoice::find($invoiceID)->items; $pattern = '/\(([0-3][0-9]|[0-1][0-9]|[\d]{4})(\/|\.|-)([0-1][0-9]|[0-3][0-9]|)(\/|\.|-)([\d]{4}|[0-1][0-9]|[0-3][0-9]) - ([0-3][0-9]|[0-1][0-9]|[\d]{4})(\/|\.|-)([0-1][0-9]|[0-3][0-9]|)(\/|\.|-)([\d]{4}|[0-1][0-9]|[0-3][0-9])\)/'; foreach ($invoiceItems as $invoiceItem) { preg_match($pattern, $invoiceItem->description, $matches); $invoiceItem->description = str_replace($matches[0], '', $invoiceItem->description); // Update the description of the invoice's line item $invoiceItem->save(); // Save to the database } }); Quickly explained: We use the Invoice model to find the related invoice during invoice creation. At this time, line items already exists. We define a regex pattern to search for the dates in the line item description. The pattern should match the following types of dates but I would test it thoroughly before using it: (28/04/2022 - 27/04/2023) (28.04.2022 - 27.04.2023) (28-04-2022 - 27-04-2023) (04/28/2022 - 04/27/2023) (2022/04/28 - 2023/04/27) (2022-04-28 - 2023-04-27) Loop through all line items. Whenever a string in the line item description matches the regex, we replace it with nothing (''). Save the new description for the line item. I hope that makes sense. There's probably a better way to do it but this was what I could with the 20 minutes I had to spare 😄 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 10, 2022 Share Posted May 10, 2022 (edited) Okay, I got the script that is similar to @DennisHermannsen solution. You can get it from here on Github. I don't post the script here since I can't update it if needed. It works with multi-line descriptions and automatically detects the Date Format in use on your system so that it always uses the right regex: DD/MM/YYYY DD.MM.YYYY DD-MM-YYYY MM/DD/YYYY YYYY/MM/DD YYYY-MM-DD It triggers on InvoiceCreated hence it doesn't affect existing invoices. I can't test right now but it should work. Edited May 10, 2022 by Kian 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted May 10, 2022 Share Posted May 10, 2022 2 minutes ago, Kian said: p.s. It triggers on InvoiceCreated hence it doesn't affect existing invoices. I can't test right now but it should work. Would that not cause a small issue when WHMCS is sending the invoice to the client? The documentation states this: Executed when an invoice has left “Draft” status and is available to its respective client. Execution of this hook occurs after sending the Invoice Created email. Your regex pattern(s) are way better than what I suggested. Quick question that might be a bit off topic: Is there any difference on using models vs capsule? I know that they're basically the same thing but I'm just wondering if there's any reason to use one instead of the other. 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 10, 2022 Share Posted May 10, 2022 (edited) 1 hour ago, DennisHermannsen said: Would that not cause a small issue when WHMCS is sending the invoice to the client? The documentation states this: Ah! Yes, you're right. InvoiceCreationPreEmail should be a better option. 1 hour ago, DennisHermannsen said: Quick question that might be a bit off topic: Is there any difference on using models vs capsule? I know that they're basically the same thing but I'm just wondering if there's any reason to use one instead of the other. Maybe I'm not the right person to answer this question. When I started coding things like PDO, MySQLi and Laravel were not even a thing. I started using plain PDO within a custom class and I never looked back (I'm lazy). The only reason why I'm using Capsule is that I can't force people to install a custom PDO class in order to use open source scripts. Back to your question, with models you can interact with database without writing a single line of SQL which is great if one doesn't know best practices and syntax. But it also makes your code more readable and less verbose. Personally I always use models when I need to create something that changes very rarely like API commands for webservices. In this case a model is well worth the extra effort. It is just clean, organized and beautiful. For all the rest I prefer just using raw SQL and writing my own statements. Nothing can stop you. On the other hand with Capsule there are certain limitations. Saldy I can't remember them 😌 They were documented somewhere on laravel.com. Something involving sorting and joins. Edited May 10, 2022 by Kian 0 Quote Link to comment Share on other sites More sharing options...
leew87 Posted May 11, 2022 Share Posted May 11, 2022 16 hours ago, DennisHermannsen said: @leew87 hooks are to be placed in /includes/hooks/ - unless they are part of a module. I don't think the code should run on InvoiceCreated, though - that hook runs after the invoice has been sent to the client, and as such the client would see the dates on the invoice. Executing on InvoiceCreation should work for this. Hook documentation can be found here: https://developers.whmcs.com/hooks-reference This should do the trick for you: <?php use WHMCS\Billing\Invoice; add_hook('InvoiceCreation', 1, function($vars) { // $vars allows us to use get the specific invoice id $invoiceID = $vars['invoiceid']; // Get data related to the invoice. See this for documentation on models: https://docs.whmcs.com/Using_Models // It would also be possible to use Capsule to query the database instead of using the Invoice model. See information on database queries here: https://developers.whmcs.com/advanced/db-interaction/ $invoiceItems = Invoice::find($invoiceID)->items; $pattern = '/\(([0-3][0-9]|[0-1][0-9]|[\d]{4})(\/|\.|-)([0-1][0-9]|[0-3][0-9]|)(\/|\.|-)([\d]{4}|[0-1][0-9]|[0-3][0-9]) - ([0-3][0-9]|[0-1][0-9]|[\d]{4})(\/|\.|-)([0-1][0-9]|[0-3][0-9]|)(\/|\.|-)([\d]{4}|[0-1][0-9]|[0-3][0-9])\)/'; foreach ($invoiceItems as $invoiceItem) { preg_match($pattern, $invoiceItem->description, $matches); $invoiceItem->description = str_replace($matches[0], '', $invoiceItem->description); // Update the description of the invoice's line item $invoiceItem->save(); // Save to the database } }); Quickly explained: We use the Invoice model to find the related invoice during invoice creation. At this time, line items already exists. We define a regex pattern to search for the dates in the line item description. The pattern should match the following types of dates but I would test it thoroughly before using it: (28/04/2022 - 27/04/2023) (28.04.2022 - 27.04.2023) (28-04-2022 - 27-04-2023) (04/28/2022 - 04/27/2023) (2022/04/28 - 2023/04/27) (2022-04-28 - 2023-04-27) Loop through all line items. Whenever a string in the line item description matches the regex, we replace it with nothing (''). Save the new description for the line item. I hope that makes sense. There's probably a better way to do it but this was what I could with the 20 minutes I had to spare 😄 hi thank you for your code when i contacted whmcs support before knowing i could post here they said the following There isn't a way within WHMCS, as it comes, to remove such description data. The data is stored in the tblinvoiceitems database table, which you will see is stored as: PRODUCT NAME (08/04/2022 - 07/05/2022) You would need to remove that from the table itself upon invoice generation, but before it's delivered to the client, via a hook like InvoiceCreation: https://developers.whmcs.com/hooks-reference/invoices-and-quotes/#invoicecreation Capsule can be used to update the value: https://developers.whmcs.com/advanced/db-interaction/ A basic to intermediate knowledge of PHP would be required to achieve it. so from what i understand InvoiceCreation is where they recomend. 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 11, 2022 Share Posted May 11, 2022 (edited) 10 hours ago, leew87 said: so from what i understand InvoiceCreation is where they recomend. As Denniss suggested, InvoiceCreationPreEmail is a better option. This way you can remove dates from items before WHMCS sends the invoice via email to customers. InvoiceCreation works too but triggers when customers have already received PDF that still has dates. Edited May 11, 2022 by Kian 0 Quote Link to comment Share on other sites More sharing options...
leew87 Posted May 11, 2022 Share Posted May 11, 2022 Ok thank you for taking the time to explain has the github script been edited to reflect or would I need to make the adjustment to change hook? 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 11, 2022 Share Posted May 11, 2022 Yep. 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted May 12, 2022 Share Posted May 12, 2022 @Kian small thing to note about InvoiceCreationPreEmail: Quote Executes as an invoice is being created in the admin area before the email is being sent I haven't used that hook event for a long time, and I'm pretty sure that ^ is the reason I stopped using it 😅 I don't know if it has been changed since I last used it. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.