IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('GetQtyRentForPeriodInMonth'))) THEN Drop procedure GetQtyRentForPeriodInMonth END IF GO create function DBA.GetQtyRentForPeriodInMonth(in @RegNo integer,in @Tip tinyint,in @PeriodStart date,in @PeriodEnd date) returns real --@Tip 0-cqloto kolichestvo, 1-platenoto kolichestvo --Izchislqva koficienta na no6tuvki v zadaden period (plateni ili vsi4ki) ot celiq mesec (broi noshtuvki v perioda v meseca)/(broi dni v meseca) begin declare @Res real; declare @DateArrive date; declare @DateLeave date; declare @DatePaidTo date; declare @MonthIsWith30Days integer; declare @CalcStart date; declare @CalcEnd date; if(@PeriodEnd <= @PeriodStart) then return(0) end if; if((MONTH(@PeriodStart) <> MONTH(@PeriodEnd)) or((YEAR(@PeriodStart) <> YEAR(@PeriodEnd)))) then return(0) end if; select FloatToDate(DateReg), FloatToDate(DateReg+NumLodgings), FloatToDate(DateReg+PayedLodgings) into @DateArrive, @DateLeave, @DatePaidTo from Book where Book.RegNum = @RegNo; if(@DateArrive >= @DateLeave) then return(0) end if; if((Month(@PeriodStart) = Month(@DateArrive)) and(Year(@PeriodStart) = Year(@DateArrive))) or((Month(@PeriodStart) = Month(@DateLeave)) and(Year(@PeriodStart) = Year(@DateLeave))) then set @MonthIsWith30Days=(select MonthIsWith30Days from Hotels where Hotel_ID = (select IntStojnost from Registers where RegisterName = 'HotelID')) else set @MonthIsWith30Days=0 end if; if(@Tip = 1) then set @DateLeave=@DatePaidTo end if; if(@DateArrive > @PeriodEnd) then return(0) end if; if(@DateLeave < @PeriodStart) then return(0) end if; set @CalcStart=@DateArrive; set @CalcEnd=@DateLeave-1; if(@CalcStart <= @PeriodStart) then set @CalcStart=@PeriodStart end if; if(@CalcEnd >= @PeriodEnd) then set @CalcEnd=@PeriodEnd end if; set @Res=(cast(DAY(@CalcEnd)-DAY(@CalcStart)+1 as real)/cast(DOM(MONTH(@PeriodStart),YEAR(@PeriodStart),@MonthIsWith30Days) as real)); if(MONTH(@PeriodStart) = 2) and(@MonthIsWith30Days = 1) then if(cast(DAY(@CalcEnd)-DAY(@CalcStart)+1 as real) = 28) then set @Res=1 end if end if; if(@Res > 1) then return(1) else return(@Res) end if end --[v. 29.07.2009] - Yoan - Saobrazqva se s PriceSeasonPeriods --[v. 04.09.2009] - Yoan & Milko - Ne se saobrazqvane kogato e posledniq den ot mesec ne vliza v spravkata go IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('FillOwePaidRentMonth'))) THEN Drop procedure FillOwePaidRentMonth END IF GO create procedure DBA.FillOwePaidRentMonth(@Month date,@DoDelete tinyint= 0) --Spravka daljim i platen naem - mesec as --Izpolzva se ot FillOweRentForMonth() begin declare @StartDateMonth date, @EndDateMonth date, @TempDate date, @NextSeasonStartDate date set @StartDateMonth="date"('1.' || MONTH(@Month) || '.' || YEAR(@Month)) set @EndDateMonth=dateadd(day,-1,dateadd(month,1,@StartDateMonth)) --message(@StartDateMonth) --message(@EndDateMonth) if(@DoDelete = 1) delete from #OwePaidRentMonth while(@StartDateMonth <= @EndDateMonth) begin select @NextSeasonStartDate = Max(StartDate) from PriceSeasonPeriods as PSP where PSP.N = ((select Max(N) from PriceSeasonPeriods as PSP1 where((PSP1.StartDate <= @StartDateMonth) and(PSP1.EndDate >= @StartDateMonth)))+1) if(@NextSeasonStartDate is null) set @NextSeasonStartDate=@EndDateMonth --message(@NextSeasonStartDate) if(@NextSeasonStartDate < @EndDateMonth) begin insert into #OwePaidRentMonth( GuestNo,NameMonth,Year,Name,UniName,Kurs,Spec,FN,Address,City, Sex,PriceKind,Usluga,Cena,Kol,SumOwe,KolPaid,SumPaid,Razlika, RealDatePaid,Koef,RoomName,Country,DaysInMonth) select B.Guest, DBA.ImeMesec(MONTH(@Month)), YEAR(@Month), G.Name, U.FullName, K.Name, S.Name, G.FN, G.Address, Settlements.SettleMentName, G.Sex, PK.Description, DBA.GetNoshtuvkaNameForPeriodInMonth(B.RegNum,@StartDateMonth,@NextSeasonStartDate-1), Cena=isNull(DBA.GetRentPriceForDate(B.RegNum,@StartDateMonth),B.Price*C.ExchangeRate), Kol=Round(DBA.GetQtyRentForPeriodInMonth(B.RegNum,0,@StartDateMonth,@NextSeasonStartDate-1),2), SumOwe=Round(DBA.GetQtyRentForPeriodInMonth(B.RegNum,0,@StartDateMonth,@NextSeasonStartDate-1)*Cena,2), KolPaid=Round(DBA.GetQtyRentForPeriodInMonth(B.RegNum,1,@StartDateMonth,@NextSeasonStartDate-1),2), SumPaid=Round(DBA.GetQtyRentForPeriodInMonth(B.RegNum,1,@StartDateMonth,@NextSeasonStartDate-1)*Cena,2), Razlika=SumOwe-SumPaid, DBA.GetRealDatePayedRentForPeriod(B.RegNum,@StartDateMonth,@NextSeasonStartDate-1), Kol, R.RoomName, Ctr.Description, DBA.DateIntersection(B.DateRegDt,dateadd(day,B.NumLodgings-1,B.DateRegDt),@StartDateMonth,@NextSeasonStartDate-1)+1 from DBA.Book as B,DBA.PriceKinds as PK,DBA.Guests as G,DBA.Kursove as K,DBA.Specialities as S, DBA.Universities as U,DBA.Currencies as C,DBA.Rooms as R,DBA.Countries as Ctr,DBA.Settlements where B.PriceKind = PK.PriceKind and G.GuestNum = B.WhoPays and PK.NachinPlat = 1 and B.DateRegDt < dateadd(month,1,"date"('1.' || MONTH(@Month) || '.' || YEAR(@Month))) and dateadd(day,B.NumLodgings,B.DateRegDt) > "date"('1.' || MONTH(@Month) || '.' || YEAR(@Month)) and G.Kurs = K.N and G.SpecNo = S.N and G.UniN = U.N and B.KodCurrency = C.KodCurrency and R.RoomNum = B.Room and Settlements.N = G.SettlementN and G.NativeCountry = Ctr.CountryKod and B.Staying <> 3 and Kol <> 0.0 set @StartDateMonth=@NextSeasonStartDate end else begin insert into #OwePaidRentMonth( GuestNo,NameMonth,Year,Name,UniName,Kurs,Spec,FN,Address,City, Sex,PriceKind,Usluga,Cena,Kol,SumOwe,KolPaid,SumPaid,Razlika, RealDatePaid,Koef,RoomName,Country,DaysInMonth) select B.Guest, DBA.ImeMesec(MONTH(@Month)), YEAR(@Month), G.Name, U.FullName, K.Name, S.Name, G.FN, G.Address, Settlements.SettleMentName, G.Sex, PK.Description, DBA.GetNoshtuvkaNameForPeriodInMonth(B.RegNum,@StartDateMonth,@EndDateMonth), Cena=isNull(DBA.GetRentPriceForDate(B.RegNum,@StartDateMonth),B.Price*C.ExchangeRate), Kol=Round(DBA.GetQtyRentForPeriodInMonth(B.RegNum,0,@StartDateMonth,@EndDateMonth),2), SumOwe=Round(DBA.GetQtyRentForPeriodInMonth(B.RegNum,0,@StartDateMonth,@EndDateMonth)*Cena,2), KolPaid=Round(DBA.GetQtyRentForPeriodInMonth(B.RegNum,1,@StartDateMonth,@EndDateMonth),2), SumPaid=Round(DBA.GetQtyRentForPeriodInMonth(B.RegNum,1,@StartDateMonth,@EndDateMonth)*Cena,2), Razlika=SumOwe-SumPaid, DBA.GetRealDatePayedRentForPeriod(B.RegNum,@StartDateMonth,@EndDateMonth), Kol, R.RoomName, Ctr.Description, DBA.DateIntersection(B.DateRegDt,dateadd(day,B.NumLodgings-1,B.DateRegDt),@StartDateMonth,@EndDateMonth)+1 from DBA.Book as B,DBA.PriceKinds as PK,DBA.Guests as G,DBA.Kursove as K,DBA.Specialities as S, DBA.Universities as U,DBA.Currencies as C,DBA.Rooms as R,DBA.Countries as Ctr,DBA.Settlements where B.PriceKind = PK.PriceKind and G.GuestNum = B.WhoPays and PK.NachinPlat = 1 and B.DateRegDt < dateadd(month,1,"date"('1.' || MONTH(@Month) || '.' || YEAR(@Month))) and dateadd(day,B.NumLodgings,B.DateRegDt) > "date"('1.' || MONTH(@Month) || '.' || YEAR(@Month)) and G.Kurs = K.N and G.SpecNo = S.N and G.UniN = U.N and B.KodCurrency = C.KodCurrency and R.RoomNum = B.Room and Settlements.N = G.SettlementN and G.NativeCountry = Ctr.CountryKod and B.Staying <> 3 and Kol <> 0.0 set @StartDateMonth=dateadd(day,1,@EndDateMonth) end end end --[29.07.2009] - Yoan - Saobrazqva se s PriceSeasonPeriods --[04.09.2009] - Yoan - sabrazqva se ako studenta e nastanen v posledniq den ot mesec