public function resaslotAction()
 {
     $this->disableLayout();
     $this->view->doc = $document;
     $dateres = $this->getParam('date');
     $reservationid = $this->getParam('reservationid');
     if ($reservationid) {
         $reservation = Object\Reservation::getById($reservationid);
         if ($reservation instanceof Object\Reservation) {
             $myreservationstartslot = $reservation->getStart()->get(Zend_Date::HOUR) . ":" . $reservation->getStart()->get(Zend_Date::MINUTE);
             $myreservationservingid = $reservation->getServing()->getId();
         }
     }
     if ($this->getParam('locationid')) {
         $locationid = $this->getParam('locationid');
     } else {
         //get default location from URL !!!! should be linked to societe
         $locationid = 55;
     }
     $location = Object_Location::getById($locationid, 1);
     $datetounix = new Zend_Date($dateres, 'dd-MM-YYYY HH:mm:ss');
     $datestart = $datetounix->getTimestamp();
     $dateend = $datestart + 86400;
     $d = $datetounix->get(Zend_Date::WEEKDAY_DIGIT);
     //First get a list of Reservation objects for the day for this location
     $dailyorders = new Object\Reservation\Listing();
     $dailyorders->setCondition("location__id =" . $locationid . " AND start >= " . $datestart . " AND end <= " . $dateend);
     //		var_dump( $dailyorders ); exit;
     if ($location instanceof Object_Location) {
         //get unit of time for that location
         $unit = $location->getResaunit() * 60;
         $maxresaperunit = $location->getMaxresaperunit();
         $maxseats = $location->getMaxseats();
         //get all servings for that location
         $servings = $location->getServings();
         $resafinal = array();
         foreach ($servings as $myserving) {
             $resa = array();
             // if( $myserving instanceof Object_Serving ){
             $mealduration = $myserving->getMealduration() * 60;
             $week = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
             if ($d == '1') {
                 $closed = $myserving->getClosedmonday();
                 $servingstart = $myserving->getTimestartmonday();
                 $servingend = $myserving->getTimeendmonday();
                 if ($myserving->getTimestartmonday()) {
                     $timestart = $datestart + $this->timeslotToMinutes($myserving->getTimestartmonday());
                 } else {
                     $timestart = $datestart;
                 }
                 if ($myserving->getTimeendmonday()) {
                     $timeend = $datestart + $this->timeslotToMinutes($myserving->getTimeendmonday());
                 } else {
                     $timeend = $datestart;
                 }
             } elseif ($d == '2') {
                 $closed = $myserving->getClosedtuesday();
                 $servingstart = $myserving->getTimestarttuesday();
                 $servingend = $myserving->getTimeendtuesday();
                 if ($myserving->getTimestarttuesday()) {
                     $timestart = $datestart + $this->timeslotToMinutes($myserving->getTimestarttuesday());
                 } else {
                     $timestart = $datestart;
                 }
                 if ($myserving->getTimeendtuesday()) {
                     $timeend = $datestart + $this->timeslotToMinutes($myserving->getTimeendtuesday());
                 } else {
                     $timeend = $datestart;
                 }
             } elseif ($d == '3') {
                 $closed = $myserving->getClosedwednesday();
                 $servingstart = $myserving->getTimestartwednesday();
                 $servingend = $myserving->getTimeendwednesday();
                 if ($myserving->getTimestartwednesday()) {
                     $timestart = $datestart + $this->timeslotToMinutes($myserving->getTimestartwednesday());
                 } else {
                     $timestart = $datestart;
                 }
                 if ($myserving->getTimeendwednesday()) {
                     $timeend = $datestart + $this->timeslotToMinutes($myserving->getTimeendwednesday());
                 } else {
                     $timeend = $datestart;
                 }
             } elseif ($d == '4') {
                 $closed = $myserving->getClosedthursday();
                 $servingstart = $myserving->getTimestartthursday();
                 $servingend = $myserving->getTimeendthursday();
                 if ($myserving->getTimestartthursday()) {
                     $timestart = $datestart + $this->timeslotToMinutes($myserving->getTimestartthursday());
                 } else {
                     $timestart = $datestart;
                 }
                 if ($myserving->getTimeendthursday()) {
                     $timeend = $datestart + $this->timeslotToMinutes($myserving->getTimeendthursday());
                 } else {
                     $timeend = $datestart;
                 }
             } elseif ($d == '5') {
                 $closed = $myserving->getClosedfriday();
                 $servingstart = $myserving->getTimestartfriday();
                 $servingend = $myserving->getTimeendfriday();
                 if ($myserving->getTimestartfriday()) {
                     $timestart = $datestart + $this->timeslotToMinutes($myserving->getTimestartfriday());
                 } else {
                     $timestart = $datestart;
                 }
                 if ($myserving->getTimeendfriday()) {
                     $timeend = $datestart + $this->timeslotToMinutes($myserving->getTimeendfriday());
                 } else {
                     $timeend = $datestart;
                 }
             } elseif ($d == '6') {
                 $closed = $myserving->getClosedsaturday();
                 $servingstart = $myserving->getTimestartsaturday();
                 $servingend = $myserving->getTimeendsaturday();
                 if ($myserving->getTimestartsaturday()) {
                     $timestart = $datestart + $this->timeslotToMinutes($myserving->getTimestartsaturday());
                 } else {
                     $timestart = $datestart;
                 }
                 if ($myserving->getTimeendsaturday()) {
                     $timeend = $datestart + $this->timeslotToMinutes($myserving->getTimeendsaturday());
                 } else {
                     $timeend = $datestart;
                 }
             } elseif ($d == '0') {
                 $closed = $myserving->getClosedsunday();
                 $servingstart = $myserving->getTimestartsunday();
                 $servingend = $myserving->getTimeendsunday();
                 if ($myserving->getTimestartsunday()) {
                     $timestart = $datestart + $this->timeslotToMinutes($myserving->getTimestartsunday());
                 } else {
                     $timestart = $datestart;
                 }
                 if ($myserving->getTimeendsunday()) {
                     $timeend = $datestart + $this->timeslotToMinutes($myserving->getTimeendsunday());
                 } else {
                     $timeend = $datestart;
                 }
             }
             if ($closed == 1) {
                 $closed = 'closed';
             } else {
                 $closed = "";
             }
             $endtime = $timeend - $mealduration;
             $resatime = array();
             $i = 0;
             while ($timestart <= $endtime) {
                 $i++;
                 $timeslot = date("H", $timestart) . ":" . date("i", $timestart);
                 $orderswarning = "";
                 $seatswarning = "";
                 $o = 0;
                 //initiate o number of orders already taken during this timeslot
                 $p = 0;
                 //initiate p number of seats already occupied during this timeslot
                 //Check if we have too many orders for this timeslot
                 foreach ($dailyorders as $order) {
                     if ($order->getStart()->getTimestamp() == $timestart) {
                         $o++;
                     }
                     //calculate number of orders in this timeslot
                 }
                 if ($o >= $maxresaperunit) {
                     $orderswarning = '-' . $o;
                 } else {
                     $orderswarning = '-ok';
                 }
                 //set warning is number exceeds max reservation per slot
                 //Check if we have available seats for this timeslot
                 $startslot = $timestart;
                 //First let s identify timestamp for startlost and endlot
                 $timestart = $timestart + $unit;
                 //Add unit of time to timestart
                 $endslot = $timestart;
                 //let's identify all orders that start after the start of the slot or that finish before the end of the slot
                 foreach ($dailyorders as $order) {
                     if ($startslot <= $order->getStart()->getTimestamp() && $endslot >= $order->getEnd()->getTimestamp() || $startslot >= $order->getStart()->getTimestamp() && $endslot <= $order->getStart()->getTimestamp() + $mealduration) {
                         $size = $order->getPartysize();
                         $p = $p + $size;
                     }
                     //echo $order->getId(); echo "<br>";
                 }
                 if ($p >= $maxseats) {
                     $seatswarning = '-' . $p;
                 } else {
                     $seatswarning = '-ok';
                 }
                 //Feed the data
                 if ($timeslot == $myreservationstartslot) {
                     $slotselected = '-selected';
                 } else {
                     $slotselected = '';
                 }
                 $resatime['shift-' . $i . $orderswarning . $seatswarning . $slotselected] = $timeslot;
                 //echo "<br>";echo "<br>";
             }
             if ($myserving->getId() == $myreservationservingid) {
                 $resafinal[$myserving->getTitle() . '_-_' . $myserving->getId() . '_-_selected_-_' . $closed . '_-_' . $servingstart . '_-_' . $servingend] = $resatime;
             } else {
                 $resafinal[$myserving->getTitle() . '_-_' . $myserving->getId() . '_-_notselected_-_' . $closed . '_-_' . $servingstart . '_-_' . $servingend] = $resatime;
             }
         }
         $reponse = new Reponse();
         $reponse->data = $resafinal;
         $reponse->message = $date;
         $reponse->success = true;
         $this->render($reponse);
     }
 }
 public function resaslotAction()
 {
     $this->disableLayout();
     $this->view->doc = $document;
     $dateres = $this->getParam('date');
     if ($this->getParam('locationid')) {
         $locationid = $this->getParam('locationid');
     } else {
         //get default location from URL !!!! should be linked to societe
         $locationid = 55;
     }
     $location = Object_Location::getById($locationid, 1);
     $datetounix = new Zend_Date($dateres, 'dd-MM-YYYY HH:mm:ss');
     $datestart = new Zend_Date($dateres . ' 00:00:00', 'dd-MM-YYYY HH:mm:ss');
     $dateend = new Zend_Date($dateres . ' 23:59:59', 'dd-MM-YYYY HH:mm:ss');
     $d = $datetounix->get(Zend_Date::WEEKDAY);
     //First get a list of Reservation objects for the day for this location
     $dailyorders = new Object\Reservation\Listing();
     $dailyorders->setCondition("location__id =" . $locationid . " AND start >= '" . $datestart->getTimestamp() . "' AND end <= '" . $dateend->getTimestamp() . "'");
     if ($location instanceof Object_Location) {
         //get unit of time for that location
         $unit = $location->getResaunit();
         $maxresaperunit = $location->getMaxresaperunit();
         $maxseats = $location->getMaxseats();
         $unit = '00:' . $unit . ':00';
         //get all servings for that location
         $servings = $location->getServings();
         $resafinal = array();
         foreach ($servings as $myserving) {
             $resa = array();
             // if( $myserving instanceof Object_Serving ){
             $meal = $myserving->getMealduration();
             //echo $meal;  echo "<BR>";
             $mealduration = '00:' . $meal . ':00';
             $week = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
             //foreach( $week as $d ){
             if ($d == 'Monday') {
                 $timestart = new Zend_Date($dateres . ' ' . $myserving->getTimestartmonday() . ':00', 'dd-MM-YYYY HH:mm:ss');
                 $timeend = new Zend_Date($dateres . ' ' . $myserving->getTimeendmonday() . ':00', 'dd-MM-YYYY HH:mm:ss');
             } elseif ($d == 'Tuesday') {
                 $timestart = new Zend_Date($dateres . ' ' . $myserving->getTimestarttuesday() . ':00', 'dd-MM-YYYY HH:mm:ss');
                 $timeend = new Zend_Date($dateres . ' ' . $myserving->getTimeendtuesday() . ':00', 'dd-MM-YYYY HH:mm:ss');
             } elseif ($d == 'Wednesday') {
                 $timestart = new Zend_Date($dateres . ' ' . $myserving->getTimestartwednesday() . ':00', 'dd-MM-YYYY HH:mm:ss');
                 $timeend = new Zend_Date($dateres . ' ' . $myserving->getTimeendwednesday() . ':00', 'dd-MM-YYYY HH:mm:ss');
             } elseif ($d == 'Thursday') {
                 $timestart = new Zend_Date($dateres . ' ' . $myserving->getTimestartthursday() . ':00', 'dd-MM-YYYY HH:mm:ss');
                 $timeend = new Zend_Date($dateres . ' ' . $myserving->getTimeendthursday() . ':00', 'dd-MM-YYYY HH:mm:ss');
             } elseif ($d == 'Friday') {
                 $timestart = new Zend_Date($dateres . ' ' . $myserving->getTimestartfriday() . ':00', 'dd-MM-YYYY HH:mm:ss');
                 $timeend = new Zend_Date($dateres . ' ' . $myserving->getTimeendfriday() . ':00', 'dd-MM-YYYY HH:mm:ss');
             } elseif ($d == 'Saturday') {
                 $timestart = new Zend_Date($dateres . ' ' . $myserving->getTimestartsaturday() . ':00', 'dd-MM-YYYY HH:mm:ss');
                 $timeend = new Zend_Date($dateres . ' ' . $myserving->getTimeendsaturday() . ':00', 'dd-MM-YYYY HH:mm:ss');
             } elseif ($d == 'Sunday') {
                 $timestart = new Zend_Date($dateres . ' ' . $myserving->getTimestartsunday() . ':00', 'dd-MM-YYYY HH:mm:ss');
                 $timeend = new Zend_Date($dateres . ' ' . $myserving->getTimeendsunday() . ':00', 'dd-MM-YYYY HH:mm:ss');
             }
             $endtime = $timeend->sub($mealduration, Zend_Date::TIMES);
             $resatime = array();
             $i = 0;
             while ($timestart->compare($endtime, Zend_Date::TIMES) == -1) {
                 $i++;
                 $timeslot = $timestart->get(Zend_Date::HOUR) . ":" . $timestart->get(Zend_Date::MINUTE);
                 $orderswarning = "";
                 $seatswarning = "";
                 $o = 0;
                 //initiate o number of orders already taken during this timeslot
                 $p = 0;
                 //initiate p number of seats already occupied during this timeslot
                 //Check if we have too many orders for this timeslot
                 foreach ($dailyorders as $order) {
                     if ($order->getStart()->getTimestamp() == $timestart->getTimestamp()) {
                         $o++;
                     }
                     //calculate number of orders in this timeslot
                 }
                 if ($o >= $maxresaperunit) {
                     $orderswarning = '-' . $o;
                 } else {
                     $orderswarning = '-ok';
                 }
                 //set warning is number exceeds max reservation per slot
                 //Check if we have available seats for this timeslot
                 $startslot = $timestart->getTimestamp();
                 //First let s identify timestamp for startlost and endlot
                 $timestart->add($unit, Zend_Date::TIMES);
                 //Add unit of time to timestart
                 $endslot = $timestart->getTimestamp();
                 //let's identify all orders that start after the start of the slot or that finish before the end of the slot
                 foreach ($dailyorders as $order) {
                     if ($order->getStart()->getTimestamp() <= $startslot and $order->getEnd()->getTimestamp() > $endslot or $order->getStart()->getTimestamp() > $startslot + (int) $meal * 60) {
                         $size = $order->getPartysize();
                         $p = $p + $size;
                     }
                 }
                 if ($p >= $maxseats) {
                     $seatswarning = '-' . $p;
                 } else {
                     $seatswarning = '-ok';
                 }
                 //Feed the data
                 $resatime['shift-' . $i . $orderswarning . $seatswarning] = $timeslot;
             }
             $resafinal[$myserving->getTitle() . '_-_' . $myserving->getId()] = $resatime;
         }
         //exit;
         $reponse = new Reponse();
         $reponse->data = $resafinal;
         $reponse->message = $date;
         $reponse->success = true;
         $this->render($reponse);
     }
 }