private function addorupdatePurchaseLineItems($order, $postedData, &$rolproducts){ $this->deletePurchaselitemsByPurchase($order->id); $totalamount = 0; $totaltaxamount = 0; foreach($postedData->purchaseproducts as $ordprod){ $qnty = $ordprod->quantity; $vldqnty = (is_numeric($qnty) && $qnty > 0); if(!$vldqnty){ continue; } $prd = \Product::find($ordprod->product_id); if(!$prd){ continue; } if($prd->rol >= ($prd->stock + $qnty)){ $rolproducts[] = $prd->name; } $unit_cp = $ordprod->unit_cp; $amount = $qnty * $unit_cp; $taxamount = $amount * $prd->taxper / 100 ; $attributes = array( "purchase_id" => $order->id, "product_id" => $ordprod->product_id, "quantity" => $ordprod->quantity, "unit_cp" => $unit_cp, "amount" => $amount, "tax" => $taxamount, ); $data = \Purchaseproduct::create($attributes); $totalamount += $amount; $totaltaxamount += $taxamount; if($this->enablestock){ $newstock= $prd->stock + $qnty; $attributes = array( "stock" => $newstock, "stockvalue" => $newstock * $unit_cp); $prd->update_attributes($attributes); } } //updating order amounts with lineitems $taxper = $totaltaxamount / $amount * 100.0;//average tax % on this order $this->updateAmntTaxInPurchase($order, $totalamount, $taxper, $totaltaxamount); }