CREATE TABLE B_SALE_AUXILIARY
(
	ID int NOT NULL IDENTITY (1, 1),
	TIMESTAMP_X datetime NOT NULL,
	ITEM varchar(255) NOT NULL,
	ITEM_MD5 varchar(32) NOT NULL,
	USER_ID int NOT NULL,
	DATE_INSERT datetime NOT NULL
)
GO
ALTER TABLE B_SALE_AUXILIARY ADD CONSTRAINT PK_B_SALE_AUXILIARY PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_AUXILIARY ADD CONSTRAINT DF_B_SALE_AUXILIARY_TIMESTAMP_X DEFAULT GETDATE() FOR TIMESTAMP_X
GO
CREATE UNIQUE INDEX IX_B_SALE_AUXILIARY_1 ON B_SALE_AUXILIARY(USER_ID, ITEM_MD5)
GO
create trigger B_SALE_AUXILIARY_UPDATE on B_SALE_AUXILIARY for update as
if (not update(TIMESTAMP_X))
begin
	UPDATE B_SALE_AUXILIARY SET
		TIMESTAMP_X = GETDATE()
	FROM
		B_SALE_AUXILIARY U,
		INSERTED I
	WHERE
		U.ID = I.ID
end
GO

CREATE TABLE B_SALE_LANG
(
	LID char(2) NOT NULL,
	CURRENCY char(3) NOT NULL
)
GO
ALTER TABLE B_SALE_LANG ADD CONSTRAINT PK_B_SALE_LANG PRIMARY KEY (LID)
GO

CREATE TABLE B_SALE_FUSER
(
	ID int NOT NULL IDENTITY (1, 1),
	DATE_INSERT datetime NOT NULL,
	DATE_UPDATE datetime NOT NULL,
	USER_ID int NULL,
	CODE varchar(32) NULL
)
GO
ALTER TABLE B_SALE_FUSER ADD CONSTRAINT PK_B_SALE_FUSER PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_FUSER_1 ON B_SALE_FUSER(USER_ID)
GO
CREATE INDEX IX_B_SALE_FUSER_2 ON B_SALE_FUSER(CODE)
GO

CREATE TABLE B_SALE_BASKET
(
	ID int NOT NULL IDENTITY (1, 1),
	FUSER_ID int NOT NULL,
	ORDER_ID int NULL,
	PRODUCT_ID int NOT NULL,
	PRODUCT_PRICE_ID int NULL,
	PRICE decimal(18, 4) NOT NULL,
	CURRENCY char(3) NOT NULL,
	BASE_PRICE decimal(18, 4) null,
	VAT_INCLUDED char(1) NOT NULL,
	DATE_INSERT datetime NOT NULL,
	DATE_UPDATE datetime NOT NULL,
	WEIGHT decimal(18, 2) NULL,
	QUANTITY decimal(18, 4) NOT NULL,
	LID char(2) NOT NULL,
	DELAY char(1) NOT NULL,
	NAME varchar(255) NOT NULL,
	CAN_BUY char(1) NOT NULL,
	MODULE varchar(100) NULL,
	CALLBACK_FUNC varchar(100) NULL,
	NOTES varchar(250) NULL,
	ORDER_CALLBACK_FUNC varchar(100) NULL,
	DETAIL_PAGE_URL varchar(250) NULL,
	DISCOUNT_PRICE decimal(18,4) NOT NULL,
	CANCEL_CALLBACK_FUNC varchar(100) NULL,
	PAY_CALLBACK_FUNC varchar(100) NULL,
	PRODUCT_PROVIDER_CLASS varchar(100) NULL,
	CATALOG_XML_ID varchar(100) null,
	PRODUCT_XML_ID varchar(100) null,
	DISCOUNT_NAME varchar(255) null,
	DISCOUNT_VALUE char(32) null,
	DISCOUNT_COUPON char(32) null,
	VAT_RATE DECIMAL(18, 2) null,
	SUBSCRIBE char(1) NOT NULL,
	RESERVED char(1) NOT NULL,
	DEDUCTED char(1) NOT NULL,
	BARCODE_MULTI char(1) NOT NULL,
	RESERVE_QUANTITY decimal(18,2) NULL,
	CUSTOM_PRICE char(1) NOT NULL,
	DIMENSIONS varchar(255) null,
	TYPE int NULL,
	SET_PARENT_ID int NULL,
	MEASURE_CODE int NULL,
	MEASURE_NAME varchar(50) null,
	RECOMMENDATION varchar(40) null
)
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT PK_B_SALE_BASKET PRIMARY KEY (ID)
GO
CREATE INDEX IXS_SBAS_XML_ID ON B_SALE_BASKET(PRODUCT_XML_ID, CATALOG_XML_ID)
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_B_SALE_BASKET_QUANTITY DEFAULT '0' FOR QUANTITY
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_B_SALE_BASKET_DELAY DEFAULT 'N' FOR DELAY
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_B_SALE_BASKET_CAN_BUY DEFAULT 'N' FOR CAN_BUY
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_B_SALE_BASKET_DISCOUNT_PRICE DEFAULT '0' FOR DISCOUNT_PRICE
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_SALE_BASKET_VAT_RATE DEFAULT '0.00' FOR VAT_RATE
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_SUBSCRIBE DEFAULT 'N' FOR SUBSCRIBE
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_B_SALE_BASKET_RESERVED DEFAULT 'N' FOR RESERVED
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_B_SALE_BASKET_DEDUCTED DEFAULT 'N' FOR DEDUCTED
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_B_SALE_BASKET_BARCODE_MULTI DEFAULT 'N' FOR BARCODE_MULTI
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_B_SALE_BASKET_CUSTOM_PRICE DEFAULT 'N' FOR CUSTOM_PRICE
GO
ALTER TABLE B_SALE_BASKET ADD CONSTRAINT DF_B_SALE_BASKET_VAT_INCLUDED DEFAULT 'Y' FOR VAT_INCLUDED
GO
CREATE INDEX IX_B_SALE_BASKET_1 ON B_SALE_BASKET(LID)
GO
CREATE INDEX IX_B_SALE_BASKET_2 ON B_SALE_BASKET(FUSER_ID)
GO
CREATE INDEX IX_B_SALE_BASKET_3 ON B_SALE_BASKET(ORDER_ID)
GO
CREATE INDEX IX_B_SALE_BASKET_4 ON B_SALE_BASKET(PRODUCT_ID)
GO
CREATE INDEX IX_B_SALE_BASKET_5 ON B_SALE_BASKET(PRODUCT_PRICE_ID)
GO
CREATE INDEX IX_B_SALE_BASKET_6 ON B_SALE_BASKET(DATE_INSERT)
GO

CREATE TABLE B_SALE_BASKET_PROPS
(
	ID int NOT NULL IDENTITY (1, 1),
	BASKET_ID int NOT NULL,
	NAME varchar(255) NOT NULL,
	VALUE varchar(255) NULL,
	CODE varchar(255) NULL,
	SORT int NOT NULL
)
GO
ALTER TABLE B_SALE_BASKET_PROPS ADD CONSTRAINT PK_B_SALE_BASKET_PROPS PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_BASKET_PROPS ADD CONSTRAINT DF_B_SALE_BASKET_PROPS_SORT DEFAULT '100' FOR SORT
GO
CREATE INDEX IX_B_SALE_BASKET_PROPS_1 ON B_SALE_BASKET_PROPS(BASKET_ID)
GO
CREATE INDEX IX_B_SALE_BASKET_PROPS_2 ON B_SALE_BASKET_PROPS(CODE)
GO

CREATE TABLE B_SALE_ORDER
(
	ID int NOT NULL IDENTITY (1, 1),
	LID char(2) NOT NULL,
	PERSON_TYPE_ID int NOT NULL,
	PAYED char(1) NOT NULL,
	DATE_PAYED datetime NULL,
	EMP_PAYED_ID int NULL,
	CANCELED char(1) NOT NULL,
	DATE_CANCELED datetime NULL,
	EMP_CANCELED_ID int NULL,
	REASON_CANCELED varchar(255) NULL,
	STATUS_ID char(2) NOT NULL,
	DATE_STATUS datetime NOT NULL,
	EMP_STATUS_ID int NULL,
	PRICE_DELIVERY decimal(18,4) NOT NULL,
	ALLOW_DELIVERY char(1) NOT NULL,
	DATE_ALLOW_DELIVERY datetime NULL,
	EMP_ALLOW_DELIVERY_ID int NULL,
	DEDUCTED char(1) NOT NULL,
	DATE_DEDUCTED datetime NULL,
	EMP_DEDUCTED_ID int NULL,
	REASON_UNDO_DEDUCTED varchar(255) NULL,
	MARKED char(1) NOT NULL,
	DATE_MARKED datetime NULL,
	EMP_MARKED_ID int NULL,
	REASON_MARKED varchar(255) NULL,
	RESERVED char(1) NOT NULL,
	PRICE decimal(18, 2) NOT NULL,
	CURRENCY char(3) NOT NULL,
	DISCOUNT_VALUE decimal(18,4) NOT NULL ,
	USER_ID int NOT NULL,
	PAY_SYSTEM_ID int NULL,
	DELIVERY_ID varchar(50) NULL,
	DATE_INSERT datetime NOT NULL,
	DATE_UPDATE datetime NOT NULL,
	USER_DESCRIPTION varchar(2000) NULL,
	ADDITIONAL_INFO varchar(255) NULL,
	PS_STATUS char(1) NULL,
	PS_STATUS_CODE char(5) NULL,
	PS_STATUS_DESCRIPTION varchar(250) NULL,
	PS_STATUS_MESSAGE varchar(250) NULL,
	PS_SUM decimal(18,2) NULL,
	PS_CURRENCY char(3) NULL,
	PS_RESPONSE_DATE datetime NULL,
	COMMENTS text NULL,
	TAX_VALUE decimal(18,2) NOT NULL,
	STAT_GID varchar(255) NULL,
	SUM_PAID decimal(18,2) NOT NULL,
	RECURRING_ID int NULL,
	PAY_VOUCHER_NUM varchar(20) NULL,
	PAY_VOUCHER_DATE datetime NULL,
	LOCKED_BY int null,
	DATE_LOCK datetime null,
	RECOUNT_FLAG char(1) default 'Y' not null,
	AFFILIATE_ID int null,
	DELIVERY_DOC_NUM varchar(20) NULL,
	DELIVERY_DOC_DATE datetime NULL,
	UPDATED_1C char(1) not null constraint DF_B_SALE_ORDER_UPDATE_1C_FLAG default 'N',
	STORE_ID int null,
	ORDER_TOPIC varchar(255) NULL,
	CREATED_BY int NULL,
	RESPONSIBLE_ID int NULL,
	DATE_PAY_BEFORE datetime NULL,
	DATE_BILL datetime NULL,
	ACCOUNT_NUMBER varchar(100) NULL,
	TRACKING_NUMBER varchar(255) NULL,
	XML_ID varchar(255) NULL,
	ID_1C varchar(15) NULL,
	VERSION_1C varchar(15) NULL,
	VERSION INT not null,
	EXTERNAL_ORDER char(1) default 'N' not null,
	BX_USER_ID varchar(32) null
)
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT PK_B_SALE_ORDER PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_PAYED DEFAULT 'N' FOR PAYED
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_CANCELED DEFAULT 'N' FOR CANCELED
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_MARKED DEFAULT 'N' FOR MARKED
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_RESERVED DEFAULT 'N' FOR RESERVED
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_DEDUCTED DEFAULT 'N' FOR DEDUCTED
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_STATUS_ID DEFAULT 'N' FOR STATUS_ID
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_ALLOW_DELIVERY DEFAULT 'N' FOR ALLOW_DELIVERY
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_TAX_VALUE DEFAULT '0' FOR TAX_VALUE
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_SUM_PAID DEFAULT '0' FOR SUM_PAID
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_VERSION DEFAULT '0' FOR VERSION
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_DISCOUNT_VALUE DEFAULT '0.0000' FOR DISCOUNT_VALUE
GO
ALTER TABLE B_SALE_ORDER ADD CONSTRAINT DF_B_SALE_ORDER_PRICE_DELIVERY DEFAULT '0.0000' FOR PRICE_DELIVERY
GO
CREATE INDEX IX_B_SALE_ORDER_2 ON B_SALE_ORDER(USER_ID)
GO
CREATE INDEX IX_SOO_AFFILIATE_ID ON B_SALE_ORDER(AFFILIATE_ID)
GO
CREATE INDEX IX_B_SALE_ORDER_3 ON B_SALE_ORDER(PERSON_TYPE_ID)
GO
CREATE INDEX IX_B_SALE_ORDER_4 ON B_SALE_ORDER(PAYED)
GO
CREATE INDEX IX_B_SALE_ORDER_6 ON B_SALE_ORDER(STATUS_ID)
GO
CREATE INDEX IX_B_SALE_ORDER_9 ON B_SALE_ORDER(RECURRING_ID)
GO
CREATE INDEX IXS_ORDER_UPDATED_1C ON B_SALE_ORDER(UPDATED_1C)
GO
CREATE INDEX IXS_SALE_COUNT ON B_SALE_ORDER(USER_ID,LID,PAYED,CANCELED)
GO
CREATE INDEX IXS_DATE_UPDATE ON B_SALE_ORDER(DATE_UPDATE)
GO
CREATE INDEX IXS_XML_ID ON B_SALE_ORDER(XML_ID)
GO
CREATE INDEX IXS_ID_1C ON B_SALE_ORDER (ID_1C)
GO
CREATE UNIQUE INDEX IXS_ACCOUNT_NUMBER ON B_SALE_ORDER(ACCOUNT_NUMBER)
GO

CREATE TABLE B_SALE_PERSON_TYPE
(
	ID int NOT NULL IDENTITY (1, 1),
	LID char(2) NOT NULL,
	NAME varchar(255) NOT NULL,
	SORT int NOT NULL,
	ACTIVE char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_PERSON_TYPE ADD CONSTRAINT PK_B_SALE_PERSON_TYPE PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_PERSON_TYPE ADD CONSTRAINT DF_B_SALE_PERSON_TYPE_SORT DEFAULT '150' FOR SORT
GO
ALTER TABLE B_SALE_PERSON_TYPE ADD CONSTRAINT DF_B_SALE_PERSON_TYPE_ACTIVE DEFAULT 'Y' FOR ACTIVE
GO
CREATE INDEX IX_B_SALE_PERSON_TYPE_1 ON B_SALE_PERSON_TYPE(LID)
GO

CREATE TABLE B_SALE_ORDER_PROPS_GROUP
(
	ID int NOT NULL IDENTITY (1, 1),
	PERSON_TYPE_ID int NOT NULL,
	NAME varchar(255) NOT NULL,
	SORT int NOT NULL
)
GO
ALTER TABLE B_SALE_ORDER_PROPS_GROUP ADD CONSTRAINT PK_B_SALE_ORDER_PROPS_GROUP PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_ORDER_PROPS_GROUP ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_GROUP_SORT DEFAULT '100' FOR SORT
GO
CREATE INDEX IX_B_SALE_ORDER_PROPS_GROUP_1 ON B_SALE_ORDER_PROPS_GROUP(PERSON_TYPE_ID)
GO

CREATE TABLE B_SALE_ORDER_PROPS
(
	ID int NOT NULL IDENTITY (1, 1),
	PERSON_TYPE_ID int NOT NULL,
	NAME varchar(255) NOT NULL,
	TYPE varchar(20) NOT NULL,
	REQUIRED char(1) NOT NULL,
	DEFAULT_VALUE varchar(500) NULL,
	SORT int NOT NULL,
	USER_PROPS char(1) NOT NULL,
	IS_LOCATION char(1) NOT NULL,
	PROPS_GROUP_ID int NOT NULL,
	DESCRIPTION varchar(255) NULL,
	IS_EMAIL char(1) NOT NULL,
	IS_PROFILE_NAME char(1) NOT NULL,
	IS_PAYER char(1) NOT NULL,
	IS_LOCATION4TAX char(1) NOT NULL,
	IS_FILTERED char(1) NOT NULL,
	CODE varchar(50) NULL,
	IS_ZIP char(1) NOT NULL,
	IS_PHONE char(1) NOT NULL,
	ACTIVE char(1) NOT NULL,
	UTIL char(1) NOT NULL,
	INPUT_FIELD_LOCATION int NOT NULL,
	MULTIPLE char(1) NOT NULL,
	IS_ADDRESS char(1) NOT NULL,
	SETTINGS varchar(500) NULL
)
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT PK_B_SALE_ORDER_PROPS PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_REQUIRED DEFAULT 'N' FOR REQUIRED
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_SORT DEFAULT '100' FOR SORT
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_USER_PROPS DEFAULT 'N' FOR USER_PROPS
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_IS_LOCATION DEFAULT 'N' FOR IS_LOCATION
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_IS_EMAIL DEFAULT 'N' FOR IS_EMAIL
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_IS_PROFILE_NAME DEFAULT 'N' FOR IS_PROFILE_NAME
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_IS_PAYER DEFAULT 'N' FOR IS_PAYER
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_IS_LOCATION4TAX DEFAULT 'N' FOR IS_LOCATION4TAX
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_IS_FILTERED DEFAULT 'N' FOR IS_FILTERED
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_IS_ZIP DEFAULT 'N' FOR IS_ZIP
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_IS_PHONE DEFAULT 'N' FOR IS_PHONE
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_ACTIVE DEFAULT 'Y' FOR ACTIVE
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_UTIL DEFAULT 'N' FOR UTIL
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_INPUT_FIELD_LOCATION DEFAULT '0' FOR INPUT_FIELD_LOCATION
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_MULTIPLE DEFAULT 'N' FOR MULTIPLE
GO
ALTER TABLE B_SALE_ORDER_PROPS ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_IS_ADDRESS DEFAULT 'N' FOR IS_ADDRESS
GO
CREATE INDEX IX_B_SALE_ORDER_PROPS_1 ON B_SALE_ORDER_PROPS(PERSON_TYPE_ID)
GO

CREATE TABLE B_SALE_ORDER_PROPS_VALUE
(
	ID int NOT NULL IDENTITY (1, 1),
	ORDER_ID int NOT NULL,
	ORDER_PROPS_ID int NULL,
	NAME varchar(255) NOT NULL,
	VALUE varchar(500) NULL,
	CODE varchar(50) NULL
)
GO
ALTER TABLE B_SALE_ORDER_PROPS_VALUE ADD CONSTRAINT PK_B_SALE_ORDER_PROPS_VALUE PRIMARY KEY (ID)
GO
CREATE UNIQUE INDEX UX_B_SALE_ORDER_PROPS_VALUE_1 ON B_SALE_ORDER_PROPS_VALUE(ORDER_ID, ORDER_PROPS_ID)
GO

CREATE TABLE B_SALE_ORDER_PROPS_VARIANT
(
	ID int NOT NULL IDENTITY (1, 1),
	ORDER_PROPS_ID int NOT NULL,
	NAME varchar(255) NOT NULL,
	VALUE varchar(255) NULL,
	SORT int NOT NULL,
	DESCRIPTION varchar(255) NULL
)
GO
ALTER TABLE B_SALE_ORDER_PROPS_VARIANT ADD CONSTRAINT PK_B_SALE_ORDER_PROPS_VARIANT PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_ORDER_PROPS_VARIANT ADD CONSTRAINT DF_B_SALE_ORDER_PROPS_VARIANT_SORT DEFAULT '100' FOR SORT
GO
CREATE INDEX IX_B_SALE_ORDER_PROPS_VARIANT_1 ON B_SALE_ORDER_PROPS_VARIANT(ORDER_PROPS_ID)
GO

CREATE TABLE B_SALE_ORDER_PROPS_RELATION
(
	PROPERTY_ID INT NOT NULL IDENTITY (1, 1),
	ENTITY_ID VARCHAR(35) NOT NULL,
	ENTITY_TYPE CHAR(1) NOT NULL,
)
ALTER TABLE B_SALE_ORDER_PROPS_RELATION ADD CONSTRAINT PK_B_SALE_ORDER_PROPS_RELATION PRIMARY KEY (PROPERTY_ID, ENTITY_ID, ENTITY_TYPE)
GO
CREATE INDEX IX_PROPERTY ON B_SALE_ORDER_PROPS_RELATION (PROPERTY_ID)
GO
CREATE INDEX IX_ENTITY_ID ON B_SALE_ORDER_PROPS_RELATION (ENTITY_ID)
GO

CREATE TABLE B_SALE_PAY_SYSTEM
(
	ID int NOT NULL IDENTITY (1, 1),
	LID char(2) NULL,
	CURRENCY char(3) NULL,
	NAME varchar(255) NOT NULL,
	ACTIVE char(1) NOT NULL,
	SORT int NOT NULL,
	DESCRIPTION varchar(2000) NULL
)
GO
ALTER TABLE B_SALE_PAY_SYSTEM ADD CONSTRAINT PK_B_SALE_PAY_SYSTEM PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_PAY_SYSTEM ADD CONSTRAINT DF_B_SALE_PAY_SYSTEM_ACTIVE DEFAULT 'Y' FOR ACTIVE
GO
ALTER TABLE B_SALE_PAY_SYSTEM ADD CONSTRAINT DF_B_SALE_PAY_SYSTEM_SORT DEFAULT '100' FOR SORT
GO
CREATE INDEX IX_B_SALE_PAY_SYSTEM_1 ON B_SALE_PAY_SYSTEM(LID)
GO

CREATE TABLE B_SALE_PAY_SYSTEM_ACTION
(
	ID int NOT NULL IDENTITY (1, 1),
	PAY_SYSTEM_ID int NOT NULL,
	PERSON_TYPE_ID int NOT NULL,
	NAME varchar(255) NOT NULL,
	ACTION_FILE varchar(255) NULL,
	RESULT_FILE varchar(255) NULL,
	NEW_WINDOW char(1) NOT NULL,
	PARAMS text NULL,
	TARIF text NULL,
	HAVE_PAYMENT char(1) NOT NULL,
	HAVE_ACTION char(1) NOT NULL,
	HAVE_RESULT char(1) NOT NULL,
	HAVE_PREPAY char(1) NOT NULL,
	HAVE_RESULT_RECEIVE CHAR(1) NOT NULL,
	ENCODING varchar(45) NULL,
	LOGOTIP int NULL,
)
GO
ALTER TABLE B_SALE_PAY_SYSTEM_ACTION ADD CONSTRAINT PK_B_SALE_PAY_SYSTEM_ACTION PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_PAY_SYSTEM_ACTION ADD CONSTRAINT DF_B_SALE_PAY_SYSTEM_ACTION_NEW_WINDOW DEFAULT 'Y' FOR NEW_WINDOW
GO
ALTER TABLE B_SALE_PAY_SYSTEM_ACTION ADD CONSTRAINT DF_B_SALE_PAY_SYSTEM_ACTION_HAVE_PAYMENT DEFAULT 'N' FOR HAVE_PAYMENT
GO
ALTER TABLE B_SALE_PAY_SYSTEM_ACTION ADD CONSTRAINT DF_B_SALE_PAY_SYSTEM_ACTION_HAVE_ACTION DEFAULT 'N' FOR HAVE_ACTION
GO
ALTER TABLE B_SALE_PAY_SYSTEM_ACTION ADD CONSTRAINT DF_B_SALE_PAY_SYSTEM_ACTION_HAVE_RESULT DEFAULT 'N' FOR HAVE_RESULT
GO
ALTER TABLE B_SALE_PAY_SYSTEM_ACTION ADD CONSTRAINT DF_B_SALE_PAY_SYSTEM_ACTION_HAVE_PREPAY DEFAULT 'N' FOR HAVE_PREPAY
GO
ALTER TABLE B_SALE_PAY_SYSTEM_ACTION ADD CONSTRAINT DF_B_SALE_PAY_SYSTEM_ACTION_HAVE_RESULT_RECEIVE DEFAULT 'N' FOR HAVE_RESULT_RECEIVE
GO
CREATE INDEX IX_B_SALE_PAY_SYSTEM_ACTION_1 ON B_SALE_PAY_SYSTEM_ACTION(PERSON_TYPE_ID)
GO
CREATE UNIQUE INDEX UX_B_SALE_PAY_SYSTEM_ACTION_1 ON B_SALE_PAY_SYSTEM_ACTION(PAY_SYSTEM_ID, PERSON_TYPE_ID)
GO

CREATE TABLE B_SALE_LOCATION_COUNTRY
(
	ID int NOT NULL IDENTITY (1, 1),
	NAME varchar(100) NOT NULL,
	SHORT_NAME varchar(100) NULL
)
GO
ALTER TABLE B_SALE_LOCATION_COUNTRY ADD CONSTRAINT PK_B_SALE_LOCATION_COUNTRY PRIMARY KEY (ID)
GO
CREATE INDEX IX_NAME ON B_SALE_LOCATION_COUNTRY(NAME)
GO

CREATE TABLE B_SALE_LOCATION_COUNTRY_LANG
(
	ID int NOT NULL IDENTITY (1, 1),
	COUNTRY_ID int NOT NULL,
	LID char(2) NOT NULL,
	NAME varchar(100) NOT NULL,
	SHORT_NAME varchar(100) NULL
)
GO
ALTER TABLE B_SALE_LOCATION_COUNTRY_LANG ADD CONSTRAINT PK_B_SALE_LOCATION_COUNTRY_LANG PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_LOCATION_COUNTRY_LANG_1 ON B_SALE_LOCATION_COUNTRY_LANG(COUNTRY_ID, LID)
GO

CREATE TABLE B_SALE_LOCATION_REGION
(
	ID int NOT NULL IDENTITY (1, 1),
	NAME varchar(255) NOT NULL,
	SHORT_NAME varchar(100) NULL
)
GO
ALTER TABLE B_SALE_LOCATION_REGION ADD CONSTRAINT PK_B_SALE_LOCATION_REGION PRIMARY KEY (ID)
GO

CREATE TABLE B_SALE_LOCATION_REGION_LANG
(
	ID int NOT NULL IDENTITY (1, 1),
	REGION_ID int NOT NULL,
	LID char(2) NOT NULL,
	NAME varchar(100) NOT NULL,
	SHORT_NAME varchar(100) NULL
)
GO
ALTER TABLE B_SALE_LOCATION_REGION_LANG ADD CONSTRAINT PK_B_SALE_LOCATION_REGION_LANG PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_LOCATION_REGION_LANG_1 ON B_SALE_LOCATION_REGION_LANG(REGION_ID, LID)
GO
CREATE INDEX IX_NAME_1 ON B_SALE_LOCATION_REGION_LANG(NAME)
GO

CREATE TABLE B_SALE_LOCATION_CITY
(
	ID int NOT NULL IDENTITY (1, 1),
	NAME varchar(100) NOT NULL,
	SHORT_NAME varchar(100) NULL,
	REGION_ID int NULL,
)
GO
ALTER TABLE B_SALE_LOCATION_CITY ADD CONSTRAINT PK_B_SALE_LOCATION_CITY PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_LOCAT_REGION ON B_SALE_LOCATION_CITY(REGION_ID)
GO

CREATE TABLE B_SALE_LOCATION_CITY_LANG
(
	ID int NOT NULL IDENTITY (1, 1),
	CITY_ID int NOT NULL,
	LID char(2) NOT NULL,
	NAME varchar(100) NOT NULL,
	SHORT_NAME varchar(100) NULL
)
GO
ALTER TABLE B_SALE_LOCATION_CITY_LANG ADD CONSTRAINT PK_B_SALE_LOCATION_CITY_LANG PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_LOCATION_CITY_LANG_1 ON B_SALE_LOCATION_CITY_LANG(CITY_ID, LID)
GO
CREATE INDEX IX_NAME_2 ON B_SALE_LOCATION_CITY_LANG(NAME)
GO

CREATE TABLE B_SALE_LOCATION_ZIP (
	ID int NOT NULL IDENTITY(1, 1),
	LOCATION_ID int NOT NULL,
	ZIP varchar(10) NOT NULL
)
GO
ALTER TABLE B_SALE_LOCATION_ZIP ADD CONSTRAINT PK_B_SALE_LOCATION_ZIP PRIMARY KEY (ID)
GO
CREATE INDEX IX_LOCATION_ID ON B_SALE_LOCATION_ZIP (LOCATION_ID)
GO
CREATE INDEX IX_ZIP ON B_SALE_LOCATION_ZIP (ZIP)
GO

CREATE TABLE B_SALE_LOCATION
(
	ID int NOT NULL IDENTITY (1, 1),
	SORT int NOT NULL,
	CODE varchar(100) NOT NULL,
	LEFT_MARGIN int,
	RIGHT_MARGIN int,
	PARENT_ID int,
	DEPTH_LEVEL int,
	TYPE_ID int,
	LATITUDE decimal(8,6),
	LONGITUDE decimal(9,6),
	COUNTRY_ID int,
	REGION_ID int,
	CITY_ID int,
	LOC_DEFAULT char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_LOCATION ADD CONSTRAINT PK_B_SALE_LOCATION PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_LOCATION ADD CONSTRAINT DF_B_SALE_LOCATION_SORT DEFAULT '100' FOR SORT
GO
ALTER TABLE B_SALE_LOCATION ADD CONSTRAINT DF_B_SALE_LOCATION_PARENT_ID DEFAULT '0' FOR PARENT_ID
GO
ALTER TABLE B_SALE_LOCATION ADD CONSTRAINT DF_B_SALE_LOCATION_DEPTH_LEVEL DEFAULT '1' FOR DEPTH_LEVEL
GO
ALTER TABLE B_SALE_LOCATION ADD CONSTRAINT DF_B_SALE_LOCATION_LOC_DEFAULT DEFAULT 'N' FOR LOC_DEFAULT
GO
CREATE unique INDEX IX_B_SALE_LOC_CODE ON B_SALE_LOCATION(CODE)
GO
CREATE INDEX IX_B_SALE_LOC_MARGINS ON B_SALE_LOCATION(LEFT_MARGIN, RIGHT_MARGIN)
GO
CREATE INDEX IX_B_SALE_LOC_MARGINS_REV ON B_SALE_LOCATION(RIGHT_MARGIN, LEFT_MARGIN)
GO
CREATE INDEX IX_B_SALE_LOC_PARENT ON B_SALE_LOCATION(PARENT_ID)
GO
CREATE INDEX IX_B_SALE_LOC_DL ON B_SALE_LOCATION(DEPTH_LEVEL)
GO
CREATE INDEX IX_B_SALE_LOC_TYPE ON B_SALE_LOCATION(TYPE_ID)
GO
CREATE INDEX IXS_LOCATION_COUNTRY_ID ON B_SALE_LOCATION(COUNTRY_ID)
GO
CREATE INDEX IXS_LOCATION_CITY_ID ON B_SALE_LOCATION(CITY_ID)
GO
CREATE INDEX IXS_LOCATION_REGION_ID ON B_SALE_LOCATION(REGION_ID)
GO

CREATE TABLE B_SALE_LOC_NAME
(
	ID int NOT NULL IDENTITY (1, 1),
	LANGUAGE_ID char(2) NOT NULL,
	LOCATION_ID int NOT NULL,
	NAME varchar(100) NOT NULL,
	NAME_UPPER varchar(100) NOT NULL,
	SHORT_NAME varchar(100)
)
GO
ALTER TABLE B_SALE_LOC_NAME ADD CONSTRAINT PK_B_SALE_LOC_NAME PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_LOC_NAME_NAME_U ON B_SALE_LOC_NAME(NAME_UPPER)
GO
CREATE INDEX IX_B_SALE_LOC_NAME_LI_LI ON B_SALE_LOC_NAME(LOCATION_ID, LANGUAGE_ID)
GO

CREATE TABLE B_SALE_LOC_EXT_SRV
(
	ID int NOT NULL IDENTITY (1, 1),
	CODE varchar(100) NOT NULL
)
GO
ALTER TABLE B_SALE_LOC_EXT_SRV ADD CONSTRAINT PK_B_SALE_LOC_EXT_SRV PRIMARY KEY (ID)
GO

CREATE TABLE B_SALE_LOC_EXT
(
	ID int NOT NULL IDENTITY (1, 1),
	SERVICE_ID int NOT NULL,
	LOCATION_ID int NOT NULL,
	XML_ID varchar(100) NOT NULL
)
GO
ALTER TABLE B_SALE_LOC_EXT ADD CONSTRAINT PK_B_SALE_LOC_EXT PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_LOC_EXT_LID_SID ON B_SALE_LOC_EXT(LOCATION_ID, SERVICE_ID)
GO

CREATE TABLE B_SALE_LOC_TYPE
(
	ID int NOT NULL IDENTITY (1, 1),
	CODE varchar(30) NOT NULL,
	SORT int,
	DISPLAY_SORT int
)
GO
ALTER TABLE B_SALE_LOC_TYPE ADD CONSTRAINT PK_B_SALE_LOC_TYPE PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_LOC_TYPE ADD CONSTRAINT DF_B_SALE_LOC_TYPE_SORT DEFAULT '100' FOR SORT
GO
ALTER TABLE B_SALE_LOC_TYPE ADD CONSTRAINT DF_B_SALE_LOC_TYPE_D_SORT DEFAULT '100' FOR DISPLAY_SORT
GO

CREATE TABLE B_SALE_LOC_TYPE_NAME
(
	ID int NOT NULL IDENTITY (1, 1),
	LANGUAGE_ID char(2) NOT NULL,
	NAME varchar(100) NOT NULL,
	TYPE_ID int NOT NULL
)
GO
ALTER TABLE B_SALE_LOC_TYPE_NAME ADD CONSTRAINT PK_B_SALE_LOC_TYPE_NAME PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_LOC_TYPE_NAME_TI_LI ON B_SALE_LOC_TYPE_NAME(TYPE_ID, LANGUAGE_ID)
GO

CREATE TABLE B_SALE_LOC_2SITE
(
	LOCATION_ID int NOT NULL,
	SITE_ID char(2) NOT NULL,
	LOCATION_TYPE char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_LOC_2SITE ADD CONSTRAINT PK_B_SALE_LOC_2SITE PRIMARY KEY (SITE_ID, LOCATION_ID, LOCATION_TYPE)
GO
ALTER TABLE B_SALE_LOC_2SITE ADD CONSTRAINT DF_B_SALE_LOC_2SITE_LOCATION_TYPE DEFAULT 'L' FOR LOCATION_TYPE
GO

CREATE TABLE B_SALE_LOC_DEF2SITE(
	LOCATION_CODE varchar(100) NOT NULL,
	SITE_ID char(2) NOT NULL,
	SORT int
)
GO
ALTER TABLE B_SALE_LOC_DEF2SITE ADD CONSTRAINT PK_B_SALE_LOC_DEF2SITE PRIMARY KEY (LOCATION_CODE, SITE_ID)
GO
ALTER TABLE B_SALE_LOC_DEF2SITE ADD CONSTRAINT DF_B_SALE_LOC_DEF2SITE_SORT DEFAULT '100' FOR SORT
GO

CREATE TABLE B_SALE_LOCATION_GROUP
(
	ID int NOT NULL IDENTITY (1, 1),
	CODE varchar(100) NOT NULL,
	SORT int
)
GO
ALTER TABLE B_SALE_LOCATION_GROUP ADD CONSTRAINT PK_B_SALE_LOCATION_GROUP PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_LOCATION_GROUP ADD CONSTRAINT DF_B_SALE_LOCATION_GROUP_SORT DEFAULT '100' FOR SORT
GO
CREATE unique INDEX IX_B_SALE_LOC_GROUP_CODE ON B_SALE_LOCATION_GROUP(CODE)
GO

CREATE TABLE B_SALE_LOCATION_GROUP_LANG
(
	ID int NOT NULL IDENTITY (1, 1),
	LOCATION_GROUP_ID int NOT NULL,
	LID char(2) NOT NULL,
	NAME varchar(250) NOT NULL
)
GO
ALTER TABLE B_SALE_LOCATION_GROUP_LANG ADD CONSTRAINT PK_B_SALE_LOCATION_GROUP_LANG PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_LOCATION_GROUP_LANG ON B_SALE_LOCATION_GROUP_LANG(LOCATION_GROUP_ID, LID)
GO

CREATE TABLE B_SALE_LOCATION2LOCATION_GROUP
(
	LOCATION_ID int NOT NULL,
	LOCATION_GROUP_ID int NOT NULL
)
GO
ALTER TABLE B_SALE_LOCATION2LOCATION_GROUP ADD CONSTRAINT PK_B_SALE_LOCATION2LOCATION_GROUP PRIMARY KEY (LOCATION_ID, LOCATION_GROUP_ID)
GO

CREATE TABLE B_SALE_DELIVERY2LOCATION
(
	DELIVERY_ID int NOT NULL,
	LOCATION_CODE varchar(100) NOT NULL,
	LOCATION_TYPE char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_DELIVERY2LOCATION ADD CONSTRAINT PK_B_SALE_DELIVERY2LOCATION PRIMARY KEY (DELIVERY_ID, LOCATION_CODE, LOCATION_TYPE)
GO
ALTER TABLE B_SALE_DELIVERY2LOCATION ADD CONSTRAINT DF_B_SALE_DELIVERY2LOCATION_LOCATION_TYPE DEFAULT 'L' FOR LOCATION_TYPE
GO

CREATE TABLE B_SALE_DISCOUNT
(
	ID int NOT NULL IDENTITY (1, 1),
	XML_ID varchar(255) NULL,
	LID char(2) NOT NULL,
	NAME varchar(255) NULL,
	PRICE_FROM decimal(18, 2) NULL,	-- deprecated
	PRICE_TO decimal(18, 2) NULL,	-- deprecated
	CURRENCY char(3) NULL,
	DISCOUNT_VALUE decimal(18, 2) NOT NULL,	-- deprecated
	DISCOUNT_TYPE char(1) NOT NULL,	-- deprecated
	ACTIVE char(1) NOT NULL,
	SORT int NOT NULL,
	ACTIVE_FROM datetime NULL,
	ACTIVE_TO datetime NULL,
	TIMESTAMP_X datetime NULL,
	MODIFIED_BY int NULL,
	DATE_CREATE datetime NULL,
	CREATED_BY int NULL,
	PRIORITY int NOT NULL,
	LAST_DISCOUNT char(1) NOT NULL,
	VERSION int NOT NULL,
	CONDITIONS text NULL,
	UNPACK text NULL,
	ACTIONS text NULL,
	APPLICATION text NULL,
	USE_COUPONS char(1) NOT NULL,
	EXECUTE_MODULE varchar(50) NOT NULL
)
GO
ALTER TABLE B_SALE_DISCOUNT ADD CONSTRAINT PK_B_SALE_DISCOUNT PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_DISCOUNT ADD CONSTRAINT DF_B_SALE_DISCOUNT_DISCOUNT_TYPE DEFAULT 'P' FOR DISCOUNT_TYPE
GO
ALTER TABLE B_SALE_DISCOUNT ADD CONSTRAINT DF_B_SALE_DISCOUNT_ACTIVE DEFAULT 'Y' FOR ACTIVE
GO
ALTER TABLE B_SALE_DISCOUNT ADD CONSTRAINT DF_B_SALE_DISCOUNT_SORT DEFAULT '100' FOR SORT
GO
ALTER TABLE B_SALE_DISCOUNT ADD CONSTRAINT DF_B_SALE_DISCOUNT_PRIORITY DEFAULT '1' FOR PRIORITY
GO
ALTER TABLE B_SALE_DISCOUNT ADD CONSTRAINT DF_B_SALE_DISCOUNT_LAST_DISCOUNT DEFAULT 'Y' FOR LAST_DISCOUNT
GO
ALTER TABLE B_SALE_DISCOUNT ADD CONSTRAINT DF_B_SALE_DISCOUNT_VERSION DEFAULT '1' FOR VERSION
GO
ALTER TABLE B_SALE_DISCOUNT ADD CONSTRAINT DF_B_SALE_DISCOUNT_USE_COUPONS DEFAULT 'N' FOR USE_COUPONS
GO
ALTER TABLE B_SALE_DISCOUNT ADD CONSTRAINT DF_B_SALE_DISCOUNT_EXECUTE_MODULE DEFAULT 'all' FOR EXECUTE_MODULE
GO
CREATE INDEX IX_B_SALE_DISCOUNT_1 ON B_SALE_DISCOUNT(LID)
GO
CREATE INDEX IX_SSD_ACTIVE_DATE ON B_SALE_DISCOUNT(ACTIVE_FROM, ACTIVE_TO)
GO

CREATE TABLE B_SALE_DISCOUNT_COUPON
(
	ID int NOT NULL IDENTITY (1, 1),
	DISCOUNT_ID int NOT NULL,
	ACTIVE char(1) NOT NULL,
	ACTIVE_FROM datetime NULL,
	ACTIVE_TO datetime NULL,
	COUPON varchar(32) NOT NULL,
	TYPE int NOT NULL,
	MAX_USE int NOT NULL,
	USE_COUNT int NOT NULL,
	USER_ID int NOT NULL,
	DATE_APPLY datetime NULL,
	TIMESTAMP_X datetime NULL,
	MODIFIED_BY int NULL,
	DATE_CREATE datetime NULL,
	CREATED_BY int NULL,
	DESCRIPTION text NULL
)
GO

ALTER TABLE B_SALE_DISCOUNT_COUPON ADD CONSTRAINT PK_SALE_DSC_CPN PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_DISCOUNT_COUPON ADD CONSTRAINT DF_SALE_DSC_CPN_ACTIVE DEFAULT 'Y' FOR ACTIVE
GO
ALTER TABLE B_SALE_DISCOUNT_COUPON ADD CONSTRAINT DF_SALE_DSC_CPN_TYPE DEFAULT 0 FOR TYPE
GO
ALTER TABLE B_SALE_DISCOUNT_COUPON ADD CONSTRAINT DF_SALE_DSC_CPN_MAX_USE DEFAULT 0 FOR MAX_USE
GO
ALTER TABLE B_SALE_DISCOUNT_COUPON ADD CONSTRAINT DF_SALE_DSC_CPN_USE_COUNT DEFAULT 0 FOR USE_COUNT
GO
ALTER TABLE B_SALE_DISCOUNT_COUPON ADD CONSTRAINT DF_SALE_DSC_CPN_USER_ID DEFAULT 0 FOR USER_ID
GO

CREATE TABLE B_SALE_DISCOUNT_GROUP
(
	ID int NOT NULL IDENTITY (1, 1),
	DISCOUNT_ID int NOT NULL,
	ACTIVE char(1) NULL,
	GROUP_ID int NOT NULL
)
GO
ALTER TABLE B_SALE_DISCOUNT_GROUP ADD CONSTRAINT PK_B_SALE_DISCOUNT_GROUP PRIMARY KEY (ID)
GO
CREATE INDEX IX_S_DISGRP_D ON B_SALE_DISCOUNT_GROUP (DISCOUNT_ID)
GO
CREATE UNIQUE INDEX IX_S_DISGRP ON B_SALE_DISCOUNT_GROUP (DISCOUNT_ID, GROUP_ID)
GO
CREATE UNIQUE INDEX IX_S_DISGRP_G ON B_SALE_DISCOUNT_GROUP (GROUP_ID, DISCOUNT_ID)
GO

CREATE TABLE B_SALE_DISCOUNT_MODULE
(
	ID int NOT NULL IDENTITY (1, 1),
	DISCOUNT_ID int NOT NULL,
	MODULE_ID varchar(50) NOT NULL
)
GO
ALTER TABLE B_SALE_DISCOUNT_MODULE ADD CONSTRAINT PK_B_SALE_DISCOUNT_MODULE PRIMARY KEY (ID)
GO
CREATE INDEX IX_SALE_DSC_MOD ON B_SALE_DISCOUNT_MODULE(DISCOUNT_ID)
GO

CREATE TABLE B_SALE_DISCOUNT_ENTITIES
(
	ID int NOT NULL IDENTITY (1, 1),
	DISCOUNT_ID int NOT NULL,
	MODULE_ID varchar(50) NOT NULL,
	ENTITY varchar(255) NOT NULL,
	FIELD_ENTITY varchar(255) NOT NULL,
	FIELD_TABLE varchar(255) NOT NULL
)
GO
ALTER TABLE B_SALE_DISCOUNT_ENTITIES ADD CONSTRAINT PK_B_SALE_DISCOUNT_ENTITIES PRIMARY KEY (ID)
GO
CREATE INDEX IX_SALE_DSC_ENT_DISCOUNT_ID ON B_SALE_DISCOUNT_ENTITIES(DISCOUNT_ID)
GO

CREATE TABLE B_SALE_ORDER_DISCOUNT
(
	ID int NOT NULL IDENTITY (1, 1),
	MODULE_ID varchar(50) NOT NULL,
	DISCOUNT_ID int NOT NULL,
	NAME varchar(255) NOT NULL,
	DISCOUNT_HASH varchar(32) NOT NULL,
	CONDITIONS text NULL,
	UNPACK text NULL,
	ACTIONS text NULL,
	APPLICATION text NULL,
	USE_COUPONS char(1) NOT NULL,
	SORT int NOT NULL,
	PRIORITY int NOT NULL,
	LAST_DISCOUNT char(1) NOT NULL,
	ACTIONS_DESCR text NULL
)
GO
ALTER TABLE B_SALE_ORDER_DISCOUNT ADD CONSTRAINT PK_B_SALE_ORDER_DISCOUNT PRIMARY KEY (ID)
GO
CREATE INDEX IX_SALE_ORDER_DSC_HASH ON B_SALE_ORDER_DISCOUNT(DISCOUNT_HASH)
GO

CREATE TABLE B_SALE_ORDER_COUPONS
(
	ID int NOT NULL IDENTITY (1, 1),
	ORDER_ID int NOT NULL,
	ORDER_DISCOUNT_ID int NOT NULL,
	COUPON varchar(32) NOT NULL,
	TYPE int NOT NULL,
	COUPON_ID int NOT NULL,
	DATA text NULL
)
GO
ALTER TABLE B_SALE_ORDER_COUPONS ADD CONSTRAINT PK_B_SALE_ORDER_COUPONS PRIMARY KEY (ID)
GO
CREATE INDEX IX_SALE_ORDER_CPN_ORDER ON B_SALE_ORDER_COUPONS(ORDER_ID)
GO

CREATE TABLE B_SALE_ORDER_MODULES
(
	ID int NOT NULL IDENTITY (1, 1),
	ORDER_DISCOUNT_ID int NOT NULL,
	MODULE_ID varchar(50) NOT NULL
)
GO
ALTER TABLE B_SALE_ORDER_MODULES ADD CONSTRAINT PK_B_SALE_ORDER_MODULES PRIMARY KEY (ID)
GO
CREATE INDEX IX_SALE_ORDER_MDL_DSC ON B_SALE_ORDER_MODULES(ORDER_DISCOUNT_ID)
GO

CREATE TABLE B_SALE_ORDER_RULES
(
	ID int NOT NULL IDENTITY (1, 1),
	MODULE_ID varchar(50) NOT NULL,
	ORDER_DISCOUNT_ID int NOT NULL,
	ORDER_ID int NOT NULL,
	ENTITY_TYPE int NOT NULL,
	ENTITY_ID int NOT NULL,
	ENTITY_VALUE varchar(255) NULL,
	COUPON_ID int NOT NULL,
	APPLY char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_ORDER_RULES ADD CONSTRAINT PK_B_SALE_ORDER_RULES PRIMARY KEY (ID)
GO
CREATE INDEX IX_SALE_ORDER_RULES_ORD ON B_SALE_ORDER_RULES(ORDER_ID)
GO

CREATE TABLE B_SALE_ORDER_DISCOUNT_DATA
(
	ID int NOT NULL IDENTITY (1, 1),
	ORDER_ID int NOT NULL,
	ENTITY_TYPE int NOT NULL,
	ENTITY_ID int NOT NULL,
	ENTITY_VALUE varchar(255) NULL,
	ENTITY_DATA text NOT NULL
)
GO
ALTER TABLE B_SALE_ORDER_DISCOUNT_DATA ADD CONSTRAINT PK_B_SALE_ORDER_DISCOUNT_DATA PRIMARY KEY (ID)
GO
CREATE INDEX IX_SALE_DSC_DATA_CMX ON B_SALE_ORDER_DISCOUNT_DATA(ORDER_ID, ENTITY_TYPE)
GO

CREATE TABLE B_SALE_ORDER_RULES_DESCR
(
	ID int NOT NULL IDENTITY (1, 1),
	MODULE_ID varchar(50) NOT NULL,
	ORDER_DISCOUNT_ID int NOT NULL,
	ORDER_ID int NOT NULL,
	RULE_ID int NOT NULL,
	DESCR text NOT NULL
)
GO
ALTER TABLE B_SALE_ORDER_RULES_DESCR ADD CONSTRAINT PK_B_SALE_ORDER_RULES_DESCR PRIMARY KEY (ID)
GO
CREATE INDEX IX_SALE_ORDER_RULES_DS_ORD ON B_SALE_ORDER_RULES_DESCR(ORDER_ID)
GO

CREATE TABLE B_SALE_USER_PROPS
(
	ID int NOT NULL IDENTITY (1, 1),
	NAME varchar(255) NOT NULL,
	USER_ID int NOT NULL,
	PERSON_TYPE_ID int NOT NULL,
	DATE_UPDATE datetime NOT NULL,
	XML_ID varchar(50) NULL,
	VERSION_1C varchar(15) NULL
)
GO
ALTER TABLE B_SALE_USER_PROPS ADD CONSTRAINT PK_B_SALE_USER_PROPS PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_USER_PROPS_1 ON B_SALE_USER_PROPS(USER_ID)
GO
CREATE INDEX IX_B_SALE_USER_PROPS_2 ON B_SALE_USER_PROPS(PERSON_TYPE_ID)
GO
CREATE INDEX IX_B_SALE_USER_PROPS_3 ON B_SALE_USER_PROPS(XML_ID)
GO

CREATE TABLE B_SALE_USER_PROPS_VALUE
(
	ID int NOT NULL IDENTITY (1, 1),
	USER_PROPS_ID int NOT NULL,
	ORDER_PROPS_ID int NOT NULL,
	NAME varchar(255) NOT NULL,
	VALUE varchar(255) NULL
)
GO
ALTER TABLE B_SALE_USER_PROPS_VALUE ADD CONSTRAINT PK_B_SALE_USER_PROPS_VALUE PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_USER_PROPS_VALUE_1 ON B_SALE_USER_PROPS_VALUE(USER_PROPS_ID)
GO
CREATE INDEX IX_B_SALE_USER_PROPS_VALUE_2 ON B_SALE_USER_PROPS_VALUE(ORDER_PROPS_ID)
GO

CREATE TABLE B_SALE_STATUS
(
	ID char(2) NOT NULL,
	TYPE CHAR(1) NOT NULL,
	SORT int NOT NULL,
	NOTIFY char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_STATUS ADD CONSTRAINT PK_B_SALE_STATUS PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_STATUS ADD CONSTRAINT DF_B_SALE_STATUS_TYPE DEFAULT 'O' FOR TYPE
GO
ALTER TABLE B_SALE_STATUS ADD CONSTRAINT DF_B_SALE_STATUS_SORT DEFAULT '100' FOR SORT
GO
ALTER TABLE B_SALE_STATUS ADD CONSTRAINT DF_B_SALE_STATUS_NOTIFY DEFAULT 'Y' FOR NOTIFY
GO

CREATE TABLE B_SALE_STATUS_LANG
(
	STATUS_ID char(2) NOT NULL,
	LID char(2) NOT NULL,
	NAME varchar(100) NOT NULL,
	DESCRIPTION varchar(250) NULL
)
GO
ALTER TABLE B_SALE_STATUS_LANG ADD CONSTRAINT PK_B_SALE_STATUS_LANG PRIMARY KEY (STATUS_ID, LID)
GO

CREATE TABLE B_SALE_STATUS_GROUP_TASK
(
	STATUS_ID char(2) NOT NULL,
	GROUP_ID  int     NOT NULL,
	TASK_ID   int     NOT NULL
)
GO
ALTER TABLE B_SALE_STATUS_GROUP_TASK ADD CONSTRAINT PK_B_SALE_STATUS_GROUP_TASK PRIMARY KEY (STATUS_ID, GROUP_ID, TASK_ID)
GO

CREATE TABLE B_SALE_TAX
(
	ID int NOT NULL IDENTITY (1, 1),
	LID char(2) NOT NULL,
	NAME varchar(250) NOT NULL,
	DESCRIPTION varchar(255) NULL,
	TIMESTAMP_X datetime NOT NULL,
	CODE varchar(50) NULL
)
GO
ALTER TABLE B_SALE_TAX ADD CONSTRAINT PK_B_SALE_TAX PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_TAX_1 ON B_SALE_TAX(LID)
GO

CREATE TABLE B_SALE_ORDER_TAX
(
	ID int NOT NULL IDENTITY (1, 1),
	ORDER_ID int NOT NULL,
	TAX_NAME varchar(255) NOT NULL,
	VALUE decimal(18,2) NULL,
	VALUE_MONEY decimal(18,2) NOT NULL,
	APPLY_ORDER int NOT NULL,
	CODE varchar(50) NULL,
	IS_PERCENT char(1) NOT NULL,
	IS_IN_PRICE char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_ORDER_TAX ADD CONSTRAINT PK_B_SALE_ORDER_TAX PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_ORDER_TAX ON B_SALE_ORDER_TAX(ORDER_ID)
GO
ALTER TABLE B_SALE_ORDER_TAX ADD CONSTRAINT DF_B_SALE_ORDER_TAX_IS_PERCENT DEFAULT 'Y' FOR IS_PERCENT
GO
ALTER TABLE B_SALE_ORDER_TAX ADD CONSTRAINT DF_B_SALE_ORDER_TAX_IS_IN_PRICE DEFAULT 'N' FOR IS_IN_PRICE
GO

CREATE TABLE B_SALE_TAX_RATE
(
	ID int NOT NULL IDENTITY (1, 1),
	TAX_ID int NOT NULL,
	PERSON_TYPE_ID int NULL,
	VALUE decimal(18,4) NOT NULL,
	CURRENCY char(3) NULL,
	IS_PERCENT char(1) NOT NULL,
	IS_IN_PRICE char(1) NOT NULL,
	APPLY_ORDER int NOT NULL,
	TIMESTAMP_X datetime NOT NULL,
	ACTIVE char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_TAX_RATE ADD CONSTRAINT PK_B_SALE_TAX_RATE PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_TAX_RATE ADD CONSTRAINT DF_B_SALE_TAX_RATE_IS_PERCENT DEFAULT 'Y' FOR IS_PERCENT
GO
ALTER TABLE B_SALE_TAX_RATE ADD CONSTRAINT DF_B_SALE_TAX_RATE_IS_IN_PRICE DEFAULT 'N' FOR IS_IN_PRICE
GO
ALTER TABLE B_SALE_TAX_RATE ADD CONSTRAINT DF_B_SALE_TAX_RATE_APPLY_ORDER DEFAULT '100' FOR APPLY_ORDER
GO
ALTER TABLE B_SALE_TAX_RATE ADD CONSTRAINT DF_B_SALE_TAX_RATE_ACTIVE DEFAULT 'Y' FOR ACTIVE
GO
ALTER TABLE B_SALE_TAX_RATE ADD CONSTRAINT DF_B_SALE_TAX_RATE_TIMESTAMP_X DEFAULT GETDATE() FOR TIMESTAMP_X
GO
CREATE INDEX IX_B_SALE_TAX_RATE_1 ON B_SALE_TAX_RATE(PERSON_TYPE_ID)
GO
CREATE INDEX IX_B_SALE_TAX_RATE_2 ON B_SALE_TAX_RATE(TAX_ID)
GO
CREATE INDEX IX_B_SALE_TAX_RATE_3 ON B_SALE_TAX_RATE(IS_IN_PRICE)
GO
CREATE TRIGGER B_SALE_TAX_RATE_UPDATE on B_SALE_TAX_RATE for update as
if (not update(TIMESTAMP_X))
begin
	UPDATE B_SALE_TAX_RATE SET
		TIMESTAMP_X = GETDATE()
	FROM
		B_SALE_TAX_RATE U,
		INSERTED I
	WHERE
		U.ID = I.ID
end
GO

CREATE TABLE B_SALE_TAX2LOCATION
(
	TAX_RATE_ID int NOT NULL,
	LOCATION_CODE varchar(100) NOT NULL,
	LOCATION_TYPE char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_TAX2LOCATION ADD CONSTRAINT PK_B_SALE_TAX2LOCATION PRIMARY KEY (TAX_RATE_ID, LOCATION_CODE, LOCATION_TYPE)
GO
ALTER TABLE B_SALE_TAX2LOCATION ADD CONSTRAINT DF_B_SALE_TAX2LOCATION_LOCATION_TYPE DEFAULT 'L' FOR LOCATION_TYPE
GO

CREATE TABLE B_SALE_TAX_EXEMPT2GROUP
(
	GROUP_ID int NOT NULL,
	TAX_ID int NOT NULL
)
GO
ALTER TABLE B_SALE_TAX_EXEMPT2GROUP ADD CONSTRAINT PK_B_SALE_TAX_EXEMPT2GROUP PRIMARY KEY (GROUP_ID, TAX_ID)
GO

CREATE TABLE B_SALE_ORDER_FLAGS2GROUP
(
	ID int NOT NULL IDENTITY (1, 1),
	GROUP_ID int NOT NULL,
	ORDER_FLAG char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_ORDER_FLAGS2GROUP ADD CONSTRAINT PK_B_SALE_ORDER_FLAGS2GROUP PRIMARY KEY (ID)
GO
CREATE UNIQUE INDEX UX_B_SALE_ORDER_FLAGS2GROUP_1 ON B_SALE_ORDER_FLAGS2GROUP(GROUP_ID, ORDER_FLAG)
GO

CREATE TABLE B_SALE_SITE2GROUP
(
	ID int NOT NULL IDENTITY (1, 1),
	GROUP_ID int NOT NULL,
	SITE_ID char(2) NOT NULL
)
GO
ALTER TABLE B_SALE_SITE2GROUP ADD CONSTRAINT PK_B_SALE_SITE2GROUP PRIMARY KEY (ID)
GO
CREATE UNIQUE INDEX UX_B_SALE_SITE2GROUP_1 ON B_SALE_SITE2GROUP(GROUP_ID, SITE_ID)
GO

CREATE TABLE B_SALE_USER_ACCOUNT
(
	ID int NOT NULL IDENTITY (1, 1),
	USER_ID int NOT NULL,
	TIMESTAMP_X datetime NOT NULL,
	CURRENT_BUDGET decimal(18,4) NOT NULL,
	CURRENCY char(3) NOT NULL,
	LOCKED char(1) NOT NULL,
	DATE_LOCKED datetime NULL,
	NOTES text NULL
)
GO
ALTER TABLE B_SALE_USER_ACCOUNT ADD CONSTRAINT PK_B_SALE_USER_ACCOUNT PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_USER_ACCOUNT ADD CONSTRAINT DF_B_SALE_USER_ACCOUNT_TIMESTAMP_X DEFAULT GETDATE() FOR TIMESTAMP_X
GO
ALTER TABLE B_SALE_USER_ACCOUNT ADD CONSTRAINT DF_B_SALE_USER_ACCOUNT_CURRENT_BUDGET DEFAULT '0' FOR CURRENT_BUDGET
GO
ALTER TABLE B_SALE_USER_ACCOUNT ADD CONSTRAINT DF_B_SALE_USER_ACCOUNT_LOCKED DEFAULT 'N' FOR LOCKED
GO
CREATE UNIQUE INDEX IX_B_SALE_USER_ACCOUNT_1 ON B_SALE_USER_ACCOUNT(USER_ID, CURRENCY)
GO
CREATE TRIGGER B_SALE_USER_ACCOUNT_UPDATE on B_SALE_USER_ACCOUNT for update as
if (not update(TIMESTAMP_X))
begin
	UPDATE B_SALE_USER_ACCOUNT SET
		TIMESTAMP_X = GETDATE()
	FROM
		B_SALE_USER_ACCOUNT U,
		INSERTED I
	WHERE
		U.ID = I.ID
end
GO

CREATE TABLE B_SALE_RECURRING
(
	ID int NOT NULL IDENTITY (1, 1),
	USER_ID int NOT NULL,
	TIMESTAMP_X datetime NOT NULL,
	MODULE varchar(100) NULL,
	PRODUCT_ID int NULL,
	PRODUCT_NAME varchar(255) NULL,
	PRODUCT_URL varchar(255) NULL,
	PRODUCT_PRICE_ID int NULL,
	PRICE_TYPE char(1) NOT NULL,
	RECUR_SCHEME_TYPE char(1) NOT NULL,
	RECUR_SCHEME_LENGTH int NOT NULL,
	WITHOUT_ORDER char(1) NOT NULL,
	PRICE decimal(18,4) NOT NULL,
	CURRENCY char(3) NULL,
	CANCELED char(1) NOT NULL,
	DATE_CANCELED datetime NULL,
	PRIOR_DATE datetime NULL,
	NEXT_DATE datetime NOT NULL,
	CALLBACK_FUNC varchar(100) NULL,
	PRODUCT_PROVIDER_CLASS varchar(100) NULL,
	DESCRIPTION varchar(255) NULL,
	CANCELED_REASON varchar(255) NULL,
	ORDER_ID int NOT NULL,
	REMAINING_ATTEMPTS int NOT NULL,
	SUCCESS_PAYMENT char(1) NOT NULL
)
GO
ALTER TABLE B_SALE_RECURRING ADD CONSTRAINT PK_B_SALE_RECURRING PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_RECURRING ADD CONSTRAINT DF_B_SALE_RECURRING_TIMESTAMP_X DEFAULT GETDATE() FOR TIMESTAMP_X
GO
ALTER TABLE B_SALE_RECURRING ADD CONSTRAINT DF_B_SALE_RECURRING_PRICE_TYPE DEFAULT 'R' FOR PRICE_TYPE
GO
ALTER TABLE B_SALE_RECURRING ADD CONSTRAINT DF_B_SALE_RECURRING_RECUR_SCHEME_TYPE DEFAULT 'M' FOR RECUR_SCHEME_TYPE
GO
ALTER TABLE B_SALE_RECURRING ADD CONSTRAINT DF_B_SALE_RECURRING_RECUR_SCHEME_LENGTH DEFAULT '0' FOR RECUR_SCHEME_LENGTH
GO
ALTER TABLE B_SALE_RECURRING ADD CONSTRAINT DF_B_SALE_RECURRING_WITHOUT_ORDER DEFAULT 'N' FOR WITHOUT_ORDER
GO
ALTER TABLE B_SALE_RECURRING ADD CONSTRAINT DF_B_SALE_RECURRING_PRICE DEFAULT '0' FOR PRICE
GO
ALTER TABLE B_SALE_RECURRING ADD CONSTRAINT DF_B_SALE_RECURRING_CANCELED DEFAULT 'N' FOR CANCELED
GO
ALTER TABLE B_SALE_RECURRING ADD CONSTRAINT DF_B_SALE_RECURRING_REMAINING_ATTEMPTS DEFAULT '0' FOR REMAINING_ATTEMPTS
GO
ALTER TABLE B_SALE_RECURRING ADD CONSTRAINT DF_B_SALE_RECURRING_SUCCESS_PAYMENT DEFAULT 'Y' FOR SUCCESS_PAYMENT
GO
CREATE INDEX IX_B_SALE_RECURRING_1 ON B_SALE_RECURRING(USER_ID)
GO
CREATE INDEX IX_B_SALE_RECURRING_2 ON B_SALE_RECURRING(NEXT_DATE, CANCELED, REMAINING_ATTEMPTS)
GO
CREATE INDEX IX_B_SALE_RECURRING_3 ON B_SALE_RECURRING(MODULE, PRODUCT_ID, PRODUCT_PRICE_ID)
GO
CREATE TRIGGER B_SALE_RECURRING_UPDATE on B_SALE_RECURRING for update as
if (not update(TIMESTAMP_X))
begin
	UPDATE B_SALE_RECURRING SET
		TIMESTAMP_X = GETDATE()
	FROM
		B_SALE_RECURRING U,
		INSERTED I
	WHERE
		U.ID = I.ID
end
GO

CREATE TABLE B_SALE_USER_CARDS
(
	ID int NOT NULL IDENTITY (1, 1),
	USER_ID int NOT NULL,
	ACTIVE char(1) NOT NULL,
	SORT int NOT NULL,
	TIMESTAMP_X datetime NOT NULL,
	PAY_SYSTEM_ACTION_ID int NOT NULL,
	CURRENCY char(3) NULL,
	CARD_TYPE varchar(20) NOT NULL,
	CARD_NUM text NOT NULL,
	CARD_CODE varchar(5) NULL,
	CARD_EXP_MONTH int NOT NULL,
	CARD_EXP_YEAR int NOT NULL,
	DESCRIPTION varchar(255) NULL,
	SUM_MIN decimal(18,4) NULL,
	SUM_MAX decimal(18,4) NULL,
	SUM_CURRENCY char(3) NULL,
	LAST_STATUS char(1) NULL,
	LAST_STATUS_CODE varchar(5) NULL,
	LAST_STATUS_DESCRIPTION varchar(250) NULL,
	LAST_STATUS_MESSAGE varchar(255) NULL,
	LAST_SUM decimal(18,4) NULL,
	LAST_CURRENCY char(3) NULL,
	LAST_DATE datetime NULL
)
GO
ALTER TABLE B_SALE_USER_CARDS ADD CONSTRAINT PK_B_SALE_USER_CARDS PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_USER_CARDS ADD CONSTRAINT DF_B_SALE_USER_CARDS_ACTIVE DEFAULT 'Y' FOR ACTIVE
GO
ALTER TABLE B_SALE_USER_CARDS ADD CONSTRAINT DF_B_SALE_USER_CARDS_SORT DEFAULT '100' FOR SORT
GO
ALTER TABLE B_SALE_USER_CARDS ADD CONSTRAINT DF_B_SALE_USER_CARDS_TIMESTAMP_X DEFAULT GETDATE() FOR TIMESTAMP_X
GO
CREATE INDEX IX_B_SALE_USER_CARDS_1 ON B_SALE_USER_CARDS(USER_ID, ACTIVE, CURRENCY)
GO
CREATE TRIGGER B_SALE_USER_CARDS_UPDATE on B_SALE_USER_CARDS for update as
if (not update(TIMESTAMP_X))
begin
	UPDATE B_SALE_USER_CARDS SET
		TIMESTAMP_X = GETDATE()
	FROM
		B_SALE_USER_CARDS U,
		INSERTED I
	WHERE
		U.ID = I.ID
end
GO

CREATE TABLE B_SALE_USER_TRANSACT
(
	ID int NOT NULL IDENTITY (1, 1),
	USER_ID int NOT NULL,
	TIMESTAMP_X datetime NOT NULL,
	TRANSACT_DATE datetime NOT NULL,
	AMOUNT decimal(18,4) NOT NULL,
	CURRENCY char(3) NOT NULL,
	DEBIT char(1) NOT NULL,
	ORDER_ID int NULL,
	PAYMENT_ID INT NULL,
	DESCRIPTION varchar(255) NOT NULL,
	NOTES text NULL,
	EMPLOYEE_ID int NULL
)
GO
ALTER TABLE B_SALE_USER_TRANSACT ADD CONSTRAINT PK_B_SALE_USER_TRANSACT PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_USER_TRANSACT ADD CONSTRAINT DF_B_SALE_USER_TRANSACT_TIMESTAMP_X DEFAULT GETDATE() FOR TIMESTAMP_X
GO
ALTER TABLE B_SALE_USER_TRANSACT ADD CONSTRAINT DF_B_SALE_USER_TRANSACT_AMOUNT DEFAULT '0' FOR AMOUNT
GO
ALTER TABLE B_SALE_USER_TRANSACT ADD CONSTRAINT DF_B_SALE_USER_TRANSACT_DEBIT DEFAULT 'N' FOR DEBIT
GO
CREATE INDEX IX_B_SALE_USER_TRANSACT_1 ON B_SALE_USER_TRANSACT(USER_ID)
GO
CREATE INDEX IX_B_SALE_USER_TRANSACT_4 ON B_SALE_USER_TRANSACT(ORDER_ID)
GO
CREATE INDEX IX_B_SALE_USER_TRANSACT_5 ON B_SALE_USER_TRANSACT(USER_ID, CURRENCY)
GO
CREATE INDEX IX_S_U_T_PAYMENT_ID ON B_SALE_USER_TRANSACT(PAYMENT_ID)
GO
CREATE TRIGGER B_SALE_USER_TRANSACT_UPDATE on B_SALE_USER_TRANSACT for update as
if (not update(TIMESTAMP_X))
begin
	UPDATE B_SALE_USER_TRANSACT SET
		TIMESTAMP_X = GETDATE()
	FROM
		B_SALE_USER_TRANSACT U,
		INSERTED I
	WHERE
		U.ID = I.ID
end
GO

create table B_SALE_AFFILIATE_PLAN
(
  ID int not null IDENTITY (1, 1)
,  SITE_ID char(2) not null
,  NAME varchar(250) not null
,  DESCRIPTION text null
,  TIMESTAMP_X datetime not null
,  ACTIVE char(1) not null default 'Y'
,  BASE_RATE decimal(18,4) not null default '0'
,  BASE_RATE_TYPE char(1) not null default 'P'
,  BASE_RATE_CURRENCY char(3) null
,  MIN_PAY decimal(18,4) not null default '0'
,  MIN_PLAN_VALUE decimal(18,4) null
,  VALUE_CURRENCY char(3) null
)
GO
ALTER TABLE B_SALE_AFFILIATE_PLAN ADD CONSTRAINT PK_B_SALE_AFFILIATE_PLAN PRIMARY KEY (ID)
GO

CREATE TABLE B_SALE_AFFILIATE
(
  ID int not null IDENTITY (1, 1)
,  SITE_ID char(2) not null
,  USER_ID int not null
,  AFFILIATE_ID int null
,  PLAN_ID int not null
,  ACTIVE char(1) not null default 'Y'
,  TIMESTAMP_X datetime not null
,  DATE_CREATE datetime not null
,  PAID_SUM decimal(18,4) not null default '0'
,  APPROVED_SUM decimal(18,4) not null default '0'
,  PENDING_SUM decimal(18,4) not null default '0'
,  ITEMS_NUMBER int not null default '0'
,  ITEMS_SUM decimal(18,4) not null default '0'
,  LAST_CALCULATE datetime null
,  AFF_SITE varchar(200) null
,  AFF_DESCRIPTION text null
,  FIX_PLAN char(1) not null default 'N'
)
GO
ALTER TABLE B_SALE_AFFILIATE ADD CONSTRAINT PK_B_SALE_AFFILIATE PRIMARY KEY (ID)
GO
CREATE UNIQUE INDEX IX_SAA_USER_ID ON B_SALE_AFFILIATE(USER_ID, SITE_ID)
GO
CREATE INDEX IX_SAA_AFFILIATE_ID ON B_SALE_AFFILIATE(AFFILIATE_ID)
GO

CREATE TABLE B_SALE_AFFILIATE_PLAN_SECTION
(
  ID int not null IDENTITY (1, 1)
,  PLAN_ID int not null
,  MODULE_ID varchar(50) not null default 'catalog'
,  SECTION_ID varchar(255) not null
,  RATE decimal(18,4) not null default '0'
,  RATE_TYPE char(1) not null default 'P'
,  RATE_CURRENCY char(3) null
)
GO
ALTER TABLE B_SALE_AFFILIATE_PLAN_SECTION ADD CONSTRAINT PK_B_SALE_AFFILIATE_PLAN_SECTION PRIMARY KEY (ID)
GO
CREATE UNIQUE INDEX IX_SAP_PLAN_ID ON B_SALE_AFFILIATE_PLAN_SECTION(PLAN_ID, MODULE_ID, SECTION_ID)
GO

CREATE TABLE B_SALE_AFFILIATE_TIER
(
  ID int not null IDENTITY (1, 1)
,  SITE_ID char(2) not null
,  RATE1 decimal(18,4) not null default '0'
,  RATE2 decimal(18,4) not null default '0'
,  RATE3 decimal(18,4) not null default '0'
,  RATE4 decimal(18,4) not null default '0'
,  RATE5 decimal(18,4) not null default '0'
)
GO
ALTER TABLE B_SALE_AFFILIATE_TIER ADD CONSTRAINT PK_B_SALE_AFFILIATE_TIER PRIMARY KEY (ID)
GO
CREATE unique INDEX IX_SAT_SITE_ID ON B_SALE_AFFILIATE_TIER(SITE_ID)
GO

CREATE TABLE B_SALE_AFFILIATE_TRANSACT
(
  ID int not null IDENTITY (1, 1)
,  AFFILIATE_ID int not null
,  TIMESTAMP_X datetime not null
,  TRANSACT_DATE datetime not null
,  AMOUNT decimal(18,4) not null
,  CURRENCY char(3) not null
,  DEBIT char(1) not null default 'N'
,  DESCRIPTION varchar(100) not null
,  EMPLOYEE_ID int null
)
GO
ALTER TABLE B_SALE_AFFILIATE_TRANSACT ADD CONSTRAINT PK_B_SALE_AFFILIATE_TRANSACT PRIMARY KEY (ID)
GO
CREATE INDEX IX_SAT_AFFILIATE_ID ON B_SALE_AFFILIATE_TRANSACT(AFFILIATE_ID)
GO

CREATE TABLE B_SALE_EXPORT
(
	ID int NOT NULL IDENTITY (1, 1),
	PERSON_TYPE_ID int NOT NULL,
	VARS text NULL
)
GO
ALTER TABLE B_SALE_EXPORT ADD CONSTRAINT PK_B_SALE_EXPORT PRIMARY KEY (ID)
GO

CREATE TABLE B_SALE_ORDER_DELIVERY (
	ID int NOT NULL IDENTITY(1, 1),
	ORDER_ID int NOT NULL,
	DATE_REQUEST datetime NULL,
	DELIVERY_LOCATION varchar(50) NOT NULL DEFAULT '',
	PARAMS text
);
GO
ALTER TABLE B_SALE_ORDER_DELIVERY ADD CONSTRAINT PK_B_SALE_ORDER_DELIVERY PRIMARY KEY (ID)
GO
CREATE INDEX IX_BSOD_ORDER_ID ON B_SALE_ORDER_DELIVERY (ORDER_ID)
GO

CREATE TABLE B_SALE_PRODUCT2PRODUCT
(
	ID int NOT NULL IDENTITY (1, 1),
	PRODUCT_ID int NOT NULL,
	PARENT_PRODUCT_ID int NOT NULL,
	CNT int NOT NULL
)
GO
ALTER TABLE B_SALE_PRODUCT2PRODUCT ADD CONSTRAINT PK_B_SALE_PRODUCT2PRODUCT PRIMARY KEY (ID)
GO
CREATE INDEX IX_B_SALE_PRODUCT2PRODUCT_1 ON B_SALE_PRODUCT2PRODUCT(PRODUCT_ID)
GO

CREATE TABLE B_SALE_PERSON_TYPE_SITE (
	PERSON_TYPE_ID int NOT NULL,
	SITE_ID char(2) NOT NULL
)
GO
CREATE UNIQUE INDEX IX_SAP_PERSON_TYPE_ID ON B_SALE_PERSON_TYPE_SITE(PERSON_TYPE_ID, SITE_ID)
GO

CREATE TABLE B_SALE_VIEWED_PRODUCT (
	ID int NOT NULL IDENTITY (1, 1),
	FUSER_ID int NOT NULL,
	DATE_VISIT datetime NOT NULL,
	PRODUCT_ID int NOT NULL,
	MODULE varchar(100) NULL,
	LID char(2) NOT NULL,
	NAME varchar(255) NOT NULL,
	DETAIL_PAGE_URL varchar(255) NULL,
	CURRENCY char(3) NULL,
	PRICE decimal(18,2) NOT NULL,
	NOTES varchar(255) NULL,
	PREVIEW_PICTURE int NULL,
	DETAIL_PICTURE int NULL,
	CALLBACK_FUNC varchar(45) NULL,
	PRODUCT_PROVIDER_CLASS varchar(100) NULL,
)
GO
ALTER TABLE B_SALE_VIEWED_PRODUCT ADD CONSTRAINT PK_B_SALE_VIEWED_PRODUCT PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_VIEWED_PRODUCT ADD CONSTRAINT DF_B_SALE_VIEWED_PRODUCT_FUSER DEFAULT '0' FOR FUSER_ID
GO
ALTER TABLE B_SALE_VIEWED_PRODUCT ADD CONSTRAINT DF_B_SALE_VIEWED_PRODUCT_PRODUCT DEFAULT '0' FOR PRODUCT_ID
GO
ALTER TABLE B_SALE_VIEWED_PRODUCT ADD CONSTRAINT DF_B_SALE_VIEWED_PRODUCT_PRICE DEFAULT '0.00' FOR PRICE
GO
CREATE INDEX ixLID ON B_SALE_VIEWED_PRODUCT(FUSER_ID,LID)
GO
CREATE INDEX ixPRODUCT_ID ON B_SALE_VIEWED_PRODUCT(PRODUCT_ID)
GO
CREATE INDEX ixDATE_VISIT ON B_SALE_VIEWED_PRODUCT(DATE_VISIT)
GO

CREATE TABLE B_SALE_ORDER_HISTORY
(
	ID int NOT NULL IDENTITY (1, 1),
	H_USER_ID int NOT NULL,
	H_DATE_INSERT datetime NOT NULL,
	H_ORDER_ID int NOT NULL,
	H_CURRENCY char(3) NOT NULL,
	PERSON_TYPE_ID int NULL,
	PAYED char(1) NULL,
	DATE_PAYED datetime NULL,
	EMP_PAYED_ID int NULL,
	CANCELED char(1) NULL,
	DATE_CANCELED datetime NULL,
	REASON_CANCELED varchar(255) NULL,
	STATUS_ID char(2) NULL,
	DATE_STATUS datetime NULL,
	EMP_STATUS_ID int NULL,
	PRICE_DELIVERY decimal(18,2) NULL,
	ALLOW_DELIVERY char(1) NULL,
	DATE_ALLOW_DELIVERY datetime NULL,
	EMP_ALLOW_DELIVERY_ID int NULL,
	RESERVED char(1) NULL,
	DEDUCTED char(1) NULL,
	DATE_DEDUCTED datetime NULL,
	REASON_UNDO_DEDUCTED varchar(255) NULL,
	MARKED char(1) NULL,
	DATE_MARKED datetime NULL,
	REASON_MARKED varchar(255) NULL,
	PRICE decimal(18, 2) NULL,
	CURRENCY char(3) NULL,
	DISCOUNT_VALUE decimal(18,2) NULL,
	USER_ID int NULL,
	PAY_SYSTEM_ID int NULL,
	DELIVERY_ID varchar(50) NULL,
	PS_STATUS char(1) NULL,
	PS_STATUS_CODE char(5) NULL,
	PS_STATUS_DESCRIPTION varchar(250) NULL,
	PS_STATUS_MESSAGE varchar(250) NULL,
	PS_SUM decimal(18,2) NULL,
	PS_CURRENCY char(3) NULL,
	PS_RESPONSE_DATE datetime NULL,
	TAX_VALUE decimal(18,2) NULL,
	STAT_GID varchar(255) NULL,
	SUM_PAID decimal(18,2) NULL,
	PAY_VOUCHER_NUM varchar(20) NULL,
	PAY_VOUCHER_DATE datetime NULL,
	AFFILIATE_ID int null,
	DELIVERY_DOC_NUM varchar(20) NULL,
	DELIVERY_DOC_DATE datetime NULL,
)
GO
ALTER TABLE B_SALE_ORDER_HISTORY ADD CONSTRAINT PK_B_SALE_ORDER_HISTORY PRIMARY KEY (ID)
GO
ALTER TABLE B_SALE_ORDER_HISTORY ADD CONSTRAINT DF_B_SALE_ORDER_HISTORY_MARKED DEFAULT 'N' FOR MARKED
GO
CREATE INDEX ixH_ORDER_ID ON B_SALE_ORDER_HISTORY(H_ORDER_ID)
GO

CREATE TABLE B_SALE_DELIVERY2PAYSYSTEM (
	DELIVERY_ID int NOT NULL,
	LINK_DIRECTION char(1) NOT NULL,
	PAYSYSTEM_ID int NOT NULL
)
GO
CREATE INDEX IX_DELIVERY ON B_SALE_DELIVERY2PAYSYSTEM (DELIVERY_ID)
GO
CREATE INDEX IX_PAYSYSTEM ON B_SALE_DELIVERY2PAYSYSTEM (PAYSYSTEM_ID)
GO
CREATE INDEX IX_LINK_DIRECTION ON B_SALE_DELIVERY2PAYSYSTEM (LINK_DIRECTION)
GO

CREATE TABLE B_SALE_STORE_BARCODE
(
	ID int NOT NULL IDENTITY (1, 1),
	BASKET_ID INT NOT NULL,
	BARCODE VARCHAR(100) NULL,
	STORE_ID INT NOT NULL,
	QUANTITY FLOAT NOT NULL,
	DATE_MODIFY DATETIME NULL,
	DATE_CREATE DATETIME NULL,
	CREATED_BY INT NULL,
	MODIFIED_BY INT NULL,
	ORDER_DELIVERY_BASKET_ID INT NOT NULL DEFAULT 0
)
GO
ALTER TABLE B_SALE_STORE_BARCODE ADD CONSTRAINT PK_B_SALE_STORE_BARCODE PRIMARY KEY (ID)
GO
CREATE INDEX IX_BSSB_O_DLV_BASKET_ID ON b_sale_store_barcode(ORDER_DELIVERY_BASKET_ID)
GO

CREATE TABLE B_SALE_ORDER_CHANGE
(
	ID INT NOT NULL IDENTITY (1, 1),
	ORDER_ID INT NOT NULL,
	TYPE VARCHAR(255) NOT NULL,
	DATA VARCHAR(512) NULL,
	DATE_CREATE DATETIME NOT NULL,
	DATE_MODIFY DATETIME NOT NULL,
	USER_ID INT NOT NULL,
)
GO
ALTER TABLE B_SALE_ORDER_CHANGE ADD CONSTRAINT PK_B_SALE_ORDER_CHANGE PRIMARY KEY (ID)
GO
CREATE INDEX IXS_ORDER_ID_CHANGE ON B_SALE_ORDER_CHANGE (ORDER_ID)
GO
CREATE INDEX IXS_TYPE_CHANGE ON B_SALE_ORDER_CHANGE (TYPE)
GO

CREATE TABLE B_SALE_ORDER_PROCESSING (
	ORDER_ID INT DEFAULT '0',
	PRODUCTS_ADDED CHAR(1) DEFAULT 'N',
	PRODUCTS_REMOVED CHAR(1) DEFAULT 'N'
)
GO

CREATE TABLE B_SALE_TRADING_PLATFORM
(
	ID INT NOT NULL IDENTITY (1, 1),
	CODE VARCHAR(20) NOT NULL,
	ACTIVE CHAR(1) NOT NULL,
	NAME VARCHAR(50)NOT NULL,
	DESCRIPTION VARCHAR(255) NULL,
	SETTINGS TEXT NULL
)
GO
ALTER TABLE B_SALE_TRADING_PLATFORM ADD CONSTRAINT PK_B_SALE_TRADING_PLATFORM PRIMARY KEY (ID)
GO
CREATE UNIQUE INDEX IX_CODE ON B_SALE_TRADING_PLATFORM(CODE)
GO

CREATE TABLE B_SALE_COMPANY
(
	ID int NOT NULL IDENTITY(1,1),
	NAME varchar(128) NOT NULL,
	LOCATION_ID varchar(128) NOT NULL,
	CODE varchar(45) NULL,
	XML_ID varchar(45) NULL,
	ACTIVE char(1) NOT NULL,
	DATE_CREATE DATETIME NOT NULL,
	DATE_MODIFY DATETIME NULL,
	CREATED_BY INT NOT NULL,
	MODIFIED_BY INT NULL
)
GO
ALTER TABLE B_SALE_COMPANY ADD CONSTRAINT PK_B_SALE_COMPANY PRIMARY KEY (ID)
GO

CREATE TABLE B_SALE_BIZVAL
(
	ID INT NOT NULL IDENTITY(1,1),
	CODE_ID INT NOT NULL,
	PERSON_TYPE_ID INT NOT NULL,
	ENTITY VARCHAR(50) NOT NULL,
	ITEM VARCHAR(255) NOT NULL
)
GO
ALTER TABLE B_SALE_BIZVAL ADD CONSTRAINT PK_B_SALE_BIZVAL PRIMARY KEY (ID)
GO
CREATE UNIQUE INDEX IX_BSB_SECONDARY ON B_SALE_BIZVAL(CODE_ID, PERSON_TYPE_ID)
GO

CREATE TABLE B_SALE_BIZVAL_CODE
(
	ID INT NOT NULL IDENTITY(1,1),
	NAME VARCHAR(50) NOT NULL,
	DOMAIN CHAR(1) NOT NULL,
	GROUP_ID INT NULL,
	SORT INT NOT NULL DEFAULT 100
)
GO
ALTER TABLE B_SALE_BIZVAL_CODE ADD CONSTRAINT PK_B_SALE_BIZVAL_CODE PRIMARY KEY (ID)
GO
CREATE UNIQUE INDEX IX_BSBC_NAME ON B_SALE_BIZVAL_CODE(NAME)
GO

CREATE TABLE B_SALE_BIZVAL_GROUP
(
	ID INT NOT NULL IDENTITY(1,1),
	NAME VARCHAR(50) NOT NULL,
	SORT INT NOT NULL DEFAULT 100
)
GO
ALTER TABLE B_SALE_BIZVAL_GROUP ADD CONSTRAINT PK_B_SALE_BIZVAL_GROUP PRIMARY KEY (ID)
GO
CREATE UNIQUE INDEX IX_BSBG_NAME ON B_SALE_BIZVAL_GROUP(NAME)
GO

CREATE TABLE B_SALE_BIZVAL_PARENT
(
	ID INT NOT NULL IDENTITY(1,1),
	NAME VARCHAR(50) NOT NULL,
	LANG_SRC VARCHAR(255) NULL
)
GO
ALTER TABLE B_SALE_BIZVAL_PARENT ADD CONSTRAINT PK_B_SALE_BIZVAL_PARENT PRIMARY KEY (ID)
GO
CREATE UNIQUE INDEX IX_BSBP_NAME ON B_SALE_BIZVAL_PARENT(NAME)
GO

CREATE TABLE B_SALE_BIZVAL_CODEPARENT
(
	CODE_ID INT NOT NULL,
	PARENT_ID INT NOT NULL
)
GO
ALTER TABLE B_SALE_BIZVAL_CODEPARENT ADD CONSTRAINT PK_B_SALE_BIZVAL_CODEPARENT PRIMARY KEY (CODE_ID, PARENT_ID)
GO

CREATE TABLE B_SALE_BIZVAL_PERSONTYPE
(
	PERSON_TYPE_ID INT NOT NULL,
	DOMAIN CHAR(1) NOT NULL
)
GO
ALTER TABLE B_SALE_BIZVAL_PERSONTYPE ADD CONSTRAINT PK_B_SALE_BIZVAL_PERSONTYPE PRIMARY KEY (PERSON_TYPE_ID, DOMAIN)
GO

CREATE TABLE B_SALE_ORDER_DELIVERY_ES
(
	ID INT NOT NULL IDENTITY(1,1),
	SHIPMENT_ID INT NOT NULL,
	EXTRA_SERVICE_ID INT NOT NULL,
	VALUE VARCHAR(255) NULL
)
GO
ALTER TABLE B_SALE_ORDER_DELIVERY_ES ADD CONSTRAINT PK_B_SALE_ORDER_DELIVERY_ES PRIMARY KEY (ID)
GO
CREATE INDEX IX_BSOD_ES_SHIPMENT_ID ON B_SALE_ORDER_DELIVERY_ES(SHIPMENT_ID)
GO
CREATE INDEX IX_BSOD_ES_EXTRA_SERVICE_ID ON B_SALE_ORDER_DELIVERY_ES(EXTRA_SERVICE_ID)
GO

CREATE TABLE B_SALE_DELIVERY_ES
(
	ID INT NOT NULL IDENTITY(1,1),
	CODE VARCHAR(50) NULL,
	NAME VARCHAR(255) NOT NULL,
	DESCRIPTION VARCHAR(255) NULL,
	CLASS_NAME VARCHAR(255) NOT NULL,
	PARAMS TEXT NULL,
	RIGHTS CHAR(3) NOT NULL,
	DELIVERY_ID INT NOT NULL,
	INIT_VALUE VARCHAR(255) NULL,
	ACTIVE CHAR(1) NOT NULL,
	SORT INT DEFAULT 100
)
GO
ALTER TABLE B_SALE_DELIVERY_ES ADD CONSTRAINT PK_B_SALE_DELIVERY_ES PRIMARY KEY (ID)
GO
CREATE INDEX IX_BSD_ES_DELIVERY_ID ON B_SALE_DELIVERY_ES(DELIVERY_ID)
GO

CREATE TABLE B_SALE_DELIVERY_RSTR
(
	ID INT NOT NULL IDENTITY(1,1),
	DELIVERY_ID INT NOT NULL,
	SORT INT DEFAULT 100,
	CLASS_NAME VARCHAR(255) NOT NULL,
	PARAMS TEXT NULL
)
GO
ALTER TABLE B_SALE_DELIVERY_RSTR ADD CONSTRAINT PK_B_SALE_DELIVERY_RSTR PRIMARY KEY (ID)
GO
CREATE INDEX IX_BSDR_DELIVERY_ID ON B_SALE_DELIVERY_RSTR(DELIVERY_ID)
GO

CREATE TABLE B_SALE_DELIVERY_SRV
(
	ID INT NOT NULL IDENTITY(1,1),
	CODE VARCHAR(50) NULL,
	PARENT_ID INT NULL,
	NAME VARCHAR(255) NOT NULL,
	ACTIVE CHAR(1) NOT NULL,
	DESCRIPTION VARCHAR(255) NULL,
	SORT INT NOT NULL,
	LOGOTIP INT NULL,
	CONFIG TEXT NULL,
	CLASS_NAME VARCHAR(255) NOT NULL,
	CURRENCY CHAR(3) NOT NULL
)
GO
ALTER TABLE B_SALE_DELIVERY_SRV ADD CONSTRAINT PK_B_SALE_DELIVERY_SRV PRIMARY KEY (ID)
GO
CREATE INDEX IX_BSD_SRV_PARENT_ID ON B_SALE_DELIVERY_SRV(PARENT_ID)
GO
CREATE INDEX IX_BSD_SRV_CODE ON B_SALE_DELIVERY_SRV(CODE)
GO

CREATE TABLE B_SALE_ORDER_DLV_BASKET(
	ID INT NOT NULL IDENTITY(1, 1),
	ORDER_DELIVERY_ID INT NOT NULL,
	BASKET_ID INT NOT NULL,
	DATE_INSERT DATETIME NOT NULL,
	QUANTITY DECIMAL(18,4) NOT NULL,
	RESERVED_QUANTITY DECIMAL(18,4) NOT NULL,
)
GO
ALTER TABLE B_SALE_ORDER_DLV_BASKET ADD CONSTRAINT PK_B_SALE_ORDER_DLV_BASKET PRIMARY KEY (ID)
GO
CREATE INDEX IX_BSODB_ORDER_DELIVERY_ID ON B_SALE_ORDER_DLV_BASKET(ORDER_DELIVERY_ID)
GO
CREATE TABLE B_SALE_ORDER_PAYMENT(
	ID INT NOT NULL IDENTITY(1, 1),
	ORDER_ID INT NOT NULL,
	PAID CHAR(1) NOT NULL DEFAULT 'N',
	DATE_PAID DATETIME NULL,
	EMP_PAID_ID INT NULL,
	PAY_SYSTEM_ID INT NOT NULL,
	PS_STATUS CHAR(1) NULL,
	PS_STATUS_CODE CHAR(5) NULL,
	PS_STATUS_DESCRIPTION VARCHAR(250) NULL,
	PS_STATUS_MESSAGE VARCHAR(250) NULL,
	PS_SUM DECIMAL(18,4) NULL,
	PS_CURRENCY CHAR(3) NULL,
	PS_RESPONSE_DATE DATETIME NULL,
	PAY_VOUCHER_NUM VARCHAR(20) NULL,
	PAY_VOUCHER_DATE DATE NULL,
	DATE_PAY_BEFORE DATETIME NULL,
	DATE_BILL DATETIME NULL,
	XML_ID VARCHAR(255) NULL,
	SUM DECIMAL(18,4) NOT NULL,
	CURRENCY CHAR(3) NOT NULL,
	PAY_SYSTEM_NAME VARCHAR(128) NOT NULL,
	RESPONSIBLE_ID INT NULL,
	DATE_RESPONSIBLE_ID datetime NULL,
	EMP_RESPONSIBLE_ID int NULL,
	COMMENTS text,
	COMPANY_ID int NULL,
	PAY_RETURN_DATE date DEFAULT NULL,
	EMP_RETURN_ID INT NULL,
	PAY_RETURN_NUM VARCHAR(20) DEFAULT NULL,
	PAY_RETURN_COMMENT text,
	IS_RETURN CHAR(1) NOT NULL DEFAULT 'N'
)
GO
CREATE INDEX IX_BSOP_ORDER_ID ON B_SALE_ORDER_PAYMENT(ORDER_ID)
GO
