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
@@ -0,0 +1,545 @@
<?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\Holiday
*/
class HolidayFactory extends Factory {
protected $table = 'holidays';
protected $pk_sequence_name = 'holidays_id_seq'; //PK Sequence name
protected $holiday_policy_obj = null;
/**
* @param $name
* @param null $parent
* @return array|null
*/
function _getFactoryOptions( $name, $parent = null ) {
$retval = null;
switch ( $name ) {
case 'columns':
$retval = [
'-1010-name' => TTi18n::gettext( 'Name' ),
'-1020-date_stamp' => TTi18n::gettext( 'Date' ),
'-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( $this->getOptions( 'default_display_columns' ), Misc::trimSortPrefix( $this->getOptions( 'columns' ) ) );
break;
case 'default_display_columns': //Columns that are displayed by default.
$retval = [
'name',
'date_stamp',
];
break;
}
return $retval;
}
/**
* @param $data
* @return array
*/
function _getVariableToFunctionMap( $data ) {
$variable_function_map = [
'id' => 'ID',
'holiday_policy_id' => 'HolidayPolicyID',
'date_stamp' => 'DateStamp',
'name' => 'Name',
'deleted' => 'Deleted',
];
return $variable_function_map;
}
/**
* @return bool|HolidayPolicyFactory
*/
function getHolidayPolicyObject() {
return $this->getGenericObject( 'HolidayPolicyListFactory', $this->getHolidayPolicyID(), 'holiday_policy_obj' );
}
/**
* @return bool|mixed
*/
function getHolidayPolicyID() {
return $this->getGenericDataValue( 'holiday_policy_id' );
}
/**
* @param string $value UUID
* @return bool
*/
function setHolidayPolicyID( $value ) {
$value = TTUUID::castUUID( $value );
return $this->setGenericDataValue( 'holiday_policy_id', $value );
}
/**
* @param int $date_stamp EPOCH
* @return bool
*/
function isUniqueDateStamp( $date_stamp ) {
$ph = [
'policy_id' => $this->getHolidayPolicyID(),
'date_stamp' => $this->db->BindDate( $date_stamp ),
];
$query = 'select id from ' . $this->getTable() . '
where holiday_policy_id = ?
AND date_stamp = ?
AND deleted=0';
$date_stamp_id = $this->db->GetOne( $query, $ph );
Debug::Arr( $date_stamp_id, 'Unique Date Stamp: ' . $date_stamp, __FILE__, __LINE__, __METHOD__, 10 );
if ( $date_stamp_id === false ) {
return true;
} else {
if ( $date_stamp_id == $this->getId() ) {
return true;
}
}
return false;
}
/**
* @param bool $raw
* @return bool|int
*/
function getDateStamp( $raw = false ) {
$value = $this->getGenericDataValue( 'date_stamp' );
if ( $value !== false ) {
if ( $raw === true ) {
return $value;
} else {
if ( !is_numeric( $value ) ) { //Optimization to avoid converting it when run in CalculatePolicy's loops
$value = TTDate::getMiddleDayEpoch( TTDate::strtotime( $value ) ); //Make sure we use middle day epoch when pulling the value from the DB the first time, to match setDateStamp() below. Otherwise setting the datestamp then getting it again before save won't match the same value after its saved to the DB.
$this->setGenericDataValue( 'date_stamp', $value );
}
return $value;
}
}
return false;
}
/**
* @param int $value EPOCH
* @return bool
*/
function setDateStamp( $value ) {
$value = ( !is_int( $value ) && $value !== null ) ? trim( $value ) : $value;//Dont trim integer values, as it changes them to strings.
if ( $value > 0 ) {
if ( $this->getDateStamp() !== $value && $this->getOldDateStamp() != $this->getDateStamp() ) {
Debug::Text( ' Setting Old DateStamp... Current Old DateStamp: ' . (int)$this->getOldDateStamp() . ' Current DateStamp: ' . (int)$this->getDateStamp(), __FILE__, __LINE__, __METHOD__, 10 );
$this->setOldDateStamp( $this->getDateStamp() );
}
}
return $this->setGenericDataValue( 'date_stamp', TTDate::getMiddleDayEpoch( $value ) );
}
/**
* @return bool
*/
function getOldDateStamp() {
return $this->getGenericTempDataValue( 'old_date_stamp' );
}
/**
* @param int $value EPOCH
* @return bool
*/
function setOldDateStamp( $value ) {
Debug::Text( ' Setting Old DateStamp: ' . TTDate::getDate( 'DATE', $value ), __FILE__, __LINE__, __METHOD__, 10 );
return $this->setGenericTempDataValue( 'old_date_stamp', TTDate::getMiddleDayEpoch( $value ) );
}
/**
* @param $name
* @return bool
*/
function isUniqueName( $name ) {
//BindDate() causes a deprecated error if date_stamp is not set, so just return TRUE so we can throw a invalid date error elsewhere instead.
//This also causes it so we can never have a invalid date and invalid name validation errors at the same time.
if ( $this->getDateStamp() == '' ) {
return true;
}
$name = trim( $name );
if ( $name == '' ) {
return false;
}
//When a holiday gets moved back/forward due to falling on weekend, it can throw off the check to see if the holiday
//appears in the same year. For example new years 01-Jan-2011 gets moved to 31-Dec-2010, its in the same year
//as the previous New Years day or 01-Jan-2010, so this check fails.
//
//I think this can only happen with New Years, or other holidays that fall within two days of the new year.
//So exclude the first three days of the year to allow for weekend adjustments.
$ph = [
'policy_id' => $this->getHolidayPolicyID(),
'name' => TTi18n::strtolower( $name ),
'start_date1' => $this->db->BindDate( ( TTDate::getBeginYearEpoch( $this->getDateStamp() ) + ( 86400 * 3 ) ) ),
'end_date1' => $this->db->BindDate( TTDate::getEndYearEpoch( $this->getDateStamp() ) ),
'start_date2' => $this->db->BindDate( ( $this->getDateStamp() - ( 86400 * 15 ) ) ),
'end_date2' => $this->db->BindDate( ( $this->getDateStamp() + ( 86400 * 15 ) ) ),
];
$query = 'select id from ' . $this->getTable() . '
where holiday_policy_id = ?
AND lower(name) = ?
AND
(
(
date_stamp >= ?
AND date_stamp <= ?
)
OR
(
date_stamp >= ?
AND date_stamp <= ?
)
)
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 );
return $this->setGenericDataValue( 'name', $value );
}
/**
* ignore_after_eligibility is used when scheduling employees as absent on a holiday, since they haven't worked after the holiday
* when the schedule is created, it will always fail.
* @param string $user_id UUID
* @param bool $ignore_after_eligibility
* @return bool
*/
function isEligible( $user_id, $ignore_after_eligibility = false ) {
if ( $user_id == '' ) {
return false;
}
$original_time_zone = TTDate::getTimeZone(); //Store current timezone so we can return to it after.
$ulf = TTnew( 'UserListFactory' ); /** @var UserListFactory $ulf */
$ulf->getById( $user_id );
if ( $ulf->getRecordCount() == 1 ) {
$user_obj = $ulf->getCurrent();
//Use CalculatePolicy to determine if they are eligible for the holiday or not.
$flags = [
'meal' => false,
'undertime_absence' => false,
'break' => false,
'holiday' => true,
'schedule_absence' => false,
'absence' => false,
'regular' => false,
'overtime' => false,
'premium' => false,
'accrual' => false,
'exception' => false,
//Exception options
'exception_premature' => false, //Calculates premature exceptions
'exception_future' => false, //Calculates exceptions in the future.
//Calculate policies for future dates.
'future_dates' => false, //Calculates dates in the future.
'past_dates' => false, //Calculates dates in the past. This is only needed when Pay Formulas that use averaging are enabled?*
];
$cp = TTNew( 'CalculatePolicy' ); /** @var CalculatePolicy $cp */
$cp->setFlag( $flags );
$cp->setUserObject( $user_obj );
$cp->getRequiredData( $this->getDateStamp(), $this->getDateStamp() );
$retval = $cp->isEligibleForHoliday( $this->getDateStamp(), $this->getHolidayPolicyObject(), $ignore_after_eligibility );
TTDate::setTimeZone( $original_time_zone ); //Store current timezone so we can return to it after.
return $retval;
}
Debug::text( 'ERROR: Unable to get user object...', __FILE__, __LINE__, __METHOD__, 10 );
return false;
}
/**
* @param bool $ignore_warning
* @return bool
*/
function Validate( $ignore_warning = true ) {
//
// BELOW: Validation code moved from set*() functions.
//
// Holiday Policy
$hplf = TTnew( 'HolidayPolicyListFactory' ); /** @var HolidayPolicyListFactory $hplf */
$this->Validator->isResultSetWithRows( 'holiday_policy',
$hplf->getByID( $this->getHolidayPolicyID() ),
TTi18n::gettext( 'Holiday Policy is invalid' )
);
// Date stamp
$this->Validator->isDate( 'date_stamp',
$this->getDateStamp(),
TTi18n::gettext( 'Incorrect date' )
);
if ( $this->Validator->isError( 'date_stamp' ) == false ) {
$this->Validator->isTrue( 'date_stamp',
$this->isUniqueDateStamp( $this->getDateStamp() ),
TTi18n::gettext( 'Date is already in use by another Holiday' )
);
}
// Name
$this->Validator->isLength( 'name',
$this->getName(),
TTi18n::gettext( 'Name is invalid' ),
2, 50
);
if ( $this->Validator->isError( 'name' ) == false ) {
$this->Validator->isTrue( 'name',
$this->isUniqueName( $this->getName() ),
TTi18n::gettext( 'Name is already in use in this year, or within 30 days' )
);
}
//
// ABOVE: Validation code moved from set*() functions.
//
if ( $this->Validator->hasError( 'date_stamp' ) == false && $this->getDateStamp() == '' ) {
$this->Validator->isTrue( 'date_stamp',
false,
TTi18n::gettext( 'Date is invalid' ) );
}
return true;
}
/**
* @return bool
*/
function postSave() {
//ReCalculate Recurring Schedule records based on this holiday, assuming its not older than a week.
// Since recurring schedules still exist for up to a week in the past typically, we still want to recalculate them if the holiday has already past,
// otherwise the schedule won't be updated and when the user tries to manually delete the scheduled absence shift, it will appear like its not being deleted because the recurring schedule will still show it.
$cutoff_date = TTDate::incrementDate( TTDate::getMiddleDayEpoch( time() ), -1, 'week' );
Debug::text( 'Cutoff Date: ' . TTDate::getDate( 'DATE', $cutoff_date ), __FILE__, __LINE__, __METHOD__, 10 );
if ( TTDate::getMiddleDayEpoch( $this->getDateStamp() ) >= TTDate::getMiddleDayEpoch( $cutoff_date )
|| ( $this->getOldDateStamp() != '' && TTDate::getMiddleDayEpoch( $this->getOldDateStamp() ) >= TTDate::getMiddleDayEpoch( $cutoff_date ) ) ) {
Debug::text( 'Holiday is less than a week old, or in the future, try to recalculate recurring schedules on this date: ' . TTDate::getDate( 'DATE', $this->getDateStamp() ) . ' Old Date: ' . TTDate::getDate( 'DATE', $this->getOldDateStamp() ), __FILE__, __LINE__, __METHOD__, 10 );
$date_ranges = [];
if ( $this->getOldDateStamp() != '' && TTDate::getMiddleDayEpoch( $this->getDateStamp() ) != TTDate::getMiddleDayEpoch( $this->getOldDateStamp() ) ) {
$date_ranges[] = [ 'start_date' => TTDate::getBeginDayEpoch( $this->getOldDateStamp() ), 'end_date' => TTDate::getEndDayEpoch( $this->getOldDateStamp() ) ];
}
$date_ranges[] = [ 'start_date' => TTDate::getBeginDayEpoch( $this->getDateStamp() ), 'end_date' => TTDate::getEndDayEpoch( $this->getDateStamp() ) ];
$system_job_queue_batch_id = TTUUID::generateUUID();
foreach ( $date_ranges as $date_range ) {
$start_date = $date_range['start_date'];
$end_date = $date_range['end_date'];
Debug::text( 'Recalculating Recurring Schedules... Start Date: ' . TTDate::getDate( 'DATE', $start_date ) . ' End Date: ' . TTDate::getDate( 'DATE', $end_date ), __FILE__, __LINE__, __METHOD__, 10 );
//Get existing recurring_schedule rows on the holiday day, so we can figure out which recurring_schedule_control records to recalculate.
$recurring_schedule_control_ids = [];
$rslf = TTnew( 'RecurringScheduleListFactory' ); /** @var RecurringScheduleListFactory $rslf */
$rslf->getByCompanyIDAndStartDateAndEndDateAndNoConflictingSchedule( $this->getHolidayPolicyObject()->getCompany(), $start_date, $end_date );
Debug::text( 'Recurring Schedule Record Count: ' . $rslf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $rslf->getRecordCount() > 0 ) {
foreach ( $rslf as $rs_obj ) {
if ( TTUUID::isUUID( $rs_obj->getRecurringScheduleControl() ) && $rs_obj->getRecurringScheduleControl() != TTUUID::getZeroID() && $rs_obj->getRecurringScheduleControl() != TTUUID::getNotExistID() ) {
$recurring_schedule_control_ids[] = $rs_obj->getRecurringScheduleControl();
}
}
}
$recurring_schedule_control_ids = array_unique( $recurring_schedule_control_ids );
Debug::Arr( $recurring_schedule_control_ids, 'Recurring Schedule Control IDs: ', __FILE__, __LINE__, __METHOD__, 10 );
if ( count( $recurring_schedule_control_ids ) > 0 ) {
//
//**THIS IS DONE IN recalculateRecurringScheduleForJobQueue, RecurringScheduleControlFactory, RecurringScheduleTemplateControlFactory, HolidayFactory postSave() as well.
//
global $config_vars;
if ( ( !isset( $config_vars['other']['enable_job_queue'] ) || $config_vars['other']['enable_job_queue'] == true ) && ( !defined( 'UNIT_TEST_MODE' ) || UNIT_TEST_MODE === false ) ) {
foreach( $recurring_schedule_control_ids as $recurring_schedule_control_id ) {
SystemJobQueue::Add( TTi18n::getText( 'Recalculating Recurring Schedule' ), $system_job_queue_batch_id, 'RecurringScheduleFactory', 'recalculateRecurringScheduleForJobQueue', [ $this->getHolidayPolicyObject()->getCompany(), $recurring_schedule_control_id, null, $start_date, $end_date, false, true ], 90 );
}
} else {
$rsf = TTnew( 'RecurringScheduleFactory' ); /** @var RecurringScheduleFactory $rsf */
$rsf->StartTransaction();
$rsf->clearRecurringSchedulesFromRecurringScheduleControl( $recurring_schedule_control_ids, $start_date, $end_date );
$rsf->addRecurringSchedulesFromRecurringScheduleControl( $this->getHolidayPolicyObject()->getCompany(), $recurring_schedule_control_ids, $start_date, $end_date );
$rsf->CommitTransaction();
}
}
}
} else {
Debug::text( 'Holiday is older than a week or not in the future...', __FILE__, __LINE__, __METHOD__, 10 );
}
return true;
}
/**
* @param $data
* @return bool
*/
function setObjectFromArray( $data ) {
if ( is_array( $data ) ) {
$variable_function_map = $this->getVariableToFunctionMap();
foreach ( $variable_function_map as $key => $function ) {
if ( isset( $data[$key] ) ) {
$function = 'set' . $function;
switch ( $key ) {
case 'date_stamp':
if ( method_exists( $this, $function ) ) {
$this->$function( TTDate::parseDateTime( $data[$key] ) );
}
break;
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 'date_stamp':
if ( method_exists( $this, $function ) ) {
$data[$variable] = TTDate::getAPIDate( 'DATE', $this->$function() );
}
break;
default:
if ( method_exists( $this, $function ) ) {
$data[$variable] = $this->$function();
}
break;
}
}
}
$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( 'Holiday' ), null, $this->getTable(), $this );
}
}
?>
@@ -0,0 +1,690 @@
<?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\Holiday
*/
class HolidayListFactory extends HolidayFactory 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|HolidayListFactory
*/
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 $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|HolidayListFactory
*/
function getByCompanyId( $company_id, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
$hpf = new HolidayPolicyFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select a.*
from ' . $this->getTable() . ' as a
LEFT JOIN ' . $hpf->getTable() . ' as b ON a.holiday_policy_id = b.id
where b.company_id = ?
AND ( a.deleted = 0 AND b.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|HolidayListFactory
*/
function getByIDAndCompanyId( $id, $company_id, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $id == '' ) {
return false;
}
if ( $order == null ) {
$order = [ 'date_stamp' => 'desc' ];
$strict = false;
} else {
$strict = true;
}
$hpf = new HolidayPolicyFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'id' => TTUUID::castUUID( $id ),
];
$query = '
select a.*
from ' . $this->getTable() . ' as a
LEFT JOIN ' . $hpf->getTable() . ' as b ON a.holiday_policy_id = b.id
where b.company_id = ?
AND a.id = ?
AND ( a.deleted = 0 AND b.deleted = 0 ) ';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param string $holiday_policy_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|HolidayListFactory
*/
function getByIdAndHolidayPolicyID( $id, $holiday_policy_id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
$ph = [
'id' => TTUUID::castUUID( $id ),
'holiday_policy_id' => TTUUID::castUUID( $holiday_policy_id ),
];
$query = '
select *
from ' . $this->getTable() . '
where id = ?
AND holiday_policy_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 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|HolidayListFactory
*/
function getByHolidayPolicyId( $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $order == null ) {
$order = [ 'date_stamp' => 'desc' ];
$strict = false;
} else {
$strict = true;
}
$ph = [];
$query = '
select *
from ' . $this->getTable() . ' as a
where holiday_policy_id in (' . $this->getListSQL( $id, $ph, 'uuid' ) . ')
AND deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string|string[] $holiday_policy_id UUID
* @param int $start_date EPOCH
* @param int $end_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|HolidayListFactory
*/
function getByHolidayPolicyIdAndStartDateAndEndDate( $holiday_policy_id, $start_date, $end_date, $where = null, $order = null ) {
if ( $holiday_policy_id == '' ) {
return false;
}
if ( $start_date == '' ) {
return false;
}
if ( $end_date == '' ) {
return false;
}
if ( $order == null ) {
$order = [ 'a.holiday_policy_id' => 'asc', 'a.date_stamp' => 'desc' ];
$strict = false;
} else {
$strict = true;
}
$hpf = new HolidayPolicyFactory();
$ph = [
'start_date' => $this->db->BindDate( $start_date ),
'end_date' => $this->db->BindDate( $end_date ),
];
$query = '
select a.*
from ' . $this->getTable() . ' as a
LEFT JOIN ' . $hpf->getTable() . ' as b ON ( a.holiday_policy_id = b.id )
where
a.date_stamp >= ?
AND a.date_stamp <= ?
AND b.id in (' . $this->getListSQL( $holiday_policy_id, $ph, 'uuid' ) . ')
AND ( a.deleted = 0 AND b.deleted=0 )
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph );
//Debug::Query( $query, $ph, __FILE__, __LINE__, __METHOD__, 10);
return $this;
}
/**
* @param string $company_id UUID
* @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|HolidayListFactory
*/
function getByCompanyIdAndHolidayPolicyId( $company_id, $id, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $id == '' ) {
return false;
}
if ( $order == null ) {
$order = [ 'date_stamp' => 'desc' ];
$strict = false;
} else {
$strict = true;
}
$hpf = new HolidayPolicyFactory();
$ph = [ 'company_id' => TTUUID::castUUID( $company_id ), ];
$query = '
select a.*
from ' . $this->getTable() . ' as a
LEFT JOIN ' . $hpf->getTable() . ' as b ON a.holiday_policy_id = b.id
where b.company_id = ?
AND a.holiday_policy_id in (' . $this->getListSQL( $id, $ph, 'uuid' ) . ')
AND ( a.deleted = 0 AND b.deleted = 0) ';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $user_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|HolidayListFactory
*/
function getByPolicyGroupUserId( $user_id, $where = null, $order = null ) {
if ( $user_id == '' ) {
return false;
}
if ( $order == null ) {
//$order = array( 'c.type_id' => 'asc', 'c.trigger_time' => 'desc' );
$strict = false;
} else {
$strict = true;
}
$pgf = new PolicyGroupFactory();
$pguf = new PolicyGroupUserFactory();
$hpf = new HolidayPolicyFactory();
$cgmf = new CompanyGenericMapFactory();
$ph = [
'user_id' => TTUUID::castUUID( $user_id ),
];
$query = '
select d.*
from ' . $pguf->getTable() . ' as a,
' . $pgf->getTable() . ' as b,
' . $hpf->getTable() . ' as c,
' . $cgmf->getTable() . ' as z,
' . $this->getTable() . ' as d
where a.policy_group_id = b.id
AND ( b.id = z.object_id AND z.company_id = b.company_id AND z.object_type_id = 180 AND z.deleted = 0 )
AND z.map_id = d.holiday_policy_id
AND d.holiday_policy_id = c.id
AND a.user_id = ?
AND ( c.deleted = 0 AND d.deleted = 0 AND b.deleted = 0 )
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $user_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|HolidayListFactory
*/
function getByPolicyGroupUserIdAndDate( $user_id, $date, $where = null, $order = null ) {
if ( $user_id == '' ) {
return false;
}
if ( $date == '' ) {
return false;
}
if ( $order == null ) {
//$order = array( 'c.type_id' => 'asc', 'c.trigger_time' => 'desc' );
$strict = false;
} else {
$strict = true;
}
$pgf = new PolicyGroupFactory();
$pguf = new PolicyGroupUserFactory();
$hpf = new HolidayPolicyFactory();
$cgmf = new CompanyGenericMapFactory();
$ph = [
'user_id' => TTUUID::castUUID( $user_id ),
'date' => $this->db->BindDate( $date ),
];
$query = '
select d.*
from ' . $pguf->getTable() . ' as a,
' . $pgf->getTable() . ' as b,
' . $hpf->getTable() . ' as c,
' . $cgmf->getTable() . ' as z,
' . $this->getTable() . ' as d
where a.policy_group_id = b.id
AND ( b.id = z.object_id AND z.company_id = b.company_id AND z.object_type_id = 180 AND z.deleted = 0 )
AND z.map_id = d.holiday_policy_id
AND d.holiday_policy_id = c.id
AND a.user_id = ?
AND d.date_stamp = ?
AND ( c.deleted = 0 AND d.deleted = 0 AND b.deleted = 0 )
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $user_id UUID
* @param int $start_date EPOCH
* @param int $end_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|HolidayListFactory
*/
function getByPolicyGroupUserIdAndStartDateAndEndDate( $user_id, $start_date, $end_date, $where = null, $order = null ) {
if ( $user_id == '' ) {
return false;
}
if ( $start_date == '' ) {
return false;
}
if ( $end_date == '' ) {
return false;
}
if ( $order == null ) {
$order = [ 'd.date_stamp' => 'desc' ];
$strict = false;
} else {
$strict = true;
}
$pgf = new PolicyGroupFactory();
$pguf = new PolicyGroupUserFactory();
$hpf = new HolidayPolicyFactory();
$cgmf = new CompanyGenericMapFactory();
$ph = [
'start_date' => $this->db->BindDate( $start_date ),
'end_date' => $this->db->BindDate( $end_date ),
];
$query = '
select distinct d.*
from ' . $pguf->getTable() . ' as a,
' . $pgf->getTable() . ' as b,
' . $hpf->getTable() . ' as c,
' . $cgmf->getTable() . ' as z,
' . $this->getTable() . ' as d
where a.policy_group_id = b.id
AND ( b.id = z.object_id AND z.company_id = b.company_id AND z.object_type_id = 180 AND z.deleted = 0 )
AND z.map_id = d.holiday_policy_id
AND d.holiday_policy_id = c.id
AND d.date_stamp >= ?
AND d.date_stamp <= ?
AND a.user_id in (' . $this->getListSQL( $user_id, $ph, 'uuid' ) . ')
AND ( c.deleted = 0 AND d.deleted=0 )
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $company_id UUID
* @param int $start_date EPOCH
* @param int $end_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|HolidayListFactory
*/
function getByCompanyIdAndStartDateAndEndDate( $company_id, $start_date, $end_date, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $start_date == '' ) {
return false;
}
if ( $end_date == '' ) {
return false;
}
if ( $order == null ) {
$order = [ 'd.date_stamp' => 'desc' ];
$strict = false;
} else {
$strict = true;
}
$pgf = new PolicyGroupFactory();
$pguf = new PolicyGroupUserFactory();
$hpf = new HolidayPolicyFactory();
$cgmf = new CompanyGenericMapFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'start_date' => $this->db->BindDate( $start_date ),
'end_date' => $this->db->BindDate( $end_date ),
];
$query = '
select distinct d.*, a.policy_group_id
from ' . $pguf->getTable() . ' as a,
' . $pgf->getTable() . ' as b,
' . $hpf->getTable() . ' as c,
' . $cgmf->getTable() . ' as z,
' . $this->getTable() . ' as d
where a.policy_group_id = b.id
AND ( b.id = z.object_id AND z.company_id = b.company_id AND z.object_type_id = 180 AND z.deleted = 0 )
AND z.map_id = d.holiday_policy_id
AND d.holiday_policy_id = c.id
AND b.company_id = ?
AND d.date_stamp >= ?
AND d.date_stamp <= ?
AND ( c.deleted = 0 AND d.deleted=0 )
';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $user_id UUID
* @param int $start_date EPOCH
* @param int $end_date EPOCH
* @return array|bool
*/
function getArrayByPolicyGroupUserId( $user_id, $start_date, $end_date ) {
$hlf = new HolidayListFactory();
$hlf->getByPolicyGroupUserIdAndStartDateAndEndDate( $user_id, $start_date, $end_date );
$list = [];
if ( $hlf->getRecordCount() > 0 ) {
foreach ( $hlf as $h_obj ) {
$list[$h_obj->getDateStamp()] = $h_obj->getName();
}
return $list;
}
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|HolidayListFactory
*/
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 = [];
$sort_column_aliases = [];
$order = $this->getColumnsFromAliases( $order, $sort_column_aliases );
if ( $order == null ) {
$order = [ 'date_stamp' => 'desc', 'name' => 'asc' ];
$strict = false;
} else {
if ( !isset( $order['date_stamp'] ) ) {
$order = Misc::prependArray( [ 'date_stamp' => 'desc' ], $order );
}
$strict = true;
}
//Debug::Arr($order, 'Order Data:', __FILE__, __LINE__, __METHOD__, 10);
//Debug::Arr($filter_data, 'Filter Data:', __FILE__, __LINE__, __METHOD__, 10);
$uf = new UserFactory();
$pgf = new PolicyGroupFactory();
$pguf = new PolicyGroupUserFactory();
$hpf = new HolidayPolicyFactory();
$cgmf = new CompanyGenericMapFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select distinct 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
LEFT JOIN ' . $hpf->getTable() . ' as hpf ON ( a.holiday_policy_id = hpf.id AND hpf.deleted = 0 )
LEFT JOIN ' . $cgmf->getTable() . ' as cgmf ON ( cgmf.company_id = hpf.company_id AND cgmf.object_type_id = 180 AND cgmf.map_id = a.holiday_policy_id AND cgmf.deleted = 0 )
LEFT JOIN ' . $pgf->getTable() . ' as pgf ON ( pgf.id = cgmf.object_id AND pgf.deleted = 0 )
LEFT JOIN ' . $pguf->getTable() . ' as pguf ON ( pguf.policy_group_id = pgf.id AND pgf.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 hpf.company_id = ?
';
$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;
$query .= ( isset( $filter_data['holiday_policy_id'] ) ) ? $this->getWhereClauseSQL( 'a.holiday_policy_id', $filter_data['holiday_policy_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['user_id'] ) ) ? $this->getWhereClauseSQL( 'pguf.user_id', $filter_data['user_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['name'] ) ) ? $this->getWhereClauseSQL( 'a.name', $filter_data['name'], 'text', $ph ) : null;
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_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;
}
}
?>
@@ -0,0 +1,725 @@
<?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\Holiday
*/
class RecurringHolidayFactory extends Factory {
protected $table = 'recurring_holiday';
protected $pk_sequence_name = 'recurring_holiday_id_seq'; //PK Sequence name
protected $company_obj = null;
/**
* @param $name
* @param null $parent
* @return array|null
*/
function _getFactoryOptions( $name, $parent = null ) {
$retval = null;
switch ( $name ) {
case 'special_day':
$retval = [
0 => TTi18n::gettext( 'N/A' ),
1 => TTi18n::gettext( 'Good Friday' ),
5 => TTi18n::gettext( 'Easter Sunday' ),
6 => TTi18n::gettext( 'Easter Monday' ),
];
break;
case 'type':
$retval = [
10 => TTi18n::gettext( 'Static' ),
20 => TTi18n::gettext( 'Dynamic: Week Interval' ),
30 => TTi18n::gettext( 'Dynamic: Pivot Day' ),
];
break;
case 'week_interval':
$retval = [
1 => TTi18n::gettext( '1st' ),
2 => TTi18n::gettext( '2nd' ),
3 => TTi18n::gettext( '3rd' ),
4 => TTi18n::gettext( '4th' ),
5 => TTi18n::gettext( '5th' ),
];
break;
case 'pivot_day_direction':
$retval = [
10 => TTi18n::gettext( 'Before' ),
20 => TTi18n::gettext( 'After' ),
30 => TTi18n::gettext( 'On or Before' ),
40 => TTi18n::gettext( 'On or After' ),
];
break;
case 'always_week_day':
$retval = [
//Adjust holiday to next weekday
0 => TTi18n::gettext( 'No' ),
1 => TTi18n::gettext( 'Yes - Previous Week Day' ),
2 => TTi18n::gettext( 'Yes - Next Week Day' ),
3 => TTi18n::gettext( 'Yes - Closest Week Day' ),
10 => TTi18n::gettext( 'Split - Sat=Sat, Sun=Mon' ),
20 => TTi18n::gettext( 'Split - Sat=Fri, Sun=Sun' ),
];
break;
case 'columns':
$retval = [
'-1010-name' => TTi18n::gettext( 'Name' ),
'-1010-type' => TTi18n::gettext( 'Type' ),
'-1020-next_date' => TTi18n::gettext( 'Next Date' ),
'-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( $this->getOptions( 'default_display_columns' ), Misc::trimSortPrefix( $this->getOptions( 'columns' ) ) );
break;
case 'default_display_columns': //Columns that are displayed by default.
$retval = [
'name',
'next_date',
'updated_date',
'updated_by',
];
break;
case 'unique_columns': //Columns that are unique, and disabled for mass editing.
$retval = [
'name',
];
break;
}
return $retval;
}
/**
* @param $data
* @return array
*/
function _getVariableToFunctionMap( $data ) {
$variable_function_map = [
'id' => 'ID',
'company_id' => 'Company',
'special_day' => 'SpecialDay',
'type_id' => 'Type',
'type' => false,
'pivot_day_direction_id' => 'PivotDayDirection',
'name' => 'Name',
'week_interval' => 'WeekInterval',
'day_of_week' => 'DayOfWeek',
'day_of_month' => 'DayOfMonth',
'month_int' => 'Month',
'always_week_day_id' => 'AlwaysOnWeekDay',
'next_date' => 'NextDate',
'deleted' => 'Deleted',
];
return $variable_function_map;
}
/**
* @return null
*/
function getCompanyObject() {
if ( is_object( $this->company_obj ) ) {
return $this->company_obj;
} else {
$clf = TTnew( 'CompanyListFactory' ); /** @var CompanyListFactory $clf */
$this->company_obj = $clf->getById( $this->getCompany() )->getCurrent();
return $this->company_obj;
}
}
/**
* @return bool|mixed
*/
function getCompany() {
return $this->getGenericDataValue( 'company_id' );
}
/**
* @param string $value UUID
* @return bool
*/
function setCompany( $value ) {
$value = TTUUID::castUUID( $value );
Debug::Text( 'Company ID: ' . $value, __FILE__, __LINE__, __METHOD__, 10 );
return $this->setGenericDataValue( 'company_id', $value );
}
/**
* @return bool|mixed
*/
function getSpecialDay() {
return $this->getGenericDataValue( 'special_day' );
}
/**
* @param $value
* @return bool
*/
function setSpecialDay( $value ) {
$value = trim( $value );
return $this->setGenericDataValue( 'special_day', $value );
}
/**
* @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 getPivotDayDirection() {
return $this->getGenericDataValue( 'pivot_day_direction_id' );
}
/**
* @param $value
* @return bool
*/
function setPivotDayDirection( $value ) {
$value = (int)trim( $value );
return $this->setGenericDataValue( 'pivot_day_direction_id', $value );
}
/**
* @param $name
* @return bool
*/
function isUniqueName( $name ) {
$name = trim( $name );
if ( $name == '' ) {
return false;
}
$ph = [
'company_id' => TTUUID::castUUID( $this->getCompany() ),
'name' => TTi18n::strtolower( $name ),
];
$query = 'select id from ' . $this->getTable() . ' 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 );
return $this->setGenericDataValue( 'name', $value );
}
/**
* @return bool|int
*/
function getWeekInterval() {
return (int)$this->getGenericDataValue( 'week_interval' );
}
/**
* @param $value
* @return bool
*/
function setWeekInterval( $value ) {
$value = trim( $value );
if ( empty( $value ) ) {
$value = 0;
}
return $this->setGenericDataValue( 'week_interval', $value );
}
/**
* @return bool|int
*/
function getDayOfWeek() {
return (int)$this->getGenericDataValue( 'day_of_week' );
}
/**
* @param $value
* @return bool
*/
function setDayOfWeek( $value ) {
$value = trim( $value );
if ( $value == '' ) {
$value = 0;
}
return $this->setGenericDataValue( 'day_of_week', $value );
}
/**
* @return bool|int
*/
function getDayOfMonth() {
return (int)$this->getGenericDataValue( 'day_of_month' );
}
/**
* @param $value
* @return bool
*/
function setDayOfMonth( $value ) {
$value = trim( $value );
if ( empty( $value ) ) {
$value = 0;
}
return $this->setGenericDataValue( 'day_of_month', $value );
}
/**
* @return bool|int
*/
function getMonth() {
return (int)$this->getGenericDataValue( 'month_int' );
}
/**
* @param $value
* @return bool
*/
function setMonth( $value ) {
$value = trim( $value );
if ( empty( $value ) ) {
$value = 0;
}
return $this->setGenericDataValue( 'month_int', $value );
}
/**
* @return bool|int
*/
function getAlwaysOnWeekDay() {
return (int)$this->getGenericDataValue( 'always_week_day_id' );
}
/**
* @param $value
* @return bool
*/
function setAlwaysOnWeekDay( $value ) {
$value = (int)$value;
return $this->setGenericDataValue( 'always_week_day_id', $value );
}
/**
* @param bool $epoch
* @return bool|false|int
*/
function getNextDate( $epoch = false, $exclude_dates = [] ) {
if ( $epoch == '' ) {
$epoch = TTDate::getTime();
}
if ( $this->getSpecialDay() == 1 || $this->getSpecialDay() == 5 || $this->getSpecialDay() == 6 ) {
Debug::text( 'Easter Sunday Date...', __FILE__, __LINE__, __METHOD__, 10 );
//Use easter_days() instead, as easter_date returns incorrect values for some timezones/years (2010 and US/Eastern on Windows)
//$easter_epoch = easter_date(date('Y', $epoch));
//$easter_epoch = mktime( 12, 0, 0, 3, ( 21 + easter_days( date('Y', $epoch) ) ), date('Y', $epoch) );
$easter_epoch = mktime( 12, 0, 0, 3, ( 21 + TTDate::getEasterDays( date( 'Y', $epoch ) ) ), date( 'Y', $epoch ) );
//Fix "cross-year" bug.
if ( $easter_epoch < $epoch ) {
//$easter_epoch = easter_date(date('Y', $epoch)+1);
//$easter_epoch = mktime( 12, 0, 0, 3, ( 21 + easter_days( (date('Y', $epoch) + 1) ) ), ( date('Y', $epoch) + 1 ) );
$easter_epoch = mktime( 12, 0, 0, 3, ( 21 + TTDate::getEasterDays( ( date( 'Y', $epoch ) + 1 ) ) ), ( date( 'Y', $epoch ) + 1 ) );
}
if ( $this->getSpecialDay() == 1 ) {
Debug::text( 'Good Friday Date...', __FILE__, __LINE__, __METHOD__, 10 );
//$holiday_epoch = mktime(12, 0, 0, date('n', $easter_epoch), date('j', $easter_epoch) - 2, date('Y', $easter_epoch));
$holiday_epoch = ( $easter_epoch - ( 2 * 86400 ) );
} else if ( $this->getSpecialDay() == 6 ) {
Debug::text( 'Easter Monday Date...', __FILE__, __LINE__, __METHOD__, 10 );
$holiday_epoch = ( $easter_epoch + 86400 );
} else {
$holiday_epoch = $easter_epoch;
}
} else {
if ( $this->getType() == 10 ) { //Static
Debug::text( 'Static Date...', __FILE__, __LINE__, __METHOD__, 10 );
//Static date
$holiday_epoch = mktime( 12, 0, 0, $this->getMonth(), $this->getDayOfMonth(), date( 'Y', $epoch ) );
if ( $holiday_epoch < $epoch ) {
$holiday_epoch = mktime( 12, 0, 0, $this->getMonth(), $this->getDayOfMonth(), ( date( 'Y', $epoch ) + 1 ) );
}
} else if ( $this->getType() == 20 ) { //Dynamic - Week Interval
Debug::text( 'Dynamic - Week Interval... Current Month: ' . TTDate::getMonth( $epoch ) . ' Holiday Month: ' . $this->getMonth(), __FILE__, __LINE__, __METHOD__, 10 );
//Dynamic
$start_month_epoch = TTDate::getBeginMonthEpoch( $epoch );
$end_month_epoch = mktime( 12, 0, 0, ( $this->getMonth() + 1 ), 1, ( date( 'Y', $epoch ) + 1 ) );
$tmp_holiday_epoch = false;
Debug::text( 'Start Epoch: ' . TTDate::getDate( 'DATE+TIME', $start_month_epoch ) . ' End Epoch: ' . TTDate::getDate( 'DATE+TIME', $end_month_epoch ) . ' Current Epoch: ' . TTDate::getDate( 'DATE+TIME', $epoch ), __FILE__, __LINE__, __METHOD__, 10 );
//Get all day of weeks in the month. Determine which is less or greater then day.
$day_of_week_dates = [];
$week_interval = 0;
//for ($i = $start_month_epoch; $i <= $end_month_epoch; $i += 86400) {
foreach ( TTDate::getDatePeriod( $start_month_epoch, $end_month_epoch, 'P1D' ) as $i ) {
if ( TTDate::getMonth( $i ) == $this->getMonth() ) {
$day_of_week = TTDate::getDayOfWeek( $i );
//Debug::text('I: '. $i .'('.TTDate::getDate('DATE+TIME', $i).') Current Day Of Week: '. $day_of_week .' Looking for Day Of Week: '. $this->getDayOfWeek(), __FILE__, __LINE__, __METHOD__, 10);
if ( $day_of_week == abs( $this->getDayOfWeek() ) ) {
$day_of_week_dates[] = date( 'j', $i );
Debug::text( 'I: ' . $i . ' Day Of Month: ' . date( 'j', $i ) . ' Week Interval: ' . $week_interval, __FILE__, __LINE__, __METHOD__, 10 );
$week_interval++;
}
if ( $week_interval >= $this->getWeekInterval() ) {
$tmp_holiday_epoch = mktime( 12, 0, 0, $this->getMonth(), $day_of_week_dates[( $this->getWeekInterval() - 1 )], date( 'Y', $i ) );
//Make sure we keep processing until the holiday comes AFTER todays date.
if ( $tmp_holiday_epoch > $epoch ) {
break;
}
}
} else {
//Outside the month we need to be in, so reset all other settings.
$week_interval = 0;
$day_of_week_dates = [];
}
}
$holiday_epoch = $tmp_holiday_epoch;
} else if ( $this->getType() == 30 ) { //Dynamic - Pivot Day
Debug::text( 'Dynamic - Pivot Date...', __FILE__, __LINE__, __METHOD__, 10 );
//Dynamic
if ( TTDate::getMonth( $epoch ) > $this->getMonth() ) {
$year_modifier = 1;
} else {
$year_modifier = 0;
}
$start_epoch = mktime( 12, 0, 0, $this->getMonth(), $this->getDayOfMonth(), ( date( 'Y', $epoch ) + $year_modifier ) );
$holiday_epoch = $start_epoch;
$x = 0;
$x_max = 100;
if ( $this->getPivotDayDirection() == 10 || $this->getPivotDayDirection() == 30 ) {
$direction_multiplier = -1;
} else {
$direction_multiplier = 1;
}
$adjustment = ( 86400 * $direction_multiplier ); // Adjust by 1 day before or after.
if ( $this->getPivotDayDirection() == 10 || $this->getPivotDayDirection() == 20 ) {
$holiday_epoch += $adjustment;
}
while ( $this->getDayOfWeek() != TTDate::getDayOfWeek( $holiday_epoch ) && $x < $x_max ) {
Debug::text( 'X: ' . $x . ' aTrying...' . TTDate::getDate( 'DATE+TIME', $holiday_epoch ), __FILE__, __LINE__, __METHOD__, 10 );
$holiday_epoch += $adjustment;
$x++;
}
}
}
if ( is_numeric( $holiday_epoch ) ) {
$holiday_epoch = TTDate::getNearestWeekDay( $holiday_epoch, $this->getAlwaysOnWeekDay(), $exclude_dates );
Debug::text( 'Next Date for: ' . $this->getName() . ' is: ' . TTDate::getDate( 'DATE+TIME', $holiday_epoch ), __FILE__, __LINE__, __METHOD__, 10 );
return $holiday_epoch;
}
Debug::text( ' Unable to get next date for holiday!', __FILE__, __LINE__, __METHOD__, 10 );
return false;
}
/**
* @param bool $ignore_warning
* @return bool
*/
function Validate( $ignore_warning = true ) {
//
// BELOW: Validation code moved from set*() functions.
//
// Company
$clf = TTnew( 'CompanyListFactory' ); /** @var CompanyListFactory $clf */
$this->Validator->isResultSetWithRows( 'company',
$clf->getByID( $this->getCompany() ),
TTi18n::gettext( 'Company is invalid' )
);
// Name
if ( $this->getName() !== false ) {
$this->Validator->isLength( 'name',
$this->getName(),
TTi18n::gettext( 'Name is invalid' ),
2, 50
);
if ( $this->Validator->isError( 'name' ) == false ) {
$this->Validator->isTrue( 'name',
$this->isUniqueName( $this->getName() ),
TTi18n::gettext( 'Name is already in use' )
);
}
}
// Special Day
$this->Validator->inArrayKey( 'special_day',
$this->getSpecialDay(),
TTi18n::gettext( 'Incorrect Special Day' ),
$this->getOptions( 'special_day' )
);
// Type
if ( $this->getType() !== false ) {
$this->Validator->inArrayKey( 'type',
$this->getType(),
TTi18n::gettext( 'Incorrect Type' ),
$this->getOptions( 'type' )
);
}
// 10 => TTi18n::gettext('Static'),
// 20 => TTi18n::gettext('Dynamic: Week Interval'),
// 30 => TTi18n::gettext('Dynamic: Pivot Day')
if ( in_array( $this->getType(), [ 20 ] ) ) { //20=Dynamic: Week Interval
// Week Interval
$this->Validator->isNumeric( 'week_interval',
$this->getWeekInterval(),
TTi18n::gettext( 'Incorrect Week Interval' )
);
// Day Of Week
$this->Validator->isNumeric( 'day_of_week',
$this->getDayOfWeek(),
TTi18n::gettext( 'Incorrect Day Of Week' )
);
}
// Pivot Day Direction
if ( in_array( $this->getType(), [ 30 ] ) && $this->getPivotDayDirection() !== false ) { //30=Dynamic: Pivot Day
$this->Validator->inArrayKey( 'pivot_day_direction',
$this->getPivotDayDirection(),
TTi18n::gettext( 'Incorrect Pivot Day Direction' ),
$this->getOptions( 'pivot_day_direction' )
);
}
if ( in_array( $this->getType(), [ 10, 30 ] ) ) { //10=Static, 30=Dynamic: Pivot Day
// Day Of Month
$this->Validator->isNumeric( 'day_of_month',
$this->getDayOfMonth(),
TTi18n::gettext( 'Incorrect Day Of Month' )
);
}
// Month
$this->Validator->isNumeric( 'month',
$this->getMonth(),
TTi18n::gettext( 'Incorrect Month' )
);
// Always on week day adjustment
$this->Validator->inArrayKey( 'always_week_day_id',
$this->getAlwaysOnWeekDay(),
TTi18n::gettext( 'Incorrect always on week day adjustment' ),
$this->getOptions( 'always_week_day' )
);
//
// ABOVE: Validation code moved from set*() functions.
//
//Don't allow deleting recurring holidays unless they are no longer in use by Holiday Policies and Remittances Agency's. Contributing Shift Policies use Holiday Policies not recurring holidays.
if ( $this->getDeleted() == true ) {
$hprhlf = TTNew( 'HolidayPolicyRecurringHolidayListFactory' ); /** @var HolidayPolicyRecurringHolidayListFactory $hprhlf */
$hprhlf->getByCompanyIdAndRecurringHolidayId( $this->getCompany(), $this->getId() );
if ( $hprhlf->getRecordCount() > 0 ) {
$this->Validator->isTRUE( 'in_use',
false,
TTi18n::gettext( 'This recurring holiday is currently in use' ) . ' ' . TTi18n::gettext( 'by holiday policies' ) );
}
$cgmlf = TTnew( 'CompanyGenericMapListFactory' ); /** @var CompanyGenericMapListFactory $cgmlf */
$cgmlf->getByCompanyIDAndObjectTypeAndMapID( $this->getCompany(), 5000, $this->getID() );
if ( $cgmlf->getRecordCount() > 0 ) {
$this->Validator->isTRUE( 'in_use',
false,
TTi18n::gettext( 'This recurring holiday is currently in use' ) . ' ' . TTi18n::gettext( 'by remittance agencies' ) );
}
}
return true;
}
/**
* @param $data
* @return bool
*/
function setObjectFromArray( $data ) {
if ( is_array( $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 'type':
case 'status':
$function = 'get' . $variable;
if ( method_exists( $this, $function ) ) {
$data[$variable] = Option::getByKey( $this->$function(), $this->getOptions( $variable ) );
}
break;
case 'next_date':
if ( method_exists( $this, $function ) ) {
$data[$variable] = TTDate::getAPIDate( 'DATE', $this->$function() );
}
break;
default:
if ( method_exists( $this, $function ) ) {
$data[$variable] = $this->$function();
}
break;
}
}
}
$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( 'Recurring Holiday' ) .': '. $this->getName(), null, $this->getTable(), $this );
}
}
?>
@@ -0,0 +1,279 @@
<?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\Holiday
*/
class RecurringHolidayListFactory extends RecurringHolidayFactory 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|RecurringHolidayListFactory
*/
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|RecurringHolidayListFactory
*/
function getByIdAndCompanyId( $id, $company_id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select *
from ' . $this->getTable() . '
where company_id = ?
AND id in (' . $this->getListSQL( $id, $ph, 'uuid' ) . ')
AND deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
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|RecurringHolidayListFactory
*/
function getByCompanyId( $id, $limit = null, $page = null, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
/*
if ( $order == NULL ) {
$order = array( 'type_id' => 'asc' );
$strict = FALSE;
} else {
$strict = TRUE;
}
*/
$ph = [
'company_id' => TTUUID::castUUID( $id ),
];
$query = '
select *
from ' . $this->getTable() . ' as a
where company_id = ?
AND deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$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|RecurringHolidayListFactory
*/
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 = [ 'type_id' ];
$sort_column_aliases = [
'type' => 'type_id',
'next_date' => false, //Don't sort
];
$order = $this->getColumnsFromAliases( $order, $sort_column_aliases );
if ( $order == null ) {
$order = [ 'name' => 'asc' ];
$strict = false;
} else {
//Always sort by 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();
$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
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 = ?
';
$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;
$query .= ( isset( $filter_data['type_id'] ) ) ? $this->getWhereClauseSQL( 'a.type_id', $filter_data['type_id'], 'numeric_list', $ph ) : null;
$query .= ( isset( $filter_data['name'] ) ) ? $this->getWhereClauseSQL( 'a.name', $filter_data['name'], 'text', $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;
}
/**
* @param string $company_id UUID
* @param bool $include_blank
* @return array|bool
*/
function getByCompanyIdArray( $company_id, $include_blank = true ) {
$rhlf = new RecurringHolidayListFactory();
$rhlf->getByCompanyId( $company_id );
$list = [];
if ( $include_blank == true ) {
$list[TTUUID::getZeroID()] = '--';
}
foreach ( $rhlf as $rh_obj ) {
$list[$rh_obj->getID()] = $rh_obj->getName();
}
if ( empty( $list ) == false ) {
return $list;
}
return false;
}
}
?>