diff --git a/app/Http/Controllers/User/Api/ContractTemplateObligationController.php b/app/Http/Controllers/User/Api/ContractTemplateObligationController.php new file mode 100644 index 0000000..70e093f --- /dev/null +++ b/app/Http/Controllers/User/Api/ContractTemplateObligationController.php @@ -0,0 +1,48 @@ +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(); + } +} diff --git a/app/Http/Rule/User/ExistCheck.php b/app/Http/Rule/User/ExistCheck.php index 720a9ff..d7e45dc 100644 --- a/app/Http/Rule/User/ExistCheck.php +++ b/app/Http/Rule/User/ExistCheck.php @@ -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; } diff --git a/app/Http/Rule/User/UnderCheck.php b/app/Http/Rule/User/UnderCheck.php index 7178e42..74341e3 100644 --- a/app/Http/Rule/User/UnderCheck.php +++ b/app/Http/Rule/User/UnderCheck.php @@ -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; } diff --git a/app/Models/ContractTemplate.php b/app/Models/ContractTemplate.php index adff273..caf7b90 100644 --- a/app/Models/ContractTemplate.php +++ b/app/Models/ContractTemplate.php @@ -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) diff --git a/app/Models/ContractTemplateObligation.php b/app/Models/ContractTemplateObligation.php index 0d7e409..77b53b3 100644 --- a/app/Models/ContractTemplateObligation.php +++ b/app/Models/ContractTemplateObligation.php @@ -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') : ''; + } } diff --git a/app/Models/ContractTemplateObligationLog.php b/app/Models/ContractTemplateObligationLog.php new file mode 100644 index 0000000..d3a40a9 --- /dev/null +++ b/app/Models/ContractTemplateObligationLog.php @@ -0,0 +1,11 @@ + 9 ] ], + 'contract_template_obligation' => [ + 'status' => [ + 'active' => 1, + 'delete' => 2, + 'pending' => 9 + ] + ], 'contract_obligation' => [ 'complete_status' => [ 'pending' => 9, diff --git a/database/migrations/2021_06_22_012630_create_contract_template_obligation_logs_table.php b/database/migrations/2021_06_22_012630_create_contract_template_obligation_logs_table.php new file mode 100644 index 0000000..8ce4187 --- /dev/null +++ b/database/migrations/2021_06_22_012630_create_contract_template_obligation_logs_table.php @@ -0,0 +1,36 @@ +id(); + $table->bigInteger('contract_template_obligation_id')->unsigned()->nullable(); + $table->foreign('contract_template_obligation_id', 'ctol_cto_id')->references('id')->on('contract_template_obligations')->onDelete('cascade'); + $table->json('history')->nullable(); + $table->text('remark')->nullable(); + $table->integer('status')->nullable()->default(1); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('contract_template_obligation_logs'); + } +} diff --git a/database/migrations/2021_06_22_012630_create_contract_template_obligation_dependencies_table.php b/database/migrations/2021_06_22_012631_create_contract_template_obligation_dependencies_table.php similarity index 100% rename from database/migrations/2021_06_22_012630_create_contract_template_obligation_dependencies_table.php rename to database/migrations/2021_06_22_012631_create_contract_template_obligation_dependencies_table.php diff --git a/routes/user/api.php b/routes/user/api.php index e1b4351..16d822f 100644 --- a/routes/user/api.php +++ b/routes/user/api.php @@ -52,6 +52,45 @@ Route::group(['namespace' => 'User', 'prefix' => 'user-backsys/api'], function ( ); }); + // Contract Templete Obligation + Route::prefix('contract-template-obligation')->group(function () { + Route::get('list', + [ + 'as' => 'api.contract-template-obligation.list', + 'uses' => 'ContractTemplateObligationController@list', + 'middleware' => ['permission:view contract_template_obligation'] + ] + ); + Route::get('show/{hash_id}', + [ + 'as' => 'api.contract-template-obligation.show', + 'uses' => 'ContractTemplateObligationController@show', + 'middleware' => ['permission:view contract_template_obligation'] + ] + ); + Route::post('store', + [ + 'as' => 'api.contract-template-obligation.store', + 'uses' => 'ContractTemplateObligationController@store', + 'middleware' => ['permission:add contract_template_obligation'] + ] + ); + Route::put('update/{hash_id}', + [ + 'as' => 'api.contract-template-obligation.update', + 'uses' => 'ContractTemplateObligationController@update', + 'middleware' => ['permission:edit contract_template_obligation'] + ] + ); + Route::delete('delete/{hash_id}', + [ + 'as' => 'api.contract-template-obligation.delete', + 'uses' => 'ContractTemplateObligationController@delete', + 'middleware' => ['permission:delete contract_template_obligation'] + ] + ); + }); + }); }); \ No newline at end of file