Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This is the MongoDB Document model class based on table "user_leaves".
- */
- class UserLeaves extends EMongoDocument {
- const STATUS_PENDING = 1;
- const STATUS_APPROVED = 2;
- const STATUS_REJECTED = 3;
- const STATUS_REVOKED = 4;
- const ISOVERUTILIZED_YES = 1;
- const ISOVERUTILIZED_NO = 0;
- const LEAVE_PAID = 0;
- const LEAVE_UNPAID = 1;
- const HALF_DAY_NO = 0;
- const HALF_DAY_YES = 1;
- const LEAVE_ACTION_APPROVE = "approve";
- const LEAVE_ACTION_REVOKE = "revoke";
- public $id;
- public $leave_id;
- public $parent_leave_id;
- public $user_id;
- public $from_date;
- public $to_date;
- public $isUnpaid = self::LEAVE_PAID;
- public $isUnpaidAll = self::LEAVE_PAID;
- public $overutilization_other_leave = self::LEAVE_UNPAID;
- public $is_half_day;
- public $orginal_leave;
- public $status;
- public $message;
- public $manager_message;
- public $tenant_id;
- public $created;
- public $is_overutilized = self::ISOVERUTILIZED_NO;
- public $compoff_used_date = array();
- public $userOptionalHoliday = array();
- // public $optional_holiday_date = array();
- // public $optional_status;
- /**
- * Returns the static model of the specified AR class.
- * @return UserLeaves the static model class
- */
- public static function model($className = __CLASS__) {
- return parent::model($className);
- }
- /**
- * returns the primary key field for this model
- */
- public function primaryKey() {
- return 'id';
- }
- /**
- * @return string the associated collection name
- */
- public function getCollectionName() {
- return 'user_leaves';
- }
- /**
- * @return array validation rules for model attributes.
- */
- public function rules() {
- // NOTE: you should only define rules for those attributes that
- // will receive user inputs.
- return array(
- array('leave_id, from_date, to_date, message', 'required', 'on' => 'apply'),
- array('leave_id, from_date, to_date, message', 'required', 'on' => 'import'),
- array('status', 'numerical', 'integerOnly' => true),
- array('leave_id, user_id, tenant_id', 'length', 'max' => 20),
- array('from_date, to_date', 'compareDates', 'on' => 'unpaid'),
- array('from_date, to_date', 'compareDates', 'on' => 'apply'),
- //array('from_date, to_date', 'compareDatesMonth', 'on' => 'apply'),
- array('from_date, to_date', 'checkyear', 'on' => 'apply'),
- array('from_date, to_date', 'compareImportDates', 'on' => 'import'),
- array('from_date', 'checkHalfDayDate', 'compare_date' => 'to_date'),
- /* Validating various leave policies based on 'leave_id' */
- array('leave_id', 'ext.validators.CLeavePoliciesValidator', 'on' => 'apply'),
- array('leave_id', 'ext.validators.CLeavePoliciesValidatorUnpaid', 'on' => 'unpaid'),
- );
- }
- public function checkHalfDayDate($attribute, $params) {
- if (!$this->hasErrors()) {
- if ($this->is_half_day == self::HALF_DAY_YES) {
- if ($this->$attribute->sec != $this->$params['compare_date']->sec) {
- $this->addError($attribute, 'Start date and End date should be same.');
- }
- }
- }
- }
- public function compareDates($attribute, $params)
- {
- if (!$this->hasErrors())
- {
- if ($this->to_date == '' || $this->to_date == '0.00000000 0')
- {
- $this->addError($attribute, 'Plese enter End Date');
- }
- else if ($this->from_date == '' || $this->from_date == '0.00000000 0')
- {
- $this->addError($attribute, 'Plese enter Start Date');
- }
- else if ($this->from_date > $this->to_date)
- {
- $this->addError($attribute, 'End Date cannot be before Start Date');
- }
- }
- }
- public function compareImportDates($attribute, $params) {
- if (!$this->hasErrors()) {
- if ($this->to_date == '') {
- $this->addError($attribute, 'Plese enter End Date');
- } else if ($this->from_date->sec > $this->to_date->sec) {
- $this->addError($attribute, 'End Date cannot be before Start Date');
- }
- }
- }
- public function compareDatesMonth($attribute, $params) {
- if (!$this->hasErrors()) {
- if ($this->from_date->sec < strtotime(date('Y-m-d', strtotime('-1 month')))) {
- $this->addError($attribute, 'Invalid Start Date (Leave applications before ' . date('Y-m-d', strtotime('-1 month')) . ' are not allowed).');
- }
- }
- }
- public function checkyear($attribute, $params)
- {
- if (!$this->hasErrors())
- {
- $leave = Leaves::model()->findByPk($this->leave_id);
- if (isset($leave->p4_carry_over_time) && $leave->p4_carry_over_time == 1)
- {
- // If leave is "Compensatory Off" then allow user for apply leave for next year is user is applying leave for Jan in Dec month. KT 10th Dec 2015
- if($leave->name == "Compensatory Off")
- {
- $currentdate = date("Y") + 1;
- }
- else
- {
- $currentdate = date("Y");
- }
- $start_date = date('Y', $this->from_date->sec);
- $end_date = date('Y', $this->to_date->sec);
- if ($currentdate != $end_date)
- {
- if ($end_date > $start_date)
- {
- $this->addError($attribute, 'Leave applications cannot have dates from the next annual leave cycle. You can apply for such leaves only after the completion of your current annual leave cycle.');
- }
- }
- if ($start_date > $currentdate)
- {
- $this->addError($attribute, 'Leave applications cannot have dates from the next annual leave cycle. You can apply for such leaves only after the completion of your current annual leave cycle.');
- }
- }
- else
- {
- $currentdate = date("Y-m");
- $start_date = date('Y-m', $this->from_date->sec);
- $end_date = date('Y-m', $this->to_date->sec);
- $currentdatearr = explode('-', $currentdate);
- $end_datearr = explode('-', $end_date);
- $start_datearr = explode('-', $start_date);
- if ($start_datearr[0] > $currentdatearr[0])
- {
- if ($start_datearr[1] > 3)
- {
- $this->addError($attribute, 'Leave applications cannot have dates from the next annual leave cycle. You can apply for such leaves only after the completion of your current annual leave cycle1.');
- }
- if ($start_datearr[0] > $currentdatearr[0] + 1)
- {
- $this->addError($attribute, 'Leave applications cannot have dates from the next annual leave cycle. You can apply for such leaves only after the completion of your current annual leave cycle2.');
- }
- }
- if ($start_datearr[0] == $currentdatearr[0] + 1)
- {
- if ($end_datearr[1] > 3)
- {
- $this->addError($attribute, 'Leave applications cannot have dates from the next annual leave cycle. You can apply for such leaves only after the completion of your current annual leave cycle3.');
- }
- }
- if ($end_datearr[0] > $start_datearr[0])
- {
- if ($end_datearr[0] > $start_datearr[0] + 1)
- {
- $this->addError($attribute, 'Leave applications cannot have dates from the next annual leave cycle. You can apply for such leaves only after the completion of your current annual leave cycle.4');
- }
- if ($end_datearr[1] > 3)
- {
- $this->addError($attribute, 'Leave applications cannot have dates from the next annual leave cycle. You can apply for such leaves only after the completion of your current annual leave cycle5.');
- }
- }
- }
- }
- }
- /**
- * @return array customized attribute labels (name=>label)
- */
- public function attributeLabels() {
- return array(
- 'id' => 'ID',
- 'leave_id' => 'Leave Type',
- 'user_id' => 'User',
- 'tenant_id' => 'Tenant',
- 'from_date' => 'Start Date',
- 'to_date' => 'End Date',
- 'status' => 'Status',
- 'message' => 'Message',
- 'manager_message' => 'Manager Message',
- 'created' => 'Created',
- );
- }
- public static function itemAlias($type, $code = NULL) {
- $_items = array(
- 'leavetype' => array(
- self::STATUS_PENDING => 'Pending',
- self::STATUS_APPROVED => 'Approved',
- self::STATUS_REJECTED => 'Rejected',
- self::STATUS_REVOKED => 'Revoked',
- ),
- 'isUnpaid' => array(
- self::LEAVE_PAID => 'No',
- self::LEAVE_UNPAID => 'Yes',
- ),
- );
- if (isset($code))
- return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
- else
- return isset($_items[$type]) ? $_items[$type] : false;
- }
- public function beforeSave() {
- if ($this->isNewRecord) {
- $this->id = uniqid();
- $this->tenant_id = ($this->tenant_id == null) ? Yii::app()->user->getTenantId() : $this->tenant_id;
- $this->created = new MongoDate(time());
- }
- return parent::beforeSave();
- }
- public function tenant($tenant_id = null) {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'tenant_id' => array('==' => ($tenant_id == null) ? Yii::app()->user->getTenantId() : $tenant_id),
- ),
- ));
- return $this;
- }
- public function leaveUser($user_id = null) {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'user_id' => array('==' => ($user_id == null) ? Yii::app()->user->id : $user_id),
- ),
- ));
- return $this;
- }
- public function leave_request($user_id) {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'user_id' => array('==' => $user_id),
- 'status' => array('==' => self::STATUS_PENDING),
- ),
- ));
- return $this;
- }
- public function user_approved_leave($user_id = null, $leave_id = null , $start = null , $end = null)
- {
- if ($leave_id != null && $start == null && $end == null && $user_id != null)
- {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'user_id' => array('==' => $user_id),
- 'status' => array('==' => self::STATUS_APPROVED),
- 'leave_id' => array('==' => $leave_id),
- ),
- ));
- }
- elseif ($start != null && $end != null && $user_id != null)
- {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'user_id' => array('==' => $user_id),
- 'to_date' => array('>=' => $start),
- 'from_date' => array('<=' => $end),
- 'leave_id' => array('==' => $leave_id),
- 'status' => array('==' => self::STATUS_APPROVED),
- ),
- ));
- }
- elseif ($leave_id != null && $start != null && $end == null && $user_id != null)
- {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'user_id' => array('==' => $user_id),
- 'from_date' => array('>=' => $start),
- 'leave_id' => array('==' => $leave_id),
- 'status' => array('==' => self::STATUS_APPROVED),
- ),
- ));
- }elseif ($leave_id != null && $start == null && $end == null && $user_id == null)
- {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'leave_id' => array('==' => $leave_id),
- 'status' => array('==' => self::STATUS_APPROVED),
- ),
- ));
- }
- elseif ($leave_id == null && $start == null && $end == null && $user_id == null)
- {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'status' => array('==' => self::STATUS_APPROVED),
- ),
- ));
- }
- else
- {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'user_id' => array('==' => $user_id),
- 'status' => array('==' => self::STATUS_APPROVED),
- ),
- ));
- }
- return $this;
- }
- public function related($related) {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'user_id' => array('in' => $related),
- ),
- ));
- return $this;
- }
- public function user_leaves($user_id, $leave_id = null) {
- if ($leave_id != null) {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'user_id' => array('==' => $user_id),
- 'leave_id' => array('==' => $leave_id),
- ),
- ));
- } else {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'user_id' => array('==' => $user_id),
- ),
- ));
- }
- return $this;
- }
- public function approved() {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'status' => array('==' => self::STATUS_APPROVED),
- ),
- 'sort' => array('from_date' => EMongoCriteria::SORT_DESC),
- ));
- return $this;
- }
- public function pending() {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'status' => array('==' => self::STATUS_PENDING),
- ),
- ));
- return $this;
- }
- public function inRange($start, $end) {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'to_date' => array('>=' => $start),
- 'from_date' => array('<=' => $end),
- ),
- 'sort' => array('from_date' => EMongoCriteria::SORT_ASC),
- ));
- return $this;
- }
- public function paid() {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'isUnpaid' => array('==' => UserLeaves::LEAVE_PAID),
- ),
- ));
- return $this;
- }
- public function unpaidCalander() {
- $today = new MongoDate(strtotime(date('Y-m-d')));
- $year = (int) date('Y') - 1;
- $duration = new MongoDate(strtotime("$year-12-31"));
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'isUnpaid' => array('==' => UserLeaves::LEAVE_UNPAID),
- 'from_date' => array('<=' => $today),
- 'from_date' => array('>' => $duration),
- ),
- ));
- return $this;
- }
- public function unpaidFinancial()
- {
- $tenant_profile = TenantProfile::model()->active()->tenant($this->tenant_id)->find();
- $currentyear = (int) date('Y');
- $currentmonth = (int) date('m');
- $financialmonth = 4;
- if(isset($tenant_profile->financial_cycle) && $tenant_profile->financial_cycle != "")
- {
- $financialmonth = (int) $tenant_profile->financial_cycle;
- if($currentmonth >= $financialmonth)
- {
- $duration = new MongoDate(strtotime("$currentyear-$financialmonth-1"));
- }
- if($currentmonth < $financialmonth)
- {
- $year = (int) date('Y') -1;
- $duration = new MongoDate(strtotime("$year-$financialmonth-1"));
- }
- }
- else
- {
- if($currentmonth >= $financialmonth)
- {
- $duration = new MongoDate(strtotime("$currentyear-$financialmonth-1"));
- }
- if($currentmonth < $financialmonth)
- {
- $year = (int) date('Y') -1;
- $duration = new MongoDate(strtotime("$year-$financialmonth-1"));
- }
- }
- $today = new MongoDate(strtotime(date('Y-m-d')));
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'isUnpaid' => array('==' => UserLeaves::LEAVE_UNPAID),
- 'from_date' => array('<=' => $today),
- 'from_date' => array('>=' => $duration),
- ),
- ));
- return $this;
- }
- public function unpaid() {
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'isUnpaid' => array('==' => UserLeaves::LEAVE_UNPAID),
- ),
- ));
- return $this;
- }
- public function duration($duration) {
- $today = new MongoDate(strtotime(date('Y-m-d')));
- $duration = new MongoDate(strtotime(date('Y-m-d', strtotime('-' . $duration . ' Month'))));
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'from_date' => array('<=' => $today),
- 'from_date' => array('>' => $duration),
- ),
- 'sort' => array('created' => EMongoCriteria::SORT_DESC),
- ));
- return $this;
- }
- public function forMonth($for_month= null, $from = null, $to = null) {
- if(isset($from,$to) && $from != null && $to != null)
- {
- $first_day = strtotime($from);
- $last_day = strtotime($to);
- }
- else
- {
- $first_day = strtotime($for_month."-01");
- $last_day = strtotime($for_month."-".date('t', $first_day));
- }
- $this->getDbCriteria()->mergeWith(array(
- 'conditions' => array(
- 'from_date' => array('>=' => new MongoDate($first_day)),
- 'to_date' => array('<=' => new MongoDate($last_day)),
- ),
- 'sort' => array('created' => EMongoCriteria::SORT_DESC),
- ));
- return $this;
- }
- public function getCountOfMonths($user_id, $leave_id, $for_month = null , $is_paid = 1, $from = null, $to = null, $location = null, $getCount=false, $getCountOfRang = false)
- {
- if(isset($from,$to) && $from != null && $to != null)
- {
- $from = strtotime($from);
- $to = strtotime($to);
- }
- else
- {
- $from = strtotime($for_month."-01");
- $to = strtotime($for_month."-".date('t', $from));
- }
- $criteria = new EMongoCriteria();
- $criteria->from_date('<=', new MongoDate($to));
- $criteria->from_date('>=', new MongoDate($from));
- if($is_paid == 1)
- {
- $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->paid()->findAll($criteria);
- }
- else
- {
- $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->unpaid()->findAll($criteria);
- }
- $total_working_days_one = array();
- foreach ($leaves_approved as $leaves)
- {
- $applied_leave_data = Leaves::model()->tenant($leaves->tenant_id)->findByPk($leaves->leave_id);
- if(isset($applied_leave_data->policy_count_intervening_holidays->status) && $applied_leave_data->policy_count_intervening_holidays->status == LeavePolicy_InterveningHolidays::STATUS_ACTIVE)
- {
- //if(isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off == 1)
- if(isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday']) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday'] == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off']) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off'] == 1)
- {
- /***Code correction not counting leaves applied on weekoff/holiday only if Intervening is ON [SG 10 Feb 2016]***/
- if($getCount)
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDaysAccToLeavesPolicy($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id, $leaves->id, $location, true, $leaves->userOptionalHoliday));
- }
- else
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- }
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- elseif (isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday']) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday'] == 1 && ((isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off']) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off'] == 0) || (!isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off'])) ))
- {
- /***Added below code not counting leaves applied on weekoff/holiday only if Intervening is ON [SG 16 Feb 2016]***/
- if($getCount)
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDaysAccToLeavesPolicy($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id, $leaves->id, $location, true, $leaves->userOptionalHoliday));
- }
- else
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- }
- //$working_days = RandomHelper::getWorkingDays($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- else
- {
- /***Added below code not counting leaves applied on weekoff/holiday only if Intervening is ON [SG 16 Feb 2016]***/
- if($getCount)
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDaysAccToLeavesPolicy($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id, $leaves->id, $location, true, $leaves->userOptionalHoliday));
- }
- else
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- $holidays_year = RandomHelper::getHolidaysWithYear($leaves->from_date->sec, $leaves->to_date->sec, $leaves->tenant_id, array(), $leaves->user($leaves->tenant_id)->personal->office_location);
- /**/
- $total_working_days_one = RandomHelper::excludeHolidaysNew($total_working_days_one, $holidays_year);
- }
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- }
- else
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- /**/
- // $holidays_year = RandomHelper::getHolidaysWithYear($leaves->from_date->sec, $leaves->to_date->sec, $leaves->tenant_id, array(), $leaves->user($leaves->tenant_id)->personal->office_location);
- //Commented above it was fetching only current year holidays now will get holidays as per the applied date[SG 12 Feb 2016]
- $holidays_year = RandomHelper::getHolidaysWithYearForHoliday($leaves->from_date->sec, $leaves->to_date->sec, $leaves->tenant_id, $leaves->user($leaves->tenant_id)->personal->office_location, $leaves->user($leaves->tenant_id)->id, $leaves->userOptionalHoliday);
- $total_working_days_one = RandomHelper::excludeHolidaysNew($total_working_days_one, $holidays_year);
- //$total_days = count(RandomHelper::excludeHolidays($working_days, $holidays));
- }
- //$total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- }
- $criteria = new EMongoCriteria();
- $criteria->to_date('>=', new MongoDate($from));
- $criteria->to_date('<=', new MongoDate($to));
- // $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->paid()->findAll($criteria);
- if($is_paid == 1)
- {
- $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->paid()->findAll($criteria);
- }
- else
- {
- $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->unpaid()->findAll($criteria);
- }
- /*** Previous leaves Calculation if applied on last month and carrying to next month [SG 11 Feb 2016] ***/
- $past_dates = array();
- if(isset($leaves_approved) && !empty($leaves_approved))
- {
- foreach ($leaves_approved as $model)
- {
- if($model->from_date->sec < $from)
- {
- $total_new_days = RandomHelper::getDaysWithDate($model->from_date->sec, $model->to_date->sec, true);
- $weekoff = UserWeeklyOff::getNonWorkingDays($model->user_id, $model->from_date->sec, $model->to_date->sec, $model->tenant_id);
- $past_dates = array_diff($total_new_days, $weekoff);
- }
- }
- }
- $total_working_days_two = array();
- foreach ($leaves_approved as $leaves)
- {
- $applied_leave_data = Leaves::model()->tenant($leaves->tenant_id)->findByPk($leaves->leave_id);
- if(isset($applied_leave_data->policy_count_intervening_holidays->status) && $applied_leave_data->policy_count_intervening_holidays->status == LeavePolicy_InterveningHolidays::STATUS_ACTIVE)
- {
- if(isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday']) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday'] == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off']) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off'] == 1)
- {
- /***Code correction not counting leaves applied on weekoff/holiday only if Intervening is ON [SG 10 Feb 2016]***/
- if($getCount)
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDaysAccToLeavesPolicy($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id, $leaves->id, $location, true, $leaves->userOptionalHoliday));
- if(!empty($past_dates))
- {
- /** This is for leave applied from last month to this month only **/
- $total_working_days_two = array_merge($total_working_days_two, $past_dates);
- }
- }
- else
- {
- $total_working_days_two = array_merge($total_working_days_one, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- }
- // $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- }
- elseif (isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday']) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday'] == 1 && ((isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off']) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off'] == 0) || (!isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off'])) ))
- {
- /***Code correction not counting leaves applied on weekoff/holiday only if Intervening is ON [SG 16 Feb 2016]***/
- if($getCount)
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDaysAccToLeavesPolicy($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id, $leaves->id, $location, true, $leaves->userOptionalHoliday));
- if(!empty($past_dates))
- {
- /** This is for leave applied from last month to this month only **/
- $total_working_days_two = array_merge($total_working_days_two, $past_dates);
- }
- }
- else
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDays($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- }
- else
- {
- /***Code correction not counting leaves applied on weekoff/holiday only if Intervening is ON [SG 16 Feb 2016]***/
- if($getCount)
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDaysAccToLeavesPolicy($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id, $leaves->id, $location, true, $leaves->userOptionalHoliday));
- if(!empty($past_dates))
- {
- /** This is for leave applied from last month to this month only **/
- $total_working_days_two = array_merge($total_working_days_two, $past_dates);
- }
- }
- else
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- $holidays_year = RandomHelper::getHolidaysWithYear($leaves->from_date->sec, $leaves->to_date->sec, $leaves->tenant_id, array(), $leaves->user($leaves->tenant_id)->personal->office_location);
- $total_working_days_two = RandomHelper::excludeHolidaysNew($total_working_days_two, $holidays_year);
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- }
- }
- else
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- // $holidays_year = RandomHelper::getHolidaysWithYear($leaves->from_date->sec, $leaves->to_date->sec, $leaves->tenant_id, array(), $leaves->user($leaves->tenant_id)->personal->office_location);
- //Commented above it was fetching only current year holidays now will get holidays as per the applied date[SG 12 Feb 2016]
- $holidays_year = RandomHelper::getHolidaysWithYearForHoliday($leaves->from_date->sec, $leaves->to_date->sec, $leaves->tenant_id, $leaves->user($leaves->tenant_id)->personal->office_location, $leaves->user($leaves->tenant_id)->id, $leaves->userOptionalHoliday);
- /**/
- $total_working_days_two = RandomHelper::excludeHolidaysNew($total_working_days_two, $holidays_year);
- //$total_days = count(RandomHelper::excludeHolidays($working_days, $holidays));
- }
- // $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- }
- if(empty($total_working_days_two))
- {
- $total_working_days_two = array_merge($total_working_days_two, $total_working_days_one);
- }
- else
- {
- foreach ($total_working_days_one as $key_one_new => $value_one_new)
- {
- if( date("Y-m",strtotime($value_one_new)) == $for_month )
- {
- $total_working_days_two[] = $value_one_new;
- }
- if( $getCountOfRang == true ){
- $total_working_days_two[] = $value_one_new;
- }
- }
- $total_working_days_two = array_unique($total_working_days_two, SORT_REGULAR);
- }
- //filter for current month days
- $days_list = self::getDateList($from, $to);
- $total_working_days_two_new = array();
- foreach ($total_working_days_two as $total_working_days)
- {
- if(in_array($total_working_days, $days_list))
- {
- $total_working_days_two_new[] = $total_working_days;
- }
- }
- return count($total_working_days_two_new);
- }
- public static function getDateList($from, $to) {
- $date_list = array();
- while($from <= $to) {
- $date_list[] = date('Y-m-d', $from);
- $from = strtotime('+1 day', $from);
- }
- return $date_list;
- }
- public function message($tenant_id = null) {
- if(isset($tenant_id) && $tenant_id == null)
- {
- return Messages::model()->tenant()->findByAttributes(array('filter_type' => Notifications::FILTER_LEAVE, 'attachments.items.id' => $this->id, 'thread_id' => '0'));
- }
- else
- {
- return Messages::model()->tenant($tenant_id)->findByAttributes(array('filter_type' => Notifications::FILTER_LEAVE, 'attachments.items.id' => $this->id, 'thread_id' => '0'));
- }
- }
- public function leaveType() {
- return Leaves::model()->tenant()->findByPk($this->leave_id);
- }
- public function user($tenant_id = null) {
- return UserMongo::model()->tenant($tenant_id)->findByPk($this->user_id);
- }
- public static function paidUnpaid() {
- $data = array(
- UserLeaves::LEAVE_PAID => UserLeaves::itemAlias('isUnpaid', UserLeaves::LEAVE_PAID),
- UserLeaves::LEAVE_UNPAID => UserLeaves::itemAlias('isUnpaid', UserLeaves::LEAVE_UNPAID)
- );
- return $data;
- }
- public function interveningHolidays($tenant_id = null, $isHalfDay = false) {
- $leave = Leaves::model()->findByPk($this->leave_id);
- if( !isset($leave) && empty($leave) ) {
- $leave_unpaid = TenantUnpaidLeave::model()->findByAttributes(array('tenant_id' => Yii::app()->user->getTenantId()));
- if(isset($leave_unpaid) && !empty($leave_unpaid)) {
- if(isset($leave_unpaid->count_weekly_off,$leave_unpaid->count_holiday) && ($leave_unpaid->count_weekly_off == TenantUnpaidLeave::STATUS_ACTIVE || $leave_unpaid->count_holiday == 2)) {
- $leave->policy_count_intervening_holidays->status = LeavePolicy_InterveningHolidays::STATUS_ACTIVE;
- if($leave_unpaid->count_weekly_off == TenantUnpaidLeave::STATUS_ACTIVE && $leave_unpaid->count_holiday == 2)
- {
- $leave->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off'] = LeavePolicy_InterveningHolidays::COUNT_INTERVENING_HOLIDAY;
- $leave->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday'] = LeavePolicy_InterveningHolidays::COUNT_INTERVENING_HOLIDAY;
- } elseif($leave_unpaid->count_weekly_off == TenantUnpaidLeave::STATUS_ACTIVE) {
- $leave->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off'] = LeavePolicy_InterveningHolidays::COUNT_INTERVENING_HOLIDAY;
- } elseif($leave_unpaid->count_holiday == 2) {
- $leave->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday'] = LeavePolicy_InterveningHolidays::COUNT_INTERVENING_HOLIDAY;
- }
- } else {
- $leave->policy_count_intervening_holidays->status = LeavePolicy_InterveningHolidays::STATUS_INACTIVE;
- }
- }
- }
- if ($leave != null && $this->user($tenant_id) != null && $this->user($tenant_id)->department != null) {
- $all_days = RandomHelper::getDaysWithDate($this->from_date->sec, $this->to_date->sec, true);
- $non_working_days_new_check = UserWeeklyOff::getNonWorkingDays($this->user($tenant_id)->id, $this->from_date->sec, $this->to_date->sec, $this->tenant_id);
- $holidays_year_new_check = RandomHelper::getHolidaysWithYearForHoliday($this->from_date->sec, $this->to_date->sec, $tenant_id, $this->user($tenant_id)->personal->office_location, $this->user($tenant_id)->id, $this->userOptionalHoliday);
- $original_applied_days = $all_days;
- $common_days = array_intersect($non_working_days_new_check, $holidays_year_new_check);
- $all_days = array_diff($all_days, $common_days);
- // Count Intervening Holidays in Leave
- if ($leave->policy_count_intervening_holidays->status == LeavePolicy_InterveningHolidays::STATUS_ACTIVE) {
- if(!empty($all_days) ) {
- if ($leave->policy_count_intervening_holidays->count_intervening_holidays != null)
- {
- // this condition count both are true (PUBLIC HOLIDAY and WEEKLY OFF)
- if (isset($leave->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday']) && isset($leave->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off']) && $leave->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday'] == LeavePolicy_InterveningHolidays::COUNT_INTERVENING_HOLIDAY && $leave->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off'] == LeavePolicy_InterveningHolidays::COUNT_INTERVENING_HOLIDAY)
- {
- // Added th code to get all working days in applied leave including holidays and weekoff [SG 09 Feb 2016]
- $final_days = RandomHelper::getInterveinigCalculatedDays($original_applied_days, $non_working_days_new_check, $holidays_year_new_check, true, true);
- if($isHalfDay) {
- $non_working_days = $non_working_days_new_check;
- $holidays_year = $holidays_year_new_check;
- } else {
- $non_working_days = null;
- $holidays_year = null;
- }
- $not_included = null;
- $working_days = $all_days;
- $total_days = count($final_days);
- }
- elseif (isset($leave->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday']) && !isset($leave->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off']) && $leave->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday'] == LeavePolicy_InterveningHolidays::COUNT_INTERVENING_HOLIDAY)
- {
- $non_working_days = UserWeeklyOff::getNonWorkingDays($this->user($tenant_id)->id, $this->from_date->sec, $this->to_date->sec, $this->tenant_id);
- $holidays_year = null;
- $not_included = $non_working_days;
- $working_days = array_diff($all_days, $non_working_days);
- $total_days = count(RandomHelper::getInterveinigCalculatedDays($original_applied_days,$non_working_days, $holidays_year_new_check, true, false));
- }
- elseif (!isset($leave->policy_count_intervening_holidays->count_intervening_holidays['count_public_holiday']) && isset($leave->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off']) && $leave->policy_count_intervening_holidays->count_intervening_holidays['count_weekly_off'] == LeavePolicy_InterveningHolidays::COUNT_INTERVENING_HOLIDAY)
- {
- /* COUNT WEEKLY OFF CHECK */
- $non_working_days = UserWeeklyOff::getNonWorkingDays($this->user($tenant_id)->id, $this->from_date->sec, $this->to_date->sec, $this->tenant_id);
- $holidays_year = RandomHelper::getHolidaysWithYearForHoliday($this->from_date->sec, $this->to_date->sec, $tenant_id, $this->user($tenant_id)->personal->office_location, $this->user($tenant_id)->id, $this->userOptionalHoliday);
- $not_included = $holidays_year;
- $working_days = array_diff($all_days, $holidays_year);
- $total_days = count(RandomHelper::getInterveinigCalculatedDays($original_applied_days,$non_working_days, $holidays_year, false, true));
- }
- } else {
- $non_working_days = null;
- $holidays_year = null;
- $not_included = null;
- $working_days = $all_days;
- $total_days = count($working_days);
- }
- } else {
- $non_working_days = $common_days;
- $holidays_year = $common_days;
- $not_included = $common_days;
- $working_days = $all_days;
- $total_days = 0;
- }
- } else {
- $non_working_days = UserWeeklyOff::getNonWorkingDays($this->user($tenant_id)->id, $this->from_date->sec, $this->to_date->sec, $this->tenant_id);
- //Changes to count holidays for back date also [SG 12 Feb 2016]
- $holidays_year = RandomHelper::getHolidaysWithYearForHoliday($this->from_date->sec, $this->to_date->sec, $tenant_id, $this->user($tenant_id)->personal->office_location, $this->user($tenant_id)->id, $this->userOptionalHoliday);
- $not_included = array_merge($non_working_days, $holidays_year);
- $working_days = array_diff($all_days, $non_working_days);
- $holidays = RandomHelper::getHolidays($this->from_date->sec, $this->to_date->sec, $tenant_id);
- $total_days = count(RandomHelper::excludeHolidaysNew($working_days, $holidays_year));
- }
- return array($non_working_days, $holidays_year, $not_included, $total_days);
- } else {
- $all_days = count(RandomHelper::getDaysWithDate($this->from_date->sec, $this->to_date->sec, true));
- return array(null, null, null,$all_days);
- }
- }
- /**
- * function to check if the user is in leave, if yes then return the user leaves object else empty array
- * @param string $user_id mongo id
- * @param timestamp $date
- * @return object
- */
- public static function inLeave($user_id, $date, $tenant_id = null) {
- $return = array();
- $date = strtotime(date('Y-m-d', $date)); // done this as cron sends 12+ in case of next day shift
- $user_leaves = UserLeaves::model()->tenant($tenant_id)->user_leaves($user_id)->findAll();
- if (!empty($user_leaves)) {
- foreach ($user_leaves as $leave) {
- $from_date = $leave->from_date->sec;
- $to_date = $leave->to_date->sec;
- if ($leave->status == UserLeaves::STATUS_APPROVED || $leave->status == UserLeaves::STATUS_PENDING) {
- //check date in leave range
- //CA_Debug::output_array(array('shift_date' => $date, 'from_date' => $from_date, 'to_date' => $to_date));
- //CA_Debug::output_array(array('shift_date' => date('d-m-Y H:i:s', $date), 'from_date' => date('d-m-Y H:i:s', $from_date), 'to_date' => date('d-m-Y H:i:s', $to_date)));
- $is_inleave = AttendanceHelper::checkInRange($from_date, $to_date, $date);
- if ($is_inleave == true) {
- //in leave
- $return = $leave;
- return $return;
- }
- }
- }
- }
- return $return;
- }
- /**
- * function to apply leave
- * @param string $user_id mongo id
- * @param number $tenant_id
- * @param string $leave_id
- * @param timestamp $from
- * @param timestamp $to
- * @param string $leave_message
- * @param integer $is_unpaid flage value
- * @param integer $is_halfday
- * @return array an array of leave_id and message_id on scccess else empty array
- */
- public static function applyLeave($user_id, $tenant_id, $leave_id, $from, $to, $leave_message, $is_unpaid, $is_halfday) {
- //get user
- $user = UserMongo::model()->tenant($tenant_id)->findByPk($user_id);
- // get recipients
- $leave_recipients = $user->getLeaveRecipients();
- //create message
- $message = new Messages(Messages::SCENARIO_INSERT, true);
- $message->tenant_id = $tenant_id;
- $message->thread_id = Messages::THREAD_ROOT;
- $message->message_type = Messages::MSG_TYPE_UPDATE;
- $message->filter_type = Notifications::FILTER_LEAVE;
- $message->sender_type = Messages::SENDER_TYPE_USER;
- $message->sender_id = $user->user_id;
- $message->privacy = Messages::MESSAGE_PRIVATE;
- $message->isDirectMessage = TRUE;
- $message->receiver_id = $leave_recipients;
- $message->body_plain = $leave_message;
- $message->group = Messages::NO_GROUP;
- $message->tags = array();
- //create leave
- $apply_leave = new UserLeaves('apply');
- $apply_leave->tenant_id = $tenant_id;
- $apply_leave->leave_id = $leave_id;
- $apply_leave->from_date = new MongoDate($from);
- $apply_leave->to_date = new MongoDate($to);
- $apply_leave->message = $leave_message;
- $apply_leave->isUnpaid = $is_unpaid;
- $apply_leave->is_half_day = $is_halfday;
- $apply_leave->user_id = $user_id;
- if ($apply_leave->validate() && $apply_leave->save()) {
- //leaves attachments
- $leaveItems = array(
- 'id' => $apply_leave->id,
- 'type' => $apply_leave->leave_id,
- 'from_date' => date('Y-m-d', $apply_leave->from_date->sec),
- 'to_date' => date('Y-m-d', $apply_leave->to_date->sec),
- 'total_days' => RandomHelper::getWorkingDays($apply_leave->from_date->sec, $apply_leave->to_date->sec, $user->id, $user->tenant_id),
- 'status' => $apply_leave->status,
- 'updated_by' => array(),
- );
- $attachment = array(
- 'type' => Notifications::FILTER_LEAVE,
- 'items' => $leaveItems,
- );
- $message->attachments = $attachment;
- if ($message->validate() && $message->save()) {
- return array('leave_id' => $apply_leave->id, 'message_id' => $message->id);
- }
- }
- //CA_Debug::output_array($apply_leave->getErrors());
- return array();
- }
- /**
- * function to update user leaves
- * @param string $leave_id mongo id
- * @param string $message_id
- * @param number $tenant_id
- * @param integer $status a constant
- * @param string $manager_message
- * @return boolean
- */
- public static function autoUpdateLeaves($leave_id, $message_id, $tenant_id = null, $status = UserLeaves::STATUS_APPROVED, $manager_message = null) {
- $userLeave = UserLeaves::model()->tenant($tenant_id)->findByPk($leave_id);
- $messages = Messages::model()->tenant($tenant_id)->findByPk($message_id);
- $user_id = $userLeave->user_id;
- $user = UserMongo::model()->tenant($tenant_id)->findByPk($user_id);
- $manager_id = ($user->manager_id == null) ? $user->id : $user->manager_id;
- $user_manager_name = $user->getFullname();
- $user_manager_id = $user->user_id;
- if ($user->manager_id != null) {
- $user_manager = UserMongo::model()->tenant($tenant_id)->findByPk($manager_id);
- if (!empty($user_manager)) {
- $user_manager_name = $user_manager->getFullname();
- $user_manager_id = $user_manager->user_id;
- }
- }
- $userLeave->status = $status;
- $userLeave->manager_message = $manager_message;
- $messages->last_action = Notifications::TYPE_REQUEST;
- if ($userLeave->save()) {
- $messages->attachments['items']['status'] = $userLeave->status;
- $messages->attachments['items']['updated_by'][$user_manager_id] = $user_manager_name;
- if ($userLeave->status != UserLeaves::STATUS_REVOKED) {
- $messages->attachments['items']['updated_by']['manager_message'] = $manager_message;
- }
- if ($messages->save()) {
- return true;
- }
- }
- return false;
- }
- /**
- * update attendance log when approving or revoking leave
- * @param type $date (timestamp)
- * @param type $leav_action(approve,revoke)
- */
- public function updateAttendanceLog($date, $leav_action, $new_leave_id = null, $optional_holiday_used = false) {
- $criteria = new EMongoCriteria();
- $criteria->shift_date('>=', new MongoDate($date));
- $criteria->shift_date('<', new MongoDate(($date + (24 * 3600))));
- $userAttendanceLog = UserAttendanceLog::model()->tenant($this->tenant_id)->user($this->user_id)->find($criteria);
- $userShift = UserShiftPlanner::getUserShift($this->user_id, $date, $this->tenant_id);
- if (!empty($userShift))
- {
- $policy_id = $userShift['policy_id'];
- $attendancePolicy = AttendancePolicy::model()->tenant($this->tenant_id)->findByPk($policy_id);
- }
- if (!empty($userAttendanceLog)) {
- if ($leav_action == self::LEAVE_ACTION_APPROVE) {
- $userAttendanceLog->in_leave->status = (int) InLeave::INLEAVE_YES;
- $userAttendanceLog->in_leave->leave_id = ($new_leave_id != null)? $new_leave_id : $this->id;
- $userAttendanceLog->is_holiday->optional_holiday_used = ($optional_holiday_used == true)? IsHoliday::IS_OPTIONAL_YES : IsHoliday::IS_OPTIONAL_NO;
- } elseif ($leav_action == self::LEAVE_ACTION_REVOKE) {
- $userAttendanceLog->in_leave->status = InLeave::INLEAVE_NO;
- $userAttendanceLog->in_leave->leave_id = null;
- if($attendancePolicy->max_optional_holiday_policy->optional_holiday_status == MaxOptionalHolidayPolicy::ALL_OPTIONAL_HOLIDAY_STATUS)
- {
- $userAttendanceLog->is_holiday->optional_holiday_used = IsHoliday::HOLIDAY_YES;
- }
- }
- $userAttendanceLog->save(false);
- }
- }
- /*annual leaves export */
- public static function getCountOfMonthsExport($user_id, $leave_id, $for_month , $is_paid = 1)
- {
- $from = strtotime($for_month."-01");
- $to = strtotime($for_month."-".date('t', $from));
- $criteria = new EMongoCriteria();
- $criteria->from_date('>=', new MongoDate($from));
- $criteria->from_date('<=', new MongoDate($to));
- if($is_paid == 1)
- {
- $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->paid()->findAll($criteria);
- }
- else{
- $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->unpaid()->findAll($criteria);
- }
- $total_working_days_one = array();
- foreach ($leaves_approved as $leaves)
- {
- $applied_leave_data = Leaves::model()->tenant($leaves->tenant_id)->findByPk($leaves->leave_id);
- $holidays = RandomHelper::getHolidays($leaves->from_date->sec, $leaves->to_date->sec, $leaves->tenant_id);
- if(isset($applied_leave_data->policy_count_intervening_holidays->status) && $applied_leave_data->policy_count_intervening_holidays->status == LeavePolicy_InterveningHolidays::STATUS_ACTIVE)
- {
- if(isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off == 1)
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- elseif (isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off == 0)
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDays($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- else
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- }
- else
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$total_days = count(RandomHelper::excludeHolidays($working_days, $holidays));
- }
- //$total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- }
- $criteria = new EMongoCriteria();
- $criteria->to_date('>=', new MongoDate($from));
- $criteria->to_date('<=', new MongoDate($to));
- // $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->paid()->findAll($criteria);
- if($is_paid == 1)
- {
- $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->paid()->findAll($criteria);
- }
- else{
- $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->unpaid()->findAll($criteria);
- }
- $total_working_days_two = array();
- foreach ($leaves_approved as $leaves)
- {
- $applied_leave_data = Leaves::model()->tenant($leaves->tenant_id)->findByPk($leaves->leave_id);
- $holidays = RandomHelper::getHolidays($leaves->from_date->sec, $leaves->to_date->sec, $leaves->tenant_id);
- $applied_leave_data = Leaves::model()->tenant($leaves->tenant_id)->findByPk($leaves->leave_id);
- $holidays = RandomHelper::getHolidays($leaves->from_date->sec, $leaves->to_date->sec, $leaves->tenant_id);
- if(isset($applied_leave_data->policy_count_intervening_holidays->status) && $applied_leave_data->policy_count_intervening_holidays->status == LeavePolicy_InterveningHolidays::STATUS_ACTIVE)
- {
- if(isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off == 1)
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- elseif (isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off == 0)
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDays($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- else
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- }
- else
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$total_days = count(RandomHelper::excludeHolidays($working_days, $holidays));
- }
- // $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- }
- if(empty($total_working_days_two))
- {
- $total_working_days_two = array_merge($total_working_days_two, $total_working_days_one);
- }
- //filter for current month days
- $days_list = self::getDateList($from, $to);
- $total_working_days_two_new = array();
- foreach ($total_working_days_two as $total_working_days) {
- if(in_array($total_working_days, $days_list)){
- $total_working_days_two_new[] = $total_working_days;
- }
- }
- return count($total_working_days_two_new);
- }
- // annual leaves export
- /**Passed additionla parameter to check correct count in case of intervening if leaves applied on holiday/weekoff only [SG 11 Feb 2016]**/
- public function getCountOfMonthsUnpaid($user_id, $for_month = null, $from = null, $to = null, $getCount = false)
- {
- if(isset($for_month) && $for_month != null)
- {
- $from = strtotime($for_month."-01");
- $to = strtotime($for_month."-".date('t', $from));
- }
- else
- {
- $from = strtotime($from);
- $to = strtotime($to);
- }
- $criteria = new EMongoCriteria();
- $criteria->from_date('>=', new MongoDate($from));
- $criteria->from_date('<=', new MongoDate($to));
- $user_mongo_details = UserMongo::model()->tenant()->findByPk($user_id);
- $leaves_approved = UserLeaves::model()->tenant($user_mongo_details->tenant_id)->user_approved_leave($user_id)->unpaid()->findAll($criteria);
- $total_working_days_one = array();
- foreach ($leaves_approved as $leaves)
- {
- //$leave_id_number = (isset($leaves->parent_leave_id) && $leaves->parent_leave_id != "") ? $leaves->parent_leave_id : "unpaid";
- $leave_id_number = "unpaid";
- $get_working_days_array = RandomHelper::getWorkingDaysAccToLeavesPolicy($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id, $leave_id_number,$user_mongo_details->personal->office_location, $getCount, $leaves->userOptionalHoliday);
- // Description : Since the above get_working_days_array return array the below code was not written in wich we need all the leaves applied date.[KP: 16 March 2016]
- if (!is_array($get_working_days_array))
- {
- $get_working_days_new_array[] = $get_working_days_array;
- }
- else
- {
- $get_working_days_new_array = $get_working_days_array;
- }
- $total_working_days_one = array_merge($total_working_days_one, $get_working_days_new_array);
- }
- $criteria = new EMongoCriteria();
- $criteria->to_date('>=', new MongoDate($from));
- $criteria->to_date('<=', new MongoDate($to));
- // $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->paid()->findAll($criteria);
- $leaves_approved = UserLeaves::model()->tenant()->user_approved_leave($user_id)->unpaid()->findAll($criteria);
- $total_working_days_two = array();
- foreach ($leaves_approved as $leaves)
- {
- //$leave_id_number = (isset($leaves->parent_leave_id) && $leaves->parent_leave_id != "") ? $leaves->parent_leave_id : "unpaid";
- $leave_id_number = "unpaid";
- $get_working_days_new_array = array();
- // Description : The Below Condition has been checked because for Infogix(lalit) from_date was saving blank array.[KP: 17 March 2016]
- $from_date_leaves = isset($leaves->from_date->sec) ? $leaves->from_date->sec : $leaves->to_date->sec;
- $get_working_days_array = RandomHelper::getWorkingDaysAccToLeavesPolicy($from_date_leaves, $leaves->to_date->sec, $user_id,$leaves->tenant_id,$leave_id_number,$user_mongo_details->personal->office_location, $getCount, $leaves->userOptionalHoliday);
- if (!is_array($get_working_days_array))
- {
- $get_working_days_new_array[] = $get_working_days_array;
- }
- else
- {
- $get_working_days_new_array = $get_working_days_array;
- }
- $total_working_days_two = array_merge($total_working_days_two, $get_working_days_new_array);
- }
- if(empty($total_working_days_two))
- {
- $total_working_days_two = array_merge($total_working_days_two, $total_working_days_one);
- }
- else
- {
- $total_working_days_new_one = array();
- if (!is_array($total_working_days_one))
- {
- $total_working_days_new_one[] = $total_working_days_one;
- }
- else
- {
- $total_working_days_new_one = $total_working_days_one;
- }
- foreach ($total_working_days_new_one as $key_one_new => $value_one_new)
- {
- if(date("Y-m",strtotime($value_one_new)) == $for_month)
- {
- $total_working_days_two[] = $value_one_new;
- }
- }
- $total_working_days_two = array_unique($total_working_days_two, SORT_REGULAR);
- }
- //filter for current month days
- $days_list = self::getDateList($from, $to);
- $total_working_days_two_new = array();
- foreach ($total_working_days_two as $total_working_days)
- {
- if(in_array($total_working_days, $days_list))
- {
- $total_working_days_two_new[] = $total_working_days;
- }
- }
- return count($total_working_days_two_new);
- }
- /*
- * Description : Below function will produce list of compoff available for user. [KP : 9 Sept 2015]
- */
- public function checkCompoffAvailable($user_id)
- {
- $user_data = UserMongo::model()->tenant()->findByAttributes(array('id' => $user_id));
- $compoff_available = UserAttendanceLog::getCompoffLeavesavailableattlist($user_data->user_id, 'compofflist');
- /* get leaves applied for compoff */
- $usermongo_data = UserMongo::model()->tenant()->findByAttributes(array('user_id' => $user_data->user_id));
- $leaves_compoff_obj = Leaves::model()->tenant()->findByAttributes(array('name' => 'Compensatory Off'));
- $criteria = new EMongoCriteria();
- $criteria->user_id('==', $usermongo_data->id);
- $criteria->leave_id('==', $leaves_compoff_obj->id);
- $criteria->status('==', UserLeaves::STATUS_APPROVED);
- $user_leaves_for_previous_compoff = UserLeaves::model()->findAll($criteria);
- /* get leaves applied for compoff */
- /* getting the compoff setting for the tenant */
- $tenant_compoff = TenantLeavesCompoff::model()->tenant()->find();
- $lapse_period = $tenant_compoff->Compoff_earned_will_lapse_after_how_many_days;
- /* Below compoof_used array will give list of compoff date used by user. */
- $compoof_used = array();
- $com_cont =0;
- foreach ($user_leaves_for_previous_compoff as $key_compoff_leaves => $value_compoff_leaves)
- {
- if(isset($value_compoff_leaves->compoff_used_date) && !empty($value_compoff_leaves->compoff_used_date) && count($value_compoff_leaves->compoff_used_date) > 0 && is_array($value_compoff_leaves->compoff_used_date))
- {
- $comp_date = array();
- foreach ($value_compoff_leaves->compoff_used_date as $key => $nvalue)
- {
- $comp_date = $nvalue;
- }
- $compoof_used[$com_cont] = $comp_date;
- $com_cont++;
- }
- }
- $compoof_available_list = array();
- $comcnt_avl=0;
- foreach ($compoff_available as $compoff_key => $compoff_value)
- {
- $lapse = (isset($lapse_period) && $lapse_period == 0) ? 'No lapse period' : date('d F Y', strtotime('+'.$lapse_period.'day', strtotime($compoff_value['date']))); // getting lapse period available for compoff.
- /* Checking weather the compoff has been lapsed or not.*/
- $status = "Active";
- if($lapse != 'No lapse period')
- {
- if(strtotime(date('Y-m-d')) > strtotime($lapse))
- {
- $status = "Lapsed";
- }
- }
- if ($status == "Active")
- {
- if(!in_array($compoff_value['date'], $compoof_used)) // Here we are checking for compoff available date is not in compoff used array.
- {
- $compoof_available_list[$comcnt_avl]= $compoff_value;
- }
- $comcnt_avl++;
- }
- }
- $compoof_available_list = array_values($compoof_available_list); /*rearranging the key for compoff available list.*/
- /* Sort the compoff available date in desending order. */
- usort($compoof_available_list, function($a, $b)
- {
- $format = 'Y-m-d';
- $descending = true;
- $zone = new DateTimeZone('UTC');
- $d1 = DateTime::createFromFormat($format, $a['date'], $zone)->getTimestamp();
- $d2 = DateTime::createFromFormat($format, $b['date'], $zone)->getTimestamp();
- return $descending ? ($d1 - $d2) : ($d2 - $d1);
- });
- return $compoof_available_list;
- }
- public function getCountOfMonthsUnScheduled($user_id=null, $leave_id= null, $for_month)
- {
- $from = strtotime($for_month."-01");
- $to = strtotime($for_month."-".date('t', $from));
- $criteria = new EMongoCriteria();
- $criteria->from_date('>=', new MongoDate($from));
- $criteria->from_date('<=', new MongoDate($to));
- $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->findAll($criteria);
- $total_working_days_one = array();
- foreach ($leaves_approved as $leaves)
- {
- if($leaves->created->sec >= $leaves->from_date->sec)
- {
- $applied_leave_data = Leaves::model()->tenant($leaves->tenant_id)->findByPk($leaves->leave_id);
- $holidays = RandomHelper::getHolidays($leaves->from_date->sec, $leaves->to_date->sec);
- if(isset($applied_leave_data->policy_count_intervening_holidays->status) && $applied_leave_data->policy_count_intervening_holidays->status == LeavePolicy_InterveningHolidays::STATUS_ACTIVE)
- {
- if(isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off == 1)
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- elseif (isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off == 0)
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDays($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- else
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- }
- else
- {
- $total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- $holidays = RandomHelper::getHolidays($leaves->from_date->sec, $leaves->to_date->sec);
- $total_working_days_one = RandomHelper::excludeHolidays($total_working_days_one, $holidays);
- //$total_days = count(RandomHelper::excludeHolidays($working_days, $holidays));
- }
- //$total_working_days_one = array_merge($total_working_days_one, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- }
- }
- $criteria = new EMongoCriteria();
- $criteria->to_date('>=', new MongoDate($from));
- $criteria->to_date('<=', new MongoDate($to));
- // $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->paid()->findAll($criteria);
- $leaves_approved = UserLeaves::model()->user_approved_leave($user_id, $leave_id)->findAll($criteria);
- $total_working_days_two = array();
- foreach ($leaves_approved as $leaves)
- {
- if($leaves->created->sec >= $leaves->from_date->sec)
- {
- $applied_leave_data = Leaves::model()->tenant($leaves->tenant_id)->findByPk($leaves->leave_id);
- $holidays = RandomHelper::getHolidays($leaves->from_date->sec, $leaves->to_date->sec);
- $applied_leave_data = Leaves::model()->tenant($leaves->tenant_id)->findByPk($leaves->leave_id);
- $holidays = RandomHelper::getHolidays($leaves->from_date->sec, $leaves->to_date->sec);
- if(isset($applied_leave_data->policy_count_intervening_holidays->status) && $applied_leave_data->policy_count_intervening_holidays->status == LeavePolicy_InterveningHolidays::STATUS_ACTIVE)
- {
- if(isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off == 1)
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- elseif (isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_public_holiday == 1 && isset($applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off) && $applied_leave_data->policy_count_intervening_holidays->count_intervening_holidays->count_weekly_off == 0)
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDays($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- else
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDaysIncludingWeeklyoff($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- //$working_days = RandomHelper::getWorkingDaysIncludingWeeklyoff($le->from_date->sec, $le->to_date->sec, $user->mongo()->id, $user->tenant_id);
- //$total_days = count($working_days);
- }
- }
- else
- {
- $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- $holidays = RandomHelper::getHolidays($leaves->from_date->sec, $leaves->to_date->sec);
- $total_working_days_two = RandomHelper::excludeHolidays($total_working_days_two, $holidays);
- //$total_days = count(RandomHelper::excludeHolidays($working_days, $holidays));
- }
- // $total_working_days_two = array_merge($total_working_days_two, RandomHelper::getWorkingDays($leaves->from_date->sec, $leaves->to_date->sec, $user_id, $leaves->tenant_id));
- }
- }
- if(empty($total_working_days_two))
- {
- $total_working_days_two = array_merge($total_working_days_two, $total_working_days_one);
- }
- else
- {
- foreach ($total_working_days_one as $key_one_new => $value_one_new)
- {
- if(date("Y-m",strtotime($value_one_new)) == $for_month)
- {
- $total_working_days_two[] = $value_one_new;
- }
- }
- $total_working_days_two = array_unique($total_working_days_two, SORT_REGULAR);
- }
- //filter for current month days
- $days_list = self::getDateList($from, $to);
- $total_working_days_two_new = array();
- foreach ($total_working_days_two as $total_working_days)
- {
- if(in_array($total_working_days, $days_list))
- {
- $total_working_days_two_new[] = $total_working_days;
- }
- }
- return count($total_working_days_two_new);
- }
- public function getCompleateLeavesDetails()
- {
- $tenant_id = Yii::app()->user->getTenantId();
- $id = Yii::app()->user->id;
- $user = User::model()->tenant()->findByPk($id);
- $leave_settings = TenantLeavesSettings::model()->findByAttributes(array('tenant_id' => $tenant_id));
- if(isset($leave_settings->unpaid_refresh) && $leave_settings->unpaid_refresh == TenantLeavesSettings::FINANCIAL_UNPAID_REFRESH)
- {
- $tenantProfile = TenantProfile::model()->tenant($tenant_id)->find();
- $year_to_pass = (date("m") < 4)? date('Y') - 1 : date('Y');
- $month_list = $tenantProfile->getMonthList('Y-m','M', $year_to_pass);
- }
- else{
- $tenantProfile = TenantProfile::model()->tenant($tenant_id)->find();
- $month_list = $tenantProfile->getMonthListCalendar('Y-m','M');
- }
- $leaveList = Leaves::getLeaveList();
- $leaveListIds = array();
- foreach ($leaveList as $id => $name)
- {
- $leaveListIds[] = Leaves::model()->tenant()->findByPk($id);
- }
- $list = $month_list;
- $leaves_total = array('c1' => 0, 'c2' => 0, 'c3' => 0, 'c4' => 0, 'c5' => 0, 'c6' => 0, 'c7' => 0, 'c8' => 0, 'c9' => 0, 'c10' => 0, 'c11' => 0, 'c12' => 0, );
- $leaves = $leaveListIds;
- if (!empty($leaves))
- {
- foreach ($leaves as $l)
- {
- if (!empty($l))
- {
- $c = 1;
- foreach ($list as $for_month => $month)
- {
- $leaves_approved = UserLeaves::model()->user_approved_leave($user->mongo()->id, $l->id)->forMonth($for_month)->paid()->findAll();
- $total_days = 0;
- foreach ($leaves_approved as $le)
- {
- $wekly_off_days = RandomHelper::getWorkingDays($le->from_date->sec, $le->to_date->sec, $le->user_id, $le->tenant_id);
- if (!empty($wekly_off_days))
- {
- if ($le->is_half_day == UserLeaves::HALF_DAY_YES)
- {
- $total_days += 0.5;
- }
- }
- else
- {
- if ($total_days > 0)
- {
- $total_days -= 0.5;
- }
- }
- }
- $counter = 0;
- $total_data_of_leaves = UserLeaves::getCountOfMonths($user->mongo()->id, $l->id, $for_month, 1);
- if ($total_data_of_leaves > 0)
- {
- $counter = UserLeaves::getCountOfMonths($user->mongo()->id, $l->id, $for_month, 1) - $total_days;
- }
- else
- {
- $counter = $total_data_of_leaves;
- }
- $leaves_total['c' . $c++] += $counter;
- }
- }
- }
- }
- $unpaid_array = array();
- foreach ($list as $for_month => $month)
- {
- $leaves_approved = UserLeaves::model()->user_approved_leave($user->mongo()->id)->forMonth($for_month)->unpaid()->findAll();
- $total_days = 0;
- foreach ($leaves_approved as $le)
- {
- $wekly_off_days = RandomHelper::getWorkingDays($le->from_date->sec, $le->to_date->sec, $le->user_id, $le->tenant_id);
- if (!empty($wekly_off_days))
- {
- if ($le->is_half_day == UserLeaves::HALF_DAY_YES)
- {
- $total_days += 0.5;
- }
- }
- else
- {
- if ($total_days > 0)
- {
- $total_days -= 0.5;
- }
- }
- }
- $counter = 0;
- $counter_new = 0;
- $counter = UserLeaves::getCountOfMonthsUnpaid($user->mongo()->id, $for_month);
- if ($counter > 0)
- {
- $counter = $counter - $total_days;
- }
- else
- {
- $counter = $counter;
- }
- $unpaid_array[] += $counter;
- }
- $leaves_per_month = array();
- $i = 0;
- foreach ($leaves_total as $total) :
- $leaves_per_month[]= ($total + $unpaid_array[$i]);
- $i++;
- endforeach;
- // UNSCHEDULED leave calculations
- $unschedule_array = array();
- foreach ($list as $for_month => $month)
- {
- $leaves_approved = UserLeaves::model()->user_approved_leave($user->mongo()->id)->forMonth($for_month)->findAll();
- $total_days_unsheduled = 0;
- foreach ($leaves_approved as $le)
- {
- if($le->created->sec >= $le->from_date->sec)
- {
- $wekly_off_days = RandomHelper::getWorkingDays($le->from_date->sec, $le->to_date->sec, $le->user_id, $le->tenant_id);
- // echo "<pre>"; print_r($wekly_off_days); "</pre>";
- if (!empty($wekly_off_days))
- {
- if ($le->is_half_day == UserLeaves::HALF_DAY_YES)
- {
- $total_days_unsheduled += 0.5;
- }
- }
- else
- {
- if ($total_days_unsheduled > 0)
- {
- $total_days_unsheduled -= 0.5;
- }
- }
- }
- }
- $counter_unscheduled = 0;
- $total_data_of_leaves = UserLeaves::getCountOfMonthsUnScheduled($user->mongo()->id, null, $for_month, 0);
- if ($total_data_of_leaves > 0)
- {
- $counter_unscheduled = $total_data_of_leaves - $total_days_unsheduled;
- }
- else
- {
- $counter_unscheduled = $total_data_of_leaves;
- }
- $unschedule_array[] = $counter_unscheduled;
- }
- // UNSCHEDULED leave calculations
- $total_leaves_taken = array_sum($leaves_per_month);
- $total_unpaid_leaves = array_sum($unpaid_array);
- $total_unscheduled_leaves = array_sum($unschedule_array);
- $params = array(
- 'total_leaves_taken'=> $total_leaves_taken,
- 'total_unpaid_leaves' => $total_unpaid_leaves,
- 'total_unscheduled_leaves' => $total_unscheduled_leaves,
- );
- return $params;
- }
- public function userAttendanceDetailsAdmin($user_indivual, $from, $to, $tenant_id,$month_for)
- {
- $index = 0;
- $output = array();
- $begin = new DateTime($from);
- $end = new DateTime($to);
- $end = $end->modify('+1 day');
- $interval = new DateInterval('P1D');
- $daterange = new DatePeriod($begin, $interval, $end);
- $from = strtotime($from);
- if($month_for == "curr_month")
- {
- $to = strtotime("+1 day", strtotime($to));
- }
- else
- {
- $to = strtotime($to);
- }
- $criteria = new EMongoCriteria();
- $criteria->shift_date('>=', new MongoDate($from));
- $criteria->shift_date('<=', new MongoDate($to));
- foreach ($user_indivual as $key => $user)
- {
- $user_name = $user['personal']['firstname'] . " " . $user['personal']['lastname'];
- $criteria->user_id = $user->id;
- $userAttendanceLogs = UserAttendanceLog::model()->tenant($tenant_id)->findAll($criteria);
- sort($userAttendanceLogs);
- $present = 0;
- $absent = 0;
- $weeklyoff_count = 0;
- $holiday_count = 0;
- $leave_count = 0;
- $unpaidleave_count = 0;
- $unscheduled_leave_count = 0;
- $output[] = array(
- $user_name,
- );
- $jindex = 0;
- foreach ($daterange as $date)
- {
- $unpaidFlag = false;
- if (isset($userAttendanceLogs[$jindex]))
- {
- if ($date->format("d") == date("d", $userAttendanceLogs[$jindex]->shift_date->sec))
- {
- $date = date("d", $userAttendanceLogs[$jindex]->shift_date->sec);
- if (isset($userAttendanceLogs[$jindex]->attendance_status))
- {
- $status = $userAttendanceLogs[$jindex]->attendance_status;
- if ($status == UserAttendance::ATTENDANCE_STATUS_ABSENT)
- {
- $checkBothClockInClockOut = false;
- $checkOnlyClockIn = false;
- $shift_planner_id = $userAttendanceLogs[$jindex]->shift_planner;
- // Get shift planner
- $useShiftPlanner = UserShiftPlanner::model()->user($user->id)->findByPk($shift_planner_id);
- if (!empty($useShiftPlanner))
- {
- $policy_id = $useShiftPlanner->policy_id;
- }
- else
- {
- $policy_id = "";
- }
- // Get user policy using policy Id.
- $attendancePolicy = AttendancePolicy::getAttendancePolicy($policy_id, $tenant_id);
- if (!empty($attendancePolicy))
- {
- if ($attendancePolicy->markin_policy->status == MarkinPolicy::MARKIN_BOTH)
- {
- $checkBothClockInClockOut = true;
- $checkOnlyClockIn = false;
- }
- else if ($attendancePolicy->markin_policy->status == MarkinPolicy::MARKIN_IN)
- {
- $checkBothClockInClockOut = false;
- $checkOnlyClockIn = true;
- }
- }
- if ($checkBothClockInClockOut)
- {
- if(($userAttendanceLogs[$jindex]->punchin_time != '') && ($userAttendanceLogs[$jindex]->punchout_time != ''))
- {
- $status = UserAttendanceLog::ATTENDANCE_STATUS_PRESENT;
- }
- else
- {
- $status = UserAttendanceLog::ATTENDANCE_STATUS_ABSENT;
- }
- }
- else if ($checkOnlyClockIn)
- {
- if(($userAttendanceLogs[$jindex]->punchin_time != ''))
- {
- $status = UserAttendanceLog::ATTENDANCE_STATUS_PRESENT;
- }
- else
- {
- $status = UserAttendanceLog::ATTENDANCE_STATUS_ABSENT;
- }
- }
- else
- {
- $status = UserAttendanceLog::ATTENDANCE_STATUS_ABSENT;
- }
- }
- elseif ($status == UserAttendance::ATTENDANCE_STATUS_PRESENT)
- {
- $status = UserAttendanceLog::ATTENDANCE_STATUS_PRESENT;
- }
- // ***************************** <END> *****************************
- $weeklyoff = $userAttendanceLogs[$jindex]->is_weekly_off->status;
- $holiday = $userAttendanceLogs[$jindex]->is_holiday->status;
- $leave = $userAttendanceLogs[$jindex]->in_leave->status;
- $leave_revoked = false;
- if (isset($userAttendanceLogs[$jindex]->in_leave->leave_id))
- {
- $unpaidLeave = UserLeaves::model()->tenant($tenant_id)->findByPk($userAttendanceLogs[$jindex]->in_leave->leave_id);
- if ($unpaidLeave->isUnpaid == UserLeaves::LEAVE_UNPAID)
- {
- $unpaidleave_count++;
- $unpaidFlag = true;
- }
- else
- {
- $unpaidFlag = false;
- }
- if ($unpaidLeave->status == UserLeaves::STATUS_REVOKED || $unpaidLeave->status == UserLeaves::STATUS_REJECTED)
- {
- $leave_revoked = true;
- }
- }
- else
- {
- $unpaidFlag = false;
- }
- if (isset($userAttendanceLogs[$jindex]->in_leave))
- {
- $unscheduleLeave = UserLeaves::model()->tenant($tenant_id)->findByPk($userAttendanceLogs[$jindex]->in_leave->leave_id);
- if (isset($unscheduleLeave->status) && $unscheduleLeave->status == UserLeaves::STATUS_APPROVED)
- {
- if($unscheduleLeave->created->sec >= $unscheduleLeave->from_date->sec)
- {
- if(isset($unscheduleLeave->is_half_day) && $unscheduleLeave->is_half_day)
- {
- $unscheduled_leave_count += 0.5;
- }
- else
- {
- $unscheduled_leave_count++;
- }
- }
- }
- }
- if (isset($weeklyoff) && $weeklyoff == "1")
- {
- if ($status == UserAttendanceLog::ATTENDANCE_STATUS_PRESENT || $status == UserAttendanceLog::ATTENDANCE_STATUS_PENDING)
- {
- $show_status = "P";
- }
- else
- {
- $show_status = "W";
- }
- $weeklyoff_count++;
- }
- elseif (isset($holiday) && $holiday == "1")
- {
- $show_status = "H";
- $holiday_count++;
- }
- elseif ((isset($leave)) && ($leave == "1") && ($leave_revoked == false))
- {
- $show_status = "L";
- $leave_count++;
- }
- elseif ($status == UserAttendanceLog::ATTENDANCE_STATUS_PRESENT || $status == UserAttendanceLog::ATTENDANCE_STATUS_PENDING)
- {
- $show_status = "P";
- $present++;
- }
- elseif ($status == UserAttendanceLog::ATTENDANCE_STATUS_ABSENT || $status == UserAttendance::ATTENDANCE_STATUS_ABSENT)
- {
- $show_status = "A";
- $absent++;
- }
- else
- {
- $show_status = "";
- }
- }
- else
- {
- $show_status = "";
- }
- $jindex++;
- }
- else
- {
- $show_status = "-";
- }
- }
- else
- {
- $show_status = "-";
- }
- if ($unpaidFlag)
- {
- $show_status = "U";
- }
- // array_push($output[$index], $show_status);
- }
- array_push($output[$index], $present);
- array_push($output[$index], $absent);
- array_push($output[$index], $weeklyoff_count);
- array_push($output[$index], $leave_count);
- array_push($output[$index], $unpaidleave_count);
- array_push($output[$index], $unscheduled_leave_count);
- $index++;
- }
- return $output;
- }
- public function updateAttendanceLogOptionalHolday($date, $leav_action, $optional_holiday_used = false)
- {
- $criteria = new EMongoCriteria();
- $criteria->shift_date('>=', new MongoDate($date));
- $criteria->shift_date('<', new MongoDate(($date + (24 * 3600))));
- $userAttendanceLog = UserAttendanceLog::model()->tenant($this->tenant_id)->user($this->user_id)->find($criteria);
- if (!empty($userAttendanceLog))
- {
- if ($leav_action == UserLeaves::LEAVE_ACTION_APPROVE)
- {
- $userAttendanceLog->in_leave->status = InLeave::INLEAVE_NO;
- $userAttendanceLog->in_leave->leave_id = null;
- $userAttendanceLog->is_holiday->optional_holiday_used = ($optional_holiday_used == true)? IsHoliday::IS_OPTIONAL_YES : IsHoliday::IS_OPTIONAL_NO;
- }
- $userAttendanceLog->save(false);
- }
- }
- }
Add Comment
Please, Sign In to add comment