Files
hotel/docker/renumber-rooms.php
DanielS f4eb1fbc9d Add docker/renumber-rooms.php: room numbers = CASPOS table numbers
Numbers all rooms consecutively from a start number (dry run by
default, --apply to write) and carries the new number into existing
bookings (htl_booking_detail, htl_cart_booking_data) and queued
casposbridge jobs. Used locally: demo rooms GR/DR/ER/LR-101..105 ->
tables 101-120 of the test POS's "Hotel" area.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 00:18:51 +01:00

56 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Renumber hotel rooms so that room numbers equal CASPOS table numbers.
* docker compose ... exec -u www-data app php docker/renumber-rooms.php 101 (dry run)
* docker compose ... exec -u www-data app php docker/renumber-rooms.php 101 --apply
* Rooms are numbered consecutively from START in the order room type, old number.
* Existing bookings keep their room but get the new number (room_num is copied into
* htl_booking_detail and used by casposbridge as table name).
*/
if (PHP_SAPI !== 'cli') {
exit;
}
define('_PS_ADMIN_DIR_', __DIR__);
require __DIR__.'/../config/config.inc.php';
$start = isset($argv[1]) ? (int)$argv[1] : 0;
$apply = in_array('--apply', $argv, true);
if ($start <= 0) {
fwrite(STDERR, "Usage: php docker/renumber-rooms.php <first table number> [--apply]\n");
exit(1);
}
$db = Db::getInstance();
$rooms = $db->executeS('SELECT ri.`id`, ri.`room_num`, ri.`id_product`, pl.`name`
FROM `'._DB_PREFIX_.'htl_room_information` ri
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON pl.`id_product` = ri.`id_product` AND pl.`id_lang` = '.(int)Configuration::get('PS_LANG_DEFAULT').'
ORDER BY ri.`id_hotel`, ri.`id_product`, ri.`room_num`, ri.`id`');
$tables = array();
foreach ($db->executeS('SHOW TABLES') as $t) {
$tables[] = reset($t);
}
$withRoomNum = array();
foreach (array('htl_booking_detail', 'htl_cart_booking_data') as $t) {
if (in_array(_DB_PREFIX_.$t, $tables, true) && $db->executeS('SHOW COLUMNS FROM `'._DB_PREFIX_.$t.'` LIKE \'room_num\'')) {
$withRoomNum[] = $t;
}
}
$n = $start;
foreach ($rooms as $r) {
$new = (string)$n++;
printf("%-28s %-8s -> %s\n", $r['name'], $r['room_num'], $new);
if ($apply && $r['room_num'] !== $new) {
$db->update('htl_room_information', array('room_num' => pSQL($new), 'date_upd' => date('Y-m-d H:i:s')), '`id` = '.(int)$r['id']);
foreach ($withRoomNum as $t) {
$db->update($t, array('room_num' => pSQL($new)), '`id_room` = '.(int)$r['id']);
}
if (in_array(_DB_PREFIX_.'casposbridge_job', $tables, true)) {
// queued (not yet posted) nights follow the new table name
$db->execute('UPDATE `'._DB_PREFIX_.'casposbridge_job` j JOIN `'._DB_PREFIX_.'htl_booking_detail` bd ON bd.`id` = j.`id_htl_booking`
SET j.`table_name` = \''.pSQL($new).'\' WHERE bd.`id_room` = '.(int)$r['id'].' AND j.`status` IN (\'pending\', \'failed\')');
}
}
}
echo $apply ? "Umbenannt.\n" : "Probelauf – mit --apply übernehmen.\n";