From 2ed57016bc790822f29a228850902a7614b8a9d6 Mon Sep 17 00:00:00 2001 From: ricky rx Date: Mon, 24 Jun 2024 15:34:20 +0700 Subject: [PATCH] chore: adjust for filter TV --- app/Helper/Common.php | 7 +++++++ app/Models/Tv.php | 26 +++++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/app/Helper/Common.php b/app/Helper/Common.php index 89c4f6c..f1c18e8 100644 --- a/app/Helper/Common.php +++ b/app/Helper/Common.php @@ -2,6 +2,8 @@ namespace App\Helper; +use Illuminate\Support\Facades\DB; + class Common { public static function convertRequestConfig(?array $requestConfig): array { $config = []; @@ -26,4 +28,9 @@ class Common { public static function trueOrFalse(mixed $value): bool { return ($value === "true" || $value === "1" || $value === 1 || $value === true) ? true : false; } + + public static function setTimezone($timezone) { + date_default_timezone_set($timezone); + DB::statement("SET TIME ZONE '$timezone'"); + } } \ No newline at end of file diff --git a/app/Models/Tv.php b/app/Models/Tv.php index 042157c..51231c0 100644 --- a/app/Models/Tv.php +++ b/app/Models/Tv.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Helper\Common; use App\Helper\DatabaseHelper; use App\Helper\JSONResponse; use App\Helper\STS\Indokargo; @@ -70,11 +71,17 @@ class Tv extends Model { ...DatabaseHelper::getOrderBysValidations(), 'search' => DatabaseHelper::getSearchValidation(), 'apkVersionCode' => 'nullable|integer', + 'tz' => 'required|timezone', 'isActive' => 'nullable|boolean', + 'installedAt' => 'nullable|array', + 'installedAt.from' => 'nullable|date', + 'installedAt.to' => 'nullable|date', 'lastConnectedAt' => 'nullable|array', 'lastConnectedAt.from' => 'nullable|date', - 'lastConnectedAt.to' => 'nullable|date' + 'lastConnectedAt.to' => 'nullable|date', + 'lastConnectedDay' => 'nullable|int' ]); + Common::setTimezone($request->tz); return Tv::when($request->isActive != null, function($q) use($request) { $q->where('is_active', $request->isActive); @@ -82,6 +89,12 @@ class Tv extends Model { ->when($request->apkVersionCode, function($q, $apkVersionCode) { $q->where('apk_version_code', $apkVersionCode); }) + ->when($request->installedAt['from'] ?? '', function($q, $from) { + $q->where(DB::raw('DATE(installed_at)'), '>=', $from); + }) + ->when($request->installedAt['to'] ?? '', function($q, $to) { + $q->where(DB::raw('DATE(installed_at)'), '<=', $to); + }) ->when($request->lastConnectedAt['from'] ?? '', function($q, $from) { $q->where(DB::raw('DATE(last_connected_at)'), '>=', $from); }) @@ -178,12 +191,7 @@ class Tv extends Model { //------------------------------------------------------------ // -- RELATED TO EXCEL public static function getExcelDetail(Request $request) { - $cols = ['code', 'apk_version_code', 'apk_version_name', 'installed_at', 'last_connected_at', - 'company_name', 'address', 'street_address', 'notes', - 'col1', 'col2', 'col3', 'col4', 'col5', - 'col6', 'col7', 'col8', 'col9', 'col10', - 'is_active']; - return JSONResponse::Success(['rows' => TV::validateAndGetEloquentFromRequest($request)->select(...$cols)->get()]); + return JSONResponse::Success(['rows' => TV::validateAndGetEloquentFromRequest($request)->get()]); } // ---- RELATED TO EXPORT IMPORT @@ -202,8 +210,8 @@ class Tv extends Model { return JSONResponse::Success(['rows' => $rows]); } public static function getExportData(Request $request) { - $rows = TV::validateAndGetEloquentFromRequest($request)->select(...self::EXCEL_TEMPLATE_COLS)->get(); - return JSONResponse::Success(['rows' => $rows]); + $rows = TV::validateAndGetEloquentFromRequest($request)->get(); + return JSONResponse::Success(['rows' => $rows, 'cols' => self::EXCEL_TEMPLATE_COLS]); } // ------ RELATED TO IMPORT private static function _checkExcelFormat($tvRows) {