adamlundrigan Posted January 28, 2018 Share Posted January 28, 2018 When adding or removing line items through the WHMCS admin panel the subtotal and total are automatically adjusted. Is there a way to trigger that same recalculation when adding a new line item via the Invoice model? Here's an example of what I tried: $invoice = Invoice::find(1); $item = new Item(); // set some fields $item->save(); $invoice->items->add($item); $invoice->save(); ...but the total isn't updated so in this case I would have to update $invoice->subtotal and $invoice->total manually. I'd rather not re-implement that logic (especially if the invoice is taxable) Link to comment Share on other sites More sharing options...
sentq Posted January 29, 2018 Share Posted January 29, 2018 tried to use API:UpdateInvoice ? Link to comment Share on other sites More sharing options...
adamlundrigan Posted January 29, 2018 Author Share Posted January 29, 2018 How did I miss that in the API docs Thanks! Link to comment Share on other sites More sharing options...
adamlundrigan Posted January 29, 2018 Author Share Posted January 29, 2018 UpdateInvoice only allows setting the description, amount and taxed fields but I needed to set the related entity ID as well so I ended up doing this: $invoice = Invoice::find(1); $item = new Item(); // set some fields $item->save(); $invoice->items->add($item); $invoice->save(); localAPI('UpdateInvoice', ['invoiceid' => $invoice->id]); Link to comment Share on other sites More sharing options...
sentq Posted January 29, 2018 Share Posted January 29, 2018 5 hours ago, adamlundrigan said: UpdateInvoice only allows setting the description, amount and taxed fields but I needed to set the related entity ID as well so I ended up doing this: $invoice = Invoice::find(1); $item = new Item(); // set some fields $item->save(); $invoice->items->add($item); $invoice->save(); localAPI('UpdateInvoice', ['invoiceid' => $invoice->id]); What I meant is that you can call the API:UpdateInvoice after adding your items, just to recalculate the invoice total not to add these items through it, the same as you did in the last code I quoted above. so my question is, did you try it already, if yes did it work or you still need help? 1 Link to comment Share on other sites More sharing options...
adamlundrigan Posted January 30, 2018 Author Share Posted January 30, 2018 I've implemented the code I posted and it does work. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts