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

GO

create procedure DBA.FillSaleOrderLines_MoveOrChangePrice( in @OldRegNo integer,in @NewRegNo integer,in @SaleOrderID integer ) 
begin
  declare @RegNo integer;
  declare @DateReg date;
  declare @Guest integer;
  declare @Room integer;
  declare @RoomName varchar(6);
  declare @NumDays integer;
  declare @Price numeric(12,4);
  declare @OldPrice numeric(12,4);
  declare @KodCurrency integer;
  declare @IsRest integer;
  declare @OldPriceKind integer;
  declare @PriceKind integer;
  declare @RDate date;
  declare @place integer;
  declare @StartDate date;
  declare @EndDate date;
  declare @SaleLineID integer;
  select B.PriceKind,B.Price into @OldPriceKind,@OldPrice
    from Book as B left outer join Rooms as R on B.Room = R.RoomNum
    where B.RegNum = @OldRegNo;
  select dateadd(day,B.PayedLodgings,B.DateRegDt),B.Guest,
    B.Room,R.RoomName,(B.NumLodgings-B.PayedLodgings),B.PriceKind,B.Price,B.KodCurrency, /*M*/ /*M*/
    IsRest,B.DateRegDt,B.Room into @DateReg,@Guest,@Room,@RoomName,@NumDays,@PriceKind,@Price,@KodCurrency,@IsRest,@RDate,@place
    from Book as B left outer join Rooms as R on B.Room = R.RoomNum
    where B.RegNum = @NewRegNo;
  if(@OldPrice = @Price) and(@OldPriceKind = @PriceKind) and(@OldRegNo <> @NewRegNo) then
    return
  end if;
  for f as curs scroll cursor for
    select ID as @ID,ItemID as @ItemID,-(RegQty-PayedQty) as @RestQty,SellPrice as @SellPrice,Discount as @Discount,VatRate as @VatRate,@RestQty*@SellPrice as @rowSum,
      ItemName as @ItemName,SaleLineGuid as @Guid,SOL.StartDate as @Sdate,SOL.EndDate as @EDate
      from SaleOrderLines as SL join SaleOrderLines_Links as SOL on SL.ID = SOL.SaleLineID
      where SOL.Book_RegNum = @OldRegNo and SL.DocId = @SaleOrderID and @RestQty <> 0 and Status = 1 do
    set @SaleLineID = GetCounter('SaleOrderLineID');
    update SaleOrderLines set Status = 2 where Id = @ID;
    insert into SaleOrderLines( Id,DocId,ItemId,Qty,SellPrice,Discount,VatRate,RowSum,ItemName,SaleLineGuid,RegQty,Status ) 
      values( @SaleLineID,@SaleOrderID,@ItemID,@RestQty,@SellPrice,@Discount,@VatRate,@Rowsum,@ItemName,@Guid,@RestQty,3 ) ;
    insert into SaleOrderLines_Links( SaleLineID,Book_RegNum,StartDate,EndDate ) 
      values( @SaleLineID,@OldRegNo,@SDate,@EDate ) 
  end for;
  call FillArticleLodgings(@NewRegNo,0,0,@place);
  for f1 as curs1 scroll cursor for
    select Name as @ArticleName,RealLodgingsNum as @RealLodgings,A.Price as @PriceInvWithDDS,Qty as @Qty,
      MaxQty as @MaxQty,MinQty as @MinQty,ZIndex as @ZIndex,TipDDS as @TipDDS,ServiceNoLodging as @ServiceNo,
      RentMonth as @RentMonth,RentYear as @RentYear,PK.NachinPlat as @NachinPlat
      from #ArticleLodgings as a //join VatRates as VR on a.TipDDS = VR.ID
        join Book as B on A.RegNum = B.RegNum
        join PriceKinds as PK on B.PriceKind = PK.PriceKind order by ZIndex asc do
    set @SaleLineID = GetCounter('SaleOrderLineID');
    insert into SaleOrderLines( Id,DocId,ItemId,Qty,SellPrice,Discount,VatRate,RowSum,ItemName,SaleLineGuid,RegQty ) 
      values( @SaleLineID,@SaleOrderID,@ServiceNo,@MaxQty,@PriceInvWithDDS*(1+(if @TipDDS = 2 then abs(bvrDDSPart) else 0 endif)),0,
      (if @TipDDS = 2 then abs(bvrDDSPart) else 0 endif),@MaxQty*@PriceInvWithDDS*(1+(if @TipDDS = 2 then abs(bvrDDSPart) else 0 endif)),
      @ArticleName,convert(varchar(50),NEWID()),@MaxQty ) ;
    if @NachinPlat = 0 then
      set @StartDate = @RDate;
      set @EndDate = dateadd(dd,@MaxQty,@RDate)
    else
      set @StartDate = '01.' || @RentMonth || '.' || @RentYear;
      set @EndDate = (DATEADD(month,1,@StartDate)-DAY(@StartDate))
    end if;
    insert into SaleOrderLines_Links( SaleLineID,Book_RegNum,StartDate,EndDate ) 
      values( @SaleLineID,@NewRegNo,@StartDate,@EndDate ) 
  end for;
  delete from #Articles
end