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;
 }
        }
    }
}
$rstcf = new RecurringScheduleTemplateControlFactory();
$rstf = new RecurringScheduleTemplateFactory();
$action = Misc::findSubmitButton();
$action = strtolower($action);
switch ($action) {
    case 'submit':
        Debug::Text('Submit!', __FILE__, __LINE__, __METHOD__, 10);
        //Debug::setVerbosity(11);
        $redirect = 0;
        $rstcf->StartTransaction();
        $rstcf->setId($data['id']);
        $rstcf->setCompany($current_company->getId());
        $rstcf->setName($data['name']);
        $rstcf->setDescription($data['description']);
        if ($rstcf->isValid()) {
            $rstc_id = $rstcf->Save();
            Debug::Text('aRecurring Schedule Template Control ID: ' . $rstc_id, __FILE__, __LINE__, __METHOD__, 10);
            if ($rstc_id === TRUE) {
                $rstc_id = $data['id'];
            }
            Debug::Text('bRecurring Schedule Template Control ID: ' . $rstc_id, __FILE__, __LINE__, __METHOD__, 10);
            //Save each weeks data.
            if (count($week_rows) > 0) {
                foreach ($week_rows as $week_row_id => $week_row) {
                    Debug::Text('Row ID: ' . $week_row_id . ' Week: ' . $week_row['week'], __FILE__, __LINE__, __METHOD__, 10);
                    if ($week_row['week'] != '' and $week_row['week'] > 0) {
                        if ($week_row_id > 0) {
                            $rstf->setID($week_row_id);