TimeTrex Community Edition v16.2.0

This commit is contained in:
2022-12-13 07:10:06 +01:00
commit 472f000c1b
6810 changed files with 2636142 additions and 0 deletions

View File

@@ -0,0 +1,745 @@
<?php
/*********************************************************************************
*
* TimeTrex is a Workforce Management program developed by
* TimeTrex Software Inc. Copyright (C) 2003 - 2021 TimeTrex Software Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY TIMETREX, TIMETREX DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
*
* You should have received a copy of the GNU Affero General Public License along
* with this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
*
* You can contact TimeTrex headquarters at Unit 22 - 2475 Dobbin Rd. Suite
* #292 West Kelowna, BC V4T 2E9, Canada or at email address info@timetrex.com.
*
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by TimeTrex" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by TimeTrex".
*
********************************************************************************/
/**
* @package Modules\Request
*/
class RequestFactory extends Factory {
protected $table = 'request';
protected $pk_sequence_name = 'request_id_seq'; //PK Sequence name
var $user_date_obj = null;
/**
* @param $name
* @param null $parent
* @return array|null
*/
function _getFactoryOptions( $name, $parent = null ) {
$retval = null;
switch ( $name ) {
case 'type':
$retval = [
10 => TTi18n::gettext( 'Missed Punch' ), //request_punch
20 => TTi18n::gettext( 'Punch Adjustment' ), //request_punch_adjust
30 => TTi18n::gettext( 'Absence (incl. Vacation)' ), //request_absence
40 => TTi18n::gettext( 'Schedule Adjustment' ), //request_schedule
100 => TTi18n::gettext( 'Other' ), //request_other
];
break;
case 'status':
$retval = [
10 => TTi18n::gettext( 'INCOMPLETE' ),
20 => TTi18n::gettext( 'OPEN' ),
30 => TTi18n::gettext( 'PENDING' ), //Used to be "Pending Authorizion"
40 => TTi18n::gettext( 'AUTHORIZATION OPEN' ),
50 => TTi18n::gettext( 'AUTHORIZED' ), //Used to be "Active"
55 => TTi18n::gettext( 'DECLINED' ), //Used to be "AUTHORIZATION DECLINED"
60 => TTi18n::gettext( 'DISABLED' ),
];
break;
case 'columns':
$retval = [
'-1010-first_name' => TTi18n::gettext( 'First Name' ),
'-1020-last_name' => TTi18n::gettext( 'Last Name' ),
'-1060-title' => TTi18n::gettext( 'Title' ),
'-1070-user_group' => TTi18n::gettext( 'Group' ),
'-1080-default_branch' => TTi18n::gettext( 'Branch' ),
'-1090-default_department' => TTi18n::gettext( 'Department' ),
'-1110-date_stamp' => TTi18n::gettext( 'Date' ),
'-1120-status' => TTi18n::gettext( 'Status' ),
'-1130-type' => TTi18n::gettext( 'Type' ),
'-2000-created_by' => TTi18n::gettext( 'Created By' ),
'-2010-created_date' => TTi18n::gettext( 'Created Date' ),
'-2020-updated_by' => TTi18n::gettext( 'Updated By' ),
'-2030-updated_date' => TTi18n::gettext( 'Updated Date' ),
];
break;
case 'list_columns':
$retval = Misc::arrayIntersectByKey( [ 'date_stamp', 'status', 'type' ], Misc::trimSortPrefix( $this->getOptions( 'columns' ) ) );
break;
case 'default_display_columns': //Columns that are displayed by default.
$retval = [
'first_name',
'last_name',
'type',
'date_stamp',
'status',
];
break;
}
return $retval;
}
/**
* @param $data
* @return array
*/
function _getVariableToFunctionMap( $data ) {
$variable_function_map = [
'id' => 'ID',
//'user_date_id' => 'UserDateID',
'user_id' => 'User',
'date_stamp' => 'DateStamp',
'pay_period_id' => 'PayPeriod',
//'user_id' => FALSE,
'first_name' => false,
'last_name' => false,
'default_branch' => false,
'default_department' => false,
'user_group' => false,
'title' => false,
'type_id' => 'Type',
'type' => false,
'hierarchy_type_id' => 'HierarchyTypeId',
'status_id' => 'Status',
'status' => false,
'authorized' => 'Authorized',
'authorization_level' => 'AuthorizationLevel',
'message' => 'Message',
'request_schedule' => false,
'deleted' => 'Deleted',
];
return $variable_function_map;
}
/**
* @return bool
*/
function getUserObject() {
return $this->getGenericObject( 'UserListFactory', $this->getUser(), 'user_obj' );
}
/**
* @return bool|mixed
*/
function getUser() {
return $this->getGenericDataValue( 'user_id' );
}
/**
* @param string $value UUID
* @return bool
*/
function setUser( $value ) {
$value = TTUUID::castUUID( $value );
return $this->setGenericDataValue( 'user_id', $value );
}
/**
* @return bool|mixed
*/
function getPayPeriod() {
return $this->getGenericDataValue( 'pay_period_id' );
}
/**
* @param string $value UUID
* @return bool
*/
function setPayPeriod( $value = null ) {
if ( $value == null ) {
$value = PayPeriodListFactory::findPayPeriod( $this->getUser(), $this->getDateStamp() );
}
$value = TTUUID::castUUID( $value );
//Allow NULL pay period, incase its an absence or something in the future.
//Cron will fill in the pay period later.
return $this->setGenericDataValue( 'pay_period_id', $value );
}
/**
* @param bool $raw
* @return bool|int
*/
function getDateStamp( $raw = false ) {
$value = $this->getGenericDataValue( 'date_stamp' );
if ( $value !== false ) {
if ( $raw === true ) {
return $value;
} else {
return TTDate::strtotime( $value );
}
}
return false;
}
/**
* @param int $value EPOCH
* @return bool
*/
function setDateStamp( $value ) {
$value = (int)$value;
return $this->setGenericDataValue( 'date_stamp', $value );
}
/**
* Convert hierarchy type_ids back to request type_ids.
* @param int $type_id
* @return array|int
*/
function getTypeIdFromHierarchyTypeId( $type_id ) {
//Make sure we support an array of type_ids.
if ( is_array( $type_id ) ) {
foreach ( $type_id as $request_type_id ) {
$retval[] = ( $request_type_id >= 1000 && $request_type_id < 2000 ) ? ( (int)$request_type_id - 1000 ) : (int)$request_type_id;
}
} else {
$retval = ( $type_id >= 1000 && $type_id < 2000 ) ? ( (int)$type_id - 1000 ) : (int)$type_id;
Debug::text( 'Hierarchy Type ID: ' . $type_id . ' Request Type ID: ' . $retval, __FILE__, __LINE__, __METHOD__, 10 );
}
return $retval;
}
/**
* @param int $type_id ID
* @return array|bool|int
*/
function getHierarchyTypeId( $type_id = null ) {
if ( $type_id == '' ) {
$type_id = $this->getType();
}
if ( $type_id == false ) {
Debug::text( 'ERROR: Type ID is FALSE', __FILE__, __LINE__, __METHOD__, 10 );
return false;
}
//Make sure we support an array of type_ids.
if ( is_array( $type_id ) ) {
foreach ( $type_id as $request_type_id ) {
$retval[] = ( (int)$request_type_id + 1000 );
}
} else {
$retval = ( (int)$type_id + 1000 );
Debug::text( 'Request Type ID: ' . $type_id . ' Hierarchy Type ID: ' . $retval, __FILE__, __LINE__, __METHOD__, 10 );
}
return $retval;
}
/**
* @return bool|int
*/
function getType() {
return $this->getGenericDataValue( 'type_id' );
}
/**
* @param $value
* @return bool
*/
function setType( $value ) {
$value = (int)trim( $value );
return $this->setGenericDataValue( 'type_id', $value );
}
/**
* @return bool|int
*/
function getStatus() {
return $this->getGenericDataValue( 'status_id' );
}
/**
* @param $value
* @return bool
*/
function setStatus( $value ) {
$value = (int)trim( $value );
return $this->setGenericDataValue( 'status_id', $value );
}
/**
* @return bool|null
*/
function getAuthorized() {
return $this->fromBool( $this->getGenericDataValue( 'authorized' ) );
}
/**
* @param $value
* @return bool
*/
function setAuthorized( $value ) {
return $this->setGenericDataValue( 'authorized', $this->toBool( $value ) );
}
/**
* @return bool|mixed
*/
function getAuthorizationLevel() {
return $this->getGenericDataValue( 'authorization_level' );
}
/**
* @param $value
* @return bool
*/
function setAuthorizationLevel( $value ) {
$value = (int)trim( $value );
if ( $value < 0 ) {
$value = 0;
}
return $this->setGenericDataValue( 'authorization_level', $value );
}
/**
* @return bool
*/
function getMessage() {
return $this->getGenericTempDataValue( 'message' );
}
/**
* @param $value
* @return bool
*/
function setMessage( $value ) {
$value = trim( $value );
return $this->setGenericTempDataValue( 'message', htmlspecialchars( $value ) );
}
/**
* @return bool|array
*/
function getRequestSchedule() {
if ( $this->getUserObject()->getCompanyObject()->getProductEdition() >= TT_PRODUCT_PROFESSIONAL ) {
$rslf = TTNew( 'RequestScheduleListFactory' ); /** @var RequestScheduleListFactory $rslf */
$rslf->getAPISearchByCompanyIdAndArrayCriteria( $this->getUserObject()->getCompany(), [ 'request_id' => $this->getId() ] );
if ( $rslf->getRecordCount() == 1 ) {
foreach ( $rslf as $rs_obj ) {
$result = $rs_obj->getObjectAsArray();
Debug::Arr( $result, 'getRequestSchedule Result: ', __FILE__, __LINE__, __METHOD__, 10 );
return $result;
}
} else {
Debug::Text( 'Request Schedule rows: 0 ', __FILE__, __LINE__, __METHOD__, 10 );
}
}
return false;
}
/**
* @param bool $ignore_warning
* @return bool
*/
function Validate( $ignore_warning = true ) {
//
// BELOW: Validation code moved from set*() functions.
//
// User
$ulf = TTnew( 'UserListFactory' ); /** @var UserListFactory $ulf */
$this->Validator->isResultSetWithRows( 'user',
$ulf->getByID( $this->getUser() ),
TTi18n::gettext( 'Invalid Employee' )
);
if ( !is_object( $this->getUserObject() ) && $this->Validator->hasError( 'user_id' ) == false ) {
$this->Validator->isTRUE( 'user_id',
false,
TTi18n::gettext( 'Invalid Employee' ) );
}
if ( $this->getDeleted() == false ) { //Relax validation checks when deleting record, specifically to allow deleting records more than 1 year old that aren't authorized.
// Pay Period
if ( $this->getPayPeriod() !== false && $this->getPayPeriod() != TTUUID::getZeroID() ) {
$pplf = TTnew( 'PayPeriodListFactory' ); /** @var PayPeriodListFactory $pplf */
$this->Validator->isResultSetWithRows( 'pay_period',
$pplf->getByID( $this->getPayPeriod() ),
TTi18n::gettext( 'Invalid Pay Period' )
);
}
// Date
$this->Validator->isDate( 'date_stamp',
$this->getDateStamp(),
TTi18n::gettext( 'Incorrect date' ) . ' (a)'
);
if ( $this->Validator->isError( 'date_stamp' ) == false ) {
if ( $this->getDateStamp() > 0 ) {
$this->setPayPeriod(); //Force pay period to be set as soon as the date is.
} else {
$this->Validator->isTRUE( 'date_stamp',
false,
TTi18n::gettext( 'Incorrect date' ) . ' (b)'
);
}
}
if ( $this->getDateStamp() == false
&& $this->Validator->hasError( 'date_stamp' ) == false ) {
$this->Validator->isTRUE( 'date_stamp',
false,
TTi18n::gettext( 'Incorrect Date' ) . ' (c)' );
}
if ( $this->Validator->isError( 'date_stamp' ) == false && $this->getDateStamp() < ( time() - ( 86400 * 365 * 1 ) ) ) { //No more than 1 year in the past
$this->Validator->isTRUE( 'date_stamp',
false,
TTi18n::gettext( 'Date cannot be more than 1 year in the past' )
);
}
if ( $this->Validator->isError( 'date_stamp' ) == false && $this->getDateStamp() > ( time() + ( 86400 * 365 * 5 ) ) ) { //No more than 5 years in the future.
$this->Validator->isTRUE( 'date_stamp',
false,
TTi18n::gettext( 'Date cannot be more than 5 years in the future' )
);
}
//Make sure the user isn't entering requests before the employees hire or after termination date
if ( $this->Validator->isError( 'date_stamp' ) == false && $this->getDateStamp() != false && is_object( $this->getUserObject() ) ) {
if ( $this->getUserObject()->getHireDate() != '' && TTDate::getBeginDayEpoch( $this->getDateStamp() ) < TTDate::getBeginDayEpoch( $this->getUserObject()->getHireDate() ) ) {
$this->Validator->isTRUE( 'date_stamp',
false,
TTi18n::gettext( 'Date cannot be before your hire date' ) );
}
//Don't bother checking termination date, as it leak sensitive information.
}
// Type
$this->Validator->inArrayKey( 'type',
$this->getType(),
TTi18n::gettext( 'Incorrect Type' ),
$this->getOptions( 'type' )
);
// Status
if ( $this->getStatus() != false ) {
$this->Validator->inArrayKey( 'status',
$this->getStatus(),
TTi18n::gettext( 'Incorrect Status' ),
$this->getOptions( 'status' )
);
}
// Authorization level
if ( $this->getAuthorizationLevel() !== false ) {
$this->Validator->isNumeric( 'authorization_level',
$this->getAuthorizationLevel(),
TTi18n::gettext( 'Incorrect authorization level' )
);
}
if ( $this->getMessage() !== false ) {
// HTML interface validates the message too soon, make it skip a 0 length message when only validating.
if ( $this->Validator->getValidateOnly() == true && $this->getMessage() == '' ) {
$minimum_length = 0;
} else {
$minimum_length = 2;
}
$this->Validator->isLength( 'message',
$this->getMessage(),
TTi18n::gettext( 'Reason / Message is too short or too long' ),
$minimum_length,
10240
);
}
//
// ABOVE: Validation code moved from set*() functions.
//
//Check to make sure this user has superiors to send a request too, otherwise we can't save the request.
if ( is_object( $this->getUserObject() ) ) {
$hlf = TTnew( 'HierarchyListFactory' ); /** @var HierarchyListFactory $hlf */
$request_parent_level_user_ids = $hlf->getHierarchyParentByCompanyIdAndUserIdAndObjectTypeID( $this->getUserObject()->getCompany(), $this->getUser(), $this->getHierarchyTypeId(), true, false ); //Request - Immediate parents only.
//Debug::Arr( $request_parent_level_user_ids, 'Check for Superiors: ', __FILE__, __LINE__, __METHOD__, 10 );
if ( !is_array( $request_parent_level_user_ids ) || count( $request_parent_level_user_ids ) == 0 ) {
$this->Validator->isTRUE( 'message',
false,
TTi18n::gettext( 'No supervisors are assigned to you at this time, please try again later' ) );
}
}
//Check to make sure an authorized/declined request is not set back to pending status.
$data_diff = $this->getDataDifferences();
if ( $this->isDataDifferent( 'status_id', $data_diff ) == true && in_array( $data_diff['status_id'], [ 50, 55 ] ) && $this->getStatus() <= 30 ) {
$this->Validator->isTRUE( 'status_id',
false,
TTi18n::gettext( 'Request has already been authorized/declined' ) );
}
}
if ( $this->isNew() == true
&& $this->Validator->hasError( 'message' ) == false
&& $this->getMessage() == false
&& $this->Validator->getValidateOnly() == false ) {
$this->Validator->isTRUE( 'message',
false,
TTi18n::gettext( 'Reason / Message must be specified' ) );
}
if ( $this->getDeleted() == true && in_array( $this->getStatus(), [ 50, 55 ] ) ) {
$this->Validator->isTRUE( 'status_id',
false,
TTi18n::gettext( 'Unable to delete requests after they have been authorized/declined' ) );
}
return true;
}
/**
* @return bool
*/
function preSave() {
//If this is a new request, find the current authorization level to assign to it.
// isNew should be a force check due to request schedule child table
if ( $this->isNew( true ) == true ) {
if ( $this->getStatus() == false || $this->getStatus() < 30 ) { //10=INCOMPLETE, 20=OPEN. When upgrading from v10 to v11 if the browser cache isn't cleared the status_id comes through as 20. We saw some cases of it coming through as 10 too.
$this->setStatus( 30 ); //Pending Auth.
}
$hierarchy_highest_level = AuthorizationFactory::getInitialHierarchyLevel( ( is_object( $this->getUserObject() ) ? $this->getUserObject()->getCompany() : 0 ), ( is_object( $this->getUserObject() ) ? $this->getUserObject()->getID() : 0 ), $this->getHierarchyTypeId() );
$this->setAuthorizationLevel( $hierarchy_highest_level );
}
if ( $this->getAuthorized() == true ) {
$this->setAuthorizationLevel( 0 );
}
return true;
}
/**
* @return bool
*/
function postSave() {
//Save message here after we have the request_id.
if ( $this->getMessage() !== false ) {
$mcf = TTnew( 'MessageControlFactory' ); /** @var MessageControlFactory $mcf */
$mcf->StartTransaction();
$hlf = TTnew( 'HierarchyListFactory' ); /** @var HierarchyListFactory $hlf */
$request_parent_level_user_ids = $hlf->getHierarchyParentByCompanyIdAndUserIdAndObjectTypeID( $this->getUserObject()->getCompany(), $this->getUser(), $this->getHierarchyTypeId(), true, false ); //Request - Immediate parents only.
Debug::Arr( $request_parent_level_user_ids, 'Sending message to current direct Superiors: ', __FILE__, __LINE__, __METHOD__, 10 );
$mcf = TTnew( 'MessageControlFactory' ); /** @var MessageControlFactory $mcf */
$mcf->setFromUserId( $this->getUser() );
$mcf->setToUserId( $request_parent_level_user_ids );
$mcf->setObjectType( 50 ); //Messages don't break out request types like hierarchies do.
$mcf->setObject( $this->getID() );
$mcf->setParent( TTUUID::getZeroID() );
$mcf->setSubject( Option::getByKey( $this->getType(), $this->getOptions( 'type' ) ) . ' ' . TTi18n::gettext( 'request from' ) . ': ' . $this->getUserObject()->getFullName( true ) );
$mcf->setBody( $this->getMessage() );
$mcf->setEnableNotificationMessage( false ); //Dont send message notification, send authorization notification instead.
if ( $mcf->isValid() ) {
$mcf->Save();
$mcf->CommitTransaction();
} else {
$mcf->FailTransaction();
}
//Send initial Pending Authorization notification to superiors. -- This should only happen on first save by the regular employee.
AuthorizationFactory::sendNotificationAuthorizationOnInitialObjectSave( $this->getUser(), $this->getHierarchyTypeId(), $this->getId() );
}
if ( $this->getUserObject()->getCompanyObject()->getProductEdition() >= TT_PRODUCT_PROFESSIONAL ) {
if ( $this->getDeleted() == false && $this->getAuthorized() == true ) {
$rsf = TTNew( 'RequestScheduleFactory' ); /** @var RequestScheduleFactory $rsf */
$add_related_schedules_retval = $rsf->addRelatedSchedules( $this );
if ( $add_related_schedules_retval == false ) {
Debug::Text( ' addRelatedSchedules failed, passing along validation errors!', __FILE__, __LINE__, __METHOD__, 10 );
$this->Validator->Merge( $rsf->Validator );
}
unset( $rsf );
}
}
if ( $this->getDeleted() == true ) {
Debug::Text( 'Delete authorization history for this request...' . $this->getId(), __FILE__, __LINE__, __METHOD__, 10 );
$alf = TTnew( 'AuthorizationListFactory' ); /** @var AuthorizationListFactory $alf */
$alf->getByObjectTypeAndObjectId( $this->getHierarchyTypeId(), $this->getId() );
foreach ( $alf as $authorization_obj ) {
Debug::Text( 'Deleting authorization ID: ' . $authorization_obj->getID(), __FILE__, __LINE__, __METHOD__, 10 );
$authorization_obj->setDeleted( true );
$authorization_obj->Save();
}
}
return true;
}
/**
* @param $data
* @return bool
*/
function setObjectFromArray( $data ) {
if ( is_array( $data ) ) {
/*
if ( isset($data['user_id']) AND $data['user_id'] != ''
AND isset($data['date_stamp']) AND $data['date_stamp'] != '' ) {
Debug::text('Setting User Date ID based on User ID:'. $data['user_id'] .' Date Stamp: '. $data['date_stamp'], __FILE__, __LINE__, __METHOD__, 10);
$this->setUserDate( $data['user_id'], TTDate::parseDateTime( $data['date_stamp'] ) );
} elseif ( isset( $data['user_date_id'] ) AND $data['user_date_id'] > 0 ) {
Debug::text(' Setting UserDateID: '. $data['user_date_id'], __FILE__, __LINE__, __METHOD__, 10);
$this->setUserDateID( $data['user_date_id'] );
} else {
Debug::text(' NOT CALLING setUserDate or setUserDateID!', __FILE__, __LINE__, __METHOD__, 10);
}
*/
if ( isset( $data['status_id'] ) && $data['status_id'] == '' ) {
unset( $data['status_id'] );
$this->setStatus( 30 ); //Pending authorization
}
if ( isset( $data['user_date_id'] ) && $data['user_date_id'] == '' ) {
unset( $data['user_date_id'] );
}
$variable_function_map = $this->getVariableToFunctionMap();
foreach ( $variable_function_map as $key => $function ) {
if ( isset( $data[$key] ) ) {
$function = 'set' . $function;
switch ( $key ) {
case 'date_stamp':
$this->setDateStamp( TTDate::parseDateTime( $data['date_stamp'] ) );
break;
default:
if ( method_exists( $this, $function ) ) {
$this->$function( $data[$key] );
}
break;
}
}
}
$this->setCreatedAndUpdatedColumns( $data );
return true;
}
return false;
}
/**
* @param null $include_columns
* @param bool $permission_children_ids
* @return mixed
*/
function getObjectAsArray( $include_columns = null, $permission_children_ids = false ) {
$variable_function_map = $this->getVariableToFunctionMap();
if ( is_array( $variable_function_map ) ) {
foreach ( $variable_function_map as $variable => $function_stub ) {
if ( $include_columns == null || ( isset( $include_columns[$variable] ) && $include_columns[$variable] == true ) ) {
$function = 'get' . $function_stub;
switch ( $variable ) {
case 'first_name':
case 'last_name':
case 'title':
case 'user_group':
case 'default_branch':
case 'default_department':
case 'user_id':
$data[$variable] = $this->getColumn( $variable );
break;
case 'message': //Message is attached in the message factory, so we can't return it here.
break;
case 'status':
case 'type':
$function = 'get' . $variable;
if ( method_exists( $this, $function ) ) {
$data[$variable] = Option::getByKey( $this->$function(), $this->getOptions( $variable ) );
}
break;
case 'date_stamp':
$data[$variable] = TTDate::getAPIDate( 'DATE', $this->getDateStamp() );
break;
case 'request_schedule':
if ( $this->getUserObject()->getCompanyObject()->getProductEdition() >= TT_PRODUCT_PROFESSIONAL ) {
if ( $this->getType() == 30 || $this->getType() == 40 ) {
$request_schedule = $this->getRequestSchedule();
if ( $request_schedule != false && count( $request_schedule ) > 0 ) {
$data[$variable] = $request_schedule;
}
}
}
break;
default:
if ( method_exists( $this, $function ) ) {
$data[$variable] = $this->$function();
}
break;
}
}
}
$this->getPermissionColumns( $data, $this->getColumn( 'user_id' ), $this->getCreatedBy(), $permission_children_ids, $include_columns );
$this->getCreatedAndUpdatedColumns( $data, $include_columns );
}
return $data;
}
/**
* @param $log_action
* @return bool
*/
function addLog( $log_action ) {
return TTLog::addEntry( $this->getId(), $log_action, TTi18n::getText( 'Request - Employee' ) . ': ' . UserListFactory::getFullNameById( $this->getUser() ) . ' ' . TTi18n::getText( 'Type' ) . ': ' . Option::getByKey( $this->getType(), $this->getOptions( 'type' ) ) . ' ' . TTi18n::getText( 'Date' ) . ': ' . TTDate::getDate( 'DATE+TIME', $this->getDateStamp() ), null, $this->getTable(), $this );
}
}
?>

View File

@@ -0,0 +1,930 @@
<?php
/*********************************************************************************
*
* TimeTrex is a Workforce Management program developed by
* TimeTrex Software Inc. Copyright (C) 2003 - 2021 TimeTrex Software Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY TIMETREX, TIMETREX DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
*
* You should have received a copy of the GNU Affero General Public License along
* with this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
*
* You can contact TimeTrex headquarters at Unit 22 - 2475 Dobbin Rd. Suite
* #292 West Kelowna, BC V4T 2E9, Canada or at email address info@timetrex.com.
*
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by TimeTrex" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by TimeTrex".
*
********************************************************************************/
/**
* @package Modules\Request
*/
class RequestListFactory extends RequestFactory implements IteratorAggregate {
/**
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return $this
*/
function getAll( $limit = null, $page = null, $where = null, $order = null ) {
$query = '
select *
from ' . $this->getTable() . '
WHERE deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, null, $limit, $page );
return $this;
}
/**
* @param string $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getById( $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
$ph = [
'id' => TTUUID::castUUID( $id ),
];
$query = '
select *
from ' . $this->getTable() . '
where id = ?
AND deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param string $company_id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getByIdAndCompanyId( $id, $company_id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'id' => TTUUID::castUUID( $id ),
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select a.*,
a.date_stamp as date_stamp
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as c
where a.user_id = c.id
AND a.id = ?
AND c.company_id = ?
AND ( a.deleted = 0 AND c.deleted = 0 )';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getByCompanyId( $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'id' => TTUUID::castUUID( $id ),
];
$query = '
select a.*
from ' . $this->getTable() . ' as a
LEFT JOIN ' . $uf->getTable() . ' as uf ON a.user_id = uf.id
where uf.company_id = ?
AND ( a.deleted = 0 AND uf.deleted = 0 )';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $user_id UUID
* @param string $company_id UUID
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getByUserIdAndCompanyId( $user_id, $company_id, $limit = null, $page = null, $where = null, $order = null ) {
if ( $user_id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
if ( $order == null ) {
$order = [ 'a.status_id' => 'asc', 'a.date_stamp' => 'desc', 'a.type_id' => 'asc' ];
$strict = false;
} else {
$strict = true;
}
$uf = new UserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'user_id' => TTUUID::castUUID( $user_id ),
];
$query = '
select a.*,
a.date_stamp as date_stamp
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as c
where a.user_id = c.id
AND c.company_id = ?
AND a.user_id = ?
AND ( a.deleted = 0 AND c.deleted = 0 )';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
/**
* @param string $user_id UUID
* @param string $company_id UUID
* @param int $start_date EPOCH
* @param int $end_date EPOCH
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getByUserIdAndCompanyIdAndStartDateAndEndDate( $user_id, $company_id, $start_date, $end_date, $limit = null, $page = null, $where = null, $order = null ) {
if ( $user_id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
if ( $start_date == '' ) {
return false;
}
if ( $end_date == '' ) {
return false;
}
if ( $order == null ) {
$order = [ 'a.status_id' => 'asc', 'a.date_stamp' => 'desc', 'a.type_id' => 'asc' ];
$strict = false;
} else {
$strict = true;
}
$uf = new UserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'user_id' => TTUUID::castUUID( $user_id ),
'start_date' => $this->db->BindDate( $start_date ),
'end_date' => $this->db->BindDate( $end_date ),
];
$query = '
select a.*,
a.date_stamp as date_stamp
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as c
where a.user_id = c.id
AND c.company_id = ?
AND a.user_id = ?
AND a.date_stamp >= ?
AND a.date_stamp <= ?
AND ( a.deleted = 0 AND c.deleted = 0 )';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
/**
* @param string $company_id UUID
* @param string $user_id UUID
* @param int $status_id
* @param int $start_date EPOCH
* @param int $end_date EPOCH
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getByCompanyIdAndUserIdAndStatusAndStartDateAndEndDate( $company_id, $user_id, $status_id, $start_date, $end_date, $limit = null, $page = null, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $user_id == '' ) {
return false;
}
if ( $status_id == '' ) {
return false;
}
if ( $start_date == '' ) {
return false;
}
if ( $end_date == '' ) {
return false;
}
if ( $order == null ) {
//$order = array( 'type_id' => 'asc' );
$strict = false;
} else {
$strict = true;
}
$uf = new UserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'user_id' => TTUUID::castUUID( $user_id ),
'status_id' => (int)$status_id,
'start_date' => $this->db->BindDate( $start_date ),
'end_date' => $this->db->BindDate( $end_date ),
];
$query = '
select a.*,
a.date_stamp as date_stamp
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as c
where a.user_id = c.id
AND c.company_id = ?
AND a.user_id = ?
AND a.status_id = ?
AND a.date_stamp >= ?
AND a.date_stamp <= ?
AND ( a.deleted = 0 AND c.deleted = 0 ) ';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
/**
* @param string $ids UUID
* @param $status
* @param $level
* @param $max_level
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getByUserIdListAndStatusAndLevelAndMaxLevelAndNotAuthorized( $ids, $status, $level, $max_level, $limit = null, $page = null, $where = null, $order = null ) {
if ( $ids == '' ) {
return false;
}
if ( $status == '' ) {
return false;
}
if ( $level == '' ) {
return false;
}
if ( $max_level == '' ) {
return false;
}
$additional_sort_fields = [ 'date_stamp', 'user_id' ];
$strict_order = true;
if ( $order == null ) {
$order = [ 'a.user_id' => 'asc', 'a.date_stamp' => 'asc' ];
$strict_order = false;
}
$ph = [
'status' => $status,
'level' => $level,
'max_level' => $max_level,
];
$query = '
select a.*
from ' . $this->getTable() . ' as a
where a.status_id = ?
AND a.authorized = 0
AND ( a.authorization_level = ? OR a.authorization_level > ? )
AND a.user_id in (' . $this->getListSQL( $ids, $ph, 'uuid' ) . ')
AND ( a.deleted = 0 )
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict_order, $additional_sort_fields );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
/**
* @param $hierarchy_level_map
* @param $status
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getByHierarchyLevelMapAndStatusAndNotAuthorized( $hierarchy_level_map, $status, $limit = null, $page = null, $where = null, $order = null ) {
if ( $hierarchy_level_map == '' ) {
return false;
}
if ( $status == '' ) {
return false;
}
$additional_sort_fields = [ 'date_stamp', 'user_id' ];
$sort_column_aliases = [
'date_stamp' => 'date_stamp',
'user_id' => 'c.last_name',
];
$order = $this->getColumnsFromAliases( $order, $sort_column_aliases );
$strict_order = true;
if ( $order == null ) {
$order = [ 'a.type_id' => 'asc', 'a.date_stamp' => 'desc', 'c.last_name' => 'asc' ];
$strict_order = false;
}
$uf = new UserFactory();
$huf = new HierarchyUserFactory();
$ph = [
'status' => $status,
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as c,
' . $huf->getTable() . ' as z
where a.user_id = z.user_id
AND a.user_id = c.id
AND a.status_id = ?
AND a.authorized = 0
AND ( ' . HierarchyLevelFactory::convertHierarchyLevelMapToSQL( $hierarchy_level_map ) . ' )
AND ( a.deleted = 0 AND c.deleted = 0 )
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict_order, $additional_sort_fields );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
/**
* @param $hierarchy_level_map
* @param int $type_id
* @param $status
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getByHierarchyLevelMapAndTypeAndStatusAndNotAuthorized( $hierarchy_level_map, $type_id, $status, $limit = null, $page = null, $where = null, $order = null ) {
if ( $hierarchy_level_map == '' ) {
return false;
}
if ( $status == '' ) {
return false;
}
$additional_sort_fields = [ 'date_stamp', 'user_id' ];
$sort_column_aliases = [
'date_stamp' => 'date_stamp',
'user_id' => 'c.last_name',
];
$order = $this->getColumnsFromAliases( $order, $sort_column_aliases );
$strict_order = true;
if ( $order == null ) {
$order = [ 'a.date_stamp' => 'desc', 'c.last_name' => 'asc' ];
$strict_order = false;
}
$uf = new UserFactory();
$huf = new HierarchyUserFactory();
$ph = [
'status' => $status,
'type_id' => (int)$type_id,
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as c,
' . $huf->getTable() . ' as z
where a.user_id = z.user_id
AND a.user_id = c.id
AND a.status_id = ?
AND a.type_id = ?
AND a.authorized = 0
AND ( ' . HierarchyLevelFactory::convertHierarchyLevelMapToSQL( $hierarchy_level_map ) . ' )
AND ( a.deleted = 0 AND c.deleted = 0 )
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict_order, $additional_sort_fields );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
/**
* @param string $pay_period_id UUID
* @param $status
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return $this
*/
function getSumByPayPeriodIdAndStatus( $pay_period_id, $status, $where = null, $order = null ) {
$ph = [
'status_id' => $status,
];
$query = '
select a.pay_period_id as pay_period_id, count(*) as total
from ' . $this->getTable() . ' as a
where a.status_id = ?
AND a.pay_period_id in (' . $this->getListSQL( $pay_period_id, $ph, 'uuid' ) . ')
AND ( a.deleted = 0 )
GROUP By a.pay_period_id
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $company_id UUID
* @param string $pay_period_id UUID
* @param $status
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return $this
*/
function getSumByCompanyIDAndPayPeriodIdAndStatus( $company_id, $pay_period_id, $status, $where = null, $order = null ) {
$uf = new UserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'status_id' => $status,
];
$query = '
select a.pay_period_id as pay_period_id, count(*) as total
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as c
where a.user_id = c.id
AND c.company_id = ?
AND a.status_id = ?
AND a.pay_period_id in (' . $this->getListSQL( $pay_period_id, $ph, 'uuid' ) . ')
AND ( a.deleted = 0 )
GROUP By a.pay_period_id
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $pay_period_id UUID
* @param $status
* @param int $before_date EPOCH
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return int
*/
function getSumByPayPeriodIdAndStatusAndBeforeDate( $pay_period_id, $status, $before_date, $where = null, $order = null ) {
$ph = [
'pay_period_id' => TTUUID::castUUID( $pay_period_id ),
'status_id' => $status,
'before_date' => $this->db->BindDate( $before_date ),
];
$query = '
select count(*)
from ' . $this->getTable() . ' as a
where a.pay_period_id = ?
AND a.status_id = ?
AND a.date_stamp <= ?
AND ( a.deleted = 0 )
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
//$this->rs = $this->db->PageExecute($query, $limit, $page);
$total = $this->db->GetOne( $query, $ph );
if ( $total === false ) {
$total = 0;
}
Debug::text( 'Total: ' . $total, __FILE__, __LINE__, __METHOD__, 10 );
return $total;
}
/**
* @param string $company_id UUID
* @param array $filter_data
* @return int
*/
function getTotalPendingByCompanyIdAndHierarchyLevelMap( $company_id, $filter_data ) {
if ( $company_id == '' ) {
return false;
}
if ( $filter_data == '' ) {
return false;
}
$uf = new UserFactory();
$huf = new HierarchyUserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
];
//getTotalPendingByCompanyIdAndHierarchyLevelMap() and getAPISearchByCompanyIdAndArrayCriteria() share various query conditions.
//Make sure to update to both if any of the similar conditions change.
$query = '
SELECT
COUNT(DISTINCT a.*)
FROM ' . $this->getTable() . ' as a
LEFT JOIN ' . $uf->getTable() . ' as b ON ( a.user_id = b.id AND b.deleted = 0 )
LEFT JOIN ' . $huf->getTable() . ' as huf ON ( a.user_id = huf.user_id )
WHERE b.company_id = ?
';
$query .= ( isset( $filter_data['permission_children_ids'] ) ) ? $this->getWhereClauseSQL( 'a.user_id', $filter_data['permission_children_ids'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['type_id'] ) ) ? $this->getWhereClauseSQL( 'a.type_id', $filter_data['type_id'], 'numeric_list', $ph ) : null;
$query .= ( isset( $filter_data['status_id'] ) ) ? $this->getWhereClauseSQL( 'a.status_id', $filter_data['status_id'], 'numeric_list', $ph ) : null;
$query .= ( isset( $filter_data['authorized'] ) ) ? $this->getWhereClauseSQL( 'a.authorized', $filter_data['authorized'], 'numeric_list', $ph ) : null;
if ( isset( $filter_data['hierarchy_level_map'] ) && is_array( $filter_data['hierarchy_level_map'] ) ) {
$query .= ' AND huf.id IS NOT NULL '; //Make sure the user maps to a hierarchy.
//$query .= ' AND ( '. HierarchyLevelFactory::convertHierarchyLevelMapToSQL( $filter_data['hierarchy_level_map'], 'a.', 'huf.', 'a.type_id' ) .' )';
$hierarchy_level_sql = HierarchyLevelFactory::convertHierarchyLevelMapToSQL( $filter_data['hierarchy_level_map'], 'a.', 'huf.', 'a.type_id' );
if ( $hierarchy_level_sql != '' ) {
$query .= ' AND ( ' . $hierarchy_level_sql . ' )';
}
} else if ( isset( $filter_data['hierarchy_level_map'] ) && $filter_data['hierarchy_level_map'] == false ) {
//If hierarchy_level_map is not an array, don't return any requests.
//$query .= ' AND huf.id = -1 '; //Make sure the user maps to a hierarchy.
$query .= ' AND huf.id = \'' . TTUUID::getNotExistID() . '\''; //Make sure the user maps to a hierarchy.
}
$query .= '
AND a.deleted = 0
';
//Debug::Query( $query, $ph, __FILE__, __LINE__, __METHOD__, 10);
$pending_requests = (int)$this->db->GetOne( $query, $ph );
return $pending_requests;
}
/**
* @param string $company_id UUID
* @param $filter_data
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getByCompanyIdAndArrayCriteria( $company_id, $filter_data, $limit = null, $page = null, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $order == null ) {
$order = [ 'a.status_id' => 'asc', 'a.date_stamp' => 'desc', 'a.type_id' => 'asc' ];
$strict = false;
} else {
$strict = true;
}
$uf = new UserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
];
//getTotalPendingByCompanyIdAndHierarchyLevelMap() and getAPISearchByCompanyIdAndArrayCriteria() share various query conditions.
//Make sure to update to both if any of the similar conditions change.
$query = '
select a.*,
a.date_stamp as date_stamp
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as c
where a.user_id = c.id
AND c.company_id = ? ';
if ( isset( $filter_data['permission_children_ids'] ) && isset( $filter_data['permission_children_ids'][0] ) && !in_array( TTUUID::getNotExistID(), (array)$filter_data['permission_children_ids'] ) ) {
$query .= ' AND a.user_id in (' . $this->getListSQL( $filter_data['permission_children_ids'], $ph ) . ') ';
}
if ( isset( $filter_data['user_id'] ) && isset( $filter_data['user_id'][0] ) && !in_array( TTUUID::getNotExistID(), (array)$filter_data['user_id'] ) ) {
$query .= ' AND a.user_id in (' . $this->getListSQL( $filter_data['user_id'], $ph ) . ') ';
}
if ( isset( $filter_data['start_date'] ) && !is_array( $filter_data['start_date'] ) && trim( $filter_data['start_date'] ) != '' ) {
$ph[] = $this->db->BindDate( (int)TTDate::parseDateTime( $filter_data['start_date'] ) );
$query .= ' AND a.date_stamp >= ?';
}
if ( isset( $filter_data['end_date'] ) && !is_array( $filter_data['end_date'] ) && trim( $filter_data['end_date'] ) != '' ) {
$ph[] = $this->db->BindDate( (int)TTDate::parseDateTime( $filter_data['end_date'] ) );
$query .= ' AND a.date_stamp <= ?';
}
$query .= ' AND ( a.deleted = 0 AND c.deleted = 0 ) ';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
/**
* @param string $company_id UUID
* @param $filter_data
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|RequestListFactory
*/
function getAPISearchByCompanyIdAndArrayCriteria( $company_id, $filter_data, $limit = null, $page = null, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( !is_array( $order ) ) {
//Use Filter Data ordering if its set.
if ( isset( $filter_data['sort_column'] ) && $filter_data['sort_order'] ) {
$order = [ Misc::trimSortPrefix( $filter_data['sort_column'] ) => $filter_data['sort_order'] ];
}
}
$additional_order_fields = [ 'date_stamp', 'user_status_id', 'last_name', 'first_name', 'default_branch', 'default_department', 'user_group', 'title' ];
$sort_column_aliases = [
'status' => 'status_id',
'user_status_id' => 'b.status_id',
'type' => 'type_id',
];
$order = $this->getColumnsFromAliases( $order, $sort_column_aliases );
if ( $order == null ) {
//Sort by date_stamp ASC first, so most recent requests always appear at the top, then by type to try to keep similar requests together.
//However when no pending requests exist, the most recent request is at the end of the list. Unless we can do conditional sorting,
//always show most recent date at the top, even though its not ideal for pending requests.
// Additionally, if the employee is no longer active and its not a pending status, show at bottom of list.
$order = [ 'status_id' => 'asc', 'b.status_id' => 'asc', 'date_stamp' => 'desc', 'type_id' => 'asc', 'last_name' => 'asc' ];
$strict = false;
} else {
//Always sort by last name, first name after other columns
if ( !isset( $order['date_stamp'] ) ) {
$order['date_stamp'] = 'desc';
}
$strict = true;
}
//Debug::Arr($order, 'Order Data:', __FILE__, __LINE__, __METHOD__, 10);
//Debug::Arr($filter_data, 'Filter Data:', __FILE__, __LINE__, __METHOD__, 10);
$uf = new UserFactory();
$bf = new BranchFactory();
$df = new DepartmentFactory();
$ugf = new UserGroupFactory();
$utf = new UserTitleFactory();
$huf = new HierarchyUserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
];
//Need to make this return DISTINCT records only, because if the same child is assigned to multiple hierarchies,
//the join to table HUF will force it to return one row for each hierarchy they are a child of. This prevents that.
$query = '
select _ADODB_COUNT
DISTINCT
a.*,
b.status_id as user_status_id,
b.first_name as first_name,
b.last_name as last_name,
b.country as country,
b.province as province,
a.date_stamp as date_stamp,
a.user_id as user_id,
c.id as default_branch_id,
c.name as default_branch,
d.id as default_department_id,
d.name as default_department,
e.id as user_group_id,
e.name as user_group,
f.id as title_id,
f.name as title,
y.first_name as created_by_first_name,
y.middle_name as created_by_middle_name,
y.last_name as created_by_last_name,
z.first_name as updated_by_first_name,
z.middle_name as updated_by_middle_name,
z.last_name as updated_by_last_name
_ADODB_COUNT
from ' . $this->getTable() . ' as a
LEFT JOIN ' . $uf->getTable() . ' as b ON ( a.user_id = b.id AND b.deleted = 0 )
LEFT JOIN ' . $huf->getTable() . ' as huf ON ( a.user_id = huf.user_id )
LEFT JOIN ' . $bf->getTable() . ' as c ON ( b.default_branch_id = c.id AND c.deleted = 0)
LEFT JOIN ' . $df->getTable() . ' as d ON ( b.default_department_id = d.id AND d.deleted = 0)
LEFT JOIN ' . $ugf->getTable() . ' as e ON ( b.group_id = e.id AND e.deleted = 0 )
LEFT JOIN ' . $utf->getTable() . ' as f ON ( b.title_id = f.id AND f.deleted = 0 )
LEFT JOIN ' . $uf->getTable() . ' as y ON ( a.created_by = y.id AND y.deleted = 0 )
LEFT JOIN ' . $uf->getTable() . ' as z ON ( a.updated_by = z.id AND z.deleted = 0 )
where b.company_id = ?
';
$query .= ( isset( $filter_data['permission_children_ids'] ) ) ? $this->getWhereClauseSQL( 'a.user_id', $filter_data['permission_children_ids'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['id'] ) ) ? $this->getWhereClauseSQL( 'a.id', $filter_data['id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['exclude_id'] ) ) ? $this->getWhereClauseSQL( 'a.user_id', $filter_data['exclude_id'], 'not_uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['user_id'] ) ) ? $this->getWhereClauseSQL( 'a.user_id', $filter_data['user_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['type_id'] ) ) ? $this->getWhereClauseSQL( 'a.type_id', $filter_data['type_id'], 'numeric_list', $ph ) : null;
$query .= ( isset( $filter_data['status_id'] ) ) ? $this->getWhereClauseSQL( 'a.status_id', $filter_data['status_id'], 'numeric_list', $ph ) : null;
$query .= ( isset( $filter_data['group_id'] ) ) ? $this->getWhereClauseSQL( 'b.group_id', $filter_data['group_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['legal_entity_id'] ) ) ? $this->getWhereClauseSQL( 'b.legal_entity_id', $filter_data['legal_entity_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['default_branch_id'] ) ) ? $this->getWhereClauseSQL( 'b.default_branch_id', $filter_data['default_branch_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['default_department_id'] ) ) ? $this->getWhereClauseSQL( 'b.default_department_id', $filter_data['default_department_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['title_id'] ) ) ? $this->getWhereClauseSQL( 'b.title_id', $filter_data['title_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['country'] ) ) ? $this->getWhereClauseSQL( 'b.country', $filter_data['country'], 'upper_text_list', $ph ) : null;
$query .= ( isset( $filter_data['province'] ) ) ? $this->getWhereClauseSQL( 'b.province', $filter_data['province'], 'upper_text_list', $ph ) : null;
$query .= ( isset( $filter_data['authorized'] ) ) ? $this->getWhereClauseSQL( 'a.authorized', $filter_data['authorized'], 'numeric_list', $ph ) : null;
if ( isset( $filter_data['hierarchy_level_map'] ) && is_array( $filter_data['hierarchy_level_map'] ) ) {
$query .= ' AND huf.id IS NOT NULL '; //Make sure the user maps to a hierarchy.
//$query .= ' AND ( '. HierarchyLevelFactory::convertHierarchyLevelMapToSQL( $filter_data['hierarchy_level_map'], 'a.', 'huf.', 'a.type_id' ) .' )';
$hierarchy_level_sql = HierarchyLevelFactory::convertHierarchyLevelMapToSQL( $filter_data['hierarchy_level_map'], 'a.', 'huf.', 'a.type_id' );
if ( $hierarchy_level_sql != '' ) {
$query .= ' AND ( ' . $hierarchy_level_sql . ' )';
}
} else if ( isset( $filter_data['hierarchy_level_map'] ) && $filter_data['hierarchy_level_map'] == false ) {
//If hierarchy_level_map is not an array, don't return any requests.
//$query .= ' AND huf.id = -1 '; //Make sure the user maps to a hierarchy.
$query .= ' AND huf.id = \'' . TTUUID::getNotExistID() . '\''; //Make sure the user maps to a hierarchy.
}
if ( isset( $filter_data['start_date'] ) && !is_array( $filter_data['start_date'] ) && trim( $filter_data['start_date'] ) != '' ) {
$ph[] = $this->db->BindDate( (int)TTDate::parseDateTime( $filter_data['start_date'] ) );
$query .= ' AND a.date_stamp >= ?';
}
if ( isset( $filter_data['end_date'] ) && !is_array( $filter_data['end_date'] ) && trim( $filter_data['end_date'] ) != '' ) {
$ph[] = $this->db->BindDate( (int)TTDate::parseDateTime( $filter_data['end_date'] ) );
$query .= ' AND a.date_stamp <= ?';
}
$query .= ( isset( $filter_data['created_date_start'] ) ) ? $this->getWhereClauseSQL( 'a.created_date', $filter_data['created_date_start'], 'start_date', $ph ) : null;
$query .= ( isset( $filter_data['created_date_end'] ) ) ? $this->getWhereClauseSQL( 'a.created_date', $filter_data['created_date_end'], 'end_date', $ph ) : null;
$query .= ( isset( $filter_data['updated_date_start'] ) ) ? $this->getWhereClauseSQL( 'a.updated_date', $filter_data['updated_date_start'], 'start_date', $ph ) : null;
$query .= ( isset( $filter_data['updated_date_end'] ) ) ? $this->getWhereClauseSQL( 'a.updated_date', $filter_data['updated_date_end'], 'end_date', $ph ) : null;
$query .= ( isset( $filter_data['created_by'] ) ) ? $this->getWhereClauseSQL( [ 'a.created_by', 'y.first_name', 'y.last_name' ], $filter_data['created_by'], 'user_id_or_name', $ph ) : null;
$query .= ( isset( $filter_data['updated_by'] ) ) ? $this->getWhereClauseSQL( [ 'a.updated_by', 'z.first_name', 'z.last_name' ], $filter_data['updated_by'], 'user_id_or_name', $ph ) : null;
$query .= '
AND a.deleted = 0
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict, $additional_order_fields );
//Debug::Query( $query, $ph, __FILE__, __LINE__, __METHOD__, 10);
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
}
?>