I'm trying to integrate xero in my application but facing the issue while updating an invoice line item. I need to update discounts on the line item but when I do this I'm getting an error saying "You cannot have multiple line items with the same LineItemID".
Following is my code:
$invoice = $xero->loadByGUID(\XeroPHP\Models\Accounting\Invoice::class,'b9e11c71-9983-44e5-b39c-4bbedda054eb');
$contact = new \XeroPHP\Models\Accounting\Contact;
$contact->setContactId('60dbd74c-4625-4df3-8786-e641346b2070');
$lineitem = new \XeroPHP\Models\Accounting\Invoice\LineItem;
$lineitem
->setDescription('ads')
->setDiscountRate('10')
->setLineItemID('d837c383-5892-4b49-afe9-13b8257c98e3');
$invoice->setDueDate(new DateTime(date('Y-m-d')))
->setDate(new DateTime(date('Y-m-d')))
->setReference('some reference')
->setContact($contact)
->addLineItem($lineitem);
$xero->save($invoice);
I've found the solution (rectification). Just wanted to share in case someone else is looking for in the future or got stuck in a similar case.
I was adding ->addLineItem($lineitem);
which was adding an extra line item in my invoice having the same LineItemId in the below code:
$invoice->setDueDate(new DateTime(date('Y-m-d')))
->setDate(new DateTime(date('Y-m-d')))
->setReference('some reference')
->setContact($contact)
->addLineItem($lineitem);
$xero->save($invoice);
To modify a line item fetch the old line item and add/update the values in it.
In my case I did something like this: $invoice->getLineitems();
to fetch the old line items.