TBroMEM Posted Friday at 03:52 PM Share Posted Friday at 03:52 PM (edited) 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 Friday at 03:54 PM by TBroMEM minor code revision 0 Quote Link to comment Share on other sites More sharing options...
Solution towhiddex Posted Friday at 04:04 PM Solution Share Posted Friday at 04:04 PM 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...' ); } 1 Quote Link to comment Share on other sites More sharing options...
TBroMEM Posted Friday at 04:53 PM Author Share Posted Friday at 04:53 PM (edited) 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 Friday at 05:06 PM by TBroMEM additional info 0 Quote Link to comment Share on other sites More sharing options...
TBroMEM Posted Friday at 08:49 PM Author Share Posted Friday at 08:49 PM Even with setting the $clientid as integer and using that in the API request, it still acts like no clientid reference is supplied. What am I missing. 0 Quote Link to comment Share on other sites More sharing options...
TBroMEM Posted Friday at 10:35 PM Author Share Posted Friday at 10:35 PM @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. 0 Quote Link to comment Share on other sites More sharing options...
TBroMEM Posted Friday at 10:48 PM Author Share Posted Friday at 10:48 PM 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). 0 Quote Link to comment Share on other sites More sharing options...
TBroMEM Posted 8 hours ago Author Share Posted 8 hours ago @towhiddex was correct. I did not initially catch the fact he was talking about where I referenced $values for the postdata but used $postData with the api request. I have resolved. Correct code as he called out: $clientid = (int) $vars['params']['userid']; $command = 'GetInvoices'; $postData = array( // instead of $values 'userid' => $clientid, 'status' => 'Unpaid', 'limitstart' => 0, 'limitnum' => 3000, ); 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.