We use the invoiceninja API to automatically create invoices and clients who buy our online service. The problem we are noticing now is that whenever a client uses a different email – but everything else is the same – invoiceninja will create a new client. Has anyone had any similar problems and how did you solve them?
The problem is there is no native way of querying Clients other than by email address, and that doing direct comparisons of collections like this is very inefficient, because you have no defined keys you’re looking for.
There’s 2 ways you could go about this: the first is that you would need to query the clients table using SQL, where you could check if there are any clients that exist where name = the client’s name, address1 = the client’s address, etc, and if so, then simply use that record’s ID for further invoices. If you have a lot of clients, this is going to be very inefficient.
The second way, which is preferred (if at all possible), is to have whatever ordering system you use keep a history of a user’s email addresses, and simply query the API for each of them until the oldest one is found, and use that record’s ID. Though I concede, This would be more difficult with a system like WooCommerce.
The third way is to extend InvoiceNinja’s core code, but this is unmaintainable and bad practise, IMO. Eloquent has beautiful query builders that would let you do something like Client::where([['name', $request->input('name')], ['address1', $request->input('address1')]])->first(); (part of the reason I absolutely love that InvoiceNinja uses Laravel), but whenever an update comes out, you’d need to re-add your custom API mods.