CREATE  TABLE IF NOT EXISTS `InvoiceBase` (
  `Oid` BIGINT(19) NOT NULL AUTO_INCREMENT ,
  `Tid` INT(10) NOT NULL ,
  `SupportConceptOid` BIGINT(19) NULL DEFAULT NULL ,
  `CostBearer2SupportConceptOid` BIGINT(19) NULL DEFAULT NULL ,
  `RecipientCustomerOid` BIGINT(19) NULL DEFAULT NULL ,
  `RecipientOrganisationOid` BIGINT(19) NULL DEFAULT NULL ,
  `RecipientPersonOid` BIGINT(19) NULL DEFAULT NULL ,
  `RecipientCostBearerOid` BIGINT(19) NULL DEFAULT NULL ,
  `RecipientAddressOid` BIGINT(19) NULL DEFAULT NULL ,
  `RecipientOrganisation` VARCHAR(256) NULL DEFAULT NULL ,
  `RecipientDivision` VARCHAR(45) NULL DEFAULT NULL ,
  `RecipientPersonFirstName` VARCHAR(256) NULL DEFAULT NULL ,
  `RecipientPersonLastName` VARCHAR(256) NULL DEFAULT NULL ,
  `InvoiceNumber` VARCHAR(256) NULL DEFAULT NULL ,
  `InvoiceTitle` VARCHAR(256) NULL DEFAULT NULL ,
  `InvoiceDate` DATETIME NULL DEFAULT NULL ,
  `AccountingPeriodStart` DATETIME NULL DEFAULT NULL ,
  `AccountingPeriodEnd` DATETIME NULL DEFAULT NULL ,
  `SenderFirstName` VARCHAR(256) NULL DEFAULT NULL ,
  `SenderLastName` VARCHAR(256) NULL DEFAULT NULL ,
  `SenderDivision` VARCHAR(256) NULL DEFAULT NULL ,
  `SenderOrganisation` VARCHAR(256) NULL DEFAULT NULL ,
  `SenderAddressOid` BIGINT(19) NULL DEFAULT NULL ,
  `CustomerReferenceNumber` VARCHAR(256) NULL DEFAULT NULL ,
  `CreatorFirstName` VARCHAR(256) NULL DEFAULT NULL ,
  `CreatorLastName` VARCHAR(256) NULL DEFAULT NULL ,
  `DueDate` DATETIME NULL DEFAULT NULL ,
  `Type` INTEGER(10) NULL DEFAULT NULL ,
  `IsPaid` TINYINT(4) NULL DEFAULT NULL ,
  `IsActive` TINYINT(4) NULL DEFAULT NULL ,
  `Notice` VARCHAR(1024) NULL DEFAULT NULL ,
  `InsTs` DATETIME NULL DEFAULT NULL ,
  `InsUser` VARCHAR(256) NULL DEFAULT NULL ,
  `Version` BIGINT(19) NULL DEFAULT NULL ,
  `UdpUser` VARCHAR(256) NULL DEFAULT NULL ,
  `SystemEntryID` INT(10) NULL DEFAULT NULL ,
  PRIMARY KEY (`Oid`) ,
  INDEX `FK_IN_PER` (`RecipientPersonOid` ASC) ,
  INDEX `FK_IN_CUS` (`RecipientCustomerOid` ASC) ,
  INDEX `FK_IN_ORG` (`RecipientOrganisationOid` ASC) ,
  INDEX `FK_IN_CB` (`RecipientCostBearerOid` ASC) ,
  INDEX `FK_IN_SC` (`SupportConceptOid` ASC) ,
  INDEX `FK_IN_ADD_RE` (`RecipientAddressOid` ASC) ,
  INDEX `FK_IN_ADD_SE` (`SenderAddressOid` ASC) ,
  INDEX `FK_IN_SC2CB` (`CostBearer2SupportConceptOid` ASC) ,
  CONSTRAINT `FK_IN_PER`
    FOREIGN KEY (`RecipientPersonOid` )
    REFERENCES `person` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_IN_CUS`
    FOREIGN KEY (`RecipientCustomerOid` )
    REFERENCES `customer` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_IN_ORG`
    FOREIGN KEY (`RecipientOrganisationOid` )
    REFERENCES `organisation` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_IN_CB`
    FOREIGN KEY (`RecipientCostBearerOid` )
    REFERENCES `costbearer` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_IN_SC`
    FOREIGN KEY (`SupportConceptOid` )
    REFERENCES `supportconcept` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_IN_ADD_RE`
    FOREIGN KEY (`RecipientAddressOid` )
    REFERENCES `address` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_IN_ADD_SE`
    FOREIGN KEY (`SenderAddressOid` )
    REFERENCES `address` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_IN_SC2CB`
    FOREIGN KEY (`CostBearer2SupportConceptOid` )
    REFERENCES `costbearer2supportconcept` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `InvoiceItem` (
  `Oid` BIGINT(19) NOT NULL AUTO_INCREMENT ,
  `Tid` INT(10) NOT NULL ,
  `InvoiceBaseOid` BIGINT(19) NULL DEFAULT NULL ,
  `ItemPeriodStart` DATETIME NULL DEFAULT NULL ,
  `ItemPeriodEnd` DATETIME NULL DEFAULT NULL ,
  `ItemDescription` VARCHAR(256) NULL DEFAULT NULL ,
  `UnitCount` INT(11) NULL DEFAULT NULL ,
  `UnitDescription` VARCHAR(45) NULL DEFAULT NULL ,
  `AmountPerUnit` DECIMAL(18,10) NULL DEFAULT NULL ,
  `AmountTotal` DECIMAL(18,10) NULL DEFAULT NULL ,
  `IsActive` VARCHAR(45) NULL DEFAULT NULL ,
  `Notice` VARCHAR(1024) NULL DEFAULT NULL ,
  `InsTs` DATETIME NULL DEFAULT NULL ,
  `InsUser` VARCHAR(256) NULL DEFAULT NULL ,
  `Version` BIGINT(19) NULL DEFAULT NULL ,
  `UdpUser` VARCHAR(256) NULL DEFAULT NULL ,
  `SystemEntryID` INT(10) NULL DEFAULT NULL ,
  PRIMARY KEY (`Oid`) ,
  INDEX `FK_INIT_INB` (`InvoiceBaseOid` ASC) ,
  CONSTRAINT `FK_INIT_INB`
    FOREIGN KEY (`InvoiceBaseOid` )
    REFERENCES `InvoiceBase` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `invoice` (
  `Oid` BIGINT(19) NOT NULL AUTO_INCREMENT ,
  `Tid` INT(10) NOT NULL ,
  `SupportConceptOid` BIGINT(19) NULL DEFAULT NULL ,
  `OrganisationOid` BIGINT(19) NULL DEFAULT NULL ,
  `InvoicingPeriodStart` DATETIME NULL DEFAULT NULL ,
  `InvoicingPeriodEnd` DATETIME NULL DEFAULT NULL ,
  `Amount` DECIMAL(18,10) NULL DEFAULT NULL ,
  `InvoiceDate` DATETIME NULL DEFAULT NULL ,
  `InvoiceNumber` VARCHAR(256) NULL DEFAULT NULL ,
  `PaidDate` DATETIME NULL DEFAULT NULL ,
  `Notice` VARCHAR(1024) NULL DEFAULT NULL ,
  `IsActive` TINYINT(4) NULL DEFAULT NULL ,
  `InsTs` DATETIME NULL DEFAULT NULL ,
  `InsUser` VARCHAR(256) NULL DEFAULT NULL ,
  `UpdUser` VARCHAR(256) NULL DEFAULT NULL ,
  `Version` BIGINT(19) NULL DEFAULT NULL ,
  `SystemEntryID` INT(10) NULL DEFAULT NULL ,
  PRIMARY KEY (`Oid`) ,
  INDEX `invoice2organisation` (`OrganisationOid` ASC) ,
  INDEX `invoce2supportconcept` (`SupportConceptOid` ASC) ,
  CONSTRAINT `invoice2organisation`
    FOREIGN KEY (`OrganisationOid` )
    REFERENCES `organisation` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `invoce2supportconcept`
    FOREIGN KEY (`SupportConceptOid` )
    REFERENCES `supportconcept` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `SettlementInvoice` (
  `Oid` BIGINT(19) NOT NULL AUTO_INCREMENT ,
  `Tid` INT(10) NOT NULL ,
  `InvoiceBaseOid` BIGINT(19) NULL DEFAULT NULL ,
  `ApprovedFLS` DECIMAL(18,10) NULL DEFAULT NULL ,
  `RecordedBillableFLSNetto` DECIMAL(18,10) NULL DEFAULT NULL ,
  `RecordedBillableFLSNotBillableOutOfApprovedSpan` DECIMAL(18,10) NULL DEFAULT NULL ,
  `RecordedBillableFLSNotBillableCustomerAbsent` DECIMAL(18,10) NULL DEFAULT NULL ,
  `AmountRecordedBillableFLS` DECIMAL(18,10) NULL DEFAULT NULL ,
  `AmountRateFactor` DECIMAL(18,10) NULL DEFAULT NULL ,
  `AmountAdvancedPayments` DECIMAL(18,10) NULL DEFAULT NULL ,
  `Claim` DECIMAL(18,10) NULL DEFAULT NULL ,
  `IsActive` VARCHAR(45) NULL DEFAULT NULL ,
  `Notice` VARCHAR(1024) NULL DEFAULT NULL ,
  `InsTs` DATETIME NULL DEFAULT NULL ,
  `InsUser` VARCHAR(256) NULL DEFAULT NULL ,
  `Version` BIGINT(19) NULL DEFAULT NULL ,
  `UdpUser` VARCHAR(256) NULL DEFAULT NULL ,
  `SystemEntryID` INT(10) NULL DEFAULT NULL ,
  `ApprovalNoticeDate` DATETIME NULL DEFAULT NULL ,
  `RecordedBillableFLSBrutto` DECIMAL(18,10) NULL DEFAULT NULL ,
  `AmountWithRateFactor` DECIMAL(18,10) NULL DEFAULT NULL ,
  `ApprovedFLSWithActualHourlyRate` DECIMAL(18,10) NULL DEFAULT NULL ,
  `ApprovedFLSWithOldHourlyRate` DECIMAL(18,10) NULL DEFAULT NULL ,
  `HourlyRateOldEndDate` DATETIME NULL DEFAULT NULL ,
  `HourlyRateNewStartDate` DATETIME NULL DEFAULT NULL ,
  `HourlyRateNew` DECIMAL(18,10) NULL DEFAULT NULL ,
  `AmountTotalAdvancedPaidWithOldHourlyRate` DECIMAL(18,10) NULL DEFAULT NULL ,
  `HourlyRateOld` DECIMAL(18,10) NULL DEFAULT NULL ,
  `AmountTotalAdvancedPaidWithNewHourlyRate` DECIMAL(18,10) NULL DEFAULT NULL ,
  PRIMARY KEY (`Oid`) ,
  INDEX `FK_SI_IB` (`InvoiceBaseOid` ASC) ,
  CONSTRAINT `FK_SI_IB`
    FOREIGN KEY (`InvoiceBaseOid` )
    REFERENCES `InvoiceBase` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

ALTER TABLE `person` ADD COLUMN `InvoiceAddressOid` BIGINT(19) NULL DEFAULT NULL  AFTER `AddressOid` , 
  ADD CONSTRAINT `FK_PERSON_ADDRESS_INVOICE`
  FOREIGN KEY (`InvoiceAddressOid` )
  REFERENCES `address` (`Oid` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
, ADD INDEX `FK_PERSON_ADDRESS_INVOICE` (`InvoiceAddressOid` ASC) ;

ALTER TABLE `organisation` ADD COLUMN `InvoiceAddressOid` BIGINT(19) NULL DEFAULT NULL  AFTER `AddressOid` , 
  ADD CONSTRAINT `FK_ORG_ADD_INV`
  FOREIGN KEY (`InvoiceAddressOid` )
  REFERENCES `address` (`Oid` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
, ADD INDEX `FK_ORG_ADD_INV` (`InvoiceAddressOid` ASC) ;

ALTER TABLE `contact` ADD COLUMN `InvoiceBaseRecipientOid` BIGINT(19) NULL DEFAULT NULL  AFTER `OrganisationOid` , ADD COLUMN `InvoiceBaseSenderOid` BIGINT(19) NULL DEFAULT NULL  AFTER `PersonOid` , 
  ADD CONSTRAINT `FK_CONTACT_INVOICEBASE_RECIPIENT`
  FOREIGN KEY (`InvoiceBaseRecipientOid` )
  REFERENCES `InvoiceBase` (`Oid` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION, 
  ADD CONSTRAINT `FK_CONTACT_INVOICEBASE_SENDER`
  FOREIGN KEY (`InvoiceBaseSenderOid` )
  REFERENCES `InvoiceBase` (`Oid` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
, ADD INDEX `FK_CONTACT_INVOICEBASE_RECIPIENT` (`InvoiceBaseRecipientOid` ASC) 
, ADD INDEX `FK_CONTACT_INVOICEBASE_SENDER` (`InvoiceBaseSenderOid` ASC) ;

ALTER TABLE `servicecategory` ADD COLUMN `BillableFixedAmount` DECIMAL(18,10) DEFAULT NULL;

ALTER TABLE `servicedescription` ADD COLUMN `BillableFixedAmount` DECIMAL(18,10) DEFAULT NULL;

ALTER TABLE `customer` ADD COLUMN `DebitorNumber` VARCHAR(128) COLLATE latin1_swedish_ci DEFAULT NULL;
ALTER TABLE `customer` ADD COLUMN `IsAdvisedOrAttended` TINYINT(4) DEFAULT NULL;

ALTER TABLE `organisation` ADD COLUMN `DebitorNumber` VARCHAR(128) COLLATE latin1_swedish_ci DEFAULT NULL;
ALTER TABLE `organisation` ADD COLUMN `CostCenter` VARCHAR(128) COLLATE latin1_swedish_ci DEFAULT NULL;
ALTER TABLE `organisation` ADD COLUMN `AccountNumber` VARCHAR(32) COLLATE latin1_swedish_ci DEFAULT NULL;

ALTER TABLE `SupportConcept` ADD COLUMN `RequisitionSendDate` DATETIME DEFAULT NULL;
ALTER TABLE `SupportConcept` ADD COLUMN `SupportConceptSendDate` DATETIME DEFAULT NULL;


CREATE  TABLE IF NOT EXISTS `AssessmentSheetCategory` (
  `Oid` BIGINT(19) NOT NULL AUTO_INCREMENT ,
  `Tid` INT(10) NOT NULL ,
  `Description` VARCHAR(512) NULL DEFAULT NULL ,
  `IsActive` VARCHAR(45) NULL DEFAULT NULL ,
  `Notice` VARCHAR(1024) NULL DEFAULT NULL ,
  `InsTs` DATETIME NULL DEFAULT NULL ,
  `InsUser` VARCHAR(256) NULL DEFAULT NULL ,
  `Version` BIGINT(19) NULL DEFAULT NULL ,
  `UdpUser` VARCHAR(256) NULL DEFAULT NULL ,
  `SystemEntryID` INT(10) NULL DEFAULT NULL ,
  PRIMARY KEY (`Oid`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;


CREATE  TABLE IF NOT EXISTS `AssessmentSheetValue` (
  `Oid` BIGINT(19) NOT NULL AUTO_INCREMENT ,
  `Tid` INT(10) NOT NULL ,
  `Sign` VARCHAR(5) NULL DEFAULT NULL ,
  `Color` VARCHAR(45) NULL DEFAULT NULL ,
  `ShortcutKey` VARCHAR(5) NULL DEFAULT NULL ,
  `Description` VARCHAR(512) NULL DEFAULT NULL ,
  `IsActive` VARCHAR(45) NULL DEFAULT NULL ,
  `Notice` VARCHAR(1024) NULL DEFAULT NULL ,
  `InsTs` DATETIME NULL DEFAULT NULL ,
  `InsUser` VARCHAR(256) NULL DEFAULT NULL ,
  `Version` BIGINT(19) NULL DEFAULT NULL ,
  `UdpUser` VARCHAR(256) NULL DEFAULT NULL ,
  `SystemEntryID` INT(10) NULL DEFAULT NULL ,
  PRIMARY KEY (`Oid`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;


CREATE  TABLE IF NOT EXISTS `AssessmentSheetEntry` (
  `Oid` BIGINT(19) NOT NULL AUTO_INCREMENT ,
  `Tid` INT(10) NOT NULL ,
  `CustomerOid` BIGINT(19) NOT NULL ,
  `Day` DATETIME NULL DEFAULT NULL ,
  `AssessmentSheetCategoryOid` BIGINT(19) NOT NULL ,
  `AssessmentSheetValueOid` BIGINT(19) NOT NULL ,
  `IsActive` VARCHAR(45) NULL DEFAULT NULL ,
  `Notice` VARCHAR(1024) NULL DEFAULT NULL ,
  `InsTs` DATETIME NULL DEFAULT NULL ,
  `InsUser` VARCHAR(256) NULL DEFAULT NULL ,
  `Version` BIGINT(19) NULL DEFAULT NULL ,
  `UdpUser` VARCHAR(256) NULL DEFAULT NULL ,
  `SystemEntryID` INT(10) NULL DEFAULT NULL ,
  PRIMARY KEY (`Oid`) ,
  INDEX `fk_AssessmentSheetEntry_customer1` (`CustomerOid` ASC) ,
  INDEX `fk_AssessmentSheetEntry_AssessmentSheetCategory1` (`AssessmentSheetCategoryOid` ASC) ,
  INDEX `fk_AssessmentSheetEntry_AssessmentSheetValue1` (`AssessmentSheetValueOid` ASC) ,
  CONSTRAINT `fk_AssessmentSheetEntry_customer1`
    FOREIGN KEY (`CustomerOid` )
    REFERENCES `customer` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_AssessmentSheetEntry_AssessmentSheetCategory1`
    FOREIGN KEY (`AssessmentSheetCategoryOid` )
    REFERENCES `AssessmentSheetCategory` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_AssessmentSheetEntry_AssessmentSheetValue1`
    FOREIGN KEY (`AssessmentSheetValueOid` )
    REFERENCES `AssessmentSheetValue` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;


CREATE  TABLE IF NOT EXISTS `AssessmentSheetCategory2AssessmentSheetValue` (
  `AssessmentSheetCategoryOid` BIGINT(19) NOT NULL ,
  `AssessmentSheetValueOid` BIGINT(19) NOT NULL ,
  PRIMARY KEY (`AssessmentSheetCategoryOid`, `AssessmentSheetValueOid`) ,
  INDEX `fk_AssessmentSheetCategory_has_AssessmentSheetValue_Assessmen1` (`AssessmentSheetCategoryOid` ASC) ,
  INDEX `fk_AssessmentSheetCategory_has_AssessmentSheetValue_Assessmen2` (`AssessmentSheetValueOid` ASC) ,
  CONSTRAINT `fk_AssessmentSheetCategory_has_AssessmentSheetValue_Assessmen1`
    FOREIGN KEY (`AssessmentSheetCategoryOid` )
    REFERENCES `AssessmentSheetCategory` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_AssessmentSheetCategory_has_AssessmentSheetValue_Assessmen2`
    FOREIGN KEY (`AssessmentSheetValueOid` )
    REFERENCES `AssessmentSheetValue` (`Oid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

ALTER TABLE `InvoiceBase` ADD COLUMN `DunningLetterCount` INT(10) NULL DEFAULT NULL  AFTER `SystemEntryID` ;




ALTER TABLE `servicerecord` ADD COLUMN `GroupPersonCount` INTEGER(10) DEFAULT NULL;
ALTER TABLE `servicerecord` ADD COLUMN `GroupRoundedDuration` DECIMAL(11,0)  DEFAULT NULL;
ALTER TABLE `servicerecord` ADD COLUMN `GroupId` BIGINT(19) DEFAULT NULL;


ALTER TABLE `mandator` ADD COLUMN `Settings` VARCHAR(1024) COLLATE latin1_swedish_ci DEFAULT NULL;