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

GO

create procedure DBA.FillOweRentForMonth()
--Spravka Daljim i platen naem
begin
  set bvrSpravkaM=bvrSpravkaM1;
  set bvrSpravkaY=bvrSpravkaY1;
  delete from #OwePaidRentForMonth;
  delete from #OwePaidRentMonth;
  while((bvrSpravkaY < bvrSpravkaY2) or((bvrSpravkaY = bvrSpravkaY2) and(bvrSpravkaM <= bvrSpravkaM2))) loop
    call FillOwePaidRentMonth("date"('1.' || bvrSpravkaM || '.' || bvrSpravkaY),0);
    if(bvrSpravkaM >= 12) then
      set bvrSpravkaM=1;
      set bvrSpravkaY=bvrSpravkaY+1
    else
      set bvrSpravkaM=bvrSpravkaM+1
    end if
  end loop;
  insert into #OwePaidRentForMonth(
    select GuestNo,NameMonth,DaysInMonth,Year,Name,UniName,Kurs,Spec,FN,Address,City,
      Sex,PriceKind,Usluga,Cena,Kol,SumOwe,KolPaid,SumPaid,Razlika,
      RealDatePaid,Koef,1,RoomName,Country,PriceKindType,PayType,PayTypeStr
      from #OwePaidRentMonth)
end
--[07.04.2009] - Nixon - promeneno DaysInMonth - broj dni v meseca prez koito studenta e bil v obshtejitieto
--[v. 29.07.2009] - Yoan - promeneno da vzema ot vremenna tablica #OwePaidRentMonth
--[23.10.2009] - Milko - dovabqne nova kolona PriceKindType
