/** * Update inventory */ public function update() { // get the hotel $hotel = $this->getHotel(); if (!$hotel) { $this->setError(JText::_('COM_CHPANEL_MANAGE_ERROR_HOTEL')); return false; } // prepare check $db = JFactory::getDbo(); $app = JFactory::getApplication(); $date_format = CHPanelHelper::getDateFormat(JText::_('COM_CHPANEL_LOCALE')); // start $start = $app->getUserStateFromRequest('com_chpanel.manage.start', 'start', JFactory::getDate()->format($date_format), 'string'); $date_start = new JDate(CHPanelHelper::correctDateFormat($start)); $this->start_date = $date_start->format('Y-m-d'); // end $end = $app->getUserStateFromRequest('com_chpanel.manage.end', 'end', JFactory::getDate('+ 1 day')->format($date_format), 'string'); $date_end = new JDate(CHPanelHelper::correctDateFormat($end)); $this->end_date = $date_end->format('Y-m-d'); // check dates if (!$this->start_date || !$this->end_date) { $this->setError(JText::_('COM_CHPANEL_MANAGE_ERROR_DATES')); return false; } // check period if ($date_end->format('Ymd') < $date_start->format('Ymd')) { $this->setError(JText::_('COM_CHPANEL_MANAGE_ERROR_DATES')); return false; } // check values $rids = array(); foreach ($hotel->rooms as $room) { if (JRequest::getString('availability_' . $room->id, '', 'post') != '') { $rids[] = $room->id; } if (JRequest::getString('rate_' . $room->id, '', 'post') != '') { $rids[] = $room->id; } } if (!$rids) { $this->setError(JText::_('COM_CHPANEL_MANAGE_ERROR_INVENTORY')); return false; } // set filter_month state JFactory::getApplication()->setUserState('com_chpanel.manage.filter.month', $date_start->format('m-Y')); // get the rooms ids $room_ids = array_unique($rids); // update inventory per room foreach ($room_ids as $room_id) { // get the room default rate foreach ($hotel->rooms as $room) { if ($room->id == $room_id) { $default_rate = $room->rate; } } // get the room current inventory $inventory_rows = $this->getRoomInventory($room_id); // prepare loop dates $date = $date_start->toUnix(); $while_date = $date_start->format('Ymd'); $while_end = $date_end->format('Ymd'); $current_date = $date_start->format('Y-m-d'); // update inventory while ($while_date <= $while_end) { // new object query $row = new stdClass(); // get row values if (JRequest::getString('availability_' . $room_id, '', 'post') != '') { $row->availability = JRequest::getInt('availability_' . $room_id, 0, 'post'); } if (JRequest::getString('rate_' . $room_id, '', 'post') != '') { $row->rate = JRequest::getFloat('rate_' . $room_id, 0, 'post'); } // update or insert inventory row if (isset($inventory_rows[$current_date])) { // update row $row->id = $inventory_rows[$current_date]->id; $db->updateObject('#__chpanel_inventory', $row, 'id'); } else { // default availability, 0 for new rows if (!isset($row->availability)) { $row->availability = 0; } // default rate, room rack rate if (!isset($row->rate)) { $row->rate = $default_rate; } // insert row $row->room_id = $room_id; $row->date = $current_date; $db->insertObject('#__chpanel_inventory', $row); } // update loop conditions $date = strtotime('+1 day', $date); $j_date = JFactory::getDate($date); $while_date = $j_date->format('Ymd'); $current_date = $j_date->format('Y-m-d'); } } // return ok JFactory::getApplication()->enqueueMessage(JText::_('COM_CHPANEL_MANAGE_APPLY_OK')); return true; }
/** * Query */ protected function getListQuery() { // main query $query = $this->_db->getQuery(true); $query->select('a.*, a.first_name AS title'); $query->from('#__chpanel_bookings AS a'); // joins $query->select('h.title AS hotel')->join('LEFT', '#__chpanel_hotels AS h ON h.id = a.hotel_id'); $query->select('r.title AS room, r.reference')->join('LEFT', '#__chpanel_rooms AS r ON r.id = a.room_id'); $query->select('l.title AS language_title')->join('LEFT', '#__languages AS l ON l.lang_code = h.language'); // checked out $query->select('uc.name AS editor'); $query->join('LEFT', '#__users AS uc ON uc.id=a.checked_out_time'); // status filter $status = $this->getState('filter.status'); if (is_numeric($status)) { if ($status % 100 == 0) { $statuses = array($status + 1, $status + 2, $status + 3); $query->where('a.status IN (' . implode(',', $statuses) . ')'); } else { $query->where('a.status = ' . $status); } } else { $query->where('a.status IN (201,202,203)'); // confirmed bookings } // state filter $state = $this->getState('filter.state'); if (is_numeric($state)) { $query->where('a.state = ' . (int) $state); } else { if ($state != '*') { $query->where('a.state = 1'); } } // search filter $search = $this->getState('filter.search'); if (!empty($search)) { if (stripos($search, 'id:') === 0) { $query->where('a.id = ' . (int) substr($search, 3)); } else { $search = $this->_db->Quote('%' . $this->_db->escape($search, true) . '%'); $query->where('(a.first_name LIKE ' . $search . ')'); } } // other standard filters foreach (array('hotel') as $filter_name) { $filter_value = $this->getState('filter.' . $filter_name); if (is_numeric($filter_value)) { $query->where('a.' . $filter_name . '_id = ' . $filter_value); } } // date filters $db = JFactory::getDbo(); $app = JFactory::getApplication(); $start = $app->getUserStateFromRequest('com_chpanel.bookings.start', 'start', '', 'string'); $end = $app->getUserStateFromRequest('com_chpanel.bookings.end', 'end', '', 'string'); $date = $app->getUserStateFromRequest('com_chpanel.bookings.date', 'date', 'created', 'string'); if (in_array($date, array('created', 'checkin', 'checkout'))) { if ($start) { $date_start = new JDate(CHPanelHelper::correctDateFormat($start)); $query_start = $db->quote($date_start->format('Y-m-d')); $query->where("{$query_start} <= DATE(a.{$date})"); } if ($end) { $date_end = new JDate(CHPanelHelper::correctDateFormat($end)); $query_end = $db->quote($date_end->format('Y-m-d')); $query->where("DATE(a.{$date}) <= {$query_end}"); } } // ordering clause $orderCol = $this->state->get('list.ordering'); $orderDirn = $this->state->get('list.direction'); $query->order($this->_db->escape("{$orderCol} {$orderDirn}")); // echo nl2br(str_replace('#__', 'jos_', $query)); return $query; }