-- liquibase formatted sql
-- changeset cyril:delivery order

CREATE TABLE `delivery_order`
(
    `id`              INT          NOT NULL AUTO_INCREMENT,
    `created`         DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `createdBy`       VARCHAR(30)  NULL     DEFAULT NULL,
    `version`         INT          NOT NULL DEFAULT '0',
    `modified`        DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `modifiedBy`      VARCHAR(30)  NULL     DEFAULT NULL,
    `deleted`         TINYINT(1)   NOT NULL DEFAULT '0',
    `purchaseOrderId` INT          NULL,
    `code`            varchar(100) NOT NULL,
    `consoCode`       varchar(100) NULL,
    `remarks`         varchar(500) NULL,
    `targetDate`      DATETIME     NOT NULL,
    `shopId`          INT          NULL,
    `status`          VARCHAR(30)  NOT NULL DEFAULT 'pending',
    `handlerId`       INT          NULL,
    CONSTRAINT pk_delivery_order PRIMARY KEY (id),
    CONSTRAINT `FK_DELIVERY_ORDER_ON_PURCHASEORDERID` FOREIGN KEY (`purchaseOrderId`) REFERENCES `purchase_order` (`id`),
    CONSTRAINT `FK_DELIVERY_ORDER_ON_SHOPID` FOREIGN KEY (`shopId`) REFERENCES `shop` (`id`),
    CONSTRAINT `FK_DELIVERY_ORDER_ON_HANDLERID` FOREIGN KEY (`handlerId`) REFERENCES `user` (`id`)
);

CREATE TABLE `delivery_order_line`
(
    `id`                  INT            NOT NULL AUTO_INCREMENT,
    `created`             DATETIME       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `createdBy`           VARCHAR(30)    NULL     DEFAULT NULL,
    `version`             INT            NOT NULL DEFAULT '0',
    `modified`            DATETIME       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `modifiedBy`          VARCHAR(30)    NULL     DEFAULT NULL,
    `deleted`             TINYINT(1)     NOT NULL DEFAULT '0',
    `purchaseOrderLineId` INT            NULL,
    `itemId`              INT            NOT NULL,
    `qty`                 DECIMAL(14, 2) NOT NULL,
    `uomId`               INT            NOT NULL,
    `status`              VARCHAR(30)    NOT NULL DEFAULT 'pending',
    CONSTRAINT pk_delivery_order_line PRIMARY KEY (id),
    CONSTRAINT `FK_DELIVERY_ORDER_LINE_ON_PURCHASEORDERLINEID` FOREIGN KEY (`purchaseOrderLineId`) REFERENCES `purchase_order_line` (`id`),
    CONSTRAINT `FK_DELIVERY_ORDER_LINE_ON_ITEMID` FOREIGN KEY (`itemId`) REFERENCES `items` (`id`),
    CONSTRAINT `FK_DELIVERY_ORDER_LINE_ON_UOMID` FOREIGN KEY (`uomId`) REFERENCES `uom_conversion` (`id`)
);