cmvr-wms/sql/projectdev/006_project_loans.sql

32 lines
2.3 KiB
MySQL
Raw Permalink Normal View History

-- target-database: cmvr-project-dev
CREATE TABLE IF NOT EXISTS inventory_cost_link (
source_event_id bigint NOT NULL, target_event_id bigint NOT NULL PRIMARY KEY,
CONSTRAINT fk_cost_link_source FOREIGN KEY(source_event_id) REFERENCES inventory_cost_event(id),
CONSTRAINT fk_cost_link_target FOREIGN KEY(target_event_id) REFERENCES inventory_cost_event(id),
CONSTRAINT ck_cost_link_order CHECK(source_event_id<target_event_id)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS project_loan (
id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, loan_no varchar(64) NOT NULL UNIQUE,
lender_project_id bigint NOT NULL, borrower_project_id bigint NOT NULL, item_id bigint NOT NULL,
quantity decimal(20,4) NOT NULL, out_event_id bigint NOT NULL, in_event_id bigint NOT NULL,
active tinyint NOT NULL DEFAULT 1, remark varchar(500) NULL, create_by varchar(64) NOT NULL, create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_loan_lender FOREIGN KEY(lender_project_id) REFERENCES project_info(id),
CONSTRAINT fk_loan_borrower FOREIGN KEY(borrower_project_id) REFERENCES project_info(id),
CONSTRAINT fk_loan_item FOREIGN KEY(item_id) REFERENCES product_info(id),
CONSTRAINT fk_loan_out FOREIGN KEY(out_event_id) REFERENCES inventory_cost_event(id),
CONSTRAINT fk_loan_in FOREIGN KEY(in_event_id) REFERENCES inventory_cost_event(id),
CONSTRAINT ck_loan_projects CHECK(lender_project_id<>borrower_project_id), CONSTRAINT ck_loan_qty CHECK(quantity>0)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS project_loan_return (
id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, loan_id bigint NOT NULL, quantity decimal(20,4) NOT NULL,
business_key varchar(160) NOT NULL UNIQUE, return_type varchar(20) NOT NULL,
out_event_id bigint NULL, in_event_id bigint NOT NULL UNIQUE, active tinyint NOT NULL DEFAULT 1,
create_by varchar(64) NOT NULL, create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_loan_return_loan FOREIGN KEY(loan_id) REFERENCES project_loan(id),
CONSTRAINT fk_loan_return_out FOREIGN KEY(out_event_id) REFERENCES inventory_cost_event(id),
CONSTRAINT fk_loan_return_in FOREIGN KEY(in_event_id) REFERENCES inventory_cost_event(id),
CONSTRAINT ck_loan_return_qty CHECK(quantity>0)
) ENGINE=InnoDB;
ALTER TABLE purchase_request ADD COLUMN purchase_purpose varchar(10) NOT NULL DEFAULT 'SELF';
ALTER TABLE purchase_request ADD COLUMN loan_id bigint NULL;