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,859 @@
<?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\Department
*/
class DepartmentFactory extends Factory {
protected $table = 'department';
protected $pk_sequence_name = 'department_id_seq'; //PK Sequence name
/**
* @param $name
* @param null $parent
* @return array|null
*/
function _getFactoryOptions( $name, $parent = null ) {
$retval = null;
switch ( $name ) {
case 'status':
$retval = [
10 => TTi18n::gettext( 'ENABLED' ),
20 => TTi18n::gettext( 'DISABLED' ),
];
break;
case 'user_group_selection_type_id':
$retval = [
10 => TTi18n::gettext( 'All Groups' ),
20 => TTi18n::gettext( 'Only Selected Groups' ),
30 => TTi18n::gettext( 'All Except Selected Groups' ),
];
break;
case 'user_title_selection_type_id':
$retval = [
10 => TTi18n::gettext( 'All Titles' ),
20 => TTi18n::gettext( 'Only Selected Titles' ),
30 => TTi18n::gettext( 'All Except Selected Titles' ),
];
break;
case 'user_punch_branch_selection_type_id':
$retval = [
10 => TTi18n::gettext( 'All Punch Branches' ),
20 => TTi18n::gettext( 'Only Selected Punch Branches' ),
30 => TTi18n::gettext( 'All Except Selected Punch Branches' ),
];
break;
case 'user_default_department_selection_type_id':
$retval = [
10 => TTi18n::gettext( 'All Default Departments' ),
20 => TTi18n::gettext( 'Only Selected Default Departments' ),
30 => TTi18n::gettext( 'All Except Selected Default Departments' ),
];
break;
case 'columns':
$retval = [
'-1010-status' => TTi18n::gettext( 'Status' ),
'-1020-manual_id' => TTi18n::gettext( 'Code' ),
'-1030-name' => TTi18n::gettext( 'Name' ),
'-1300-tag' => TTi18n::gettext( 'Tags' ),
'-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' ),
];
$retval = $this->getCustomFieldsColumns( $retval, null );
break;
case 'list_columns':
$retval = Misc::arrayIntersectByKey( $this->getOptions( 'default_display_columns' ), Misc::trimSortPrefix( $this->getOptions( 'columns' ) ) );
break;
case 'default_display_columns': //Columns that are displayed by default.
$retval = [
'manual_id',
'name',
];
break;
case 'unique_columns': //Columns that are unique, and disabled for mass editing.
$retval = [
'name',
'manual_id',
];
}
return $retval;
}
/**
* @param $data
* @return array
*/
function _getVariableToFunctionMap( $data ) {
$variable_function_map = [
'id' => 'ID',
'company_id' => 'Company',
'status_id' => 'Status',
'status' => false,
'manual_id' => 'ManualID',
'name' => 'Name',
'name_metaphone' => 'NameMetaphone',
'geo_fence_ids' => 'GEOFenceIds',
'tag' => 'Tag',
'user_group_selection_type_id' => 'UserGroupSelectionType',
'user_group_ids' => 'UserGroup',
'include_user_ids' => 'IncludeUser',
'exclude_user_ids' => 'ExcludeUser',
'user_title_selection_type_id' => 'UserTitleSelectionType',
'user_title_ids' => 'UserTitle',
'user_punch_branch_selection_type_id' => 'UserPunchBranchSelectionType',
'user_punch_branch_ids' => 'UserPunchBranch',
'user_default_department_selection_type_id' => 'UserDefaultDepartmentSelectionType',
'user_default_department_ids' => 'UserDefaultDepartment',
'include_user_default_department_id' => 'IncludeUserDefaultDepartment',
'deleted' => 'Deleted',
];
return $variable_function_map;
}
/**
* @return mixed
*/
function getCompany() {
return $this->getGenericDataValue( 'company_id' );
}
/**
* @param string $value UUID
* @return bool
*/
function setCompany( $value ) {
$value = TTUUID::castUUID( $value );
return $this->setGenericDataValue( 'company_id', $value );
}
/**
* @return int
*/
function getStatus() {
//Have to return the KEY because it should always be a drop down box.
//return Option::getByKey($this->data['status_id'], $this->getOptions('status') );
return $this->getGenericDataValue( 'status_id' );
}
/**
* @param $value
* @return bool
*/
function setStatus( $value ) {
$value = (int)trim( $value );
return $this->setGenericDataValue( 'status_id', $value );
}
/**
* @param string $id UUID
* @return bool
*/
function isUniqueManualID( $id ) {
if ( $this->getCompany() == false ) {
return false;
}
$ph = [
'manual_id' => $id,
'company_id' => $this->getCompany(),
];
$query = 'select id from ' . $this->getTable() . ' where manual_id = ? AND company_id = ? AND deleted=0';
$id = $this->db->GetOne( $query, $ph );
Debug::Arr( $id, 'Unique Department: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
if ( $id === false ) {
return true;
} else {
if ( $id == $this->getId() ) {
return true;
}
}
return false;
}
/**
* @param string $company_id UUID
* @return int
*/
function getNextAvailableManualId( $company_id = null ) {
global $current_company;
if ( $company_id == '' && is_object( $current_company ) ) {
$company_id = $current_company->getId();
} else if ( $company_id == '' && isset( $this ) && is_object( $this ) ) {
$company_id = $this->getCompany();
}
$dlf = TTnew( 'DepartmentListFactory' ); /** @var DepartmentListFactory $dlf */
$dlf->getHighestManualIDByCompanyId( $company_id );
if ( $dlf->getRecordCount() > 0 ) {
$next_available_manual_id = ( $dlf->getCurrent()->getManualId() + 1 );
} else {
$next_available_manual_id = 1;
}
return $next_available_manual_id;
}
/**
* @return bool|int
*/
function getManualID() {
return (int)$this->getGenericDataValue( 'manual_id' );
}
/**
* @param $value
* @return bool
*/
function setManualID( $value ) {
$value = $this->Validator->stripNonNumeric( trim( $value ) );
return $this->setGenericDataValue( 'manual_id', $value );
}
/**
* @param $name
* @return bool
*/
function isUniqueName( $name ) {
if ( $this->getCompany() == false ) {
return false;
}
$name = trim( $name );
if ( $name == '' ) {
return false;
}
$ph = [
'company_id' => TTUUID::castUUID( $this->getCompany() ),
'name' => TTi18n::strtolower( $name ),
];
$query = 'select id from ' . $this->table . '
where company_id = ?
AND lower(name) = ?
AND deleted = 0';
$name_id = $this->db->GetOne( $query, $ph );
//Debug::Arr($name_id, 'Unique Name: '. $name, __FILE__, __LINE__, __METHOD__, 10);
if ( $name_id === false ) {
return true;
} else {
if ( $name_id == $this->getId() ) {
return true;
}
}
return false;
}
/**
* @return bool|mixed
*/
function getName() {
return $this->getGenericDataValue( 'name' );
}
/**
* @param $value
* @return bool
*/
function setName( $value ) {
$value = trim( $value );
$this->setNameMetaphone( $value );
return $this->setGenericDataValue( 'name', $value );
}
/**
* @return bool|mixed
*/
function getNameMetaphone() {
return $this->getGenericDataValue( 'name_metaphone' );
}
/**
* @param $value
* @return bool
*/
function setNameMetaphone( $value ) {
$value = metaphone( trim( $value ) );
if ( $value != '' ) {
$this->setGenericDataValue( 'name_metaphone', $value );
return true;
}
return false;
}
/**
* @return array|bool
*/
function getGEOFenceIds() {
return CompanyGenericMapListFactory::getArrayByCompanyIDAndObjectTypeIDAndObjectID( $this->getCompany(), 4010, $this->getID() );
}
/**
* @param string $ids UUID
* @return bool
*/
function setGEOFenceIds( $ids ) {
Debug::text( 'Setting GEO Fence IDs...', __FILE__, __LINE__, __METHOD__, 10 );
return CompanyGenericMapFactory::setMapIDs( $this->getCompany(), 4010, $this->getID(), (array)$ids );
}
/**
* @return bool|string
*/
function getTag() {
//Check to see if any temporary data is set for the tags, if not, make a call to the database instead.
//postSave() needs to get the tmp_data.
$value = $this->getGenericTempDataValue( 'tags' );
if ( $value !== false ) {
return $value;
} else if ( TTUUID::isUUID( $this->getCompany() ) && $this->getCompany() != TTUUID::getZeroID() && $this->getCompany() != TTUUID::getNotExistID()
&& TTUUID::isUUID( $this->getID() ) && $this->getID() != TTUUID::getZeroID() && $this->getID() != TTUUID::getNotExistID() ) {
return CompanyGenericTagMapListFactory::getStringByCompanyIDAndObjectTypeIDAndObjectID( $this->getCompany(), 120, $this->getID() );
}
return false;
}
/**
* @param $value
* @return bool
*/
function setTag( $value ) {
$value = trim( $value );
//Save the tags in temporary memory to be committed in postSave()
return $this->setGenericTempDataValue( 'tags', $value );
}
/**
* @param string $id UUID
* @return bool
*/
function isAllowedUser( $id, $branch_id ) {
if ( $this->isNew() ) {
return true; //We don't have anything to go on yet
} else {
//Don't bother going to the database for expensive isAllowed check if all the selection types are allowed.
if ( $this->getUserGroupSelectionType() == 10 && $this->getUserPunchBranchSelectionType() == 10 && $this->getUserDefaultDepartmentSelectionType() == 10 && $this->getUserTitleSelectionType() == 10 && $this->getExcludeUser() == false ) {
return true;
} else {
//Debug::text('Checking database for Department ID: '. $id, __FILE__, __LINE__, __METHOD__, 10);
$dlf = TTnew( 'DepartmentListFactory' ); /** @var DepartmentListFactory $dlf */
$dlf->getByCompanyIdAndIdAndUserIdAndBranchId( $this->getCompany(), $this->getID(), $id, $branch_id );
if ( $dlf->getRecordCount() == 1 ) {
return true;
}
}
}
return false;
}
/**
* @return bool|int
*/
function getUserGroupSelectionType() {
return (int)$this->getGenericDataValue( 'user_group_selection_type_id' );
}
/**
* @param $value
* @return bool
*/
function setUserGroupSelectionType( $value ) {
$value = trim( $value );
return $this->setGenericDataValue( 'user_group_selection_type_id', $value );
}
/**
* @return array|bool
*/
function getUserGroup() {
return CompanyGenericMapListFactory::getArrayByCompanyIDAndObjectTypeIDAndObjectID( $this->getCompany(), 8000, $this->getID() );
}
/**
* @param string $ids UUID
* @return bool
*/
function setUserGroup( $ids ) {
Debug::text( 'Setting User Group IDs : ', __FILE__, __LINE__, __METHOD__, 10 );
return CompanyGenericMapFactory::setMapIDs( $this->getCompany(), 8000, $this->getID(), $ids );
}
/**
* @return array|bool
*/
function getIncludeUser() {
return CompanyGenericMapListFactory::getArrayByCompanyIDAndObjectTypeIDAndObjectID( $this->getCompany(), 8010, $this->getID() );
}
/**
* @param string $ids UUID
* @return bool
*/
function setIncludeUser( $ids ) {
Debug::text( 'Setting Include User IDs : ', __FILE__, __LINE__, __METHOD__, 10 );
return CompanyGenericMapFactory::setMapIDs( $this->getCompany(), 8010, $this->getID(), $ids );
}
/**
* @return array|bool
*/
function getExcludeUser() {
return CompanyGenericMapListFactory::getArrayByCompanyIDAndObjectTypeIDAndObjectID( $this->getCompany(), 8020, $this->getID() );
}
/**
* @param string $ids UUID
* @return bool
*/
function setExcludeUser( $ids ) {
Debug::text( 'Setting Exclude User IDs : ', __FILE__, __LINE__, __METHOD__, 10 );
return CompanyGenericMapFactory::setMapIDs( $this->getCompany(), 8020, $this->getID(), $ids );
}
/**
* @return bool|int
*/
function getUserTitleSelectionType() {
return (int)$this->getGenericDataValue( 'user_title_selection_type_id' );
}
/**
* @param $value
* @return bool
*/
function setUserTitleSelectionType( $value ) {
$value = trim( $value );
return $this->setGenericDataValue( 'user_title_selection_type_id', $value );
}
/**
* @return array|bool
*/
function getUserTitle() {
return CompanyGenericMapListFactory::getArrayByCompanyIDAndObjectTypeIDAndObjectID( $this->getCompany(), 8030, $this->getID() );
}
/**
* @param string $ids UUID
* @return bool
*/
function setUserTitle( $ids ) {
Debug::text( 'Setting User Title IDs : ', __FILE__, __LINE__, __METHOD__, 10 );
return CompanyGenericMapFactory::setMapIDs( $this->getCompany(), 8030, $this->getID(), $ids );
}
/**
* @return bool|int
*/
function getUserPunchBranchSelectionType() {
return (int)$this->getGenericDataValue( 'user_punch_branch_selection_type_id' );
}
/**
* @param $value
* @return bool
*/
function setUserPunchBranchSelectionType( $value ) {
$value = trim( $value );
return $this->setGenericDataValue( 'user_punch_branch_selection_type_id', $value );
}
/**
* @return array|bool
*/
function getUserPunchBranch() {
return CompanyGenericMapListFactory::getArrayByCompanyIDAndObjectTypeIDAndObjectID( $this->getCompany(), 8040, $this->getID() );
}
/**
* @param string $ids UUID
* @return bool
*/
function setUserPunchBranch( $ids ) {
Debug::text( 'Setting User Default Branch IDs : ', __FILE__, __LINE__, __METHOD__, 10 );
return CompanyGenericMapFactory::setMapIDs( $this->getCompany(), 8040, $this->getID(), $ids );
}
/**
* @return bool|int
*/
function getUserDefaultDepartmentSelectionType() {
return (int)$this->getGenericDataValue( 'user_default_department_selection_type_id' );
}
/**
* @param $value
* @return bool
*/
function setUserDefaultDepartmentSelectionType( $value ) {
$value = trim( $value );
return $this->setGenericDataValue( 'user_default_department_selection_type_id', $value );
}
/**
* @return array|bool
*/
function getUserDefaultDepartment() {
return CompanyGenericMapListFactory::getArrayByCompanyIDAndObjectTypeIDAndObjectID( $this->getCompany(), 8050, $this->getID() );
}
/**
* @param string $ids UUID
* @return bool
*/
function setUserDefaultDepartment( $ids ) {
Debug::text( 'Setting User Default Department IDs : ', __FILE__, __LINE__, __METHOD__, 10 );
return CompanyGenericMapFactory::setMapIDs( $this->getCompany(), 8050, $this->getID(), $ids );
}
/**
* @return bool
*/
function getIncludeUserDefaultDepartment() {
return $this->fromBool( $this->getGenericDataValue( 'include_user_default_department_id' ) );
}
/**
* @param $value
* @return bool
*/
function setIncludeUserDefaultDepartment( $value ) {
return $this->setGenericDataValue( 'include_user_default_department_id', $this->toBool( $value ) );
}
/**
* @return bool
*/
function Validate() {
//
// BELOW: Validation code moved from set*() functions.
//
// Company
if ( $this->getCompany() != TTUUID::getZeroID() ) {
$clf = TTnew( 'CompanyListFactory' ); /** @var CompanyListFactory $clf */
$this->Validator->isResultSetWithRows( 'company',
$clf->getByID( $this->getCompany() ),
TTi18n::gettext( 'Company is invalid' )
);
}
// Status
if ( $this->getStatus() !== false ) {
$this->Validator->inArrayKey( 'status',
$this->getStatus(),
TTi18n::gettext( 'Incorrect Status' ),
$this->getOptions( 'status' )
);
}
// Code
if ( $this->getManualID() != '' ) {
$this->Validator->isNumeric( 'manual_id',
$this->getManualID(),
TTi18n::gettext( 'Code is invalid' )
);
if ( $this->Validator->isError( 'manual_id' ) == false ) {
$this->Validator->isLength( 'manual_id',
$this->getManualID(),
TTi18n::gettext( 'Code has too many digits' ),
0,
10
);
}
if ( $this->Validator->getValidateOnly() == false && $this->Validator->isError( 'manual_id' ) == false ) {
$this->Validator->isTrue( 'manual_id',
( (int)$this->getManualID() === 0 ) ? false : true,
TTi18n::gettext( 'Code is invalid, must not be 0' )
);
}
if ( $this->Validator->isError( 'manual_id' ) == false ) {
$this->Validator->isTrue( 'manual_id',
( (int)$this->getManualID() !== 0 && $this->Validator->stripNon32bitInteger( $this->getManualID() ) === 0 ) ? false : true,
TTi18n::gettext( 'Code is invalid, maximum value exceeded' )
);
}
if ( $this->Validator->isError( 'manual_id' ) == false ) {
$this->Validator->isTrue( 'manual_id',
$this->isUniqueManualID( $this->getManualID() ),
TTi18n::gettext( 'Code is already in use, please enter a different one' )
);
}
}
// Department name
if ( $this->getName() !== false ) {
$this->Validator->isLength( 'name',
$this->getName(),
TTi18n::gettext( 'Department name is too short or too long' ),
2,
100
);
if ( $this->Validator->isError( 'name' ) == false ) {
$this->Validator->isTrue( 'name',
$this->isUniqueName( $this->getName() ),
TTi18n::gettext( 'Department already exists' )
);
}
}
$this->validateCustomFields( $this->getCompany() );
//
// ABOVE: Validation code moved from set*() functions.
//
return true;
}
/**
* @return bool
*/
function preSave() {
if ( $this->getStatus() == false ) {
$this->setStatus( 10 );
}
if ( $this->getManualID() == false ) {
$this->setManualID( $this->getNextAvailableManualId( $this->getCompany() ) );
}
return true;
}
/**
* @return bool
*/
function postSave() {
$this->removeCache( $this->getId() );
//Remove cache for checking if a user is allowed or not. isAllowedUser()
$this->removeCache( null, 'department_user_is_allowed_' . $this->getCompany() );
if ( $this->getDeleted() == false ) {
CompanyGenericTagMapFactory::setTags( $this->getCompany(), 120, $this->getID(), $this->getTag() );
}
if ( $this->getDeleted() == true ) {
Debug::Text( 'UnAssign Hours from Department: ' . $this->getId(), __FILE__, __LINE__, __METHOD__, 10 );
//Unassign hours from this department.
$pcf = TTnew( 'PunchControlFactory' ); /** @var PunchControlFactory $pcf */
$query = 'update ' . $pcf->getTable() . ' set department_id = \'' . TTUUID::getZeroID() . '\' where department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
$udtf = TTnew( 'UserDateTotalFactory' ); /** @var UserDateTotalFactory $udtf */
$query = 'update ' . $udtf->getTable() . ' set department_id = \'' . TTUUID::getZeroID() . '\' where department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
$sf_b = TTnew( 'ScheduleFactory' ); /** @var ScheduleFactory $sf_b */
$query = 'update ' . $sf_b->getTable() . ' set department_id = \'' . TTUUID::getZeroID() . '\' where department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
$uf = TTnew( 'UserFactory' ); /** @var UserFactory $uf */
$query = 'update ' . $uf->getTable() . ' set default_department_id = \'' . TTUUID::getZeroID() . '\' where company_id = \'' . TTUUID::castUUID( $this->getCompany() ) . '\' AND default_department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
$udf = TTnew( 'UserDefaultFactory' ); /** @var UserDefaultFactory $udf */
$query = 'update ' . $udf->getTable() . ' set default_department_id = \'' . TTUUID::getZeroID() . '\' where company_id = \'' . TTUUID::castUUID( $this->getCompany() ) . '\' AND default_department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
$sf = TTnew( 'StationFactory' ); /** @var StationFactory $sf */
$query = 'update ' . $sf->getTable() . ' set department_id = \'' . TTUUID::getZeroID() . '\' where company_id = \'' . TTUUID::castUUID( $this->getCompany() ) . '\' AND department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
$sdf = TTnew( 'StationDepartmentFactory' ); /** @var StationDepartmentFactory $sdf */
$query = 'delete from ' . $sdf->getTable() . ' where department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
$rstf = TTnew( 'RecurringScheduleTemplateFactory' ); /** @var RecurringScheduleTemplateFactory $rstf */
$query = 'update ' . $rstf->getTable() . ' set department_id = \'' . TTUUID::getZeroID() . '\' where department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
$rsf = TTnew( 'RecurringScheduleFactory' ); /** @var RecurringScheduleFactory $rsf */
$query = 'update ' . $rsf->getTable() . ' set department_id = \'' . TTUUID::getZeroID() . '\' where department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
if ( getTTProductEdition() >= TT_PRODUCT_CORPORATE ) {
$jf = TTNew( 'JobFactory' ); /** @var JobFactory $jf */
$query = 'update ' . $jf->getTable() . ' set department_id = \'' . TTUUID::getZeroID() . '\' where department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
//Job employee criteria
$cgmlf = TTnew( 'CompanyGenericMapListFactory' ); /** @var CompanyGenericMapListFactory $cgmlf */
$cgmlf->getByCompanyIDAndObjectTypeAndMapID( $this->getCompany(), 1020, $this->getID() );
if ( $cgmlf->getRecordCount() > 0 ) {
foreach ( $cgmlf as $cgm_obj ) {
Debug::text( 'Deleting from Company Generic Map: ' . $cgm_obj->getID(), __FILE__, __LINE__, __METHOD__, 10 );
$cgm_obj->Delete();
}
}
}
if ( getTTProductEdition() >= TT_PRODUCT_ENTERPRISE ) {
$uef = TTNew( 'UserExpenseFactory' ); /** @var UserExpenseFactory $uef */
$query = 'update ' . $uef->getTable() . ' set department_id = \'' . TTUUID::getZeroID() . '\' where department_id = \'' . TTUUID::castUUID( $this->getId() ) . '\'';
$this->ExecuteSQL( $query );
}
}
return true;
}
/**
* Support setting created_by, updated_by especially for importing data.
* Make sure data is set based on the getVariableToFunctionMap order.
* @param $data
* @return bool
*/
function setObjectFromArray( $data ) {
if ( is_array( $data ) ) {
$data = $this->parseCustomFieldsFromArray( $data );
$variable_function_map = $this->getVariableToFunctionMap();
foreach ( $variable_function_map as $key => $function ) {
if ( isset( $data[$key] ) ) {
$function = 'set' . $function;
switch ( $key ) {
default:
if ( method_exists( $this, $function ) ) {
$this->$function( $data[$key] );
}
break;
}
}
}
$this->setCreatedAndUpdatedColumns( $data );
return true;
}
return false;
}
/**
* @param null $include_columns
* @return array
*/
function getObjectAsArray( $include_columns = null ) {
$data = [];
$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 'status':
$function = 'get' . $variable;
if ( method_exists( $this, $function ) ) {
$data[$variable] = Option::getByKey( $this->$function(), $this->getOptions( $variable ) );
}
break;
case 'name_metaphone':
break;
default:
if ( method_exists( $this, $function ) ) {
$data[$variable] = $this->$function();
}
break;
}
}
}
$this->getCreatedAndUpdatedColumns( $data, $include_columns );
$data = $this->getCustomFields( $this->getCompany(), $data, $include_columns );
}
return $data;
}
/**
* @param $log_action
* @return bool
*/
function addLog( $log_action ) {
return TTLog::addEntry( $this->getId(), $log_action, TTi18n::getText( 'Department' ) . ': ' . $this->getName(), null, $this->getTable(), $this );
}
}
?>

View File

@ -0,0 +1,878 @@
<?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\Department
*/
class DepartmentListFactory extends DepartmentFactory 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|DepartmentListFactory
*/
function getById( $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
$this->rs = $this->getCache( $id );
if ( $this->rs === 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 );
$this->saveCache( $this->rs, $id );
}
return $this;
}
/**
* @param string $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|DepartmentListFactory
*/
function getByCompanyId( $id, $limit = null, $page = null, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $order == null ) {
$order = [ 'status_id' => 'asc', 'name' => 'asc' ];
$strict = false;
} else {
$strict = true;
}
$ph = [
'id' => TTUUID::castUUID( $id ),
];
$query = '
select *
from ' . $this->getTable() . '
where company_id = ?
AND 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 int $status_id
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|DepartmentListFactory
*/
function getByCompanyIdAndStatus( $company_id, $status_id, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $status_id == '' ) {
return false;
}
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'status_id' => (int)$status_id,
];
$query = '
select *
from ' . $this->getTable() . '
where company_id = ?
AND status_id = ?
AND deleted = 0';
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $company_id UUID
* @param string $id UUID
* @param string $user_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|DepartmentListFactory
*/
function getByCompanyIdAndIdAndUserId( $company_id, $id, $user_id, $limit = null, $page = null, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $user_id == '' ) {
return false;
}
if ( $id == '' ) {
return false;
}
$cache_id = $company_id . $id . $user_id;
$group_id = 'department_user_is_allowed_' . $company_id; //Cache with a special group that we can clear easily when a department is saved.
$this->rs = $this->getCache( $cache_id, $group_id );
if ( $this->rs === false ) {
if ( $order == null ) {
$order = [ 'a.status_id' => 'asc', 'a.name' => 'asc', 'a.manual_id' => 'asc' ];
$strict = false;
} else {
$strict = true;
}
$uf = new UserFactory();
$cgmf = new CompanyGenericMapFactory();
$ph = [
'user_id' => TTUUID::castUUID( $user_id ),
'company_id' => TTUUID::castUUID( $company_id ),
'id' => TTUUID::castUUID( $id ),
];
$query = '
select _ADODB_COUNT
a.*
_ADODB_COUNT
from ' . $this->getTable() . ' as a
LEFT JOIN ' . $uf->getTable() . ' as z ON z.id = ?
where
a.company_id = ?
AND a.id = ?
AND
(
( a.include_user_default_department_id = 1 AND a.id = z.default_department_id )
OR
(
(
(
(
a.user_group_selection_type_id = 10
OR ( a.user_group_selection_type_id = 20 AND z.group_id in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8000 AND b.deleted = 0 ) )
OR ( a.user_group_selection_type_id = 30 AND z.group_id not in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8000 AND b.deleted = 0 ) )
)
)
AND
(
a.user_title_selection_type_id = 10
OR ( a.user_title_selection_type_id = 20 AND z.title_id in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8030 AND b.deleted = 0 ) )
OR ( a.user_title_selection_type_id = 30 AND z.title_id not in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8030 AND b.deleted = 0 ) )
)
AND
(
a.user_default_department_selection_type_id = 10
OR ( a.user_default_department_selection_type_id = 20 AND z.default_department_id in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8050 AND b.deleted = 0 ) )
OR ( a.user_default_department_selection_type_id = 30 AND z.default_department_id not in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8050 AND b.deleted = 0 ) )
)
AND ( a.id not in ( select b.object_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8020 AND b.deleted = 0 ' . $this->getWhereClauseSQL( 'b.map_id', $user_id, 'uuid_list', $ph ) . ' ) )
)
OR ( a.id in ( select b.object_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8010 AND b.deleted = 0 ' . $this->getWhereClauseSQL( 'b.map_id', $user_id, 'uuid_list', $ph ) . ' ) )
)
)
AND ( a.deleted = 0 AND z.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
$this->saveCache( $this->rs, $cache_id, $group_id );
}
return $this;
}
/**
* @param string $company_id UUID
* @param string $id UUID
* @param string $user_id UUID
* @param string $branch_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|DepartmentListFactory
*/
function getByCompanyIdAndIdAndUserIdAndBranchId( $company_id, $id, $user_id, $branch_id, $limit = null, $page = null, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $user_id == '' ) {
return false;
}
if ( $id == '' ) {
return false;
}
if ( $branch_id == '' ) {
return false;
}
$cache_id = $company_id . $id . $user_id . $branch_id;
$group_id = 'department_user_is_allowed_' . $company_id; //Cache with a special group that we can clear easily when a department is saved.
$this->rs = $this->getCache( $cache_id, $group_id );
if ( $this->rs === false ) {
if ( $order == null ) {
$order = [ 'a.status_id' => 'asc', 'a.name' => 'asc', 'a.manual_id' => 'asc' ];
$strict = false;
} else {
$strict = true;
}
$uf = new UserFactory();
$cgmf = new CompanyGenericMapFactory();
$ph = [
'user_id' => TTUUID::castUUID( $user_id ),
'company_id' => TTUUID::castUUID( $company_id ),
'id' => TTUUID::castUUID( $id ),
];
$query = '
select _ADODB_COUNT
a.*
_ADODB_COUNT
from ' . $this->getTable() . ' as a
LEFT JOIN ' . $uf->getTable() . ' as z ON z.id = ?
where
a.company_id = ?
AND a.id = ?
AND
(
( a.include_user_default_department_id = 1 AND a.id = z.default_department_id )
OR
(
(
(
(
a.user_group_selection_type_id = 10
OR ( a.user_group_selection_type_id = 20 AND z.group_id in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8000 AND b.deleted = 0 ) )
OR ( a.user_group_selection_type_id = 30 AND z.group_id not in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8000 AND b.deleted = 0 ) )
)
)
AND
(
a.user_title_selection_type_id = 10
OR ( a.user_title_selection_type_id = 20 AND z.title_id in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8030 AND b.deleted = 0 ) )
OR ( a.user_title_selection_type_id = 30 AND z.title_id not in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8030 AND b.deleted = 0 ) )
)
AND
(
a.user_punch_branch_selection_type_id = 10
OR ( a.user_punch_branch_selection_type_id = 20 AND a.id in ( select b.object_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8040 ' . $this->getWhereClauseSQL( 'b.map_id', $branch_id, 'uuid_list', $ph ) . ' AND b.deleted = 0 ) )
OR ( a.user_punch_branch_selection_type_id = 30 AND a.id not in ( select b.object_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8040 ' . $this->getWhereClauseSQL( 'b.map_id', $branch_id, 'uuid_list', $ph ) . ' AND b.deleted = 0 ) )
)
AND
(
a.user_default_department_selection_type_id = 10
OR ( a.user_default_department_selection_type_id = 20 AND z.default_department_id in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8050 AND b.deleted = 0 ) )
OR ( a.user_default_department_selection_type_id = 30 AND z.default_department_id not in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8050 AND b.deleted = 0 ) )
)
AND ( a.id not in ( select b.object_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8020 AND b.deleted = 0 ' . $this->getWhereClauseSQL( 'b.map_id', $user_id, 'uuid_list', $ph ) . ' ) )
)
OR ( a.id in ( select b.object_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8010 AND b.deleted = 0 ' . $this->getWhereClauseSQL( 'b.map_id', $user_id, 'uuid_list', $ph ) . ' ) )
)
)
AND ( a.deleted = 0 AND z.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
$this->saveCache( $this->rs, $cache_id, $group_id );
}
return $this;
}
/**
* @param string $id UUID
* @param string $company_id UUID
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|DepartmentListFactory
*/
function getByIdAndCompanyId( $id, $company_id, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'id' => TTUUID::castUUID( $id ),
];
$query = '
select *
from ' . $this->getTable() . '
where company_id = ?
AND id = ?
AND deleted = 0';
$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|DepartmentListFactory
*/
function getByManualIdAndCompanyId( $id, $company_id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
$ph = [
'id' => (int)$this->Validator->stripNon32bitInteger( $id ),
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select *
from ' . $this->getTable() . '
where manual_id = ?
AND company_id = ?
AND deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $company_id UUID
* @param int $status_id
* @param int $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|DepartmentListFactory
*/
function getByCompanyIDAndStatusAndDate( $company_id, $status_id, $date = null, $limit = null, $page = null, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $status_id == '' ) {
return false;
}
if ( $date == '' ) {
$date = 0;
}
if ( $order == null ) {
$order = [ 'a.id' => 'asc' ];
$strict = false;
} else {
$strict = true;
}
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'status_id' => (int)$status_id,
];
$query = '
select a.*
from ' . $this->getTable() . ' as a
where a.company_id = ?
AND a.status_id = ?
';
if ( isset( $date ) && $date > 0 ) {
//Append the same date twice for created and updated.
$ph[] = $date;
$ph[] = $date;
$query .= ' AND ( a.created_date >= ? OR a.updated_date >= ? )';
}
$query .= ' AND ( a.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 int $status_id
* @param int $date EPOCH
* @param array $valid_ids
* @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|DepartmentListFactory
*/
function getByCompanyIDAndStatusAndDateAndValidIDs( $company_id, $status_id, $date = null, $valid_ids = [], $limit = null, $page = null, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $status_id == '' ) {
return false;
}
if ( $date == '' ) {
$date = 0;
}
if ( $order == null ) {
$order = [ 'a.id' => 'asc' ];
$strict = false;
} else {
$strict = true;
}
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'status_id' => (int)$status_id,
];
//Make sure we return distinct rows so there aren't duplicates.
$query = '
select distinct a.*
from ' . $this->getTable() . ' as a
where a.company_id = ?
AND a.status_id = ?
AND (
1=1
';
if ( isset( $date ) && $date > 0 ) {
//Append the same date twice for created and updated.
$ph[] = (int)$date;
$ph[] = (int)$date;
$query .= ' AND ( a.created_date >= ? OR a.updated_date >= ? ) ';
}
if ( isset( $valid_ids ) && is_array( $valid_ids ) && count( $valid_ids ) > 0 ) {
$query .= ' OR a.id in (' . $this->getListSQL( $valid_ids, $ph, 'uuid' ) . ') ';
}
$query .= ' )
AND ( a.deleted = 0 )';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph, $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|DepartmentListFactory
*/
function getHighestManualIDByCompanyId( $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
$ph = [
'id' => TTUUID::castUUID( $id ),
'id2' => $id,
];
$query = '
select *
from ' . $this->getTable() . ' as a
where company_id = ?
AND id = ( select id
from ' . $this->getTable() . '
where company_id = ?
AND manual_id IS NOT NULL
AND deleted = 0
ORDER BY manual_id DESC
LIMIT 1
)
AND deleted = 0
LIMIT 1
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $company_id UUID
* @param bool $include_blank
* @param bool $include_disabled
* @return array|bool
*/
function getByCompanyIdArray( $company_id, $include_blank = true, $include_disabled = true ) {
$dlf = new DepartmentListFactory();
$dlf->getByCompanyId( $company_id );
return $dlf->getArrayByListFactory( $dlf, $include_blank, $include_disabled );
}
/**
* @param $lf
* @param bool $include_blank
* @param bool $include_disabled
* @return array|bool
*/
function getArrayByListFactory( $lf, $include_blank = true, $include_disabled = true ) {
if ( !is_object( $lf ) ) {
return false;
}
$list = [];
if ( $include_blank == true ) {
$list[TTUUID::getZeroID()] = '--';
}
foreach ( $lf as $obj ) {
if ( $obj->getStatus() == 20 ) {
$status = '(DISABLED) ';
} else {
$status = null;
}
if ( $include_disabled == true || ( $include_disabled == false && $obj->getStatus() == 10 ) ) {
$list[$obj->getID()] = $status . $obj->getName();
}
}
if ( empty( $list ) == false ) {
return $list;
}
return false;
}
/**
* @param string $company_id UUID
* @param int $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 bool
*/
function getIsModifiedByCompanyIdAndDate( $company_id, $date, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $date == '' ) {
return false;
}
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'created_date' => $date,
'updated_date' => $date,
'deleted_date' => $date,
];
//INCLUDE Deleted rows in this query.
$query = '
select *
from ' . $this->getTable() . '
where
company_id = ?
AND
( created_date >= ? OR updated_date >= ? OR ( deleted = 1 AND deleted_date >= ? ) )
LIMIT 1
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
if ( $this->getRecordCount() > 0 ) {
Debug::text( 'Rows have been modified: ' . $this->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
return true;
}
Debug::text( 'Rows have NOT been modified', __FILE__, __LINE__, __METHOD__, 10 );
return false;
}
/**
* @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|DepartmentListFactory
*/
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 = [ 'status_id' ];
$sort_column_aliases = [
'status' => 'status_id',
];
$order = $this->getColumnsFromAliases( $order, $sort_column_aliases );
if ( $order == null ) {
$order = [ 'status_id' => 'asc', 'name' => 'asc' ];
$strict = false;
} else {
//Always try to order by status first so INACTIVE employees go to the bottom.
if ( !isset( $order['status_id'] ) ) {
$order = Misc::prependArray( [ 'status_id' => 'asc' ], $order );
}
//Always sort by last name, first name after other columns
if ( !isset( $order['name'] ) ) {
$order['name'] = 'asc';
}
$strict = true;
}
//Debug::Arr($order, 'Order Data:', __FILE__, __LINE__, __METHOD__, 10);
//Debug::Arr($filter_data, 'Filter Data:', __FILE__, __LINE__, __METHOD__, 10);
$uf = new UserFactory();
$cgmf = new CompanyGenericMapFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select a.*,
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
from ' . $this->getTable() . ' as a';
if ( isset( $filter_data['user_id'] ) ) {
//For punching in and out we need to join user table to get branches based on employee criteria
array_unshift( $ph, TTUUID::castUUID( $filter_data['user_id'] ) );
$query .= '
LEFT JOIN ' . $uf->getTable() . ' as uf ON ( a.company_id = uf.company_id AND uf.id = ? AND uf.deleted = 0 )';
}
$query .= '
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 a.company_id = ?
AND
(
1=1
';
$query .= ( isset( $filter_data['permission_children_ids'] ) ) ? $this->getWhereClauseSQL( 'a.created_by', $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.id', $filter_data['exclude_id'], 'not_uuid_list', $ph ) : null;
if ( isset( $filter_data['status'] ) && !is_array( $filter_data['status'] ) && trim( $filter_data['status'] ) != '' && !isset( $filter_data['status_id'] ) ) {
$filter_data['status_id'] = Option::getByFuzzyValue( $filter_data['status'], $this->getOptions( 'status' ) );
}
$query .= ( isset( $filter_data['status_id'] ) ) ? $this->getWhereClauseSQL( 'a.status_id', $filter_data['status_id'], 'numeric_list', $ph ) : null;
$query .= ( isset( $filter_data['name'] ) ) ? $this->getWhereClauseSQL( 'a.name', $filter_data['name'], 'text_metaphone', $ph ) : null;
$query .= ( isset( $filter_data['manual_id'] ) ) ? $this->getWhereClauseSQL( 'a.manual_id', $this->Validator->stripNon32bitInteger( $filter_data['manual_id'] ), 'numeric', $ph ) : null;
$query .= ( isset( $filter_data['tag'] ) ) ? $this->getWhereClauseSQL( 'a.id', [ 'company_id' => TTUUID::castUUID( $company_id ), 'object_type_id' => 120, 'tag' => $filter_data['tag'] ], 'tag', $ph ) : null;
$query .= $this->getCustomFieldWhereSQL( $company_id, 'a.custom_field', $filter_data, $ph );
if ( getTTProductEdition() >= TT_PRODUCT_CORPORATE && isset( $filter_data['user_id'] ) && TTUUID::isUUID( $filter_data['user_id'] ) && $filter_data['user_id'] != TTUUID::getZeroID() ) {
if ( !isset( $filter_data['branch_id'] ) ) {
$filter_data['branch_id'] = TTUUID::getZeroID();
}
$query .= ' AND
(
( a.include_user_default_department_id = 1 AND a.id = uf.default_department_id )
OR
(
(
(
(
a.user_group_selection_type_id = 10
OR ( a.user_group_selection_type_id = 20 AND uf.group_id in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8000 AND b.deleted = 0 ) )
OR ( a.user_group_selection_type_id = 30 AND uf.group_id not in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8000 AND b.deleted = 0 ) )
)
)
AND
(
a.user_title_selection_type_id = 10
OR ( a.user_title_selection_type_id = 20 AND uf.title_id in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8030 AND b.deleted = 0 ) )
OR ( a.user_title_selection_type_id = 30 AND uf.title_id not in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8030 AND b.deleted = 0 ) )
) ';
if ( $filter_data['branch_id'] !== TTUUID::getNotExistID() ) {
$query .= '
AND
(
a.user_punch_branch_selection_type_id = 10
OR ( a.user_punch_branch_selection_type_id = 20 AND a.id in ( select b.object_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8040 ' . $this->getWhereClauseSQL( 'b.map_id', $filter_data['branch_id'], 'uuid_list', $ph ) . ' AND b.deleted = 0 ) )
OR ( a.user_punch_branch_selection_type_id = 30 AND a.id not in ( select b.object_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8040 ' . $this->getWhereClauseSQL( 'b.map_id', $filter_data['branch_id'], 'uuid_list', $ph ) . ' AND b.deleted = 0 ) )
) ';
}
$query .= '
AND
(
a.user_default_department_selection_type_id = 10
OR ( a.user_default_department_selection_type_id = 20 AND uf.default_department_id in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8050 AND b.deleted = 0 ) )
OR ( a.user_default_department_selection_type_id = 30 AND uf.default_department_id not in ( select b.map_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8050 AND b.deleted = 0 ) )
)
)
AND ( a.id not in ( select b.object_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8020 AND b.deleted = 0 ' . $this->getWhereClauseSQL( 'b.map_id', $filter_data['user_id'], 'uuid_list', $ph ) . ' ) )
)
OR ( a.id in ( select b.object_id from ' . $cgmf->getTable() . ' as b WHERE a.id = b.object_id AND b.company_id = a.company_id AND b.object_type_id = 8010 AND b.deleted = 0 ' . $this->getWhereClauseSQL( 'b.map_id', $filter_data['user_id'], 'uuid_list', $ph ) . ' ) )
)
';
}
$query .= ( isset( $filter_data['include_id'] ) ) ? ' OR ' . $this->getWhereClauseSQL( 'a.id', $filter_data['include_id'], 'uuid_list', $ph, '', false ) : null;
$query .= ( isset( $filter_data['created_date'] ) ) ? $this->getWhereClauseSQL( 'a.created_date', $filter_data['created_date'], 'date_range', $ph ) : null;
$query .= ( isset( $filter_data['updated_date'] ) ) ? $this->getWhereClauseSQL( 'a.updated_date', $filter_data['updated_date'], 'date_range', $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 );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
}
?>