Very simple task. Getting multiple data (invoices) from an API.
foreach($items->data as $item) {
$invoice = Invoice::firstOrNew(array('invoice_number' => $item->DocumentNo));
$invoice->total = $item->GrandTotal;
$invoice->save();
foreach ($item->lines as $product) {
$productModel = Product::find(1)->where('unique_id', '=', $product->Product)->first();
if(isset($productModel)) {
$productToInvoice = ProductToInvoice::firstOrNew(array('product_id' => $productModel->product_id, 'invoice_id' => $invoice->id));
$productToInvoice->total = $product->LineNetAmt;
$productToInvoice->quantity = $product->QtyEntered;
$productToInvoice->price = $product->PriceEntered;
$productToInvoice->save();
}
$m++;
}
}
$item->lines are the products inside the invoice.
When the invoice doesn't exist, everything seems to be ok. Product lines are added correctly. When the invoice exists (and also the product lines): it updates the lines but total, quantity and price are the same for all lines.
What exactly am I doing wrong here?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire