385 lines
12 KiB
PHP
385 lines
12 KiB
PHP
<?php
|
|
|
|
namespace Aiko;
|
|
|
|
use Exception;
|
|
|
|
/**
|
|
* ApprovalRequest class is wrapper for approval trait.
|
|
*
|
|
* the benefit of using this class
|
|
*
|
|
* no need to pass the main table name frequently. it handled in construction process
|
|
* every method safely handled by try catch.
|
|
* every transaction method using db transaction
|
|
*
|
|
* if there is a case to handle multiple approval with different main table name, use as follow inside constructor
|
|
*
|
|
* private $approvalRequestA;
|
|
* private $approvalRequestB;
|
|
*
|
|
* $this-approvalRequestA = new AppovalRequest($registry, $patternObj, 'my_main_approval_table_a', 'my_module_name');
|
|
* $this-approvalRequestB = new AppovalRequest($registry, $patternObj, 'my_main_approval_table_b', 'my_module_name');
|
|
*
|
|
*/
|
|
class ApprovalRequest
|
|
{
|
|
use ApprovalRequestTrait;
|
|
|
|
protected $registry;
|
|
|
|
protected $moduleName = 'ApprovalRequest';
|
|
|
|
protected $tableName;
|
|
|
|
/** @var \modules\approvalpattern\model\Approvalpattern $approvalPatternObj */
|
|
protected $approvalPatternObj;
|
|
|
|
public function __construct($registry, $approvalPatternObj, $tableName, $moduleName = '')
|
|
{
|
|
$this->registry = $registry;
|
|
$this->approvalPatternObj = $approvalPatternObj;
|
|
$this->tableName = $tableName;
|
|
$this->moduleName = $moduleName;
|
|
}
|
|
|
|
/**
|
|
* Approval create
|
|
*
|
|
* Please handle try catch error in implementation,
|
|
* because this always combined with other logic.
|
|
*
|
|
* @uses ApprovalPattern@setApproval
|
|
*
|
|
* @param mixed $transactionId
|
|
* @param string $requestBy
|
|
* @param string $action
|
|
* @param string $startDate is from setApproval
|
|
* @param string $endDate is from setApproval
|
|
* @param string $effectiveDate is effective by for request by. leave it null if only need latest active employee until this day
|
|
*
|
|
*/
|
|
public function create(
|
|
$requestBy,
|
|
$transactionId,
|
|
$action,
|
|
$startDate = '1970-01-01',
|
|
$endDate = '1970-01-01',
|
|
$effectiveDate = null
|
|
) {
|
|
return $this->createApprovalRequest(
|
|
$this->approvalPatternObj,
|
|
$this->tableName,
|
|
$requestBy,
|
|
$transactionId,
|
|
$action,
|
|
$startDate,
|
|
$endDate,
|
|
$effectiveDate
|
|
);
|
|
}
|
|
|
|
/**
|
|
* This method used for approve request.
|
|
* it already handle transaction within try-catch
|
|
*
|
|
* @param mixed $transactionId is an id of main table
|
|
* @param string $requestBy
|
|
*
|
|
*/
|
|
public function approve(
|
|
$requestBy,
|
|
$transactionId,
|
|
$startDateAs = '1970-01-01',
|
|
$endDateAs = '1970-01-01'
|
|
) {
|
|
try {
|
|
$this->registry->db->beginTransaction();
|
|
$approve = $this->approveRequest($this->approvalPatternObj, $this->tableName, $requestBy, $transactionId, $startDateAs, $endDateAs);
|
|
if ($approve == false) {
|
|
throw new Exception("Failed approval");
|
|
}
|
|
if ($approve['isSuccess'] == 0) {
|
|
$this->registry->db->rollBack();
|
|
return $approve;
|
|
}
|
|
$this->registry->db->commit();
|
|
return $approve;
|
|
} catch (Exception $e) {
|
|
$this->registry->db->rollback();
|
|
$this->registry
|
|
->log
|
|
->error("$this->moduleName / action : approve"
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine() . ', User: '
|
|
. \Helper::getSessionVar('username'));
|
|
return array(
|
|
'isSuccess' => 0,
|
|
'isLastApproval' => -1,
|
|
'approval_id' => $requestBy,
|
|
'message' => 'Internal server error'
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This method used for reject request
|
|
* it already handle transaction within try-catch
|
|
*
|
|
* @param mixed $transactionId is an id of main table
|
|
* @param string $requestBy
|
|
* @param string $comment is reject comment
|
|
*
|
|
*/
|
|
public function reject(
|
|
$requestBy,
|
|
$transactionId,
|
|
$comment = '-',
|
|
$startDateAs = '1970-01-01',
|
|
$endDateAs = '1970-01-01'
|
|
) {
|
|
try {
|
|
$this->registry->db->beginTransaction();
|
|
$reject = $this->rejectRequest($this->approvalPatternObj, $this->tableName, $requestBy, $transactionId, $comment, $startDateAs, $endDateAs);
|
|
if ($reject == false) {
|
|
throw new Exception("Error reject", 1);
|
|
}
|
|
if (!$reject['isSuccess']) {
|
|
$this->registry->db->rollBack();
|
|
return $reject;
|
|
}
|
|
$this->registry->db->commit();
|
|
return $reject;
|
|
} catch (Exception $e) {
|
|
$this->registry->db->rollback();
|
|
$this->registry
|
|
->log
|
|
->error("$this->moduleName / action : reject"
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine() . ', User: '
|
|
. \Helper::getSessionVar('username'));
|
|
return array(
|
|
'isSuccess' => 0,
|
|
'isLastApproval' => -1,
|
|
'approval_id' => $transactionId,
|
|
'message' => 'Internal server error'
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This method used for approve multiple request.
|
|
*
|
|
* @param string $requestBy
|
|
* @param array<mixed> $transactionIds is array id of main table
|
|
*
|
|
* format of $transactionIds is [1, 2, 3]
|
|
* use Helper::getArrayValueByKey($myArrayOrObject, 'id') to extract only id from multidimentional array object
|
|
*
|
|
*/
|
|
public function batchApprove(
|
|
$requestBy,
|
|
$transactionIds = [],
|
|
$startDateAs = '1970-01-01',
|
|
$endDateAs = '1970-01-01'
|
|
) {
|
|
$success = [];
|
|
$failed = [];
|
|
$ctr = count($transactionIds);
|
|
for ($i = 0; $i < $ctr; $i++) {
|
|
$result = $this->approve($requestBy, $transactionIds[$i], $startDateAs, $endDateAs);
|
|
if ($result['isSuccess'] == 1) {
|
|
array_push($success, $result);
|
|
} else {
|
|
array_push($failed, $result);
|
|
}
|
|
}
|
|
return ['success' => $success, 'failed' => $failed];
|
|
}
|
|
|
|
/**
|
|
* This method used for reject multiple request
|
|
*
|
|
* @param string $requestBy
|
|
* @param array<mixed> $transactionIds is array id of main table
|
|
* @param string $comment is reject comment
|
|
*
|
|
* format of $transactionIds is [1, 2, 3]
|
|
* use Helper::getArrayValueByKey($myArrayOrObject, 'id') to extract only id from multidimentional array object
|
|
*
|
|
*/
|
|
public function batchReject(
|
|
$requestBy,
|
|
$transactionIds = [],
|
|
$comment = '',
|
|
$startDateAs = '1970-01-01',
|
|
$endDateAs = '1970-01-01'
|
|
) {
|
|
$success = [];
|
|
$failed = [];
|
|
$ctr = count($transactionIds);
|
|
for ($i = 0; $i < $ctr; $i++) {
|
|
$result = $this->reject($requestBy, $transactionIds[$i], $comment, $startDateAs, $endDateAs);
|
|
if ($result['isSuccess'] == 1) {
|
|
array_push($success, $result);
|
|
} else {
|
|
array_push($failed, $result);
|
|
}
|
|
}
|
|
return ['success' => $success, 'failed' => $failed];
|
|
}
|
|
|
|
/**
|
|
* This method used for unapprove multiple request
|
|
*
|
|
* @param string $requestBy
|
|
* @param array<mixed> $transactionIds is array id of main table
|
|
*
|
|
* format of $transactionIds is [1, 2, 3]
|
|
* use Helper::getArrayValueByKey($myArrayOrObject, 'id') to extract only id from multidimentional array object
|
|
*
|
|
*/
|
|
public function batchUnapprove(
|
|
$requestBy,
|
|
$transactionIds = []
|
|
) {
|
|
$success = [];
|
|
$failed = [];
|
|
$ctr = count($transactionIds);
|
|
for ($i = 0; $i < $ctr; $i++) {
|
|
$result = $this->unApprove($requestBy, $transactionIds[$i]);
|
|
if ($result['isSuccess'] == 1) {
|
|
array_push($success, $result);
|
|
} else {
|
|
array_push($failed, $result);
|
|
}
|
|
}
|
|
return ['success' => $success, 'failed' => $failed];
|
|
}
|
|
|
|
|
|
/**
|
|
* This method used for unReject multiple request
|
|
*
|
|
* @param string $requestBy
|
|
* @param array<mixed> $transactionIds is array id of main table
|
|
*
|
|
* format of $transactionIds is [1, 2, 3]
|
|
* use Helper::getArrayValueByKey($myArrayOrObject, 'id') to extract only id from multidimentional array object
|
|
*
|
|
*/
|
|
public function batchUnreject(
|
|
$requestBy,
|
|
$transactionIds = []
|
|
) {
|
|
return $this->batchUnapprove($requestBy, $transactionIds);
|
|
}
|
|
|
|
|
|
/**
|
|
* This method used for unapprove request
|
|
* it already handle transaction within try-catch
|
|
*
|
|
* @param string $requestBy is usually the login user
|
|
* @param mixed $transactionId is an id of main table
|
|
*
|
|
*/
|
|
public function unApprove($requestBy, $transactionId)
|
|
{
|
|
try {
|
|
$this->registry->db->beginTransaction();
|
|
$approval = $this->unapproveRequest($this->approvalPatternObj, $this->tableName, $requestBy, $transactionId);
|
|
if ($approval == false) {
|
|
throw new Exception("Failed unapprove");
|
|
}
|
|
if ($approval['isSuccess'] == 0) {
|
|
$this->registry->db->rollBack();
|
|
return $approval;
|
|
}
|
|
$this->registry->db->commit();
|
|
return $approval;
|
|
} catch (Exception $e) {
|
|
$this->registry->db->rollback();
|
|
$this->registry
|
|
->log
|
|
->error("$this->moduleName / action : unApprove"
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine() . ', User: '
|
|
. \Helper::getSessionVar('username'));
|
|
return array(
|
|
'isSuccess' => 0,
|
|
'is_last_approval_approved' => -1,
|
|
'message' => 'Internal server error'
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This method used for unreject request
|
|
* it already handle transaction within try-catch
|
|
*
|
|
* @param string $requestBy is usually the login user
|
|
* @param mixed $transactionId is an id of main table
|
|
*
|
|
*/
|
|
public function unReject($requestBy, $transactionId)
|
|
{
|
|
try {
|
|
$this->registry->db->beginTransaction();
|
|
$approval = $this->unrejectRequest($this->approvalPatternObj, $this->tableName, $transactionId, $requestBy);
|
|
if ($approval == false) {
|
|
throw new Exception("Failed unApprove");
|
|
}
|
|
if ($approval['isSuccess'] == 0) {
|
|
$this->registry->db->rollBack();
|
|
return $approval;
|
|
}
|
|
$this->registry->db->commit();
|
|
return $approval;
|
|
} catch (Exception $e) {
|
|
$this->registry->db->rollback();
|
|
$this->registry
|
|
->log
|
|
->error("$this->moduleName / action : unReject"
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine() . ', User: '
|
|
. \Helper::getSessionVar('username'));
|
|
return array(
|
|
'isSuccess' => 0,
|
|
'is_last_approval_approved' => -1,
|
|
'message' => 'Internal server error'
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This method used for fetch latest approval from db
|
|
*
|
|
* @param mixed $transactionId
|
|
* @param bool $sigle
|
|
*
|
|
* if single true the return will be single array, if false will be multi dimensional array
|
|
*
|
|
*/
|
|
public function latestApproval($transactionId, $single = false)
|
|
{
|
|
$data = $this->getLatestApproval($this->tableName, $transactionId);
|
|
if ($single) {
|
|
return count($data) > 0 ? $data[0] : [];
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
|
|
/**
|
|
* This method used for fetch info styled approval
|
|
*
|
|
* @param mixed $transactionId
|
|
*
|
|
*/
|
|
public function infoApproval($transactionId)
|
|
{
|
|
return $this->getApprovalList($this->tableName . "_approval", $this->tableName . "_id", $transactionId);
|
|
}
|
|
}
|