Skip to content

Instantly share code, notes, and snippets.

@rajurayhan
Created August 23, 2021 05:52
Show Gist options
  • Save rajurayhan/0a68cd2a0e55c9749c52f6d764accf31 to your computer and use it in GitHub Desktop.
Save rajurayhan/0a68cd2a0e55c9749c52f6d764accf31 to your computer and use it in GitHub Desktop.
Approval Process Service For DSS
<?php
namespace App\Services;
use App\Libraries\WebApiResponse;
use App\Models\EmployeeInformation\ApprovalProcess\EmployeeApprovalData;
use App\Models\EmployeeInformation\Employee;
use App\Models\PayrollManagement\EmployeSalaryAllowancePolicy;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
class ApprovalSystemService{
public static $approvalModel;
private static $models = [
'jobJoiningInformation' => 'App\Models\EmployeeInformation\JobJoiningInformation',
'employee' => 'App\Models\EmployeeInformation\Employee',
'quotaInformation' => 'App\Models\EmployeeInformation\EmployeeQuota',
'presentAddress' => 'App\Models\EmployeeInformation\PresentAddress',
'permanentAddress' => 'App\Models\EmployeeInformation\PermanentAddress',
'educationQuality' => 'App\Models\EmployeeInformation\EducationalQualification',
'spouse' => 'App\Models\EmployeeInformation\Spouse',
'childrenInfo' => 'App\Models\EmployeeInformation\ChildInformation',
'nomineeInfo' => 'App\Models\EmployeeInformation\EmployeeNominee',
'languageInfo' => 'App\Models\EmployeeInformation\LanguageInformation',
'localTrainig' => 'App\Models\EmployeeInformation\LocalTraining',
'foreignTraining' => 'App\Models\EmployeeInformation\ForeignTraining',
'officialResidentialInfo' => 'App\OfficialResidential',
'foreignTravel' => 'App\Models\EmployeeInformation\ForeignTravel',
'additionalProfessionalQualifications' => 'App\Models\EmployeeInformation\AdditionalProfessionalQualification',
'publications' => 'App\Models\EmployeeInformation\Publication',
'honoursAndAward' => 'App\Models\EmployeeInformation\HonoursAward',
'desciplinaryAction' => 'App\Models\EmployeeInformation\DisciplinaryAction',
'reference' => 'App\Models\EmployeeInformation\Reference'
];
private static $subModules;
public static function init(){
self::$approvalModel = new EmployeeApprovalData();
self::$subModules = array_flip(self::$models);
}
public static function insertApprovalData($employee_id, $model, $requestData, $parent_id = NULL, $id = NULL){
try {
// For Direct Update on Approval Table with ID
if($id){
$existingDataQuery = self::$approvalModel::query();
$existingData = $existingDataQuery->find($id);
}
// Check if there is any pending record for this parent id
else{
$existingDataQuery = self::$approvalModel::query();
$existingDataQuery->where('employee_id', $employee_id);
$existingDataQuery->where('parent_id', $parent_id);
$existingDataQuery->where('model', $model);
$existingDataQuery->whereNull('approved_at');
$existingData = $existingDataQuery->first();
}
// Update if Data Exists
if($existingData && $requestData){
$existingData->data = $requestData;
$existingData->save();
}
// Else Create
else{
$existingData = self::$approvalModel::create([
'employee_id' => $employee_id,
'model' => $model,
'sub_module' => self::$subModules[$model],
'data' => $requestData,
'parent_id' => $parent_id
]);
}
return $existingData;
} catch (\Throwable $th) {
return false;
// return WebApiResponse::error(500, $errors = [$th->getMessage()], trans('messages.faild_show_all'));
}
}
public static function approveData($id){
try {
$existingDataQuery = self::$approvalModel::query();
$existingData = $existingDataQuery->whereNull('approved_at')->find($id);
if(!$existingData){
return false;
}
$data = json_decode($existingData->data , true); // Data To push
$model = $existingData->model; // Which Table to Push
$query = new $model;
$table = $query->getTable();
$columns = Schema::getColumnListing($table);
$dataToPush = [];
foreach($columns as $column){
if(isset($data[$column])){
$dataToPush[$column] = $data[$column];
}
}
// return $model;
$dataToUpdate = $model::updateOrCreate(
['id' => $existingData->parent_id ?? null],
$dataToPush
);
$existingData->approved_by = request()->user() ? request()->user() : 153;
$existingData->approved_at = Carbon::now();
$existingData->save();
// Customization For Job Joining Current
if($model == self::$models['jobJoiningInformation']){
$dataToPush['current'] = $data['current'] ?? false;
// Process Job Joining and Salary data
return self::processJobJoiningData($dataToPush);
}
return true;
} catch (\Throwable $th) {
// throw;
return WebApiResponse::error(500, $errors = [$th->getMessage()], trans('messages.faild_show_all'));
return 'false';
}
}
private static function processJobJoiningData($data){
$employee = Employee::find($data['employee_id']);
if($employee){
// $employee->present_basic_salary = $data['pay_scale'] ?? 0;
if(isset($data['current']) && $data['current'] == true){
$employee->present_basic_salary = $data['pay_scale'] ?? 0;
// Update Employee data
$employee->designation_id = $data['designation_id'] ?? NULL;
$employee->office_id = $data['office_id'] ?? NULL;
// Update Salary data
$salaryAllowancePolicy = EmployeSalaryAllowancePolicy::updateOrCreate(
['employee_id' => $employee->id, 'salary_allowance_id' => 23], // 23 for Basic Salary ID
['allowance_amount' => $data['pay_scale']]
);
}
$employee->save();
}
return true;
}
}
ApprovalSystemService::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment