Stock, Partial Receipt, Delivery, Item Return

  • Code

    1. Pre Order

    Screen

    Tables

    Form hidden fields for editing
    
                       html+= '<input type="hidden" name="prealert_details_id[]" value="'+row.id+'">';
                                  html+= '<input type="hidden" name="delivery_details_id[]" value="'+row.delivery_details_id+'">';
                                  html+= '<input type="hidden" name="receipt_details_id[]" value="'+(row.receipt_details_id!=null ? row.receipt_details_id : '')+'">';
    
                       
    Save Function
    
                      //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;
    
                        } 
    
                      
    form
    
                       html+= '<input type="hidden" name="prealert_details_id[]" value="'+row.id+'">';
                                  html+= '<input type="hidden" name="delivery_details_id[]" value="'+row.delivery_details_id+'">';
                                  html+= '<input type="hidden" name="receipt_details_id[]" value="'+(row.receipt_details_id!=null ? row.receipt_details_id : '')+'">';
    
                       
    Save function
    
    
                      //in controller
    
                      //1. delete items and stock
                        $receiptDetails=WareReceiptDetail::where('receipt_id',$input['id'])->select('id')->get()->toArray();
                        if(isset($receiptDetails)){
                                
                                foreach($receiptDetails as $row){
                                    
                                    if(isset($input['receipt_details_id']) && in_array($row['id'], $input['receipt_details_id'])){
    
                                    }
                                    else{
                                        $input['deleted_id'][]=$row['id'];
                                    }
    
                                }
    
                        }
    
                        if(isset($input['deleted_id'])){
                                foreach($input['deleted_id'] as $val){
                                    WareReceiptDetail::where('id', $val)->delete();
                                    \App\Repositories\Warehouse\ProductTransactionRepository::delete($val,'receiptDetails');
                                }
                        } 
    
    
    
                        //2. if prealert items , calculate pending quantity
                        if(isset($input['prealert_details_id'][$key]) && $input['prealert_details_id'][$key]>0){ 
                              
                              $receiptItems['prealert_received']=self::receivedTotal($input['prealert_details_id'][$key])+$receiptItems['received_quantity'];
                              if(isset($prealertDetail->quantity)){
                                  $receiptItems['pending_quantity']=$prealertDetail->quantity-$receiptItems['prealert_received'];
                              }                                               
                          
                          }   
    
    
                          //3. update receiptDetails
                          if(isset($input['receipt_details_id'][$key]) && $input['receipt_details_id'][$key]>0 && $input['receipt_details_id'][$key]!='undefined'){ 
                              $wareReceiptDetail=WareReceiptDetail::find($input['receipt_details_id'][$key]); 
                              if(isset($wareReceiptDetail)){
                                  $wareReceiptDetail->update($receiptItems);
                              }
                              // print_R( $wareReceiptDetail);exit;
                          }
                          else{                                       
                              $receiptItems['receipt_id']=$mainObject->id;                                        
                              $wareReceiptDetail=WareReceiptDetail::create($receiptItems);                                                
                          }
    
    
    
    
                          //4. update prealertDetails 
                          if(isset($input['prealert_details_id'][$key]) && $input['prealert_details_id'][$key]>0){ 
                                                    if(isset($prealertDetail)){
                                                        $prealertDetail->prealert_received=$receiptItems['prealert_received'];  
                                                        $prealertDetail->pending_quantity=$receiptItems['pending_quantity'];
                                                        $prealertDetail->save();
                                                    }                                                
                          }
    
    
                          //5. update stock
                          if(isset($input['updateStock']) && $input['updateStock']==1){ 
                                                    
                                                    \App\Repositories\Warehouse\ProductTransactionRepository::insert([
                                                        'identifier'=>'receiptDetails',
                                                        'warehouse_id'=>$receiptItems['warehouse_id'],
                                                        'receipt_details_id'=>$wareReceiptDetail->id,
                                                        'receipt_id'=>$mainObject->id,
                                                        'customer_id'=>$mainObject->customer_id,
                                                        'product_id'=>$wareReceiptDetail['product_id'],
                                                        'date'=>date('Y-m-d H:i:s'),
                                                        'inward'=>$receiptItems['received_quantity'],
                                                        'status'=>'verified',
                                                        'remarks'=>'Receipt'
                                                    ]);
                                
                                
                                                    \App\Models\Warehouse\WareProductTransactionHistory::create([
                                                        'identifier'=>'receiptDetails',
                                                        'warehouse_id'=>$receiptItems['warehouse_id'],
                                                        'parent_id'=>$mainObject->id,
                                                        'receipt_details_id'=>$wareReceiptDetail->id,
                                                        'customer_id'=>$mainObject->customer_id,
                                                        'product_id'=>$wareReceiptDetail['product_id'],
                                                        'date'=>date('Y-m-d H:i:s'),
                                                        'inward'=>$receiptItems['received_quantity'],
                                                        
                                                    ]);
                         }
    
    
    
    
                         //in repository 
    
                         public static function insert($data){
                                  if($data['identifier']=='receiptDetails'){
    
                                          $wreProductTransaction=WareProductTransaction::updateOrCreate([
                                                  'identifier' => $data['identifier'],
                                                  'receipt_details_id' => $data['receipt_details_id'],
                                                  'receipt_id'=>$data['receipt_id'],
                                          ], 
                                          $data );
    
                                  }
                                  else{
                                          $wreProductTransaction=WareProductTransaction::updateOrCreate([
                                                  'identifier' => $data['identifier'],
                                                  'delivery_details_id' => $data['delivery_details_id'],
                                                  'delivery_id'=>$data['delivery_id'],
                                          ], 
                                          $data );
                                  }
                                  
    
                                  return $wreProductTransaction;
                          } 
    
    
                          public static function delete($parent_id, $identifier){
                                  if($identifier=='receiptDetails'){
                                          WareProductTransaction::where('receipt_details_id',$parent_id)->where('identifier', $identifier)->delete();  
                                  }
                                  else{
                                        WareProductTransaction::where('delivery_details_id',$parent_id)->where('identifier', $identifier)->delete();  
                                  }
                                          
                          }