IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('MoveToRoom')))
THEN Drop procedure MoveToRoom
END IF

GO

create procedure DBA.MoveToRoom(in @OldRegNo integer,in @RoomNo integer,in @NowDate real,in @NumDays integer,in @KodAdmin integer,in @Price decimal(12,2),in @KodCurrency integer,in @PriceService decimal(12,2),in @PriceKind integer)
//Premestva daden gost ot edna staia v druga
//Ako ima predplateni noshtuvki i sumata e razlichna ot novata suma dobavia edna usluga - "Noshtuvka" s razlikata v cenite
begin atomic
  declare @OldLeftLodgings integer;
  declare @OldPayed integer;
  declare @GuestNo integer;
  declare @HumanKind integer;
  declare @OldWhoPays integer;
  declare @OldGroupPays integer;
  declare @OldContractPays integer;
  declare @OldPrice double;
  declare @OldKodCurrency integer;
  declare @OldRoomNo integer;
  declare @OldDateReg real;
  declare @NewNo integer;
  declare @NewPayed integer;
  declare @ServiceNoLodging integer;
  declare @OldRoomName varchar(10);
  declare @NewRoomName varchar(10);
  declare @OldPriceKind integer;
  declare @DateRegDt Date;
  declare @OldOldRegNum integer;
  declare @OldOldEndDate Date;
  declare @NumDaysToRemove integer;
  declare @RoomClass integer;
  declare @NewPricePK numeric(12, 2);
  select PayedLodgings,Guest,WhoPays,GroupPays,ContractPays,Price,KodCurrency,Room,DateReg,Guests.HumanKind,Book.PriceKind into @OldPayed,
    @GuestNo,@OldWhoPays,@OldGroupPays,@OldContractPays,@OldPrice,
    @OldKodCurrency,@OldRoomNo,@OldDateReg,@HumanKind,@OldPriceKind from Book,Guests where RegNum = @OldRegNo and Book.Guest = Guests.GuestNum;
  set @OldLeftLodgings=ceiling(@NowDate-@OldDateReg);
  //
  //Ako ima predplateni dni wmukwa edna usluga s razlikata v cenite
  if @OldLeftLodgings < @OldPayed then
    if @PriceService <> 0 then
      set @NewNo=GetCounter('creditcounter');
      if @HumanKind = 1 then
        set @ServiceNoLodging=-1
      else
        set @ServiceNoLodging=-2
      end if;
      insert into ServiceCredits( DateCredit,GuestNo,Service,SumCredit,KodCurrency,KodAdmin,NumberServices,CreditNo,PayedServices,WhoPays,
        GroupPays,ContractPays,RoomPays) 
        values( @NowDate,@GuestNo,@ServiceNoLodging,@PriceService,@KodCurrency,@KodAdmin,1,@NewNo,0,@OldWhoPays,
        @OldGroupPays,@OldContractPays,0) 
    end if
  end if;
  //Izchistva gosta ot starata staia
  if @OldLeftLodgings < @OldPayed then
    set @NewPayed=@OldLeftLodgings
  else
    set @NewPayed=@OldPayed
  end if;
  if ((@OldRoomNo=@RoomNo) and (@OldDateReg>@NowDate) ) then
	set @DateRegDt = FloatToDate(@NowDate);
	if @NumDays<@OldPayed then
		set @NewPayed=@NumDays;
	else
		set @NewPayed=@OldPayed;
	end if;
	set @RoomClass=(Select RoomCat from Rooms where RoomNum=@RoomNo);
	set @NewPricePK=(SELECT PriceList.PriceAll 
		FROM PriceList,PriceKinds, Registers WHERE PriceList.PriceKindNo=PriceKinds.PriceKind 
		AND PriceList.SeasonNo=Registers.IntStojnost AND Registers.RegisterName='ActivePriceSeason'
		AND PriceList.ClassNo=@RoomClass AND PriceList.HumanKind=1 AND PriceList.AdultType=0 and PriceKinds.PriceKind=@PriceKind);	
	
	Update Book set NumLodgings = @NumDays, PayedLodgings = @NewPayed, Price=@NewPricePK, PriceKind=@PriceKind, DateReg=@NowDate, DateRegDt=@DateRegDt where RegNum = @OldRegNo;
	
	set @OldOldRegNum=(Select first RegNum from Book where (Guest=@GuestNo) and (RegNum<@OldRegNo) and (@DateRegDt between DateRegDt and DATEADD(day, NumLodgings, DateRegDt)));
	if (@OldOldRegNum is not null) then
		set @OldOldEndDate=(Select DATEADD(day, NumLodgings, DateRegDt) from Book where RegNum=@OldOldRegNum);
		set @NumDaysToRemove=(select DATEDIFF(day, @DateRegDt, @OldOldEndDate));
		if (select PayedLodgings from Book where RegNum=@OldOldRegNum)<@NumDaysToRemove then
			Update Book set NumLodgings=NumLodgings-@NumDaysToRemove where RegNum=@OldOldRegNum;
		else
			Update Book set NumLodgings=NumLodgings-@NumDaysToRemove, PayedLodgings=PayedLodgings-@NumDaysToRemove where RegNum=@OldOldRegNum;
		end if;
	end if;	
  else
	update Book set NumLodgings = @OldLeftLodgings,PayedLodgings = @NewPayed,Staying = 2
    where RegNum = @OldRegNo;
	//
	update Rooms set Buzy = (if NumPeople = 1 then 0 else 2
    endif),NumPeople = NumPeople-1 where RoomNum = @OldRoomNo;
	//Nastaniava gosta v novata staia
	set @NewNo=GetCounter('bookcounter');
	set @NewPayed=(@OldPayed-@OldLeftLodgings);
	if @NewPayed < 0 then
		set @NewPayed=0
	end if;
	insert into Book( RegNum,DateReg,NumLodgings,PayedLodgings,Payed,Staying,PriceKind,KodAdmin,Room,
		Price,Guest,WhoPays,KodCurrency,GroupPays,ContractPays,IsRest) 
		values( @NewNo,@NowDate,@NumDays,@NewPayed,
		0,1,@PriceKind,@KodAdmin,@RoomNo,@Price,@GuestNo,@OldWhoPays,@KodCurrency,@OldGroupPays,@OldContractPays,0) ;
	//vmukva uslugite ot paketa
	call InsertPackage(@NewNo);
	//updejtva infoto za staita
	update Rooms set NumPeople = NumPeople+1,Buzy = 2 where RoomNum = @RoomNo;
  end if;
end
//[v 01.02.26]
//[v 22.10.2003] Jorko v insert na Book @OldKodCurrency --> @KodCurrency
//[v 10.12.2008 MON] Nixon - Dobaven cenorazpis
