-- liquibase formatted sql
-- changeset cyril:update_delivery_order

ALTER TABLE `delivery_order`
    DROP FOREIGN KEY `FK_DELIVERY_ORDER_ON_PURCHASEORDERID`;
ALTER TABLE `delivery_order`
    DROP COLUMN `targetDate`,
    DROP COLUMN `remarks`,
    DROP COLUMN `consoCode`,
    DROP COLUMN `purchaseOrderId`,
    ADD COLUMN `supplierId` INT NULL DEFAULT NULL AFTER `code`,
    ADD COLUMN `currencyId` INT NULL DEFAULT NULL AFTER `shopId`,
    ADD COLUMN `orderDate` DATETIME NULL DEFAULT NULL AFTER `currencyId`,
    ADD COLUMN `estimatedArrivalDate` DATETIME NULL DEFAULT NULL AFTER `orderDate`,
    ADD COLUMN `completeDate` DATETIME NULL AFTER `estimatedArrivalDate`,
    ADD COLUMN `m18DataLogId` INT NOT NULL AFTER `handlerId`,
    CHANGE COLUMN `code` `code` VARCHAR(100) NULL ,
    CHANGE COLUMN `status` `status` VARCHAR(10) NOT NULL DEFAULT 'pending' ,
    ADD INDEX `FK_DELIVERY_ORDER_ON_SUPPLIERID` (`supplierId` ASC) VISIBLE,
    ADD INDEX `FK_DELIVERY_ORDER_ON_CURRENCYID` (`currencyId` ASC) VISIBLE,
    ADD INDEX `FK_DELIVERY_ORDER_ON_M18DATALOGID` (`m18DataLogId` ASC) VISIBLE,
    DROP INDEX `FK_DELIVERY_ORDER_ON_PURCHASEORDERID` ;
;
ALTER TABLE `delivery_order`
    ADD CONSTRAINT `FK_DELIVERY_ORDER_ON_SUPPLIERID`
        FOREIGN KEY (`supplierId`)
            REFERENCES `shop` (`id`)
            ON DELETE RESTRICT
            ON UPDATE RESTRICT,
    ADD CONSTRAINT `FK_DELIVERY_ORDER_ON_CURRENCYID`
        FOREIGN KEY (`currencyId`)
            REFERENCES `currency` (`id`)
            ON DELETE RESTRICT
            ON UPDATE RESTRICT,
    ADD CONSTRAINT `FK_DELIVERY_ORDER_ON_M18DATALOGID`
        FOREIGN KEY (`m18DataLogId`)
            REFERENCES `m18_data_log` (`id`)
            ON DELETE RESTRICT
            ON UPDATE RESTRICT;

ALTER TABLE `delivery_order_line`
    DROP FOREIGN KEY `FK_DELIVERY_ORDER_LINE_ON_UOMID`,
    DROP FOREIGN KEY `FK_DELIVERY_ORDER_LINE_ON_PURCHASEORDERLINEID`;
ALTER TABLE `delivery_order_line`
    DROP COLUMN `purchaseOrderLineId`,
    ADD COLUMN `deliveryOrderId` INT NULL AFTER `deleted`,
    ADD COLUMN `itemNo` VARCHAR(100) NOT NULL AFTER `itemId`,
    ADD COLUMN `price` DECIMAL(14,2) NULL AFTER `uomId`,
    ADD COLUMN `m18DataLogId` INT NOT NULL AFTER `status`,
    CHANGE COLUMN `qty` `qty` DECIMAL(14,2) NULL ,
    CHANGE COLUMN `uomId` `uomId` INT NULL ,
    ADD INDEX `FK_DELIVERY_ORDER_LINE_ON_DELIVERYORDERID` (`deliveryOrderId` ASC) VISIBLE,
    ADD INDEX `FK_DELIVERY_ORDER_LINE_ON_M18DATALOGID` (`m18DataLogId` ASC) VISIBLE,
    DROP INDEX `FK_DELIVERY_ORDER_LINE_ON_PURCHASEORDERLINEID` ;
;
ALTER TABLE `delivery_order_line`
    ADD CONSTRAINT `FK_DELIVERY_ORDER_LINE_ON_UOMID`
        FOREIGN KEY (`uomId`)
            REFERENCES `uom_conversion` (`id`),
    ADD CONSTRAINT `FK_DELIVERY_ORDER_LINE_ON_DELIVERYORDERID`
        FOREIGN KEY (`deliveryOrderId`)
            REFERENCES `delivery_order` (`id`)
            ON DELETE RESTRICT
            ON UPDATE RESTRICT,
    ADD CONSTRAINT `FK_DELIVERY_ORDER_LINE_ON_M18DATALOGID`
        FOREIGN KEY (`m18DataLogId`)
            REFERENCES `m18_data_log` (`id`)
            ON DELETE RESTRICT
            ON UPDATE RESTRICT;
