Name('Přehled umísťování');
Designed('HRP_MAN','WinDUO');

WnsType(1)

NewWNS('ChkRegsS.WNS');

DeclareRec('SkupinovaOperace');

if(ExistFilter(SkupinovaOperace))
  begin
    DeclareActiveIndex(SkupinovaOperace,'KeyHP')
  end else
  begin
    DeclareRec('KeySkupinovaOperace5')
    KeyHP:KeySkupinovaOperace5
  end;
DeclareRec('PohybSeriovehoCisla');
DeclareRec('KeyPohybSeriovehoCisla4');
DeclareRec('SkladovySystem');
DeclareRec('KeySkladovySystem1');
DeclareRec('Sklad');
DeclareRec('KeySklad10');
DeclareRec('SerioveCislo');
DeclareRec('KeySerioveCislo1');
DeclareRec('Program');

CheckSklad:Word=Val(GetIni('Settings','ChkRegsS.WNS'))
if(CheckSklad=0)
  CheckSklad=Program.Sklad
Datum:Word=GetDate()

DefDialog('Volby',176,148);

DlgShade(-1,,4,4,168,74);
DlgStatic(-1,'&Sklad',16,20,64);
DlgEdit(,CheckSklad,80,16,48,,4,ES_LEFT,'0123456789')
DlgStatic(-1,'&Datum',16,50,64);
DlgEditDate(,Datum,80,46)
DlgButton(id_OK,,16,84);
DlgButton(id_Cancel,,96,84);

if(DlgExecute(True)<>id_OK)
  Halt;

SetIni('Settings','ChkRegsS.WNS',CheckSklad);

Record ChkRec
  dummy     :LongInt;
  CisHP     :LongInt;
  CisMat    :LongInt;
  CisSN     :LongInt;
  MnozstviSN:LongInt;
  MnozstviSS:LongInt;
  Material  :String[14];
  Nazev     :String[35];
end;

Record ChkKey
  Length    :Byte;
  CisHP     :LongInt;
  CisMat    :LongInt;
  CisSN     :LongInt;
end;

Record UsedHPKey
  Length    :Byte;
  CisHP     :LongInt
end;

Veta:LongInt;
VetaT:LongInt;
VetaS:LongInt;
Pozice:LongInt;
LocBo:Boolean
LocBo2:Boolean
LocStr:String

MakeTempFile(ChkRec,ChkKey,False)
MakeTempFile(,UsedHPKey,False)

InitStatus(UsedKeys(KeyPohybSeriovehoCisla4),'Načítám skladové pohyby')

ClearKey(KeyPohybSeriovehoCisla4)

While(NextKey(KeyPohybSeriovehoCisla4,Veta))
  begin
    ReadRec(PohybSeriovehoCisla,Veta)
    if(PohybSeriovehoCisla.CisloHP<>0)
      begin
        KeySklad10.Length=4
        KeySklad10.Cislo=SwapL(PohybSeriovehoCisla.CisloMat)
        if(FindKey(KeySklad10,VetaS))
          begin
            ReadRec(Sklad,VetaS)
          end else
          begin
            ClearRec(Sklad)
          end;
        if(Sklad.Sklad=CheckSklad)
          begin
            ClearRec(ChkRec);
            ChkRec.CisHP=PohybSeriovehoCisla.CisloHP
            ChkRec.CisMat=PohybSeriovehoCisla.CisloMat
            ChkRec.CisSN=PohybSeriovehoCisla.CisloSN
            ChkRec.MnozstviSN=PohybSeriovehoCisla.Mnozstvi
            if(PohybSeriovehoCisla.Vydaj)
              ChkRec.MnozstviSN=RoundQuantity(-ChkRec.MnozstviSN)
            ChkRec.Material=Sklad.Material
            ChkRec.Nazev=Sklad.Nazev
            ChkKey.Length=SizeOf(ChkKey)-1
            ChkKey.CisHP=KeyPohybSeriovehoCisla4.CisloHP
            ChkKey.CisSN=KeyPohybSeriovehoCisla4.CisloSN
            ChkKey.CisMat=SwapL(PohybSeriovehoCisla.CisloMat)
            AddKey(ChkKey,AddRec(ChkRec))
          end;
      end;
    Inc(Pozice)
    SetStatus(Pozice)
  end;

InitStatus(UsedKeys(KeySkladovySystem1),'Načítám umístěnky')

ClearKey(KeySkladovySystem1)

Pozice=0

While(NextKey(KeySkladovySystem1,Veta))
  begin
    ReadRec(SkladovySystem,Veta);
    if((SkladovySystem.CisHrom<>0)and(SkladovySystem.dt_Change<>-1))
      begin
        KeySklad10.Length=4
        KeySklad10.Cislo=SwapL(SkladovySystem.CisMat)
        if(FindKey(KeySklad10,VetaS))
          begin
            ReadRec(Sklad,VetaS)
          end else
          begin
            ClearRec(Sklad)
          end;
        if(Sklad.Sklad=CheckSklad)
          begin
            ChkKey.Length=SizeOf(ChkKey)-1
            ChkKey.CisHP=SwapL(SkladovySystem.CisHrom)
            ChkKey.CisSN=SwapL(SkladovySystem.CisSN)
            ChkKey.CisMat=SwapL(SkladovySystem.CisMat)
            if(FindKey(ChkKey,VetaT))
              begin
                ReadRec(ChkRec,VetaT);
                ChkRec.MnozstviSS=ChkRec.MnozstviSS+SkladovySystem.Mnozstvi;
                ModifyRec(ChkRec,VetaT)
              end else
              begin
                ClearRec(ChkRec);
                ChkRec.MnozstviSS=SkladovySystem.Mnozstvi;
                ChkRec.CisHP=SkladovySystem.CisHrom
                ChkRec.CisMat=SkladovySystem.CisSN
                ChkRec.CisSN=SkladovySystem.CisMat
                ChkRec.Material=Sklad.Material
                ChkRec.Nazev=Sklad.Nazev
                AddKey(ChkKey,AddRec(ChkRec))
              end;
            UsedHPKey.Length=SizeOf(UsedHPKey)-1
            UsedHPKey.CisHP=SwapL(SkladovySystem.CisHrom)
            if(not FindKey(UsedHPKey,VetaT))
              begin
                AddKey(UsedHPKey,Veta);
              end;
          end;
      end;
    Inc(Pozice)
    SetStatus(Pozice)
  end;

InitStatus(UsedKeys(KeyHP),'Vytvářím sestavu')

SetDescr(0,0,TA_Right|descr_PageNum,'Strana: ');
SetDescr(1,0,TA_Center,'Kontrola umísťování');
SetDescr(2,0,TA_Left|descr_Date,'Datum: ');
SetDescr(3,1,TA_Center,'ve dni '+StrDate(Datum));

SetColumn(0,TA_LEFT,2);                 Typ
SetColumn(1,TA_LEFT,10);                Doklad
SetColumn(2,TA_RIGHT,10);               Datum
SetColumn(3,TA_LEFT,14);                Material
SetColumn(4,TA_LEFT,35);                Nazev
SetColumn(5,TA_LEFT,22);                Seriove cislo
SetColumn(6,TA_RIGHT,12);               Pohyb na sklade
SetColumn(7,TA_RIGHT,12);               Umisteno
SetColumn(8,TA_LEFT,7);                 MJ
SetColumn(9,TA_CENTER,3);               Chyba

SetHead('\tDoklad\tDatum\tPoložka\tNázev\tSériové číslo\tMnožství\tUmístěno\tMJ\t')

ClearKey(KeyHP)

Pozice=0

While(NextKey(KeyHP,Veta))
  begin
    ReadRec(SkupinovaOperace,Veta);
    if(((Datum=SkupinovaOperace.Datum)or(Datum=0))and(InFilter(SkupinovaOperace)))
      begin
        UsedHPKey.Length=SizeOf(UsedHPKey)-1
        UsedHPKey.CisHP=SwapL(SkupinovaOperace.Cislo);
        if(FindKey(UsedHPKey,VetaT))
          begin
            LocBo2=False
            ChkKey.Length=4;
            ChkKey.CisHP=SwapL(SkupinovaOperace.Cislo)
            LocBo=SearchKey(ChkKey,VetaT);
            While((LocBo)and(ChkKey.CisHP=SwapL(SkupinovaOperace.Cislo)))
              begin
                ReadRec(ChkRec,VetaT);
                if(SkupinovaOperace.Vydaj)
                  begin
                    if(SkupinovaOperace.TypDok=4)
                      LocStr='X'
                    else
                      LocStr='V'
                  end else
                  begin
                    LocStr='P'
                  end
                if(LocBo2)
                  begin
                    AddText(,,,,)
                  end else
                  begin
                    AddText(LocStr,SkupinovaOperace.Doklad,StrDate(SkupinovaOperace.Datum),)
                    LocBo2=True
                  end;
                AddText(ChkRec.Material,ChkRec.Nazev,)
                KeySerioveCislo1.Length=4
                KeySerioveCislo1.Cislo=SwapL(ChkRec.CisSN)
                if(FindKey(KeySerioveCislo1,VetaS))
                  begin
                    ReadRec(SerioveCislo,VetaS);
                  end else
                  begin
                    ClearRec(SerioveCislo)
                  end;
                AddText(SerioveCislo.Number,StrQuantity(ChkRec.MnozstviSN),StrQuantity(ChkRec.MnozstviSS),Sklad.MJ,);
                if(ChkRec.MnozstviSN<>ChkRec.MnozstviSS)
                  begin
                    AddText('!!!');
                  end;
                AddLine();
                LocBo=NextKey(ChkKey,VetaT);
              end;
          end;
      end;
    Inc(Pozice);
    SetStatus(Pozice);
  end;

END