Name('Import skupinových operací')
Designed('HRP_MAN')
Author('Winduo Team','EMail: winduo@winduo.cz','http://www.winduo.com','http://www.winduo.cz')
Descr('Exportuje skupinové operace do formátu XML')

WNSType(-1);

;****************************************************************************

DeclareRec('SkupinovaOperace');
DeclareRec('KeySkupinovaOperace4');
DeclareRec('Pohyb');
DeclareRec('KeyPohyb5');
DeclareRec('SerioveCislo');
DeclareRec('KeySerioveCislo3');
DeclareRec('PohybSeriovehoCisla');
DeclareRec('KeyPohybSeriovehoCisla3');
DeclareRec('Sklad');
Sklad2:Sklad
DeclareRec('KeySklad2');

ArrayFromStruct('HPStruct',SkupinovaOperace);
ArrayFromStruct('PHStruct',Pohyb);
ArrayFromStruct('SNMStruct',PohybSeriovehoCisla);

NoveDatum:Word=GetMinDate();
NoveObdobi:Word=Year(NoveDatum)%100+Month(NoveDatum)*100;
Veta:LongInt
VetaP:LongInt
VetaS:LongInt
VetaSN:LongInt
VetaSNM:LongInt
XML:TagFile;
LocW:Word
LocW2:Word
OK:Boolean
OK2:Boolean
OK3:Boolean
VarName:String
Zpracovano:LongInt
HPCount:LongInt
PHCount:LongInt
SNCount:LongInt

Open(XML,'HROMPOH.XML');

SetStatusText('Kontroluji záznamy ...');

OK=FindBranch(XML,'?xml','SkupinoveOperace','SkupinovaOperace')

While(OK)
  begin
    Inc(HPCount);
    LocW=GetFirstIndex(HPStruct);
    LocW2=GetLastIndex(HPStruct);
    While(LocW<=LocW2)
      begin
        VarName='SkupinovaOperace.'+HPStruct[LocW]
        @VarName=GetBranchAttr(XML,HPStruct[LocW],true);
        Inc(LocW);
      end
    if(FindBranch(XML,'Pohyby'))
      begin
        OK2=FindBranch(XML,'Pohyb');
        if(OK2)
          begin
            While(OK2)
              begin
                ClearRec(Sklad);
                Sklad.Sklad=GetBranchAttr(XML,'Sklad',true);
                Sklad.Material=GetBranchAttr(XML,'Material',true);
                BuildKey(KeySklad2,Sklad)
                if(FindKey(KeySklad2,VetaS))
                  begin
                    ReadRec(Sklad,VetaS);
                  end else
                  begin
                    Halt('Nenalezena položka číslo '+Sklad.Material+' na skladě '+Str(Sklad.Sklad));
                  end;

                Inc(PHCount);
                LocW=GetFirstIndex(PHStruct);
                LocW2=GetLastIndex(PHStruct);
                While(LocW<=LocW2)
                  begin
                    VarName='Pohyb.'+PHStruct[LocW]
                    @VarName=GetBranchAttr(XML,PHStruct[LocW],true);
                    Inc(LocW);
                  end
                OK3=FindBranch(XML,'SerioveCislo');
                if(OK3)
                  begin
                    While(OK3)
                      begin
                        ClearRec(SerioveCislo)
                        SerioveCislo.Number=GetBranchAttr(XML,'cislo',true);
                        SerioveCislo.CisloMat=Sklad.cislo;
                        BuildKey(KeySerioveCislo3,SerioveCislo);
                        if(FindKey(KeySerioveCislo3,VetaSN))
                          begin
                            ReadRec(SerioveCislo,VetaSN)
                          end else
                          begin
                            Halt('Nenalezeno sériové číslo '+SerioveCislo.Number+' na položce číslo '+Sklad.Material+' na skladě '+Str(Sklad.Sklad));
                          end;
                        Inc(SNCount);
                        LocW=GetFirstIndex(SNMStruct);
                        LocW2=GetLastIndex(SNMStruct);
                        While(LocW<=LocW2)
                          begin
                            VarName='PohybSeriovehoCisla.'+SNMStruct[LocW]
                            @VarName=GetBranchAttr(XML,SNMStruct[LocW],true);
                            Inc(LocW);
                          end
                        if(PohybSeriovehoCisla.CisloVyr<>0)
                          begin
                            ClearRec(Sklad2);
                            Sklad2.Material=GetBranchAttr(XML,'Vyrobek',true);
                            Sklad2.Sklad=GetBranchAttr(XML,'VyrobekSklad',true);
                            BuildKey(KeySklad2,Sklad2)
                            if(FindKey(KeySklad2,VetaS))
                              begin
                                ReadRec(Sklad2,VetaS);
                              end else
                              begin
                                Halt('Nenalezena položka výrobku číslo '+Sklad2.Material+' na skladě '+Str(Sklad2.Sklad));
                              end;
                            if(Sklad2.TypKarty<>2)
                              begin
                                Halt('Položka číslo '+Sklad2.Material+' na skladě '+Str(Sklad2.Sklad)+' není výrobek.');
                              end;
                            PohybSeriovehoCisla.CisloVyr=Sklad2.Cislo
                          end;
                        OK3=NextBranch(XML,'SerioveCislo');
                      end;
                    BranchUp(XML);
                  end;
                OK2=NextBranch(XML,'Pohyb');
              end;
            BranchUp(XML);
          end;
        BranchUp(XML);
      end
    OK=NextBranch(XML,'SkupinovaOperace')
  end;

Counter:LongInt;

ModifyMode(true);

InitStatus(HPCount+PHCount+SNCount,'Importuji záznamy ...');

Open(XML,'HROMPOH.XML');

OK=FindBranch(XML,'?xml','SkupinoveOperace','SkupinovaOperace')

While(OK)
  begin
    Inc(Counter);
    LocW=GetFirstIndex(HPStruct);
    LocW2=GetLastIndex(HPStruct);
    While(LocW<=LocW2)
      begin
        VarName='SkupinovaOperace.'+HPStruct[LocW]
        @VarName=GetBranchAttr(XML,HPStruct[LocW],true);
        Inc(LocW);
      end

    SkupinovaOperace.Cislo=GetNewIdent();
    SkupinovaOperace.cisdok=0;
    SkupinovaOperace.typdok=0;
    SkupinovaOperace.datum=NoveDatum;
    SkupinovaOperace.datum_zp=NoveDatum;
    SkupinovaOperace.obdobi=NoveObdobi;

    AddRec(SkupinovaOperace);

    if(FindBranch(XML,'Pohyby'))
      begin
        OK2=FindBranch(XML,'Pohyb');
        if(OK2)
          begin
            While(OK2)
              begin
                ClearRec(Sklad);
                Sklad.Sklad=GetBranchAttr(XML,'Sklad',true);
                Sklad.Material=GetBranchAttr(XML,'Material',true);
                BuildKey(KeySklad2,Sklad)
                if(FindKey(KeySklad2,VetaS))
                  begin
                    ReadRec(Sklad,VetaS);
                  end else
                  begin
                    Halt('Nenalezena položka číslo '+Sklad.Material+' na skladě '+Str(Sklad.Sklad));
                  end;
                Inc(Counter);
                LocW=GetFirstIndex(PHStruct);
                LocW2=GetLastIndex(PHStruct);
                While(LocW<=LocW2)
                  begin
                    VarName='Pohyb.'+PHStruct[LocW]
                    @VarName=GetBranchAttr(XML,PHStruct[LocW],true);
                    Inc(LocW);
                  end

                Pohyb.Cislo=GetNewIdent()
                Pohyb.datum=SkupinovaOperace.datum;
                Pohyb.datum_zp=SkupinovaOperace.datum_zp;
                Pohyb.obdobi=SkupinovaOperace.obdobi;
                Pohyb.CisloMat=Sklad.Cislo
                Pohyb.Hrom=SkupinovaOperace.cislo
                Pohyb.CisloDok=0
                Pohyb.SubDok=0

                AddRec(Pohyb)

                OK3=FindBranch(XML,'SerioveCislo');
                if(OK3)
                  begin
                    While(OK3)
                      begin
                        ClearRec(SerioveCislo)
                        SerioveCislo.Number=GetBranchAttr(XML,'cislo',true);
                        SerioveCislo.CisloMat=Sklad.cislo;
                        BuildKey(KeySerioveCislo3,SerioveCislo);
                        if(FindKey(KeySerioveCislo3,VetaSN))
                          begin
                            ReadRec(SerioveCislo,VetaSN)
                          end else
                          begin
                            Halt('Nenalezeno sériové číslo '+SerioveCislo.Number+' na položce číslo '+Sklad.Material+' na skladě '+Str(Sklad.Sklad));
                          end;
                        Inc(Counter);
                        LocW=GetFirstIndex(SNMStruct);
                        LocW2=GetLastIndex(SNMStruct);
                        While(LocW<=LocW2)
                          begin
                            VarName='PohybSeriovehoCisla.'+SNMStruct[LocW]
                            @VarName=GetBranchAttr(XML,SNMStruct[LocW],true);
                            Inc(LocW);
                          end

                        PohybSeriovehoCisla.CisloPH=Pohyb.cislo
                        PohybSeriovehoCisla.CisloSN=SerioveCislo.cislo
                        PohybSeriovehoCisla.CisloMat=Sklad.cislo
                        PohybSeriovehoCisla.CisloHP=SkupinovaOperace.cislo

                        if(PohybSeriovehoCisla.CisloVyr<>0)
                          begin
                            ClearRec(Sklad2);
                            Sklad2.Material=GetBranchAttr(XML,'Vyrobek',true);
                            Sklad2.Sklad=GetBranchAttr(XML,'VyrobekSklad',true);
                            BuildKey(KeySklad2,Sklad2)
                            if(FindKey(KeySklad2,VetaS))
                              begin
                                ReadRec(Sklad2,VetaS);
                              end else
                              begin
                                Halt('Nenalezena položka výrobku číslo '+Sklad2.Material+' na skladě '+Str(Sklad2.Sklad));
                              end;
                            if(Sklad2.TypKarty<>2)
                              begin
                                Halt('Položka číslo '+Sklad2.Material+' na skladě '+Str(Sklad2.Sklad)+' není výrobek.');
                              end;
                            PohybSeriovehoCisla.CisloVyr=Sklad2.Cislo
                          end;

                        AddRec(PohybSeriovehoCisla);

                        OK3=NextBranch(XML,'SerioveCislo');
                      end;
                    BranchUp(XML);
                  end;
                OK2=NextBranch(XML,'Pohyb');
              end;
            BranchUp(XML);
          end;
        BranchUp(XML);
      end
    OK=NextBranch(XML,'SkupinovaOperace')
    SetStatus(Counter);
  end;

  Message('Nalezeno\rSkupinových operací: '+Str(HPCount)+'\rpohybů: '+Str(PHCount)+'\rSériových čísel: '+Str(SNCount));
END
