'ari_updates', 'primary' => 'id_ari_updates', 'fields' => array( 'id_hotel' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 'id_room_type' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 'date' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true), 'ari_data' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), 'status' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'), 'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'), ), ); //Returns the array of existing ARI update data for a room type, keyed by date public function getExistingAriUpdateData($idHotel, $idRoomType) { $sql = 'SELECT `id_ari_updates` as `id`, `date`, `ari_data` FROM `'._DB_PREFIX_.'ari_updates` WHERE id_hotel = '.(int)$idHotel.' AND id_room_type = '.(int)$idRoomType; $existingAriData = Db::getInstance()->executeS($sql); // Reindex array by date $result = []; if ($existingAriData) { foreach ($existingAriData as $value) { $dateKey = date('Y-m-d', strtotime($value['date'])); $result[$dateKey] = $value; } } return $result; } //Returns the row hotel (order) wise where the queue row is not processed public function getPendingAriUpdateRows() { $sql = 'SELECT * FROM `'._DB_PREFIX_.'ari_updates` WHERE status = ' . (int) self::ARI_SYNC_NOT_PROCESSED . ' ORDER BY id_hotel ASC, date_add ASC'; return Db::getInstance()->executeS($sql); } //Universal function to save rows in the table, currently handles availablity only public function saveAriUpdateRow($idHotel, $idRoomType, $dateFrom, $dateTo, $ariData = []) { if (empty($idHotel) || empty($idRoomType)) { return false; } // Set default ari_data if not provided if(empty($ariData)) { $ariData['inventory'] = 1; } // Set default ari_data if not provided $defaultAriData = []; // Merge provided data with defaults $ariData = array_merge($defaultAriData, $ariData); // Get existing data for this room type, keyed by date $getExistingDataByRoomType = $this->getExistingAriUpdateData($idHotel, $idRoomType); // for loop from dateFrom to dateTo for ($date = $dateFrom; $date <= $dateTo; $date = date('Y-m-d', strtotime($date . ' +1 day'))) { if (isset($getExistingDataByRoomType[$date])) { // Update existing record $objAriUpdates = new QcmcAriUpdates($getExistingDataByRoomType[$date]['id']); $existingAriData = json_decode($getExistingDataByRoomType[$date]['ari_data'], true); if ($existingAriData) { $ariData = array_merge($existingAriData, $ariData); } } else { // Create new record $objAriUpdates = new QcmcAriUpdates(); } $objAriUpdates->id_hotel = (int)$idHotel; $objAriUpdates->id_room_type = (int)$idRoomType; $objAriUpdates->date = pSQL($date); $objAriUpdates->ari_data = json_encode($ariData); $objAriUpdates->status = self::ARI_SYNC_NOT_PROCESSED; $objAriUpdates->save(); } return true; } //mark the rows in the table under-processing public function markProcessingAriUpdateData($ids) { if (!empty($ids)) { foreach ($ids as $id) { $objAriUpdates = new QcmcAriUpdates((int)$id); if (Validate::isLoadedObject($objAriUpdates)) { $objAriUpdates->status = self::ARI_SYNC_PROCESSING; $objAriUpdates->date_upd = date('Y-m-d H:i:s'); $objAriUpdates->update(); } } } } //Delete the rows which gets successfully processed public function deleteAriUpdateDataByIds($ids) { if (!empty($ids)) { foreach ($ids as $id) { $objAriUpdates = new QcmcAriUpdates((int)$id); if (Validate::isLoadedObject($objAriUpdates)) { $objAriUpdates->delete(); } } } } //If the response return false then mark all the rows to not processed which were marked as processing public function resetAriUpdateDataToPending($ids) { if (!empty($ids)) { foreach ($ids as $id) { $objAriUpdates = new QcmcAriUpdates((int)$id); if (Validate::isLoadedObject($objAriUpdates)) { $objAriUpdates->status = self::ARI_SYNC_NOT_PROCESSED; $objAriUpdates->date_upd = date('Y-m-d H:i:s'); $objAriUpdates->update(); } } } } }