app/Http/Controllers/ItemRequestController.php
/*
    * Send request district to central level
    * Check district level request by user_id and stage=(1-5)
    * status = 2
    */
Send request district to central level
Check district level request by user_id and stage=(1-5)
status = 2
Send request district to central level Check district level request by user_id and stage = ( 1 - 5 ) status = 2
public function requestDistrictToCentral(Request $request){
        try{
            $formationUser = $request->user(); 
            if(!$formationUser || !$formationUser->hasRole('formation'))
                throw new Exception("You need to logged in as formation level!");
            $terms = TermRelation::where(['user_id'=>$formationUser->id])->first();
            $districtBoss = TermRelation::retrieveDistrictCentral($terms->central_office_id);
            $pendingRequest = KitItemRequest::where([
                'district_user_id'=>$formationUser->id,
                'stage'=>2,
                'status'=>3
            ])->get();
            if(count($pendingRequest) == 0)
                throw new Exception("Formation have not any accepted request");
            $districtLevelRequest = [];
            $parentIds = '';
            $condemnation_id = 0;
            $requestItems = 0;
            foreach($pendingRequest as $key=>$pRequest){
                $districtLevelRequest[$key]['quarter_master_user_id'] = $pRequest->quarter_master_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->district_user_id = $formationUser->id;
            $newRequest->central_user_id = $districtBoss->user_id;
            $newRequest->stage = 1;
            $newRequest->status = 4;
            $newRequest->parent_ids = $parentIds;
            $newRequest->request_items = $requestItems;
            $newRequest->kit_items = \GuzzleHttp\json_encode($districtLevelRequest);
            $newRequest->save();
            return ['success'=> true, 'message'=>"Request send to central level!", 'data'=>$pendingRequest];
        }catch (Exception $e){
            return ['success'=>false, 'message'=>$e->getMessage()];
        }
    }
public function requestDistrictToCentral ( Request $ request ) { try { $ formationUser = $ request -> user ( ) ; if ( ! $ formationUser || ! $ formationUser -> hasRole ( 'formation' ) ) throw new Exception ( " You need to logged in as formation level! " ) ; $ terms = TermRelation :: where ( [ 'user_id' => $ formationUser -> id ] ) -> first ( ) ; $ districtBoss = TermRelation :: retrieveDistrictCentral ( $ terms -> central_office_id ) ; $ pendingRequest = KitItemRequest :: where ( [ 'district_user_id' => $ formationUser -> id , 'stage' => 2 , 'status' => 3 ] ) -> get ( ) ; if ( count ( $ pendingRequest ) == 0 ) throw new Exception ( " Formation have not any accepted request " ) ; $ districtLevelRequest = [ ] ; $ parentIds = '' ; $ condemnation_id = 0 ; $ requestItems = 0 ; foreach ( $ pendingRequest as $ key => $ pRequest ) { $ districtLevelRequest [ $ key ] [ 'quarter_master_user_id' ] = $ pRequest -> quarter_master_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 -> district_user_id = $ formationUser -> id ; $ newRequest -> central_user_id = $ districtBoss -> user_id ; $ newRequest -> stage = 1 ; $ newRequest -> status = 4 ; $ newRequest -> parent_ids = $ parentIds ; $ newRequest -> request_items = $ requestItems ; $ newRequest -> kit_items = \ GuzzleHttp \ json_encode ( $ districtLevelRequest ) ; $ newRequest -> save ( ) ; return [ 'success' => true , 'message' => " Request send to central level! " , 'data' => $ pendingRequest ] ; } catch ( Exception $ e ) { return [ 'success' => false , 'message' => $ e -> getMessage ( ) ] ; } }
Send request district to central level
 Check district level request by user_id and stage=(1-5)
 status = 2
Send request district to central level Check district level request by user_id and stage = ( 1 - 5 ) status = 2
// Get from auth