mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/unity.git
synced 2026-08-19 12:24:01 +00:00
contract generate
This commit is contained in:
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Modules\User;
|
||||
|
||||
use Auth;
|
||||
use App\Models\Contract;
|
||||
use App\Models\ContractLog;
|
||||
use App\Models\ContractTemplate;
|
||||
use App\Models\ContractObligation;
|
||||
|
||||
use App\Http\Modules\User\ContractObligationModule;
|
||||
use App\Http\Modules\User\ContractObligationDependencyModule;
|
||||
|
||||
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 ContractModule extends BaseController
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
General::storeCachePerviousQuery($request->query(), 'contract_query');
|
||||
|
||||
$name = $request->get('name', null);
|
||||
$status = $request->get('status', []);
|
||||
$page = $request->get('page', null);
|
||||
$limit = $request->get('limit', 10);
|
||||
|
||||
$query = Contract::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_list = $query->paginate($limit);
|
||||
|
||||
$extract = [];
|
||||
foreach ($contract_list as $key => $row) {
|
||||
$extract[] = $row;
|
||||
|
||||
}
|
||||
$pagination = General::paginationGenerator($contract_list, $params, $extract);
|
||||
|
||||
return $pagination;
|
||||
}
|
||||
|
||||
public static function show(Request $request, $hash_id)
|
||||
{
|
||||
$id = Hasher::decode('contracts', $hash_id);
|
||||
$user_id = Auth::guard('user-api')->user()->id;
|
||||
$contract = Contract::
|
||||
where('id', $id)
|
||||
->where('user_id', $user_id)
|
||||
->first();
|
||||
if ($contract) {
|
||||
$data = General::returnData($contract, '');
|
||||
return response()->json($data);
|
||||
}
|
||||
$data = General::returnNotFoundData();
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public static function generate(Request $request)
|
||||
{
|
||||
$validation = ContractModule::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);
|
||||
|
||||
$contract = new Contract();
|
||||
$contract->user_id = Auth::guard('user-api')->user()->id;
|
||||
$contract->contract_template_id = $contarct_template->id;
|
||||
$contract->name = $contarct_template->name;
|
||||
$contract->in_time_contract_template = json_encode($contarct_template);
|
||||
$contract->status = config('constants.contact.status.pending');
|
||||
$contract->save();
|
||||
|
||||
// generate contract obligation
|
||||
$contract_template_obligation_list = $contarct_template->contract_template_obligation()
|
||||
->orderBy('sequence', 'asc')
|
||||
->where('status', config('constants.contract_template_obligation.status.active'))
|
||||
->get();
|
||||
|
||||
foreach ($contract_template_obligation_list as $key => $row) {
|
||||
$request->request->add([
|
||||
'contract_template_obligation_id' => [$row->hash_id],
|
||||
'contract_id' => [$contract->hash_id]
|
||||
]);
|
||||
|
||||
$contract_obligation = ContractObligationModule::generate($request);
|
||||
}
|
||||
|
||||
// generate contract dependency
|
||||
foreach ($contract_template_obligation_list as $key => $row) {
|
||||
$contract_template_obligation_dependency = $row->contract_template_obligation_dependency()
|
||||
->where('status', config('constants.contract_template_obligation_dependency.status.active'))
|
||||
->get();
|
||||
|
||||
if (!empty($contract_template_obligation_dependency[0])) {
|
||||
$contract_obligation = ContractObligation::
|
||||
where('contract_id', $contract->id)
|
||||
->where('contract_template_obligation_id', $contract_template_obligation_dependency[0]->contract_template_obligation_id)
|
||||
->first();
|
||||
|
||||
$depend_contract_obligation = ContractObligation::
|
||||
where('contract_id', $contract->id)
|
||||
->where('contract_template_obligation_id', $contract_template_obligation_dependency[0]->depend_contract_template_obligation_id)
|
||||
->first();
|
||||
|
||||
$request->request->add([
|
||||
'contract_obligation_id' => [$contract_obligation->hash_id],
|
||||
'depend_contract_obligation_id' => [$depend_contract_obligation->hash_id]
|
||||
]);
|
||||
$contract_obligation_dependency = ContractObligationDependencyModule::generate($request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// log
|
||||
ContractModule::makeLog($contract->id, 'user (' . Auth::guard('user-api')->user()->id . ') created');
|
||||
|
||||
// DB::commit();
|
||||
|
||||
$query_string = General::getCachePerviousQuery('contract_query');
|
||||
$data = General::returnData($contract, $query_string);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public static function update(Request $request, $hash_id)
|
||||
{
|
||||
$id = Hasher::decode('contracts', $hash_id);
|
||||
$validation = ContractModule::validation($request, $hash_id, 'PUT');
|
||||
if (!$validation->status) {
|
||||
return response()->json($validation, 422);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
$name = $request->input('name');
|
||||
$complete_day_range = $request->input('complete_day_range');
|
||||
|
||||
$contract = Contract::find($id);
|
||||
$contract->name = $name ?? $contract->name;
|
||||
$contract->complete_day_range = $complete_day_range ?? $contract->complete_day_range;
|
||||
$contract->update();
|
||||
|
||||
|
||||
|
||||
|
||||
// log
|
||||
ContractModule::makeLog($contract->id, 'user (' . Auth::guard('user-api')->user()->id . ') updated');
|
||||
|
||||
DB::commit();
|
||||
|
||||
$query_string = General::getCachePerviousQuery('contract_query');
|
||||
$data = General::returnData($contract, $query_string);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public static function delete(Request $request, $hash_id)
|
||||
{
|
||||
$id = Hasher::decode('contracts', $hash_id);
|
||||
$validation = ContractModule::validation($request, $hash_id, 'DEL');
|
||||
if (!$validation->status) {
|
||||
return response()->json($validation, 422);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
$contract = Contract::find($id);
|
||||
$contract->status = config('constants.contact_template.status.delete');
|
||||
$contract->update();
|
||||
|
||||
// log
|
||||
ContractModule::makeLog($contract->id, 'user (' . Auth::guard('user-api')->user()->id . ') delete');
|
||||
|
||||
DB::commit();
|
||||
|
||||
$query_string = General::getCachePerviousQuery('contract_query');
|
||||
$data = General::returnData($contract, $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')]
|
||||
];
|
||||
}
|
||||
|
||||
if ($method == 'PUT') {
|
||||
$data['id'] = $id;
|
||||
$rule = [
|
||||
'id' => [new UnderCheck('contract'), new ExistCheck('contract')],
|
||||
'name' => 'required',
|
||||
'complete_day_range' => 'required|numeric|gt:-1'
|
||||
];
|
||||
}
|
||||
|
||||
if ($method == 'DEL') {
|
||||
$data['id'] = $id;
|
||||
$rule = [
|
||||
'id' => [new UnderCheck('contract'), new ExistCheck('contract')],
|
||||
];
|
||||
}
|
||||
|
||||
$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 = Contract::find($id);
|
||||
$contract_log = new ContractLog();
|
||||
$contract_log->contract_id = $contract->id;
|
||||
$contract_log->history = json_encode($contract);
|
||||
$contract_log->remark = $remark;
|
||||
$contract_log->save();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Modules\User;
|
||||
|
||||
use Auth;
|
||||
use App\Models\ContractObligationDependency;
|
||||
use App\Models\ContractObligation;
|
||||
|
||||
use App\Http\Modules\User\ContractObligationModule;
|
||||
|
||||
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 ContractObligationDependencyModule extends BaseController
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
General::storeCachePerviousQuery($request->query(), 'contract_query');
|
||||
|
||||
$name = $request->get('name', null);
|
||||
$status = $request->get('status', []);
|
||||
$page = $request->get('page', null);
|
||||
$limit = $request->get('limit', 10);
|
||||
|
||||
$query = ContractObligationDependency::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_list = $query->paginate($limit);
|
||||
|
||||
$extract = [];
|
||||
foreach ($contract_list as $key => $row) {
|
||||
$extract[] = $row;
|
||||
|
||||
}
|
||||
$pagination = General::paginationGenerator($contract_list, $params, $extract);
|
||||
|
||||
return $pagination;
|
||||
}
|
||||
|
||||
public static function show(Request $request, $hash_id)
|
||||
{
|
||||
$id = Hasher::decode('contracts', $hash_id);
|
||||
$user_id = Auth::guard('user-api')->user()->id;
|
||||
$contract = Contract::
|
||||
where('id', $id)
|
||||
->where('user_id', $user_id)
|
||||
->first();
|
||||
if ($contract) {
|
||||
$data = General::returnData($contract, '');
|
||||
return response()->json($data);
|
||||
}
|
||||
$data = General::returnNotFoundData();
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public static function generate(Request $request)
|
||||
{
|
||||
$validation = ContractObligationDependencyModule::validation($request);
|
||||
if (!$validation->status) {
|
||||
return response()->json($validation, 422);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
$contract_obligation_id = $request->input('contract_obligation_id');
|
||||
$contract_obligation_id = Hasher::decode('contract_obligations', $contract_obligation_id[0]);
|
||||
$contract_obligation = ContractObligation::find($contract_obligation_id);
|
||||
|
||||
$depend_contract_obligation_id = $request->input('depend_contract_obligation_id');
|
||||
$depend_contract_obligation_id = Hasher::decode('contract_obligations', $depend_contract_obligation_id[0]);
|
||||
$depend_contract_obligation = ContractObligation::find($depend_contract_obligation_id);
|
||||
|
||||
$contract_obligation_dependency = new ContractObligationDependency();
|
||||
$contract_obligation_dependency->user_id = Auth::guard('user-api')->user()->id;
|
||||
$contract_obligation_dependency->contract_obligation_id = $contract_obligation->id;
|
||||
$contract_obligation_dependency->depend_contract_obligation_id = $depend_contract_obligation->id;
|
||||
$contract_obligation_dependency->status = config('constants.contract_dependency.status.active');
|
||||
$contract_obligation_dependency->save();
|
||||
|
||||
// log
|
||||
ContractObligationModule::makeLog($contract_obligation->id, 'user (' . Auth::guard('user-api')->user()->id . ') created dependency');
|
||||
|
||||
DB::commit();
|
||||
|
||||
$query_string = General::getCachePerviousQuery('contract_query');
|
||||
$data = General::returnData($contract_obligation, $query_string);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
private static function validation(Request $request, $id = '', $method = 'POST')
|
||||
{
|
||||
$data = $request->all();
|
||||
$rule= [];
|
||||
|
||||
if ($method == 'POST') {
|
||||
|
||||
}
|
||||
|
||||
if ($method == 'PUT') {
|
||||
|
||||
}
|
||||
|
||||
if ($method == 'DEL') {
|
||||
$data['id'] = $id;
|
||||
$rule = [
|
||||
'id' => [new UnderCheck('contract'), new ExistCheck('contract')],
|
||||
];
|
||||
}
|
||||
|
||||
$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_obligation = ContractObligation::find($id);
|
||||
$contract_obligation_log = new ContractObligationLog();
|
||||
$contract_obligation_log->contract_obligation_id = $contract_obligation->id;
|
||||
$contract_obligation_log->history = json_encode($contract_obligation);
|
||||
$contract_obligation_log->remark = $remark;
|
||||
$contract_obligation_log->save();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Modules\User;
|
||||
|
||||
use Auth;
|
||||
use App\Models\ContractObligation;
|
||||
use App\Models\ContractObligationLog;
|
||||
use App\Models\Contract;
|
||||
use App\Models\ContractTemplateObligation;
|
||||
|
||||
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 ContractObligationModule extends BaseController
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
General::storeCachePerviousQuery($request->query(), 'contract_query');
|
||||
|
||||
$name = $request->get('name', null);
|
||||
$status = $request->get('status', []);
|
||||
$page = $request->get('page', null);
|
||||
$limit = $request->get('limit', 10);
|
||||
|
||||
$query = Contract::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_list = $query->paginate($limit);
|
||||
|
||||
$extract = [];
|
||||
foreach ($contract_list as $key => $row) {
|
||||
$extract[] = $row;
|
||||
|
||||
}
|
||||
$pagination = General::paginationGenerator($contract_list, $params, $extract);
|
||||
|
||||
return $pagination;
|
||||
}
|
||||
|
||||
public static function show(Request $request, $hash_id)
|
||||
{
|
||||
$id = Hasher::decode('contracts', $hash_id);
|
||||
$user_id = Auth::guard('user-api')->user()->id;
|
||||
$contract = Contract::
|
||||
where('id', $id)
|
||||
->where('user_id', $user_id)
|
||||
->first();
|
||||
if ($contract) {
|
||||
$data = General::returnData($contract, '');
|
||||
return response()->json($data);
|
||||
}
|
||||
$data = General::returnNotFoundData();
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public static function generate(Request $request)
|
||||
{
|
||||
$validation = ContractObligationModule::validation($request);
|
||||
if (!$validation->status) {
|
||||
return response()->json($validation, 422);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
$contract_id = $request->input('contract_id');
|
||||
$contract_id = Hasher::decode('contracts', $contract_id[0]);
|
||||
$contract = Contract::find($contract_id);
|
||||
|
||||
$contract_template_obligation_id = $request->input('contract_template_obligation_id');
|
||||
$contract_template_obligation_id = Hasher::decode('contract_template_obligations', $contract_template_obligation_id[0]);
|
||||
$contract_template_obligation = ContractTemplateObligation::find($contract_template_obligation_id);
|
||||
|
||||
$contract_obligation = new ContractObligation();
|
||||
$contract_obligation->user_id = Auth::guard('user-api')->user()->id;
|
||||
$contract_obligation->contract_id = $contract->id;
|
||||
$contract_obligation->contract_template_obligation_id = $contract_template_obligation->id;
|
||||
$contract_obligation->name = $contract_template_obligation->name;
|
||||
$contract_obligation->content = json_encode($contract_template_obligation->content);
|
||||
$contract_obligation->must_complete = $contract_template_obligation->must_complete;
|
||||
$contract_obligation->must_accept = $contract_template_obligation->must_accept;
|
||||
$contract_obligation->in_time_contract_obligation_template = json_encode($contract_template_obligation);
|
||||
$contract_obligation->type = $contract_template_obligation->type;
|
||||
$contract_obligation->status = config('constants.contact.status.pending');
|
||||
$contract_obligation->save();
|
||||
|
||||
// log
|
||||
ContractObligationModule::makeLog($contract_obligation->id, 'user (' . Auth::guard('user-api')->user()->id . ') created');
|
||||
|
||||
DB::commit();
|
||||
|
||||
$query_string = General::getCachePerviousQuery('contract_query');
|
||||
$data = General::returnData($contract, $query_string);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
private static function validation(Request $request, $id = '', $method = 'POST')
|
||||
{
|
||||
$data = $request->all();
|
||||
$rule= [];
|
||||
|
||||
if ($method == 'POST') {
|
||||
|
||||
}
|
||||
|
||||
if ($method == 'PUT') {
|
||||
|
||||
}
|
||||
|
||||
if ($method == 'DEL') {
|
||||
$data['id'] = $id;
|
||||
$rule = [
|
||||
'id' => [new UnderCheck('contract_obligation'), new ExistCheck('contract_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_obligation = ContractObligation::find($id);
|
||||
$contract_obligation_log = new ContractObligationLog();
|
||||
$contract_obligation_log->contract_obligation_id = $contract_obligation->id;
|
||||
$contract_obligation_log->history = json_encode($contract_obligation);
|
||||
$contract_obligation_log->remark = $remark;
|
||||
$contract_obligation_log->save();
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,10 @@ class ContractTemplateModule extends BaseController
|
||||
public static function show(Request $request, $hash_id)
|
||||
{
|
||||
$id = Hasher::decode('contract_templates', $hash_id);
|
||||
$contract_template = ContractTemplate::find($id);
|
||||
$contract_template = ContractTemplate::
|
||||
where('id', $id)
|
||||
->where('user_id', $user_id)
|
||||
->first();
|
||||
if ($contract_template) {
|
||||
$data = General::returnData($contract_template, '');
|
||||
return response()->json($data);
|
||||
|
||||
Reference in New Issue
Block a user