Jump to content

Recalculate invoice when adding a line item via model


adamlundrigan

Recommended Posts

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

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

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?

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • 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