RatingBrowserController

Ada comment // UPDATE CLIENT-DEVICE FOR RATING itu tambahannya
mail@pastecode.io avatar
unknown
php
2 years ago
5.4 kB
3
Indexable
<?php

namespace App\Http\Controllers\Rating;

use App\Collections\Rating\RatingCollection;
use App\Containers\Search\Rating\RatingSearch;
use App\Http\Controllers\Controller;
use App\Support\Browse\Browse;
use App\Support\Response\Json;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;

class RatingBrowseController extends Controller
{
    use Browse, RatingCollection {
        RatingCollection::__construct as private __RatingCollectionConstruct;
    }

    public function __construct(Request $request)
    {
        if ($request) {
            $this->_Request = $request;
            $this->_Request->CustomFieldId = 'id_rating';
        }
        if (!str_contains($request->path(), 'case/result') && !str_contains($request->path(), 'my/check')) {
            $this->middleware(['role:admin|approver']);
        }
        
        
        $this->__RatingCollectionConstruct();
    }

    public function Anything() {
        if ($this->_Request->ArrQuery) {
            $this->_ArrQuery = $this->_Request->ArrQuery;
            if (!empty($this->_ArrQuery->{'s'})){
                $this->SearchModel = App::make(RatingSearch::class);
                $this->Search = $this->_ArrQuery->{'s'};
            }
        }

        // UPDATE CLIENT DEVICE FOR RATING
        $origin = $this->_Request->header('Client-Device');
        $RatingModel = $this->RatingModel::where(function ($query) use ($origin) {
            if (isset($origin)) {
              $query->where('origin', $origin);
            } else {
              $query->where('origin', 'app');
            }
            if (isset($this->_ArrQuery->{'my'}) && $this->_ArrQuery->{'my'} === 'check') {
                $now = Carbon::now();
                $quartal = ceil((int)$now->format('n') / 3) ;
                $year = (int)$now->format('Y');
                $query->where([
                    [DB::raw("CEIL(CAST(DATE_FORMAT(rating_date, '%c' )  AS SIGNED) / 3)"), $quartal],
                    [DB::raw("CAST(DATE_FORMAT(rating_date, '%Y' )  AS SIGNED)"), $year],
                    ['user_name', $this->_Request->Me->USERNAME],
                ]);
            }
            if (isset($this->_ArrQuery->{'case'}) && $this->_ArrQuery->{'case'} === 'result') {
                $now = Carbon::now();
                $quartal = ceil((int)$now->format('n') / 3) ;
                $year = (int)$now->format('Y');
                $query->where([
                    [DB::raw("CEIL(CAST(DATE_FORMAT(rating_date, '%c' )  AS SIGNED) / 3)"), $quartal],
                    [DB::raw("CAST(DATE_FORMAT(rating_date, '%Y' )  AS SIGNED)"), $year],
                ]);
            }
            if (isset($this->_ArrQuery->{'year'}) && intval($this->_ArrQuery->{'year'}) !== 0) {
                $year = (int)$this->_ArrQuery->{'year'};
                $query->where([
                    [DB::raw("CAST(DATE_FORMAT(rating_date, '%Y' )  AS SIGNED)"), $year],
                ]);
            }
            if (isset($this->_ArrQuery->{'quartal'}) && intval($this->_ArrQuery->{'quartal'}) !== 0) {
                $quartal = (int)$this->_ArrQuery->{'quartal'};
                $query->where([
                    [DB::raw("CEIL(CAST(DATE_FORMAT(rating_date, '%c' )  AS SIGNED) / 3)"), $quartal],
                ]);
            }
            if (!empty($this->_ArrQuery->{'date.from'})) {
                $query->where(DB::raw("DATE_FORMAT(rating_date, '%Y-%m-%d' )"), '>=', $this->_ArrQuery->{'date.from'});
            } 
            if (!empty($this->_ArrQuery->{'date.to'})) {
                $query->where(DB::raw("DATE_FORMAT(rating_date, '%Y-%m-%d' )"), '<=', $this->_ArrQuery->{'date.to'});
            } 
        });

        $RatingModel->select(
            "$this->RatingTable.id_rating as Rating.id_rating",
            "$this->RatingTable.user_name as Rating.user_name",
            "$this->RatingTable.rating_val as Rating.rating_val",
            "$this->RatingTable.rating_comment as Rating.rating_comment",
            "$this->RatingTable.rating_date as Rating.rating_date",
            "$this->RatingTable.rating_periode as Rating.rating_periode",
        );


        $Browse = $this->orderBy(['id', 'rating_date', 'rating_periode', 'rating_val'])->Browse($this->_Request, $RatingModel, function ($data) {
            $data = $this->Manipulate($data);
            $data = $this->getUserDetail($data);
            if (isset($this->_ArrQuery->{'case'}) && $this->_ArrQuery->{'case'} === 'result') {
                $data = $this->getResult($data);
            }
            if (isset($this->_ArrQuery->{'case'}) && $this->_ArrQuery->{'case'} === 'result-admin') {
                $data = $this->getResultAdmin($data);
            }
            return $data;
        });

        Json::set('data', $Browse);
        Json::set('response.code', 200);
        Json::set('response.description', 'OK');
        return response()->json(Json::get(), Json::get('response.code'));
    }

    private function Manipulate($records)
    {
        return $records->map(function ($item) {
            foreach ($item->getAttributes() as $key => $value) {
                $this->Group($item, $key, 'Rating.', $item);
            }
            return $item;
        });
    }
    
}