--liquibase formatted sql

--changeset derek:create_stock_and_out
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',
    `lotNo` VARCHAR(255) NOT NULL,
    `itemId` INT NOT NULL,
    `stockInLineId` INT NOT NULL,
    `qty` DECIMAL(14,2) NOT NULL DEFAULT 0,
    `expiryDate` DATE NOT NULL,
    `uomId` INT(11) NOT NULL DEFAULT 0,
    `status` VARCHAR(50) NOT NULL DEFAULT 'stored',
    PRIMARY KEY (`id`),
    CONSTRAINT fk_inventory_item_id FOREIGN KEY (`itemId`) REFERENCES `items` (`id`)
);
CREATE TABLE `stock_out` (
    `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',
    `type` VARCHAR(50) NOT NULL,
    `deliveryOrderCode` VARCHAR(255) NULL,
    `pickOrderCode` VARCHAR(255) NULL,
    `consoCode` VARCHAR(255) NULL,
    `completeDate` DATETIME NULL,
    `status` VARCHAR(50) NOT NULL DEFAULT 'pending',
    `handlerId` INT(11) NOT NULL,
    `targetOutletId` INT(11) NULL,
    `remarks` VARCHAR(255) NULL,
    PRIMARY KEY (`id`)
);
CREATE TABLE `stock_out_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',
    `itemId` INT(11) NOT NULL,
    `qty` DECIMAL(14,2) NOT NULL,
    `inventoryId` INT(11) NULL,
    `status` VARCHAR(30) NOT NULL DEFAULT 'pending',
    `pickTime` DATETIME NULL,
    `pickerId` INT(11) NULL,
    PRIMARY KEY (`id`),
    CONSTRAINT fk_stock_out_line_items_id FOREIGN KEY (`itemId`) REFERENCES `items` (`id`)
);
CREATE TABLE `stock_ledger` (
    `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',
    `stockInLineId` INT(11) NULL,
    `stockOutLineId` INT(11) NULL,
    `inventoryId` INT(11) NOT NULL,
    `inQty` DECIMAL(14,2) NULL,
    `outQty` DECIMAL(14,2) NULL,
    `balance` DECIMAL(14,2) NULL,
    PRIMARY KEY (`id`),
    CONSTRAINT fk_ledger_stock_out_line_id FOREIGN KEY (`stockOutLineId`) REFERENCES `stock_out_line` (`id`),
    CONSTRAINT fk_ledger_inventory_id FOREIGN KEY (`inventoryId`) REFERENCES `inventory` (`id`)
);