/*==============================================================*/
/* DBMS name:      Microsoft SQL Server 2000                    */
/* Created on:     17.1.2014 13:08:23                           */

/* UPDATED ON:     3.9.2014                                     */
/* UPDATED BY:     Miha M                                       */
/* CHANGES:        - samo dodani komentarji za kdaj je bila narejena sprememba,
					kdo jo je naredil in kakšne so spremembe
   VERSION:        14-1   (14 sem mislil kot za leto 2014 in 1 kot prva sprememba v letu 2014. Naslednja bi bila 14_2. Naj nekdo spremeni, če ima boljšo idejo.    */

/* UPDATED ON:     11.9.2014                                     */
/* UPDATED BY:     Blaž Roženbergar                                     */
/* CHANGES:        Dodana dva stolpca v tabelo Head, ki določata kontaktno osebo stranke in način dostave.  
   VERSION:        14_2 */

/* UPDATED ON:     24.9.2014                                     */
/* UPDATED BY:     Janez Grudnik                                     */
/* CHANGES:        Vsi nvarchar columni so nastavljeni, da so CASE INSENSITIVE (COLATE NOCASE)
   VERSION:        14_3 */
   
/* UPDATED ON:     14.10.2014                                     */
/* UPDATED BY:     Blaž Roženbergar                                   */
/* CHANGES:        Dodana tabela UserProductCustomer 
   VERSION:        14_4 */

/* UPDATED ON:     21.10.2014                                     */
/* UPDATED BY:     Janez Grudnik */
/* CHANGES:        Dodan stolpec Categ3 v tabelo Products */
/* VERSION:        14_5 */

/* UPDATED ON:     23.10.2014                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec Changed v tabelo Position */
/* VERSION:        14_6 */

/* UPDATED ON:     27.11.2014                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodani Categ4, Categ5 stolpci v tabeli Products */
/* VERSION:        14_7 */

/* UPDATED ON:     3.12.2014                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodana Longitude in Latitude stolpca v tabeli Head */
/* VERSION:        14_8 */

/* UPDATED ON:     10.12.2014                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodana PrintAmountTax in PrintAmountDiscount stolpca v tabeli Position */
/* VERSION:        14_9 */

/* UPDATED ON:     11.12.2014                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodan Discount stolpec v tabeli Products (namenjen enostavni cenovni politiki) */
/* VERSION:        14_10 */

/* UPDATED ON:     11.12.2014                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodani stolpci LinkedCustomer1, LinkedCustomer2 v Head */
/*	    		   Dodani stoplci CompanyRegNumber, Post, Phone, Fax, Email, ContactPerson, ContactPersonMobile, Transport, Storage v Customer	 */
/* 				   Dodani stolpci CustomField1,CustomField2,CustomField3,CustomField4,CustomField5,CustomField6 v Products */
/* VERSION:        14_11 */

/* UPDATED ON:     18.12.2014                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodani stolpci County v tabelo Customer
				   Dodani stolpci CustomField7, CustomField8, CustomField9 v tabelo Products */
/* VERSION:        14_12 */

/* UPDATED ON:     19.12.2014                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Podaljšana stolpca iz nvarchar(50) v nvarchar(100): T: Barcode S: BarCode; T: Stock S: ObjectSerial */
/* VERSION:        14_13 */

/* UPDATED ON:     22.1.2015                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodani stolpec SourceDocType v tabelo DocLocalHistoryViewAtl in sprememba PK na tej tabeli da vsebuje tudi DocType */
/* VERSION:        15_1 */

/* UPDATED ON:     29.1.2015                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec BookkeepingType v tabelo Products, ki pove knjigovodsko vrsto izdelka */
/* VERSION:        15_2 */

/* UPDATED ON:     2.2.2015                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec PackQty v tabelo Position, ki pove število kolijev */
/* VERSION:        15_3 */

/* UPDATED ON:     9.2.2015                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec CreditPolicyStatus v tabelo Head, ki pove blokiranost stranke (1 = blokirana in pomeni, da se je delala le NUMERIKA) */
/* VERSION:        15_4 */

/* UPDATED ON:     12.2.2015                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec Reason v tabelo Head, ki je razlog za obisk */
/* VERSION:        15_5 */

/* UPDATED ON:     24.2.2015                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodan stolpec CustomField10 v tabelo Products  */
/* VERSION:        15_6 */

/* UPDATED ON:     4.3.2015                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodan stolpec CustomField11 v tabelo Products  */
/* VERSION:        15_7 */

/* UPDATED ON:     6.3.2015                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodan stolpec CustomField12 in CustomField13 v tabelo Products  */
/* VERSION:        15_8 */

/* UPDATED ON:     11.3.2015                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodan stolpec ExpectedAmount v tabelo Position  */
/* VERSION:        15_9 */

/* UPDATED ON:     5.10.2015                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodan stolpec NewInventory v tabelo Stock, ki pri InvOS pove, da gre za novo inventuro  */
/* VERSION:        15_10 */

/* UPDATED ON:     12.11.2015                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodan stolpec PriceMPC v tabelo Products  */
/* VERSION:        15_11 */

/* UPDATED ON:     13.11.2015                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodana stolpca AmountPaid in AmountReturned v tabelo head  */
/* VERSION:        15_12 */

/* UPDATED ON:     16.11.2015                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodana tabela DL_SetItemPriceForWrh  */
/* VERSION:        15_13 */

/* UPDATED ON:     16.11.2015                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodan stolpec CustomSalePrice v Customer  */
/* VERSION:        15_14 */

/* UPDATED ON:     18.11.2015                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodana tabela ERPDocPayment  */
/* VERSION:        15_15 */

/* UPDATED ON:     18.12.2015                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodan column Note na tabeli Stock  */
/* VERSION:        15_16 */

/* UPDATED ON:     21.12.2015                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodan column PackQty na tabeli Stock  */
/* VERSION:        15_17 */

/* UPDATED ON:     22.12.2015                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan column TaxNumber na tabeli Users  */
/* VERSION:        15_18 */

/* UPDATED ON:     19.01.2016                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan primary key na HeadFiscalSi tabeli  */
/* VERSION:        16_1 */

/* UPDATED ON:     15.03.2016                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodano polje DeliveryNoteNumber v tabelo Head za številko dobavnice  */
/* VERSION:        16_2 */

/* UPDATED ON:     1.04.2016                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodano polje FixedAsset v tabelo Products, ki simbolizira osnovno sredstvo  */
/* VERSION:        16_3 */

/* UPDATED ON:     1.04.2016                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodana tabela Head_Object  */
/* VERSION:        16_4 */

/* UPDATED ON:     17.05.2016                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodan stolpec ReportId v tabelo SettingsErpDoc  */
/* VERSION:        16_5 */

/* UPDATED ON:     16.11.2016                                    */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodan stolpec CashRegisterHead v tabelo Head  */
/* VERSION:        16_6 */

/* UPDATED ON:     2.12.2016                                    */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec InvoiceJson v tabelo HeadFiscalSi*/
/* VERSION:        16_7 */

/* UPDATED ON:     17.01.2017                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodana tabela za package delivery*/
/* VERSION:        17_1 */

/* UPDATED ON:     4.05.2017                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodana tabela DeliveryMethod in stolpec InternalNote v Head*/
/* VERSION:        17_2 */

/* UPDATED ON:     11.07.2017                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodana tabela ObjectFoto za InvOS - fotografije */
/* VERSION:        17_5 */

/* UPDATED ON:     25.07.2017                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodana tabela InventorySignature in InventorySignatureAssetLink za InvOS - podpis */
/* VERSION:        17_6 */

/* UPDATED ON:     31.07.2017                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodan stolpec DtDueDuration v Head - valuta oz. zapadlost računa */
/* VERSION:        17_7 */

/* UPDATED ON:     30.08.2017                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec RacunTypeJson v Racun - json zapis računa */
/* VERSION:        17_8 */

/* UPDATED ON:     08.09.2017                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodana tabela za custom folder od podjetja Njiva d.o.o. - _NJIVA_JMD_SetItem*/
/* VERSION:        17_9 */

/* UPDATED ON:     18.09.2017                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodan stolpec TransferDiscountToErp v Position - ali se prenaša popust, ki je bil vpisan na terminalu na poziciji v cenik/ERP */
/* VERSION:        17_10 */

/* UPDATED ON:     17.10.2017                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodan stolpec OrderPackAmount v Position - Koli količina na naročilu */
/* VERSION:        17_11 */

/* UPDATED ON:     18.01.2018                                     */
/* UPDATED BY:     Jure Maver   */
/* CHANGES:        Dodan stolpec CustomerOrderNumber v Head - šifra naročila, ki jo je kupec sporočil ob oddaji naročila */
/* VERSION:        18_03 */

/* UPDATED ON:     19.01.2018                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec OverrideErpDocIdd (Prepis ErpDocIdd ob pošiljanju v ERP) in AutoGenerateErpNumber (ali se dokument številči v ERPu) v SettingsErpDoc */
/* VERSION:        18_04 */

/* UPDATED ON:     07.02.2018                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodane tabele in stolpci za SAOP cenike in cene */
/* VERSION:        18_05 */

/* UPDATED ON:     05.03.2018                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodan stolpec v Users - AllowLoginManager */
/* VERSION:        18_06 */

/* UPDATED ON:     23.03.2018                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec v Position - PriceManuallyModified */
/* VERSION:        18_07 */

/* UPDATED ON:     16.05.2018                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodan stolpec v Position - NumberOfScans */
/* VERSION:        18_08 */

/* UPDATED ON:     25.05.2018                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec v Products - SaopItemStatus */
/* VERSION:        18_09 */

/* UPDATED ON:     21.06.2018                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodan stolpec v Products - ArdCategory */
/* VERSION:        18_10 */

/* UPDATED ON:     24.07.2018                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodana tabela CostUnit - stroškovni nosilci in stolpec CostUnit v Head */
/* VERSION:        18_11 */

/* UPDATED ON:     20.08.2018                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodana stolpec ProcessedCounter v Stock - število obdelav OS pri inventuri osnovnih sredstev */
/* VERSION:        18_12 */

/* UPDATED ON:     17.09.2018                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec ContactPersonPayer v Head */
/* VERSION:        18_13 */

/* UPDATED ON:     09.01.2019                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodani tabeli za kontakte strank             
				   - CustomerContacts in CustomerContactInfo    */
/* VERSION:        19_01 */

/* UPDATED ON:     30.01.2019                                     */
/* UPDATED BY:     Miha Mikec   */
/* CHANGES:        Dodana tabela za fotografije artiklov              
				   - ProductPhoto   */
/* VERSION:        19_03 */

/* UPDATED ON:     01.03.2019                                     */
/* UPDATED BY:     Blaž Roženbergar   */
/* CHANGES:        Dodan stolpec Id (identity) v CostUnit tabelo in vezava na Head  */
/* VERSION:        19_05 */

/* UPDATED ON:     03.04.2019                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodan stolpec GUID (identity) v ProductPhoto tabelo*/
/* VERSION:        19_06 */

/* UPDATED ON:     03.04.2019                                     */
/* UPDATED BY:     Janez Grudnik   */
/* CHANGES:        Dodan stolpec IsDefault v ProductPhoto tabelo*/
/* VERSION:        19_07 */

/* UPDATED ON:     02.06.2023                                     */
/* UPDATED BY:     Gašper Perovič   */
/* CHANGES:        Dodana tabela RfidHistory*/
/* VERSION:        23.01 */

/* UPDATED ON:     06.06.2023                                     */
/* UPDATED BY:     Gašper Perovič   */
/* CHANGES:        Dodana tabela WrongLocationRfid*/
/* VERSION:        23.01 */

/*==============================================================*/
/* Table: TypesGroups                                           */
/*==============================================================*/
create table TypesGroups (
   GroupIdx             int                  not null,
   Name                 nvarchar(100) COLLATE NOCASE      null,
   Description          nvarchar(200) COLLATE NOCASE       null,
   constraint PK_TYPESGROUPS primary key (GroupIdx)
)
go

/*==============================================================*/
/* Table: Types                                                 */
/*==============================================================*/
create table Types (
   Type                 int                  not null,
   GroupIdx             int                  null,
   DocumentTitle        nvarchar(50) COLLATE NOCASE        null,
   DocumentIdent        nvarchar(50) COLLATE NOCASE        null,
   Status               int                  null,
   constraint PK_TYPES primary key (Type),
   constraint FK_TYP_REF_TYSGRO foreign key (GroupIdx)
      references TypesGroups (GroupIdx)
)
go

/*==============================================================*/
/* Table: Firm                                                  */
/*==============================================================*/
create table Firm (
   Firm                 int                  not null,
   Name                 nvarchar(50) COLLATE NOCASE         null,
   Description          nvarchar(50) COLLATE NOCASE         null,
   Status               int                  null,
   Type                 int                  null,
   constraint PK_FIRM primary key (Firm),
   constraint FK_TYP_REF_FIRMTYPE foreign key (Type)
      references Types (Type)
)
go

/*==============================================================*/
/* Table: TimeLine                                              */
/*==============================================================*/
create table TimeLine (
   TL_Idd               int                  not null,
   Firm                 int                  null,
   Type                 int                  null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   Name                 nvarchar(50) COLLATE NOCASE         null,
   Description          nvarchar(50) COLLATE NOCASE         null,
   TimeFrom             datetime             null,
   TimeTo               datetime             null,
   TimeAdd1             datetime             null,
   IValue               int                  null,
   CValue               nvarchar(50) COLLATE NOCASE         null,
   FValue               float                null,
   status               int                  null,
   constraint PK_TIMELINE primary key (TL_Idd),
   constraint FK_TIMELINE_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm),
   constraint FK_TIMELINE_REFERENCE_TYPES foreign key (Type)
      references Types (Type)
)
go

/*==============================================================*/
/* Table: Txt                                                   */
/*==============================================================*/
create table Txt (
   Txt                  nvarchar(500) COLLATE NOCASE         null,
   TIdd                 int                  not null,
   Change               datetime             null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   Status               int                  null,
   constraint PK_TXT primary key (TIdd)
)
go

/*==============================================================*/
/* Table: Object                                                */
/*==============================================================*/
create table Object (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Change               datetime             null,
   Type                 int                  not null,
   TIdd                 int                  null,
   TL_Idd               int                  null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   Name                 nvarchar(50) COLLATE NOCASE         null,
   Name_ASCII           nvarchar(50) COLLATE NOCASE         null,
   Status               int                  not null,
   constraint PK_OBJECT primary key (Code, Firm, Type),
   constraint FK_OBJECT_REFERENCE_TIMELINE foreign key (TL_Idd)
      references TimeLine (TL_Idd),
   constraint FK_OBJECT_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm),
   constraint FK_OBJECT_REFERENCE_TYPES foreign key (Type)
      references Types (Type),
   constraint FK_OBJECT_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
 go

/*==============================================================*/
/* Table: AddObjectData                                         */
/*==============================================================*/
create table AddObjectData (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   AddObjectDataType    int                  not null,
   TIdd                 int                  null,
   AOD_Int              int                  null,
   AOD_Char             nvarchar(500) COLLATE NOCASE        null,
   AOD_Idd              int                  not null,
   AOD_Time             datetime             null,
   Change               datetime             null,
   Status               int                  null,
   constraint PK_ADDOBJECTDATA primary key (AOD_Idd),
   constraint FK_ADDOBJEC_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type),
   constraint FK_ADDOBJEC_REFERENCE_TYPES foreign key (AddObjectDataType)
      references Types (Type),
   constraint FK_ADDOBJEC_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Index: I00081                                                */
/*==============================================================*/
create index I00081 on AddObjectData (
AddObjectDataType ASC
)
go

/*==============================================================*/
/* Table: QuestionTypes                                         */
/*==============================================================*/
create table QuestionTypes (
   QuestionType         int                  not null,
   TypeName             nvarchar(50) COLLATE NOCASE        null,
   constraint PK_QUESTIONTYPES primary key (QuestionType)
)
go

/*==============================================================*/
/* Table: AnwserGroup                                           */
/*==============================================================*/
create table AnwserGroup (
   GroupID              int                  not null,
   QuestionType         int                  null,
   GroupName            nvarchar(50) COLLATE NOCASE         null,
   constraint PK_ANWSERGROUP primary key (GroupID),
   constraint FK_ANWSERGR_REFERENCE_QUESTION foreign key (QuestionType)
      references QuestionTypes (QuestionType)
)
go

/*==============================================================*/
/* Table: Anwsers                                               */
/*==============================================================*/
create table Anwsers (
   AnwserID             int                  not null,
   GroupID              int                  null,
   Anwser               nvarchar(50) COLLATE NOCASE         null,
   OrderNr              int                  null,
   constraint PK_ANWSERS primary key (AnwserID),
   constraint FK_ANWSERS_REFERENCE_ANWSERGR foreign key (GroupID)
      references AnwserGroup (GroupID)
)
go

/*==============================================================*/
/* Table: Subject                                               */
/*==============================================================*/
create table Subject (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Change               datetime             null,
   Type                 int                  not null,
   TIdd                 int                  null,
   TL_Idd               int                  null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   Name                 nvarchar(50) COLLATE NOCASE         null,
   Name_ASCII           nvarchar(50) COLLATE NOCASE         null,
   Status               int                  not null,
   constraint PK_SUBJECT primary key (Code, Firm, Type),
   constraint FK_SUBJECT_REFERENCE_TIMELINE foreign key (TL_Idd)
      references TimeLine (TL_Idd),
   constraint FK_SUBJECT_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm),
   constraint FK_SUBJECT_REFERENCE_TYPES foreign key (Type)
      references Types (Type),
   constraint FK_SUBJECT_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Table: Users                                                 */
/*==============================================================*/
create table Users (
   UserIdx              int                  not null,
   Firm                 int                  null,
   Type                 int                  null,
   UserName             nvarchar(50) COLLATE NOCASE         null,
   Password             nvarchar(50) COLLATE NOCASE         null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   Serial               nvarchar(50) COLLATE NOCASE         null,
   SecLevel             nvarchar(50) COLLATE NOCASE         null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   Status               int                  null,
   sapCashier           nvarchar(5) COLLATE NOCASE          null,
   sapSalesGroup        nvarchar(5) COLLATE NOCASE          null,
   sapSalesOffice       nvarchar(5) COLLATE NOCASE          null,
   TaxNumber			nvarchar(255) COLLATE NOCASE		null,
   AllowLoginManager	int					null,
   constraint PK_USERS primary key (UserIdx),
   constraint FK_USERS_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Table: InventoryRound												*/
/*==============================================================*/
create table InventoryRound (
	Id					int								not null,
	Firm				int								not null, 
	LastChange			datetime						null, 
	Name				nvarchar (250) COLLATE NOCASE	null,
    Status				int								not null,
	constraint PK_InventoryRound primary key (Id)
)
go

/*==============================================================*/
/* Table: Stock                                                 */
/*==============================================================*/
create table Stock (
   SIdd                 int                  not null,
   LocCode              nvarchar(50) COLLATE NOCASE         null,
   LocFirm              int                  null,
   LocType              int                  null,
   OwnCode              nvarchar(50) COLLATE NOCASE         null,
   OwnFirm              int                  null,
   OwnType              int                  null,
   ObjectCode           nvarchar(50) COLLATE NOCASE        null,
   ObjectFirm           int                  null,
   ObjectType           int                  null,
   ObjectAmount1        float                null,
   ObjectUnit1          int                  null,
   ObjectAmount2        float                null,
   ObjectUnit2          int                  null,
   MicroLocType         int                  null,
   MicroLocCode         nvarchar(50) COLLATE NOCASE         null,
   MicroLocFirm         int                  null,
   Sub_Type             int                  null,
   TIdd                 int                  null,
   TL_Idd               int                  null,
   UserIdx              int                  null,
   ObjectSerial         nvarchar(100) COLLATE NOCASE         null,
   InsertDateTime       datetime             null,
   ObjectDateTime       datetime             null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   Status               int                  not null,
   ChangeDateTime       datetime             null,
   SourceMicroLocCode   nvarchar(50) COLLATE NOCASE         null,
   SettIdd              int                  null,
   NewInventory 		int 				 null,
   Note                 nvarchar(250) COLLATE NOCASE        null,
   PackQty 				int 				 null,
   ErpSIdd				int					 null,
   NumberOfScans        int                  null,
   ProcessedCounter     int                  null,
   InventoryRoundId		int					 null,
   constraint PK_STOCK primary key (SIdd),
   constraint FK_STOCK_R_TXT foreign key (TIdd)
      references Txt (TIdd),
   constraint FK_STOCK_REFERENCE_TIMELINE foreign key (TL_Idd)
      references TimeLine (TL_Idd),
   constraint FK_STOCK_REFERENCE_USERS foreign key (UserIdx)
      references Users (UserIdx),
   constraint FK_STOCK_REFERENCE_SUBJECT foreign key (MicroLocCode, MicroLocFirm, Sub_Type)
      references Subject (Code, Firm, Type),
   constraint STOCK_SUB_LOC foreign key (LocCode, LocFirm, LocType)
      references Subject (Code, Firm, Type),
   constraint STOCK_SUB_OWN foreign key (OwnCode, OwnFirm, OwnType)
      references Subject (Code, Firm, Type),
   constraint FK_STOCK_REFERENCE_OBJECT foreign key (ObjectCode, ObjectFirm, ObjectType)
      references Object (Code, Firm, Type),
   constraint FK_STOCK_TYPES_1 foreign key (ObjectUnit1)
      references Types (Type),
   constraint FK_STOCK_TYPES_2 foreign key (ObjectUnit2)
      references Types (Type),
   constraint FK_STOCK_REFERENCE_TYPES foreign key (MicroLocType)
      references Types (Type),
	constraint FK_STOCK_INVENTORYROUND foreign key (InventoryRoundId)
      references InventoryRound (Id)
)
go

/*==============================================================*/
/* Table: DeliveryMethod                                        */
/*==============================================================*/
create table DeliveryMethod (
   Code                 nvarchar(50)         not null,
   Firm                 int                  not null,
   Name                 nvarchar(100)        not null,   
   CONSTRAINT PK_DELIVERYMETHOD PRIMARY KEY (Code, Firm)
)
go

/*==============================================================*/
/* Table: Head                                                  */
/*==============================================================*/
create table Head (
   Firm                 int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   DocType              int                  not null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   Type                 int                  null,
   TIdd                 int                  null,
   UserIdx              int                  null,
   PaymentMethod        int                  null,
   HeadLink             nvarchar(50) COLLATE NOCASE         null,
   HeadLinkMobos        nvarchar(50) COLLATE NOCASE         null,
   txt                  int                  null,
   DTLastChange         datetime             null,
   DTLastPrint          datetime             null,
   DTCreation           datetime             null,
   DTAdv                datetime             null,
   DTAdd1               datetime             null,
   DTDueDuration        datetime             null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   AddParam1            nvarchar(50) COLLATE NOCASE         null,
   AddParam2            nvarchar(50) COLLATE NOCASE         null,
   AddParam3            nvarchar(50) COLLATE NOCASE         null,
   Status               int                  null,
   SuperDiscount        float                null,
   AddParam4            nvarchar(50) COLLATE NOCASE         null,
   SourceWarehouseCode  nvarchar(50) COLLATE NOCASE         null,
   SourceWarehouseType  int                  null,
   DestinationWarehouseCode nvarchar(50) COLLATE NOCASE         null,
   DestinationWarehouseType int                  null,
   Notes                nvarchar(250) COLLATE NOCASE        null,
   InternalNote         nvarchar(250) COLLATE NOCASE        null,
   PrintedDocType       int                  null,
   reference            nvarchar(20) COLLATE NOCASE         null,
   Amount1              float                null,
   Amount2              float                null,
   PriorityOrder        int                  null,
   ERPStatus            int                  null,
   ERPExport            int                  null,
   DeliveryName         nvarchar(50) COLLATE NOCASE         null,
   ContactPerson        nvarchar(50) COLLATE NOCASE         null,
   ContactPersonPayer   nvarchar(50) COLLATE NOCASE         null,
   Longitude            nvarchar(50)         null,
   Latitude             nvarchar(50)         null,
   LinkedCustomer1      nvarchar(50)         null,
   LinkedCustomer2      nvarchar(50)         null,
   CreditPolicyStatus 	int 				 null,
   Reason               nvarchar(250)        null,
   AmountPaid           float                null,
   AmountReturned       float                null,
   DeliveryNoteNumber   nvarchar(50) COLLATE NOCASE         null,
   CashRegisterHead     nvarchar(50)         null,
   CustomerOrderNumber  nvarchar(50)         null,
   CostUnit				nvarchar(50) COLLATE NOCASE         null,
   constraint PK_HEAD primary key (Head, Firm, DocType),
   constraint FK_HEAD_REFERENCE_SUBJECT foreign key (Code, Firm, Type)
      references Subject (Code, Firm, Type),
   constraint FK_HEAD_TYPES_PAYMENT foreign key (PaymentMethod)
      references Types (Type),
   constraint FK_HEAD_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd),
   constraint FK_HEAD_REFERENCE_USERS foreign key (UserIdx)
      references Users (UserIdx),
   constraint FK_HEAD_REFERENCE_TYPES foreign key (DocType)
      references Types (Type),
   constraint FK_HEAD_FK_HEAD_T_TYPES foreign key (PaymentMethod)
      references Types (Type),
   constraint FK_LINKEDCUSTOMER1_REFERENCE_CUSTOMER foreign key (LinkedCustomer1, Firm, Type)
      references Customer (Code,Firm,Type),

   constraint FK_LINKEDCUSTOMER2_REFERENCE_CUSTOMER foreign key (LinkedCustomer2, Firm, Type)
      references Customer (Code,Firm,Type)
)
go

/*==============================================================*/
/* Table: BData                                                 */
/*==============================================================*/
create table BData (
   BinaryData           image                null,
   BDataIdx             int                  not null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   Firm                 int                  null,
   Type                 int                  null,
   Sub_Code             nvarchar(50) COLLATE NOCASE         null,
   Sub_Firm             int                  null,
   Sub_Type             int                  null,
   SIdd                 int                  null,
   Head                 nvarchar(50) COLLATE NOCASE         null,
   Hea_Firm             int                  null,
   DocType              int                  null,
   Data_Type              int                null,
   constraint PK_BDATA primary key (BDataIdx),
   constraint FK_BDATA_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type),
   constraint FK_BDATA_REFERENCE_SUBJECT foreign key (Sub_Code, Sub_Firm, Sub_Type)
      references Subject (Code, Firm, Type),
   constraint FK_BDATA_REFERENCE_STOCK foreign key (SIdd)
      references Stock (SIdd),
   constraint FK_BDATA_REFERENCE_HEAD foreign key (Head, Hea_Firm, DocType)
      references Head (Head, Firm, DocType)
)
go

/*==============================================================*/
/* Table: Barcode                                               */
/*==============================================================*/
create table Barcode (
   BIdd                 int                  not null,
   Firm                 int                  null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   Type                 int                  not null,
   BarCode              nvarchar(100) COLLATE NOCASE         not null,
   Status               int                  not null,
   SIdd                 int                  null,
   BcType               int                  null,
   CustomerCode         nvarchar(50) COLLATE NOCASE         null,
   constraint PK_BARCODE primary key (BIdd),
   constraint FK_BARCODE_REFERENCE_STOCK foreign key (SIdd)
      references Stock (SIdd),
   constraint FK_BARCODE_REFERENCE_TYPES2 foreign key (BcType)
      references Types (Type),
   constraint FK_BARCODE_REFERENCE_TYPES foreign key (Type)
      references Types (Type),
   constraint FK_BARCODE_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Index: I00005                                                */
/*==============================================================*/
create index I00005 on Barcode (
SIdd ASC
)
go

/*==============================================================*/
/* Index: I00006                                                */
/*==============================================================*/
create index I00006 on Barcode (
Firm ASC,
Code ASC,
Type ASC
)
go

/*==============================================================*/
/* Index: I00007                                                */
/*==============================================================*/
create index I00007 on Barcode (
BarCode ASC
)
go

/*==============================================================*/
/* Index: I00011                                                */
/*==============================================================*/
create index I00011 on Barcode (
CustomerCode ASC
)
go

/*==============================================================*/
/* Table: Classes                                               */
/*==============================================================*/
create table Classes (
   ClassIdx             int                  not null,
   Firm                 int                  null,
   Type                 int                  null,
   ClassCode            nvarchar(50) COLLATE NOCASE         null,
   Name                 nvarchar(50) COLLATE NOCASE         null,
   Description          nvarchar(50) COLLATE NOCASE         null,
   Status               int                  null,
   constraint PK_CLASSES primary key (ClassIdx),
   constraint FK_CLAS_REF_FI foreign key (Firm)
      references Firm (Firm),
   constraint FK_CLAS_R_TY foreign key (Type)
      references Types (Type)
)
go

/*==============================================================*/
/* Table: Tasks                                                 */
/*==============================================================*/
create table Tasks (
   Task                 int                  not null,
   TimeStart            datetime             null,
   TimeLastUse          datetime             null,
   constraint PK_TASKS primary key (Task)
)
go

/*==============================================================*/
/* Table: Customer                                              */
/*==============================================================*/
create table Customer (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   Task                 int                  null,
   TIdd                 int                  null,
   Change               datetime             null,
   Name_full            nvarchar(250) COLLATE NOCASE        null,
   Address              nvarchar(50) COLLATE NOCASE         null,
   City                 nvarchar(50) COLLATE NOCASE         null,
   Country              nvarchar(50) COLLATE NOCASE         null,
   TaxNumber            nvarchar(50) COLLATE NOCASE         null,
   LinkToUpper          nvarchar(50) COLLATE NOCASE         null,
   Categ1               nvarchar(50) COLLATE NOCASE         null,
   Categ2               nvarchar(50) COLLATE NOCASE         null,
   Categ3               nvarchar(50) COLLATE NOCASE         null,
   AddParam1            nvarchar(50) COLLATE NOCASE         null,
   AddParam2            nvarchar(50) COLLATE NOCASE         null,
   AddParam3            nvarchar(50) COLLATE NOCASE         null,
   Status               int                  not null,
   Discount             float                null,
   DueDuration          int                  null,
   PriceList            int                  null,
   Block                smallint             null,
   Buyer                smallint             null,
   Supplier             smallint             null,
   Warehouse            smallint             null,
   CustomSalePrice		int					 null,
   LimitAmount          float                null,
   CurrentDebt          float                null,
   DefaultPaymentMethod int                  null,
   Owner                int                  null,
   LinkToUpperType      int                  null,
   proContract1         nvarchar(15) COLLATE NOCASE         null,
   proContract2         nvarchar(15) COLLATE NOCASE         null,
   proPriceList         nvarchar(5) COLLATE NOCASE          null,
   proCustType          nvarchar(15) COLLATE NOCASE         null,
   SaopDiscountGroup    nvarchar(20) COLLATE NOCASE NULL,
   SaopPriceList        nvarchar(20) COLLATE NOCASE NULL,
   SaopOnSalePriceList  nvarchar(20) COLLATE NOCASE NULL,
   SaopSupplierPriceList nvarchar(20) COLLATE NOCASE NULL,
   CommonInvoice        int                  null,
   sapContCred          nvarchar(15) COLLATE NOCASE         null,
   sapSortInvoice       nvarchar(3) COLLATE NOCASE          null,
   sapSortDelNote       nvarchar(3) COLLATE NOCASE          null,
   sapSortPricesDelNote nvarchar(3) COLLATE NOCASE          null,
   sapOldestOpenClaim   datetime             null,
   sapPackingPayable    nchar(1)             null,
   CustomField1         nvarchar(250) COLLATE NOCASE        null,
   CustomField2         nvarchar(250) COLLATE NOCASE        null,
   CustomField3         nvarchar(250) COLLATE NOCASE        null,
   CustomField4         nvarchar(250) COLLATE NOCASE        null,
   CustomField5         nvarchar(250) COLLATE NOCASE        null,
   DefaultCustomerDocument nvarchar(50) COLLATE NOCASE     null,
   DefaultDeliveryCode  nvarchar(50) COLLATE NOCASE     null,
   CompanyRegNumber     nvarchar(50) COLLATE NOCASE        null,
   Post     			nvarchar(50) COLLATE NOCASE        null,
   Phone     			nvarchar(50) COLLATE NOCASE        null,
   Fax     				nvarchar(50) COLLATE NOCASE        null,
   EMail     			nvarchar(50) COLLATE NOCASE        null,
   ContactPerson 		nvarchar(50) COLLATE NOCASE        null,
   ContactPersonMobile	nvarchar(50) COLLATE NOCASE        null,
   Transport 			int 				null,
   Storage 				int 				null,
   County				nvarchar(250) COLLATE NOCASE       null,
   Addition				float                null,
   constraint PK_CUSTOMER primary key (Code, Firm, Type),
   constraint FK_CUSTOMER_REFERENCE_SUBJECT foreign key (Code, Firm, Type)
      references Subject (Code, Firm, Type),
   constraint FK_CUSTOMER_REFERENCE_TASKS foreign key (Task)
      references Tasks (Task),
   constraint FK_CUSTOMER_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Table: DL_SetSubjPriceCateg                                  */
/*==============================================================*/
create table DL_SetSubjPriceCateg (
   Firm                 int                  not null,
   acSubject            nvarchar(30) COLLATE NOCASE         not null,
   acClassif            nvarchar(16) COLLATE NOCASE         not null,
   acInvoInOut          nchar(1)             not null,
   adDateStart          datetime             not null,
   adDateEnd            datetime             null,
   acType               nchar(1)             null,
   anPrice              float                null,
   anRebate             float                null,
   anRebate2            float                null,
   anRebate3            float                null,
   acCurrency           nchar(3)             null,
   acFormula            nchar(3)             null,
   anDeliveryDeadline   int                  null,
   anPaymentDeadline    int                  null,
   constraint PK_DL_SETSUBJPRICECATEG primary key (Firm, acSubject, acClassif, acInvoInOut, adDateStart)
)
go

/*==============================================================*/
/* Table: DL_SetSubjPriceItem                                   */
/*==============================================================*/
create table DL_SetSubjPriceItem (
   Firm                 int                  not null,
   acSubject            nvarchar(30) COLLATE NOCASE         not null,
   acIdent              nvarchar(16) COLLATE NOCASE         not null,
   acInvoInOut          nchar(1)             not null,
   adDateStart          datetime             not null,
   adDateEnd            datetime             null,
   acType               nchar(1)             null,
   anPrice              float                null,
   anRebate             float                null,
   anRebate2            float                null,
   anRebate3            float                null,
   acCurrency           nchar(3)             null,
   acFormula            nchar(3)             null,
   anDeliveryDeadline   int                  null,
   anPaymentDeadline    int                  null,
   ignoreCustomerDiscount	int				 null,
   constraint PK_DL_SETSUBJPRICEITEM primary key (Firm, acSubject, acIdent, acInvoInOut, adDateStart)
)
go

/*==============================================================*/
/* Table: DL_SetSubjTypePriceCateg                              */
/*==============================================================*/
create table DL_SetSubjTypePriceCateg (
   Firm                 int                  not null,
   acSubjType           nvarchar(16) COLLATE NOCASE         not null,
   acClassif            nvarchar(16) COLLATE NOCASE         not null,
   acInvoInOut          nchar(1)             not null,
   adDateStart          datetime             not null,
   adDateEnd            datetime             null,
   acType               nchar(1)             null,
   anPrice              float                null,
   anRebate             float                null,
   anRebate2            float                null,
   anRebate3            float                null,
   acCurrency           nchar(3)             null,
   acFormula            nchar(3)             null,
   anDeliveryDeadline   int                  null,
   anPaymentDeadline    int                  null,
   constraint PK_DL_SETSUBJTYPEPRICECATEG primary key (Firm, acSubjType, acClassif, acInvoInOut, adDateStart)
)
go

/*==============================================================*/
/* Table: DL_SetSubjTypePriceItem                               */
/*==============================================================*/
create table DL_SetSubjTypePriceItem (
   Firm                 int                  not null,
   acSubjType           nvarchar(16) COLLATE NOCASE         not null,
   acIdent              nvarchar(16) COLLATE NOCASE         not null,
   acInvoInOut          nchar(1)             not null,
   adDateStart          datetime             not null,
   adDateEnd            datetime             null,
   acType               nchar(1)             null,
   anPrice              float                null,
   anRebate             float                null,
   anRebate2            float                null,
   anRebate3            float                null,
   acCurrency           nchar(3)             null,
   acFormula            nchar(3)             null,
   anDeliveryDeadline   int                  null,
   anPaymentDeadline    int                  null,
   constraint PK_DL_SETSUBJTYPEPRICEITEM primary key (Firm, acSubjType, acIdent, acInvoInOut, adDateStart)
)
go

/*==============================================================*/
/* Table: DL_SetItemPriceForWrh                               	*/
/*==============================================================*/
create table DL_SetItemPriceForWrh (
   Firm                 int            		not null,
   acIdent            	nvarchar(16) COLLATE NOCASE		not null, 
   acWarehouse          nvarchar(30) COLLATE NOCASE        not null, 
   anPrStPrice			float				not null, 
   anWSPrice2			float				not null, 
   anWSPrice			float				not null,
   anDiscount			float				not null,
   anRTPrice			float				not null,
   acFormula			nchar(3)			null,	 
   anSalePrice			float				not null, 
   acCurrency			nchar(3)			null,
   adDiscountBegin		datetime			null,	  
   adDiscountEnd		datetime			null,     
   constraint PK_DL_SETITEMPRICEFORWRH primary key (Firm, acIdent, acWarehouse)
)
go

/*==============================================================*/
/* Table: ERPDocPayment                               	        */
/*==============================================================*/
CREATE TABLE ERPDocPayment
(
	Firm int not null,
	Type int not null,
	ERPDocIdd nvarchar(50) COLLATE NOCASE not null,
	TypeOfPayment nvarchar(1) COLLATE NOCASE not null,
	CashRegister nvarchar(1) COLLATE NOCASE not null,
	DefMethod int null,
	constraint PK_ERPDocPayment primary key (firm, Type, ERPDocIdd),
	constraint FK_ERPDocPayment_Types foreign key (Type) references Types (Type),
	constraint FK_ERPDocPayment_Firm  foreign key (Firm) references Firm (Firm),
	constraint FK_ERPDocPayment_ERPDocIdd  foreign key (Firm, ERPDocIdd) references SettingsErpDoc (Firm, ERPDocIdd)
)
go

/*==============================================================*/
/* Table: DelNote                                               */
/*==============================================================*/
create table DelNote (
   Firm                 int                  not null,
   DocType              int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   DelNote              nvarchar(50) COLLATE NOCASE         not null,
   TIdd                 int                  null,
   UserIdx              int                  null,
   DelNoteLink          nvarchar(50) COLLATE NOCASE         null,
   txt                  int                  null,
   DTLastChange         datetime             null,
   DTCreation           datetime             null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   Status               int                  null,
   BagSerial            nvarchar(30) COLLATE NOCASE         null,
   BagMark1             nvarchar(30) COLLATE NOCASE         null,
   BagMark2             nvarchar(30) COLLATE NOCASE         null,
   constraint PK_DELNOTE primary key (DelNote, Head, Firm, DocType),
   constraint FK_DELNOTE_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd),
   constraint FK_DELNOTE_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm),
   constraint FK_DELNOTE_REFERENCE_HEAD foreign key (Head, Firm, DocType)
      references Head (Head, Firm, DocType),
   constraint FK_DELNOTE_REFERENCE_USERS foreign key (UserIdx)
      references Users (UserIdx)
)
go

/*==============================================================*/
/* Index: I10001                                                */
/*==============================================================*/
create index I10001 on DelNote (
Firm ASC,
DocType ASC,
Head ASC,
DelNote ASC,
Status ASC
)
go

/*==============================================================*/
/* Index: I20001                                                */
/*==============================================================*/
create index I20001 on DelNote (
BagMark1 ASC,
BagMark2 ASC
)
go

/*==============================================================*/
/* Index: INDEX_DELNOTE                                         */
/*==============================================================*/
create unique index INDEX_DELNOTE on DelNote (
Firm ASC,
DocType ASC,
Head ASC,
DelNote ASC
)
go

/*==============================================================*/
/* Table: DocLocalHistoryViewAtl                                */
/*==============================================================*/
create table DocLocalHistoryViewAtl (
   CompanyCode          nvarchar(50) COLLATE NOCASE         not null,
   ProductCode          nvarchar(50) COLLATE NOCASE         not null,
   SourceDocType		int					 				not null,
   Order1               float                null,
   Order2               float                null,
   Order3               float                null,
   Order4               float                null,
   Order5               float                null,
   Date1                datetime             null,
   Date2                datetime             null,
   Date3                datetime             null,
   Date4                datetime             null,
   Date5                datetime             null,
   constraint PK_DOCLOCALHISTORYVIEWATL primary key (CompanyCode, ProductCode, SourceDocType)
)
go

/*==============================================================*/
/* Table: DocSettings                                           */
/*==============================================================*/
create table DocSettings (
   DT2_Idd              int                  not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   IValue               int                  null,
   CValue               nvarchar(50) COLLATE NOCASE         null,
   Fvalue               float                null,
   constraint PK_DOCSETTINGS primary key (DT2_Idd, Firm, Type),
   constraint FK_DOCSET_R_FI foreign key (Firm)
      references Firm (Firm),
   constraint FK_DSET_R_TY_11 foreign key (Type)
      references Types (Type)
)
go

/*==============================================================*/
/* Table: Position                                              */
/*==============================================================*/
create table Position (
   Firm                 int                  not null,
   DocType              int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   DelNote              nvarchar(50) COLLATE NOCASE         not null,
   Position             int                  not null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   Type                 int                  null,
   TIdd                 int                  null,
   UserIdx              int                  null,
   PositionLink         int                  null,
   txt                  int                  null,
   DTCreation           datetime             null,
   DTLastChange         datetime             null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   OFirm                nvarchar(50) COLLATE NOCASE         null,
   OCode                int                  null,
   OType                int                  null,
   AmountInv            float                null,
   Amount1              float                null,
   Amount2              float                null,
   Price1               float                null,
   Price2               float                null,
   AddParam1            nvarchar(50) COLLATE NOCASE         null,
   AddParam2            nvarchar(50) COLLATE NOCASE         null,
   UnitInv              int                  null,
   Unit1                int                  null,
   Unit2                int                  null,
   Status               int                  null,
   Amount1_disp         nvarchar(50) COLLATE NOCASE         null,
   Amount2_disp         nvarchar(50) COLLATE NOCASE         null,
   Discount             float                null,
   Discount2            float                null,
   Discount3            float                null,
   Notes                nvarchar(250) COLLATE NOCASE        null,
   SAPDiscountType      nvarchar(5) COLLATE NOCASE          null,
   SAPDiscount2Type     nvarchar(5) COLLATE NOCASE          null,
   SAPDiscount3Type     nvarchar(5) COLLATE NOCASE          null,
   SAPPacking           nvarchar(1) COLLATE NOCASE          null,
   PosWeight            int                  null,
   EHIdd                int                  null,
   Changed              int                  null,
   PrintAmountDiscount  float				 null,
   PrintAmountTax		float				 null,
   PackQty				float				 null,
   ExpectedAmount       float                null,   
   TransferDiscountToErp int                 null,
   OrderPackAmount      float                null,
   PriceManuallyModified int				 null,
   NumberOfScans        int                  null,
   constraint PK_POSITION primary key (Position, Head, DelNote, Firm, DocType),
   constraint FK_POSITION_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type),
   constraint FK_POSITION_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd),
   constraint FK_POSITION_REFERENCE_DELNOTE foreign key (DelNote, Head, Firm, DocType)
      references DelNote (DelNote, Head, Firm, DocType),
   constraint FK_POSITION_REFERENCE_USERS foreign key (UserIdx)
      references Users (UserIdx),
   constraint FK_POSITION_TYPES1 foreign key (Unit1)
      references Types (Type),
   constraint FK_POSITION_TYPES2 foreign key (Unit2)
      references Types (Type),
   constraint FK_POSITION_REFERENCE_TYPES foreign key (UnitInv)
      references Types (Type)
)
go

/*==============================================================*/
/* Table: OutSerial                                             */
/*==============================================================*/
create table OutSerial (
   Firm                 int                  not null,
   DocType              int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   DelNote              nvarchar(50) COLLATE NOCASE         not null,
   Position             int                  not null,
   OutSerial            int                  not null,
   TIdd                 int                  null,
   UserIdx              int                  null,
   OutSerialLink        int                  null,
   txt                  int                  null,
   DTCreation           datetime             null,
   DTLastChange         datetime             null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   Serial               nvarchar(50) COLLATE NOCASE         null,
   SerialLink           int                  null,
   Status               int                  null,
   constraint PK_OUTSERIAL primary key (OutSerial, Head, DelNote, Position, Firm, DocType),
   constraint FK_OUTSERIA_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd),
   constraint FK_OUTSERIA_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm),
   constraint FK_OUTSERIA_REFERENCE_POSITION foreign key (Position, Head, DelNote, Firm, DocType)
      references Position (Position, Head, DelNote, Firm, DocType),
   constraint FK_OUTSERIA_REFERENCE_USERS foreign key (UserIdx)
      references Users (UserIdx)
)
go

/*==============================================================*/
/* Table: Documents_Stock_link                                  */
/*==============================================================*/
create table Documents_Stock_link (
   Firm                 int                  null,
   Hea_DocType          int                  null,
   Del_DocType          int                  null,
   Pos_DocType          int                  null,
   DocType              int                  null,
   DIdd                 int                  not null,
   SIdd                 int                  null,
   Head                 nvarchar(50) COLLATE NOCASE         null,
   DelNote              nvarchar(50) COLLATE NOCASE         null,
   Position             int                  null,
   OutSerial            int                  null,
   Change               datetime             null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   Status               int                  null,
   constraint PK_DOCUMENTS_STOCK_LINK primary key (DIdd),
   constraint FK_DOCUMENT_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm),
   constraint FK_DOCUMENT_REFERENCE_STOCK foreign key (SIdd)
      references Stock (SIdd),
   constraint FK_DOCUMENT_REFERENCE_HEAD foreign key (Head, Firm, DocType)
      references Head (Head, Firm, DocType),
   constraint FK_DOCUMENT_REFERENCE_DELNOTE foreign key (DelNote, Head, Firm, DocType)
      references DelNote (DelNote, Head, Firm, DocType),
   constraint FK_DOCUMENT_REFERENCE_POSITION foreign key (Position, Head, DelNote, Firm, DocType)
      references Position (Position, Head, DelNote, Firm, DocType),
   constraint FK_DOCUMENT_REFERENCE_OUTSERIA foreign key (OutSerial, Head, DelNote, Position, Firm, DocType)
      references OutSerial (OutSerial, Head, DelNote, Position, Firm, DocType)
)
go

/*==============================================================*/
/* Index: I00013                                                */
/*==============================================================*/
create index I00013 on Documents_Stock_link (
Firm ASC,
Head ASC,
DelNote ASC,
Position ASC,
OutSerial ASC
)
go

/*==============================================================*/
/* Index: DSL_Stock_Sidd                                        */
/*==============================================================*/
create index DSL_Stock_Sidd on Documents_Stock_link (
SIdd ASC
)
go

/*==============================================================*/
/* Table: SettingsErpDoc                                        */
/*==============================================================*/
create table SettingsErpDoc (
   Firm                 int                  not null,
   ErpDocIdd            nvarchar(50) COLLATE NOCASE         not null,
   DocType              int                  null,
   InDocType            int                  null,
   DocName              nvarchar(50) COLLATE NOCASE         null,
   ShortDocName         nvarchar(50) COLLATE NOCASE         null,
   IssueWarehouseCode   nvarchar(50) COLLATE NOCASE         null,
   ReceiveWarehouseCode nvarchar(50) COLLATE NOCASE         null,
   Type                 int                  null,
   LastUsedDocumentNumber int                  null,
   Status               int                  null,
   IssueWarehouseType   int                  null,
   ReceiveWarehouseType int                  null,
   SendEmail 			int 				null,
   ReportId 			int 				null,
   OverrideErpDocIdd	nvarchar(50) COLLATE NOCASE			null,
   AutoGenerateErpNumber	bit			null,
   constraint PK_SETTINGSERPDOC primary key (Firm, ErpDocIdd),
   constraint FK_SET_REF_SUB_1 foreign key (ReceiveWarehouseCode, Firm, Type)
      references Subject (Code, Firm, Type),
   constraint FK_SET_REF_SUB2 foreign key (IssueWarehouseCode, Firm, Type)
      references Subject (Code, Firm, Type),
   constraint FK_SET_REF_TY_4 foreign key (DocType)
      references Types (Type),
   constraint FK_SET_REF_TY foreign key (Type)
      references Types (Type),
   constraint FK_SET_REF_FI_1 foreign key (Firm)
      references Firm (Firm),
   constraint FK_SettingsErpDoc_InDocType foreign key (InDocType)
      references Types (Type),
   constraint FK_SettingsErpDoc_Types_ReportId foreign key (ReportId) 
	  references Types (Type)
)
go

/*==============================================================*/
/* Table: ErpHeads                                              */
/*==============================================================*/
create table ErpHeads (
   PartD                nvarchar(15) COLLATE NOCASE         null,
   PartC                nvarchar(15) COLLATE NOCASE         null,
   PartB                nvarchar(15) COLLATE NOCASE         null,
   PartA                nvarchar(15) COLLATE NOCASE         null,
   EHIdd                int                  not null,
   TIdd                 int                  null,
   Firm                 int                  null,
   ErpDocIdd            nvarchar(50) COLLATE NOCASE         null,
   Status               int                  null,
   Add1                 nvarchar(50) COLLATE NOCASE         null,
   Add2                 nvarchar(50) COLLATE NOCASE         null,
   constraint PK_ERPHEADS primary key (EHIdd),
   constraint FK_EH_REF_SET foreign key (Firm, ErpDocIdd)
      references SettingsErpDoc (Firm, ErpDocIdd),
   constraint FK_EHEADS_REF_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Table: FilterHead                                            */
/*==============================================================*/
create table FilterHead (
   Type                 int                  not null,
   FilterName           nvarchar(50) COLLATE NOCASE         not null,
   Id                   int                  not null,
   Description          nvarchar(150) COLLATE NOCASE        null,
   AddParam1            nvarchar(50) COLLATE NOCASE         null,
   InsertDate           datetime             null,
   ChangeDate           datetime             null,
   constraint PK_FILTERHEAD primary key (Type, Id),
   constraint FK_FILTERHE_REFERENCE_TYPES foreign key (Type)
      references Types (Type)
)
go

/*==============================================================*/
/* Table: FilterPosition                                        */
/*==============================================================*/
create table FilterPosition (
   Type                 int                  not null,
   Id                   int                  not null,
   Position             int                  not null,
   Firm                 int                  null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   constraint PK_FILTERPOSITION primary key (Type, Id, Position),
   constraint FK_FILTERPO_REFERENCE_FILTERHE foreign key (Type, Id)
      references FilterHead (Type, Id),
   constraint FK_FILTERPO_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Index: I00017                                                */
/*==============================================================*/
create index I00017 on Head (
Code ASC,
Firm ASC,
Type ASC
)
go

/*==============================================================*/
/* Index: I00018                                                */
/*==============================================================*/
create index I00018 on Head (
Firm ASC,
Head ASC,
DocType ASC,
Status ASC
)
go

/*==============================================================*/
/* Index: """NonClusteredIndex-20130927-144420"""               */
/*==============================================================*/
create index """NonClusteredIndex-20130927-144420""" on Head (
Firm ASC,
Status ASC
)
go

/*==============================================================*/
/* Table: HeadLinkErpHeads                                      */
/*==============================================================*/
create table HeadLinkErpHeads (
   EHIdd                int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   DocType              int                  not null,
   TIdd                 int                  null,
   Add1                 nvarchar(50) COLLATE NOCASE         null,
   Add2                 nvarchar(50) COLLATE NOCASE         null,
   Status               int                  null,
   LinkIdd              int                  not null,
   constraint PK_HEADLINKERPHEADS primary key (EHIdd, Head, Firm, DocType, LinkIdd),
   constraint FK_HEAD_REF_HEAD foreign key (Head, Firm, DocType)
      references Head (Head, Firm, DocType),
   constraint FK_HEAD_REF_TXT foreign key (TIdd)
      references Txt (TIdd),
   constraint FK_HEAD_REF_EHEADS foreign key (EHIdd)
      references ErpHeads (EHIdd)
)
go

/*==============================================================*/
/* Index: I00086                                                */
/*==============================================================*/
create index I00086 on HeadLinkErpHeads (
Head ASC,
Firm ASC,
DocType ASC
)
go

/*==============================================================*/
/* Table: Inventory                                             */
/*==============================================================*/
create table Inventory (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   Task                 int                  null,
   TIdd                 int                  null,
   BDataIdx             int                  null,
   TL_Idd               int                  null,
   Change               datetime             null,
   Status               int                  not null,
   Name_full            nvarchar(250) COLLATE NOCASE        null,
   by_pcs               int                  null,
   bad_sticker          int                  null,
   for_remove           int                  null,
   AddParam1            nvarchar(50) COLLATE NOCASE         null,
   AddParam2            nvarchar(50) COLLATE NOCASE         null,
   Last                 datetime             null,
   Next                 datetime             null,
   New                  int                  null,
   CustomField1         nvarchar(250) COLLATE NOCASE        null,
   CustomField2         nvarchar(250) COLLATE NOCASE        null,
   CustomField3         nvarchar(250) COLLATE NOCASE        null,
   CustomField4         nvarchar(250) COLLATE NOCASE        null,
   CustomField5         nvarchar(250) COLLATE NOCASE        null,
   constraint PK_INVENTORY primary key (Code, Firm, Type),
   constraint FK_INVENTOR_REFERENCE_BDATA foreign key (BDataIdx)
      references BData (BDataIdx),
   constraint FK_INVENTOR_REFERENCE_TIMELINE foreign key (TL_Idd)
      references TimeLine (TL_Idd),
   constraint FK_INVENTOR_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type),
   constraint FK_INVENTOR_REFERENCE_TASKS foreign key (Task)
      references Tasks (Task),
   constraint FK_INVENTOR_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Table: Locations                                             */
/*==============================================================*/
create table Locations (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   Task                 int                  null,
   TIdd                 int                  null,
   LocCode              nvarchar(50) COLLATE NOCASE         null,
   LocFirm              int                  null,
   LocType              int                  null,
   Change               datetime             null,
   Name_full            nvarchar(250) COLLATE NOCASE        null,
   Status               int                  not null,
   AddInt1              int                  null,
   AddInt2              int                  null,
   AddChar1             nvarchar(50) COLLATE NOCASE         null,
   AddChar2             nvarchar(50) COLLATE NOCASE         null,
   Weight               int                  null,
   CustomField1         nvarchar(250) COLLATE NOCASE        null,
   CustomField2         nvarchar(250) COLLATE NOCASE        null,
   CustomField3         nvarchar(250) COLLATE NOCASE        null,
   CustomField4         nvarchar(250) COLLATE NOCASE        null,
   CustomField5         nvarchar(250) COLLATE NOCASE        null,
   New                  int                  null,
   LeftExtent           int                  null,
   RightExtent          int                  null,
   constraint PK_LOCATIONS primary key (Code, Firm, Type),
   constraint FK_LOC_REF_SUBLoc foreign key (LocCode, LocFirm, LocType)
      references Subject (Code, Firm, Type),
   constraint FK_LOCATION_REFERENCE_SUBJECT foreign key (Code, Firm, Type)
      references Subject (Code, Firm, Type),
   constraint FK_LOCATION_REFERENCE_TASKS foreign key (Task)
      references Tasks (Task),
   constraint FK_LOCATION_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Index: I00087                                                */
/*==============================================================*/
create unique index I00087 on Locations (
LocCode ASC,
LocFirm ASC,
LocType ASC,
Code ASC,
Firm ASC,
Type ASC
)
go

/*==============================================================*/
/* Table: MenuGroup                                             */
/*==============================================================*/
create table MenuGroup (
   MenuGroupIdx         int                  not null,
   TitleGroup           nvarchar(50) COLLATE NOCASE         null,
   constraint PK_MENUGROUP primary key (MenuGroupIdx)
)
go

/*==============================================================*/
/* Table: Menu                                                  */
/*==============================================================*/
create table Menu (
   MenuIdx              int                  not null,
   MenuGroupIdx         int                  not null,
   TitleMenu            nvarchar(50) COLLATE NOCASE         not null,
   constraint PK_MENU primary key (MenuIdx, MenuGroupIdx),
   constraint FK_ME_SPAD_MGRO foreign key (MenuGroupIdx)
      references MenuGroup (MenuGroupIdx)
)
go

/*==============================================================*/
/* Index: I00022                                                */
/*==============================================================*/
create index I00022 on Menu (
MenuGroupIdx ASC
)
go

/*==============================================================*/
/* Table: MicroFilm                                             */
/*==============================================================*/
create table MicroFilm (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   Task                 int                  null,
   TIdd                 int                  null,
   change               datetime             null,
   AddParam1            nvarchar(50) COLLATE NOCASE         null,
   AddParam2            nvarchar(50) COLLATE NOCASE         null,
   AddParam3            nvarchar(50) COLLATE NOCASE         null,
   AddParam4            nvarchar(50) COLLATE NOCASE         null,
   SteviloPosnetkov     int                  null,
   DatumSnemanja        datetime             null,
   SteviloDK            int                  null,
   DatumArhiviranja     datetime             null,
   DatumOddaje          datetime             null,
   AddParam5            int                  null,
   AddParam6            int                  null,
   Status               int                  not null,
   "Insert"             datetime             null,
   constraint PK_MICROFILM primary key (Code, Firm, Type),
   constraint FK_MICROFIL_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type),
   constraint FK_MICROFIL_REFERENCE_TASKS foreign key (Task)
      references Tasks (Task),
   constraint FK_MICROFIL_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Table: Racun                                                 */
/*==============================================================*/
create table Racun (
   Firm                 int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   DocType              int                  not null,
   Oib                  nvarchar(11) COLLATE NOCASE         not null,
   USustPdv             bit                  not null,
   DatumVrijeme         datetime             not null,
   OznSlijed            nvarchar(1) COLLATE NOCASE          not null,
   BrOznRac             nvarchar(20) COLLATE NOCASE         not null,
   OznPosPr             nvarchar(20) COLLATE NOCASE         not null,
   OznNapUr             nvarchar(20) COLLATE NOCASE         not null,
   IznosOslobPdv        float                null,
   IznosMarza           float                null,
   IznosNePodlOpor      float                null,
   UznosNePodlOpor      float                null,
   IznosUkupno          float                not null,
   NacinPlac            nvarchar(1) COLLATE NOCASE          not null,
   OibOper              nvarchar(11) COLLATE NOCASE         not null,
   ZastKod              nvarchar(32) COLLATE NOCASE         not null,
   NakDost              bit                  not null,
   ParagonBrRac         nvarchar(100) COLLATE NOCASE        null,
   SpecNamj             nvarchar(1000) COLLATE NOCASE       null,
   Jir                  nvarchar(36) COLLATE NOCASE         null,
   RacunTypeJson        nvarchar(2000) COLLATE NOCASE       null,
   constraint PK_RACUN primary key (Head, Firm, DocType)
)
go

/*==============================================================*/
/* Table: Naknada                                               */
/*==============================================================*/
create table Naknada (
   Firm                 int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   DocType              int                  not null,
   NazivNaknade         nvarchar(100) COLLATE NOCASE        not null,
   IznosNaknade         float                not null,
   constraint FK_NAKNADA_REFERENCE_RACUN foreign key (Head, Firm, DocType)
      references Racun (Head, Firm, DocType)
)
go

/*==============================================================*/
/* Table: OPersons                                              */
/*==============================================================*/
create table OPersons (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   FName                nvarchar(50) COLLATE NOCASE         null,
   MName                nvarchar(50) COLLATE NOCASE         null,
   SName                nvarchar(50) COLLATE NOCASE         null,
   EMSO                 nvarchar(13) COLLATE NOCASE         null,
   OSS                  nvarchar(10) COLLATE NOCASE         null,
   OE                   nvarchar(50) COLLATE NOCASE         null,
   Status               int                  not null,
   InsertDateTime       datetime             null,
   ChangeDateTime       datetime             null,
   PrintDateTime        datetime             null,
   constraint PK_OPERSONS primary key (Code, Firm, Type),
   constraint FK_OPERSONS_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type)
)
go

/*==============================================================*/
/* Table: OSearch                                               */
/*==============================================================*/
create table OSearch (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   IntStart             int                  null,
   IntEnd               int                  null,
   StringStart          nvarchar(50) COLLATE NOCASE         null,
   StringEnd            nvarchar(50) COLLATE NOCASE         null,
   TimeStart            datetime             null,
   TimeEnd              datetime             null,
   Idd                  int                  not null,
   DataSearchType       int                  not null,
   AddParam1            nvarchar(50) COLLATE NOCASE         null,
   AddParam2            int                  null,
   Status               int                  null,
   constraint PK_OSEARCH primary key (Idd),
   constraint FK_OSEARCH_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type),
   constraint FK_OSEARCH_REFERENCE_TYPES foreign key (DataSearchType)
      references Types (Type)
)
go

/*==============================================================*/
/* Index: I00089                                                */
/*==============================================================*/
create index I00089 on OSearch (
IntStart ASC,
DataSearchType ASC,
AddParam1 ASC,
AddParam2 ASC
)
go

/*==============================================================*/
/* Index: I00090                                                */
/*==============================================================*/
create index I00090 on OSearch (
IntEnd ASC,
DataSearchType ASC,
AddParam1 ASC,
AddParam2 ASC
)
go

/*==============================================================*/
/* Index: I00091                                                */
/*==============================================================*/
create index I00091 on OSearch (
StringStart ASC,
DataSearchType ASC,
AddParam1 ASC,
AddParam2 ASC
)
go

/*==============================================================*/
/* Index: I00092                                                */
/*==============================================================*/
create index I00092 on OSearch (
StringEnd ASC,
DataSearchType ASC,
AddParam1 ASC,
AddParam2 ASC
)
go

/*==============================================================*/
/* Index: I00093                                                */
/*==============================================================*/
create index I00093 on OSearch (
TimeStart ASC,
DataSearchType ASC,
AddParam1 ASC,
AddParam2 ASC
)
go

/*==============================================================*/
/* Index: I00094                                                */
/*==============================================================*/
create index I00094 on OSearch (
TimeEnd ASC,
DataSearchType ASC,
AddParam1 ASC,
AddParam2 ASC
)
go

/*==============================================================*/
/* Index: I00025                                                */
/*==============================================================*/
create index I00025 on Object (
Name ASC
)
go

/*==============================================================*/
/* Index: I00026                                                */
/*==============================================================*/
create index I00026 on Object (
Status ASC
)
go

/*==============================================================*/
/* Table: ObjectLists                                           */
/*==============================================================*/
create table ObjectLists (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   Task                 int                  null,
   TIdd                 int                  null,
   TL_Idd               int                  null,
   change               datetime             null,
   Name_full            nvarchar(250) COLLATE NOCASE        null,
   Status               int                  not null,
   constraint PK_OBJECTLISTS primary key (Code, Firm, Type),
   constraint FK_OBJECTLI_REFERENCE_TIMELINE foreign key (TL_Idd)
      references TimeLine (TL_Idd),
   constraint FK_OBJECTLI_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type),
   constraint FK_OBJECTLI_REFERENCE_TASKS foreign key (Task)
      references Tasks (Task),
   constraint FK_OBJECTLI_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Table: Object_Interconnections                               */
/*==============================================================*/
create table Object_Interconnections (
   DateTime1            datetime             null,
   DateTime2            datetime             null,
   OIC_Idd              int                  not null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   Firm                 int                  null,
   Type                 int                  null,
   Prev_Code            nvarchar(50) COLLATE NOCASE         null,
   Prev_Firm            int                  null,
   Prev_Type            int                  null,
   Curr_Code            nvarchar(50) COLLATE NOCASE         null,
   Curr_Firm            int                  null,
   Curr_Type            int                  null,
   Priority             int                  null,
   Status               int                  not null,
   PackAmount           float                null,
   constraint PK_OBJECT_INTERCONNECTIONS primary key (OIC_Idd),
   constraint FK_OBJECT_I_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type),
   constraint OBJ_I_OBJ_PREV foreign key (Prev_Code, Prev_Firm, Prev_Type)
      references Object (Code, Firm, Type),
   constraint OBJ_I_OBJ_NEXT foreign key (Curr_Code, Curr_Firm, Curr_Type)
      references Object (Code, Firm, Type),
   constraint FK_OBJECT_I_REFERENCE_TYPES foreign key (Priority)
      references Types (Type)
)
go

/*==============================================================*/
/* Index: I00027                                                */
/*==============================================================*/
create index I00027 on Object_Interconnections (
Code ASC,
Firm ASC,
Type ASC
)
go

/*==============================================================*/
/* Index: I00028                                                */
/*==============================================================*/
create index I00028 on Object_Interconnections (
Prev_Code ASC,
Prev_Firm ASC,
Prev_Type ASC
)
go

/*==============================================================*/
/* Index: I00029                                                */
/*==============================================================*/
create index I00029 on Object_Interconnections (
Curr_Code ASC,
Curr_Firm ASC,
Curr_Type ASC
)
go

/*==============================================================*/
/* Table: Object_Locations_Interconnections                     */
/*==============================================================*/
create table Object_Locations_Interconnections (
   ObjectCode           nvarchar(50) COLLATE NOCASE         not null,
   ObjectFirm           int                  not null,
   ObjectType           int                  not null,
   LocCode              nvarchar(50) COLLATE NOCASE         not null,
   LocFirm              int                  not null,
   LocType              int                  not null,
   constraint PK_OBJECT_LOCATIONS primary key (ObjectCode, ObjectFirm, ObjectType, LocCode, LocFirm, LocType),
   constraint FK_LOCATIONS_CODE_FIRM_TYPE foreign key (LocCode, LocFirm, LocType)
      references Locations (Code, Firm, Type),
   constraint FK_OBJECT_CODE_FIRM_TYPE foreign key (ObjectCode, ObjectFirm, ObjectType)
      references Object (Code, Firm, Type)
)
go

/*==============================================================*/
/* Table: Terminals                                             */
/*==============================================================*/
create table Terminals (
   TerminalIdx          int                  not null,
   TerminalID           nvarchar(50) COLLATE NOCASE         null,
   status               int                  null,
   constraint PK_TERMINALS primary key (TerminalIdx)
)
go

/*==============================================================*/
/* Table: OrderDelegation                                       */
/*==============================================================*/
create table OrderDelegation (
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   DocType              int                  not null,
   TerminalIdx          int                  not null,
   DTCreation           datetime             null,
   DTLastChange         datetime             null,
   Status               int                  null,
   constraint PK_ORDERDELEGATION primary key (Head, Firm, DocType, TerminalIdx),
   constraint FK_ORDERDEL_REFERENCE_HEAD foreign key (Head, Firm, DocType)
      references Head (Head, Firm, DocType),
   constraint FK_ORDERDEL_REFERENCE_TERMINAL foreign key (TerminalIdx)
      references Terminals (TerminalIdx)
)
go

/*==============================================================*/
/* Table: OstaliPor                                             */
/*==============================================================*/
create table OstaliPor (
   Firm                 int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   DocType              int                  not null,
   NazivPoreza          nvarchar(100) COLLATE NOCASE        not null,
   PoreznaStopa         float                not null,
   Osnovica             float                not null,
   IznosPoreza          float                not null,
   constraint FK_OSTALIPOR_REFERENCE_RACUN foreign key (Head, Firm, DocType)
      references Racun (Head, Firm, DocType)
)
go

/*==============================================================*/
/* Index: INDEX_OUTSERIAL                                       */
/*==============================================================*/
create unique index INDEX_OUTSERIAL on OutSerial (
Firm ASC,
DocType ASC,
Head ASC,
DelNote ASC,
Position ASC,
OutSerial ASC
)
go

/*==============================================================*/
/* Table: Overrides                                             */
/*==============================================================*/
create table Overrides (
   OverridesIdd         int                  not null,
   BIdd                 int                  null,
   Firm                 int                  null,
   Type                 int                  null,
   Object_Code          nvarchar(50) COLLATE NOCASE         null,
   Subject_Code         nvarchar(50) COLLATE NOCASE         null,
   Change               datetime             null,
   Status               int                  null,
   PriceMin             float                null,
   PriceMax             float                null,
   constraint PK_OVERRIDES primary key (OverridesIdd),
   constraint FK_OVER_REF_BAR foreign key (BIdd)
      references Barcode (BIdd),
   constraint FK_OVER_REF_TY1 foreign key (Type)
      references Types (Type),
   constraint FK_OVER_REF_FIRM2 foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Index: Index_2                                               */
/*==============================================================*/
create index Index_2 on Overrides (
BIdd ASC
)
go

/*==============================================================*/
/* Index: I00095                                                */
/*==============================================================*/
create index I00095 on Overrides (
Firm ASC,
Type ASC,
Object_Code ASC
)
go

/*==============================================================*/
/* Index: I00096                                                */
/*==============================================================*/
create index I00096 on Overrides (
Firm ASC,
Type ASC,
Subject_Code ASC
)
go

/*==============================================================*/
/* Table: OverridesAddInfo                                      */
/*==============================================================*/
create table OverridesAddInfo (
   OverridesAddInfo     int                  not null,
   OverridesIdd         int                  null,
   Firm                 int                  null,
   Type                 int                  null,
   AddInt1              float                null,
   AddInt2              int                  null,
   AddChar1             nvarchar(50) COLLATE NOCASE         null,
   AddChar2             nvarchar(50) COLLATE NOCASE         null,
   Status               int                  null,
   BoxInLayer           int                  null,
   LifeInMonths         int                  null,
   constraint PK_OVERRIDESADDINFO primary key (OverridesAddInfo),
   constraint FK_OVER_REF_OVER foreign key (OverridesIdd)
      references Overrides (OverridesIdd),
   constraint FK_OVER_REF_FIRM3 foreign key (Firm)
      references Firm (Firm),
   constraint FK_OVER_REF_TYPES foreign key (Type)
      references Types (Type)
)
go

/*==============================================================*/
/* Index: Index_6                                               */
/*==============================================================*/
create index Index_6 on OverridesAddInfo (
OverridesIdd ASC
)
go

/*==============================================================*/
/* Table: Pdv                                                   */
/*==============================================================*/
create table Pdv (
   Firm                 int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   DocType              int                  not null,
   PoreznaStopa         float                not null,
   Osnovica             float                not null,
   IznosPoreza          float                not null,
   constraint FK_PDV_REFERENCE_RACUN foreign key (Head, Firm, DocType)
      references Racun (Head, Firm, DocType)
)
go

/*==============================================================*/
/* Table: Pnp                                                   */
/*==============================================================*/
create table Pnp (
   Firm                 int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   DocType              int                  not null,
   PoreznaStopa         float                not null,
   Osnovica             float                not null,
   IznosPoreza          float                not null,
   constraint FK_PNP_REFERENCE_RACUN foreign key (Head, Firm, DocType)
      references Racun (Head, Firm, DocType)
)
go

/*==============================================================*/
/* Table: Poll                                                  */
/*==============================================================*/
create table Poll (
   idx                  int                  not null,
   pollName             nvarchar(50) COLLATE NOCASE         null,
   constraint PK_POLL primary key (idx)
)
go

/*==============================================================*/
/* Table: PollHeader                                            */
/*==============================================================*/
create table PollHeader (
   TIdd                 int                  null,
   idx                  int                  null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   DocType              int                  not null,
   changed              datetime             null,
   Status               int                  null,
   constraint PK_POLLHEADER primary key (Head, Firm, DocType),
   constraint FK_POLLHEAD_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd),
   constraint FK_POLLHEAD_REFERENCE_POLL foreign key (idx)
      references Poll (idx),
   constraint FK_POLLHEAD_REFERENCE_HEAD foreign key (Head, Firm, DocType)
      references Head (Head, Firm, DocType)
)
go

/*==============================================================*/
/* Table: PollQuestions                                         */
/*==============================================================*/
create table PollQuestions (
   QuestionID           int                  not null,
   PollQuestion         nvarchar(150) COLLATE NOCASE        null,
   constraint PK_POLLQUESTIONS primary key (QuestionID)
)
go

/*==============================================================*/
/* Table: PollLink                                              */
/*==============================================================*/
create table PollLink (
   idx                  int                  not null,
   QuestionID           int                  not null,
   OrderNr              int                  null,
   constraint AK_KEY_1_POLLLINK unique (idx, QuestionID),
   constraint FK_POLLLINK_REFERENCE_POLL foreign key (idx)
      references Poll (idx),
   constraint FK_POLLLINK_REFERENCE_POLLQUES foreign key (QuestionID)
      references PollQuestions (QuestionID)
)
go

/*==============================================================*/
/* Table: PollResults                                           */
/*==============================================================*/
create table PollResults (
   QuestionId           int                  not null,
   Selections           nvarchar(50) COLLATE NOCASE         null,
   TextNum              nvarchar(50) COLLATE NOCASE         null,
   Idd                  int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         null,
   Firm                 int                  null,
   DocType              int                  null,
   Status               int                  null,
   constraint PK_POLLRESULTS primary key (Idd),
   constraint FK_POLLRESU_REFERENCE_POLLHEAD foreign key (Head, Firm, DocType)
      references PollHeader (Head, Firm, DocType)
)
go

/*==============================================================*/
/* Index: I10002                                                */
/*==============================================================*/
create index I10002 on Position (
Firm ASC,
DocType ASC,
Head ASC,
DelNote ASC,
Position ASC,
Status ASC
)
go

/*==============================================================*/
/* Index: INDEX_POSITION                                        */
/*==============================================================*/
create index INDEX_POSITION on Position (
Firm ASC,
DocType ASC,
Head ASC,
DelNote ASC,
Position ASC
)
go

/*==============================================================*/
/* Index: IPosition_FirmCodeType                                */
/*==============================================================*/
create index IPosition_FirmCodeType on Position (
Firm ASC,
Code ASC,
Type ASC
)
go

/*==============================================================*/
/* Table: PoslovniProstor                                       */
/*==============================================================*/
create table PoslovniProstor (
   Firm                 int                  not null,
   Head                 nvarchar(50) COLLATE NOCASE         not null,
   DocType              int                  not null,
   Oib                  nvarchar(11) COLLATE NOCASE         not null,
   OznPoslProstora      nvarchar(20) COLLATE NOCASE         not null,
   Ulica                nvarchar(100) COLLATE NOCASE        null,
   KucniBroj            nvarchar(4) COLLATE NOCASE          null,
   KucniBrojDodatak     nvarchar(4) COLLATE NOCASE          null,
   BrojPoste            nvarchar(12) COLLATE NOCASE         null,
   Naselje              nvarchar(35) COLLATE NOCASE         null,
   Opcina               nvarchar(35) COLLATE NOCASE         null,
   OstaliTipoviPP       nvarchar(100) COLLATE NOCASE        null,
   RadnoVrijeme         nvarchar(1000) COLLATE NOCASE       not null,
   DatumPocetkaPrimjene datetime             not null,
   OznakaZatvaranja     nvarchar(1) COLLATE NOCASE          null,
   SpecNamj             nvarchar(1000) COLLATE NOCASE       null,
   Poslan               int                  not null,
   constraint PK_POSLOVNIPROSTOR primary key (Head, Firm, DocType)
)
go

/*==============================================================*/
/* Table: PriceCorrection_ProBit                                */
/*==============================================================*/
create table PriceCorrection_ProBit (
   Firm                 int                  not null,
   Link_Sub             nvarchar(50) COLLATE NOCASE         not null,
   Link_Obj             nvarchar(50) COLLATE NOCASE         not null,
   Type_Obj             int                  not null,
   VersionNum           int                  not null,
   DateStart            datetime             not null,
   DateStop             datetime             null,
   ProPriceList         nvarchar(5) COLLATE NOCASE          null,
   price                float                null,
   discount1            float                null,
   discount2            float                null,
   discount3            float                null,
   constraint PK_PRICECORRECTION_PROBIT primary key (Link_Sub, Link_Obj, Type_Obj, VersionNum, DateStart, Firm)
)
go

/*==============================================================*/
/* Index: Index_5                                               */
/*==============================================================*/
create index Index_5 on PriceCorrection_ProBit (
Firm ASC,
Link_Sub ASC,
Link_Obj ASC,
Type_Obj ASC,
VersionNum ASC
)
go

/*==============================================================*/
/* Index: INDEX_PRICECORRECTION_PROBIT                          */
/*==============================================================*/
create index INDEX_PRICECORRECTION_PROBIT on PriceCorrection_ProBit (
Firm ASC,
Link_Sub ASC,
Link_Obj ASC,
Type_Obj ASC,
VersionNum ASC,
DateStart ASC
)
go

/*==============================================================*/
/* Table: PriceCorrections_Price_ProBit                         */
/*==============================================================*/
create table PriceCorrections_Price_ProBit (
   ProPriceList         nvarchar(5) COLLATE NOCASE          not null,
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   VersionNum           int                  not null,
   price                float                null,
   DateStart            datetime             not null,
   constraint PK_PRICECORRECTIONS_PRICE_PROB primary key (ProPriceList, Code, Firm, Type, VersionNum, DateStart)
)
go

/*==============================================================*/
/* Table: PrintTemplates                                        */
/*==============================================================*/
create table PrintTemplates (
   TemplateIdd          int                  not null,
   Firm                 int                  not null,
   Name                 nvarchar(50) COLLATE NOCASE         null,
   DisplayName          nvarchar(50) COLLATE NOCASE         null,
   Content              image                null,
   Status               int                  not null,
   constraint PK_PRINTTEMPLATES primary key (TemplateIdd, Firm),
   constraint FK_PRINTTEM_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Table: Products                                              */
/*==============================================================*/
create table Products (
   Code						nvarchar(50) COLLATE NOCASE         not null,
   Firm						int                  not null,
   Type						int                  not null,
   Task						int                  null,
   TIdd						int                  null,
   Change					datetime             null,
   Name_full				nvarchar(250) COLLATE NOCASE        null,
   Categ1					nvarchar(50) COLLATE NOCASE         null,
   Categ2					nvarchar(50) COLLATE NOCASE         null,
   Categ3					nvarchar(50) COLLATE NOCASE         null,
   AddParam1				nvarchar(50) COLLATE NOCASE         null,
   AddParam2				nvarchar(50) COLLATE NOCASE         null,
   AddParam3				nvarchar(50) COLLATE NOCASE         null,
   AddParam4				nvarchar(50) COLLATE NOCASE         null,
   Count					nvarchar(50) COLLATE NOCASE         null,
   Unit1					int                  null,
   Unit2					int                  null,
   Status					int                  not null,
   Price1					float                null,
   Price2					float                null,
   Price3					float                null,
   PriceMPC					float                null,
   Tax						float                null,
   Discount					float                null,
   Pack						float                null,
   proClass					nvarchar(10) COLLATE NOCASE         null,
   proGroup1				nvarchar(10) COLLATE NOCASE         null,
   proGroup2				nvarchar(10) COLLATE NOCASE         null,
   proGroup3				nvarchar(10) COLLATE NOCASE         null,
   proGroup4				nvarchar(10) COLLATE NOCASE         null,
   SaopDiscountGroup		nvarchar(20) COLLATE NOCASE NULL,
   SaopItemStatus			nvarchar(3) COLLATE NOCASE NULL,
   PriceSuggMin				float                null,
   PriceSuggMax				float                null,
   Categ4					nvarchar(50) COLLATE NOCASE null,
   Categ5					nvarchar(50) COLLATE NOCASE null,
   CustomField1				nvarchar(250) COLLATE NOCASE        null,
   CustomField2				nvarchar(250) COLLATE NOCASE        null,
   CustomField3				nvarchar(250) COLLATE NOCASE        null,
   CustomField4				nvarchar(250) COLLATE NOCASE        null,
   CustomField5				nvarchar(250) COLLATE NOCASE        null,
   CustomField6				nvarchar(250) COLLATE NOCASE        null,
   CustomField7				nvarchar(250) COLLATE NOCASE        null,
   CustomField8				nvarchar(250) COLLATE NOCASE        null,
   CustomField9				nvarchar(250) COLLATE NOCASE        null,
   CustomField10			nvarchar(250) COLLATE NOCASE        null,
   CustomField11			nvarchar(250) COLLATE NOCASE        null,   
   CustomField12			nvarchar(250) COLLATE NOCASE        null,   
   CustomField13			nvarchar(250) COLLATE NOCASE        null,      
   BookkeepingType 			nvarchar(50)  COLLATE NOCASE 		null,
   FixedAsset 				int 				 null,
   FixedAssetSerial 		nvarchar(50) COLLATE NOCASE 		null,
   ErpProductManagement		nvarchar(50) COLLATE NOCASE         null,
   AdrCategory				int 								null,
   AdrCategoryAmountFactor	decimal(18, 4)						null,
   constraint PK_PRODUCTS primary key (Code, Firm, Type),
   constraint FK_PRODUCTS_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type),
   constraint FK_PRODUCTS_REFERENCE_TASKS foreign key (Task)
      references Tasks (Task),
   constraint FK_PRODUCTS_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd),
   constraint FK_PROD_TYPES_U1 foreign key (Unit1)
      references Types (Type),
   constraint FK_PROD_TYPES_U2 foreign key (Unit2)
      references Types (Type)
)
go

/*==============================================================*/
/* Table: ProductPhoto                                          */
/*==============================================================*/
CREATE TABLE ProductPhoto (
	Code NVARCHAR(50) NOT NULL,
	Firm INT NOT NULL, 
	Type INT NOT NULL, 
    IsDefault BIT NOT NULL DEFAULT 0,
	Title NVARCHAR(100) NULL, 
	Description NVARCHAR(250) NULL,
	Guid NVARCHAR(250) NOT NULL,
	CONSTRAINT FK_PRODUCTPHOTO_REFERENCE_PRODUCTS FOREIGN KEY (Code, Firm, Type) REFERENCES Products (Code, Firm, Type)
)
go

/*==============================================================*/
/* Table: QuestionLink                                          */
/*==============================================================*/
create table QuestionLink (
   QuestionID           int                  not null,
   GroupID              int                  not null,
   constraint AK_KEY_1_QUESTION unique (QuestionID, GroupID),
   constraint FK_QUESTION_REFERENCE_POLLQUES foreign key (QuestionID)
      references PollQuestions (QuestionID),
   constraint FK_QUESTION_REFERENCE_ANWSERGR foreign key (GroupID)
      references AnwserGroup (GroupID)
)
go

/*==============================================================*/
/* Table: RawScanAtl                                            */
/*==============================================================*/
create table RawScanAtl (
   Head                 nvarchar(50) COLLATE NOCASE         null,
   Scan                 nvarchar(50) COLLATE NOCASE         null,
   Price                float                null,
   Action               bit                  null
)
go

/*==============================================================*/
/* Table: SAP_004                                               */
/*==============================================================*/
create table SAP_004 (
   Firm                 int                  not null,
   CondType             nvarchar(5) COLLATE NOCASE          not null,
   Obj_Code             nvarchar(50) COLLATE NOCASE         not null,
   Obj_Type             int                  not null,
   Rate                 float                null,
   RateUnit             nvarchar(5) COLLATE NOCASE          null,
   CondPriceUnit        float                null,
   PriceUnit            nvarchar(5) COLLATE NOCASE          null,
   FromDate             datetime             null,
   ToDate               datetime             not null,
   insertTime           datetime             null,
   CondNr               int                  not null,
   constraint PK_SAP_004 primary key (Firm, CondType, Obj_Code, Obj_Type, ToDate)
)
go

/*==============================================================*/
/* Table: SAP_305                                               */
/*==============================================================*/
create table SAP_305 (
   Firm                 int                  not null,
   CondType             nvarchar(5) COLLATE NOCASE          not null,
   Sub_Code             nvarchar(50) COLLATE NOCASE         not null,
   Sub_Type             int                  not null,
   Obj_Code             nvarchar(50) COLLATE NOCASE         not null,
   Obj_Type             int                  not null,
   Rate                 float                null,
   RateUnit             nvarchar(5) COLLATE NOCASE          null,
   CondPriceUnit        float                null,
   CondUnit             nvarchar(5) COLLATE NOCASE          null,
   FromDate             datetime             null,
   ToDate               datetime             not null,
   insertTime           datetime             null,
   CondNr               int                  null,
   constraint PK_SAP_305 primary key (Firm, CondType, Sub_Code, Sub_Type, Obj_Code, Obj_Type, ToDate)
)
go

/*==============================================================*/
/* Table: SAP_907                                               */
/*==============================================================*/
create table SAP_907 (
   Firm                 int                  not null,
   CondType             nvarchar(5) COLLATE NOCASE          not null,
   Obj_Code             nvarchar(50) COLLATE NOCASE         not null,
   Obj_Type             int                  not null,
   PriceList            int                  not null,
   Rate                 float                null,
   RateUnit             nvarchar(5) COLLATE NOCASE          null,
   CondPriceUnit        float                null,
   CondUnit             nvarchar(5) COLLATE NOCASE          null,
   FromDate             datetime             null,
   ToDate               datetime             not null,
   insertTime           datetime             null,
   CondNr               int                  null,
   constraint PK_SAP_907 primary key (Firm, CondType, Obj_Code, Obj_Type, PriceList, ToDate)
)
go

/*==============================================================*/
/* Table: SAP_908                                               */
/*==============================================================*/
create table SAP_908 (
   Firm                 int                  not null,
   Sub_Code             nvarchar(50) COLLATE NOCASE         not null,
   Sub_Type             int                  not null,
   CondType             nvarchar(5) COLLATE NOCASE          not null,
   Categ                nvarchar(50) COLLATE NOCASE         not null,
   Rate                 float                null,
   RateUnit             nvarchar(5) COLLATE NOCASE          null,
   CondPriceUnit        float                null,
   CondUnit             nvarchar(5) COLLATE NOCASE          null,
   FromDate             datetime             null,
   ToDate               datetime             not null,
   insertTime           datetime             null,
   CondNr               int                  null,
   constraint PK_SAP_908 primary key (Firm, Sub_Code, Sub_Type, CondType, Categ, ToDate)
)
go

/*==============================================================*/
/* Table: SQLInserts                                            */
/*==============================================================*/
create table SQLInserts (
   Idd                  int                  not null,
   TableName            nvarchar(50) COLLATE NOCASE         null,
   ColumnNames          nvarchar(1000) COLLATE NOCASE       null,
   constraint PK_SQLINSERTS primary key (Idd)
)
go

/*==============================================================*/
/* Table: SQLInsertValues                                       */
/*==============================================================*/
create table SQLInsertValues (
   Idd                  int                  not null,
   Val                  nvarchar(1500) COLLATE NOCASE       null,
   KeyColumn            int                  identity,
   constraint PK_SQLINSERTVALUES primary key (KeyColumn),
   constraint FK_SQLINSER_REFERENCE_SQLINSER foreign key (Idd)
      references SQLInserts (Idd)
)
go

/*==============================================================*/
/* Table: SerialAtl                                             */
/*==============================================================*/
create table SerialAtl (
   Code                 nvarchar(18) COLLATE NOCASE         not null,
   Serial               nvarchar(10) COLLATE NOCASE         not null,
   DueDate              datetime             null,
   AddDate              datetime             null,
   InsertDateTime       datetime             null,
   Status               int                  null,
   constraint PK_SERIALATL primary key (Code, Serial)
)
go

/*==============================================================*/
/* Index: Index_4                                               */
/*==============================================================*/
create index Index_4 on SerialAtl (
Code ASC,
DueDate ASC
)
go

/*==============================================================*/
/* Index: Index_3                                               */
/*==============================================================*/
create index Index_3 on SerialAtl (
Code ASC,
AddDate ASC
)
go

/*==============================================================*/
/* Table: SettingsErpVariable                                   */
/*==============================================================*/
create table SettingsErpVariable (
   Firm                 int                  not null,
   ErpVarIdd            nvarchar(50) COLLATE NOCASE         not null,
   IValue               int                  null,
   CValue               nvarchar(50) COLLATE NOCASE         null,
   FValue               float                null,
   constraint PK_SETTINGSERPVARIABLE primary key (Firm, ErpVarIdd),
   constraint FK_SET_REF_FI foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Table: UserProductCustomer                                   */
/*==============================================================*/
create table UserProductCustomer (
   Firm                 int                  not null,
   Type                 int                  not null,
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   UserIdx              int                  not null,
   Category             nvarchar(50) COLLATE NOCASE         not null,
   RouteIdx             int                  null,
   constraint PK_UserProductCustomer primary key (Firm, Type, Code, UserIdx, Category),
   constraint FK_UPC_REFERENCE_FIRM foreign key (Firm) references Firm (Firm),
   constraint FK_UPC_REFERENCE_TYPES foreign key (Type) references Types (Type),
   constraint FK_UPC_REFERENCE_CUSTOMER foreign key (Code, Firm, Type) references Customer (Code, Firm, Type),
   constraint FK_UPC_REFERENCE_USERS foreign key (Useridx) references Users (UserIdx)
)
go

/*==============================================================*/
/* Table: UsersGroups                                           */
/*==============================================================*/
create table UsersGroups (
   UserGroupIdx         int                  not null,
   UserIdx              int                  null,
   status               int                  null,
   UserGroupName4       nvarchar(50) COLLATE NOCASE         null,
   constraint PK_USERSGROUPS primary key (UserGroupIdx),
   constraint FK_USER_REF_USE foreign key (UserIdx)
      references Users (UserIdx)
)
go

/*==============================================================*/
/* Table: TerminalsGroups                                       */
/*==============================================================*/
create table TerminalsGroups (
   TerminalGroupIdx     int                  not null,
   TerminalIdx          int                  null,
   status               int                  null,
   TerminalGroupName    nvarchar(50) COLLATE NOCASE         null,
   constraint PK_TERMINALSGROUPS primary key (TerminalGroupIdx),
   constraint FK_TER_REF_TER foreign key (TerminalIdx)
      references Terminals (TerminalIdx)
)
go

/*==============================================================*/
/* Table: SubMenu                                               */
/*==============================================================*/
create table SubMenu (
   SubIdx               int                  not null,
   MenuGroupIdx         int                  null,
   MenuIdx              int                  null,
   Type                 int                  not null,
   MenuOrder            int                  null,
   constraint PK_SUBMENU primary key (SubIdx),
   constraint FK_SUBMENU_MENU_IMA__MENU foreign key (MenuIdx, MenuGroupIdx)
      references Menu (MenuIdx, MenuGroupIdx),
   constraint FK_SUBMENU_REFERENCE_TYPES foreign key (Type)
      references Types (Type)
)
go

/*==============================================================*/
/* Table: Settings                                              */
/*==============================================================*/
create table Settings (
   SettIdd              int                  not null,
   UserIdx              int                  null,
   TerminalIdx          int                  null,
   Sett_Type            int                  null,
   Firm                 int                  null,
   Type                 int                  null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   ErpDocIdd            nvarchar(50) COLLATE NOCASE         null,
   ErpVarIdd            nvarchar(50) COLLATE NOCASE         null,
   UserGroupIdx         int                  null,
   TerminalGroupIdx     int                  null,
   MenuGroupIdx         int                  null,
   SubIdx               int                  null,
   DT2_Idd              int                  null,
   Doc_Firm             int                  null,
   Doc_Type             int                  null,
   BDataIdx             int                  null,
   TL_Idd               int                  null,
   PrintTemplateIdd     int                  null,
   IValue               int                  null,
   CValue               nvarchar(50) COLLATE NOCASE         null,
   FValue               float                null,
   constraint PK_SETTINGS primary key (SettIdd),
   constraint FK_SET_REF_SET foreign key (Firm, ErpDocIdd)
      references SettingsErpDoc (Firm, ErpDocIdd),
   constraint FK_SET_REF_SET_1 foreign key (Firm, ErpVarIdd)
      references SettingsErpVariable (Firm, ErpVarIdd),
   constraint FK_SET_REF_USGRO foreign key (UserGroupIdx)
      references UsersGroups (UserGroupIdx),
   constraint FK_SET_REF_TER_7 foreign key (TerminalGroupIdx)
      references TerminalsGroups (TerminalGroupIdx),
   constraint FK_SET_REF_MEGR foreign key (MenuGroupIdx)
      references MenuGroup (MenuGroupIdx),
   constraint FK_SET_REF_SUB1 foreign key (SubIdx)
      references SubMenu (SubIdx),
   constraint FK_SET_REF_DSET_10 foreign key (DT2_Idd, Doc_Firm, Doc_Type)
      references DocSettings (DT2_Idd, Firm, Type),
   constraint FK_SETTINGS_REFERENCE_BDATA foreign key (BDataIdx)
      references BData (BDataIdx),
   constraint FK_SETTINGS_REFERENCE_TIMELINE foreign key (TL_Idd)
      references TimeLine (TL_Idd),
   constraint FK_SETTINGS_REFERENCE_PRINTTEM foreign key (PrintTemplateIdd, Firm)
      references PrintTemplates (TemplateIdd, Firm),
   constraint FK_SET_REF_USERS foreign key (UserIdx)
      references Users (UserIdx),
   constraint FK_SET_REF_TERM_8 foreign key (TerminalIdx)
      references Terminals (TerminalIdx),
   constraint FK_SET_REF_FIRM_2 foreign key (Firm)
      references Firm (Firm),
   constraint FK_SET_REF_TYPES_1 foreign key (Type)
      references Types (Type),
   constraint FK_SET_REF_TY_3 foreign key (Sett_Type)
      references Types (Type)
)
go

/*==============================================================*/
/* Table: Slink                                                 */
/*==============================================================*/
create table Slink (
   SLinkIdd             int                  not null,
   Source               int                  null,
   Destination          int                  null,
   Task                 int                  null,
   InsertDateTime       datetime             null,
   Chage                datetime             null,
   Status               int                  null,
   constraint PK_SLINK primary key (SLinkIdd),
   constraint SLINK_STOCK_SOURCE foreign key (Source)
      references Stock (SIdd),
   constraint SLINK_ST_DEST foreign key (Destination)
      references Stock (SIdd),
   constraint FK_SLINK_REFERENCE_TASKS foreign key (Task)
      references Tasks (Task)
)
go

/*==============================================================*/
/* Index: I00044                                                */
/*==============================================================*/
create index I00044 on Slink (
Source ASC
)
go

/*==============================================================*/
/* Index: I00045                                                */
/*==============================================================*/
create index I00045 on Slink (
Destination ASC
)
go

/*==============================================================*/
/* Index: I00046                                                */
/*==============================================================*/
create index I00046 on Slink (
Task ASC
)
go

/*==============================================================*/
/* Index: I00051                                                */
/*==============================================================*/
create index I00051 on Stock (
LocCode ASC,
LocFirm ASC,
LocType ASC
)
go

/*==============================================================*/
/* Index: I00052                                                */
/*==============================================================*/
create index I00052 on Stock (
OwnCode ASC,
OwnFirm ASC,
OwnType ASC
)
go

/*==============================================================*/
/* Index: I00053                                                */
/*==============================================================*/
create index I00053 on Stock (
ObjectCode ASC,
ObjectFirm ASC,
ObjectType ASC
)
go

/*==============================================================*/
/* Index: I00054                                                */
/*==============================================================*/
create index I00054 on Stock (
Status ASC
)
go

/*==============================================================*/
/* Index: I00201                                                */
/*==============================================================*/
create index I00201 on Stock (
MicroLocType ASC,
MicroLocCode ASC,
MicroLocFirm ASC
)
go

/*==============================================================*/
/* Index: StockDeleteSupport                                    */
/*==============================================================*/
create index StockDeleteSupport on Stock (
SIdd ASC,
LocCode ASC,
OwnCode ASC,
OwnFirm ASC,
OwnType ASC,
ObjectCode ASC,
ObjectFirm ASC,
ObjectType ASC
)
go

/*==============================================================*/
/* Index: """NonClusteredIndex-20130807-140351"""               */
/*==============================================================*/
create index """NonClusteredIndex-20130807-140351""" on Stock (
MicroLocCode ASC,
MicroLocFirm ASC,
Sub_Type ASC
)
go

/*==============================================================*/
/* Table: StockAddInfo                                          */
/*==============================================================*/
create table StockAddInfo (
   SIdd                 int                  not null,
   Type                 int                  null,
   Firm                 int                  null,
   Object_Code          nvarchar(50) COLLATE NOCASE         null,
   Subject_Code         nvarchar(50) COLLATE NOCASE         null,
   AddInt1              int                  null,
   AddInt2              int                  null,
   AddChar1             nvarchar(50) COLLATE NOCASE         null,
   AddChar2             nvarchar(50) COLLATE NOCASE         null,
   StockAddInfoIdd      int                  not null,
   Change               datetime             null,
   Status               int                  not null,
   constraint PK_STOCKADDINFO primary key (StockAddInfoIdd),
   constraint FK_STOCKADD_REFERENCE_STOCK foreign key (SIdd)
      references Stock (SIdd),
   constraint FK_STOCKADD_REFERENCE_TYPES foreign key (Type)
      references Types (Type),
   constraint FK_STOCKADD_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Index: I00103                                                */
/*==============================================================*/
create index I00103 on StockAddInfo (
Type ASC,
Firm ASC,
Object_Code ASC
)
go

/*==============================================================*/
/* Index: I00104                                                */
/*==============================================================*/
create index I00104 on StockAddInfo (
Type ASC,
Firm ASC,
Subject_Code ASC
)
go

/*==============================================================*/
/* Index: I00105                                                */
/*==============================================================*/
create index I00105 on StockAddInfo (
SIdd ASC
)
go

/*==============================================================*/
/* Table: Stops                                                 */
/*==============================================================*/
create table Stops (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   Task                 int                  null,
   TIdd                 int                  null,
   Change               datetime             null,
   Status               int                  not null,
   Name_full            nvarchar(250) COLLATE NOCASE        null,
   constraint PK_STOPS primary key (Code, Firm, Type),
   constraint FK_STOPS_REFERENCE_SUBJECT foreign key (Code, Firm, Type)
      references Subject (Code, Firm, Type),
   constraint FK_STOPS_REFERENCE_TASKS foreign key (Task)
      references Tasks (Task),
   constraint FK_STOPS_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Table: SubParts                                              */
/*==============================================================*/
create table SubParts (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   Task                 int                  null,
   TIdd                 int                  null,
   Change               datetime             null,
   Status               int                  not null,
   Name_full            nvarchar(250) COLLATE NOCASE        null,
   constraint PK_SUBPARTS primary key (Code, Firm, Type),
   constraint FK_SUBPARTS_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type),
   constraint FK_SUBPARTS_REFERENCE_TASKS foreign key (Task)
      references Tasks (Task),
   constraint FK_SUBPARTS_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Index: I00056                                                */
/*==============================================================*/
create index I00056 on Subject (
Name ASC
)
go

/*==============================================================*/
/* Index: I00057                                                */
/*==============================================================*/
create index I00057 on Subject (
Status ASC
)
go

/*==============================================================*/
/* Index: I00058                                                */
/*==============================================================*/
create index I00058 on Subject (
Firm ASC,
Type ASC
)
go

/*==============================================================*/
/* Table: SubjectLists                                          */
/*==============================================================*/
create table SubjectLists (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   Task                 int                  null,
   TIdd                 int                  null,
   TL_Idd               int                  null,
   Name_full            nvarchar(250) COLLATE NOCASE        null,
   Change               datetime             null,
   Status               int                  not null,
   constraint PK_SUBJECTLISTS primary key (Code, Firm, Type),
   constraint FK_SUBJECTL_REFERENCE_TIMELINE foreign key (TL_Idd)
      references TimeLine (TL_Idd),
   constraint FK_SUBJECTL_REFERENCE_SUBJECT foreign key (Code, Firm, Type)
      references Subject (Code, Firm, Type),
   constraint FK_SUBJECTL_REFERENCE_TASKS foreign key (Task)
      references Tasks (Task),
   constraint FK_SUBJECTL_REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Table: Subject_Interconnections                              */
/*==============================================================*/
create table Subject_Interconnections (
   DateTime1            datetime             null,
   DateTime2            datetime             null,
   SIC_Idd              int                  not null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   Firm                 int                  null,
   Type                 int                  null,
   Prev_Code            nvarchar(50) COLLATE NOCASE         null,
   Prev_Firm            int                  null,
   Prev_Type            int                  null,
   Curr_Code            nvarchar(50) COLLATE NOCASE         null,
   Curr_Firm            int                  null,
   Curr_Type            int                  null,
   Priority             int                  null,
   Status               int                  not null,
   constraint PK_SUBJECT_INTERCONNECTIONS primary key (SIC_Idd),
   constraint SUBJECT_SUBJECTa foreign key (Code, Firm, Type)
      references Subject (Code, Firm, Type),
   constraint SUB_SUB_PREV foreign key (Prev_Code, Prev_Firm, Prev_Type)
      references Subject (Code, Firm, Type),
   constraint SUB_SUB_NEXT foreign key (Curr_Code, Curr_Firm, Curr_Type)
      references Subject (Code, Firm, Type),
   constraint FK_SUBJECT__REFERENCE_TYPES foreign key (Priority)
      references Types (Type)
)
go

/*==============================================================*/
/* Index: I00059                                                */
/*==============================================================*/
create index I00059 on Subject_Interconnections (
Code ASC,
Firm ASC,
Type ASC
)
go

/*==============================================================*/
/* Index: I00060                                                */
/*==============================================================*/
create index I00060 on Subject_Interconnections (
Prev_Code ASC,
Prev_Firm ASC,
Prev_Type ASC
)
go

/*==============================================================*/
/* Index: I00061                                                */
/*==============================================================*/
create index I00061 on Subject_Interconnections (
Curr_Code ASC,
Curr_Firm ASC,
Curr_Type ASC
)
go

/*==============================================================*/
/* Table: Subject_Object_Interconnections                       */
/*==============================================================*/
create table Subject_Object_Interconnections (
   SIIC_Idd             int                  not null,
   Obj_Code             nvarchar(50) COLLATE NOCASE         null,
   Obj_Firm             int                  null,
   Obj_Type             int                  null,
   Sub_Code             nvarchar(50) COLLATE NOCASE         null,
   Sub_Firm             int                  null,
   Sub_Type             int                  null,
   TIdd                 int                  null,
   Own_Code             nvarchar(50) COLLATE NOCASE         null,
   Own_Firm             int                  null,
   Own_Type             int                  null,
   Add_Code             nvarchar(50) COLLATE NOCASE         null,
   Add_Firm             int                  null,
   Add_Type             int                  null,
   changed              datetime             null,
   Status               int                  null,
   constraint PK_SUBJECT_OBJECT_INTERCONNECT primary key (SIIC_Idd),
   constraint SUB_2_OB foreign key (Obj_Code, Obj_Firm, Obj_Type)
      references Object (Code, Firm, Type),
   constraint SUB_1_SUB foreign key (Sub_Code, Sub_Firm, Sub_Type)
      references Subject (Code, Firm, Type),
   constraint FK_SUBJECT__REFERENCE_TXT foreign key (TIdd)
      references Txt (TIdd),
   constraint FK_SUB_SUBa foreign key (Own_Code, Own_Firm, Own_Type)
      references Subject (Code, Firm, Type)
)
go

/*==============================================================*/
/* Index: I00064                                                */
/*==============================================================*/
create index I00064 on Subject_Object_Interconnections (
Obj_Code ASC,
Obj_Firm ASC,
Obj_Type ASC
)
go

/*==============================================================*/
/* Index: I00065                                                */
/*==============================================================*/
create index I00065 on Subject_Object_Interconnections (
Sub_Code ASC,
Sub_Firm ASC,
Sub_Type ASC
)
go

/*==============================================================*/
/* Table: Txt_Add                                               */
/*==============================================================*/
create table Txt_Add (
   TIdd                 int                  not null,
   Idd                  int                  not null,
   Txt                  nvarchar(500) COLLATE NOCASE        null,
   constraint PK_TXT_ADD primary key (TIdd, Idd),
   constraint FK_TXT_ADD_REF_TXT foreign key (TIdd)
      references Txt (TIdd)
)
go

/*==============================================================*/
/* Index: I00078                                                */
/*==============================================================*/
create index I00078 on Users (
Code ASC
)
go


/*==============================================================*/
/* Table: CustomerContacts                                      */
/*==============================================================*/
CREATE TABLE CustomerContacts (
	Code NVARCHAR(50) COLLATE NOCASE NOT NULL, 
    Firm INT NOT NULL, 
    Type INT NOT NULL, 
	ContactId INT NOT NULL, 
	FirstName NVARCHAR(50) COLLATE NOCASE NULL, 
	LastName NVARCHAR(50) COLLATE NOCASE NULL, 
    Phone NVARCHAR(50) COLLATE NOCASE NULL, 
    Fax NVARCHAR(50) COLLATE NOCASE NULL, 
    Email NVARCHAR(50) COLLATE NOCASE NULL, 
    Title NVARCHAR(50) COLLATE NOCASE NULL, 
    ContactFunction NVARCHAR(50) COLLATE NOCASE NULL, 
    Note NVARCHAR(50) COLLATE NOCASE NULL, 
	Department NVARCHAR(250) COLLATE NOCASE NULL, 
	ErpId NVARCHAR(50) COLLATE NOCASE NULL, 
    IsProcessed INT NOT NULL DEFAULT 0,
	IsNew INT NULL, 
	IsDeleted INT NULL, 
    InputType INT NOT NULL DEFAULT 0,
	ChangeDt DATETIME NOT NULL DEFAULT (datetime('now','localtime')), 
    PRIMARY KEY (Code, Firm, Type, ContactId), 
    CONSTRAINT FK_CUSTOMERCONTACTS_REFERENCE_SUBJECT FOREIGN KEY (Code, Firm, Type) REFERENCES Subject (Code, Firm, Type)
)
go

/*==============================================================*/
/* Table: CustomerContactInfo                                   */
/*==============================================================*/
CREATE TABLE CustomerContactInfo (
	Code NVARCHAR(50) COLLATE NOCASE NOT NULL, 
	Firm INT NOT NULL, 
	Type INT NOT NULL, 
	InfoId INT NOT NULL,
	ContactId INT NOT NULL, 
	InfoType INT NOT NULL,
	InfoValue NVARCHAR(50) COLLATE NOCASE NOT NULL,
	IsNew INT NULL, 
	IsDeleted INT NULL, 
	IsProcessed INT NOT NULL DEFAULT 0, 
	InputType INT NOT NULL DEFAULT 0, 
	ChangeDt DATETIME NOT NULL DEFAULT (datetime('now','localtime')), 
	PRIMARY KEY ([Code], [Firm], [Type], [ContactId], [InfoId]),
	CONSTRAINT FK_CUSTOMERCONTACTINFO_REFERENCE_CUSTOMERCONTACTS FOREIGN KEY (Code, Firm, Type, ContactId) REFERENCES CustomerContacts (Code, Firm, Type, ContactId)
)
go

/*==============================================================*/
/* Table: Variable                                              */
/*==============================================================*/
create table Variable (
   Firm                 int                  not null,
   Type                 int                  not null,
   Name                 nvarchar(350) COLLATE NOCASE        not null,
   IValue               int                  null,
   CValue               nvarchar(350) COLLATE NOCASE        null,
   MRead1               bigint               null,
   MRead2               bigint               null,
   MWrite1              bigint               null,
   MWrite2              bigint               null,
   VarDateTime			DateTime			 null,
   Status               int                  null,
   constraint PK_VARIABLE primary key (Firm, Type, Name),
   constraint FK_VARIABLE_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Table: HeadFiscalSi                                          */
/*==============================================================*/
create table HeadFiscalSi (
	Head nvarchar(50) COLLATE NOCASE not null,
	Firm int not null,
	DocType int not null,
	IssueDT DateTime2 not null,
	BusinessPremise nvarchar(20) COLLATE NOCASE not null,
	ElectronicDevice nvarchar(20) COLLATE NOCASE not null,
	InvoiceNumber nvarchar(20) COLLATE NOCASE not null,
	Zoi nvarchar(32) COLLATE NOCASE not null,
	Eor nvarchar(36) COLLATE NOCASE null,
	SubsequentSubmit bit null,
	InvoiceJson nvarchar(2000) COLLATE NOCASE null,
	constraint PK_HEADFISCALSI primary key (Head, Firm, DocType),
	constraint FK_HeadFiscalSi_Head foreign key (Head, Firm, DocType) references Head (Head, Firm, DocType)
)
go

CREATE TABLE Head_Object
(
	Id INT IDENTITY(1,1) PRIMARY KEY,
    Head                     NVARCHAR (50) COLLATE NOCASE  NOT NULL,
	Head_Firm                     INT            NOT NULL,
    Head_DocType                  INT            NOT NULL,
	Object_Code            NVARCHAR (50) COLLATE NOCASE  NOT NULL,
    Object_Firm            INT            NOT NULL,
    Object_Type            INT            NOT NULL, 
    CONSTRAINT FK_Head_Object_To_Head FOREIGN KEY (Head, Head_Firm, Head_DocType ) REFERENCES Head(Head, Firm, DocType), 
    CONSTRAINT FK_Head_Object_To_Object FOREIGN KEY (Object_Code, Object_Firm, Object_Type ) REFERENCES Object(Code, Firm, Type) 
)
go

/*==============================================================*/
/* Table: atlantic_object_add                                   */
/*==============================================================*/
create table atlantic_object_add (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   prim                 nvarchar(50) COLLATE NOCASE         null,
   sek                  nvarchar(50) COLLATE NOCASE         null,
   constraint PK_ATLANTIC_OBJECT_ADD primary key (Code, Firm, Type),
   constraint FK_ATLANTIC_REFERENCE_OBJECT foreign key (Code, Firm, Type)
      references Object (Code, Firm, Type)
)
go

/*==============================================================*/
/* Table: atlantic_reportHelper1                                */
/*==============================================================*/
create table atlantic_reportHelper1 (
   ObjectCode           nvarchar(50) COLLATE NOCASE         not null,
   ObjectFirm           int                  not null,
   ObjectType           int                  not null,
   SubjectCode          nvarchar(50) COLLATE NOCASE         not null,
   SubjectFirm          int                  not null,
   SubjectType          int                  not null,
   ObjectName           nvarchar(250) COLLATE NOCASE        null,
   SubjectName          nvarchar(250) COLLATE NOCASE        null,
   Object_prim          nvarchar(50) COLLATE NOCASE         null,
   Object_sec           nvarchar(50) COLLATE NOCASE         null,
   Sub_tip_pr_m         nvarchar(50) COLLATE NOCASE         null,
   Sub_trgovina         nvarchar(50) COLLATE NOCASE         null,
   Sub_regija           nvarchar(50) COLLATE NOCASE         null,
   Sub_teren            nvarchar(50) COLLATE NOCASE         null,
   listing              int                  null,
   prisotnost           int                  null,
   obvezni              int                  null,
   oos                  int                  null,
   ObjectCode1          nvarchar(50) COLLATE NOCASE         null,
   ObjectName1          nvarchar(250) COLLATE NOCASE        null,
   SubjectCode1         nvarchar(50) COLLATE NOCASE         null,
   SubjectName1         nvarchar(250) COLLATE NOCASE        null,
   ObjectBarcode        nvarchar(50) COLLATE NOCASE         null,
   constraint PK_ATLANTIC_REPORTHELPER1 primary key (ObjectCode, ObjectFirm, ObjectType, SubjectCode, SubjectFirm, SubjectType),
   constraint FK_ATLANTIC_FK_ATLANT_OBJECT foreign key (ObjectCode, ObjectFirm, ObjectType)
      references Object (Code, Firm, Type),
   constraint FK_ATLANTIC_FK_ATLANT_SUBJECT foreign key (SubjectCode, SubjectFirm, SubjectType)
      references Subject (Code, Firm, Type)
)
go

/*==============================================================*/
/* Index: """NonClusteredIndex-20130807-140248"""               */
/*==============================================================*/
create index """NonClusteredIndex-20130807-140248""" on atlantic_reportHelper1 (
SubjectCode ASC,
SubjectFirm ASC,
SubjectType ASC
)
go

/*==============================================================*/
/* Table: atlantic_subject_add                                  */
/*==============================================================*/
create table atlantic_subject_add (
   Code                 nvarchar(50) COLLATE NOCASE         not null,
   Firm                 int                  not null,
   Type                 int                  not null,
   tip_pr_m             nvarchar(50) COLLATE NOCASE         null,
   regija               nvarchar(50) COLLATE NOCASE         null,
   trgovina             nvarchar(50) COLLATE NOCASE         null,
   teren                nvarchar(50) COLLATE NOCASE         null,
   constraint PK_ATLANTIC_SUBJECT_ADD primary key (Code, Firm, Type),
   constraint FK_ATLANTIC_REFERENCE_SUBJECT foreign key (Code, Firm, Type)
      references Subject (Code, Firm, Type)
)
go

/*==============================================================*/
/* Table: atlntic_users                                         */
/*==============================================================*/
create table atlntic_users (
   UserIdx              int                  not null,
   Firm                 int                  not null,
   Teren                nvarchar(100) COLLATE NOCASE        null,
   constraint PK_ATLNTIC_USERS primary key (UserIdx, Firm),
   constraint FK_ATLNTIC__REFERENCE_USERS foreign key (UserIdx)
      references Users (UserIdx),
   constraint FK_ATLNTIC__REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Table: atlntic_pivot                                         */
/*==============================================================*/
create table atlntic_pivot (
   Sub_Code             nvarchar(50) COLLATE NOCASE         not null,
   Sub_Firm             int                  not null,
   Sub_Type             int                  not null,
   Obj_Code             nvarchar(50) COLLATE NOCASE         not null,
   Obj_Firm             int                  not null,
   Obj_Type             int                  not null,
   UserIdx              int                  not null,
   Firm                 int                  not null,
   listing              bit                  null,
   prisotnost           bit                  null,
   obvezni              bit                  null,
   oos                  bit                  null,
   constraint PK_ATLNTIC_PIVOT primary key (Sub_Code, Sub_Firm, Sub_Type, Obj_Code, Obj_Firm, Obj_Type, UserIdx, Firm),
   constraint FK_ATLNTIC__REFERENCE_SUBJECT foreign key (Sub_Code, Sub_Firm, Sub_Type)
      references Subject (Code, Firm, Type),
   constraint FK_ATLNTIC__REFERENCE_OBJECT foreign key (Obj_Code, Obj_Firm, Obj_Type)
      references Object (Code, Firm, Type),
   constraint FK_ATLNTIC__REFERENCE_ATLNTIC_ foreign key (UserIdx, Firm)
      references atlntic_users (UserIdx, Firm)
)
go

/*==============================================================*/
/* Table: transfer_in                                           */
/*==============================================================*/
create table transfer_in (
   priority             smallint             not null,
   xmldata              image                null,
   constraint PK_TRANSFER_IN primary key (priority)
)
go

/*==============================================================*/
/* Table: transfer_in_sap                                       */
/*==============================================================*/
create table transfer_in_sap (
   priority             smallint             not null,
   xmldata              varbinary(MAX)       null,
   constraint PK_TRANSFER_IN_SAP primary key (priority)
)
go

/*==============================================================*/
/* Table: transfer_log                                          */
/*==============================================================*/
create table transfer_log (
   eventTime            datetime             not null,
   eventDirection       nvarchar(3) COLLATE NOCASE          null,
   txt                  nvarchar(300) COLLATE NOCASE        not null,
   constraint PK_TRANSFER_LOG primary key (eventTime, txt)
)
go

/*==============================================================*/
/* Table: transfer_out                                          */
/*==============================================================*/
create table transfer_out (
   priority             smallint             not null,
   Firm                 int                  null,
   xmldata              image                null,
   constraint PK_TRANSFER_OUT primary key (priority),
   constraint FK_TR_REF_FI foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Table: HeadDeliveryNumber                                    */
/*==============================================================*/
create table HeadDeliveryNumber (
	Firm                     INT            NOT NULL,
	Head                     NVARCHAR (50)  NOT NULL,
	DocType                  INT            NOT NULL,
	DeliveryNoteNumber       NVARCHAR (50)  NOT NULL,
	DtCreated       		 DateTime  		NOT NULL,
    constraint PK_HEADDELIVERYNUMBER primary key (Head, Firm, DocType, DeliveryNoteNumber),
	constraint FK_HEADDELIVERYNUMBER_REFERENCE_FIRM foreign key (Firm) 
		references Firm (Firm),
	constraint FK_HEADDELIVERYNUMBER_REFERENCE_HEAD foreign key (Head, Firm, DocType) 
		references Head (Head, Firm, DocType)
)
go

/*==============================================================*/
/* Table: ObjectPhoto                                    */
/*==============================================================*/
create table ObjectPhoto(
	Code            NVARCHAR (50)  NOT NULL,
    Firm            INT           NOT NULL,
    Type            INT           NOT NULL,
	BinaryData      image       NOT NULL,
	DTCreation      DATETIME      NOT NULL,
    UserIdx		  INT           NOT NULL,
	TerminalIdx     INT           NOT NULL,
	IsSent			INT			  NULL,
	CONSTRAINT FK_OBJECTFOTO_REFERENCE_OBJECT FOREIGN KEY (Code, Firm, Type) REFERENCES Object (Code, Firm, Type),
	CONSTRAINT FK_OBJECTFOTO_REFERENCE_USERS FOREIGN KEY (UserIdx) REFERENCES Users (UserIdx),
	CONSTRAINT FK_OBJECTFOTO_REFERENCE_TERMINALS FOREIGN KEY (TerminalIdx) REFERENCES Terminals (TerminalIdx)
)
go

/*==============================================================*/
/* Table: InventorySignature                                    */
/*==============================================================*/
create table InventorySignature(
    Firm INT NOT NULL, 
    UserIdx INT NOT NULL, 
    Name NVARCHAR(50) NULL, 
    BinaryData image NOT NULL, 
    DTCreation DATETIME NULL,
	Note NVARCHAR(500) NULL,
	IsSent INT NULL
)
go

/*==============================================================*/
/* Table: InventorySignatureStockLink                           */
/*==============================================================*/
create table InventorySignatureStockLink(
	IdSignature INT NOT NULL, 
    Sidd INT NOT NULL,
	IsSent INT NULL,
	CONSTRAINT PK_INVENTORYSIGNATURESTOCKLINK PRIMARY KEY (IdSignature, Sidd),
    CONSTRAINT FK_INVENTORYSIGNATURESTOCKLINK_REFERENCE_INVENTORYSIGNATURE FOREIGN KEY (IdSignature) REFERENCES InventorySignature (Id),
    CONSTRAINT FK_INVENTORYSIGNATURESTOCKLINK_REFERENCE_STOCK FOREIGN KEY (Sidd) REFERENCES Stock (Sidd)
)
go

/*==============================================================*/
/* Table: InventorySignatureParameters                           */
/*==============================================================*/
create table InventorySignatureParameters(
	IdSignature INT NOT NULL, 
	Firm INT NOT NULL, 
    ObjectCode NVARCHAR(50) NULL, 
    LocationCode NVARCHAR(50) NULL, 
    SubjectCode NVARCHAR(50) NULL,
	InventoryStatus INT NULL, 
	IsSent INT NULL,
	CONSTRAINT [PK_INVENTORYSIGNATUREPARAMETERS] PRIMARY KEY (IdSignature, Firm),
    CONSTRAINT [FK_INVENTORYSIGNATUREPARAMETERS_REFERENCE_SIGNATURE] FOREIGN KEY (IdSignature, Firm) REFERENCES InventorySignature (Id, Firm)
)
go


/*==============================================================*/
/* Table: _NJIVA_JMD_SetItem                           */
/*==============================================================*/
create table _NJIVA_JMD_SetItem (
	Code nvarchar(50) COLLATE NOCASE        NOT NULL,
	Firm INT NULL,
	Type INT NULL,
	Cena nvarchar(50) COLLATE NOCASE         NULL,
	zaloga nvarchar(50) COLLATE NOCASE         NULL,
	narkup nvarchar(50) COLLATE NOCASE         NULL,
	razpol nvarchar(50) COLLATE NOCASE         NULL,
	rezerv nvarchar(50) COLLATE NOCASE         NULL,
	zaizd nvarchar(50) COLLATE NOCASE         NULL,
	nardob nvarchar(50) COLLATE NOCASE         NULL,
	naklad nvarchar(50) COLLATE NOCASE         NULL,
	razklad nvarchar(50) COLLATE NOCASE         NULL,
	proizv nvarchar(50) COLLATE NOCASE         NULL,
	PrviDN nvarchar(50) COLLATE NOCASE         NULL,
	DatDN nvarchar(50) COLLATE NOCASE         NULL,
	constraint PK__NJIVA_JMD_SetItem_Data primary key (Code, Firm, Type)
)
go

/*==============================================================*/
/* Table: SaopCenovnaPodrocja									*/
/*==============================================================*/
CREATE TABLE SaopCenovnaPodrocja (
	Firm INT NOT NULL, 
	PriceListId NVARCHAR(20) COLLATE NOCASE NOT NULL, 
	PriceListDescription NVARCHAR(30) COLLATE NOCASE NULL, 
	InsertDate DATETIME NOT NULL, 
	ChangeDate DATETIME NOT NULL, 
	CurrencyId NVARCHAR(20) COLLATE NOCASE NULL, 
	ExcludeDiscount BIT NULL, 
	LimitedView BIT NULL, 
	VatIncluded BIT NOT NULL, 
	PriceListValidityFrom DATETIME NULL, 
	PriceListValidityTo DATETIME NULL, 
	Active BIT NOT NULL, 
	PriceListCustomerId NVARCHAR(20) COLLATE NOCASE NULL, 
	InsUpdDate DATETIME NULL, 
	CONSTRAINT PK_SaopCenovnaPodrocja PRIMARY KEY (Firm, PriceListId)
)
go

/*==============================================================*/
/* Table: SaopArtikliCeneCP										*/
/*==============================================================*/
CREATE TABLE SaopArtikliCeneCP (
	Firm INT NOT NULL, 
	PriceListId NVARCHAR(20) COLLATE NOCASE NOT NULL, 
	ItemCode NVARCHAR(20) COLLATE NOCASE NOT NULL, 
	InsertDate DATETIME NOT NULL, 
	ChangeDate DATETIME NOT NULL, 
	VATRate FLOAT NULL, 
	Price FLOAT NULL, 
	UnitOfMeasurementForPrice FLOAT NULL, 
	VATIncluded BIT NOT NULL, 
	Active BIT NULL, 
	InsUpdDate DATETIME NULL, 
	Quantity1 FLOAT NULL, 
    PriceForQuantity1 FLOAT NULL, 
    Quantity2 FLOAT NULL, 
    PriceForQuantity2 FLOAT NULL, 
    Quantity3 FLOAT NULL, 
    PriceForQuantity3 FLOAT NULL, 
    Quantity4 FLOAT NULL, 
    PriceForQuantity4 FLOAT NULL, 
    Quantity5 FLOAT NULL, 
    PriceForQuantity5 FLOAT NULL, 
	CONSTRAINT PK_SaopArtikliCeneCP PRIMARY KEY (Firm, PriceListId, ItemCode)
)
go

/*==============================================================*/
/* Table: SaopArtikliPopustiStranke								*/
/*==============================================================*/
CREATE TABLE SaopArtikliPopustiStranke (
	Firm INT NOT NULL, 
	DiscountCustomerGroupID NVARCHAR(20) COLLATE NOCASE NOT NULL, 
	DiscountItemGroupID NVARCHAR(20) COLLATE NOCASE NOT NULL, 
	InsertDate DATETIME NOT NULL, 
	ChangeDate DATETIME NOT NULL,
	ThresholdQuantity FLOAT NULL, 
	DiscountPercent FLOAT NULL, 
	ValidFrom DATETIME NULL, 
	ValidTo DATETIME NULL,
	InsUpdDate DATETIME NULL, 
	CONSTRAINT PK_SaopArtikliPopustiStranke PRIMARY KEY (Firm, DiscountCustomerGroupID, DiscountItemGroupID)
)
go

/*==============================================================*/
/* Table: CostUnit												*/
/*==============================================================*/
CREATE TABLE CostUnit (
	Code NVARCHAR(50) COLLATE NOCASE NOT NULL, 
	Firm INT NOT NULL, 
	Name NVARCHAR(50) COLLATE NOCASE NOT NULL, 
	DateStart DATETIME NULL, 
	DateEnd DATETIME NULL,
	ErpCode NVARCHAR(50) COLLATE NOCASE NULL, 
	IsNew BIT NOT NULL DEFAULT 0, 
	Status INT NOT NULL, 
	CONSTRAINT PK_CostUnit PRIMARY KEY ([Code], [Firm])
)
go


/*==============================================================*/
/* Table: RfidHistory                                               */
/*==============================================================*/
create table RfidHistory (
   BIdd                 int                  not null,
   Firm                 int                  null,
   Code                 nvarchar(50) COLLATE NOCASE         null,
   Type                 int                  not null,
   BarCode              nvarchar(100) COLLATE NOCASE         not null,
   Status               int                  not null,
   SIdd                 int                  null,
   BcType               int                  null,
   CustomerCode         nvarchar(50) COLLATE NOCASE         null,
   Name_full            nvarchar(250) COLLATE NOCASE        null,
   constraint PK_BARCODE primary key (BIdd),
   constraint FK_BARCODE_REFERENCE_STOCK foreign key (SIdd)
      references Stock (SIdd),
   constraint FK_BARCODE_REFERENCE_TYPES2 foreign key (BcType)
      references Types (Type),
   constraint FK_BARCODE_REFERENCE_TYPES foreign key (Type)
      references Types (Type),
   constraint FK_BARCODE_REFERENCE_FIRM foreign key (Firm)
      references Firm (Firm)
)
go

/*==============================================================*/
/* Table: WrongLocationRfid                                               */
/*==============================================================*/
create table WrongLocationRfid (
    Id                   integer    primary key AUTOINCREMENT,
    Firm                 int                  null,
    Code                 nvarchar(50) COLLATE NOCASE         null,
    Type                 int                  not null,
    LocCode              nvarchar(50) COLLATE NOCASE         null,
    LocFirm              int                  null,
    LocType              int                  null,
    Rssi                 int                  null,
    UserIdx              int                  not null,
    Timestamp            datetime             not null,


    /*constraint PK_WRONGLOCATION primary key (Id),*/
    constraint FK_WRONGLOCATION_Code foreign key (Code)
        references RfidHistory (Code),
    constraint FK_WRONGLOCATION_Type foreign key (Type)
        references Types (Type),
    constraint FK_WRONGLOCATION_Firm foreign key (Firm)
        references Firm (Firm),
    constraint FK_WRONGLOCATION_Loc foreign key (LocCode, LocFirm, LocType)
        references Subject (Code, Firm, Type),
    constraint FK_WRONGLOCATION_User foreign key (UserIdx)
        references Users (UserIdx)
)
go