warehouseoffer v2

This commit is contained in:
Luca Haid
2025-07-15 12:57:41 +02:00
parent 5609e73923
commit 11e4d4b270
8 changed files with 1655 additions and 660 deletions
@@ -0,0 +1,117 @@
<?php /** @noinspection ALL */
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class WarehouseOfferVersioning extends AbstractMigration
{
public function up(): void
{
$this->execute("
CREATE TABLE IF NOT EXISTS `WarehouseOfferClosingText` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`text` text COLLATE utf8mb4_unicode_ci NOT NULL,
`createBy` int(11) NOT NULL,
`create` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
");
$this->execute("
CREATE TABLE IF NOT EXISTS `WarehouseOfferJournal` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`offerId` int(11) DEFAULT NULL,
`fileIds` text COLLATE utf8mb4_unicode_ci,
`message` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`create` int(11) DEFAULT NULL,
`createBy` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `offerId` (`offerId`),
KEY `createBy` (`createBy`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
");
$this->execute("
ALTER TABLE `WarehouseOffer`
ADD COLUMN IF NOT EXISTS `contactPersonEmail` VARCHAR(255) NULL DEFAULT NULL AFTER `contactPerson`,
ADD COLUMN IF NOT EXISTS `lastSentDate` INT(11) NULL DEFAULT NULL AFTER `status`,
ADD COLUMN IF NOT EXISTS `version` INT(11) NOT NULL DEFAULT 1 AFTER `id`,
ADD COLUMN IF NOT EXISTS `history_id` INT(11) NULL DEFAULT NULL AFTER `version`;
");
$this->execute("
ALTER TABLE `WarehouseHistory`
ADD COLUMN IF NOT EXISTS `data` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL AFTER `note`;
");
$this->execute("DELETE FROM `WarehouseHistory` WHERE `table` = 'WarehouseOffer';");
$this->execute("
INSERT INTO `WarehouseHistory` (`table`, `row_id`, `key`, `old_value`, `new_value`, `note`, `data`, `user_id`, `create`)
SELECT
'WarehouseOffer' AS `table`,
wo.id AS `row_id`,
'version' AS `key`,
0 AS `old_value`,
1 AS `new_value`,
'Baseline Version 1 erstellt durch Migration.' AS `note`,
JSON_OBJECT(
'id', wo.id,
'version', wo.version,
'history_id', wo.history_id,
'offerNumber', wo.offerNumber,
'reference', wo.reference,
'customerNumber', wo.customerNumber,
'customerName', wo.customerName,
'contactPerson', wo.contactPerson,
'contactPersonEmail', wo.contactPersonEmail,
'customerStreet', wo.customerStreet,
'customerCity', wo.customerCity,
'customerZip', wo.customerZip,
'customerVAT', wo.customerVAT,
'editor', wo.editor,
'purpose', wo.purpose,
'positions', wo.positions,
'alternativePositions', wo.alternativePositions,
'totalDiscount', wo.totalDiscount,
'paymentTerms', wo.paymentTerms,
'deliveryTerms', wo.deliveryTerms,
'closingText', wo.closingText,
'notes', wo.notes,
'status', wo.status,
'lastSentDate', wo.lastSentDate,
'totalAmount', wo.totalAmount,
'create', wo.create,
'createBy', wo.createBy
) AS `data`,
wo.createBy AS `user_id`,
UNIX_TIMESTAMP() AS `create`
FROM `WarehouseOffer` wo;
");
$this->execute("
UPDATE `WarehouseOffer` wo
JOIN `WarehouseHistory` wh ON wo.id = wh.row_id
SET wo.history_id = wh.id
WHERE wh.`table` = 'WarehouseOffer' AND wh.new_value = 1 AND wh.note = 'Baseline Version 1 erstellt durch Migration.';
");
}
public function down(): void
{
$this->execute("DELETE FROM `WarehouseHistory` WHERE `note` = 'Baseline Version 1 erstellt durch Migration.' AND `table` = 'WarehouseOffer';");
$warehouseOfferTable = $this->table('WarehouseOffer');
if ($warehouseOfferTable->hasColumn('history_id')) $warehouseOfferTable->removeColumn('history_id')->save();
if ($warehouseOfferTable->hasColumn('version')) $warehouseOfferTable->removeColumn('version')->save();
if ($warehouseOfferTable->hasColumn('lastSentDate')) $warehouseOfferTable->removeColumn('lastSentDate')->save();
if ($warehouseOfferTable->hasColumn('contactPersonEmail')) $warehouseOfferTable->removeColumn('contactPersonEmail')->save();
$warehouseHistoryTable = $this->table('WarehouseHistory');
if ($warehouseHistoryTable->hasColumn('data')) $warehouseHistoryTable->removeColumn('data')->save();
if ($this->hasTable('WarehouseOfferJournal')) $this->table('WarehouseOfferJournal')->drop()->save();
if ($this->hasTable('WarehouseOfferClosingText')) $this->table('WarehouseOfferClosingText')->drop()->save();
}
}