app/Http/Controllers/ItemRequestController.php
/*
     * Send request unit to district/formation level
     * Check unit level request by user_id and stage=(1-5)
     * status = 1
     */
Send request unit to district/formation level
Check unit level request by user_id and stage=(1-5)
status = 1
Send request unit to district / formation level Check unit level request by user_id and stage = ( 1 - 5 ) status = 1
public function requestUnitToQuarterMaster(Request $request){
        try{
            if(!$request->input('condemnation_id'))
                throw new Exception("Condemnation Id is required");
            $unitUser = $request->user(); 
            if(!$unitUser || !$unitUser->hasRole('unit'))
                throw new Exception("You need to logged in as unit level!");
            $terms = TermRelation::where(['user_id'=>$unitUser->id])->first();
            $unitBoss = TermRelation::retrieveUnitQuarterMaster($terms->quarter_master_id);
            $pendingRequest = KitItemRequest::where([
                    'condemnation_id'=> $request->input('condemnation_id'),
                    'unit_user_id'=>$unitUser->id,
                    'stage'=>2,
                    'status'=>1
                    ])->get();
            if(count($pendingRequest) == 0)
                throw new Exception("Unit have not any accepted request");
            $districtLevelRequest = [];
            $parentIds = '';
            $condemnation_id = 0;
            $requestItems = 0;
            foreach($pendingRequest as $key=>$pRequest){
                $districtLevelRequest[$key]['company_user_id'] = $pRequest->company_user_id;
                $districtLevelRequest[$key]['kit_items'] = \GuzzleHttp\json_decode($pRequest->kit_items);
                $parentIds = $parentIds == '' ? $pRequest->id : $parentIds.','.$pRequest->id;
                $pRequest->stage = 4;
                $condemnation_id = $pRequest->condemnation_id;
                $requestItems +=$pRequest->request_items;
                $pRequest->save();
            }
            $newRequest = new KitItemRequest();
            $newRequest->condemnation_id = $condemnation_id;
            $newRequest->unit_user_id = $unitUser->id;
            $newRequest->quarter_master_user_id = $unitBoss->user_id;
            $newRequest->stage = 1;
            $newRequest->status = 2;
            $newRequest->parent_ids = $parentIds;
            $newRequest->request_items = $requestItems;
            $newRequest->kit_items = \GuzzleHttp\json_encode($districtLevelRequest);
            $newRequest->save();
            $newRequest->qa_device_id = $unitBoss->device_id;
            return ['success'=> true, 'message'=>"Request send to quarter master!", 'data'=>$pendingRequest];
        }catch (Exception $e){
            return ['success'=>false, 'message'=>$e->getMessage()];
        }
    }
public function requestUnitToQuarterMaster ( Request $ request ) { try { if ( ! $ request -> input ( 'condemnation_id' ) ) throw new Exception ( " Condemnation Id is required " ) ; $ unitUser = $ request -> user ( ) ; if ( ! $ unitUser || ! $ unitUser -> hasRole ( 'unit' ) ) throw new Exception ( " You need to logged in as unit level! " ) ; $ terms = TermRelation :: where ( [ 'user_id' => $ unitUser -> id ] ) -> first ( ) ; $ unitBoss = TermRelation :: retrieveUnitQuarterMaster ( $ terms -> quarter_master_id ) ; $ pendingRequest = KitItemRequest :: where ( [ 'condemnation_id' => $ request -> input ( 'condemnation_id' ) , 'unit_user_id' => $ unitUser -> id , 'stage' => 2 , 'status' => 1 ] ) -> get ( ) ; if ( count ( $ pendingRequest ) == 0 ) throw new Exception ( " Unit have not any accepted request " ) ; $ districtLevelRequest = [ ] ; $ parentIds = '' ; $ condemnation_id = 0 ; $ requestItems = 0 ; foreach ( $ pendingRequest as $ key => $ pRequest ) { $ districtLevelRequest [ $ key ] [ 'company_user_id' ] = $ pRequest -> company_user_id ; $ districtLevelRequest [ $ key ] [ 'kit_items' ] = \ GuzzleHttp \ json_decode ( $ pRequest -> kit_items ) ; $ parentIds = $ parentIds == '' ? $ pRequest -> id : $ parentIds . ',' . $ pRequest -> id ; $ pRequest -> stage = 4 ; $ condemnation_id = $ pRequest -> condemnation_id ; $ requestItems += $ pRequest -> request_items ; $ pRequest -> save ( ) ; } $ newRequest = new KitItemRequest ( ) ; $ newRequest -> condemnation_id = $ condemnation_id ; $ newRequest -> unit_user_id = $ unitUser -> id ; $ newRequest -> quarter_master_user_id = $ unitBoss -> user_id ; $ newRequest -> stage = 1 ; $ newRequest -> status = 2 ; $ newRequest -> parent_ids = $ parentIds ; $ newRequest -> request_items = $ requestItems ; $ newRequest -> kit_items = \ GuzzleHttp \ json_encode ( $ districtLevelRequest ) ; $ newRequest -> save ( ) ; $ newRequest -> qa_device_id = $ unitBoss -> device_id ; return [ 'success' => true , 'message' => " Request send to quarter master! " , 'data' => $ pendingRequest ] ; } catch ( Exception $ e ) { return [ 'success' => false , 'message' => $ e -> getMessage ( ) ] ; } }
Send request unit to district/formation level
 Check unit level request by user_id and stage=(1-5)
 status = 1
Send request unit to district / formation level Check unit level request by user_id and stage = ( 1 - 5 ) status = 1
// Get from auth