You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
5.4 KiB
PHP
140 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Helper\JSONResponse;
|
|
use App\Helper\STS\Indokargo;
|
|
use App\Helper\Traits\Models\CanMultiOrderBy;
|
|
use App\Helper\Traits\Models\CanMultiSearch;
|
|
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class Tv extends Model {
|
|
use HasFactory;
|
|
use CanMultiSearch;
|
|
use CanMultiOrderBy;
|
|
|
|
protected $table = 'tvs';
|
|
protected $hidden = ['ik_cust_id', 'ik_address_id'];
|
|
protected $casts = ['device_info'=>AsArrayObject::class];
|
|
|
|
//------------------------------------------------------------
|
|
// -- RELATED TO RELATIONSHIP
|
|
/// HAS ONE
|
|
public function new_tv_request(): HasOne { return $this->hasOne(NewTvRequest::class, 'tv_fk', 'id'); }
|
|
public function tv_app_info(): HasOne { return $this->hasOne(TvAppInfo::class, 'tv_fk', 'id'); }
|
|
/// HAS MANY
|
|
public function tv_connect_logs(): HasMany { return $this->hasMany(TvConnectLog::class, 'tv_fk', 'id'); }
|
|
public function tv_sessions(): HasMany { return $this->hasMany(TvSession::class, 'tv_fk', 'id'); }
|
|
/// BELONGS TO
|
|
public function outlet(): BelongsTo { return $this->belongsTo(Outlet::class, 'outlet_fk', 'id'); }
|
|
// -- END RELATED TO RELATIONSHIP
|
|
//------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------
|
|
// -- RELATED TO DATA FUNCTION
|
|
// -- END RELATED TO DATA FUNCTION
|
|
//------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------
|
|
// -- RELATED TO REQUEST
|
|
public static function modifyAndValidateUpsertRequest(Request $request, $isUpdate = false) {
|
|
if($request->code) $request->merge(['code' => strtoupper($request->code)]);
|
|
$validationRules = [
|
|
'code' => ['required','string',Rule::unique('tvs', 'code')
|
|
->when($request->id, function($q, $id) {
|
|
$q->whereNot('id', $id);
|
|
})],
|
|
'col1' => 'nullable|string',
|
|
'col2' => 'nullable|string',
|
|
'col3' => 'nullable|string',
|
|
'col4' => 'nullable|string',
|
|
'col5' => 'nullable|string',
|
|
'col6' => 'nullable|string',
|
|
'col7' => 'nullable|string',
|
|
'col8' => 'nullable|string',
|
|
'col9' => 'nullable|string',
|
|
'col10' => 'nullable|string',
|
|
'notes' => 'nullable|string',
|
|
];
|
|
if($isUpdate) $validationRules['id'] = 'nullable|integer|exists:App\Models\Tv';
|
|
$request->validate();
|
|
return $request;
|
|
}
|
|
public static function updateFromRequest(Request $request) {
|
|
if($request->code) $request->merge(['code' => strtoupper($request->code)]);
|
|
$request->validate([
|
|
'id' => 'required|integer|exists:App\Models\Tv',
|
|
'code' => ['required','string',Rule::unique('tvs', 'code')
|
|
->when($request->id, function($q, $id) {
|
|
$q->whereNot('id', $id);
|
|
})],
|
|
'col1' => 'nullable|string',
|
|
'col2' => 'nullable|string',
|
|
'col3' => 'nullable|string',
|
|
'col4' => 'nullable|string',
|
|
'col5' => 'nullable|string',
|
|
'col6' => 'nullable|string',
|
|
'col7' => 'nullable|string',
|
|
'col8' => 'nullable|string',
|
|
'col9' => 'nullable|string',
|
|
'col10' => 'nullable|string',
|
|
'notes' => 'nullable|string',
|
|
]);
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$tv = TV::findOrFail($request->id);
|
|
$tv->code = $request->code;
|
|
$tv->col1 = $request->col1;
|
|
$tv->col2 = $request->col2;
|
|
$tv->col3 = $request->col3;
|
|
$tv->col4 = $request->col4;
|
|
$tv->col5 = $request->col5;
|
|
$tv->col6 = $request->col6;
|
|
$tv->col7 = $request->col7;
|
|
$tv->col8 = $request->col8;
|
|
$tv->col9 = $request->col9;
|
|
$tv->col10 = $request->col10;
|
|
$tv->notes = $request->notes;
|
|
$tv->update();
|
|
|
|
// try to sys_to_sys with indokargo
|
|
$jsonResponse = Indokargo::updateTVAddress($request, $tv->id, $tv->ik_address_id);
|
|
DB::commit();
|
|
return $jsonResponse;
|
|
} catch(\Throwable $th) {
|
|
DB::rollback();
|
|
throw $th;
|
|
}
|
|
|
|
}
|
|
public static function changeStatusFromRequest(Request $request) {
|
|
$request->validate(['id' => 'required|integer|exists:App\Models\TV,id']);
|
|
|
|
try {
|
|
$tv = Tv::findOrFail($request->id);
|
|
$tv->is_active = !$tv->is_active;
|
|
$tv->save();
|
|
|
|
// try to sys_to_sys with indokargo
|
|
$jsonResponse = Indokargo::changeStatusAddress(new Request(['is_active' => $tv->is_active]),
|
|
$tv->id, $tv->ik_address_id);
|
|
DB::commit();
|
|
return $jsonResponse;
|
|
} catch(\Throwable $th) {
|
|
DB::rollback();
|
|
throw $th;
|
|
}
|
|
}
|
|
// -- END RELATED TO REQUES
|
|
//------------------------------------------------------------
|
|
}
|