Jump to content
DennisMidjord

Hook to cancel invoices for terminated products

Recommended Posts

Don't know if this is the right category to post this in, but I thought I wanted to share this.

It's a very small hook that cancels an invoice once the product gets terminated.

//<?php

use WHMCS\Database\Capsule;
add_hook('PreModuleTerminate', 1, function($vars) {
	$invoiceid = Capsule::table('tblinvoiceitems')
		->where('relid', '=', $vars['params']['serviceid'])
		->orderBy('duedate', 'desc')
		->first();
	Capsule::table('tblinvoices')
		->where('id', '=', $invoiceid->invoiceid)
		->update(['Status' => 'Cancelled']);
});

Any feedback is much appreciated.

Share this post


Link to post
Share on other sites
//Updated to only target products of Hosting-type.
<?php
use WHMCS\Database\Capsule;
add_hook('PreModuleTerminate', 1, function($vars) {
	$invoiceid = Capsule::table('tblinvoiceitems')
		->where('relid', '=', $vars['params']['serviceid'])
                ->where('type','Hosting')
		 ->orderBy('duedate', 'desc')
		->first();
	Capsule::table('tblinvoices')
		->where('id', '=', $invoiceid->invoiceid)
		->update(['Status' => 'Cancelled']);
});

Share this post


Link to post
Share on other sites

I'm guessing this would just go in /includes/hooks?

Share this post


Link to post
Share on other sites
4 minutes ago, MikeP said:

I'm guessing this would just go in /includes/hooks?

yes. 🙂

Share this post


Link to post
Share on other sites

Thanks for confirming.  Would it cancel the invoice if there was another product on the invoice as well?

Share this post


Link to post
Share on other sites
1 minute ago, MikeP said:

Thanks for confirming.  Would it cancel the invoice if there was another product on the invoice as well?

Oh. Great catch. It actually will - I've never had that in mind.

Share this post


Link to post
Share on other sites

There are various paid options available for this functionality but at least you've made the effort to provide this free of charge 🙂

I'd be interested if you were to add additional functionality, such as remove the product from the invoice and provide a log in the activity log.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated