if (select count(*) from Services where ServiceNo=-8)=0 then insert into Services(ServiceNo,ServiceName,ServicePrice,KodCurrency,Active,TipSdelka,isstandard,ServiceType,TimeSteps) values(-8,'VTD Депозит',0,1,1,1,1,0,0); end if GO IF (EXISTS (select proc_name from sysprocedure where LCase(proc_name)=LCase('VtdFillLodgingsForContractBill'))) THEN Drop function VtdFillLodgingsForContractBill END IF GO create function DBA.VtdFillLodgingsForContractBill(in @RegNo integer,in @DateReg date,in @Room integer,in @NumDays integer,in @Price double,in @KodCurrency integer,in @IsRest integer,in @ArticleNumber integer,in @place varchar(50)) returns integer begin declare @ArticleName varchar(40); declare @ArticlePrice double; declare @PackagePrice double; declare @PackagePrWithDDS double; //nt declare @TipDDS integer; declare @ServiceNoLodging integer; declare @I integer; declare @Disc double; //nt declare @ArticlePrWithDDS double; //nt declare @IsForAlServices integer; declare @ContractNo integer; //Proverqva dali ima otstypka po dogovora i dali tazi otstypka e za vsi4ki services select C.IsDiscForAll,C.Discount,c.ContractNo into @IsForAlServices, @Disc, @ContractNo from Contracts as C,Book as B where B.ContractPays = C.ContractNo and B.RegNum = @Regno; if(@IsForAlServices = 0) then set @Disc=0 end if; //@ServiceNoLodging e N-na na noshtuvka ili pochivka syotvetno za nashenec ili chujdenec select(if Guests.HumanKind = 1 then(-1) else(-2) endif) into @ServiceNoLodging from Book,Guests where Book.RegNum = @RegNo and Book.Guest = Guests.GuestNum; //Izchislenie cenata na noshtuvkata v leva syobrazno otstypkata po dogovora ako ima otstypka select(ExchangeRate*@Price) into @ArticlePrice from Currencies where KodCurrency = @KodCurrency; set @ArticlePrWithDDS=@ArticlePrice; //Podvajda sumata na uslugite ot paketa ot select Sum(if VidSdelki.TipDDS = 2 then(1.07)*Packages.Price*Currencies.ExchangeRate else Packages.Price*Currencies.ExchangeRate endif) into @PackagePrice from Packages,Currencies,Services,VidSdelki where Packages.RegNo = @RegNo and Packages.KodCurrency = Currencies.KodCurrency and Packages.ServiceNo = Services.ServiceNo and Services.TipSdelka = VidSdelki.N; if @PackagePrice is null then set @PackagePrice=0; set @PackagePrWithDDS=0 //nt else set @PackagePrWithDDS=@PackagePrice //nt end if; //Izchisliava se tipa DDS za noshtuvkata select V.TipDDS into @TipDDS from Services as S,VidSdelki as V where S.ServiceNo = @ServiceNoLodging and S.TipSdelka = V.N; if @TipDDS = 2 then set @ArticlePrice=@ArticlePrice*(1.07); //ako dds-to e vkliucheno v cenata set @TipDDS=1 end if; set @ArticlePrice=@ArticlePrice-@PackagePrice; if @TipDDS = 1 then set @ArticlePrice=@ArticlePrice/(1.07); //ako dds-to e vkliucheno v cenata set @TipDDS=2 end if; if @IsRest = 1 then set @ArticleName='Почивка ' || @place || cast(@Room as varchar(5))+' за '+dateformat(@DateReg,'Dd.Mm') else set @ArticleName='Нощувка ' || @place || cast(@Room as varchar(5))+' от '+dateformat(@DateReg,'Dd.Mm')+' до '+dateformat(@DateReg+@NumDays,'Dd.Mm') end if; if(@Disc = 0) then select srv.Discount into @Disc from ServicesForContract as srv,Book as b where b.ContractPays = srv.ContractNo and b.RegNum = @RegNo and srv.ServiceNo = @ServiceNoLodging end if; //set @ArticlePrice=CalcPriceWithDiscount(@Disc,@ArticlePrice) set @ArticlePrWithDDS=@ArticlePrWithDDS-@PackagePrWithDDS; //nt //Insertva noshtuvka ili pochivka insert into #Articles( ArticleNumber,ArticleName,Measure,ArticleQty,MaxQty,MinQty,ArticlePrice,Discount,RegNo, CreditNo,ArticleSum,TipDDS,ServiceNo,PriceInvWithDDS,ArticleSumWithDDS,RegDate) values( @ArticleNumber,@ArticleName,'бр.', @NumDays,@NumDays,0,@ArticlePrice,@Disc,@RegNo,0,((@ArticlePrice*@NumDays)-((@ArticlePrice*@NumDays)*@Disc)/100), @TipDDS,@ServiceNoLodging,cast(FormatNum(@ArticlePrWithDDS,'0.00') as double), ((@ArticlePrWithDDS*@NumDays)-((@ArticlePrWithDDS*@NumDays)*@Disc)/100),null) ; set @I=1; set @Disc=0; // if(@IsForAlServices=0) then set @Disc=0 // end if // ; //Insertva i uslugite ot paketa for f as curs scroll cursor for select Packages.ServiceNo as @ServiceNo,Packages.Price as @ServicePrice,Packages.KodCurrency as @ServiceCurrency, Services.ServiceName as @ServiceName from Packages,Services where RegNo = @RegNo and Packages.ServiceNo = Services.ServiceNo do select(ExchangeRate*@ServicePrice) into @ArticlePrice from Currencies where KodCurrency = @ServiceCurrency; //Izchisliava se tipa DDS za uslugata // select distinct Max(Contracts.Discount) into @Disc from Contracts,Guests,Book,Packages where Guests.Contract=Contracts.ContractNo and Guests.GuestNum=Book.Guest and Packages.RegNo=Book.RegNum and Book.RegNum=@RegNo; //nt select V.TipDDS into @TipDDS from Services as S,VidSdelki as V where S.ServiceNo = @ServiceNo and S.TipSdelka = V.N; if @TipDDS = 1 then set @ArticlePrice=@ArticlePrice/(1.07); //ako dds-to e vkliucheno v cenata set @TipDDS=2 end if; //set @ArticlePrice=CalcPriceWithDiscount(@Disc,@ArticlePrice); if(@Disc = 0) then select Discount into @Disc from ServicesForContract where ContractNo = @ContractNo and ServiceNo = @ServiceNo end if; // set @ArticlePrice=CalcPriceWithDiscount(@Disc,@ArticlePrice); //Insertva uslugata vuv #Articles insert into #Articles( ArticleNumber,ArticleName,Measure,ArticleQty,MaxQty,MinQty,ArticlePrice,Discount,RegNo, CreditNo,ArticleSum,TipDDS,ServiceNo,PriceInvWithDDS,ArticleSumWithDDS,RegDate) values( @ArticleNumber+@I,@ServiceName,'бр.', @NumDays,@NumDays,0,@ArticlePrice,@Disc,-@RegNo,0,((@ArticlePrice*@NumDays)-((@ArticlePrice*@NumDays)*@Disc)/100), @TipDDS,@ServiceNo,@ArticlePrWithDDS,((@ArticlePrWithDDS*@NumDays)-((@ArticlePrWithDDS*@NumDays)*@Disc)/100),null) ; set @I=@I+1; set @Disc=0 end for; return(@I) end GO IF (EXISTS (select proc_name from sysprocedure where LCase(proc_name)=LCase('VtdFillServicesForBill'))) THEN Drop procedure VtdFillServicesForBill END IF GO create procedure DBA.VtdFillServicesForBill(in @ArticleNumber integer,in @ArticleName varchar(40),in @Qty integer,in @ArticlePrice double,in @CreditNo integer,in @TipDDS integer,in @ServiceNo integer,in @Discount double,in @RegDate date) // Tazi procedura se izpolzva za popylwane vuv wremennata tablica #Articles // na opredelen red ot ServiceCredits begin declare @MaxQty integer; declare @MinQty integer; declare @ArticlePrWithDDS double; //nt if @Qty < 0 then set @MaxQty=0; set @MinQty=@Qty else set @MaxQty=@Qty; set @MinQty=0 end if; set @ArticlePrWithDDS=@ArticlePrice; //nt if @TipDDS = 1 then set @ArticlePrice=@ArticlePrice/(1.07); set @TipDDS=2 end if; insert into #Articles( ArticleNumber,ArticleName,Measure,ArticleQty,MaxQty,MinQty,ArticlePrice,Discount,RegNo, CreditNo,ArticleSum,TipDDS,ServiceNo,PriceInvWithDDS,ArticleSumWithDDS,RegDate) values( @ArticleNumber,@ArticleName,'бр.',@Qty,@MaxQty,@MinQty,@ArticlePrice,@Discount,0, //nt // CreditNo,ArticleSum,TipDDS,ServiceNo) values(@ArticleNumber,@ArticleName,'бр.',@Qty,@MaxQty,@MinQty,@ArticlePrice,0,0, @CreditNo,((@ArticlePrice*@Qty)-((@ArticlePrice*@Qty)*@Discount)/100),@TipDDS,@ServiceNo,@ArticlePrWithDDS, ((@ArticlePrWithDDS*@Qty)-((@ArticlePrWithDDS*@Qty)*@Discount)/100),@RegDate) end GO IF (EXISTS (select proc_name from sysprocedure where LCase(proc_name)=LCase('VtdPrepareContractBill'))) THEN Drop procedure VtdPrepareContractBill END IF GO create procedure DBA.VtdPrepareContractBill(in @ContractNo integer,in @place varchar(50)) // Tazi procedura se izpolzva za napylwane na wremennata tablica #Articles // sus noshtuvkite,pochivkite i uslugite koito triabva da plashtat gostite nastaneni po daden dogovor begin declare @ArticleNumber integer; declare @Deposit double; declare @Disc decimal(12,2); declare @PrWithDisc decimal(12,2); declare @IsForAlServices integer; declare @HumanKind integer; delete from #Articles; select Max(ArticleNumber)+1 into @ArticleNumber from #Articles; if @ArticleNumber is null then set @ArticleNumber=1 end if; //Proverqva dali ima ostanovene otstypka za vsi4ki uslugi ot paketa uslugi po dogovor select C.IsDiscForAll,C.Discount into @IsForAlServices,@Disc from Contracts as C where C.ContractNo = @ContractNo; if(@IsForAlServices = 0) then set @Disc=0 end if; for f2 as curs2 scroll cursor for select B.RegNum as @RegNo,dateadd(day,B.PayedLodgings,B.DateRegDt) as @DateReg,B.Guest as @Guest, B.Room as @Room,(B.NumLodgings-B.PayedLodgings) as @NumDays,B.Price as @Price,B.KodCurrency as @KodCurrency,IsRest as @IsRest from Book as B where B.ContractPays = @ContractNo and(B.NumLodgings-B.PayedLodgings) <> 0 do set @ArticleNumber=@ArticleNumber+VtdFillLodgingsForContractBill(@RegNo,@DateReg,@Room,@NumDays,@Price,@KodCurrency,@IsRest,@ArticleNumber,@place) end for; //Naliva uslugi po dogovor if(@IsForAlServices = 0) then set @Disc=0 end if; for f1 as curs1 scroll cursor for select SC.CreditNo as @CreditNo, S.ServiceName as @ArticleName, SC.RoomNum as @Room, (SC.SumCredit*C.ExchangeRate) as @Price,SC.Service as @Service, (NumberServices-PayedServices) as @ArticleQty,VS.TipDDS as @TipDDS, SC.DateCreditDt as @DateCr from ServiceCredits as SC,Services as S,VidSdelki as VS,Currencies as C where SC.ContractPays = @ContractNo and(NumberServices-PayedServices) <> 0 and SC.Service = S.ServiceNo and S.TipSdelka = VS.N and SC.KodCurrency = C.KodCurrency do if(@Room is not null) then set @ArticleName=@ArticleName || @place || cast(@Room as varchar); else set @ArticleName=@ArticleName end if; if(@Disc = 0) then select Discount into @Disc from ServicesForContract where ServiceNo = @Service and ContractNo = @ContractNo; //set @Price=CalcPriceWithDiscount(@Disc,@Price); end if; call VtdFillServicesForBill(@ArticleNumber,@ArticleName,@ArticleQty,@Price,@CreditNo,@TipDDS,@Service,@Disc,@DateCr); set @Disc=0; set @ArticleNumber=@ArticleNumber+1 end for; set @Deposit=0; set @Deposit=@Deposit+CalcContractDepositSum(@ContractNo)/(1.07); if @Deposit <> 0 then insert into #Articles( ArticleNumber,ArticleName,Measure,ArticleQty,MaxQty,MinQty,ArticlePrice,Discount,RegNo, CreditNo,ArticleSum,TipDDS,ServiceNo,RegDate) values( @ArticleNumber,'Депозити(и)','бр.',-1,0,-1,@Deposit,0, 0,0,-@Deposit,2,1004,today(*)) end if end //[v 18.02.2003] Jorko //[v 18.07.2003] Jorko Pribavia data na uslugata 1004 GO IF (EXISTS (select proc_name from sysprocedure where LCase(proc_name)=LCase('VtdPrepareFacByProform'))) THEN Drop procedure VtdPrepareFacByProform END IF GO create procedure DBA.VtdPrepareFacByProform(in @ProformNo integer,in @place varchar(50)) // Tazi procedura se izpolzva za napylwane na wremennata tablica #Articles // sus noshtuvkite,pochivkite i uslugite koito triabva da plashtat gostite //se pravi faktura po predvaritelno izbrana proforma begin declare @ArticleNumber integer; declare @Deposit double; declare @Disc double; declare @PrWithDisc double; declare @IsForAlServices integer; declare @HumanKind integer; declare @MaxQty integer; declare @MinQty integer; declare @ArticlePrWithDDS double; //Izprazva vremennata tablica za uslugi i noshtuvki delete from #Articles; select Max(ArticleNumber)+1 into @ArticleNumber from #Articles; if @ArticleNumber is null then set @ArticleNumber=1 end if; for f as curs scroll cursor for select ArticleName as @ArticleName,Qty as @Qty,Price as @ArticlePrice,Discount as @Discount,RegNo as @RegNo, CreditNo as @CreditNo,TipDDS as @TipDDS,ServiceNo as @ServiceNo from ProArticles where ProformNo = @ProformNo and ArticleNo > 0 do if(@Qty < 0) then set @MaxQty=0; set @MinQty=@Qty else set @MaxQty=@Qty; set @MinQty=0 end if; set @ArticlePrWithDDS=@ArticlePrice; if @TipDDS = 1 then set @ArticlePrice=@ArticlePrice/(1.07); set @TipDDS=2 end if; insert into #Articles( ArticleNumber,ArticleName,Measure,ArticleQty,MaxQty,MinQty,ArticlePrice,Discount, RegNo,CreditNo,ArticleSum,TipDDS,ServiceNo,PriceInvWithDDS,ArticleSumWithDDS,RegDate) values( @ArticleNumber,@ArticleName,'бр.',@Qty,@MaxQty,@MinQty,@ArticlePrice,@Discount,@RegNo, @CreditNo,((@ArticlePrice*@Qty)-((@ArticlePrice*@Qty)*@Discount)/100),@TipDDS,@ServiceNo,@ArticlePrWithDDS, ((@ArticlePrWithDDS*@Qty)-((@ArticlePrWithDDS*@Qty)*@Discount)/100),null) ; set @ArticleNumber=@ArticleNumber+1 end for //Proverqva dali ima ostanovene otstypka za vsi4ki uslugi ot paketa uslugi po dogovor end //[v 18.02.2003] Jorko GO IF (EXISTS (select proc_name from sysprocedure where LCase(proc_name)=LCase('FinishBill'))) THEN Drop function FinishBill END IF GO create function DBA.FinishBill(in @PayHow integer,in @InvoiceType integer,in @InvoiceDate integer,in @KodAdmin integer,in @Whom varchar(50),in @TotalNoVAT decimal(12,2),in @VAT decimal(12,2),in @GrandTotal decimal(12,2),in @WithoutVAT decimal(12,2),in @BillType integer,in @No integer,in @IsInvoice integer,in @VidSmetka integer,in @ExchangeRate decimal(12,4),in @ContractNo integer) returns integer //Insertva dannite za fakturata vuv Invoices i Articles i insertva parite v ResMoney //@No e syotvetno nomer na grupa, rezervaciq, gost, staia i anulirana smetka, ako smetkata e za grupa, rezervacia, gost, staia ili anulirashta begin atomic declare @InvoiceNo integer; declare @DepositNo integer; declare @TrueInvoiceNo integer; declare @CancelInvoiceNo integer; declare @ServiceNo integer; declare @CreditNo integer; declare @ResMoneyStatus integer; declare @SmetkaCurrency integer; declare @NullDepositCount integer; declare @InvoiceMask integer; //ivaylo if @BillType <> 7 then select Count(ServiceNo) into @NullDepositCount from #Articles where ArticleQty = 0 and ServiceNo = 1005 end if; if @BillType = 7 then select Count(ServiceNo) into @NullDepositCount from #Articles where ArticleQty = 0 and ServiceNo = 1004 end if; //Vzima Tipa valuta ot vida smetka select KodCurrency into @SmetkaCurrency from VidSmetki where N = @VidSmetka; //Iztriva nulevite kolichestva delete from #Articles where ArticleQty = 0; //Vmukva dannite za fakturata set @InvoiceNo=GetCounter('InvoiceCounter'); if @IsInvoice = 1 then set @TrueInvoiceNo=GetCounter('TrueInvoiceNo') else set @TrueInvoiceNo=0 end if; if @BillType = -1 then set @CancelInvoiceNo=@No else set @CancelInvoiceNo=0 end if; set @ResMoneyStatus=1; select IntStojnost into @InvoiceMask from Registers where RegisterName = 'MaskaFaktura'; //ivaylo //Trqbva da se dobavi i nomera na dogovora pri insert-a na nova smetka //ivaylo insert into Invoices( InvoiceNo,Status,PayHow,InvoiceType,InvoiceDate,BankTown,Upolnomosten, UpolnomDate,UpolnomNum,KodAdmin,Whom,MOL,WhomTown,WhomDanNum,Bulstat,KodCurrency, TotalNoVAT,VAT,GrandTotal,CancelInvoiceNo,TrueInvoiceNo,WithoutVAT,TrueInvoiceDate,ExchangeRate,VidSmetka,ContractNo,MaskaFaktura) values( @InvoiceNo,1,@PayHow,@InvoiceType,@InvoiceDate,'','','','',@KodAdmin, @Whom,'','','','',@SmetkaCurrency,@TotalNoVAT,@VAT,@GrandTotal,@CancelInvoiceNo,@TrueInvoiceNo,@WithoutVAT,FloatToDate(@InvoiceDate),@ExchangeRate,@VidSmetka,@ContractNo,@InvoiceMask) ; for f1 as curs1 scroll cursor for select ArticleNumber as @ArticleNumber,ArticleName as @ArticleName,ArticleQty as @ArticleQty, ArticlePrice as @ArticlePrice,RegNo as @RegNo,CreditNo as @CreditNum,ArticleSum as @ArticleSum, TipDDS as @TipDDS,Discount as @Discount,ServiceNo as @Service,ArticleSumInvCurrency as @SumInvCurrency, RegDate as @RDate from #Articles do //Ako e swobodna smetka vmukva vuv uslugite na gosta uslugite ot svobodnata smetka if @BillType = 5 then select ServiceNo into @ServiceNo from Services where ServiceNo = @Service; if(@ServiceNo <> 1005) and(@ServiceNo <> 1004) and (@ServiceNo <> -8) then set @CreditNo=GetCounter('CreditCounter'); insert into ServiceCredits( CreditNo,DateCredit,GuestNo,Service,SumCredit,KodCurrency,KodAdmin,NumberServices,PayedServices,WhoPays) values( @CreditNo,@InvoiceDate,@No,@ServiceNo,@ArticleSum,1,@KodAdmin,@ArticleQty,0,@No) else set @ResMoneyStatus=2; set @CreditNo=0 end if else set @CreditNo=@CreditNum end if; //Insertva zapis vuv Articles insert into Articles( InvoiceNo,RegNo,Price,CreditNo,TotalSum,TotalSumInvCurrency,Qty,KodCurrency,ArticleNo, ArticleName,TipDDS,Discount,ServiceNo,RegDate) values( @InvoiceNo,@RegNo,@ArticlePrice,@CreditNo,@ArticleSum,@SumInvCurrency, @ArticleQty,1,@ArticleNumber,@ArticleName,@TipDDS,@Discount,@Service,@RDate) ; //Promenia noshtuvkite if @RegNo > 0 then update Book set PayedLodgings = PayedLodgings+sign(@BillType)*@ArticleQty where RegNum = @RegNo end if; //Promenia uslugite if @CreditNo > 0 then update ServiceCredits set PayedServices = PayedServices+sign(@BillType)*@ArticleQty where CreditNo = @CreditNo end if end for; //Ako e anulirane na smetka i anuliranata smetka ima slujebni zapisi za markirane na depositi gi iztriva //Promenia CancelInvoiceNo na anuliranata smetka na -1 if @BillType = -1 then delete from DBA.ResMoney where InvoiceNo = @No and DateMoney = 0; update Invoices set CancelInvoiceNo = -1 where InvoiceNo = @No; select max(Status) into @ResMoneyStatus from ResMoney where InvoiceNo = @No end if; //Nulira predishnite depoziti syotvetno na gost, grupa, staia i reservacia //Nulira depositi, ako e smetka za gost if @BillType = 1 then call CheckDeposits(@No,@KodAdmin,@InvoiceNo,@NullDepositCount) end if; //Nulira depositi, ako e smetka za grupa if @BillType = 2 then for f2 as curs2 scroll cursor for select GuestNum as @GuestNum from Guests where "Group" = @No do call CheckDeposits(@GuestNum,@KodAdmin,@InvoiceNo,@NullDepositCount) end for end if; //Nulira Depositi ako e smetka za staia if @BillType = 3 then for f3 as curs3 scroll cursor for select Guest as @GuestNum from Book where Room = @No and Staying = 1 do call CheckDeposits(@GuestNum,@KodAdmin,@InvoiceNo,@NullDepositCount) end for end if; //Nulira depositi, ako e smetka za grupa if @BillType = 4 then for f4 as curs4 scroll cursor for select GuestNum as @GuestNum from Guests where Reservation = @No do call CheckDeposits(@GuestNum,@KodAdmin,@InvoiceNo,@NullDepositCount) end for end if; //Nulira depositi, ako e smetka po dogovor if(@BillType = 7) and(@ContractNo <> 0) then for f5 as curs5 scroll cursor for select Contract from Guests where contract = @ContractNo do call CheckContractDeposits(@ContractNo,@KodAdmin,@InvoiceNo,@NullDepositCount) end for end if; //Vmukva plashtaniata ot wremennata tablica za plashtania for f6 as curs6 scroll cursor for select PaymentType as @PaymentType,Sum as @Sum,KodCurrency as @KodCurrency,AccountNumber as @AccountNumber, GuestNo as @mGuestNo,GroupNo as @mGroupNo,RoomNo as @mRoomNo,ReserveNo as @mReserveNo,PTVid as @PTVid from //,ContractNo as @Contract #PaymentTypes do set @DepositNo=GetCounter('DepositCounter'); insert into ResMoney( ReserveNo,DateMoney,KodAdmin,Sum,Currency,Payment,Note,GuestNo,DepositNo,Status,GroupNo, ContractNo,InvoiceNo,RoomNo,AccountNumber,PTVid) values( @mReserveNo,@InvoiceDate,@KodAdmin,@Sum,@KodCurrency,@PaymentType,'',@mGuestNo,@DepositNo,@ResMoneyStatus,@mGroupNo,@ContractNo,@InvoiceNo,@mRoomNo,@accountNumber,@PTVid) end for; return(@InvoiceNo) end //[v 00.11.18] //[v 01.07.09] - Niki - za vidovete smetki //[v 18.09.02] - Dobaven e parametyr za N na Dogovora vyv vryska s Smetki po dogovori //[v 18.02.2003] Jorko