public function locationsListAction()
 {
     //get societe/location and serving
     $reponse = new Reponse();
     $societe = $this->societe;
     $locations = $societe->getLocations();
     //we check if we are in the Editor
     if ($_POST['action']) {
         //if REMOVE
         if ($_POST['action'] == "remove") {
             foreach ($_POST['id'] as $id) {
                 $location = \Object_Location::getById($id, 1);
                 if ($location instanceof Object\Location) {
                     $location->delete();
                 }
             }
             $reponse->message = 'TXT_RESERVATION_LIST';
             $reponse->success = true;
             $reponse->data = '';
         }
         //if EDIT
         if ($_POST['action'] == "edit") {
             $location = \Object\Location::getById($_POST['id'], 1);
             if ($location instanceof \Object\Location) {
                 $location->setName($_POST['data']['name']);
                 $location->setMaxSeats($_POST['data']['maxSeats']);
                 $location->setMaxTables($_POST['data']['maxTables']);
                 $location->setResaUnit($_POST['data']['resaUnit']);
                 $location->setMaxResaPerUnit($_POST['data']['maxResaPerUnit']);
                 $location->save();
             }
             $data = $_POST['data'];
             $data['DT_RowId'] = "row_" . $_POST['data']['id'];
             $reponse->message = 'TXT_RESERVATION_LIST';
             $reponse->success = true;
             $reponse->row = $data;
         }
         //if CREATE
         if ($_POST['action'] == "create") {
             $societe = $this->societe;
             //SET DEFAULT DATA FROM SOCIETE
             $row['address'] = $societe->getAddress();
             $row['zip'] = $societe->getZip();
             $row['city'] = $societe->getCity();
             $row['email'] = $societe->getEmail();
             $row['tel'] = $societe->getTel();
             $row['fax'] = $societe->getFax();
             $row['email'] = $societe->getEmail();
             $row['maxSeats'] = $societe->getMaxSeats();
             $row['maxTables'] = $societe->getMaxTables();
             $row['maxResaPerUnit'] = $societe->getMaxResaPerUnit();
             $row['mealduration'] = $societe->getMealduration();
             $row['resaUnit'] = $societe->getResaUnit();
             $row['description'] = $societe->getDescription();
             //COLECT DATA FROM DATATABLE EDITOR
             $row['name'] = $_POST['data']['name'];
             //CREATE THE LOCATION
             $result = $societe->createLocation($row);
             if ($result instanceof \Object\Location) {
                 //CREATE NEW SERVING
                 $newserving['title'] = 'Nouveau Service';
                 $newserving['maxseats'] = $societe->getMaxSeats();
                 $newserving['maxtables'] = $societe->getMaxTables();
                 $newserving['mealduration'] = $societe->getMealduration();
                 $serving = $result->createServing($newserving);
                 $row['DT_RowId'] = $result->getId();
                 $row['id'] = $result->getId();
                 $reponse->success = true;
                 $reponse->message = "TXT_CREATE_OK";
                 $reponse->row = $row;
                 $reponse->debug = $data;
             } else {
                 $reponse->success = false;
                 $reponse->message = "TXT_CREATE_ERROR";
                 $reponse->row = $result;
                 $reponse->debug = $result;
             }
         }
     } else {
         $data = array();
         foreach ($locations as $key => $location) {
             $i++;
             $array = array();
             $array = $location->toArray();
             $servingarray = array();
             foreach ($location->getServings() as $serving) {
                 array_push($servingarray, $serving->getTitle() . '----' . $serving->getId());
             }
             $array['servings'] = implode('____', $servingarray);
             $array['id'] = $location->getId();
             $array['DT_RowId'] = $location->getId();
             array_push($data, $array);
         }
         $reponse->message = 'TXT_RESERVATION_LIST';
         $reponse->success = true;
         $reponse->data = $data;
     }
     $this->render($reponse);
 }
 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 reservationListAction()
 {
     //get societe/location and serving
     $reponse = new Reponse();
     $societe = $this->societe;
     $locations = $societe->getLocations();
     //if location is not defined we take the first in the list
     $initiallocationid = $locations[0]->getId();
     if ($this->getParam('locationid')) {
         $locationid = $this->getParam('locationid');
     } else {
         $locationid = $initiallocationid;
     }
     $this->view->locations = $locations;
     $mylocation = Object_Location::getById($locationid, 1);
     if ($mylocation instanceof Object_Location) {
         //we check if we are in the Editor
         if ($_POST['action']) {
             //if REMOVE
             if ($_POST['action'] == "remove") {
                 foreach ($_POST['id'] as $id) {
                     $myreservation = Object_Reservation::getById($id, 1);
                     if ($myreservation instanceof Object_Reservation) {
                         $myreservation->SetStatus('cancelled');
                         $myreservation->save();
                     }
                 }
                 $reponse->message = 'TXT_RESERVATION_LIST';
                 $reponse->success = true;
                 $reponse->data = '';
             }
             //if EDIT
             if ($_POST['action'] == "edit") {
                 $myreservation = Object_Reservation::getById($_POST['id'], 1);
                 if ($_POST['data']['guestid']) {
                     $guest = Object_Guest::getById($_POST['data']['guestid'], 1);
                     if ($guest instanceof Object_Guest) {
                         $guest->setLastname($_POST['data']['guestname']);
                         $guest->setTel($_POST['data']['guesttel']);
                         $guest->save();
                     }
                 }
                 //date update needs to be reworked
                 //$myreservation->setStart($_POST['data']['start']);
                 $myreservation->setPartysize($_POST['data']['partysize']);
                 $myreservation->setBookingref($_POST['data']['bookingref']);
                 $myreservation->setBookingnotes($_POST['data']['bookingnotes']);
                 $myreservation->setStatus($_POST['data']['status']);
                 $myreservation->save();
                 $data = $_POST['data'];
                 $data['DT_RowId'] = "row_" . $_POST['data']['id'];
                 $reponse->message = 'TXT_RESERVATION_LIST';
                 $reponse->success = true;
                 $reponse->row = $data;
             }
         } else {
             $reservations = $mylocation->getReservations();
             $data = array();
             $i = 0;
             foreach ($reservations as $key => $reservation) {
                 $i++;
                 $resa = $this->formatReservation($reservation);
                 array_push($data, $resa);
             }
             $reponse->message = 'TXT_RESERVATION_LIST';
             $reponse->success = true;
             $reponse->data = $data;
         }
     } else {
         $data = array();
         $reponse->message = 'TXT_NOT_RESERVATION_LIST';
         $reponse->success = false;
         $reponse->data = $data;
     }
     $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);
     }
 }