-- [1] change types -- alter table Invoices modify TrueInvoiceNo numeric(10,0) GO alter table SpaInvoices modify TrueInvoiceNo numeric(10,0) GO alter table VaucherInvoices modify TrueInvoiceNo numeric(10,0) GO alter table Proform modify TrueInvoiceNo numeric(10,0) GO alter table Proform delete primary key GO alter table Proform modify ProformNo numeric(10,0), add primary key (ProformNo) GO alter table Counters modify TrueInvoiceNo numeric(10,0), modify TrueVaucherInvoiceNo numeric(10,0), modify TrueProfNo numeric(10,0) GO alter table SpaCounters modify TrueInvoiceNo numeric(10,0) GO alter table VtdVauchers modify TrueInvoceNo numeric(10,0) -- [2] alter function GetCounter -- GO IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('GetCounter'))) THEN Drop procedure GetCounter END IF GO create function DBA.GetCounter(in CounterName char(50)) returns integer begin atomic declare @R integer; --Maria + declare error exception for sqlstate value '40W02'; --Maria - // message 'before' type info to console; --Maria + if (CounterName = 'TrueInvoiceNo' or CounterName = 'TrueVaucherInvoiceNo' or CounterName = 'TrueProfNo') then signal error else --Maria - execute immediate 'set @R = isnull((SELECT ' || CounterName || ' FROM Counters),0)'; message 'middle' type info to console; execute immediate 'UPDATE Counters SET ' || CounterName || '=' || (@R+1); message 'after' type info to console; --Maria + end if; --Maria - return(@R+1) end GO -- [3] alter function SpaGetCounter -- IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('SpaGetCounter'))) THEN Drop procedure SpaGetCounter END IF GO create function DBA.SpaGetCounter(in CounterName char(50)) returns integer begin atomic declare @R integer; --Maria + declare error exception for sqlstate value '40W02'; if (CounterName = 'TrueInvoiceNo') then signal error else --Maria - execute immediate 'set @R = isnull((SELECT ' || CounterName || ' FROM SpaCounters),0)'; execute immediate 'UPDATE SpaCounters SET ' || CounterName || '=' || (@R+1); --Maria + end if; --Maria - return(@R+1) end GO -- [4] create GetFakturaCounter -- IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('GetFakturaCounter'))) THEN Drop procedure GetFakturaCounter END IF GO create function DBA.GetFakturaCounter(in CounterName char(50)) returns numeric(10,0) begin atomic declare @R numeric(10,0); // message 'before' type info to console; execute immediate 'set @R = isnull((SELECT ' || CounterName || ' FROM Counters),0)'; message 'middle' type info to console; execute immediate 'UPDATE Counters SET ' || CounterName || '=' || (@R+1); message 'after' type info to console; return(@R+1) end GO -- [5] create GetSpaFakturaCounter -- IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('GetSpaFakturaCounter'))) THEN Drop procedure GetSpaFakturaCounter END IF GO create function DBA.GetSpaFakturaCounter(in CounterName char(50)) returns numeric(10,0) begin atomic declare @R numeric(10,0); // message 'before' type info to console; execute immediate 'set @R = isnull((SELECT ' || CounterName || ' FROM SpaCounters),0)'; message 'middle' type info to console; execute immediate 'UPDATE SpaCounters SET ' || CounterName || '=' || (@R+1); message 'after' type info to console; return(@R+1) end GO -- [6] alter function FinishBill -- IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('FinishBill'))) THEN Drop procedure 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,in @DDSType 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; --Maria + --declare @TrueInvoiceNo integer; declare @TrueInvoiceNo numeric(10,0); --Maria - declare @CancelInvoiceNo integer; declare @ServiceNo integer; declare @CreditNo integer; declare @ResMoneyStatus integer; declare @SmetkaCurrency integer; declare @NullDepositCount integer; declare @InvoiceMask integer; //ivaylo declare @DepContractNo 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 --Maria + --set @TrueInvoiceNo=GetCounter('TrueInvoiceNo') set @TrueInvoiceNo=GetFakturaCounter('TrueInvoiceNo') --Maria - 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,DDSType) values( @InvoiceNo,1,@PayHow,@InvoiceType,@InvoiceDate,'','','','',@KodAdmin, @Whom,'','','','',1,@TotalNoVAT,@VAT,@GrandTotal,@CancelInvoiceNo,@TrueInvoiceNo,@WithoutVAT,FloatToDate(@InvoiceDate),1.0,@VidSmetka,@ContractNo,@InvoiceMask,@DDSType) ; 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,DDSType) values( @InvoiceNo,@RegNo,@ArticlePrice,@CreditNo,@ArticleSum,@SumInvCurrency, @ArticleQty,1,@ArticleNumber,@ArticleName,@TipDDS,@Discount,@Service,@RDate,@DDSType) ; //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; //////////////////////////////////////////////// ivaylo set @DepContractNo=0; select MAX(ContractNo) into @DepContractNo from ResMoney where InvoiceNo = @No; if(@DepContractNo <> 0) then update DepositForContracts set GlobalInvoice = 0 where GlobalInvoice = @No and ContractNo = @DepContractNo end if end if; //////////////////////////////////////////////// ivaylo //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 - fixed if(@BillType = 7) and(@ContractNo <> 0) then call CheckContractDeposits(@ContractNo,@KodAdmin,@InvoiceNo,@NullDepositCount) 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'); ///////////////////////////////////// ivaylo if((@ContractNo <> 0) and(@BillType = 5) and(@mGuestNo = 0)) then insert into DepositForContracts( GlobalInvoice,SubInvoice,ContractNo) values( 0,@InvoiceNo,@ContractNo) end if; //////////////////////////////////// ivaylo 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 //[ 02.11.2007] Kozludjov - fix depoziti po dogovor //[v 14.03.2008] Maria - tip na TrueInvoiceNo e promenen i se vika funkcia GetFakturaCounter, koiato dava nomera na fakturata GO -- [7] alter function SpaFinishBill -- IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('SpaFinishBill'))) THEN Drop procedure SpaFinishBill END IF GO create function DBA.SpaFinishBill(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 begin atomic declare @InvoiceNo integer; declare @DepositNo integer; --Maria + --declare @TrueInvoiceNo integer; declare @TrueInvoiceNo numeric(10,0); --Maria - 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; select KodCurrency into @SmetkaCurrency from VidSmetki where N = @VidSmetka; delete from #Articles where ArticleQty = 0; set @InvoiceNo=SpaGetCounter('InvoiceCounter'); if @IsInvoice = 1 then --Maria + --set @TrueInvoiceNo=SpaGetCounter('TrueInvoiceNo') set @TrueInvoiceNo=GetSpaFakturaCounter('TrueInvoiceNo') --Maria - 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 SpaRegisters where RegisterName = 'MaskaFaktura'; //ivaylo insert into SpaInvoices( 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,SpaResID as @SpaReservationID from #Articles do if @BillType = 5 then select ServiceNo into @ServiceNo from Services where ServiceNo = @Service; if(@ServiceNo <> 1005) and(@ServiceNo <> 1004) then set @CreditNo=GetCounter('CreditCounter'); insert into ServiceCredits( CreditNo,DateCredit,GuestNo,Service,SumCredit,KodCurrency,KodAdmin,NumberServices,PayedServices,WhoPays,SPaReservationFk,SpaGuestTypeFlag) values( @CreditNo,@InvoiceDate,@No,@ServiceNo,@ArticleSum,1,@KodAdmin,@ArticleQty,0,@No,@SpaReservationID,1) else set @ResMoneyStatus=2; set @CreditNo=0 end if else set @CreditNo=@CreditNum end if; insert into SpaArticles( 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) ; if @RegNo > 0 then update Book set PayedLodgings = PayedLodgings+sign(@BillType)*@ArticleQty where RegNum = @RegNo end if; if @CreditNo > 0 then update ServiceCredits set PayedServices = PayedServices+sign(@BillType)*@ArticleQty where CreditNo = @CreditNo end if end for; if @BillType = -1 then delete from DBA.SpaResMoney where InvoiceNo = @No and DateMoney = 0; update SpaInvoices set CancelInvoiceNo = -1 where InvoiceNo = @No; select max(Status) into @ResMoneyStatus from SpaResMoney where InvoiceNo = @No end if; if @BillType = 1 then call SpaCheckDeposits(@No,@KodAdmin,@InvoiceNo,@NullDepositCount) end if; if @BillType = 2 then for f2 as curs2 scroll cursor for select GuestNum as @GuestNum from Guests where "Group" = @No do call SpaCheckDeposits(@GuestNum,@KodAdmin,@InvoiceNo,@NullDepositCount) end for end if; 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 SpaCheckDeposits(@GuestNum,@KodAdmin,@InvoiceNo,@NullDepositCount) end for end if; if @BillType = 4 then for f4 as curs4 scroll cursor for select GuestNum as @GuestNum from Guests where Reservation = @No do call SpaCheckDeposits(@GuestNum,@KodAdmin,@InvoiceNo,@NullDepositCount) end for end if; if(@BillType = 7) and(@ContractNo <> 0) then for f5 as curs5 scroll cursor for select Contract from Guests where contract = @ContractNo do call SpaCheckContractDeposits(@ContractNo,@KodAdmin,@InvoiceNo,@NullDepositCount) end for end if; 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 #PaymentTypes do set @DepositNo=SpaGetCounter('DepositCounter'); insert into SpaResMoney( 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 14.03.2008] - Maria - tip na TrueInvoiceNo e promenen i se vika funkcia GetSpaFakturaCounter, koiato dava nomera na fakturata GO -- [8] alter function FinishProformBill -- IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('FinishProformBill'))) THEN Drop procedure FinishProformBill END IF GO create function DBA.FinishProformBill(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,in @DDSType integer) --Maria + --returns integer returns numeric(10,0) --Maria - //Insertva dannite fakturata vuv Proform i ProArticles i sled tova po tazi Proforma moje da se izdava faktura begin atomic --Maria + --declare @ProformNo integer; declare @ProformNo numeric(10,0); --Maria - declare @DepositNo integer; --Maria + --declare @TrueInvoiceNo integer; declare @TrueInvoiceNo numeric(10,0); --Maria - declare @CancelInvoiceNo integer; declare @ServiceNo integer; declare @CreditNo integer; declare @ResMoneyStatus integer; declare @SmetkaCurrency integer; //declare @NullDepositCount integer; //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 --Maria + --set @ProformNo=GetCounter('TrueProfNo'); set @ProformNo=GetFakturaCounter('TrueProfNo'); --Maria - //Trqbva da se dobavi i nomera na dogovora pri insert-a na nova smetka insert into Proform( ProformNo,PayHow,InvoiceType,InvoiceDate,BankTown,Upolnomosten, UpolnomDate,UpolnomNum,KodAdmin,Whom,MOL,WhomTown,WhomDanNum,Bulstat,KodCurrency, TotalNoVAT,VAT,GrandTotal,TrueInvoiceNo,WithoutVAT,TrueInvoiceDate,ExchangeRate,VidSmetka,ContractNo,DDSType) values( @ProformNo,@PayHow,@InvoiceType,@InvoiceDate,'','','','',@KodAdmin, @Whom,'','','','',1,@TotalNoVAT,@VAT,@GrandTotal,@TrueInvoiceNo,@WithoutVAT,FloatToDate(@InvoiceDate),1.0,@VidSmetka,@ContractNo,@DDSType) ; for f as curs 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 from #Articles do //Ako e swobodna smetka vmukva vuv uslugite na gosta uslugite ot svobodnata smetka //Insertva zapis vuv Articles insert into ProArticles( ProformNo,RegNo,Price,CreditNo,TotalSum,TotalSumInvCurrency,Qty,KodCurrency,ArticleNo, ArticleName,TipDDS,Discount,ServiceNo,DDSType) values( @ProformNo,@RegNo,@ArticlePrice,@CreditNum,@ArticleSum,@SumInvCurrency, @ArticleQty,1,@ArticleNumber,@ArticleName,@TipDDS,@Discount,@Service,@DDSType) end for; //Promenia noshtuvkite //Ako e anulirane na smetka i anuliranata smetka ima slujebni zapisi za markirane na depositi gi iztriva //Promenia CancelInvoiceNo na anuliranata smetka na -1 return(@ProformNo) 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 14.03.2008] - Maria - TrueInvoiceNo i ProformNo da sa numeric i funkcia GetFakturaCounter GO -- [9] alter FinishVaucherBill -- IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('FinishVaucherBill'))) THEN Drop procedure FinishVaucherBill END IF GO create function DBA.FinishVaucherBill(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 @BillType integer,in @No integer,in @IsInvoice integer,in @VidSmetka integer,in @ExchangeRate decimal(12,4),in @ContractNo integer,in @WithoutVAT decimal(12,2)) 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; --Maria + --declare @TrueInvoiceNo integer; declare @TrueInvoiceNo numeric(10,0); --Maria - declare @CancelInvoiceNo integer; declare @ServiceNo integer; declare @CreditNo integer; declare @ArtNum integer; declare @ResMoneyStatus integer; declare @SmetkaCurrency integer; declare @NullDepositCount integer; declare @InvoiceMask integer; //ivaylo declare @p integer; -- ------------------------------------- message '+ FinishVaucherBill START' type info to console; -- Yanko + /* 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; */ set @NullDepositCount=(select Count(*) from #VaucherArticles where ServiceNo = -8 and TotalNights = 0); -- Yanko - //Vzima Tipa valuta ot vida smetka select KodCurrency into @SmetkaCurrency from VidSmetki where N = @VidSmetka; //Iztriva nulevite kolichestva delete from #VaucherArticles where TotalNights = 0; //Vmukva dannite za fakturata //-------------------------------------------------- ivaylo set @p=(select IntStojnost from Registers where RegisterName = 'VTDCommonCounter'); if @p = 1 then set @InvoiceNo=GetCounter('VaucherInvoiceCounter') // //ivaylo else set @InvoiceNo=GetCounter('InvoiceCounter') // //ivaylo end if; if @IsInvoice = 1 then if @p = 1 then --Maria + --set @TrueInvoiceNo=GetCounter('TrueVaucherInvoiceNo') //TrueVaucherInvoiceNo ivaylo set @TrueInvoiceNo=GetFakturaCounter('TrueVaucherInvoiceNo') --Maria - else --Maria + --set @TrueInvoiceNo=GetCounter('TrueInvoiceNo') //TrueVaucherInvoiceNo ivaylo set @TrueInvoiceNo=GetFakturaCounter('TrueInvoiceNo') --Maria - end if else set @TrueInvoiceNo=0 end if; //-------------------------------------------------- ivaylo -- Yanko +, mahane na subtotalite ako po dogovor niama razbivane na invoisa po vaucher ili gost if(select count(*) from OtherDiskForVaucher where ContractNo = @ContractNo) = 0 then delete from #VaucherArticles where RowType in( 3,4,5,6,11) end if; -- Yanko - if @BillType = -1 then set @CancelInvoiceNo=@No else set @CancelInvoiceNo=0 end if; set @ResMoneyStatus=1; set @ArtNum=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 insert into VaucherInvoices( VaucherInvoiceNo,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) ; //Insertva kato zapis sumata ot vsi4ki vau4eri po dogovor v ServiceKredits kato usluga "VTD" set @CreditNo=GetCounter('CreditCounter'); insert into ServiceCredits( DateCredit,GuestNo,Service,SumCredit,KodCurrency,KodAdmin,NumberServices,CreditNo,PayedServices,WhoPays,GroupPays,ContractPays,RoomPays,PBXLogN,DateCreditDt,RoomNum) select @InvoiceDate,0,ServiceNo,sum(GrandTotal),@SmetkaCurrency,@KodAdmin,1,@CreditNo,1,0,0,@ContractNo,0,null,FloatToDate(@InvoiceDate),null from #VaucherArticles where VaucherN <> 0 group by ServiceNo; //Insertva zapis vuv Articles za suma s vaucheri po dogovor for f0 as curs0 scroll cursor for select VaucherN as @VaucherNo,VaucherNum as @VaucherNum,VaucherSeason as @Seasson,VaucherBoard as @BoardPlan, VaucherRoomType as @Room,VaucherBedType as @AdditionBed,Adult as @Adult,Child as @Child,Nights as @Nights, TotalNights as @TotalNights,SinglePrice as @RealPrice,GrandTotal as @GrTotal,ArticleName as @ArticleName, Discount as @Disc,ServiceNo as @ServiceNum,Currency as @Currency,TranslateName as @TranslateName, TranslateSeason as @TranslateSeason,TranslateBed as @TranslateBed,GuestName as @GuestName,TranslateGuestName as @TranslateGuestName, SeasonNo as @SeassonNo,HumanKind as @HumanKind,AdultType as @AdultType,ExtraBed as @HasExtraBed,BoardPlan as @ValBoardPlan, RoomType as @RoomType,ForVaucherN as @ForVaucherN,RowType as @RowType,TempGuestID as @TempGuestID from #VaucherArticles where VaucherN <> 0 order by VaucherN asc do insert into VaucherArticles( InvoiceNo,ArticleNo,VaucherN, VaucherNum,VaucherSeason,VaucherBoard,VaucherRoomType,VaucherBedType,Adult,Child,Nights,TotalNights,SinglePrice,GrandTotal,ArticleName,GuestName,CreditNo,Discount,ServiceNo,Currency,TranslateName,TranslateSeason,TranslateBed,TranslateGuestName, SeasonNo,HumanKind,AdultType,ExtraBed,BoardPlan,RoomType,ForVaucherN,RowType,TempGuestID) values( @InvoiceNo,@ArtNum,@VaucherNo,@VaucherNum,@Seasson,@BoardPlan,@Room,@AdditionBed,@Adult,@Child,@Nights,@TotalNights, @RealPrice,@GrTotal,@ArticleName,@GuestName,@CreditNo,@Disc,@ServiceNum,@SmetkaCurrency,@TranslateName,@TranslateSeason,@TranslateBed,@TranslateGuestName, @SeassonNo,@HumanKind,@AdultType,@HasExtraBed,@ValBoardPlan,@RoomType,@ForVaucherN,@RowType,@TempGuestID) ; set @ArtNum=@ArtNum+1 end for; for f2 as curs2 scroll cursor for select v.VaucherN as @Vaucher from #VaucherArticles as va left outer join VtdVauchers as v on (v.VaucherN = va.VaucherN) where v.VaucherN is not null and v.ContractNo = @ContractNo group by v.VaucherN do if @BillType <> -1 then update VtdVauchers set TrueInvoceNo = @TrueInvoiceNo where ContractNo = @ContractNo and VaucherN = @Vaucher; update VtdVauchers set InvoiceNo = @InvoiceNo where ContractNo = @ContractNo and VaucherN = @Vaucher else update VtdVauchers set TrueInvoceNo = 0,CancelInvoiceNo = @CancelInvoiceNo where ContractNo = @ContractNo and VaucherN = @Vaucher; update VtdVauchers set InvoiceNo = 0 where ContractNo = @ContractNo and VaucherN = @Vaucher end if end for; for f3 as curs3 scroll cursor for select SinglePrice as @ArticlePrice,GrandTotal as @ArticleSum,Adult as @Adult,Child as @Child, TotalNights as @ArticleQty,ArticleName as @ArticleName,Discount as @Discount, ServiceNo as @Service,TranslateName as @TranslateName,TranslateSeason as @TranslateSeason, TranslateBed as @TranslateBed,ForVaucherN as @ForVaucherN,RowType as @RowType,TempGuestID as @TempGuestID from #VaucherArticles where (VaucherN = 0) and(Creditno is not null) do insert into VaucherArticles( InvoiceNo,ArticleNo,VaucherN,VaucherNum,VaucherSeason,VaucherBoard,VaucherRoomType, VaucherBedType,Adult,Child,Nights,TotalNights,SinglePrice,GrandTotal,ArticleName,CreditNo,Discount,ServiceNo, Currency,TranslateName,TranslateSeason,TranslateBed,ForVaucherN,RowType,TempGuestID) values( @InvoiceNo,@ArtNum,'','','','','','',@Adult,@Child,@ArticleQty,@ArticleQty,@ArticlePrice,@ArticleSum,@ArticleName, 0,@Discount,@Service,@SmetkaCurrency,@TranslateName,@TranslateSeason,@TranslateBed,@ForVaucherN,@RowType,@TempGuestID) ; set @ArtNum=@ArtNum+1 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 VaucherInvoiceNo = @No and DateMoney = 0; update VaucherInvoices set CancelInvoiceNo = -1 where VaucherInvoiceNo = @No; select max(Status) into @ResMoneyStatus from ResMoney where VaucherInvoiceNo = @No end if; //Nulira depositi, ako e smetka po dogovor -- Yanko + if @NullDepositCount = 0 then if((@BillType = 7) or(@BillType = 8)) and(@ContractNo <> 0) then call CheckContractDeposits(@ContractNo,@KodAdmin,@InvoiceNo,@NullDepositCount) end if end if; -- Yanko - //Vmukva plashtaniata ot wremennata tablica za plashtania for f4 as curs4 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 #PaymentTypes do set @DepositNo=GetCounter('DepositCounter'); insert into ResMoney( ReserveNo,DateMoney,KodAdmin,Sum,Currency,Payment,Note,GuestNo,DepositNo,Status,GroupNo, ContractNo,VaucherInvoiceNo,RoomNo,AccountNumber,PTVid) values( @mReserveNo,@InvoiceDate,@KodAdmin,@Sum,@KodCurrency,@PaymentType,'',@mGuestNo,@DepositNo,@ResMoneyStatus,@mGroupNo,@ContractNo,@InvoiceNo,@mRoomNo,@accountNumber,@PTVid) end for; message '+ FinishVaucherBill END' type info to console; return(@InvoiceNo) end // v.x [] // v.x1 [01.Oct.2007] - Yanko, Mahane na subtotalite ako po dogovor niama razbivane na invoisa po vaucher ili gost // v.x2 [14.March.2008] - Maria, TrueInvoiceNo da e numeric i funkcia GetFakturaCounter, koiato dava stoinostta na TrueVaucherInvoiceNo ot Counters GO -- [10] alter function FinishVaucherProformBill -- IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('FinishVaucherProformBill'))) THEN Drop procedure FinishVaucherProformBill END IF GO create function DBA.FinishVaucherProformBill(in @PayHow integer,in @InvoiceType integer,in @InvoiceDate integer,in @KodAdmin integer,in @Whom varchar(50),in @TotalNoVAT double,in @VAT double,in @GrandTotal double,in @WithoutVAT double,in @BillType integer,in @No integer,in @IsInvoice integer,in @VidSmetka integer,in @ExchangeRate double,in @ContractNo integer) --Maria + --returns integer returns numeric(10,0) --Maria - //Insertva dannite fakturata vuv Proform i ProVaucherArticles i sled tova po tazi Proforma moje da se izdava faktura za smetka dogovor - vaucher begin atomic --Maria + --declare @ProformNo integer; declare @ProformNo numeric(10,0); --Maria - declare @DepositNo integer; --Maria + --declare @TrueInvoiceNo integer; declare @TrueInvoiceNo numeric(10,0); --Maria - declare @CancelInvoiceNo integer; declare @ServiceNo integer; //declare @CreditNo integer; declare @ResMoneyStatus integer; declare @SmetkaCurrency integer; declare @ArtNo integer; -- --------------------------------- message '+ FinishVaucherProformBill START' type info to console; //Vzima Tipa valuta ot vida smetka select KodCurrency into @SmetkaCurrency from VidSmetki where N = @VidSmetka; //Iztriva nulevite kolichestva delete from #VaucherArticles where TotalNights = 0; //Vmukva dannite za fakturata --Maria + --set @ProformNo=GetCounter('TrueProfNo'); set @ProformNo=GetFakturaCounter('TrueProfNo'); --Maria - //Trqbva da se dobavi i nomera na dogovora pri insert-a na nova smetka insert into Proform( ProformNo,PayHow,InvoiceType,InvoiceDate,BankTown,Upolnomosten, UpolnomDate,UpolnomNum,KodAdmin,Whom,MOL,WhomTown,WhomDanNum,Bulstat,KodCurrency, TotalNoVAT,VAT,GrandTotal,TrueInvoiceNo,WithoutVAT,TrueInvoiceDate,ExchangeRate,VidSmetka,ContractNo,DDSType) values( @ProformNo,@PayHow,@InvoiceType,@InvoiceDate,'','','','',@KodAdmin, @Whom,'','','','',@SmetkaCurrency,@TotalNoVAT,@VAT,@GrandTotal,@TrueInvoiceNo,@WithoutVAT,FloatToDate(@InvoiceDate),@ExchangeRate,@VidSmetka,@ContractNo,2) ; set @ArtNo=1; // return; for f1 as curs1 scroll cursor for select ArticleNumber,VaucherN as @VaucherNo,VaucherNum as @VaucherNum,VaucherSeason as @Seasson, VaucherBoard as @BoardPlan,VaucherRoomType as @Room,VaucherBedType as @AdditionBed,Adult as @Adult,Child as @Child, Nights as @Nights,TotalNights as @TotalNights,SinglePrice as @RealPrice,GrandTotal as @GrTotal,ArticleName as @ArticleName, Discount as @Disc,ServiceNo as @ServiceNum,Currency as @Currency,CreditNo as @CreditNo, TranslateName as @TranslateName,TranslateSeason as @TranslateSeason,TranslateBed as @TranslateBed, GuestName as @GuestName,TranslateGuestName as @TranslateGuestName, SeasonNo as @SeassonNo,HumanKind as @HumanKind,AdultType as @AdultType,ExtraBed as @HasExtraBed, BoardPlan as @ValBoardPlan,RoomType as @RoomType,RowType as @RowType,TempGuestID as @TempGuestID from #VaucherArticles where RowType = 1 order by ArticleNumber asc do insert into ProVaucherArticles( ProformNo,ArticleNo,VaucherN, VaucherNum,VaucherSeason,VaucherBoard,VaucherRoomType,VaucherBedType,Adult,Child,Nights,TotalNights,SinglePrice,GrandTotal,ArticleName,GuestName,CreditNo,Discount,ServiceNo,Currency,TranslateName,TranslateSeason,TranslateBed,TranslateGuestName, SeasonNo,HumanKind,AdultType,ExtraBed,BoardPlan,RoomType,ForVaucherN,RowType,TempGuestID) values( @ProformNo,@ArtNo,@VaucherNo,@VaucherNum,@Seasson,@BoardPlan,@Room,@AdditionBed,@Adult,@Child,@Nights,@TotalNights, @RealPrice,@GrTotal,@ArticleName,@GuestName,@CreditNo,@Disc,@ServiceNum,@Currency,@TranslateName,@TranslateSeason,@TranslateBed,@TranslateGuestName, @SeassonNo,@HumanKind,@AdultType,@HasExtraBed,@ValBoardPlan,@RoomType,@VaucherNo,@RowType,@TempGuestID) ; set @ArtNo=@ArtNo+1 end for; //----------------------------------------- ivaylo for f4 as curs4 scroll cursor for select ArticleNumber,VaucherN as @VaucherNo,VaucherNum as @VaucherNum,VaucherSeason as @Seasson, VaucherBoard as @BoardPlan,VaucherRoomType as @Room,VaucherBedType as @AdditionBed,Adult as @Adult,Child as @Child, Nights as @Nights,TotalNights as @TotalNights,SinglePrice as @RealPrice,GrandTotal as @GrTotal,ArticleName as @ArticleName, Discount as @Disc,ServiceNo as @ServiceNum,Currency as @Currency,CreditNo as @CreditNo, TranslateName as @TranslateName,TranslateSeason as @TranslateSeason,TranslateBed as @TranslateBed, GuestName as @GuestName,TranslateGuestName as @TranslateGuestName, SeasonNo as @SeassonNo,HumanKind as @HumanKind,AdultType as @AdultType,ExtraBed as @HasExtraBed,BoardPlan as @ValBoardPlan, RoomType as @RoomType,ForVaucherN as @ForVaucherN,RowType as @RowType,TempGuestID as @TempGuestID from #VaucherArticles where RowType in( 10,12) order by ArticleNumber asc do insert into ProVaucherArticles( ProformNo,ArticleNo,VaucherN, VaucherNum,VaucherSeason,VaucherBoard,VaucherRoomType,VaucherBedType,Adult,Child,Nights,TotalNights,SinglePrice,GrandTotal,ArticleName,GuestName,CreditNo,Discount,ServiceNo,Currency,TranslateName,TranslateSeason,TranslateBed,TranslateGuestName, SeasonNo,HumanKind,AdultType,ExtraBed,BoardPlan,RoomType,ForVaucherN,RowType,TempGuestID) values( @ProformNo,@ArtNo,0,@VaucherNum,@Seasson,@BoardPlan,@Room,@AdditionBed,@Adult,@Child,@Nights,@TotalNights, @RealPrice,@GrTotal,@ArticleName,@GuestName,@CreditNo,@Disc,@ServiceNum,@Currency,@TranslateName,@TranslateSeason,@TranslateBed,@TranslateGuestName, @SeassonNo,@HumanKind,@AdultType,@HasExtraBed,@ValBoardPlan,@RoomType,@ForVaucherN,@RowType,@TempGuestID) ; set @ArtNo=@ArtNo+1 end for; //----------------------------------------- ivaylo for f2 as curs2 scroll cursor for select ArticleNumber,VaucherN as @VaucherNo,VaucherNum as @VaucherNum,VaucherSeason as @Seasson, VaucherBoard as @BoardPlan,VaucherRoomType as @Room,VaucherBedType as @AdditionBed,Adult as @Adult,Child as @Child, Nights as @Nights,TotalNights as @TotalNights,SinglePrice as @RealPrice,GrandTotal as @GrTotal,ArticleName as @ArticleName, Discount as @Disc,ServiceNo as @ServiceNum,Currency as @Currency,CreditNo as @CreditNo, TranslateName as @TranslateName,TranslateSeason as @TranslateSeason,TranslateBed as @TranslateBed, GuestName as @GuestName,TranslateGuestName as @TranslateGuestName, SeasonNo as @SeassonNo,HumanKind as @HumanKind,AdultType as @AdultType,ExtraBed as @HasExtraBed, BoardPlan as @ValBoardPlan,RoomType as @RoomType,RowType as @RowType,TempGuestID as @TempGuestID from #VaucherArticles where RowType = 2 order by ArticleNumber asc do insert into ProVaucherArticles( ProformNo,ArticleNo,VaucherN, VaucherNum,VaucherSeason,VaucherBoard,VaucherRoomType,VaucherBedType,Adult,Child,Nights,TotalNights,SinglePrice,GrandTotal,ArticleName,GuestName,CreditNo,Discount,ServiceNo,Currency,TranslateName,TranslateSeason,TranslateBed,TranslateGuestName, SeasonNo,HumanKind,AdultType,ExtraBed,BoardPlan,RoomType,RowType,TempGuestID) values( @ProformNo,@ArtNo,@VaucherNo,@VaucherNum,@Seasson,@BoardPlan,@Room,@AdditionBed,@Adult,@Child,@Nights,@TotalNights, @RealPrice,@GrTotal,@ArticleName,@GuestName,@CreditNo,@Disc,@ServiceNum,@Currency,@TranslateName,@TranslateSeason,@TranslateBed,@TranslateGuestName, @SeassonNo,@HumanKind,@AdultType,@HasExtraBed,@ValBoardPlan,@RoomType,@RowType,@TempGuestID) ; set @ArtNo=@ArtNo+1 end for; //------------------------------------------------------------------- ivaylo Depozit for f3 as curs3 scroll cursor for select ArticleNumber,VaucherN as @VaucherNo,VaucherNum as @VaucherNum,VaucherSeason as @Seasson, VaucherBoard as @BoardPlan,VaucherRoomType as @Room,VaucherBedType as @AdditionBed,Adult as @Adult,Child as @Child, Nights as @Nights,TotalNights as @TotalNights,SinglePrice as @RealPrice,GrandTotal as @GrTotal,ArticleName as @ArticleName, Discount as @Disc,ServiceNo as @ServiceNum,Currency as @Currency,CreditNo as @CreditNo, TranslateName as @TranslateName,TranslateSeason as @TranslateSeason,TranslateBed as @TranslateBed, GuestName as @GuestName,TranslateGuestName as @TranslateGuestName, SeasonNo as @SeassonNo,HumanKind as @HumanKind,AdultType as @AdultType,ExtraBed as @HasExtraBed, BoardPlan as @ValBoardPlan,RoomType as @RoomType,RowType as @RowType,TempGuestID as @TempGuestID from #VaucherArticles where RowType = 7 order by ArticleNumber asc do insert into ProVaucherArticles( ProformNo,ArticleNo,VaucherN, VaucherNum,VaucherSeason,VaucherBoard,VaucherRoomType,VaucherBedType,Adult,Child,Nights,TotalNights,SinglePrice,GrandTotal,ArticleName,GuestName,CreditNo,Discount,ServiceNo,Currency,TranslateName,TranslateSeason,TranslateBed,TranslateGuestName, SeasonNo,HumanKind,AdultType,ExtraBed,BoardPlan,RoomType,ForVaucherN,RowType,TempGuestID) values( @ProformNo,@ArtNo,@VaucherNo,@VaucherNum,@Seasson,@BoardPlan,@Room,@AdditionBed,@Adult,@Child,@Nights,@TotalNights, @RealPrice,@GrTotal,@ArticleName,@GuestName,@CreditNo,@Disc,@ServiceNum,@Currency,@TranslateName,@TranslateSeason,@TranslateBed,@TranslateGuestName, @SeassonNo,@HumanKind,@AdultType,@HasExtraBed,@ValBoardPlan,@RoomType,@VaucherNo,@RowType,@TempGuestID) ; set @ArtNo=@ArtNo+1 end for; //------------------------------------------------------------------- ivaylo Depozit message '+ FinishVaucherProformBill END' type info to console; return(@ProformNo) end; //[14.03.2008] - Maria, TrueInvoiceNo i ProformNo da sa numeric i funkcia GetFakturaCounter da dava poreden nomer za ProformNo