Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SalesInvoiceDetails from draft SalesInvoice #226

Open
davidspeijer opened this issue Feb 5, 2021 · 0 comments
Open

Remove SalesInvoiceDetails from draft SalesInvoice #226

davidspeijer opened this issue Feb 5, 2021 · 0 comments

Comments

@davidspeijer
Copy link

As I have spend some time to figure this out I want to share this with the rest of the world;

In my app I create SalesInvoices for a service provided. If a new service is provided on the same day I only want to apply the travel expense once on the SalesInvoice (at the last row of the SalesInvoice).

To accompolish this I created a new $salesInvoiceDetailsArray[] with

  • Old SalesInvoiceDetail that need to be removed $detail->_destroy = true
  • New lines

Thereafter I add it to $salesInvoice->details.

Perhaps it would be an idea to highlight the _destroy attribute in the docs?

private function updateSalesInvoice(SalesInvoice $salesInvoice, $newSalesInvoiceDetails)
{
    $newPeriod = $newSalesInvoiceDetails[0]->period;
    $salesInvoiceDetailsArray = [];

    /**
     * Check old invoice and set _destroy true for lines that match same period and are travel expenses.
     */
    foreach ($salesInvoice->details as $detail) {
        if (($detail->period == $newPeriod) and ($detail->product_id == XXXX)) {
            $detail->_destroy = true;
            $salesInvoiceDetailsArray[] = $detail;
        }
    }

    /**
     * Add the new lines
     */
    foreach ($newSalesInvoiceDetails as $newSalesInvoiceDetail) {
        $salesInvoiceDetailsArray[] = $newSalesInvoiceDetail;
    }

    $salesInvoice->details = $salesInvoiceDetailsArray;

    $salesInvoice->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant