//in controller
//1. delete items when updating
$prealertDetails=WarePrealertDetail::where('prealert_id',$input['id'])->select('id')->get()->toArray();
if(isset($prealertDetails)){
foreach($prealertDetails as $row){
if(isset($input['prealert_details_id']) && in_array($row['id'], $input['prealert_details_id'])){
}
else{
$input['deleted_id'][]=$row['id'];
}
}
}
if(isset($input['deleted_id'])){
foreach($input['deleted_id'] as $val){
WarePrealertDetail::where('id', $val)->delete();
//\App\Repositories\Warehouse\ProductTransactionRepository::delete($val,'prealertDetails');
}
}
//2. save each items
foreach(){
$prealertItems['quantity']=$item['quantity'];
$prealertItems['pending_quantity']=$prealertItems['quantity']-\App\Repositories\Warehouse\ReceiptRepository::receivedTotal($warePrealertDetail->id);
WarePrealertDetail::create($prealertItems)
}
//3. in repository
public static function receivedTotal($id){
$total=WareReceiptDetail::join('ware_prealert_details','ware_prealert_details.id','ware_receipt_details.prealert_details_id')
->join('ware_product_transactions.receipt_details_id','ware_receipt_details.id')
->where('ware_prealert_details.id',$id)
->sum('ware_receipt_details.received_quantity');
return $total;
}
2. Receiving
Screen
//in controller
$this->data['prealertDetails']=\App\Repositories\Warehouse\PrealertRepository::readyForReceiptItems($this->data['prealert_id']);
//in repository , get only pending items
public static function readyForReceiptItems($parentId){
$warePrealertDetail=WarePrealertDetail::join('ware_products','ware_products.id','ware_prealert_details.product_id')->leftJoin('ware_units','ware_units.id','ware_products.unit_id')
->select('ware_prealert_details.*','ware_units.title as unit_title')
->where('prealert_id',$parentId)
->where('ware_prealert_details.pending_quantity','>','0')
->get();
return $warePrealertDetail;
}