CREATE TABLE B_SALE_AUXILIARY
(
	ID NUMBER(18) NOT NULL,
	TIMESTAMP_X DATE DEFAULT SYSDATE NOT NULL,
	ITEM VARCHAR2(255 CHAR) NOT NULL,
	ITEM_MD5 VARCHAR2(32 CHAR) NOT NULL,
	USER_ID NUMBER(18) NOT NULL,
	DATE_INSERT DATE NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE UNIQUE INDEX IX_STT_USER_ITEM ON B_SALE_AUXILIARY(USER_ID, ITEM_MD5)
/
CREATE SEQUENCE SQ_SALE_AUXILIARY INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_AUXILIARY_INSERT
BEFORE INSERT
ON B_SALE_AUXILIARY
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_AUXILIARY.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LANG
(
	LID CHAR(2 CHAR) NOT NULL,
	CURRENCY CHAR(3 CHAR) NOT NULL,
	PRIMARY KEY (LID)
)
/

CREATE TABLE B_SALE_FUSER
(
	ID NUMBER(18) NOT NULL,
	DATE_INSERT DATE NOT NULL,
	DATE_UPDATE DATE NOT NULL,
	USER_ID NUMBER(18) NULL,
	CODE VARCHAR2(32 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_SALE_FUSER INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX IX_FUSER_ID ON B_SALE_FUSER(USER_ID)
/
CREATE OR REPLACE TRIGGER B_SALE_FUSER_INSERT
BEFORE INSERT
ON B_SALE_FUSER
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_FUSER.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/
CREATE INDEX IX_FUSER_ID_CODE ON B_SALE_FUSER(CODE)
/

CREATE TABLE B_SALE_BASKET
(
	ID NUMBER(18) NOT NULL,
	FUSER_ID NUMBER(18) NOT NULL,
	ORDER_ID NUMBER(18) NULL,
	PRODUCT_ID NUMBER(18) NOT NULL,
	PRODUCT_PRICE_ID NUMBER(18) NULL,
	PRICE NUMBER(20, 4) NOT NULL,
	CURRENCY CHAR(3 CHAR) NOT NULL,
	BASE_PRICE NUMBER(18, 4) null,
	VAT_INCLUDED char(1 CHAR) DEFAULT 'Y' NOT NULL,
	DATE_INSERT DATE NOT NULL,
	DATE_UPDATE DATE NOT NULL,
	WEIGHT NUMBER(22, 2) NULL,
	QUANTITY NUMBER(24, 4) DEFAULT '0' NOT NULL,
	LID CHAR(2 CHAR) NOT NULL,
	DELAY CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	CAN_BUY CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	MODULE VARCHAR2(100 CHAR) NULL,
	CALLBACK_FUNC VARCHAR2(100 CHAR) NULL,
	NOTES VARCHAR2(250 CHAR) NULL,
	ORDER_CALLBACK_FUNC VARCHAR2(100 CHAR) NULL,
	DETAIL_PAGE_URL VARCHAR2(250 CHAR) NULL,
	DISCOUNT_PRICE NUMBER(20,4) NOT NULL,
	CANCEL_CALLBACK_FUNC VARCHAR2(100 CHAR) NULL,
	PAY_CALLBACK_FUNC VARCHAR2(100 CHAR) NULL,
	PRODUCT_PROVIDER_CLASS VARCHAR2(100 CHAR) NULL,
	CATALOG_XML_ID VARCHAR2(100 CHAR) null,
	PRODUCT_XML_ID VARCHAR2(100 CHAR) null,
	DISCOUNT_NAME VARCHAR2(255 CHAR) null,
	DISCOUNT_VALUE CHAR(32 CHAR) null,
	DISCOUNT_COUPON CHAR(32 CHAR) null,
	VAT_RATE NUMBER(18, 2) DEFAULT '0.00' NULL,
	SUBSCRIBE CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	RESERVED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DEDUCTED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	BARCODE_MULTI CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	RESERVE_QUANTITY NUMBER(18,2) NULL,
	CUSTOM_PRICE CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DIMENSIONS VARCHAR2(255 CHAR) NULL,
	TYPE NUMBER(18) NULL,
	SET_PARENT_ID NUMBER(18) NULL,
	MEASURE_CODE NUMBER(18) NULL,
	MEASURE_NAME VARCHAR2(50 CHAR) null,
	RECOMMENDATION VARCHAR2(40 CHAR) null,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_SBAS_XML_ID ON B_SALE_BASKET(PRODUCT_XML_ID, CATALOG_XML_ID)
/
CREATE INDEX IXS_BASKET_LID ON B_SALE_BASKET(LID)
/
CREATE INDEX IXS_BASKET_USER_ID ON B_SALE_BASKET(FUSER_ID)
/
CREATE INDEX IXS_BASKET_ORDER_ID ON B_SALE_BASKET(ORDER_ID)
/
CREATE INDEX IXS_BASKET_PRODUCT_ID ON B_SALE_BASKET(PRODUCT_ID)
/
CREATE INDEX IXS_BASKET_DATE_INSERT ON B_SALE_BASKET(DATE_INSERT)
/
CREATE INDEX IXS_BASKET_PRODUCT_PRICE_ID ON B_SALE_BASKET(PRODUCT_PRICE_ID)
/
CREATE SEQUENCE SQ_B_SALE_BASKET INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_BASKET_INSERT
BEFORE INSERT
ON B_SALE_BASKET
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_B_SALE_BASKET.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_BASKET_PROPS
(
	ID NUMBER(18) NOT NULL,
	BASKET_ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	VALUE VARCHAR2(255 CHAR) NULL,
	CODE VARCHAR2(255 CHAR) NULL,
	SORT NUMBER(18) DEFAULT '100' NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_BASKET_PROPS_BASKET ON B_SALE_BASKET_PROPS(BASKET_ID)
/
CREATE INDEX IXS_BASKET_PROPS_CODE ON B_SALE_BASKET_PROPS(CODE)
/
CREATE SEQUENCE SQ_B_SALE_BASKET_PROPS INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_BASKET_PROPS_INSERT
BEFORE INSERT
ON B_SALE_BASKET_PROPS
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_B_SALE_BASKET_PROPS.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_ORDER
(
	ID NUMBER(18) NOT NULL,
	LID CHAR(2 CHAR) NOT NULL,
	PERSON_TYPE_ID NUMBER(18) NOT NULL,
	PAYED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DATE_PAYED DATE NULL,
	EMP_PAYED_ID NUMBER(18) NULL,
	CANCELED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DATE_CANCELED DATE NULL,
	EMP_CANCELED_ID NUMBER(18) NULL,
	REASON_CANCELED VARCHAR2(255 CHAR) NULL,
	STATUS_ID CHAR(2 CHAR) NOT NULL,
	DATE_STATUS DATE NOT NULL,
	EMP_STATUS_ID NUMBER(18) NULL,
	PRICE_DELIVERY NUMBER(20,4) DEFAULT 0.0 NOT NULL,
	ALLOW_DELIVERY CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DATE_ALLOW_DELIVERY DATE NULL,
	EMP_ALLOW_DELIVERY_ID NUMBER(18) NULL,
	DEDUCTED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DATE_DEDUCTED DATE NULL,
	EMP_DEDUCTED_ID NUMBER(18) NULL,
	REASON_UNDO_DEDUCTED VARCHAR2(255 CHAR) NULL,
	MARKED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DATE_MARKED DATE NULL,
	EMP_MARKED_ID NUMBER(18) NULL,
	REASON_MARKED VARCHAR2(255 CHAR) NULL,
	RESERVED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	PRICE NUMBER(20, 4) NOT NULL,
	CURRENCY CHAR(3 CHAR) NOT NULL,
	DISCOUNT_VALUE NUMBER(20,4) DEFAULT 0.0 NOT NULL,
	USER_ID NUMBER(18) NOT NULL,
	PAY_SYSTEM_ID NUMBER(18) NULL,
	DELIVERY_ID VARCHAR2(50 CHAR) NULL,
	DATE_INSERT DATE NOT NULL,
	DATE_UPDATE DATE NOT NULL,
	USER_DESCRIPTION VARCHAR2(2000 CHAR) NULL,
	ADDITIONAL_INFO VARCHAR2(255 CHAR) NULL,
	PS_STATUS CHAR(1 CHAR) NULL,
	PS_STATUS_CODE CHAR(5 CHAR) NULL,
	PS_STATUS_DESCRIPTION VARCHAR2(250 CHAR) NULL,
	PS_STATUS_MESSAGE VARCHAR2(250 CHAR) NULL,
	PS_SUM NUMBER(18,2) NULL,
	PS_CURRENCY CHAR(3 CHAR) NULL,
	PS_RESPONSE_DATE DATE NULL,
	COMMENTS CLOB NULL,
	TAX_VALUE NUMBER(18,2) DEFAULT 0 NOT NULL,
	STAT_GID VARCHAR2(255 CHAR) NULL,
	SUM_PAID NUMBER(18,2) DEFAULT 0 NOT NULL,
	RECURRING_ID NUMBER(18) NULL,
	PAY_VOUCHER_NUM VARCHAR2(20 CHAR) NULL,
	PAY_VOUCHER_DATE DATE NULL,
	LOCKED_BY NUMBER(18) NULL,
	DATE_LOCK DATE NULL,
	RECOUNT_FLAG CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	AFFILIATE_ID INT NULL,
	DELIVERY_DOC_NUM VARCHAR2(20 CHAR) NULL,
	DELIVERY_DOC_DATE DATE NULL,
	UPDATED_1C char(1 char) default 'N' not null,
	STORE_ID INT NULL,
	ORDER_TOPIC VARCHAR2(255 CHAR) NULL,
	CREATED_BY NUMBER(18) NULL,
	RESPONSIBLE_ID NUMBER(18) NULL,
	DATE_PAY_BEFORE DATE NULL,
	DATE_BILL DATE NULL,
	ACCOUNT_NUMBER VARCHAR2(100 CHAR) NULL,
	TRACKING_NUMBER VARCHAR2(255 CHAR) NULL,
	XML_ID VARCHAR2(255 CHAR) NULL,
	ID_1C VARCHAR2(15 CHAR) NULL,
	VERSION_1C VARCHAR2(15 CHAR) NULL,
	VERSION NUMBER(18) default 0 not null,
	EXTERNAL_ORDER char(1 char) default 'N' not null,
	BX_USER_ID varchar2(32 char) null,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_SOO_AFFILIATE_ID ON b_sale_order(AFFILIATE_ID)
/
CREATE INDEX IXS_ORDER_USER_ID ON B_SALE_ORDER(USER_ID)
/
CREATE INDEX IXS_ORDER_PERSON_TYPE_ID ON B_SALE_ORDER(PERSON_TYPE_ID)
/
CREATE INDEX IXS_ORDER_PAYED ON B_SALE_ORDER(PAYED)
/
CREATE INDEX IXS_ORDER_STATUS_ID ON B_SALE_ORDER(STATUS_ID)
/
CREATE SEQUENCE SQ_SALE_ORDER INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX IXS_ORDER_REC_ID ON B_SALE_ORDER(RECURRING_ID)
/
CREATE INDEX IXS_ORDER_UPDATED_1C ON B_SALE_ORDER(UPDATED_1C)
/
CREATE INDEX IXS_SALE_COUNT ON B_SALE_ORDER(USER_ID,LID,PAYED,CANCELED)
/
CREATE INDEX IXS_DATE_UPDATE ON B_SALE_ORDER(DATE_UPDATE)
/
CREATE INDEX IXS_XML_ID ON B_SALE_ORDER(XML_ID)
/
CREATE INDEX IXS_ID_1C ON B_SALE_ORDER (ID_1C)
/
CREATE UNIQUE INDEX IXS_ACCOUNT_NUMBER ON B_SALE_ORDER(ACCOUNT_NUMBER)
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_INSERT
BEFORE INSERT
ON B_SALE_ORDER
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_ORDER.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_PERSON_TYPE
(
	ID NUMBER(18) NOT NULL,
	LID CHAR(2 CHAR) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	SORT NUMBER(18) DEFAULT '150' NOT NULL,
	ACTIVE CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_PERSON_TYPE_LID ON B_SALE_PERSON_TYPE(LID)
/
CREATE SEQUENCE SQ_SALE_PERSON_TYPE START WITH 4 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_PERSON_TYPE_INSERT
BEFORE INSERT
ON B_SALE_PERSON_TYPE
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_PERSON_TYPE.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_ORDER_PROPS_GROUP
(
	ID NUMBER(18) NOT NULL,
	PERSON_TYPE_ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	SORT NUMBER(18) DEFAULT '100' NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_ORDER_PROPS_GROUP_PT_ID ON B_SALE_ORDER_PROPS_GROUP(PERSON_TYPE_ID)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_PROPS_GROUP START WITH 5 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_OPG_INSERT
BEFORE INSERT
ON B_SALE_ORDER_PROPS_GROUP
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_B_SALE_ORDER_PROPS_GROUP.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_ORDER_PROPS
(
	ID NUMBER(18) NOT NULL,
	PERSON_TYPE_ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	TYPE VARCHAR2(20 CHAR) NOT NULL,
	REQUIRED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DEFAULT_VALUE VARCHAR2(500 CHAR) NULL,
	SORT NUMBER(18) DEFAULT '100' NOT NULL,
	USER_PROPS CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	IS_LOCATION CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	PROPS_GROUP_ID NUMBER(18) NOT NULL,
	DESCRIPTION VARCHAR2(255 CHAR) NULL,
	IS_EMAIL CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	IS_PROFILE_NAME CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	IS_PAYER CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	IS_LOCATION4TAX CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	IS_FILTERED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	CODE VARCHAR2(50 CHAR) NULL,
	IS_ZIP CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	IS_PHONE CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	ACTIVE CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	UTIL CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	INPUT_FIELD_LOCATION NUMBER(18) DEFAULT '0' NOT NULL,
	MULTIPLE CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	IS_ADDRESS CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	SETTINGS VARCHAR2(500 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_ORDER_PROPS_PERSON_TYPE_ID ON B_SALE_ORDER_PROPS(PERSON_TYPE_ID)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_PROPS START WITH 16 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_PROPS_INSERT
BEFORE INSERT
ON B_SALE_ORDER_PROPS
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_ORDER_PROPS.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_ORDER_PROPS_VALUE
(
	ID NUMBER(18) NOT NULL,
	ORDER_ID NUMBER(18) NOT NULL,
	ORDER_PROPS_ID NUMBER(18) NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	VALUE VARCHAR2(500 CHAR) NULL,
	CODE VARCHAR2(50 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE UNIQUE INDEX IX_SOPV_ORD_PROP_UNI ON B_SALE_ORDER_PROPS_VALUE(ORDER_ID, ORDER_PROPS_ID)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_PROPS_VALUE INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_OPV_INSERT
BEFORE INSERT
ON B_SALE_ORDER_PROPS_VALUE
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_B_SALE_ORDER_PROPS_VALUE.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_ORDER_PROPS_VARIANT
(
	ID NUMBER(18) NOT NULL,
	ORDER_PROPS_ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	VALUE VARCHAR2(255 CHAR) NULL,
	SORT NUMBER(18) DEFAULT '100' NOT NULL,
	DESCRIPTION VARCHAR2(255 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_OPV_ORDER_PROPS_ID ON B_SALE_ORDER_PROPS_VARIANT(ORDER_PROPS_ID)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_PROPS_VARIANT START WITH 3 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_OPVAR_INSERT
BEFORE INSERT
ON B_SALE_ORDER_PROPS_VARIANT
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_B_SALE_ORDER_PROPS_VARIANT.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_ORDER_PROPS_RELATION
(
	PROPERTY_ID NUMBER(18) NOT NULL,
	ENTITY_ID VARCHAR2(35 CHAR) NOT NULL,
	ENTITY_TYPE CHAR(1 CHAR) NOT NULL,
	PRIMARY KEY (PROPERTY_ID, ENTITY_ID, ENTITY_TYPE)
)
/
CREATE INDEX IX_PROPERTY ON B_SALE_ORDER_PROPS_RELATION (PROPERTY_ID)
/
CREATE INDEX IX_ENTITY_ID ON B_SALE_ORDER_PROPS_RELATION (ENTITY_ID)
/

CREATE TABLE B_SALE_PAY_SYSTEM
(
	ID NUMBER(18) NOT NULL,
	LID CHAR(2 CHAR) NULL,
	CURRENCY CHAR(3 CHAR) NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	ACTIVE CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	SORT NUMBER(18) DEFAULT '100' NOT NULL,
	DESCRIPTION VARCHAR2(2000 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_PAY_SYSTEM_LID ON B_SALE_PAY_SYSTEM(LID)
/
CREATE SEQUENCE SQ_SALE_PAY_SYSTEM START WITH 12 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_PAY_SYSTEM_INSERT
BEFORE INSERT
ON B_SALE_PAY_SYSTEM
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_PAY_SYSTEM.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_PAY_SYSTEM_ACTION
(
	ID NUMBER(18) NOT NULL,
	PAY_SYSTEM_ID NUMBER(18) NOT NULL,
	PERSON_TYPE_ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	ACTION_FILE VARCHAR2(255 CHAR) NULL,
	RESULT_FILE VARCHAR2(255 CHAR) NULL,
	NEW_WINDOW CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	PARAMS CLOB NULL,
	TARIF CLOB NULL,
	HAVE_PAYMENT CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	HAVE_ACTION CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	HAVE_RESULT CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	HAVE_PREPAY CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	HAVE_RESULT_RECEIVE CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	ENCODING VARCHAR2(45 CHAR) NULL,
	LOGOTIP NUMBER(18) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_PSA_PERSON_TYPE_ID ON B_SALE_PAY_SYSTEM_ACTION(PERSON_TYPE_ID)
/
CREATE SEQUENCE SQ_SALE_PAY_SYSTEM_ACTION START WITH 12 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE UNIQUE INDEX IX_SPSA_PSPT_UNI ON B_SALE_PAY_SYSTEM_ACTION(PAY_SYSTEM_ID, PERSON_TYPE_ID)
/

CREATE OR REPLACE TRIGGER B_SALE_PS_ACTION_INSERT
BEFORE INSERT
ON B_SALE_PAY_SYSTEM_ACTION
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_PAY_SYSTEM_ACTION.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_DELIVERY
(
	ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	LID CHAR(2 CHAR) NOT NULL,
	PERIOD_FROM NUMBER(18) NULL,
	PERIOD_TO NUMBER(18) NULL,
	PERIOD_TYPE CHAR(1 CHAR) NULL,
	WEIGHT_FROM NUMBER(18) NULL,
	WEIGHT_TO NUMBER(18) NULL,
	ORDER_PRICE_FROM NUMBER(18, 2) NULL,
	ORDER_PRICE_TO NUMBER(18, 2) NULL,
	ORDER_CURRENCY CHAR(3 CHAR) NULL,
	ACTIVE CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	PRICE NUMBER(18, 2) NOT NULL,
	CURRENCY CHAR(3 CHAR) NOT NULL,
	SORT NUMBER(18) DEFAULT '100' NOT NULL,
	DESCRIPTION VARCHAR2(250 CHAR) NULL,
	LOGOTIP NUMBER(11) NULL,
	STORE VARCHAR2(250 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_DELIVERY_LID ON B_SALE_DELIVERY(LID)
/
CREATE SEQUENCE SQ_SALE_DELIVERY START WITH 4 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_DELIVERY_INSERT
BEFORE INSERT
ON B_SALE_DELIVERY
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_DELIVERY.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOCATION_COUNTRY
(
	ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(100 CHAR) NOT NULL,
	SHORT_NAME VARCHAR2(100 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_SALE_LOCATION_COUNTRY START WITH 2 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX IXS_NAME ON B_SALE_LOCATION_COUNTRY(NAME)
/
CREATE OR REPLACE TRIGGER B_SALE_LOCATION_COUNTRY_INSERT
BEFORE INSERT
ON B_SALE_LOCATION_COUNTRY
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_LOCATION_COUNTRY.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOCATION_COUNTRY_LANG
(
	ID NUMBER(18) NOT NULL,
	COUNTRY_ID NUMBER(18) NOT NULL,
	LID CHAR(2 CHAR) NOT NULL,
	NAME VARCHAR2(100 CHAR) NOT NULL,
	SHORT_NAME VARCHAR2(100 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_LOCAT_CNTR_LID ON B_SALE_LOCATION_COUNTRY_LANG(COUNTRY_ID, LID)
/
CREATE SEQUENCE SQ_SALE_LOCATION_COUNTRY_LANG START WITH 3 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_LC_LANG_INSERT
BEFORE INSERT
ON B_SALE_LOCATION_COUNTRY_LANG
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_LOCATION_COUNTRY_LANG.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOCATION_REGION
(
	ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(100 CHAR) NOT NULL,
	SHORT_NAME VARCHAR2(100 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_SALE_LOCATION_REGION INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_LOCATION_REGION_INSERT
BEFORE INSERT
ON B_SALE_LOCATION_REGION
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_LOCATION_REGION.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOCATION_REGION_LANG
(
	ID NUMBER(18) NOT NULL,
	REGION_ID NUMBER(18) NOT NULL,
	LID CHAR(2 CHAR) NOT NULL,
	NAME VARCHAR2(100 CHAR) NOT NULL,
	SHORT_NAME VARCHAR2(100 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_LOCAT_REGION_LID ON B_SALE_LOCATION_REGION_LANG(REGION_ID, LID)
/
CREATE INDEX IXS_NAME_1 ON B_SALE_LOCATION_REGION_LANG(NAME)
/
CREATE SEQUENCE SQ_SALE_LOCATION_REGION_LANG INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_L_REGION_L_INSERT
BEFORE INSERT
ON B_SALE_LOCATION_REGION_LANG
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_LOCATION_REGION_LANG.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOCATION_CITY
(
	ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(100 CHAR) NOT NULL,
	SHORT_NAME VARCHAR2(100 CHAR) NULL,
	REGION_ID NUMBER(18) NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_SALE_LOCATION_CITY START WITH 4 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX IXS_LOCAT_CITY_REGION_ID ON B_SALE_LOCATION_CITY(REGION_ID)
/
CREATE OR REPLACE TRIGGER B_SALE_LOCATION_CITY_INSERT
BEFORE INSERT
ON B_SALE_LOCATION_CITY
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_LOCATION_CITY.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOCATION_CITY_LANG
(
	ID NUMBER(18) NOT NULL,
	CITY_ID NUMBER(18) NOT NULL,
	LID CHAR(2 CHAR) NOT NULL,
	NAME VARCHAR2(100 CHAR) NOT NULL,
	SHORT_NAME VARCHAR2(100 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_LOCAT_CITY_LID ON B_SALE_LOCATION_CITY_LANG(CITY_ID, LID)
/
CREATE INDEX IXS_NAME_CITY ON B_SALE_LOCATION_CITY_LANG(NAME)
/
CREATE SEQUENCE SQ_SALE_LOCATION_CITY_LANG START WITH 7 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_L_CITY_L_INSERT
BEFORE INSERT
ON B_SALE_LOCATION_CITY_LANG
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_LOCATION_CITY_LANG.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/
CREATE TABLE B_SALE_LOCATION_ZIP (
	ID NUMBER(18) NOT NULL,
	LOCATION_ID NUMBER(18) NOT NULL,
	ZIP CHAR(10 CHAR) NOT NULL
)
/
CREATE SEQUENCE SQ_B_SALE_LOCATION_ZIP START WITH 1 INCREMENT BY 1 NOMINVALUE NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX IX_LOCATION_ID ON B_SALE_LOCATION_ZIP(LOCATION_ID)
/
CREATE INDEX IX_ZIP ON B_SALE_LOCATION_ZIP(ZIP)
/

CREATE TABLE B_SALE_LOCATION
(
	ID NUMBER(18) NOT NULL,
	SORT NUMBER(18) DEFAULT '100' NOT NULL,
	CODE VARCHAR2(100 CHAR) NOT NULL,
	LEFT_MARGIN NUMBER(18),
	RIGHT_MARGIN NUMBER(18),
	PARENT_ID NUMBER(18) DEFAULT '0',
	DEPTH_LEVEL NUMBER(18) DEFAULT '1',
	TYPE_ID NUMBER(18),
	LATITUDE NUMBER(8,6),
	LONGITUDE NUMBER(9,6),

	COUNTRY_ID NUMBER(18),
	REGION_ID NUMBER(18),
	CITY_ID NUMBER(18),
	LOC_DEFAULT CHAR(1 CHAR) DEFAULT 'N' NOT NULL,

	PRIMARY KEY (ID)
)
/
CREATE unique INDEX IX_B_SALE_LOC_CODE ON B_SALE_LOCATION(CODE)
/
CREATE INDEX IX_B_SALE_LOC_MARGINS ON B_SALE_LOCATION(LEFT_MARGIN, RIGHT_MARGIN)
/
CREATE INDEX IX_B_SALE_LOC_MARGINS_REV ON B_SALE_LOCATION(RIGHT_MARGIN, LEFT_MARGIN)
/
CREATE INDEX IX_B_SALE_LOC_PARENT ON B_SALE_LOCATION(PARENT_ID)
/
CREATE INDEX IX_B_SALE_LOC_DL ON B_SALE_LOCATION(DEPTH_LEVEL)
/
CREATE INDEX IX_B_SALE_LOC_TYPE ON B_SALE_LOCATION(TYPE_ID)
/
CREATE SEQUENCE SQ_B_SALE_LOCATION START WITH 5 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_LOCATION_INSERT
BEFORE INSERT
ON B_SALE_LOCATION
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_LOCATION.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE INDEX IXS_LOCATION_COUNTRY_ID ON B_SALE_LOCATION(COUNTRY_ID)
/
CREATE INDEX IXS_LOCATION_CITY_ID ON B_SALE_LOCATION(CITY_ID)
/
CREATE INDEX IXS_LOCATION_REGION_ID ON B_SALE_LOCATION(REGION_ID)
/

CREATE TABLE B_SALE_LOC_NAME
(
	ID NUMBER(18) NOT NULL,
	LANGUAGE_ID CHAR(2 CHAR) NOT NULL,
	LOCATION_ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(100 CHAR) NOT NULL,
	NAME_UPPER VARCHAR2(100 CHAR) NOT NULL,
	SHORT_NAME VARCHAR2(100 CHAR),

	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_B_SALE_LOC_NAME_NAME_U ON B_SALE_LOC_NAME(NAME_UPPER)
/
CREATE INDEX IX_B_SALE_LOC_NAME_LI_LI ON B_SALE_LOC_NAME(LOCATION_ID, LANGUAGE_ID)
/
CREATE SEQUENCE SQ_B_SALE_LOC_NAME INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_LOC_NAME_INSERT
BEFORE INSERT
ON B_SALE_LOC_NAME
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_LOC_NAME.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOC_EXT_SRV
(
	ID NUMBER(18) NOT NULL,
	CODE VARCHAR2(100 CHAR) NOT NULL,

	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_LOC_EXT_SRV INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_LOC_EXT_SRV_INSERT
BEFORE INSERT
ON B_SALE_LOC_EXT_SRV
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_LOC_EXT_SRV.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOC_EXT
(
	ID NUMBER(18) NOT NULL,
	SERVICE_ID NUMBER(18) NOT NULL,
	LOCATION_ID NUMBER(18) NOT NULL,
	XML_ID VARCHAR2(100 CHAR) NOT NULL,

	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_B_SALE_LOC_EXT_LID_SID ON B_SALE_LOC_EXT(LOCATION_ID, SERVICE_ID)
/
CREATE SEQUENCE SQ_B_SALE_LOC_EXT INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_LOC_EXT_INSERT
BEFORE INSERT
ON B_SALE_LOC_EXT
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_LOC_EXT.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOC_TYPE
(
	ID NUMBER(18) NOT NULL,
	CODE VARCHAR2(30 CHAR) NOT NULL,
	SORT NUMBER(18) DEFAULT '100',
	DISPLAY_SORT NUMBER(18) DEFAULT '100',

	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_LOC_TYPE INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_LOC_TYPE_INSERT
BEFORE INSERT
ON B_SALE_LOC_TYPE
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_LOC_TYPE.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOC_TYPE_NAME
(
	ID NUMBER(18) NOT NULL,
	LANGUAGE_ID CHAR(2 CHAR) NOT NULL,
	NAME VARCHAR2(100 CHAR) NOT NULL,
	TYPE_ID NUMBER(18) NOT NULL,

	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_B_SALE_LOC_TYPE_NAME_TI_LI ON B_SALE_LOC_TYPE_NAME(TYPE_ID, LANGUAGE_ID)
/
CREATE SEQUENCE SQ_B_SALE_LOC_TYPE_NAME INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_LOC_TYPE_NAME_INSERT
BEFORE INSERT
ON B_SALE_LOC_TYPE_NAME
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_LOC_TYPE_NAME.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOC_2SITE
(
	LOCATION_ID NUMBER(18) NOT NULL,
	SITE_ID CHAR(2 CHAR) NOT NULL,
	LOCATION_TYPE CHAR(1 CHAR) DEFAULT 'L' NOT NULL,

	PRIMARY KEY (SITE_ID, LOCATION_ID, LOCATION_TYPE)
)
/

CREATE TABLE B_SALE_LOC_DEF2SITE(
	LOCATION_CODE VARCHAR2(100 CHAR) NOT NULL,
	SITE_ID CHAR(2 CHAR) NOT NULL,
	SORT NUMBER(18) DEFAULT '100',

	PRIMARY KEY (LOCATION_CODE, SITE_ID)
)
/

CREATE TABLE B_SALE_LOCATION_GROUP
(
	ID NUMBER(18) NOT NULL,
	CODE VARCHAR2(100 CHAR) NOT NULL,
	SORT NUMBER(18) DEFAULT '100',
	PRIMARY KEY (ID)
)
/
CREATE unique INDEX IX_B_SALE_LOC_GROUP_CODE ON B_SALE_LOCATION_GROUP(CODE)
/
CREATE SEQUENCE SQ_B_SALE_LOCATION_GROUP START WITH 2 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_LOCATION_GROUP_INSERT
BEFORE INSERT
ON B_SALE_LOCATION_GROUP
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_LOCATION_GROUP.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOCATION_GROUP_LANG
(
	ID NUMBER(18) NOT NULL,
	LOCATION_GROUP_ID NUMBER(18) NOT NULL,
	LID CHAR(2 CHAR) NOT NULL,
	NAME VARCHAR2(250 CHAR) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_B_SALE_LOCATION_GROUP_LANG ON B_SALE_LOCATION_GROUP_LANG(LOCATION_GROUP_ID, LID)
/
CREATE SEQUENCE SQ_B_SALE_LOCATION_GROUP_LANG START WITH 3 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_LOCGR_LANG_INSERT
BEFORE INSERT
ON B_SALE_LOCATION_GROUP_LANG
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_LOCATION_GROUP_LANG.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_LOCATION2LOCATION_GROUP
(
	LOCATION_ID NUMBER(18) NOT NULL,
	LOCATION_GROUP_ID NUMBER(18) NOT NULL,
	PRIMARY KEY (LOCATION_ID, LOCATION_GROUP_ID)
)
/

CREATE TABLE B_SALE_DELIVERY2LOCATION
(
	DELIVERY_ID NUMBER(18) NOT NULL,
	LOCATION_CODE VARCHAR2(100 CHAR) NOT NULL,
	LOCATION_TYPE CHAR(1 CHAR) DEFAULT 'L' NOT NULL,
	PRIMARY KEY (DELIVERY_ID, LOCATION_CODE, LOCATION_TYPE)
)
/

CREATE TABLE B_SALE_DISCOUNT
(
	ID NUMBER(18) NOT NULL,
	XML_ID VARCHAR2(255 CHAR) NULL,
	LID CHAR(2 CHAR) NOT NULL,
	NAME VARCHAR2(255 CHAR) NULL,
	PRICE_FROM NUMBER(18, 2) NULL,	-- deprecated
	PRICE_TO NUMBER(18, 2) NULL,	-- deprecated
	CURRENCY CHAR(3 CHAR) NULL,
	DISCOUNT_VALUE NUMBER(18, 2) NOT NULL,	-- deprecated
	DISCOUNT_TYPE CHAR(1 CHAR) DEFAULT 'P' NOT NULL,	-- deprecated
	ACTIVE CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	SORT NUMBER(18) DEFAULT 100 NOT NULL,
	ACTIVE_FROM DATE NULL,
	ACTIVE_TO DATE NULL,
	TIMESTAMP_X DATE NULL,
	MODIFIED_BY NUMBER(18) NULL,
	DATE_CREATE DATE NULL,
	CREATED_BY NUMBER(18) NULL,
	PRIORITY NUMBER(18) DEFAULT 1 NOT NULL,
	LAST_DISCOUNT CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	VERSION NUMBER(18) DEFAULT 1 NOT NULL,
	CONDITIONS CLOB NULL,
	UNPACK CLOB NULL,
	ACTIONS CLOB NULL,
	APPLICATION CLOB NULL,
	USE_COUPONS CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	EXECUTE_MODULE VARCHAR2(50 CHAR) DEFAULT 'all' NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_DISCOUNT_LID ON B_SALE_DISCOUNT(LID)
/
CREATE INDEX IX_SSD_ACTIVE_DATE ON B_SALE_DISCOUNT(ACTIVE_FROM, ACTIVE_TO)
/
CREATE SEQUENCE SQ_B_SALE_DISCOUNT START WITH 2 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_DISCOUNT_INSERT
BEFORE INSERT
ON B_SALE_DISCOUNT
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_DISCOUNT.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_DISCOUNT_COUPON
(
	ID NUMBER(18) NOT NULL,
	DISCOUNT_ID NUMBER(18) NOT NULL,
	ACTIVE CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	ACTIVE_FROM DATE NULL,
	ACTIVE_TO DATE NULL,
	COUPON VARCHAR2(32 CHAR) NOT NULL,
	TYPE NUMBER(18) DEFAULT 0 NOT NULL,
	MAX_USE NUMBER(18) DEFAULT 0 NOT NULL,
	USE_COUNT NUMBER(18) DEFAULT 0 NOT NULL,
	USER_ID NUMBER(18) DEFAULT 0 NOT NULL,
	DATE_APPLY DATE NULL,
	TIMESTAMP_X DATE NULL,
	MODIFIED_BY NUMBER(18) NULL,
	DATE_CREATE DATE NULL,
	CREATED_BY NUMBER(18) NULL,
	DESCRIPTION CLOB NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_DISCOUNT_COUPON START WITH 1 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_DSC_CPN_INSERT
BEFORE INSERT
ON B_SALE_DISCOUNT_COUPON
FOR EACH ROW
	BEGIN
		IF :NEW.ID IS NULL THEN
			SELECT SQ_B_SALE_DISCOUNT_COUPON.NEXTVAL INTO :NEW.ID FROM dual;
		END IF;
	END;
/

CREATE TABLE B_SALE_DISCOUNT_GROUP (
	ID NUMBER(18) NOT NULL,
	DISCOUNT_ID NUMBER(18) NOT NULL,
	ACTIVE CHAR(1 CHAR) NULL,
	GROUP_ID NUMBER(18) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_DISCOUNT_GROUP INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX IX_S_DISGRP_D ON B_SALE_DISCOUNT_GROUP(DISCOUNT_ID)
/
CREATE UNIQUE INDEX IX_S_DISGRP ON B_SALE_DISCOUNT_GROUP(DISCOUNT_ID, GROUP_ID)
/
CREATE UNIQUE INDEX IX_S_DISGRP_G ON B_SALE_DISCOUNT_GROUP(GROUP_ID, DISCOUNT_ID)
/
CREATE OR REPLACE TRIGGER B_SALE_DISCOUNT_GROUP_INSERT
BEFORE INSERT
ON B_SALE_DISCOUNT_GROUP
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_DISCOUNT_GROUP.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_DISCOUNT_MODULE
(
	ID NUMBER(18) NOT NULL,
	DISCOUNT_ID NUMBER(18) NOT NULL,
	MODULE_ID VARCHAR2(50 CHAR) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_SALE_DSC_MOD ON B_SALE_DISCOUNT_MODULE(DISCOUNT_ID)
/
CREATE SEQUENCE SQ_B_SALE_DISCOUNT_MODULE INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_DSC_MODULE_INSERT
BEFORE INSERT
ON B_SALE_DISCOUNT_MODULE
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_DISCOUNT_MODULE.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_DISCOUNT_ENTITIES
(
	ID NUMBER(18) NOT NULL,
	DISCOUNT_ID NUMBER(18) NOT NULL,
	MODULE_ID VARCHAR2(50 CHAR) NOT NULL,
	ENTITY VARCHAR2(255 CHAR) NOT NULL,
	FIELD_ENTITY VARCHAR2(255 CHAR) NOT NULL,
	FIELD_TABLE VARCHAR2(255 CHAR) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_SALE_DSC_ENT_DISCOUNT_ID ON B_SALE_DISCOUNT_ENTITIES(DISCOUNT_ID)
/
CREATE SEQUENCE SQ_B_SALE_DISCOUNT_ENTITIES INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_DSC_ENTITIES_INSERT
BEFORE INSERT
ON B_SALE_DISCOUNT_ENTITIES
FOR EACH ROW
	BEGIN
		IF :NEW.ID IS NULL THEN
			SELECT SQ_B_SALE_DISCOUNT_ENTITIES.NEXTVAL INTO :NEW.ID FROM dual;
		END IF;
	END;
/

CREATE TABLE B_SALE_ORDER_DISCOUNT
(
	ID NUMBER(18) NOT NULL,
	MODULE_ID VARCHAR2(50 CHAR) NOT NULL,
	DISCOUNT_ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	DISCOUNT_HASH VARCHAR2(32 CHAR) NOT NULL,
	CONDITIONS CLOB NULL,
	UNPACK CLOB NULL,
	ACTIONS CLOB NULL,
	APPLICATION CLOB NULL,
	USE_COUPONS CHAR(1 CHAR) NOT NULL,
	SORT NUMBER(18) NOT NULL,
	PRIORITY NUMBER(18) NOT NULL,
	LAST_DISCOUNT CHAR(1 CHAR) not NULL,
	ACTIONS_DESCR CLOB NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_SALE_ORDER_DSC_HASH ON B_SALE_ORDER_DISCOUNT(DISCOUNT_HASH)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_DISCOUNT INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_DSC_INSERT
BEFORE INSERT
ON B_SALE_ORDER_DISCOUNT
FOR EACH ROW
	BEGIN
		IF :NEW.ID IS NULL THEN
			SELECT SQ_B_SALE_ORDER_DISCOUNT.NEXTVAL INTO :NEW.ID FROM dual;
		END IF;
	END;
/

CREATE TABLE B_SALE_ORDER_COUPONS
(
	ID NUMBER(18) NOT NULL,
	ORDER_ID NUMBER(18) NOT NULL,
	ORDER_DISCOUNT_ID NUMBER(18) NOT NULL,
	COUPON VARCHAR2(32 CHAR) NOT NULL,
	TYPE NUMBER(18) NOT NULL,
	COUPON_ID NUMBER(18) NOT NULL,
	DATA CLOB NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_SALE_ORDER_CPN_ORDER ON B_SALE_ORDER_COUPONS(ORDER_ID)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_COUPONS INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_CPN_INSERT
BEFORE INSERT
ON B_SALE_ORDER_COUPONS
FOR EACH ROW
	BEGIN
		IF :NEW.ID IS NULL THEN
			SELECT SQ_B_SALE_ORDER_COUPONS.NEXTVAL INTO :NEW.ID FROM dual;
		END IF;
	END;
/

CREATE TABLE B_SALE_ORDER_MODULES
(
	ID NUMBER(18) NOT NULL,
	ORDER_DISCOUNT_ID NUMBER(18) NOT NULL,
	MODULE_ID VARCHAR2(50 CHAR) NOT NULL,
	primary key (ID)
)
/
CREATE INDEX IX_SALE_ORDER_MDL_DSC ON B_SALE_ORDER_MODULES(ORDER_DISCOUNT_ID)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_MODULES INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_MDL_INSERT
BEFORE INSERT
ON B_SALE_ORDER_MODULES
FOR EACH ROW
	BEGIN
		IF :NEW.ID IS NULL THEN
			SELECT SQ_B_SALE_ORDER_MODULES.NEXTVAL INTO :NEW.ID FROM dual;
		END IF;
	END;
/

CREATE TABLE B_SALE_ORDER_RULES
(
	ID NUMBER(18) NOT NULL,
	MODULE_ID VARCHAR2(50 CHAR) NOT NULL,
	ORDER_DISCOUNT_ID NUMBER(18) NOT NULL,
	ORDER_ID NUMBER(18) NOT NULL,
	ENTITY_TYPE NUMBER(18) NOT NULL,
	ENTITY_ID NUMBER(18) NOT NULL,
	ENTITY_VALUE VARCHAR2(255 CHAR) NULL,
	COUPON_ID NUMBER(18) NOT NULL,
	APPLY CHAR(1 CHAR) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_SALE_ORDER_RULES_ORD ON B_SALE_ORDER_RULES(ORDER_ID)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_RULES INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_RLS_INSERT
BEFORE INSERT
ON B_SALE_ORDER_RULES
FOR EACH ROW
	BEGIN
		IF :NEW.ID IS NULL THEN
			SELECT SQ_B_SALE_ORDER_RULES.NEXTVAL INTO :NEW.ID FROM dual;
		END IF;
	END;
/

CREATE TABLE B_SALE_ORDER_DISCOUNT_DATA
(
	ID NUMBER(18) NOT NULL,
	ORDER_ID NUMBER(18) NOT NULL,
	ENTITY_TYPE NUMBER(18) NOT NULL,
	ENTITY_ID NUMBER(18) NOT NULL,
	ENTITY_VALUE VARCHAR2(255 CHAR) NULL,
	ENTITY_DATA CLOB NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_SALE_DSC_DATA_CMX ON B_SALE_ORDER_DISCOUNT_DATA(ORDER_ID, ENTITY_TYPE)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_DISCOUNT_DATA INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_DSCDT_INSERT
BEFORE INSERT
ON B_SALE_ORDER_DISCOUNT_DATA
FOR EACH ROW
	BEGIN
		IF :NEW.ID IS NULL THEN
			SELECT SQ_B_SALE_ORDER_DISCOUNT_DATA.NEXTVAL INTO :NEW.ID FROM dual;
		END IF;
	END;
/

CREATE TABLE B_SALE_ORDER_RULES_DESCR
(
	ID NUMBER(18) NOT NULL,
	MODULE_ID VARCHAR2(50 CHAR) NOT NULL,
	ORDER_DISCOUNT_ID NUMBER(18) NOT NULL,
	ORDER_ID NUMBER(18) NOT NULL,
	RULE_ID NUMBER(18) NOT NULL,
	DESCR CLOB NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_SALE_ORDER_RULES_DS_ORD ON B_SALE_ORDER_RULES_DESCR(ORDER_ID)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_RULES_DESCR INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_RLSDSC_INSERT
BEFORE INSERT
ON B_SALE_ORDER_RULES_DESCR
FOR EACH ROW
	BEGIN
		IF :NEW.ID IS NULL THEN
			SELECT SQ_B_SALE_ORDER_RULES_DESCR.NEXTVAL INTO :NEW.ID FROM dual;
		END IF;
	END;
/

CREATE TABLE B_SALE_USER_PROPS
(
	ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	USER_ID NUMBER(18) NOT NULL,
	PERSON_TYPE_ID NUMBER(18) NOT NULL,
	DATE_UPDATE DATE NOT NULL,
	XML_ID VARCHAR2(50 CHAR) NULL,
	VERSION_1C VARCHAR2(15 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_USER_PROPS_USER_ID ON B_SALE_USER_PROPS(USER_ID)
/
CREATE INDEX IXS_USER_PROPS_PERSON_TYPE_ID ON B_SALE_USER_PROPS(PERSON_TYPE_ID)
/
CREATE INDEX IXS_USER_PROPS_XML_ID ON B_SALE_USER_PROPS(XML_ID)
/
CREATE SEQUENCE SQ_SALE_USER_PROPS INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_USER_PROPS_INSERT
BEFORE INSERT
ON B_SALE_USER_PROPS
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_SALE_USER_PROPS.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_USER_PROPS_VALUE
(
	ID NUMBER(18) NOT NULL,
	USER_PROPS_ID NUMBER(18) NOT NULL,
	ORDER_PROPS_ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	VALUE VARCHAR2(255 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_USER_PVU_PROPS_ID ON B_SALE_USER_PROPS_VALUE(USER_PROPS_ID)
/
CREATE INDEX IXS_USER_PVU_O_PROPS_ID ON B_SALE_USER_PROPS_VALUE(ORDER_PROPS_ID)
/
CREATE SEQUENCE SQ_SALE_USER_PROPS_VALUE INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_USER_PROPS_VALUE_INSERT
BEFORE INSERT
ON B_SALE_USER_PROPS_VALUE
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_SALE_USER_PROPS_VALUE.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_STATUS
(
	ID CHAR(2 CHAR) NOT NULL,
	TYPE CHAR(1 CHAR) DEFAULT 'O' NOT NULL,
	SORT NUMBER(18) DEFAULT '100' NOT NULL,
	NOTIFY CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	PRIMARY KEY (ID)
)
/

CREATE TABLE B_SALE_STATUS_LANG
(
	STATUS_ID CHAR(2 CHAR) NOT NULL,
	LID CHAR(2 CHAR) NOT NULL,
	NAME VARCHAR2(100 CHAR) NOT NULL,
	DESCRIPTION VARCHAR2(250 CHAR) NULL,
	PRIMARY KEY (STATUS_ID, LID)
)
/

CREATE TABLE B_SALE_STATUS_GROUP_TASK
(
	STATUS_ID CHAR  (2 CHAR) NOT NULL,
	GROUP_ID  NUMBER(18) NOT NULL,
	TASK_ID   NUMBER(18) NOT NULL,
	PRIMARY KEY (STATUS_ID, GROUP_ID, TASK_ID)
)
/

CREATE TABLE B_SALE_TAX
(
	 ID          NUMBER(18)    NOT NULL,
	 LID         CHAR(2 CHAR)       NOT NULL,
	 NAME        VARCHAR2(250 CHAR) NOT NULL,
	 DESCRIPTION VARCHAR2(255 CHAR)     NULL,
	 TIMESTAMP_X DATE          NOT NULL,
	 CODE VARCHAR2(50 CHAR) NULL,
	 PRIMARY KEY (ID)
)
/
CREATE INDEX ITAX_LID ON B_SALE_TAX(LID)
/
CREATE SEQUENCE SQ_B_SALE_TAX START WITH 1 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/

CREATE TABLE B_SALE_ORDER_TAX
(
	ID            NUMBER(18)    NOT NULL,
	ORDER_ID      NUMBER(18)    NOT NULL,
	TAX_NAME      VARCHAR2(255 CHAR) NOT NULL,
	VALUE NUMBER(18,2)      NULL,
	VALUE_MONEY   NUMBER(18,2)  NOT NULL,
	APPLY_ORDER   NUMBER(18)    NOT NULL,
	CODE          VARCHAR2(50 CHAR)      NULL,
	IS_PERCENT    CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	IS_IN_PRICE   CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IXS_SOT_ORDER_ID ON B_SALE_ORDER_TAX(ORDER_ID)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_TAX START WITH 1 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/

CREATE TABLE B_SALE_TAX_RATE
(
	ID NUMBER(18) NOT NULL,
	TAX_ID NUMBER(18) NOT NULL,
	PERSON_TYPE_ID NUMBER(18) NULL,
	VALUE NUMBER(18,4) NOT NULL,
	CURRENCY CHAR(3 CHAR) NULL,
	IS_PERCENT CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	IS_IN_PRICE CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	APPLY_ORDER NUMBER(18) DEFAULT 100 NOT NULL,
	TIMESTAMP_X DATE NOT NULL,
	ACTIVE CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX ITAX_PERS_TYPE ON B_SALE_TAX_RATE(PERSON_TYPE_ID)
/
CREATE INDEX ITAX_LID1 ON B_SALE_TAX_RATE(TAX_ID)
/
CREATE INDEX ITAX_INPRICE ON B_SALE_TAX_RATE(IS_IN_PRICE)
/
CREATE SEQUENCE SQ_B_SALE_TAX_RATE START WITH 1 INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/

CREATE TABLE B_SALE_TAX2LOCATION
(
	TAX_RATE_ID NUMBER(18) NOT NULL,
	LOCATION_CODE VARCHAR2(100 CHAR) NOT NULL,
	LOCATION_TYPE CHAR(1 CHAR) DEFAULT 'L' NOT NULL,
	PRIMARY KEY (TAX_RATE_ID, LOCATION_CODE, LOCATION_TYPE)
)
/

CREATE TABLE B_SALE_TAX_EXEMPT2GROUP
(
	GROUP_ID NUMBER(18) NOT NULL,
	TAX_ID NUMBER(18) NOT NULL,
	PRIMARY KEY (GROUP_ID, TAX_ID)
)
/

CREATE TABLE B_SALE_ORDER_FLAGS2GROUP
(
	ID NUMBER(18) NULL,
	GROUP_ID NUMBER(18) NOT NULL,
	ORDER_FLAG CHAR(1 CHAR) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE UNIQUE INDEX IX_SALE_ORDFLA2GROUP ON B_SALE_ORDER_FLAGS2GROUP(GROUP_ID, ORDER_FLAG)
/
CREATE SEQUENCE SQ_SALE_ORDER_FLAGS2GROUP INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/

CREATE OR REPLACE TRIGGER B_SALE_ORDER_FLG2GRP_INSERT
BEFORE INSERT
ON B_SALE_ORDER_FLAGS2GROUP
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_ORDER_FLAGS2GROUP.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_SITE2GROUP
(
	ID NUMBER(18) NOT NULL,
	GROUP_ID NUMBER(18) NOT NULL,
	SITE_ID CHAR(2 CHAR) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE UNIQUE INDEX IX_SALE_SITE2GROUP ON B_SALE_SITE2GROUP(GROUP_ID, SITE_ID)
/
CREATE SEQUENCE SQ_SALE_SITE2GROUP INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/

CREATE OR REPLACE TRIGGER B_SALE_SITE2GROUP_INSERT
BEFORE INSERT
ON B_SALE_SITE2GROUP
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_SITE2GROUP.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_USER_ACCOUNT
(
	ID NUMBER(18) NOT NULL,
	USER_ID NUMBER(18) NOT NULL,
	TIMESTAMP_X DATE DEFAULT SYSDATE NOT NULL,
	CURRENT_BUDGET NUMBER(18,4) DEFAULT 0.0 NOT NULL,
	CURRENCY CHAR(3 CHAR) NOT NULL,
	LOCKED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DATE_LOCKED DATE NULL,
	NOTES CLOB NULL,
	PRIMARY KEY (ID)
)
/
CREATE UNIQUE INDEX IX_S_U_USER_ID ON B_SALE_USER_ACCOUNT(USER_ID, CURRENCY)
/
CREATE SEQUENCE SQ_SALE_USER_ACCOUNT INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_USER_ACCOUNT_INSERT
BEFORE INSERT
ON B_SALE_USER_ACCOUNT
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_USER_ACCOUNT.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_RECURRING
(
	ID NUMBER(18) NOT NULL,
	USER_ID NUMBER(18) NOT NULL,
	TIMESTAMP_X DATE DEFAULT SYSDATE NOT NULL,
	MODULE VARCHAR2(100 CHAR) NULL,
	PRODUCT_ID NUMBER(18) NULL,
	PRODUCT_NAME VARCHAR2(255 CHAR) NULL,
	PRODUCT_URL VARCHAR2(255 CHAR) NULL,
	PRODUCT_PRICE_ID NUMBER(18) NULL,
	PRICE_TYPE CHAR(1 CHAR) DEFAULT 'R' NOT NULL,
	RECUR_SCHEME_TYPE CHAR(1 CHAR) DEFAULT 'M' NOT NULL,
	RECUR_SCHEME_LENGTH NUMBER(18) DEFAULT '0' NOT NULL,
	WITHOUT_ORDER CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	PRICE NUMBER(18,4) DEFAULT '0.0' NOT NULL,
	CURRENCY CHAR(3 CHAR) NULL,
	CANCELED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DATE_CANCELED DATE NULL,
	PRIOR_DATE DATE NULL,
	NEXT_DATE DATE NOT NULL,
	CALLBACK_FUNC VARCHAR2(100 CHAR) NULL,
	PRODUCT_PROVIDER_CLASS VARCHAR2(100 CHAR) NULL,
	DESCRIPTION VARCHAR2(255 CHAR) NULL,
	CANCELED_REASON VARCHAR2(255 CHAR) NULL,
	ORDER_ID NUMBER(18) NOT NULL,
	REMAINING_ATTEMPTS NUMBER(18) DEFAULT 0 NOT NULL,
	SUCCESS_PAYMENT CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_S_R_USER_ID ON B_SALE_RECURRING(USER_ID)
/
CREATE INDEX IX_S_R_NEXT_DATE ON B_SALE_RECURRING(NEXT_DATE, CANCELED, REMAINING_ATTEMPTS)
/
CREATE INDEX IX_S_R_PRODUCT_ID ON B_SALE_RECURRING(MODULE, PRODUCT_ID, PRODUCT_PRICE_ID)
/
CREATE SEQUENCE SQ_SALE_RECURRING INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_RECURRING_INSERT
BEFORE INSERT
ON B_SALE_RECURRING
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_RECURRING.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_USER_CARDS
(
	ID NUMBER(18) NOT NULL,
	USER_ID NUMBER(18) NOT NULL,
	ACTIVE CHAR(1 CHAR) DEFAULT 'Y' NOT NULL,
	SORT NUMBER(18) DEFAULT 100 NOT NULL,
	TIMESTAMP_X DATE DEFAULT SYSDATE NOT NULL,
	PAY_SYSTEM_ACTION_ID NUMBER(18) NOT NULL,
	CURRENCY CHAR(3 CHAR) NULL,
	CARD_TYPE VARCHAR2(20 CHAR) NOT NULL,
	CARD_NUM CLOB NOT NULL,
	CARD_CODE VARCHAR2(5 CHAR) NULL,
	CARD_EXP_MONTH NUMBER(18) NOT NULL,
	CARD_EXP_YEAR NUMBER(18) NOT NULL,
	DESCRIPTION VARCHAR2(255 CHAR) NULL,
	SUM_MIN NUMBER(18,4) NULL,
	SUM_MAX NUMBER(18,4) NULL,
	SUM_CURRENCY CHAR(3 CHAR) NULL,
	LAST_STATUS CHAR(1 CHAR) NULL,
	LAST_STATUS_CODE VARCHAR2(5 CHAR) NULL,
	LAST_STATUS_DESCRIPTION VARCHAR2(250 CHAR) NULL,
	LAST_STATUS_MESSAGE VARCHAR2(255 CHAR) NULL,
	LAST_SUM NUMBER(18,4) NULL,
	LAST_CURRENCY CHAR(3 CHAR) NULL,
	LAST_DATE DATE NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_S_U_C_USER_ID ON B_SALE_USER_CARDS(USER_ID, ACTIVE, CURRENCY)
/
CREATE SEQUENCE SQ_SALE_USER_CARDS INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_USER_CARDS_INSERT
BEFORE INSERT
ON B_SALE_USER_CARDS
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_USER_CARDS.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_USER_TRANSACT
(
	ID NUMBER(18) NOT NULL,
	USER_ID NUMBER(18) NOT NULL,
	TIMESTAMP_X DATE DEFAULT SYSDATE NOT NULL,
	TRANSACT_DATE DATE NOT NULL,
	AMOUNT NUMBER(18,4) DEFAULT 0.0 NOT NULL,
	CURRENCY CHAR(3 CHAR) NOT NULL,
	DEBIT CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	ORDER_ID NUMBER(18) NULL,
	DESCRIPTION VARCHAR2(255 CHAR) NOT NULL,
	PAYMENT_ID number(18) NULL,
	NOTES CLOB NULL,
	EMPLOYEE_ID NUMBER(18) NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_S_U_T_USER_ID ON B_SALE_USER_TRANSACT(USER_ID)
/
CREATE INDEX IX_S_U_T_ORDER_ID ON B_SALE_USER_TRANSACT(ORDER_ID)
/
CREATE INDEX IX_S_U_T_USER_ID_CURRENCY ON B_SALE_USER_TRANSACT(USER_ID, CURRENCY)
/
CREATE INDEX IX_S_U_T_PAYMENT_ID ON B_SALE_USER_TRANSACT (PAYMENT_ID)
/
CREATE SEQUENCE SQ_SALE_USER_TRANSACT INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_USER_TRANSACT_INSERT
BEFORE INSERT
ON B_SALE_USER_TRANSACT
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_USER_TRANSACT.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE b_sale_affiliate_plan
(
  ID NUMBER(18) not null
,  SITE_ID char(2 CHAR) not null
,  NAME varchar2(250 CHAR) not null
,  DESCRIPTION CLOB null
,  TIMESTAMP_X DATE not null
,  ACTIVE char(1 CHAR) default 'Y' not null
,  BASE_RATE NUMBER(18,4) default 0 not null
,  BASE_RATE_TYPE char(1 CHAR) default 'P' not null
,  BASE_RATE_CURRENCY char(3 CHAR) null
,  MIN_PAY NUMBER(18,4) default '0' not null
,  MIN_PLAN_VALUE NUMBER(18,4) null
,  VALUE_CURRENCY char(3 CHAR) null
,  primary key (ID)
)
/
CREATE SEQUENCE SQ_SALE_AFFILIATE_PLAN INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_AFFILIATE_PLAN
BEFORE INSERT
ON B_SALE_AFFILIATE_PLAN
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_AFFILIATE_PLAN.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_AFFILIATE
(
  ID NUMBER(18) not null
,  SITE_ID char(2 CHAR) not null
,  USER_ID NUMBER(18) not null
,  AFFILIATE_ID NUMBER(18) null
,  PLAN_ID NUMBER(18) not null
,  ACTIVE char(1 CHAR) default 'Y' not null
,  TIMESTAMP_X DATE not null
,  DATE_CREATE DATE not null
,  PAID_SUM NUMBER(18,4) default '0' not null
,  APPROVED_SUM NUMBER(18,4) default '0' not null
,  PENDING_SUM NUMBER(18,4) default '0' not null
,  ITEMS_NUMBER NUMBER(18) default '0' not null
,  ITEMS_SUM NUMBER(18,4) default '0' not null
,  LAST_CALCULATE DATE null
,  AFF_SITE VARCHAR2(200 CHAR) null
,  AFF_DESCRIPTION clob null
,  FIX_PLAN char(1 CHAR) default 'N' not null
,  primary key (ID)
)
/
CREATE unique INDEX IX_SAA_USER_ID ON b_sale_affiliate(USER_ID, SITE_ID)
/
CREATE INDEX IX_SAA_AFFILIATE_ID ON b_sale_affiliate(AFFILIATE_ID)
/
CREATE SEQUENCE SQ_SALE_AFFILIATE INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_AFFILIATE
BEFORE INSERT
ON B_SALE_AFFILIATE
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_AFFILIATE.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_AFFILIATE_PLAN_SECTION
(
  ID NUMBER(18) not null
,  PLAN_ID NUMBER(18) not null
,  MODULE_ID varchar2(50 CHAR) default 'catalog' not null
,  SECTION_ID varchar2(255 CHAR) not null
,  RATE NUMBER(18,4) default '0' not null
,  RATE_TYPE char(1 CHAR) default 'P' not null
,  RATE_CURRENCY char(3 CHAR) null
,  primary key (ID)
)
/
CREATE unique INDEX IX_SAP_PLAN_ID ON b_sale_affiliate_plan_section(PLAN_ID, MODULE_ID, SECTION_ID)
/
CREATE SEQUENCE SQ_SALE_AFFILIATE_PLAN_SECTION INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_AFFILIATE_PLAN_SECTION
BEFORE INSERT
ON B_SALE_AFFILIATE_PLAN_SECTION
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_AFFILIATE_PLAN_SECTION.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_AFFILIATE_TIER
(
  ID NUMBER(18) not null
,  SITE_ID char(2 CHAR) not null
,  RATE1 NUMBER(18,4) default '0' not null
,  RATE2 NUMBER(18,4) default '0' not null
,  RATE3 NUMBER(18,4) default '0' not null
,  RATE4 NUMBER(18,4) default '0' not null
,  RATE5 NUMBER(18,4) default '0' not null
,  primary key (ID)
)
/
CREATE unique INDEX IX_SAT_SITE_ID ON b_sale_affiliate_tier(SITE_ID)
/
CREATE SEQUENCE SQ_SALE_AFFILIATE_TIER INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_AFFILIATE_TIER
BEFORE INSERT
ON B_SALE_AFFILIATE_TIER
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_AFFILIATE_TIER.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_AFFILIATE_TRANSACT
(
  ID NUMBER(18) not null
,  AFFILIATE_ID NUMBER(18) not null
,  TIMESTAMP_X DATE not null
,  TRANSACT_DATE date not null
,  AMOUNT NUMBER(18,4) not null
,  CURRENCY char(3 CHAR) not null
,  DEBIT char(1 CHAR) default 'N' not null
,  DESCRIPTION varchar2(100 CHAR) not null
,  EMPLOYEE_ID NUMBER(18) null
,  primary key (ID)
)
/
CREATE INDEX IX_SAT_AFFILIATE_ID ON b_sale_affiliate_transact(AFFILIATE_ID)
/
CREATE SEQUENCE SQ_SALE_AFFILIATE_TRANSACT INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_AFFILIATE_TRANSACT
BEFORE INSERT
ON B_SALE_AFFILIATE_TRANSACT
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_AFFILIATE_TRANSACT.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_EXPORT
(
	ID NUMBER(18) NOT NULL,
	PERSON_TYPE_ID NUMBER(18) NOT NULL,
	VARS CLOB NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_SALE_EXPORT INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_EXPORT_INSERT
BEFORE INSERT
ON B_SALE_EXPORT
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_EXPORT.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_DELIVERY_HANDLER (
	ID NUMBER (18) NOT NULL,
	LID CHAR(2 CHAR) DEFAULT '' NULL,
	ACTIVE CHAR(1 CHAR) DEFAULT 'Y' NULL,
	HID VARCHAR2(50 CHAR) DEFAULT '' NOT NULL,
	NAME VARCHAR2(250 CHAR) DEFAULT '' NOT NULL,
	SORT NUMBER (11) DEFAULT 100 NOT NULL,
	DESCRIPTION VARCHAR2(4000 CHAR) DEFAULT '' NULL,
	HANDLER VARCHAR2(250 CHAR) DEFAULT '' NOT NULL,
	SETTINGS VARCHAR2(4000 CHAR) DEFAULT '' NULL,
	PROFILES VARCHAR2(4000 CHAR) DEFAULT '' NULL,
	TAX_RATE float DEFAULT 0 NULL,
	LOGOTIP NUMBER (11) NULL,
	BASE_CURRENCY VARCHAR2(3 CHAR) NULL,
	PRIMARY KEY  (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_DELIVERY_HANDLER START WITH 1 INCREMENT BY 1 NOMINVALUE NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX IX_HID ON B_SALE_DELIVERY_HANDLER(HID)
/

CREATE TABLE B_SALE_ORDER_DELIVERY (
	ID NUMBER (18) NOT NULL,
	ORDER_ID NUMBER (18) NOT NULL,
	DATE_REQUEST DATE NULL,
	DELIVERY_LOCATION VARCHAR2(50 CHAR) DEFAULT '' NOT NULL,
	PARAMS VARCHAR2(4000 CHAR) DEFAULT '' NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_DELIVERY START WITH 1 INCREMENT BY 1 NOMINVALUE NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX IX_BSOD_ORDER_ID ON B_SALE_ORDER_DELIVERY(ORDER_ID)
/

CREATE TABLE B_SALE_PRODUCT2PRODUCT
(
	ID NUMBER(18) NOT NULL,
	PRODUCT_ID NUMBER(18) NOT NULL,
	PARENT_PRODUCT_ID NUMBER(18) NOT NULL,
	CNT NUMBER(18) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_SALE_PRODUCT2PRODUCT INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX IX_PRODUCT2PRODUCT ON B_SALE_PRODUCT2PRODUCT(PRODUCT_ID)
/
CREATE OR REPLACE TRIGGER B_SALE_PRODUCT2PRODUCT_INSERT
BEFORE INSERT
ON B_SALE_PRODUCT2PRODUCT
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
 		SELECT SQ_SALE_PRODUCT2PRODUCT.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_PERSON_TYPE_SITE
(
	PERSON_TYPE_ID NUMBER(18) NOT NULL,
	SITE_ID CHAR(2 CHAR) NOT NULL
)
/
CREATE unique INDEX IX_PERSON_TYPE_SITE ON B_SALE_PERSON_TYPE_SITE(PERSON_TYPE_ID, SITE_ID)
/

CREATE TABLE B_SALE_VIEWED_PRODUCT (
	ID NUMBER(18) NOT NULL,
	FUSER_ID NUMBER(18) DEFAULT '0' NOT NULL,
	DATE_VISIT DATE NOT NULL,
	PRODUCT_ID NUMBER(18) DEFAULT '0' NOT NULL,
	MODULE VARCHAR2(100 CHAR) NOT NULL,
	LID CHAR(2 CHAR) NOT NULL,
	NAME VARCHAR2(255 CHAR) NOT NULL,
	DETAIL_PAGE_URL VARCHAR2(255 CHAR) NULL,
	CURRENCY CHAR(3 CHAR) NULL,
	PRICE decimal(18,2) DEFAULT '0.00' NOT NULL,
	NOTES VARCHAR2(255 CHAR) NULL,
	PREVIEW_PICTURE NUMBER(18, 2) NULL,
	DETAIL_PICTURE NUMBER(18, 2) NULL,
	CALLBACK_FUNC VARCHAR2(45 CHAR) NULL,
	PRODUCT_PROVIDER_CLASS VARCHAR2(100 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_SALE_VIEWED_PRODUCT INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX ixLID ON B_SALE_VIEWED_PRODUCT(FUSER_ID,LID)
/
CREATE INDEX ixPRODUCT_ID ON B_SALE_VIEWED_PRODUCT(PRODUCT_ID)
/
CREATE INDEX ixDATE_VISIT ON B_SALE_VIEWED_PRODUCT(DATE_VISIT)
/
CREATE OR REPLACE TRIGGER B_SALE_VIEWED_PRODUCT_INSERT
BEFORE INSERT
ON B_SALE_VIEWED_PRODUCT
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_SALE_VIEWED_PRODUCT.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_ORDER_HISTORY (
	ID NUMBER(18) NOT NULL,
	H_USER_ID NUMBER(18) NOT NULL,
	H_DATE_INSERT DATE NOT NULL,
	H_ORDER_ID NUMBER(18) NOT NULL,
	H_CURRENCY CHAR(3 CHAR) NOT NULL,
	PERSON_TYPE_ID NUMBER(18) NULL,
	PAYED CHAR(1 CHAR) NULL,
	DATE_PAYED DATE NULL,
	EMP_PAYED_ID NUMBER(18) NULL,
	CANCELED CHAR(1 CHAR) NULL,
	DATE_CANCELED DATE NULL,
	REASON_CANCELED VARCHAR2(255 CHAR) NULL,
	STATUS_ID CHAR(2 CHAR) NULL,
	DATE_STATUS DATE NULL,
	PRICE_DELIVERY NUMBER(18,2) NULL,
	ALLOW_DELIVERY CHAR(1 CHAR) NULL,
	DATE_ALLOW_DELIVERY DATE NULL,
	RESERVED CHAR(1 CHAR) NULL,
	DEDUCTED CHAR(1 CHAR) NULL,
	DATE_DEDUCTED DATE NULL,
	REASON_UNDO_DEDUCTED VARCHAR2(255 CHAR) NULL,
	MARKED CHAR(1 CHAR) NULL,
	DATE_MARKED DATE NULL,
	REASON_MARKED VARCHAR2(255 CHAR) NULL,
	PRICE NUMBER(18, 2) NULL,
	CURRENCY CHAR(3 CHAR) NULL,
	DISCOUNT_VALUE NUMBER(18,2) NULL,
	USER_ID NUMBER(18) NULL,
	PAY_SYSTEM_ID NUMBER(18) NULL,
	DELIVERY_ID VARCHAR2(50 CHAR) NULL,
	PS_STATUS CHAR(1 CHAR) NULL,
	PS_STATUS_CODE CHAR(5 CHAR) NULL,
	PS_STATUS_DESCRIPTION VARCHAR2(250 CHAR) NULL,
	PS_STATUS_MESSAGE VARCHAR2(250 CHAR) NULL,
	PS_SUM NUMBER(18,2) NULL,
	PS_CURRENCY CHAR(3 CHAR) NULL,
	PS_RESPONSE_DATE DATE NULL,
	TAX_VALUE NUMBER(18,2) NULL,
	STAT_GID VARCHAR2(255 CHAR) NULL,
	SUM_PAID NUMBER(18,2) NULL,
	PAY_VOUCHER_NUM VARCHAR2(20 CHAR) NULL,
	PAY_VOUCHER_DATE DATE NULL,
	AFFILIATE_ID INT NULL,
	DELIVERY_DOC_NUM VARCHAR2(20 CHAR) NULL,
	DELIVERY_DOC_DATE DATE NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_SALE_ORDER_HISTORY INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE INDEX ixH_ORDER_ID ON B_SALE_ORDER_HISTORY(H_ORDER_ID)
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_HISTORY_INSERT
BEFORE INSERT
ON B_SALE_ORDER_HISTORY
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_SALE_ORDER_HISTORY.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/

CREATE TABLE B_SALE_DELIVERY2PAYSYSTEM (
	DELIVERY_ID NUMBER(18) NOT NULL,
	LINK_DIRECTION CHAR(1 CHAR) NOT NULL,
	PAYSYSTEM_ID NUMBER(18) NOT NULL
)
/
CREATE INDEX IX_DELIVERY ON B_SALE_DELIVERY2PAYSYSTEM (DELIVERY_ID)
/
CREATE INDEX IX_PAYSYSTEM ON B_SALE_DELIVERY2PAYSYSTEM (PAYSYSTEM_ID)
/
CREATE INDEX IX_LINK_DIRECTION ON B_SALE_DELIVERY2PAYSYSTEM (LINK_DIRECTION)
/

CREATE TABLE B_SALE_STORE_BARCODE
(
	ID NUMBER(18) NOT NULL,
	BASKET_ID NUMBER(18) NOT NULL,
	BARCODE VARCHAR2(100 CHAR) NULL,
	STORE_ID NUMBER(18) NOT NULL,
	QUANTITY FLOAT NOT NULL,
	DATE_MODIFY DATE NULL,
	DATE_CREATE DATE NULL,
	CREATED_BY NUMBER(18) NULL,
	MODIFIED_BY NUMBER(18) NULL,
	ORDER_DELIVERY_BASKET_ID NUMBER(18) DEFAULT 0 NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_SALE_STORE_BARCODE INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_STORE_BARCODE_INSERT
BEFORE INSERT
ON B_SALE_STORE_BARCODE
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_SALE_STORE_BARCODE.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/
CREATE INDEX IX_BSSB_O_DLV_BASKET_ID ON b_sale_store_barcode (ORDER_DELIVERY_BASKET_ID)
/

CREATE TABLE B_SALE_ORDER_CHANGE
(
	ID NUMBER(18) NOT NULL,
	ORDER_ID NUMBER(18) NOT NULL,
	TYPE VARCHAR2(255 CHAR) NOT NULL,
	DATA VARCHAR2(512 CHAR) NULL,
	DATE_CREATE DATE NOT NULL,
	DATE_MODIFY DATE NOT NULL,
	USER_ID NUMBER(18) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_SALE_ORDER_CHANGE INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_CHANGE_INSERT
BEFORE INSERT
ON B_SALE_ORDER_CHANGE
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_SALE_ORDER_CHANGE.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/
CREATE INDEX IXS_ORDER_ID_CHANGE ON B_SALE_ORDER_CHANGE (ORDER_ID)
/
CREATE INDEX IXS_TYPE_CHANGE ON B_SALE_ORDER_CHANGE (TYPE)
/

CREATE TABLE B_SALE_ORDER_PROCESSING
(
	ORDER_ID NUMBER(18) NOT NULL,
	PRODUCTS_ADDED CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	PRODUCTS_REMOVED CHAR(1 CHAR) DEFAULT 'N' NOT NULL
)
/

CREATE TABLE B_SALE_TRADING_PLATFORM
(
	ID NUMBER(18) NOT NULL,
	CODE VARCHAR2(20 CHAR) NOT NULL,
	ACTIVE CHAR(1 CHAR) NOT NULL,
	NAME VARCHAR2(50 CHAR) NOT NULL,
	DESCRIPTION VARCHAR2(255 CHAR) NULL,
	SETTINGS CLOB NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_TRADING_PLATFORM INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_TRADING_PLATFORM_INSERT
BEFORE INSERT
ON B_SALE_TRADING_PLATFORM
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_TRADING_PLATFORM.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/
CREATE UNIQUE INDEX IX_CODE ON B_SALE_TRADING_PLATFORM(CODE)
/

CREATE TABLE B_SALE_COMPANY
(
	ID NUMBER(18) NOT NULL,
	NAME VARCHAR2(128 CHAR) NOT NULL,
	LOCATION_ID VARCHAR2(128 CHAR) NOT NULL,
	CODE VARCHAR2(45 CHAR) NULL,
	XML_ID VARCHAR2(45 CHAR) NULL,
	ACTIVE CHAR(1 CHAR) NOT NULL,
	DATE_CREATE DATE NOT NULL,
	DATE_MODIFY DATE NOT NULL,
	CREATED_BY NUMBER(18) NULL,
	MODIFIED_BY NUMBER(18) NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_COMPANY INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/

CREATE TABLE B_SALE_BIZVAL
(
	ID NUMBER(18) NOT NULL,
	CODE_ID NUMBER(18) NOT NULL,
	PERSON_TYPE_ID NUMBER(18) NOT NULL,
	ENTITY VARCHAR(50 CHAR) NOT NULL,
	ITEM VARCHAR(255 CHAR) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_BIZVAL INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE UNIQUE INDEX IX_BSB_SECONDARY ON B_SALE_BIZVAL(CODE_ID, PERSON_TYPE_ID)
/

CREATE TABLE B_SALE_BIZVAL_CODE
(
	ID NUMBER(18) NOT NULL,
	NAME VARCHAR(50 CHAR) NOT NULL,
	DOMAIN CHAR(1 CHAR) NOT NULL,
	GROUP_ID NUMBER(18) NULL,
	SORT NUMBER(18) DEFAULT 100 NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_BIZVAL_CODE INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE UNIQUE INDEX IX_BSBC_NAME ON B_SALE_BIZVAL_CODE(NAME)
/

CREATE TABLE B_SALE_BIZVAL_GROUP
(
	ID NUMBER(18) NOT NULL,
	NAME VARCHAR(50 CHAR) NOT NULL,
	SORT NUMBER(18) DEFAULT 100 NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_BIZVAL_GROUP INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE UNIQUE INDEX IX_BSBG_NAME ON B_SALE_BIZVAL_GROUP(NAME)
/

CREATE TABLE B_SALE_BIZVAL_PARENT
(
	ID NUMBER(18) NOT NULL,
	NAME VARCHAR(50 CHAR) NOT NULL,
	LANG_SRC VARCHAR(255 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_BIZVAL_PARENT INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE UNIQUE INDEX IX_BSBP_NAME ON B_SALE_BIZVAL_PARENT(NAME)
/

CREATE TABLE B_SALE_BIZVAL_CODEPARENT
(
	CODE_ID NUMBER(18) NOT NULL,
	PARENT_ID NUMBER(18) NOT NULL,
	PRIMARY KEY (CODE_ID, PARENT_ID)
)
/

CREATE TABLE B_SALE_BIZVAL_PERSONTYPE
(
	PERSON_TYPE_ID NUMBER(18) NOT NULL,
	DOMAIN CHAR(1 CHAR) NOT NULL,
	PRIMARY KEY (PERSON_TYPE_ID, DOMAIN)
)
/

CREATE TABLE B_SALE_ORDER_DELIVERY_ES
(
	ID NUMBER(18) NOT NULL,
	SHIPMENT_ID NUMBER(18) NOT NULL,
	EXTRA_SERVICE_ID NUMBER(18) NOT NULL,
	VALUE VARCHAR(255 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_DELIVERY_ES INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_DLVR_ES_INSERT
BEFORE INSERT
ON B_SALE_ORDER_DELIVERY_ES
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_ORDER_DELIVERY_ES.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/
CREATE INDEX IX_BSOD_ES_SHIPMENT_ID ON B_SALE_ORDER_DELIVERY_ES (SHIPMENT_ID)
/
CREATE INDEX IX_BSOD_ES_EXTRA_SERVICE_ID ON B_SALE_ORDER_DELIVERY_ES (EXTRA_SERVICE_ID)
/

CREATE TABLE B_SALE_DELIVERY_ES
(
	ID NUMBER(18) NOT NULL,
	CODE VARCHAR(50 CHAR) NULL,
	NAME VARCHAR(255 CHAR) NULL,
	DESCRIPTION VARCHAR(255 CHAR) NULL,
	CLASS_NAME VARCHAR(255 CHAR) NULL,
	PARAMS VARCHAR(4000 CHAR) NULL,
	RIGHTS CHAR(3 CHAR) NOT NULL,
	DELIVERY_ID NUMBER(18) NOT NULL,
	INIT_VALUE VARCHAR(255 CHAR) NULL,
	ACTIVE CHAR(1 CHAR) NOT NULL,
	SORT NUMBER(11) DEFAULT 100 NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_DELIVERY_ES INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_DELIVERY_ES_INSERT
BEFORE INSERT
ON B_SALE_DELIVERY_ES
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_DELIVERY_ES.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/
CREATE INDEX IX_BSD_ES_DELIVERY_ID ON B_SALE_DELIVERY_ES (DELIVERY_ID)
/

CREATE TABLE B_SALE_DELIVERY_RSTR
(
	ID NUMBER(18) NOT NULL,
	DELIVERY_ID NUMBER(18) NOT NULL,
	SORT NUMBER(11) DEFAULT 100 NOT NULL,
	CLASS_NAME VARCHAR(255 CHAR) NULL,
	PARAMS VARCHAR(4000 CHAR) NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_DELIVERY_RSTR INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_DELIVERY_RSTR_INSERT
BEFORE INSERT
ON B_SALE_DELIVERY_RSTR
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_DELIVERY_RSTR.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/
CREATE INDEX IX_BSDR_DELIVERY_ID ON B_SALE_DELIVERY_RSTR (DELIVERY_ID)
/

CREATE TABLE B_SALE_DELIVERY_SRV
(
	ID NUMBER(18) NOT NULL,
	CODE VARCHAR(50 CHAR) NULL,
	PARENT_ID NUMBER(18) NULL,
	NAME VARCHAR(255 CHAR) NOT NULL,
	ACTIVE CHAR(1 CHAR) NOT NULL,
	DESCRIPTION VARCHAR(255 CHAR) NULL,
	SORT NUMBER(11) DEFAULT 100 NOT NULL,
	LOGOTIP NUMBER (11) NULL,
	CONFIG CLOB NULL,
	CLASS_NAME VARCHAR(255 CHAR) NULL,
	CURRENCY CHAR(3 CHAR) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE SEQUENCE SQ_B_SALE_DELIVERY_SRV INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_DELIVERY_SRV_INSERT
BEFORE INSERT
ON B_SALE_DELIVERY_SRV
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_DELIVERY_SRV.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/
CREATE INDEX IX_BSD_SRV_PARENT_ID ON B_SALE_DELIVERY_SRV (PARENT_ID)
/
CREATE INDEX IX_BSD_SRV_CODE ON B_SALE_DELIVERY_SRV (CODE)
/
CREATE TABLE B_SALE_ORDER_DLV_BASKET(
	ID NUMBER(18) NOT NULL,
	ORDER_DELIVERY_ID NUMBER(18) NOT NULL,
	BASKET_ID NUMBER(18) NOT NULL,
	DATE_INSERT DATE NOT NULL,
	QUANTITY DECIMAL(18,4) NOT NULL,
	RESERVED_QUANTITY DECIMAL(18,4) NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_BSODB_ORDER_DELIVERY_ID ON B_SALE_ORDER_DLV_BASKET(ORDER_DELIVERY_ID)
/
CREATE SEQUENCE SQ_B_SALE_ORDER_DLV_BASKET INCREMENT BY 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER
/
CREATE OR REPLACE TRIGGER B_SALE_ORDER_DLV_BASKET_INSERT
BEFORE INSERT
ON B_SALE_ORDER_DLV_BASKET
FOR EACH ROW
BEGIN
	IF :NEW.ID IS NULL THEN
		SELECT SQ_B_SALE_ORDER_DLV_BASKET.NEXTVAL INTO :NEW.ID FROM dual;
	END IF;
END;
/
CREATE TABLE B_SALE_ORDER_PAYMENT(
	ID NUMBER(18) NOT NULL,
	ORDER_ID NUMBER(18) NOT NULL,
	PAID CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	DATE_PAID DATE NULL,
	EMP_PAID_ID NUMBER(18) NULL,
	PAY_SYSTEM_ID NUMBER(18) NOT NULL,
	PS_STATUS CHAR(1 CHAR) NULL,
	PS_STATUS_CODE CHAR(5 CHAR) NULL,
	PS_STATUS_DESCRIPTION VARCHAR2(250 CHAR) NULL,
	PS_STATUS_MESSAGE VARCHAR2(250 CHAR) NULL,
	PS_SUM DECIMAL(18,4) NULL,
	PS_CURRENCY CHAR(3 CHAR) NULL,
	PS_RESPONSE_DATE DATE NULL,
	PAY_VOUCHER_NUM VARCHAR2(20 CHAR) NULL,
	PAY_VOUCHER_DATE DATE NULL,
	DATE_PAY_BEFORE DATE NULL,
	DATE_BILL DATE NULL,
	XML_ID VARCHAR2(255 CHAR) NULL,
	SUM DECIMAL(18,4) NOT NULL,
	CURRENCY CHAR(3 CHAR) NOT NULL,
	PAY_SYSTEM_NAME VARCHAR2(128 CHAR) NOT NULL,
	RESPONSIBLE_ID NUMBER(18) NULL,
	DATE_RESPONSIBLE_ID date NULL,
	EMP_RESPONSIBLE_ID NUMBER(18) NULL,
	COMMENTS clob,
	COMPANY_ID NUMBER(18) NULL,
	PAY_RETURN_DATE date NULL,
	EMP_RETURN_ID NUMBER(18) NULL,
	PAY_RETURN_NUM VARCHAR2(20 CHAR) NULL,
	PAY_RETURN_COMMENT clob,
	IS_RETURN CHAR(1 CHAR) DEFAULT 'N' NOT NULL,
	PRIMARY KEY (ID)
)
/
CREATE INDEX IX_BSOP_ORDER_ID ON B_SALE_ORDER_PAYMENT(ORDER_ID)
/