mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/unity.git
synced 2026-08-19 04:13:58 +00:00
contract template obligation api
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\User\Api;
|
||||
|
||||
use Auth;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Http\Modules\User\ContractTemplateObligationModule;
|
||||
|
||||
use App\Http\Controllers\User\BaseController;
|
||||
|
||||
class ContractTemplateObligationController extends BaseController
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function list(Request $request)
|
||||
{
|
||||
$contract_template_obligation = ContractTemplateObligationModule::list($request);
|
||||
return $contract_template_obligation;
|
||||
}
|
||||
|
||||
public function show(Request $request, $hash_id)
|
||||
{
|
||||
$contract_template_obligation = ContractTemplateObligationModule::show($request, $hash_id);
|
||||
return $contract_template_obligation;
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$contract_template_obligation = ContractTemplateObligationModule::store($request);
|
||||
return $contract_template_obligation;
|
||||
}
|
||||
|
||||
public function update(Request $request, $hash_id)
|
||||
{
|
||||
$contract_template_obligation = ContractTemplateObligationModule::update($request, $hash_id);
|
||||
return $contract_template_obligation;
|
||||
}
|
||||
|
||||
public function delete(Request $request, $hash_id)
|
||||
{
|
||||
$contract_template_obligation = ContractTemplateObligationModule::delete($request, $hash_id);
|
||||
return $contract_template_obligation;
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@ use App\Http\Rules\User\ExistCheck;
|
||||
|
||||
use App\Http\Helpers\General;
|
||||
use App\Http\Helpers\Hasher;
|
||||
use App\Http\Helpers\ImageHandel;
|
||||
use Carbon\Carbon;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Modules\User;
|
||||
|
||||
use Auth;
|
||||
use App\Models\ContractTemplateObligation;
|
||||
use App\Models\ContractTemplateObligationLog;
|
||||
use App\Models\ContractTemplate;
|
||||
|
||||
use App\Http\Rules\User\UnderCheck;
|
||||
use App\Http\Rules\User\ExistCheck;
|
||||
|
||||
use App\Http\Helpers\General;
|
||||
use App\Http\Helpers\Hasher;
|
||||
use Carbon\Carbon;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
use App\Http\Controllers\User\BaseController;
|
||||
|
||||
class ContractTemplateObligationModule extends BaseController
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
General::storeCachePerviousQuery($request->query(), 'contract_template_obligation_query');
|
||||
|
||||
$name = $request->get('name', null);
|
||||
$status = $request->get('status', []);
|
||||
$page = $request->get('page', null);
|
||||
$limit = $request->get('limit', 10);
|
||||
|
||||
$query = ContractTemplateObligation::query();
|
||||
|
||||
if (strlen($name) > 0) {
|
||||
$query->where('name', 'like', '%' . $name . '%');
|
||||
}
|
||||
|
||||
if (!empty($status) > 0) {
|
||||
$query->whereIn('status', $status);
|
||||
}
|
||||
|
||||
$query->where('user_id', Auth::guard('user-api')->user()->id);
|
||||
|
||||
$query->orderBy('id', 'desc');
|
||||
|
||||
$params = [
|
||||
'name' => $name,
|
||||
'status' => $status,
|
||||
'page' => $page
|
||||
];
|
||||
|
||||
$contract_template_obligation_list = $query->paginate($limit);
|
||||
|
||||
$extract = [];
|
||||
foreach ($contract_template_obligation_list as $key => $row) {
|
||||
$extract[] = $row;
|
||||
|
||||
}
|
||||
$pagination = General::paginationGenerator($contract_template_obligation_list, $params, $extract);
|
||||
|
||||
return $pagination;
|
||||
}
|
||||
|
||||
public static function show(Request $request, $hash_id)
|
||||
{
|
||||
$id = Hasher::decode('contract_template_obligations', $hash_id);
|
||||
$contract_template_obligation = ContractTemplateObligation::find($id);
|
||||
if ($contract_template_obligation) {
|
||||
$data = General::returnData($contract_template_obligation, '');
|
||||
return response()->json($data);
|
||||
}
|
||||
$data = General::returnNotFoundData();
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public static function store(Request $request)
|
||||
{
|
||||
$validation = ContractTemplateObligationModule::validation($request);
|
||||
if (!$validation->status) {
|
||||
return response()->json($validation, 422);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
|
||||
$contract_template_id = $request->input('contract_template_id');
|
||||
$contract_template_id = Hasher::decode('contract_templates', $contract_template_id[0]);
|
||||
$contarct_template = ContractTemplate::find($contract_template_id);
|
||||
|
||||
$name = $request->input('name');
|
||||
$sequence = $request->input('sequence');
|
||||
$reference = $request->input('reference');
|
||||
$content = $request->input('content');
|
||||
$must_complete = $request->input('must_complete');
|
||||
$must_accept = $request->input('must_accept');
|
||||
$complete_day_range = $request->input('complete_day_range');
|
||||
$type = $request->input('type');
|
||||
|
||||
$contract_template_obligation = new ContractTemplateObligation();
|
||||
$contract_template_obligation->contract_template_id = $contarct_template->id;
|
||||
$contract_template_obligation->user_id = Auth::guard('user-api')->user()->id;
|
||||
$contract_template_obligation->sequence = $sequence;
|
||||
$contract_template_obligation->reference = $reference;
|
||||
$contract_template_obligation->content = json_encode($content);
|
||||
$contract_template_obligation->must_complete = $must_complete;
|
||||
$contract_template_obligation->must_accept = $must_accept;
|
||||
$contract_template_obligation->complete_day_range = $complete_day_range;
|
||||
$contract_template_obligation->type = $type;
|
||||
$contract_template_obligation->status = config('constants.contract_template_obligation.status.pending');
|
||||
$contract_template_obligation->save();
|
||||
|
||||
// log
|
||||
ContractTemplateObligationModule::makeLog($contract_template_obligation->id, 'user (' . Auth::guard('user-api')->user()->id . ') created');
|
||||
|
||||
DB::commit();
|
||||
|
||||
$query_string = General::getCachePerviousQuery('contract_template_obligation_query');
|
||||
$data = General::returnData($contract_template_obligation, $query_string);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public static function update(Request $request, $hash_id)
|
||||
{
|
||||
$id = Hasher::decode('contract_template_obligations', $hash_id);
|
||||
$validation = ContractTemplateObligationModule::validation($request, $hash_id, 'PUT');
|
||||
if (!$validation->status) {
|
||||
return response()->json($validation, 422);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
$name = $request->input('name');
|
||||
$sequence = $request->input('sequence');
|
||||
$reference = $request->input('reference');
|
||||
$content = $request->input('content');
|
||||
$must_complete = $request->input('must_complete');
|
||||
$must_accept = $request->input('must_accept');
|
||||
$complete_day_range = $request->input('complete_day_range');
|
||||
$type = $request->input('type');
|
||||
|
||||
$contract_template_obligation = ContractTemplateObligation::find($id);
|
||||
$contract_template_obligation->name = $name ?? $contract_template_obligation->name;
|
||||
$contract_template_obligation->sequence = $sequence ?? $contract_template_obligation->sequence;
|
||||
$contract_template_obligation->reference = $reference ?? $contract_template_obligation->reference;
|
||||
$contract_template_obligation->content = json_encode($content) ?? json_encode($contract_template_obligation->content);
|
||||
$contract_template_obligation->must_complete = $must_complete ?? $contract_template_obligation->must_complete;
|
||||
$contract_template_obligation->must_accept = $must_accept ?? $contract_template_obligation->must_accept;
|
||||
$contract_template_obligation->complete_day_range = $complete_day_range ?? $contract_template_obligation->complete_day_range;
|
||||
$contract_template_obligation->type = $type ?? $contract_template_obligation->type;
|
||||
$contract_template_obligation->update();
|
||||
|
||||
// log
|
||||
ContractTemplateObligationModule::makeLog($contract_template_obligation->id, 'user (' . Auth::guard('user-api')->user()->id . ') updated');
|
||||
|
||||
DB::commit();
|
||||
|
||||
$query_string = General::getCachePerviousQuery('contract_template_obligation_query');
|
||||
$data = General::returnData($contract_template_obligation, $query_string);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public static function delete(Request $request, $hash_id)
|
||||
{
|
||||
$id = Hasher::decode('contract_template_obligations', $hash_id);
|
||||
$validation = ContractTemplateObligationModule::validation($request, $hash_id, 'DEL');
|
||||
if (!$validation->status) {
|
||||
return response()->json($validation, 422);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
$contract_template_obligation = ContractTemplateObligation::find($id);
|
||||
$contract_template_obligation->status = config('constants.contract_template_obligation.status.delete');
|
||||
$contract_template_obligation->update();
|
||||
|
||||
// log
|
||||
ContractTemplateObligationModule::makeLog($contract_template_obligation->id, 'user (' . Auth::guard('user-api')->user()->id . ') delete');
|
||||
|
||||
DB::commit();
|
||||
|
||||
$query_string = General::getCachePerviousQuery('contract_template_obligation_query');
|
||||
$data = General::returnData($contract_template_obligation, $query_string);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
|
||||
private static function validation(Request $request, $id = '', $method = 'POST')
|
||||
{
|
||||
$data = $request->all();
|
||||
$rule= [];
|
||||
|
||||
if ($method == 'POST') {
|
||||
$rule = [
|
||||
'contract_template_id' => ['required', new UnderCheck('contract_template'), new ExistCheck('contract_template')],
|
||||
'name' => 'required',
|
||||
'sequence' => 'required|numeric|gt:-1',
|
||||
'reference' => '',
|
||||
'content' => '',
|
||||
'must_complete' => '',
|
||||
'must_accept' => '',
|
||||
'complete_day_range' => 'required|numeric|gt:-1',
|
||||
'type' => 'required',
|
||||
];
|
||||
}
|
||||
|
||||
if ($method == 'PUT') {
|
||||
$data['id'] = $id;
|
||||
$rule = [
|
||||
'id' => [new UnderCheck('contract_template_obligation'), new ExistCheck('contract_template_obligation')],
|
||||
'name' => '',
|
||||
'sequence' => 'nullable|numeric|gt:-1',
|
||||
'reference' => '',
|
||||
'content' => '',
|
||||
'must_complete' => '',
|
||||
'must_accept' => '',
|
||||
'complete_day_range' => 'nullable|numeric|gt:-1',
|
||||
'type' => '',
|
||||
];
|
||||
}
|
||||
|
||||
if ($method == 'DEL') {
|
||||
$data['id'] = $id;
|
||||
$rule = [
|
||||
'id' => [new UnderCheck('contract_template_obligation'), new ExistCheck('contract_template_obligation')],
|
||||
];
|
||||
}
|
||||
|
||||
$validator = Validator::make($data, $rule, config('error_code'));
|
||||
|
||||
$errors = $validator->errors();
|
||||
|
||||
if ($validator->fails()) {
|
||||
$data = General::returnErrorData($errors);
|
||||
return $data;
|
||||
}
|
||||
else {
|
||||
return (object)['status' => true];
|
||||
}
|
||||
}
|
||||
|
||||
public static function makeLog($id = '', $remark = '')
|
||||
{
|
||||
$contract_template_obligation = ContractTemplateObligation::find($id);
|
||||
$contract_template_obligation_log = new ContractTemplateObligationLog();
|
||||
$contract_template_obligation_log->contract_template_obligation_id = $contract_template_obligation->id;
|
||||
$contract_template_obligation_log->history = json_encode($contract_template_obligation);
|
||||
$contract_template_obligation_log->remark = $remark;
|
||||
$contract_template_obligation_log->save();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Rules\User;
|
||||
|
||||
use Auth;
|
||||
use App\Models\ContractTemplate;
|
||||
use App\Models\ContractTemplateObligation;
|
||||
|
||||
use App\Http\Helpers\Hasher;
|
||||
|
||||
@@ -50,6 +51,20 @@ class ExistCheck implements Rule
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->type == 'contract_template_obligation') {
|
||||
foreach ($value as $key => $row) {
|
||||
if(!empty($row)) {
|
||||
$id = Hasher::decode('contract_template_obligations', $row);
|
||||
$data = ContractTemplateObligation::
|
||||
where('id', $id)
|
||||
->count();
|
||||
if (!$data) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Rules\User;
|
||||
|
||||
use Auth;
|
||||
use App\Models\ContractTemplate;
|
||||
use App\Models\ContractTemplateObligation;
|
||||
|
||||
use App\Http\Helpers\Hasher;
|
||||
|
||||
@@ -51,6 +52,21 @@ class UnderCheck implements Rule
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->type == 'contract_template_obligation') {
|
||||
foreach ($value as $key => $row) {
|
||||
if(!empty($row)) {
|
||||
$id = Hasher::decode('contract_template_obligations', $row);
|
||||
$data = ContractTemplateObligation::
|
||||
where('id', $id)
|
||||
->where('user_id', Auth::guard('user-api')->user()->id)
|
||||
->count();
|
||||
if (!$data) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ class ContractTemplate extends Model
|
||||
|
||||
public function getContractTemplateObligationListAttribute()
|
||||
{
|
||||
return $this->contract_template_obligation()->get();
|
||||
return $this->contract_template_obligation()->get()->makeHidden(['contract_template_list']);
|
||||
}
|
||||
|
||||
public function getUpdatedAtAttribute($date)
|
||||
|
||||
@@ -2,10 +2,39 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use App\Http\Helpers\Hasher;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class ContractTemplateObligation extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $table = 'contract_template_obligations';
|
||||
|
||||
protected $appends = ['hash_id', 'contract_template_list'];
|
||||
|
||||
public function contract_template()
|
||||
{
|
||||
return $this->belongsTo('App\Models\ContractTemplate', 'contract_template_id');
|
||||
}
|
||||
|
||||
public function getHashIdAttribute()
|
||||
{
|
||||
return Hasher::encode('contract_template_obligations', $this->id);
|
||||
}
|
||||
|
||||
public function getContractTemplateListAttribute()
|
||||
{
|
||||
return $this->contract_template()->get()->makeHidden(['contract_template_obligation_list']);
|
||||
}
|
||||
|
||||
public function getUpdatedAtAttribute($date)
|
||||
{
|
||||
return $date ? Carbon::parse($date)->timezone('Asia/Kuala_Lumpur')->format('Y-m-d') : '';
|
||||
}
|
||||
|
||||
public function getCreatedAtAttribute($date)
|
||||
{
|
||||
return $date ? Carbon::parse($date)->timezone('Asia/Kuala_Lumpur')->format('Y-m-d') : '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ContractTemplateObligationLog extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
}
|
||||
Reference in New Issue
Block a user