app/Http/Controllers/ItemRequestController.php
/*
     * Send request to the unit/district level
     * Collect all request with their item types
     * Make a json stringify object
     * Create a new entry to the kit_item_request
     *
     * @fields(company_id, user_id, condemnation_id)
     */
Send request to the unit/district level
Collect all request with their item types
Make a json stringify object
Create a new entry to the kit_item_request


Send request to the unit / district level Collect all request with their item types Make a json stringify object Create a new entry to the kit_item_request
public function requestCompanyToUnit(Request $request){
        try{
            $companyUser = $request->user();
            if(!$companyUser || !$companyUser->hasRole('company'))
                throw new Exception("Must be send by company");
            $validator = Validator::make($request->all(), [
                'condemnation_id'=>'required'
            ]);
            if( $validator->fails()){
                $validatorErrors = [];
                foreach($validator->messages()->getMessages() as $fieldName => $messages) {
                    foreach( $messages as $message){
                        $validatorErrors[$fieldName] = $message;
                    }
                }
                throw new Exception(implode(' ',$validatorErrors));
            }
            $activeCondemnation = Condemnation::where(['id'=>$request->input('condemnation_id'),'status'=>0]);
            if(!$activeCondemnation)
                throw new Exception("Sorry condemnation not found!");
            $company_term = TermRelation::where(['user_id'=>$companyUser->id,'role'=>4, 'term_type'=>0])->first();
            $allPendingRequest = SolderItemRequest::where([
                'company_id'=>$company_term->company_id,
                'status'=>2
            ])->get();
            if(count($allPendingRequest) === 0 )
                throw new Exception("Pending request not found!");
            $requestJsonData = array();
            $requestItems = 0;
            $collectedKitTypes = [];
            $mainCollection =[];
            foreach( $allPendingRequest as $key=>$pendingRequest ){
                $kitItem = SolderKits::find($pendingRequest->solder_kit_id);
                if(isset($requestJsonData['kit_ids'])){
                    $requestJsonData['kit_ids'] = $requestJsonData['kit_ids'].','.$kitItem->item_id;
                }else {
                    $requestJsonData['kit_ids'] = (string)$kitItem->item_id;
                }
                $item_types = ItemType::find($kitItem->item_type_id);
                if(isset($collectedKitTypes[$item_types->type_name])){
                    $collectedKitTypes[$item_types->type_name]['items'] +=1;
                }else {
                    $collectedKitTypes[$item_types->type_name]['items'] = 1;
                    $collectedKitTypes[$item_types->type_name]['type_id'] = $kitItem->item_type_id;
                }
                $requestItems += 1;
                $pendingRequest->status = 2;
                $pendingRequest->save();
            }
            if(count($collectedKitTypes)>0){
                foreach($collectedKitTypes as $type_name=>$kitType ){
                    array_push($mainCollection, ['type_name'=>$type_name,'quantity'=>$kitType['items'], 'type_id'=>$kitType['type_id']]);
                }
            }
            $companyUnitUser = TermRelation::getCompanyUnitUser($company_term->unit_id);
            if( count($requestJsonData) > 0 ) {
                $requestJsonData = (object) $requestJsonData;
                $requestJsonData->kit_types = $mainCollection;
                $unitRequest = KitItemRequest::where([
                    'condemnation_id'=>$request->input('condemnation_id'),
                    'company_user_id'=>$companyUser->id,
                    'stage'=>1,
                    'status'=> 1 
                ])->first();
                if(!$unitRequest )
                    $unitRequest = new KitItemRequest();
                $unitRequest->stage = 1;
                $unitRequest->status = 1; 
                $unitRequest->request_items = $requestItems;
                $unitRequest->kit_items = \GuzzleHttp\json_encode($requestJsonData);
                $unitRequest->unit_user_id = $companyUnitUser->user_id;
                $unitRequest->company_user_id = $companyUser->id;
                $unitRequest->condemnation_id = $request->input('condemnation_id');
                $unitRequest->save();
            }else {
                throw new Exception("Sorry request item not found!");
            }
            return ['success'=>true, 'message'=>"Request send to unit level!", 'data'=>$company_term ];
        }catch (Exception $e){
            return ['success'=>false, 'message'=>$e->getMessage()];
        }
    }
public function requestCompanyToUnit ( Request $ request ) { try { $ companyUser = $ request -> user ( ) ; if ( ! $ companyUser || ! $ companyUser -> hasRole ( 'company' ) ) throw new Exception ( " Must be send by company " ) ; $ validator = Validator :: make ( $ request -> all ( ) , [ 'condemnation_id' => 'required' ] ) ; if ( $ validator -> fails ( ) ) { $ validatorErrors = [ ] ; foreach ( $ validator -> messages ( ) -> getMessages ( ) as $ fieldName => $ messages ) { foreach ( $ messages as $ message ) { $ validatorErrors [ $ fieldName ] = $ message ; } } throw new Exception ( implode ( ' ' , $ validatorErrors ) ) ; } $ activeCondemnation = Condemnation :: where ( [ 'id' => $ request -> input ( 'condemnation_id' ) , 'status' => 0 ] ) ; if ( ! $ activeCondemnation ) throw new Exception ( " Sorry condemnation not found! " ) ; $ company_term = TermRelation :: where ( [ 'user_id' => $ companyUser -> id , 'role' => 4 , 'term_type' => 0 ] ) -> first ( ) ; $ allPendingRequest = SolderItemRequest :: where ( [ 'company_id' => $ company_term -> company_id , 'status' => 2 ] ) -> get ( ) ; if ( count ( $ allPendingRequest ) === 0 ) throw new Exception ( " Pending request not found! " ) ; $ requestJsonData = array ( ) ; $ requestItems = 0 ; $ collectedKitTypes = [ ] ; $ mainCollection = [ ] ; foreach ( $ allPendingRequest as $ key => $ pendingRequest ) { $ kitItem = SolderKits :: find ( $ pendingRequest -> solder_kit_id ) ; if ( isset ( $ requestJsonData [ 'kit_ids' ] ) ) { $ requestJsonData [ 'kit_ids' ] = $ requestJsonData [ 'kit_ids' ] . ',' . $ kitItem -> item_id ; } else { $ requestJsonData [ 'kit_ids' ] = ( string ) $ kitItem -> item_id ; } $ item_types = ItemType :: find ( $ kitItem -> item_type_id ) ; if ( isset ( $ collectedKitTypes [ $ item_types -> type_name ] ) ) { $ collectedKitTypes [ $ item_types -> type_name ] [ 'items' ] += 1 ; } else { $ collectedKitTypes [ $ item_types -> type_name ] [ 'items' ] = 1 ; $ collectedKitTypes [ $ item_types -> type_name ] [ 'type_id' ] = $ kitItem -> item_type_id ; } $ requestItems += 1 ; $ pendingRequest -> status = 2 ; $ pendingRequest -> save ( ) ; } if ( count ( $ collectedKitTypes ) > 0 ) { foreach ( $ collectedKitTypes as $ type_name => $ kitType ) { array_push ( $ mainCollection , [ 'type_name' => $ type_name , 'quantity' => $ kitType [ 'items' ] , 'type_id' => $ kitType [ 'type_id' ] ] ) ; } } $ companyUnitUser = TermRelation :: getCompanyUnitUser ( $ company_term -> unit_id ) ; if ( count ( $ requestJsonData ) > 0 ) { $ requestJsonData = ( object ) $ requestJsonData ; $ requestJsonData -> kit_types = $ mainCollection ; $ unitRequest = KitItemRequest :: where ( [ 'condemnation_id' => $ request -> input ( 'condemnation_id' ) , 'company_user_id' => $ companyUser -> id , 'stage' => 1 , 'status' => 1 ] ) -> first ( ) ; if ( ! $ unitRequest ) $ unitRequest = new KitItemRequest ( ) ; $ unitRequest -> stage = 1 ; $ unitRequest -> status = 1 ; $ unitRequest -> request_items = $ requestItems ; $ unitRequest -> kit_items = \ GuzzleHttp \ json_encode ( $ requestJsonData ) ; $ unitRequest -> unit_user_id = $ companyUnitUser -> user_id ; $ unitRequest -> company_user_id = $ companyUser -> id ; $ unitRequest -> condemnation_id = $ request -> input ( 'condemnation_id' ) ; $ unitRequest -> save ( ) ; } else { throw new Exception ( " Sorry request item not found! " ) ; } return [ 'success' => true , 'message' => " Request send to unit level! " , 'data' => $ company_term ] ; } catch ( Exception $ e ) { return [ 'success' => false , 'message' => $ e -> getMessage ( ) ] ; } }
Send request to the unit/district level
 Collect all request with their item types
 Make a json stringify object
 Create a new entry to the kit_item_request
Send request to the unit / district level Collect all request with their item types Make a json stringify object Create a new entry to the kit_item_request
// Collect all request based on company id // Get all solder request that approve by company // Get company term // Get request item // Collect item Ids // Get kit types // Check if it has already a request // unit level // For unit //Send push notification to unit level