-- liquibase formatted sql
-- changeset cyril:stock take

CREATE TABLE `stock_take`
(
    `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',
    `code`        VARCHAR(50)  NOT NULL,
    `planStart`   DATETIME     NOT NULL,
    `planEnd`     DATETIME     NOT NULL,
    `actualStart` DATETIME     NULL,
    `actualEnd`   DATETIME     NULL,
    `status`      VARCHAR(20)  NOT NULL DEFAULT 'pending',
    `remarks`     VARCHAR(500) NULL,
    CONSTRAINT pk_stock_take PRIMARY KEY (id)
);

CREATE TABLE `stock_take_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',
    `stockTakeId`        INT    NOT NULL,
    `inventoryLotLineId` INT    NOT NULL,
    `initialQty`         DECIMAL(14, 2) NULL,
    `finalQty`           DECIMAL(14, 2) NULL,
    `uomId`              INT            NULL,
    `completeDate`       DATETIME       NULL,
    `status`             VARCHAR(20)    NOT NULL DEFAULT 'pending',
    `remarks`            VARCHAR(500)   NULL,
    CONSTRAINT pk_stock_take_line PRIMARY KEY (id),
    CONSTRAINT `FK_STOCK_TAKE_LINE_ON_STOCKTAKEID` FOREIGN KEY (`stockTakeId`) REFERENCES `stock_take` (`id`),
    CONSTRAINT `FK_STOCK_TAKE_LINE_ON_INVENTORYLOTLINEID` FOREIGN KEY (`inventoryLotLineId`) REFERENCES `inventory_lot_line` (`id`),
    CONSTRAINT `FK_STOCK_TAKE_LINE_ON_UOMID` FOREIGN KEY (`uomId`) REFERENCES `uom_conversion` (`id`)
);