create or alter procedure P_AK_SKLADNIK (
    PNR_AKT integer,
    PSKLADNIK varchar(20),
    PDATA_WYPLATY date,
    POST_DATA_MCA date)
returns (
    PCURR numeric(15,2),
    PINTEGER integer,
    PFLOAT numeric(15,4),
    PBOOL smallint,
    PSTRING varchar(30),
    PDATA date,
    PWARTOSC varchar(30),
    OKRES integer,
    TYP_DANE integer,
    ODKIEDY date)
as
declare variable ROK integer;
declare variable MC integer;
declare variable DZIEN integer;
declare variable WARTOSC numeric(15,4);
declare variable WARTOSC_SUM numeric(15,4);
declare variable OST_DZIEN integer;
declare variable WSPOL numeric(15,4);
begin

  OKRES=-1;

  SELECT TYP_DANE, OKRES FROM VKIP_STRUSKLADNIKI WHERE SKLADNIK=:PSKLADNIK INTO TYP_DANE, OKRES;

  PCURR = 0.00;
  PINTEGER =0;
  PFLOAT = 0.0000;
  PBOOL=0;
  PSTRING='';
  PDATA=NULL;
  PWARTOSC='';

  IF (OKRES=0) THEN
  BEGIN
   SELECT PCURR, PINTEGER, PFLOAT, PBOOL, PSTRING, PDATA, PWARTOSC, ODKIEDY
   FROM VKIP_SKLADNIKI
   WHERE NR_AKT=:PNR_AKT AND SKLADNIK=:PSKLADNIK AND ODKIEDY<=:PDATA_WYPLATY
   ORDER BY ODKIEDY DESC ROWS 1
   INTO PCURR, PINTEGER, PFLOAT, PBOOL, PSTRING, PDATA, PWARTOSC, ODKIEDY;
  END

  IF ((OKRES=1) AND (TYP_DANE>2)) THEN
  BEGIN
    SELECT PCURR, PINTEGER, PFLOAT, PBOOL, PSTRING, PDATA, PWARTOSC, ODKIEDY
    FROM VKIP_SKLADNIKI
    WHERE NR_AKT=:PNR_AKT AND SKLADNIK=:PSKLADNIK AND ODKIEDY<=:POST_DATA_MCA
    ORDER BY ODKIEDY DESC ROWS 1
    INTO PCURR, PINTEGER, PFLOAT, PBOOL, PSTRING, PDATA, PWARTOSC, ODKIEDY;
  END

  WARTOSC=0.0000;
  WARTOSC_SUM=0.0000;
  OST_DZIEN=EXTRACT(DAY FROM :POST_DATA_MCA);
  ROK=EXTRACT(YEAR FROM :POST_DATA_MCA);
  MC=EXTRACT(MONTH FROM :POST_DATA_MCA);

  IF ((OKRES=1) AND (TYP_DANE<3)) THEN
  BEGIN
    FOR SELECT PCURR, PINTEGER, PFLOAT, PBOOL, PSTRING, PDATA, PWARTOSC, ODKIEDY
    FROM VKIP_SKLADNIKI
    WHERE NR_AKT=:PNR_AKT AND SKLADNIK=:PSKLADNIK AND ODKIEDY<=:POST_DATA_MCA
    ORDER BY ODKIEDY DESC ROWS 31
    INTO PCURR, PINTEGER, PFLOAT, PBOOL, PSTRING, PDATA, PWARTOSC, ODKIEDY
    DO BEGIN
      if ((ROK=EXTRACT(YEAR FROM ODKIEDY)) AND (MC=EXTRACT(MONTH FROM ODKIEDY)))
      then DZIEN = EXTRACT(DAY FROM ODKIEDY); else DZIEN = 1;

      if (TYP_DANE=0) then WARTOSC=PCURR;
      if (TYP_DANE=1) then WARTOSC=PINTEGER;
      if (TYP_DANE=2) then WARTOSC=PFLOAT;

      if ((DZIEN=1) AND (OST_DZIEN=EXTRACT(DAY FROM :POST_DATA_MCA))) THEN
      BEGIN
        WARTOSC_SUM=WARTOSC;
      END ELSE
      if ((DZIEN<>OST_DZIEN)AND(OST_DZIEN>0)) then
      BEGIN
        SELECT C_WSPOL FROM P_AK_GODZNOMOKRES(:PNR_AKT,:MC, :ROK, :DZIEN, :OST_DZIEN) INTO WSPOL;
        WARTOSC_SUM = WARTOSC_SUM + WARTOSC*WSPOL;
      END

      OST_DZIEN = DZIEN-1;
    END
    if (TYP_DANE=0) then PCURR=ROUND(WARTOSC_SUM*100.00)/100.00;
    if (TYP_DANE=1) then PINTEGER=ROUND(WARTOSC_SUM);
    if (TYP_DANE=2) then PFLOAT=ROUND(WARTOSC_SUM*10000.0000)/10000.0000;

    if (TYP_DANE=0) then PWARTOSC=CAST(PCURR AS VARCHAR(30));
    if (TYP_DANE=1) then PWARTOSC=CAST(PINTEGER AS VARCHAR(30));
    if (TYP_DANE=2) then PWARTOSC=CAST(PFLOAT AS VARCHAR(30));
  END
  suspend;
end