Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.84 KB | None | 0 0
  1.  $agent = Auth::guard('agent')->user();
  2.  
  3.             $startDate = '';
  4.             $endDate = '';
  5.  
  6.             // get dealer
  7.             $locationCode = LocationCode::select('dealer_code', 'karesidenan')->where('agent_code',$agent->code);
  8.  
  9.             // get dealer
  10.             $dealer = clone $locationCode;
  11.             $locationDealerCode = $dealer->where('location_type','1')->get()->pluck('dealer_code');
  12.  
  13.             // get main dealer
  14.             $mainDealer = clone $locationCode;
  15.             $locationMainDealerCode = $mainDealer->where('location_type','2')->get()->pluck('dealer_code');
  16.  
  17.  
  18.             $joinVehicleType = VehicleType::select('vehicle_type.id','category','segment','sub_series', 'type', 'code', 'commercial_name')
  19.                 ->where('agent_code',$agent->code)
  20.                 ->whereNotNull('category')
  21.                 ->whereNotNull('segment')
  22.                 ->whereNotNull('sub_series')
  23.                 ->whereNotNull('type');
  24.  
  25.             $joinVehicle = Vehicle::select('vehicles.id', 'vehicle_type_id', 'qrcode_code', 'vt.category', 'vt.segment', 'vt.sub_series', 'vt.type', 'vt.code', 'vt.commercial_name')
  26.                 ->where('agent_code', $agent->code);
  27.  
  28.             $joinVehicle->join(\DB::raw("(" . QueryLib::getSqlWithBindings($joinVehicleType) . ") as vt"), 'vt.id', 'vehicles.vehicle_type_id');
  29.  
  30.             // total vehicle
  31.             $totalVehicle = ScanLog::selectRaw('COUNT(DISTINCT(scan_log.qrcode_code)) as total,  v.category, v.segment, v.sub_series, v.type, v.code, v.commercial_name')
  32.                 ->where('agent_code', $agent->code)->where('flag', ScanLog::OUT)
  33.                 ->whereIn('agent_from', $locationMainDealerCode)
  34.                 ->where('agent_to', $agent->merchant_code)
  35.                 ->groupBy('v.sub_series');
  36.  
  37.             // scan dealer spg
  38.             $scanIn = ScanLog::selectRaw('COUNT(DISTINCT(scan_log.qrcode_code)) as total, sub_series')
  39.                 ->where('agent_code', $agent->code)->whereRaw("LOWER(activity_description) = 'spg'")
  40.                 ->where('branch_code', $agent->merchant_code)
  41.                 ->groupBy('v.sub_series');
  42.  
  43.             $scanLog = ScanLog::where('scan_log.agent_code',$agent->code)
  44.                 ->selectRaw('DISTINCT(scan_log.qrcode_code)')
  45.                 ->where('agent_to', $agent->merchant_code)
  46.                 ->whereRaw("LOWER(activity_description) = 'spg'");
  47.  
  48.  
  49.             $claimWithoutSpg = VehicleClaim::selectRaw("COUNT(DISTINCT(vehicle_claim.qrcode_code)) as total, v.sub_series")
  50.                 ->where('vehicle_claim.branch_code', $agent->merchant_code)
  51.                 ->where('vehicle_claim.agent_code', $agent->code);
  52.  
  53.             $claimWithoutSpg->join(\DB::raw("(".QueryLib::getSqlWithBindings($joinVehicle).") as v"), 'vehicle_claim.vehicle_id', 'v.id')
  54.                 ->groupBy('v.sub_series');
  55.  
  56.             // join to vehicle
  57.             $totalVehicle->join(\DB::raw("(".QueryLib::getSqlWithBindings($joinVehicle).") as v"), 'scan_log.qrcode_code', 'v.qrcode_code');
  58.             $scanIn->join(\DB::raw("(".QueryLib::getSqlWithBindings($joinVehicle).") as v"), 'scan_log.object_id', 'v.id');
  59.  
  60.             // total sales
  61.             $vehicleClaim = VehicleClaim::selectRaw('COUNT(DISTINCT(vehicle_claim.qrcode_code)) as total, v.category, v.segment, v.sub_series')
  62.                 ->where('agent_code', $agent->code)
  63.                 ->where('vehicle_claim.branch_code', $agent->merchant_code)
  64.                 ->groupBy('v.sub_series');
  65.  
  66.             // join sales with to vehicle
  67.             $vehicleClaim->join(\DB::raw("(".QueryLib::getSqlWithBindings($joinVehicle).") as v"), 'vehicle_claim.vehicle_id', 'v.id');
  68.  
  69.             // set start_date and end_date by request month
  70.             if ($request->month && $request->month >= 1){
  71.                 $startDate = DateLib::getStartDate($request->month);
  72.                 if ($request->month == 2) {
  73.                     $endDate = DateLib::getEndDate(1);
  74.                 } else {
  75.                     $endDate = date('Y-m-d 23:59:59');
  76.                 }
  77.             }
  78.  
  79.             // set start_date and end_date by request date_start and date_end
  80.             if($request->date_start)
  81.                 $startDate = substr($request->date_start,0,10) . ' 00:00:00';
  82.  
  83.             if($request->date_end)
  84.                 $endDate = substr($request->date_end,0,10) . ' 23:59:59';
  85.  
  86.             if ($startDate)
  87.             {
  88.                 $scanInIncoming = clone  $scanIn;
  89.                 $claimWithoutSpgIncoming = clone $claimWithoutSpg;
  90.                 $claimIncoming = clone $vehicleClaim;
  91.  
  92.                 $scanInIncoming->whereRaw("IF(saved_at IS NOT NULL, saved_at, created_at) <= '{$startDate}' ");
  93.                 $claimWithoutSpgIncoming->whereRaw("IF(vehicle_claim.invoice_date IS NOT NULL, vehicle_claim.invoice_date, vehicle_claim.created_at) <= '{$startDate}'");
  94.                 $claimIncoming->whereRaw("IF(invoice_date IS NOT NULL, invoice_date, created_at) <= '{$startDate}' ");
  95.  
  96.             }
  97.  
  98.             if ($endDate)
  99.             {
  100.                 $scanIn->whereRaw("IF(saved_at IS NOT NULL, saved_at, created_at) <= '{$endDate}'");
  101.                 $claimWithoutSpg->whereRaw("IF(vehicle_claim.invoice_date IS NOT NULL, vehicle_claim.invoice_date, vehicle_claim.created_at) <= '{$endDate}'");
  102.             }
  103.  
  104.             $claimWithoutSpg->leftJoin(\DB::raw("(".QueryLib::getSqlWithBindings($scanLog).") as s"), 's.qrcode_code', 'vehicle_claim.qrcode_code')->whereNull('v.qrcode_code');
  105.  
  106.             if ($startDate && $endDate)
  107.             {
  108.                 $totalVehicle->whereRaw("IF(saved_at IS NOT NULL, saved_at, created_at) >= '{$startDate}' AND IF(saved_at IS NOT NULL, saved_at, created_at) <= '{$endDate}'");
  109.                 $vehicle = clone $totalVehicle;
  110.  
  111.                 $vehicleClaim->whereRaw("IF(invoice_date IS NOT NULL, invoice_date, created_at) >= '{$startDate}' AND IF(invoice_date IS NOT NULL, invoice_date, created_at) <= '{$endDate}'");
  112.             }
  113.  
  114.             // total vehicle
  115.             $totalVehicleSubSeries = $totalVehicle->pluck('total', 'sub_series');
  116.  
  117.             // claim wihtout spg
  118.             $claimWithoutSpgSubSeries = $claimWithoutSpg->pluck('total', 'sub_series');
  119.  
  120.             // scanIn
  121.             $scanInSubSeries = $scanIn->pluck('total', 'sub_series');
  122.  
  123.             // claim without spg
  124.             $claimWithoutSpgIncomingSubSeries = $claimWithoutSpgIncoming->pluck('total', 'sub_series');
  125.  
  126.             // scanin incoming
  127.             $scanInIncomingSubSeries = $scanInIncoming->pluck('total', 'sub_series');
  128.  
  129.             // claim incoming
  130.             $claimIncomingSubSeries = $claimIncoming->pluck('total', 'sub_series');
  131.  
  132.             // total sales
  133.             $totalSalesSubSeries = $vehicleClaim->pluck('total', 'sub_series');
  134.  
  135.             $vehicle = $vehicle->get();
  136.             $collectData = collect($vehicle);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement