163 lines
5.1 KiB
PHP
163 lines
5.1 KiB
PHP
<?php
|
|
class ApprovalStatus{
|
|
|
|
const APP_ST_APPROVED = 1;
|
|
const APP_ST_REJECTED = 0;
|
|
|
|
private static $me = null;
|
|
|
|
private function __construct(){}
|
|
|
|
public static function getInstance(){
|
|
if(empty(self::$me)){
|
|
self::$me = new ApprovalStatus();
|
|
}
|
|
|
|
return self::$me;
|
|
}
|
|
|
|
|
|
|
|
public function isDirectApproval($employeeId){
|
|
$emp = new Employee();
|
|
$emp->Load("id = ?",array($employeeId));
|
|
if(empty($emp->approver1) && empty($emp->approver2) && empty($emp->approver3)){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function getResolvedStatuses($type, $id){
|
|
$employeeApproval = new EmployeeApproval();
|
|
$eas = $employeeApproval->Find("type = ? and element = ? and status > -1 order by level", array($type, $id));
|
|
return $eas;
|
|
}
|
|
|
|
public function approvalChainExists($type, $id){
|
|
$list = $this->getAllStatuses($type, $id);
|
|
return count($list) > 0;
|
|
}
|
|
|
|
public function getAllStatuses($type, $id){
|
|
$employeeApproval = new EmployeeApproval();
|
|
$eas = $employeeApproval->Find("type = ? and element = ? order by level", array($type, $id));
|
|
return $eas;
|
|
}
|
|
|
|
public function initializeApprovalChain($type, $id){
|
|
$element = new $type();
|
|
$element->Load("id = ?",array($id));
|
|
$employeeId = $element->employee;
|
|
|
|
for($i = 1; $i < 4; $i++){
|
|
$approver = $this->getApproverByLevel($i, $employeeId);
|
|
if(!empty($approver)){
|
|
$employeeApproval = new EmployeeApproval();
|
|
$employeeApproval->type = $type;
|
|
$employeeApproval->element = $id;
|
|
$employeeApproval->approver = $approver;
|
|
$employeeApproval->level = $i;
|
|
$employeeApproval->status = -1;
|
|
$employeeApproval->active = 0;
|
|
$employeeApproval->created = date("Y-m-d H:i:s");
|
|
$employeeApproval->updated = date("Y-m-d H:i:s");
|
|
$ok = $employeeApproval->Save();
|
|
if(!$ok){
|
|
LogManager::getInstance()->error("Error:".$employeeApproval->ErrorMsg());
|
|
}
|
|
}else{
|
|
LogManager::getInstance()->error("Approver is empty level:".$i);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public function updateApprovalStatus($type, $id, $currentEmployee, $status){
|
|
LogManager::getInstance()->error('updateApprovalStatus 1');
|
|
if(!$this->approvalChainExists($type, $id)){
|
|
LogManager::getInstance()->error('updateApprovalStatus 2');
|
|
return new IceResponse(IceResponse::SUCCESS, array(NULL, NULL));
|
|
}
|
|
|
|
if($status != 0 && $status != 1){
|
|
return new IceResponse(IceResponse::ERROR, "Invalid data");
|
|
}
|
|
|
|
$element = new $type();
|
|
$element->Load("id = ?",array($id));
|
|
$employeeId = $element->employee;
|
|
|
|
$eas = $this->getAllStatuses($type, $id);
|
|
$level = 0;
|
|
//check if the element is already rejected
|
|
foreach($eas as $ea){
|
|
if($ea->status == 0){
|
|
return new IceResponse(IceResponse::ERROR, "This item is already rejected");
|
|
}else if($ea->active == 1){
|
|
$level = intval($ea->level);
|
|
}
|
|
}
|
|
|
|
LogManager::getInstance()->error('level '.$level);
|
|
|
|
$currentAL = NULL;
|
|
if($level > 0){
|
|
$currentAL = new EmployeeApproval();
|
|
$currentAL->Load("type = ? and element = ? and level = ?",array($type, $id, $level));
|
|
}
|
|
|
|
$nextAL = null;
|
|
if($level < 3){
|
|
$nextAL = new EmployeeApproval();
|
|
$nextAL->Load("type = ? and element = ? and level = ?",array($type, $id, intval($level)+1));
|
|
|
|
LogManager::getInstance()->error('next AL '.print_r($nextAL,true));
|
|
if(empty($nextAL->id)){
|
|
$nextAL = NULL;
|
|
}
|
|
}
|
|
|
|
//Check if the current employee is allowed to approve
|
|
if($level > 0 && $currentEmployee != $currentAL->approver){
|
|
return new IceResponse(IceResponse::ERROR, "You are not allowed to approve or reject");
|
|
}
|
|
|
|
if(!empty($currentAL)){
|
|
//Now mark the approval status
|
|
$currentAL->status = $status;
|
|
$currentAL->Save();
|
|
}
|
|
|
|
if(!empty($nextAL)) {
|
|
foreach ($eas as $ea) {
|
|
if ($ea->id == $nextAL->id) {
|
|
$nextAL->active = 1;
|
|
$nextAL->Save();
|
|
} else {
|
|
$ea->active = 0;
|
|
$ea->Save();
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!empty($currentAL)){
|
|
$oldCurrAlId = $currentAL->id;
|
|
$currentAL = new EmployeeApproval();
|
|
$currentAL->Load("id = ?",array($oldCurrAlId));
|
|
}
|
|
|
|
|
|
return new IceResponse(IceResponse::SUCCESS, array($currentAL, $nextAL));
|
|
|
|
}
|
|
|
|
|
|
private function getApproverByLevel($level, $employeeId){
|
|
$emp = new Employee();
|
|
$emp->Load("id = ?",array($employeeId));
|
|
$approver = NULL;
|
|
$alevel = "approver".$level;
|
|
return $emp->$alevel;
|
|
}
|
|
|
|
} |