chess/app/Models/ModelModel.php
Ramadhon Ikhsan Prasetya 603e31b0f4 init
2024-08-29 10:56:32 +07:00

179 lines
6.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\DataTables;
class ModelModel extends Model
{
//
protected $primaryKey = 'id';
protected $table = 'master_model';
public $timestamps = true;
public function initData($request,$route)
{
// INIT DB
$data['title'] = 'Referensi Model';
$data['actButton'] = ['edit','hapus'];
$data['tableHead'] =
array(
["Nama Model","all","description"],
["Status Aktif","all","is_active"],
["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 FROM {$this->table}
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'] = [
'description' => $request->post('description'),
'user_upd_id' => Auth::user()->id,
'company_id' => Auth::user()->company_id,
'is_active' => $request->post('is_active')
];
} else {
$data['set_field'] = [
'description' => $request->post('description'),
'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']['head'] = DB::selectOne("SELECT * FROM {$this->table} where id = ?",[$request->post('id')]);
$data['get_data_edit']['detail'] = ModelingTaskModel::where('model_id', $request->post('id'))->get();
}
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;
}
public function storeCustom($request)
{
DB::beginTransaction();
try {
$init = $this->initData($request,$request->get('type'));
$get_id = $request->input('get_id');
$setField = $init['set_field'];
$timestamps = $init['timestamps'];
if ($get_id) {
if ($timestamps) {
$setField['updated_at'] = date('Y-m-d H:i:s');
}
DB::table($init['db'])->where($init['db_key'], $get_id)->update($setField);
ModelingTaskModel::where('model_id', $get_id)->delete();
if(count($request->modelingTask)>0) {
$dataModelingTask = [];
// $idMt = DB::table('rel_model_task')->max('id');
// $noId = $idMt + 1;
foreach($request->modelingTask as $k => $v) {
$dataModelingTask[$k] = [
// 'id' => $noId++,
'model_id' => $get_id,
'task_id' => $v,
'company_id' => Auth::user()->company_id,
];
}
ModelingTaskModel::insert($dataModelingTask);
}
//IF ADD DATA
}else {
if ($timestamps) {
$setField['created_at'] = date('Y-m-d H:i:s');
$setField['updated_at'] = date('Y-m-d H:i:s');
}
if ($init['idManual']) {
$getId = DB::table($init['db'])->max('id');
$setField['id'] = $getId+1;
}
if ($init['seq']) {
$seq = DB::Table($init['db'])->max('seq');
$setField['seq'] = $seq+1;
}
$id = DB::table($init['db'])->insertGetId($setField);
if(count($request->modelingTask)>0) {
$dataModelingTask = [];
// $idMt = DB::table('rel_model_task')->max('id');
// $noId = $idMt + 1;
foreach($request->modelingTask as $k => $v) {
$dataModelingTask[$k] = [
// 'id' => $noId++,
'model_id' => $id,
'task_id' => $v,
'company_id' => Auth::user()->company_id,
];
}
ModelingTaskModel::insert($dataModelingTask);
}
}
DB::commit();
return response()->json([
'rc' => 0,
'rm' => "Berhasil"
]);
} catch (\Throwable $e) {
//throw $th;
DB::rollBack();
return response()->json([
'rc' => 99,
'data' => $e->getMessage()
]);
} catch (QueryException $e) {
DB::rollBack();
return response()->json([
'rc' => 500,
'data' => $e->getMessage()
]);
}
}
}