function createRecurringScheduleTemplate($company_id, $type, $schedule_policy_id = NULL)
 {
     $rstcf = new RecurringScheduleTemplateControlFactory();
     $rstcf->setCompany($company_id);
     switch ($type) {
         case 10:
             //Morning Shift
             $rstcf->setName('Morning Shift');
             $rstcf->setDescription('6:00AM - 3:00PM');
             if ($rstcf->isValid()) {
                 $rstc_id = $rstcf->Save();
                 Debug::Text('bRecurring Schedule Template Control ID: ' . $rstc_id, __FILE__, __LINE__, __METHOD__, 10);
                 //Week 1
                 $rstf = new RecurringScheduleTemplateFactory();
                 $rstf->setRecurringScheduleTemplateControl($rstc_id);
                 $rstf->setWeek(1);
                 $rstf->setSun(FALSE);
                 $rstf->setMon(TRUE);
                 $rstf->setTue(TRUE);
                 $rstf->setWed(TRUE);
                 $rstf->setThu(TRUE);
                 $rstf->setFri(TRUE);
                 $rstf->setSat(FALSE);
                 $rstf->setStartTime(strtotime('06:00 AM'));
                 $rstf->setEndTime(strtotime('03:00 PM'));
                 if ($schedule_policy_id > 0) {
                     $rstf->setSchedulePolicyID($schedule_policy_id);
                 }
                 $rstf->setBranch('-1');
                 //Default
                 $rstf->setDepartment('-1');
                 //Default
                 if ($rstf->isValid()) {
                     Debug::Text('Saving Recurring Schedule Week...', __FILE__, __LINE__, __METHOD__, 10);
                     $rstf->Save();
                 }
                 return $rstc_id;
             }
             break;
         case 20:
             //Afternoon Shift
             $rstcf->setName('Afternoon Shift');
             $rstcf->setDescription('10:00AM - 7:00PM');
             if ($rstcf->isValid()) {
                 $rstc_id = $rstcf->Save();
                 Debug::Text('bRecurring Schedule Template Control ID: ' . $rstc_id, __FILE__, __LINE__, __METHOD__, 10);
                 //Week 1
                 $rstf = new RecurringScheduleTemplateFactory();
                 $rstf->setRecurringScheduleTemplateControl($rstc_id);
                 $rstf->setWeek(1);
                 $rstf->setSun(FALSE);
                 $rstf->setMon(TRUE);
                 $rstf->setTue(TRUE);
                 $rstf->setWed(TRUE);
                 $rstf->setThu(TRUE);
                 $rstf->setFri(TRUE);
                 $rstf->setSat(FALSE);
                 $rstf->setStartTime(strtotime('10:00 AM'));
                 $rstf->setEndTime(strtotime('07:00 PM'));
                 if ($schedule_policy_id > 0) {
                     $rstf->setSchedulePolicyID($schedule_policy_id);
                 }
                 $rstf->setBranch('-1');
                 //Default
                 $rstf->setDepartment('-1');
                 //Default
                 if ($rstf->isValid()) {
                     Debug::Text('Saving Recurring Schedule Week...', __FILE__, __LINE__, __METHOD__, 10);
                     $rstf->Save();
                 }
                 return $rstc_id;
             }
             break;
         case 30:
             //Evening Shift
             $rstcf->setName('Evening Shift');
             $rstcf->setDescription('2:00PM - 11:00PM');
             if ($rstcf->isValid()) {
                 $rstc_id = $rstcf->Save();
                 Debug::Text('bRecurring Schedule Template Control ID: ' . $rstc_id, __FILE__, __LINE__, __METHOD__, 10);
                 //Week 1
                 $rstf = new RecurringScheduleTemplateFactory();
                 $rstf->setRecurringScheduleTemplateControl($rstc_id);
                 $rstf->setWeek(1);
                 $rstf->setSun(FALSE);
                 $rstf->setMon(TRUE);
                 $rstf->setTue(TRUE);
                 $rstf->setWed(TRUE);
                 $rstf->setThu(TRUE);
                 $rstf->setFri(TRUE);
                 $rstf->setSat(FALSE);
                 $rstf->setStartTime(strtotime('02:00 PM'));
                 $rstf->setEndTime(strtotime('11:00 PM'));
                 if ($schedule_policy_id > 0) {
                     $rstf->setSchedulePolicyID($schedule_policy_id);
                 }
                 $rstf->setBranch('-1');
                 //Default
                 $rstf->setDepartment('-1');
                 //Default
                 if ($rstf->isValid()) {
                     Debug::Text('Saving Recurring Schedule Week...', __FILE__, __LINE__, __METHOD__, 10);
                     $rstf->Save();
                 }
                 return $rstc_id;
             }
             break;
         case 40:
             //Split Shift
             $rstcf->setName('Split Shift');
             $rstcf->setDescription('8:00AM-12:00PM, 5:00PM-9:00PM ');
             if ($rstcf->isValid()) {
                 $rstc_id = $rstcf->Save();
                 Debug::Text('bRecurring Schedule Template Control ID: ' . $rstc_id, __FILE__, __LINE__, __METHOD__, 10);
                 //Week 1
                 $rstf = new RecurringScheduleTemplateFactory();
                 $rstf->setRecurringScheduleTemplateControl($rstc_id);
                 $rstf->setWeek(1);
                 $rstf->setSun(FALSE);
                 $rstf->setMon(TRUE);
                 $rstf->setTue(TRUE);
                 $rstf->setWed(TRUE);
                 $rstf->setThu(TRUE);
                 $rstf->setFri(TRUE);
                 $rstf->setSat(FALSE);
                 $rstf->setStartTime(strtotime('08:00 AM'));
                 $rstf->setEndTime(strtotime('12:00 PM'));
                 if ($schedule_policy_id > 0) {
                     $rstf->setSchedulePolicyID($schedule_policy_id);
                 }
                 $rstf->setBranch('-1');
                 //Default
                 $rstf->setDepartment('-1');
                 //Default
                 if ($rstf->isValid()) {
                     Debug::Text('Saving Recurring Schedule Week...', __FILE__, __LINE__, __METHOD__, 10);
                     $rstf->Save();
                 }
                 //Week 1
                 $rstf = new RecurringScheduleTemplateFactory();
                 $rstf->setRecurringScheduleTemplateControl($rstc_id);
                 $rstf->setWeek(1);
                 $rstf->setSun(FALSE);
                 $rstf->setMon(TRUE);
                 $rstf->setTue(TRUE);
                 $rstf->setWed(TRUE);
                 $rstf->setThu(TRUE);
                 $rstf->setFri(TRUE);
                 $rstf->setSat(FALSE);
                 $rstf->setStartTime(strtotime('05:00 PM'));
                 $rstf->setEndTime(strtotime('9:00 PM'));
                 if ($schedule_policy_id > 0) {
                     $rstf->setSchedulePolicyID($schedule_policy_id);
                 }
                 $rstf->setBranch('-1');
                 //Default
                 $rstf->setDepartment('-1');
                 //Default
                 if ($rstf->isValid()) {
                     Debug::Text('Saving Recurring Schedule Week...', __FILE__, __LINE__, __METHOD__, 10);
                     $rstf->Save();
                 }
                 return $rstc_id;
             }
             break;
         case 50:
             //Full Rotation
             $rstcf->setName('Full Rotation');
             $rstcf->setDescription('1wk-Morning, 1wk-Afternoon, 1wk-Evening');
             if ($rstcf->isValid()) {
                 $rstc_id = $rstcf->Save();
                 Debug::Text('bRecurring Schedule Template Control ID: ' . $rstc_id, __FILE__, __LINE__, __METHOD__, 10);
                 //Week 1
                 $rstf = new RecurringScheduleTemplateFactory();
                 $rstf->setRecurringScheduleTemplateControl($rstc_id);
                 $rstf->setWeek(1);
                 $rstf->setSun(FALSE);
                 $rstf->setMon(TRUE);
                 $rstf->setTue(TRUE);
                 $rstf->setWed(TRUE);
                 $rstf->setThu(TRUE);
                 $rstf->setFri(TRUE);
                 $rstf->setSat(FALSE);
                 $rstf->setStartTime(strtotime('06:00 AM'));
                 $rstf->setEndTime(strtotime('03:00 PM'));
                 if ($schedule_policy_id > 0) {
                     $rstf->setSchedulePolicyID($schedule_policy_id);
                 }
                 $rstf->setBranch('-1');
                 //Default
                 $rstf->setDepartment('-1');
                 //Default
                 if ($rstf->isValid()) {
                     Debug::Text('Saving Recurring Schedule Week...', __FILE__, __LINE__, __METHOD__, 10);
                     $rstf->Save();
                 }
                 //Week 2
                 $rstf = new RecurringScheduleTemplateFactory();
                 $rstf->setRecurringScheduleTemplateControl($rstc_id);
                 $rstf->setWeek(2);
                 $rstf->setSun(FALSE);
                 $rstf->setMon(TRUE);
                 $rstf->setTue(TRUE);
                 $rstf->setWed(TRUE);
                 $rstf->setThu(TRUE);
                 $rstf->setFri(TRUE);
                 $rstf->setSat(FALSE);
                 $rstf->setStartTime(strtotime('10:00 AM'));
                 $rstf->setEndTime(strtotime('07:00 PM'));
                 if ($schedule_policy_id > 0) {
                     $rstf->setSchedulePolicyID($schedule_policy_id);
                 }
                 $rstf->setBranch('-1');
                 //Default
                 $rstf->setDepartment('-1');
                 //Default
                 if ($rstf->isValid()) {
                     Debug::Text('Saving Recurring Schedule Week...', __FILE__, __LINE__, __METHOD__, 10);
                     $rstf->Save();
                 }
                 //Week 3
                 $rstf = new RecurringScheduleTemplateFactory();
                 $rstf->setRecurringScheduleTemplateControl($rstc_id);
                 $rstf->setWeek(3);
                 $rstf->setSun(FALSE);
                 $rstf->setMon(TRUE);
                 $rstf->setTue(TRUE);
                 $rstf->setWed(TRUE);
                 $rstf->setThu(TRUE);
                 $rstf->setFri(TRUE);
                 $rstf->setSat(FALSE);
                 $rstf->setStartTime(strtotime('02:00 PM'));
                 $rstf->setEndTime(strtotime('11:00 PM'));
                 if ($schedule_policy_id > 0) {
                     $rstf->setSchedulePolicyID($schedule_policy_id);
                 }
                 $rstf->setBranch('-1');
                 //Default
                 $rstf->setDepartment('-1');
                 //Default
                 if ($rstf->isValid()) {
                     Debug::Text('Saving Recurring Schedule Week...', __FILE__, __LINE__, __METHOD__, 10);
                     $rstf->Save();
                 }
                 return $rstc_id;
             }
             break;
     }
     Debug::Text('ERROR Saving schedule template!', __FILE__, __LINE__, __METHOD__, 10);
     return FALSE;
 }
 function postSave()
 {
     $this->removeCache($this->getId());
     if ($this->getDeleted() == TRUE) {
         Debug::Text('UnAssign Hours from Branch: ' . $this->getId(), __FILE__, __LINE__, __METHOD__, 10);
         //Unassign hours from this branch.
         $pcf = new PunchControlFactory();
         $udtf = new UserDateTotalFactory();
         $uf = new UserFactory();
         $sf = new StationFactory();
         $sf_b = new ScheduleFactory();
         $udf = new UserDefaultFactory();
         $rstf = new RecurringScheduleTemplateFactory();
         $query = 'update ' . $pcf->getTable() . ' set branch_id = 0 where branch_id = ' . $this->getId();
         $this->db->Execute($query);
         $query = 'update ' . $udtf->getTable() . ' set branch_id = 0 where branch_id = ' . $this->getId();
         $this->db->Execute($query);
         $query = 'update ' . $sf_b->getTable() . ' set branch_id = 0 where branch_id = ' . $this->getId();
         $this->db->Execute($query);
         $query = 'update ' . $uf->getTable() . ' set default_branch_id = 0 where company_id = ' . $this->getCompany() . ' AND default_branch_id = ' . $this->getId();
         $this->db->Execute($query);
         $query = 'update ' . $udf->getTable() . ' set default_branch_id = 0 where company_id = ' . $this->getCompany() . ' AND default_branch_id = ' . $this->getId();
         $this->db->Execute($query);
         $query = 'update ' . $sf->getTable() . ' set branch_id = 0 where company_id = ' . $this->getCompany() . ' AND branch_id = ' . $this->getId();
         $this->db->Execute($query);
         $query = 'update ' . $rstf->getTable() . ' set branch_id = 0 where branch_id = ' . $this->getId();
         $this->db->Execute($query);
     }
     return TRUE;
 }
 * Get FORM variables
 */
extract(FormVariables::GetVariables(array('action', 'page', 'sort_column', 'sort_order', 'ids')));
URLBuilder::setURL($_SERVER['SCRIPT_NAME'], array('sort_column' => $sort_column, 'sort_order' => $sort_order, 'page' => $page));
$sort_array = NULL;
if ($sort_column != '') {
    $sort_array = array($sort_column => $sort_order);
}
Debug::Arr($ids, 'Selected Objects', __FILE__, __LINE__, __METHOD__, 10);
$action = Misc::findSubmitButton();
switch ($action) {
    case 'add':
        Redirect::Page(URLBuilder::getURL(NULL, 'EditRecurringScheduleTemplate.php', FALSE));
        break;
    case 'copy':
        $rstf = new RecurringScheduleTemplateFactory();
        $rstlf = new RecurringScheduleTemplateListFactory();
        $rstclf = new RecurringScheduleTemplateControlListFactory();
        foreach ($ids as $id) {
            $rstclf->getByIdAndCompanyId($id, $current_company->getId());
            foreach ($rstclf as $rstc_obj) {
                $rstc_obj->StartTransaction();
                //Get week data
                $rstlf->getByRecurringScheduleTemplateControlId($rstc_obj->getId());
                if ($rstlf->getRecordCount() > 0) {
                    foreach ($rstlf as $rst_obj) {
                        $week_rows[$rst_obj->getId()] = array('id' => $rst_obj->getId(), 'week' => $rst_obj->getWeek(), 'sun' => $rst_obj->getSun(), 'mon' => $rst_obj->getMon(), 'tue' => $rst_obj->getTue(), 'wed' => $rst_obj->getWed(), 'thu' => $rst_obj->getThu(), 'fri' => $rst_obj->getFri(), 'sat' => $rst_obj->getSat(), 'start_time' => $rst_obj->getStartTime(), 'end_time' => $rst_obj->getEndTime(), 'total_time' => $rst_obj->getTotalTime(), 'schedule_policy_id' => $rst_obj->getSchedulePolicyID(), 'branch_id' => $rst_obj->getBranch(), 'department_id' => $rst_obj->getDepartment(), 'job_id' => $rst_obj->getJob(), 'job_item_id' => $rst_obj->getJobItem());
                    }
                }
                $rstc_obj->setId(FALSE);
                $rstc_obj->setName(Misc::generateCopyName($rstc_obj->getName()));
 function postSave()
 {
     if ($this->getDeleted() == TRUE) {
         Debug::Text('UnAssign Schedule Policy from Schedule/Recurring Schedules...' . $this->getId(), __FILE__, __LINE__, __METHOD__, 10);
         $sf = new ScheduleFactory();
         $rstf = new RecurringScheduleTemplateFactory();
         $query = 'update ' . $sf->getTable() . ' set schedule_policy_id = 0 where schedule_policy_id = ' . $this->getId();
         $this->db->Execute($query);
         $query = 'update ' . $rstf->getTable() . ' set schedule_policy_id = 0 where schedule_policy_id = ' . $this->getId();
         $this->db->Execute($query);
     }
     $this->removeCache($this->getId());
     return TRUE;
 }
     //Find next week
     $last_new_week = $week_rows[$row_keys[0]]['week'];
     $last_saved_week = $week_rows[array_pop($row_keys)]['week'];
     Debug::Text('Last New Week: ' . $last_new_week . ' Last Saved Week: ' . $last_saved_week, __FILE__, __LINE__, __METHOD__, 10);
     if ($last_new_week > $last_saved_week) {
         $last_week = $last_new_week;
     } else {
         $last_week = $last_saved_week;
     }
     Debug::Text('Last Week: ' . $last_week, __FILE__, __LINE__, __METHOD__, 10);
     $next_total_time = 0;
     if (count($week_rows) > 0) {
         foreach ($week_rows as $week_row_id => $week_row) {
             if ($week_row['week'] != '' and $week_row['week'] > 0) {
                 Debug::Text('Row ID: ' . $week_row_id . ' Week: ' . $week_row['week'] . ' Schedule Policy ID: ' . $week_row['schedule_policy_id'], __FILE__, __LINE__, __METHOD__, 10);
                 $rstf = new RecurringScheduleTemplateFactory();
                 $rstf->setStartTime($week_row['start_time']);
                 $rstf->setEndTime($week_row['end_time']);
                 $rstf->setSchedulePolicyID($week_row['schedule_policy_id']);
                 $rstf->preSave();
                 $week_rows[$week_row_id]['total_time'] = $rstf->getTotalTime();
                 if ($week_row_id == $lowest_id) {
                     $next_total_time = $week_rows[$week_row_id]['total_time'];
                 }
             }
         }
     }
     $week_rows[$next_blank_id] = array('id' => $next_blank_id, 'week' => $last_week + 1, 'start_time' => $week_rows[$lowest_id]['start_time'], 'end_time' => $week_rows[$lowest_id]['end_time'], 'total_time' => $next_total_time, 'schedule_policy_id' => $week_rows[$lowest_id]['schedule_policy_id'], 'branch_id' => $week_rows[$lowest_id]['branch_id'], 'department_id' => $week_rows[$lowest_id]['department_id'], 'mon' => @$week_rows[$lowest_id]['mon'], 'tue' => @$week_rows[$lowest_id]['tue'], 'wed' => @$week_rows[$lowest_id]['wed'], 'thu' => @$week_rows[$lowest_id]['thu'], 'fri' => @$week_rows[$lowest_id]['fri'], 'sat' => @$week_rows[$lowest_id]['sat'], 'sun' => @$week_rows[$lowest_id]['sun']);
 } elseif ($action != 'submit' and $action != 'delete') {
     $week_rows[-1] = array('id' => -1, 'week' => 1, 'mon' => TRUE, 'tue' => TRUE, 'wed' => TRUE, 'thu' => TRUE, 'fri' => TRUE, 'start_time' => strtotime('08:00'), 'end_time' => strtotime('17:00'), 'total_time' => 9 * 3600, 'branch_id' => -1, 'department_id' => -1, 'schedule_policy_id' => 0);
 }
    function getMaximumStartStopWindowByCompanyIdAndRecurringScheduleControlID($company_id, $id, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if ($id == '') {
            return FALSE;
        }
        if ($order == NULL) {
            $order = array('a.name' => 'asc');
            $strict = FALSE;
        } else {
            $strict = TRUE;
        }
        $rstf = new RecurringScheduleTemplateFactory();
        $ph = array('company' => $company_id, 'id' => $id);
        $query = '
					select 	max(start_stop_window)
					from	' . $this->getTable() . ' as spf
					LEFT JOIN ' . $rstf->getTable() . ' as rstf ON ( spf.id = rstf.schedule_policy_id )
					where	spf.company_id = ?
						AND rstf.recurring_schedule_template_control_id = ?
						AND ( spf.deleted = 0 AND rstf.deleted = 0 )';
        $window = $this->db->GetOne($query, $ph);
        if ($window === FALSE) {
            $window = 0;
        }
        Debug::text('Max Start/Stop Window: ' . $window, __FILE__, __LINE__, __METHOD__, 10);
        return $window;
    }