function createPunchPair($user_id, $in_time_stamp, $out_time_stamp, $data = NULL, $calc_total_time = TRUE)
 {
     $fail_transaction = FALSE;
     Debug::Text('Punch Full In Time Stamp: (' . $in_time_stamp . ') ' . TTDate::getDate('DATE+TIME', $in_time_stamp) . ' Out: (' . $out_time_stamp . ') ' . TTDate::getDate('DATE+TIME', $out_time_stamp), __FILE__, __LINE__, __METHOD__, 10);
     $pf = new PunchFactory();
     $pf->StartTransaction();
     //Out Punch
     //Save out punch first so the $pf object is for the In Punch if there is one.
     if ($out_time_stamp !== NULL) {
         $pf_in = new PunchFactory();
         $pf_in->setTransfer(FALSE);
         $pf_in->setUser($user_id);
         $pf_in->setType($data['out_type_id']);
         $pf_in->setStatus(20);
         $pf_in->setTimeStamp($out_time_stamp);
         if ($pf_in->isNew()) {
             $pf_in->setActualTimeStamp($out_time_stamp);
             $pf_in->setOriginalTimeStamp($pf_in->getTimeStamp());
         }
         $pf_in->setPunchControlID($pf_in->findPunchControlID());
         if ($pf_in->isValid()) {
             if ($pf_in->Save(FALSE) === FALSE) {
                 Debug::Text(' aFail Transaction: ', __FILE__, __LINE__, __METHOD__, 10);
                 $fail_transaction = TRUE;
             }
         }
     }
     if ($in_time_stamp !== NULL) {
         //In Punch
         $pf_out = new PunchFactory();
         $pf_out->setTransfer(FALSE);
         $pf_out->setUser($user_id);
         $pf_out->setType($data['in_type_id']);
         $pf_out->setStatus(10);
         $pf_out->setTimeStamp($in_time_stamp);
         if ($pf_out->isNew()) {
             $pf_out->setActualTimeStamp($in_time_stamp);
             $pf_out->setOriginalTimeStamp($pf_out->getTimeStamp());
         }
         if (isset($pf_in) and $pf_in->getPunchControlID() != FALSE) {
             //Get Punch Control ID from above Out punch.
             $pf_out->setPunchControlID($pf_in->getPunchControlID());
         } else {
             $pf_out->setPunchControlID($pf_out->findPunchControlID());
         }
         if ($pf_out->isValid()) {
             if ($pf_out->Save(FALSE) === FALSE) {
                 Debug::Text(' aFail Transaction: ', __FILE__, __LINE__, __METHOD__, 10);
                 $fail_transaction = TRUE;
             }
         }
     }
     if ($fail_transaction == FALSE) {
         if (isset($pf_in) and is_object($pf_in)) {
             Debug::Text(' Using In Punch Object... TimeStamp: ' . $pf_in->getTimeStamp(), __FILE__, __LINE__, __METHOD__, 10);
             $pf = $pf_in;
         } elseif (isset($pf_out) and is_object($pf_out)) {
             Debug::Text(' Using Out Punch Object... TimeStamp: ' . $pf_out->getTimeStamp(), __FILE__, __LINE__, __METHOD__, 10);
             $pf = $pf_out;
         }
         $pcf = new PunchControlFactory();
         $pcf->setId($pf->getPunchControlID());
         $pcf->setPunchObject($pf);
         $pcf->setBranch($data['branch_id']);
         $pcf->setDepartment($data['department_id']);
         if (isset($data['job_id'])) {
             $pcf->setJob($data['job_id']);
         }
         if (isset($data['job_item_id'])) {
             $pcf->setJobItem($data['job_item_id']);
         }
         if (isset($data['quantity'])) {
             $pcf->setQuantity($data['quantity']);
         }
         if (isset($data['bad_quantity'])) {
             $pcf->setBadQuantity($data['bad_quantity']);
         }
         $pcf->setEnableCalcUserDateID(TRUE);
         $pcf->setEnableCalcTotalTime($calc_total_time);
         $pcf->setEnableCalcSystemTotalTime($calc_total_time);
         $pcf->setEnableCalcWeeklySystemTotalTime($calc_total_time);
         $pcf->setEnableCalcUserDateTotal($calc_total_time);
         $pcf->setEnableCalcException($calc_total_time);
         if ($pcf->isValid() == TRUE) {
             $punch_control_id = $pcf->Save(TRUE, TRUE);
             //Force lookup
             if ($fail_transaction == FALSE) {
                 Debug::Text('Punch Control ID: ' . $punch_control_id, __FILE__, __LINE__, __METHOD__, 10);
                 $pf->CommitTransaction();
                 return TRUE;
             }
         }
     }
     Debug::Text('Failed Creating Punch!', __FILE__, __LINE__, __METHOD__, 10);
     $pf->FailTransaction();
     return FALSE;
 }
Beispiel #2
0
     $pcf->setOtherID2($pc_data['other_id2']);
 }
 if (isset($pc_data['other_id3'])) {
     $pcf->setOtherID3($pc_data['other_id3']);
 }
 if (isset($pc_data['other_id4'])) {
     $pcf->setOtherID4($pc_data['other_id4']);
 }
 if (isset($pc_data['other_id5'])) {
     $pcf->setOtherID5($pc_data['other_id5']);
 }
 $pcf->setEnableStrictJobValidation(TRUE);
 $pcf->setEnableCalcUserDateID(TRUE);
 $pcf->setEnableCalcTotalTime(TRUE);
 $pcf->setEnableCalcSystemTotalTime(TRUE);
 $pcf->setEnableCalcWeeklySystemTotalTime(TRUE);
 $pcf->setEnableCalcUserDateTotal(TRUE);
 $pcf->setEnableCalcException(TRUE);
 if ($pcf->isValid() == TRUE) {
     Debug::Text(' Punch Control is valid, saving...: ', __FILE__, __LINE__, __METHOD__, 10);
     if ($pcf->Save(TRUE, TRUE) != TRUE) {
         //Force isNew() lookup.
         Debug::Text(' aFail Transaction: ', __FILE__, __LINE__, __METHOD__, 10);
         $fail_transaction = TRUE;
         break;
     }
 } else {
     Debug::Text(' bFail Transaction: ', __FILE__, __LINE__, __METHOD__, 10);
     $fail_transaction = TRUE;
     break;
 }
 function preSave()
 {
     if ($this->isNew()) {
         //Debug::text(' Setting Original TimeStamp: '. $this->getTimeStamp(), __FILE__, __LINE__, __METHOD__,10);
         $this->setOriginalTimeStamp($this->getTimeStamp());
     }
     if ($this->getDeleted() == FALSE) {
         if ($this->getTransfer() == TRUE and $this->getEnableAutoTransfer() == TRUE) {
             Debug::text(' Transfer is Enabled, automatic punch out of last punch pair: ', __FILE__, __LINE__, __METHOD__, 10);
             //Check to make sure there is an open punch pair.
             $plf = new PunchListFactory();
             $plf->getPreviousPunchByUserIdAndEpoch($this->getUser(), $this->getTimeStamp());
             if ($plf->getRecordCount() > 0) {
                 $p_obj = $plf->getCurrent();
                 Debug::text(' Found Last Punch: ', __FILE__, __LINE__, __METHOD__, 10);
                 if ($p_obj->getStatus() == 10) {
                     Debug::text(' Last Punch was in. Auto Punch Out now: ', __FILE__, __LINE__, __METHOD__, 10);
                     //Make sure the current punch status is IN
                     $this->setStatus(10);
                     //In
                     $this->setType(10);
                     //Normal (can't transfer in/out of lunches?)
                     $pf = new PunchFactory();
                     $pf->setUser($this->getUser());
                     $pf->setEnableAutoTransfer(FALSE);
                     $pf->setPunchControlID($p_obj->getPunchControlID());
                     $pf->setTransfer(TRUE);
                     $pf->setType($p_obj->getNextType());
                     $pf->setStatus(20);
                     //Out
                     $pf->setTimeStamp($this->getTimeStamp(), FALSE);
                     //Disable rounding.
                     $pf->setActualTimeStamp($this->getTimeStamp());
                     $pf->setOriginalTimeStamp($this->getTimeStamp());
                     if ($pf->isValid()) {
                         if ($pf->Save(FALSE) == TRUE) {
                             $p_obj->getPunchControlObject()->setEnableCalcTotalTime(TRUE);
                             $p_obj->getPunchControlObject()->setEnableCalcSystemTotalTime(TRUE);
                             $p_obj->getPunchControlObject()->setEnableCalcUserDateTotal(TRUE);
                             $p_obj->getPunchControlObject()->setEnableCalcException(TRUE);
                             $p_obj->getPunchControlObject()->setEnablePreMatureException(TRUE);
                             if ($p_obj->getPunchControlObject()->isValid()) {
                                 $p_obj->getPunchControlObject()->Save();
                             } else {
                                 Debug::text(' aError saving auto out punch...', __FILE__, __LINE__, __METHOD__, 10);
                             }
                         } else {
                             Debug::text(' bError saving auto out punch...', __FILE__, __LINE__, __METHOD__, 10);
                         }
                     } else {
                         Debug::text(' cError saving auto out punch...', __FILE__, __LINE__, __METHOD__, 10);
                     }
                 } else {
                     Debug::text(' Last Punch was out. No Auto Punch ', __FILE__, __LINE__, __METHOD__, 10);
                 }
             }
             unset($plf, $p_obj, $pf);
         }
         //Split punch at midnight.
         //This has to be an Out punch, and the previous punch has to be an in punch in order for the split to occur.
         //Check to make sure there is an open punch pair.
         //Make sure this punch isn't right at midnight either, as no point in splitting a punch at that time.
         //FIXME: What happens if a supervisor edits a 11:30PM punch and makes it 5:00AM the next day?
         //		We can't split punches when editing, because we have to split punch_control_ids prior to saving etc...
         if ($this->isNew() == TRUE and $this->getStatus() == 20 and $this->getEnableSplitAtMidnight() == TRUE and $this->getTimeStamp() != TTDate::getBeginDayEpoch($this->getTimeStamp()) and (is_object($this->getPunchControlObject()) and is_object($this->getPunchControlObject()->getPayPeriodScheduleObject()) and $this->getPunchControlObject()->getPayPeriodScheduleObject()->getShiftAssignedDay() == 40)) {
             $plf = new PunchListFactory();
             $plf->getPreviousPunchByUserIdAndEpoch($this->getUser(), $this->getTimeStamp());
             if ($plf->getRecordCount() > 0) {
                 $p_obj = $plf->getCurrent();
                 Debug::text(' Found Last Punch: ', __FILE__, __LINE__, __METHOD__, 10);
                 if ($p_obj->getStatus() == 10 and TTDate::doesRangeSpanMidnight($this->getTimeStamp(), $p_obj->getTimeStamp())) {
                     Debug::text(' Last Punch was in and this is an out punch that spans midnight. Split Punch at midnight now: ', __FILE__, __LINE__, __METHOD__, 10);
                     //FIXME: This will fail if a shift spans multiple days!
                     //Make sure the current punch status is OUT
                     //But we can split LUNCH/Break punches, because someone could punch in at 8PM, then out for lunch at 1:00AM, this would need to be split.
                     $this->setStatus(20);
                     //Out
                     //Reduce the out punch by 60 seconds, and increase the current punch by 60seconds so no time is lost.
                     $this->setTimeStamp($this->getTimeStamp() + 60);
                     //FIXME: May need to use ActualTimeStamp here so we aren't double rounding.
                     //Get new punch control ID for the midnight punch and this one.
                     $new_punch_control_id = $this->getPunchControlObject()->getNextInsertId();
                     $this->setPunchControlID($new_punch_control_id);
                     Debug::text(' Split Punch: Punching out just before midnight yesterday...', __FILE__, __LINE__, __METHOD__, 10);
                     //
                     //Punch out just before midnight
                     //
                     $pf = new PunchFactory();
                     $pf->setUser($this->getUser());
                     $pf->setEnableSplitAtMidnight(FALSE);
                     $pf->setTransfer(FALSE);
                     $pf->setEnableAutoTransfer(FALSE);
                     $pf->setType(10);
                     //Normal
                     $pf->setStatus(20);
                     //Out
                     $before_midnight_timestamp = TTDate::getBeginDayEpoch($this->getTimeStamp()) - 60;
                     $pf->setTimeStamp($before_midnight_timestamp, FALSE);
                     //Disable rounding.
                     $pf->setActualTimeStamp($before_midnight_timestamp);
                     $pf->setOriginalTimeStamp($before_midnight_timestamp);
                     $pf->setPunchControlID($p_obj->getPunchControlID());
                     if ($pf->isValid()) {
                         if ($pf->Save(FALSE) == TRUE) {
                             $p_obj->getPunchControlObject()->setEnableCalcTotalTime(TRUE);
                             $p_obj->getPunchControlObject()->setEnableCalcSystemTotalTime(TRUE);
                             $p_obj->getPunchControlObject()->setEnableCalcUserDateTotal(TRUE);
                             $p_obj->getPunchControlObject()->setEnableCalcException(TRUE);
                             $p_obj->getPunchControlObject()->setEnablePreMatureException(TRUE);
                             $p_obj->getPunchControlObject()->Save();
                         }
                     }
                     unset($pf, $p_obj, $before_midnight_timestamp);
                     Debug::text(' Split Punch: Punching int at midnight today...', __FILE__, __LINE__, __METHOD__, 10);
                     //
                     //Punch in again right at midnight.
                     //
                     $pf = new PunchFactory();
                     $pf->setUser($this->getUser());
                     $pf->setEnableSplitAtMidnight(FALSE);
                     $pf->setTransfer(FALSE);
                     $pf->setEnableAutoTransfer(FALSE);
                     $pf->setType(10);
                     //Normal
                     $pf->setStatus(10);
                     //In
                     $at_midnight_timestamp = TTDate::getBeginDayEpoch($this->getTimeStamp());
                     $pf->setTimeStamp($at_midnight_timestamp, FALSE);
                     //Disable rounding.
                     $pf->setActualTimeStamp($at_midnight_timestamp);
                     $pf->setOriginalTimeStamp($at_midnight_timestamp);
                     $pf->setPunchControlID($new_punch_control_id);
                     if ($pf->isValid()) {
                         if ($pf->Save(FALSE) == TRUE) {
                             $pcf = new PunchControlFactory();
                             $pcf->setId($pf->getPunchControlID());
                             $pcf->setPunchObject($pf);
                             $pcf->setBranch($this->getPunchControlObject()->getBranch());
                             $pcf->setDepartment($this->getPunchControlObject()->getDepartment());
                             $pcf->setJob($this->getPunchControlObject()->getJob());
                             $pcf->setJobItem($this->getPunchControlObject()->getJobItem());
                             $pcf->setOtherID1($this->getPunchControlObject()->getOtherID1());
                             $pcf->setOtherID2($this->getPunchControlObject()->getOtherID2());
                             $pcf->setOtherID3($this->getPunchControlObject()->getOtherID3());
                             $pcf->setOtherID4($this->getPunchControlObject()->getOtherID4());
                             $pcf->setOtherID5($this->getPunchControlObject()->getOtherID5());
                             $pcf->setEnableStrictJobValidation(TRUE);
                             $pcf->setEnableCalcUserDateID(TRUE);
                             $pcf->setEnableCalcTotalTime(TRUE);
                             $pcf->setEnableCalcSystemTotalTime(TRUE);
                             $pcf->setEnableCalcWeeklySystemTotalTime(TRUE);
                             $pcf->setEnableCalcUserDateTotal(TRUE);
                             $pcf->setEnableCalcException(TRUE);
                             if ($pcf->isValid() == TRUE) {
                                 $pcf->Save(TRUE, TRUE);
                                 //Force isNEW() lookup.
                             }
                         }
                     }
                     unset($pf, $at_midnight_timestamp, $new_punch_control_id);
                 } else {
                     Debug::text(' Last Punch was out. No Auto Punch ', __FILE__, __LINE__, __METHOD__, 10);
                 }
             }
         }
     }
     return TRUE;
 }