chess/app/Models/UnitModel.php

95 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2024-08-29 03:56:32 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\DataTables;
class UnitModel extends Model
{
//
protected $primaryKey = 'id';
protected $table = 'master_unit';
public $timestamps = true;
public function initData($request,$route)
{
// INIT DB
$data['title'] = 'Master Unit';
$data['actButton'] = ['edit','hapus'];
$data['tableHead'] =
array(
["Nama Unit","all","unit_name"],
["Unit Code","all","unit_code"],
["Jenis Task","all","definition"],
["Nama Perusahaan","all","company_name"],
["Act","all","action"]
);
$data['db'] = $this->table;
$data['db_key'] = $this->primaryKey;
$data['route'] = $route;
$data['timestamps'] = true;
$data['bisaAdd'] = true;
$data['adaDetail'] = false;
$data['idManual'] = true;
$data['seq'] = false;
$data['serial'] = true;
$dtable = DB::select("SELECT {$this->table}.*, mc.company_name, rtt.definition FROM {$this->table}
JOIN ref_task_type rtt ON rtt.id = {$this->table}.task_type_id
JOIN master_company mc ON mc.id = {$this->table}.company_id
WHERE {$this->table}.company_id = ? order by {$this->table}.id desc",[Auth::user()->company_id]);
// LIST DATA TABLE
$data['data_table'] = $dtable;
// FORM FIELD FOR STORE
if($request->post('get_id')) {
$data['set_field'] = [
'unit_name' => $request->post('unit_name'),
'unit_code' => $request->post('unit_code'),
'task_type_id' => $request->post('task_type_id'),
'user_upd_id' => Auth::user()->id,
'company_id' => Auth::user()->company_id,
'is_active' => $request->post('is_active')
];
} else {
$data['set_field'] = [
'unit_name' => $request->post('unit_name'),
'unit_code' => $request->post('unit_code'),
'task_type_id' => $request->post('task_type_id'),
'user_crt_id' => Auth::user()->id,
'company_id' => Auth::user()->company_id,
'is_active' => $request->post('is_active')
];
}
// GET DATA FOR EDIT
if ($request->post('id')) {
$data['get_data_edit'] = DB::selectOne("SELECT * FROM {$this->table} where id = ?",[$request->post('id')]);
}
foreach($data['tableHead'] as $v){
$arrHead[] = $v[2];
}
$data['head'] = implode(",",$arrHead);
return $data;
}
public function getDT($data,$init)
{
$dt = DataTables::of($data);
$dt->editColumn('is_active', function($data) {
return $data->is_active?'Aktif':'Non Aktif';
});
return $dt;
}
}