CREATE PROCEDURE [dbo].[UpdateMISTable] 
	@SystemDateStr CHAR(8), 
	@Room_HallType tinyint, 
	@ReferenceNumber INT, 
	@GuestNumber INT,  
	@FolioNumber INT, 
	@UpdateType TINYINT, 
	@PackageSameDayAsLogi BIT, 
	@BookedNumberOfRooms INT, 
	@TotalAdults INT,
	@TotalChildren1 INT, 
	@TotalChildren2 INT,   
	@TotalPriceDay MONEY,
	@VatOfPrice MONEY, 
	@VatOfPacket MONEY, 
	@PackagePrice MONEY, 
	@CurrentDateStr CHAR(8), 
	@CurrentDateStrDeparturePackage CHAR(8), 
	@SegmentNumber INT, 
	@PointOfSaleNumber INT, 
	@AllocatedTypeNumber INT, 
	@ValueType TINYINT, 
	@HotelCustomerNumber INT, 
	@Status SmallInt, 
	@CampaignCode SmallInt, 
	@ProjectCode SmallInt, 
	@StaffID int, 
	@WareGroupNumber int, 
	@ResultAccountNumber int, 
	@DepartmentNumber int, 
	@ProductWareLinkNumber int, 
	@IsPackage bit, 
	@Text varchar(256),
	@OwnerReservation bit,
	@ProductNumber int AS
BEGIN


/*

  Created by Kent Johansen July 2009
  Used to save in the MISTable
  Changes:


*/
/*
   Changes:
    OF 20120808: Ext: ProductNumber added to mis
    OF 20120502: Ext: OwnerReservation included.
    OF 20120425: Ext: MisTable is now used as detail view for MIS Detail viewer.
	OF 20120417: Ext: Primary keys removed and detailed mis introduced.
    OF 20120113: Ext: Text added for detailview.
    OF 20111208: Bug: Multiple changes to turnover arround systemdate. Rev. 2.
	OF 20111201: Bug: Pieces and pax arround systemdate was not correct. Som code reverted to old mis model.
	OF 20111125: Ext: Modifications to pax/pieces. Attempt 6. Pax and pieces are resolved from reservations only. I will soon give up..........
    OF 20110819: Ext: New mis version for handling budget to.
    OF 20110503: Ext: Staff, CampaignCode and ProjectCode added.
    OF 20110209: Bug: ReservationStatus added in as param in update mistable sections
    OF 20110208: Ext: Reservations Status added.
    OF 20091116: Ext: Storing in #MISTable_Source added. Fixed.
*/

  IF @UpdateType = 1 OR @UpdateType = 3
  BEGIN
    SET @TotalAdults = 0
    SET @TotalChildren1 = 0
    SET @TotalChildren2 = 0
  END


  IF @UpdateType = 1 OR @UpdateType = 2
  BEGIN
    -- Update Actual
    IF @PackageSameDayAsLogi = 1
    BEGIN
      if @IsPackage = 0
      begin
		  -- insert logi 
			INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, ProductWareLinkNumber, IsPackage, Text, OwnerReservation, ProductNumber) 
			VALUES (@Room_HallType, @ReferenceNumber, @GuestNumber, @FolioNumber, @CurrentDateStr, @SegmentNumber, @PointOfSaleNumber, @AllocatedTypeNumber, 0, 0, 0, 0, 0, 0, 0, 0, @TotalAdults, @TotalChildren1, @TotalChildren2, @BookedNumberOfRooms, @TotalPriceDay, @VatOfPrice, 0, 0, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @ResultAccountNumber, @DepartmentNumber, @ProductWareLinkNumber, @IsPackage, @Text, @OwnerReservation, @ProductNumber)
	  end
      else
	  begin
		  -- Insert package
			INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, ProductWareLinkNumber, IsPackage, Text, OwnerReservation, ProductNumber) 
			VALUES (@Room_HallType, @ReferenceNumber, @GuestNumber, @FolioNumber, @CurrentDateStrDeparturePackage, @SegmentNumber, @PointOfSaleNumber, @AllocatedTypeNumber, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @PackagePrice, @VatOfPacket, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @ResultAccountNumber, @DepartmentNumber, @ProductWareLinkNumber, @IsPackage, @Text, @OwnerReservation, @ProductNumber) 
      end
    END
    ELSE
    BEGIN 
	  -- Insert logi
	  -- Insert package
	    INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, ProductWareLinkNumber, IsPackage, Text, OwnerReservation, ProductNumber) 
        VALUES (@Room_HallType, @ReferenceNumber, @GuestNumber, @FolioNumber, @CurrentDateStr, @SegmentNumber, @PointOfSaleNumber, @AllocatedTypeNumber, 0, 0, 0, 0, 0, 0, 0, 0, @TotalAdults, @TotalChildren1, @TotalChildren2, @BookedNumberOfRooms, @TotalPriceDay, @VatOfPrice, @PackagePrice, @VatOfPacket, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @ResultAccountNumber, @DepartmentNumber, @ProductWareLinkNumber, @IsPackage, @Text, @OwnerReservation, @ProductNumber) 
   END
  END
  ELSE IF @UpdateType = 3 OR @UpdateType = 4
  BEGIN
    -- Insert booked
    IF @PackageSameDayAsLogi = 1
    BEGIN
      if @IsPackage = 0
      begin
	      -- Insert logi
		  -- booked
			INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, ProductWareLinkNumber, IsPackage, Text, OwnerReservation, ProductNumber) 
			VALUES (@Room_HallType, @ReferenceNumber, @GuestNumber, @FolioNumber, @CurrentDateStr, @SegmentNumber, @PointOfSaleNumber, @AllocatedTypeNumber, @TotalAdults, @TotalChildren1, @TotalChildren2, @BookedNumberOfRooms, @TotalPriceDay, @VatOfPrice, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @ResultAccountNumber, @DepartmentNumber, @ProductWareLinkNumber, @IsPackage, @Text, @OwnerReservation, @ProductNumber)
      end
	  else
	  begin
		  -- booked
		  -- Insert package
			INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, ProductWareLinkNumber, IsPackage, Text, OwnerReservation, ProductNumber) 
			VALUES (@Room_HallType, @ReferenceNumber, @GuestNumber, @FolioNumber, @CurrentDateStrDeparturePackage, @SegmentNumber, @PointOfSaleNumber, @AllocatedTypeNumber, 0, 0, 0, 0, 0, 0, @PackagePrice, @VatOfPacket, 0, 0, 0, 0, 0, 0, 0, 0, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @ResultAccountNumber, @DepartmentNumber, @ProductWareLinkNumber, @IsPackage, @Text, @OwnerReservation, @ProductNumber)
      end
    END
    ELSE
    BEGIN
		  -- Booked
		  -- Insert logi
		  -- Insert package
			INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, ProductWareLinkNumber, IsPackage, Text, OwnerReservation, ProductNumber) 
			VALUES (@Room_HallType, @ReferenceNumber, @GuestNumber, @FolioNumber, @CurrentDateStr, @SegmentNumber, @PointOfSaleNumber, @AllocatedTypeNumber, @TotalAdults, @TotalChildren1, @TotalChildren2, @BookedNumberOfRooms, @TotalPriceDay, @VatOfPrice, @PackagePrice, @VatOfPacket, 0, 0, 0, 0, 0, 0, 0, 0, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @ResultAccountNumber, @DepartmentNumber, @ProductWareLinkNumber, @IsPackage, @Text, @OwnerReservation, @ProductNumber)
    END
  END 
END