controller

mail@pastecode.io avatar
unknown
plain_text
2 years ago
15 kB
2
Indexable
Never
<?php

namespace App\Http\Controllers;

use App\positions;
use App\UoReport;
use App\EoReport;
use App\LjReport;
use App\PbReport;
use App\CtReport;
use App\LjVsAgrupReport;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class PositionsController extends Controller {

     /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()  {
      $this->middleware('auth');
    }

  public function import(Request $request)   {
        //dd($request->request);
      $totalRows = 0;
    switch ($request->typeBulkLoad) {
        case 'Uo_Report':
            UoReport::truncate();
            $totalRows = $this->saveUOReport(request()->file('file')->path());
        break;
        case 'Eo_Report':
            EoReport::truncate();
            $totalRows = $this->saveEOReport(request()->file('file')->path());
        break;
        case 'Lj_Report':
            LjReport::truncate();
            $totalRows = $this->saveLjReport(request()->file('file')->path());
        break;
        case 'Pb_Report':
            PbReport::truncate();
            $totalRows = $this->savePbReport(request()->file('file')->path());
        break;
        case 'Ct_Report':
            CtReport::truncate();
            $totalRows = $this->saveCtReport(request()->file('file')->path());
        break;
        case 'LjVsAgrup_Report':
            LjVsAgrupReport::truncate();
            $totalRows = $this->saveLjVsAgrupReport(request()->file('file')->path());
        break;
    }
    return back()->with('status', $request->typeBulkLoad.' '.$totalRows.' registros guardados correctamente.');
  }
  public function jsonPositions(positions $positions) {
    $data = $positions::paginate(5);
    dd($data);
    $collection = Collection::make($data);
    foreach ($collection->toArray() as $value) {
      foreach ($value as $k => $v) {
        dd($k);
      }
    }
    return response()->json([
      "success" => true,
      "data" => $positions::paginate(5)
    ]);
  }

  public function index()  {
    
    $reports = array(
      '' => '[Seleccione un archivo]',
      'Uo_Report' => 'Uo Report',
      'Eo_Report' => 'Eo Report',
      'Lj_Report' => 'Local Job Report',
      'Pb_Report' => 'Position Blocker Report',
      'Ct_Report' => 'Cost Type Report',
      'LjVsAgrup_Report' => 'Agrupadores vs local job Report'
    );  
      
    return view('bulkLoad', compact('reports'));
  }
  
  private function saveUOReport($filePath){
      $line = 0;
      $myfile = fopen($filePath, "r");     
      $data_row = array();
      $cont = 0;
      while (($data = fgetcsv($myfile, ",")) == true) 
      {
          $data = array_map("utf8_encode", $data);
          $line++;
          $cont++;
          if( $line == 1 ){
            continue;
          }
          $data_row[] =  array(
                'id' => trim($data[0]),
                'Object_abbreviation' => trim($data[1]),
                'Object_name' => trim($data[2]),
                'UO_donde_vive' => trim($data[3]),
                'CeCo' => trim($data[4]),
                'ABI_Entity' => trim($data[5]),
                'Macro_Entity' => trim($data[6]),
                'Local_Entity' => trim($data[7]),
                'Company_Code_id' => trim($data[8]),
                'Company_Code_name' => trim($data[9]),
                'Personnel_subarea_id' => trim($data[10]),
                'Personnel_subarea_name' => trim($data[11]),
                'Building' => trim($data[12]),
                'Building_Name' => trim($data[13]),
                'id_Jefe' => trim($data[14]),
                'start_date' => trim($data[15]),
                'end_date' => trim($data[16]),
                'user_name' => trim($data[17]),
                'created_at' => date("Y-m-d H:i:s"),
                'updated_at' => date("Y-m-d H:i:s")
          );
          if($cont == 1000){
            UoReport::insert($data_row);
            $data_row = array();
            $cont = 0;
         }
      }
      if( count($data_row) > 0 ){
        UoReport::insert($data_row);
        $data_row = array();
      }
      fclose($myfile);
      return $line;
  }
  
  private function saveEOReport($filePath){
      $line = 0;
      $myfile = fopen($filePath, "r");     
      $data_row = array();
      $cont = 0;
      
      while (($data = fgetcsv($myfile, ",")) == true) 
      {
          $data = array_map("utf8_encode", $data);
          $line++;
          $cont++;
          if( $line == 1 ){
            continue;
          }
          
          $data_row[] =  array(
                'id' => trim($data[0]),
                'Abrev_Position'  => trim($data[1]) ,
                'POSITION'  => trim($data[2]) ,
                'Start_date'  => trim($data[3]) ,
                'End_Date'  => trim($data[4]) ,
                'Line_Manager'  => trim($data[5]) ,
                'ID_LOCAL_JOB'  => trim($data[6]) ,
                'Abrev_LJ'  => trim($data[7]) ,
                'Local_Job'  => trim($data[8]) ,
                'ID_UO'  => trim($data[9]) ,
                'UO'  => trim($data[10]) ,
                'ID_CeCo'  => trim($data[11]) ,
                'ID_Global_Job'  => trim($data[12]) ,
                'Global_Job'  => trim($data[13]) ,
                'ID_EMP'  => trim($data[14]) ,
                'Name'  => trim($data[15]) ,
                'ID_Analysis_Block'  => trim($data[16]) ,
                'Analysis_Block'  => trim($data[17]) ,
                'PS/PayGradeType'  => trim($data[18]) ,
                'PS/Pay_grade_area'  => trim($data[19]) ,
                'Band'  => trim($data[20]) ,
                'Highest_eligible_gro'  => trim($data[21]) ,
                'Lowest_eligible_leve'  => trim($data[22]) ,
                'Highest_eligible_lev'  => trim($data[23]) ,
                'Vacant'  => trim($data[24]) ,
                'VacantName'  => trim($data[25]) ,
                'Company_Code'  => trim($data[26]) ,
                'Company_Name'  => trim($data[27]) ,
                'Personnel_area'  => trim($data[28]) ,
                'Personnel_subarea'  => trim($data[29]) ,
                'Personnel_subarea_name'  => trim($data[30]) ,
                'Employee_group'  => trim($data[31]) ,
                'Employee_group_Name'  => trim($data[32]) ,
                'Employee_subgroup'  => trim($data[33]) ,
                'Employee_subgroup_Name'  => trim($data[34]) ,
                'Active'  => trim($data[35]) ,
                'Inactive'  => trim($data[36]) ,
                'Frozen'  => trim($data[37]) ,
                'Delimit'  => trim($data[38]) ,
                'Budgeted'  => trim($data[39]) ,
                'Non Budgeted'  => trim($data[40]) ,
                'Building'  => trim($data[41]) ,
                'Capex'  => trim($data[42]) ,
                'Overhead'  => trim($data[43]) ,
                'Position_Type'  => trim($data[44]) ,
                'Position_Type_Name'  => isset($data[45]) ? trim($data[45]) : '' ,
                'LEVEL1'  => isset($data[46]) ? trim($data[46]) : '' ,
                'LEVEL2'  =>isset($data[47]) ? trim($data[47]) : '' ,
                'Cost_Center'  => isset($data[48]) ? trim($data[48]) : '' ,
                'CAPEX_d'  => isset($data[49]) ? trim($data[49]) : '' ,
                'AB-InBev_Entity'  => isset($data[50]) ? trim($data[50]) : '' ,
                'InBev_Entity'  => isset($data[51]) ? trim($data[51]) : '' ,
                'Inbev_Entity_Descrip'  => isset($data[52]) ? trim($data[52]) : '' ,
                'created_at' => date("Y-m-d H:i:s"),
                'updated_at' => date("Y-m-d H:i:s")
          );
          
          if($cont == 1000){
            EoReport::insertOrIgnore($data_row);
            $data_row = array();
            $cont = 0;
         }
      }
      if( count($data_row) > 0 ){
        EoReport::insertOrIgnore($data_row);
        $data_row = array();
      }
      fclose($myfile);
      return $line;
  }
  
  
  private function saveLjReport($filePath){
      $line = 0;
      $myfile = fopen($filePath, "r");     
      $data_row = array();
      $cont = 0;
      while (($data = fgetcsv($myfile, ",")) == true) 
      {
          $data = array_map("utf8_encode", $data);
          $line++;
          $cont++;
          if( $line == 1 ){
            continue;
          }
          $data_row[] =  array(
                'Country_Grouping' => trim($data[0]),
                'Local_Job_ID' => trim($data[1]),
                'Start_Date' => trim($data[2]),
                'End_Date' => trim($data[3]),
                'Object_Type' => trim($data[4]),
                'Local_Job_ID2' => trim($data[5]),
                'Object_abbreviation' => trim($data[6]),
                'Local_Job_Descriptio' => trim($data[7]),
                'Pay_Scale_Area' => trim($data[8]),
                'Pay_Scale_Group' => trim($data[9]),
                'Job_Level' => trim($data[10]),
                'Job_Level_text' => trim($data[11]),
                'Job' => trim($data[12]),
                'Job_text' => trim($data[13]),
                'Analysis_Block' => trim($data[14]),
                'Analysis_block_Descr' => trim($data[15]),
                'Employee_group' => trim($data[16]),
                'Name_of_employee_grp' => trim($data[17]),
                'Employee_subgroup' => isset($data[18]) ? trim($data[18]) : '',
                'Name_of_EE_subgroup' => isset($data[19]) ? trim($data[19]) : '',
                'Position_Type' => isset($data[20]) ? trim($data[20]) : '',
                'Position_Type_Text' => isset($data[21]) ? trim($data[21]) : '',
                'Object_ID' => isset($data[22]) ? trim($data[22]) : '',
                'ABI_Entity_Text' => isset($data[23]) ? trim($data[23]) : '',
                'Action' => isset($data[24]) ? trim($data[24]) : '',
                'created_at' => date("Y-m-d H:i:s"),
                'update_at' => date("Y-m-d H:i:s")
          );
          
          if($cont == 1000){
            LjReport::insert($data_row);
            $data_row = array();
            $cont = 0;
         }
      }
      if( count($data_row) > 0 ){
        LjReport::insert($data_row);
        $data_row = array();
      }
      fclose($myfile);
      return $line;
  }
  
  
  private function savePbReport($filePath){
      $line = 0;
      $myfile = fopen($filePath, "r");     
      $data_row = array();
      $cont = 0;
      while (($data = fgetcsv($myfile, ",")) == true) 
      {
          $data = array_map("utf8_encode", $data);
          $line++;
          $cont++;
          if( $line == 1 ){
            continue;
          }
          $data_row[] =  array(
                'Cl' => trim($data[0]),
                'CGrpg' => trim($data[1]),
                'Zone' => trim($data[2]),
                'Group_code' => trim($data[3]),
                'Group_Description' => trim($data[4]),
                'COAr' => trim($data[5]),
                'CoCd' => trim($data[6]),
                'Cost_Ctr' => trim($data[7]),
                'YEAR' => trim($data[8]),
                'Month' => trim($data[9]),
                'Target' => isset($data[10]) ? trim($data[10]) : '',
                'Increase_target_positions' => isset($data[11]) ? trim($data[11]) : '',
                'Dec_Postio' => isset($data[12]) ? trim($data[12]) : '',
                'Actual' => isset($data[13]) ? trim($data[13]) : '',
                'created_at' => date("Y-m-d H:i:s"),
                'updated_at' => date("Y-m-d H:i:s")
          );
          
          if($cont == 1000){
            PbReport::insert($data_row);
            $data_row = array();
            $cont = 0;
         }
      }
      if( count($data_row) > 0 ){
        PbReport::insert($data_row);
        $data_row = array();
      }
      fclose($myfile);
      return $line;
  }
  
  private function saveCtReport($filePath){
      $line = 0;
      $myfile = fopen($filePath, "r");     
      $data_row = array();
      $cont = 0;
      while (($data = fgetcsv($myfile, ",")) == true) 
      {
          $data = array_map("utf8_encode", $data);
          $line++;
          $cont++;
          if( $line == 1 ){
            continue;
          }
          $data_row[] =  array(
                'Cl' => trim($data[0]),
                'COAr' => trim($data[1]),
                'Cost_Ctr' => trim($data[2]),
                'Start_Date' => trim($data[3]),
                'End_Date' => trim($data[4]),
                'SITE_Code' => trim($data[5]),
                'SITE_Description' => isset($data[6]) ? trim($data[6]) : '',
                'SITE_TYPE' => isset($data[7]) ? trim($data[7]) : '',
                'CoCd' => isset($data[8]) ? trim($data[8]) : '',
                'Cost_Type_L1' => isset($data[9]) ? trim($data[9]) : '',
                'Cost_Type_L2' => isset($data[10]) ? trim($data[10]) : '',
                'Action' => isset($data[11]) ? trim($data[11]) : '',
                'created_at' => date("Y-m-d H:i:s"),
                'updated_at' => date("Y-m-d H:i:s")
          );
          
          if($cont == 1000){
            CtReport::insert($data_row);
            $data_row = array();
            $cont = 0;
         }
      }
      if( count($data_row) > 0 ){
        CtReport::insert($data_row);
        $data_row = array();
      }
      fclose($myfile);
      return $line;
  }
  
  private function saveLjVsAgrupReport($filePath){
      $line = 0;
      $myfile = fopen($filePath, "r");     
      $data_row = array();
      $cont = 0;
      while (($data = fgetcsv($myfile, ",")) == true) 
      {
          $data = array_map("utf8_encode", $data);
          $line++;
          $cont++;
          if( $line == 1 ){
            continue;
          }
          $data_row[] =  array(
                'Cl' => isset($data[0]) ? trim($data[0]) : '',
                'Zone' => isset($data[1]) ? trim($data[1]) : '',
                'CGrpg' => isset($data[2]) ? trim($data[2]) : '',
                'Group_code' => isset($data[3]) ? trim($data[3]) : '',
                'Group_Description' => isset($data[4]) ? trim($data[4]) : '',
                'OT' => isset($data[5]) ? trim($data[5]) : '',
                'Local_Job_ID' => isset($data[6]) ? trim($data[6]) : '',
                'Local_Job_Description' => isset($data[7]) ? trim($data[7]) : '',
                'Start_Date' => isset($data[8]) ? trim($data[8]) : '',
                'End_Date' => isset($data[9]) ? trim($data[9]) : '',
                'created_at' => date("Y-m-d H:i:s"),
                'updated_at' => date("Y-m-d H:i:s")
          );
          
          if($cont == 1000){
            LjVsAgrupReport::insert($data_row);
            $data_row = array();
            $cont = 0;
         }
      }
      if( count($data_row) > 0 ){
        LjVsAgrupReport::insert($data_row);
        $data_row = array();
      }
      fclose($myfile);
      return $line;
  }
  

  
}