Sunday, January 24, 2016

Adding multiple events

Hi,

I'm working on a project which creates entries on individual working days from the start date and the end date selected. I have done the calculation to get no. of working days between the dates selected, however, i dont have any idea how will i go about inserting into the DB, taking out start date and end date for individual working days from the calculated no. of working days.

Below is my code to where I'm up to currently. Any bit of help is much appreciated since i'm a newbie to cakephp

public function add_multiple() {
        if ($this->request->is('post')) {
            $this->Leave->create();
            $user = $this->Session->read('Auth.User');
            $this->request->data['Leave']['created_by'] = $user['name'];
            $this->request->data['Approval'][0]['user_id'] = $user['id'];
            $this->request->data['Approval'][0]['state_id'] = 2;
           
           
            $startTime = $this->request->data['Leave']['event_date'];
            $endTime = $this->request->data['Leave']['event_stop_time'];
            $daysweekly = $this->request->data['Leave']['days_weekly'] ;
                                       
               
            $start = strtotime($startTime); //Convert to unix time
            $end = strtotime($endTime); //Convert to unix time             
           
            $days = ($end - $start) / 86400 + 1;

            $no_full_weeks = floor($days / 7);
            $no_remaining_days = fmod($days, 7);
           
             //returns 1 for Monday,7 for Sunday
            $the_first_day_of_week = date("N", $start);
            $the_last_day_of_week = ($start - $daysweekly);
           
            //First case, interval within a week..Second case, interval falls in two weeks
            if ($the_first_day_of_week <= $the_last_day_of_week) {
                if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week)
                        $no_remaining_days--;
                if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week)
                        $no_remaining_days--;
    }
    else {
       
        // the day of the week for start is later than the day of the week for end
        if ($the_first_day_of_week == 7) {
            // if the start date is a Sunday, definitely subtract 1 day
            $no_remaining_days--;

            if ($the_last_day_of_week == 6) {
                // if the end date is a Saturday, subtract another day
                $no_remaining_days--;
            }
        }
        else {
            // the start date was a Saturday (or earlier), end date was (Mon..Fri)
            // skip an entire weekend and subtract 2 days
            $no_remaining_days -= 2;
        }
    }

    //working days: (no. of weeks between the 2 dates) * (days per week) + the remainder
    //february in none leap years gives a remainder of 0, still calculated weekends between first and last day
   $workingDays = $no_full_weeks * $daysweekly;
    if ($no_remaining_days > 0 )
    {
      $workingDays += $no_remaining_days;
    }
   
    //calculate holidays
    $holidays = $this->Leave->find ('all', array('conditions'=>array('DATE_FORMAT(Leave.event_date,"%Y/%m/%d")'=>'public_holidays.SetDate')));
   
     foreach($holidays as $holiday){
        $time_stamp=strtotime($holiday);
        //If the holiday doesn't fall in weekend
        if ($start <= $time_stamp && $time_stamp <= $end && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
            $workingDays--;
        }
               
        return $workingDays;
}

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at https://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: