--liquibase formatted sql

--changeset derek:create inventory related
ALTER TABLE `stock_ledger`
DROP CONSTRAINT `fk_ledger_inventory_id`;

DROP TABLE `inventory_lot_no`;
DROP TABLE `inventory`;
CREATE TABLE `inventory`
(
    `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',
    `qty` DECIMAL(14, 2) NOT NULL,
    `price` VARCHAR(30) NOT NULL,
    `currencyId` INT(11) NOT NULL,
    `cpu` DECIMAL(14, 2) NOT NULL,
    `cpuUnit` VARCHAR(255) NOT NULL,
    `cpm` DECIMAL(14, 2) NOT NULL,
    `cpmUnit` VARCHAR(255) NOT NULL,
    `uomId` INT(11) NOT NULL,
    `status` VARCHAR(255) NOT NULL,
    CONSTRAINT pk_inventory PRIMARY KEY (id),
    CONSTRAINT FK_INVENTORY_TO_CURRENCY_ON_CURRENCY_ID FOREIGN KEY (`currencyId`) REFERENCES `currency` (`id`),
    CONSTRAINT FK_INVENTORY_TO_UOM_ON_UOM_ID FOREIGN KEY (`uomId`) REFERENCES `uom_conversion` (`id`)
);
CREATE TABLE `inventory_lot`
(
    `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',
    `itemId` INT(11) NOT NULL,
    `stockInLineId` INT(30) NOT NULL,
    `productionDate` DATETIME NULL,
    `stockInDate` DATETIME NULL,
    `expiryDate` DATE NOT NULL,
    `lotNo` VARCHAR(512) NOT NULL,
    CONSTRAINT pk_inventory_lot PRIMARY KEY (id),
    CONSTRAINT FK_INVENTORY_LOT_TO_ITEMS_ON_ITEM_ID FOREIGN KEY (`itemId`) REFERENCES `items` (`id`),
    CONSTRAINT FK_INVENTORY_LOT_TO_STOCK_IN_LINE_ON_STOCKINLINE_ID FOREIGN KEY (`stockInLineId`) REFERENCES `stock_in_line` (`id`)
);
CREATE TABLE `inventory_lot_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',
    `inQty` DECIMAL(14, 2) NOT NULL,
    `outQty` DECIMAL(14, 2) NOT NULL,
    `holdQty` DECIMAL(14, 2) NOT NULL,
    `status` VARCHAR(255) NOT NULL DEFAULT 'AVAILABLE',
    `remarks` VARCHAR(1024) NULL,
    `qrCode` JSON NOT NULL,
    CONSTRAINT pk_inventory_lot PRIMARY KEY (id)
);
ALTER TABLE `stock_ledger`
ADD CONSTRAINT `fk_ledger_inventory_id` FOREIGN KEY (inventoryId) REFERENCES `inventory` (`id`);