Jump to content

Curious problem with GetInvoices API with response totals


TBroMEM

Recommended Posts

I am using the following localAPI request in a hook (excerpt) to determine number of invoices matching the request (e.g. # unpaid for a defined client).

$clientid = $vars['params']['userid']; // triggered from premodulerenew hook
$invoicecount = 1;
$command = 'GetInvoices';
$values = array(
	'userid' => $clientid,
	'status' => 'Unpaid'
);
				
$getClientInvoices = localAPI($command, $postData);
if ($getClientInvoices['result'] == 'success') {
	$invoicecount =  $getClientInvoices['numreturned'];
}

if ($invoicecount >= 1) {
	logActivity('Service renewal will be aborted for client ' . $clientid . '. It is not qualified because it has ' . $invoicecount . ' unpaid invoices - by hook_send_renewal_notice_toadmin...');
}

The $clientid listed in the logactivity matches the clientid passed to the LocalAPI ('userid' => $clientid), but it first returned over a 1103 as totalresult in the response. So, I thought I would switch to numreturned in the response, and it went to 25 (which is default for number to return)

1103 from $invoicecount from response.totalresults
Service renewal will be aborted for client 585. It is not qualified because it has 1103 unpaid invoices - by hook_send_renewal_notice_toadmin...

25 from $invoicecount using response.numreturned
Service renewal will be aborted for client 585. It is not qualified because it has 25 unpaid invoices - by hook_send_renewal_notice_toadmin...

585 is the correct clientid as reported in the logactivity so how in the world is it reporting 1103 and 25? There only 2 invoices recorded for the client and only 1 is unpaid.

Edited by TBroMEM
minor code revision
Link to comment
Share on other sites

your API call isn’t using the filter array you think it is, and you’re reading the wrong field from the response.
 

1103 = totalresults for all unpaid invoices (because filters weren’t applied). 25 = numreturned default page size, with no limitnum specified.

 

$clientid = (int) $vars['params']['userid'];

$command  = 'GetInvoices';
$postData = array(
    'userid'     => $clientid,
    'status'     => 'Unpaid',
    'limitstart' => 0,
    'limitnum'   => 3000,
);

$getClientInvoices = localAPI($command, $postData); // , $adminuser if needed

$invoicecount = 0;

if ($getClientInvoices['result'] === 'success') {
    $invoicecount = (int) $getClientInvoices['totalresults'];
}

if ($invoicecount >= 1) {
    logActivity(
        'Service renewal will be aborted for client ' . $clientid .
        '. It is not qualified because it has ' . $invoicecount .
        ' unpaid invoices - by hook_send_renewal_notice_toadmin...'
    );
}

Link to comment
Share on other sites

It is acting like it is not recognizing the client id and returning for all client invoices matching unpaid. not sure why.

Will be declaring $clientid as int and then passing it in. We'll see.

$client = $vars['params']['userid']; // triggered from premodulerenew hook
$clientid = (int) $client;

The module params documentation indicates userid is Id from  tblclients.id, which indicates in schema it is already an int.

The curious thing is, I pulled same code from another hook that used same localAPI for GetInvoices and it had no issues at all.

Edited by TBroMEM
additional info
Link to comment
Share on other sites

@towhiddex The one thing you missed is that I did pass in the $clientid (params userid assignment). The client in question had no Unpaid invoices (as defined in the postdata array. When I tested similar using postman request to our production (there are limits in our dev) it returned anticipated results of 0 for totalresults and numreturned which is what I am anticipating. I am not concerned about limitnum because the default return of 25 would be fine. All I need to evaluate is whether it is 0 or >=1.

Link to comment
Share on other sites

Here are some screenshots.

One shows view of client record invoices (there are only two invoices, and none are unpaid)

The other reflects what was captured from the declared/captured values (25 for num of invoices is from numreturned).

 

Screenshot 2025-12-05 164423.png

Screenshot 2025-12-05 164204.png

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