Jump to content

sorting within invoices on admin view


son

Recommended Posts

The default sorting of invoices within /admin/invoices.php is by due date, which is confusing for my business. Is there a way to change the default sorting on page load to invoice #?

There's also a distinction between visible invoice number and invoice $_GET['id'] which WHMCS doesn't seem to understand - I'm definitely trying to sort by visible invoice number only.

Thank you!

Link to comment
Share on other sites

Don't think there is a built-in option for the default sort.  Perhaps a javascript in an admin output hook that does the sorting call on page load would do the trick. 

The ID in the $_GET['id'] is the database id, where the visible ID is the one that can be reset and formatted for sequential invoices.

Link to comment
Share on other sites

18 hours ago, son said:

The default sorting of invoices within /admin/invoices.php is by due date, which is confusing for my business. Is there a way to change the default sorting on page load to invoice #? 

Doable with an action hook.

18 hours ago, son said:

There's also a distinction between visible invoice number and invoice $_GET['id'] which WHMCS doesn't seem to understand - I'm definitely trying to sort by visible invoice number only. 

Not possible, you'll need to create a custom page to get this result.

Link to comment
Share on other sites

20 hours ago, son said:

The default sorting of invoices within /admin/invoices.php is by due date, which is confusing for my business. Is there a way to change the default sorting on page load to invoice #?

another way would be to change the link in the admin menu to invoices.php?orderby=id - though i'd agree with Kian that hooking would be a better long-term solution.

20 hours ago, son said:

There's also a distinction between visible invoice number and invoice $_GET['id'] which WHMCS doesn't seem to understand - I'm definitely trying to sort by visible invoice number only.

if sorting by custom invoice number format (and not it's ID), then the above should work... if you were wanting to order by invoice ID using a similar method, best you could do would be to sort by invoice date (orderby=date).

Link to comment
Share on other sites

I got it to this point so far:

function admin_sort_invoices_by_id($vars){

	$filename = $vars['filename'];
    $filter = $_GET['filter'];
    $action = $_GET['action'];
	if (($filter == "") && ($action == "") && ($filename == "invoices")) {
		$url = 'invoices.php?orderby=id';
		header( 'Location: '.$url );
	}
}
add_hook('AdminAreaPage', 1, "admin_sort_invoices_by_id");

Is there an asc/desc option that I'm missing?

https://developers.whmcs.com/api-reference/getinvoices/ implies that adding &order=desc would work but I guess invoices.php is not relying on the API.

Link to comment
Share on other sites

On 26/04/2019 at 17:24, son said:

Is there an asc/desc option that I'm missing?

not that i'm aware of (Kian might know different!) - orderby acts as a toggle... which is one of the reasons why I suggested putting the link in the menu (in case you need to press it twice to get the order you ideally want).

On 26/04/2019 at 17:24, son said:

implies that adding &order=desc would work but I guess invoices.php is not relying on the API.

it doesn't. 🙂

Link to comment
Share on other sites

The guy responsible for column sorting is $_COOKIE['WHMCSSD'] that is a base64 encoded string of a json encoded object. In other words if you place this code inside your hook...

<?

echo '<pre>';
print_r(json_decode(base64_decode($_COOKIE['WHMCSSD']), true));
echo '</pre>';
die();

You'll get the following response:

Array
(
    [invoices] => Array
        (
            [orderby] => id
            [sort] => DESC
        )

)

I'm converting the object to an array just because I like it more. Anyway as soon as you open invoice.php this cookie is empty therefore the table assumes that you want to use the default sorting (Due Date DESC). Since you want to sort by ID DESC we could simply define this cookie exactly as we want.

<?

function admin_sort_invoices_by_id($vars)
{
	// If current page is invoices and the cookie is still empty we define it
	if ($vars['filename'] == 'invoices' AND !$_COOKIE['WHMCSSD'])
	{
		setcookie('WHMCSSD', base64_encode(json_encode(array('invoices' => array('orderby' => 'id', 'sort' => 'DESC')))), time() + (86400 * 30), '/');
		header('Location: invoices.php');
		die();
	}
}

add_hook('AdminAreaPage', 1, "admin_sort_invoices_by_id");

You can normally sort by other columns ASC/DESC. The only difference is that you are overriding default sorting. I tested it on one of my WHMCS and it works but I do a lot of mistakes so double-check it 😀

Edited by Kian
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