// âîññòàíîâëåíèå ñîñòàâà ÷åêîâ ïîñëå èõ ïîòåðè èç ðåçåðâíîãî æóðíàëà

var Form :TForm;
    Lab,Lab2 :TLabel;
    Lab1 :TLabel;
    EditDate :TDateTimePicker;
    EditDate1 :TdateTimePicker;
    Bevel,Bevel1 :Tbevel;
    Button1 :Tbutton;
    Button2 :tButton;
    ButtonAg :tButton;
    Edit :TEdit;

    TaN1 :TTable;
    TaT :ttable;
    tat1 :ttable;
    taTovar :ttable;
    taprt :ttable;

    i :integer;

    Directory :string;
    FileList :tstringlist;
    dt :tdatetime;
    fname :string;

    List :tstringlist;

    cds :tclientdataset;


    MissN,MissT,MissNN,RecC,RecAdd,recadd1 :integer;

    ID :integer;
    id1 :integer;
    lastid :integer;
    out_rows_found :boolean;

    lastid1 :integer;


function MaxVAlue (VAl1,Val2 :integer) :integer;
begin
  if VAl1 >= Val2 then result := VAl1
  else Result := Val2;
end;


begin

  cds := tclientdataset.create(selfscript);
  tan1 := ttable.create(selfscript);
  tat := ttable.create(selfscript);
  tat1 := ttable.create(selfscript);
  tatovar := ttable.create(selfscript);
  taprt :=ttable.create(selfscript);

  filelist := tstringlist.create;
  Form := Tform.create(selfscript);
  try
    begin // ñîçäàåì ôîðìó

      Form.Caption := 'Âîññòàíîâëåíèå äàííûõ';
      Form.BorderStyle := bsDialog;
      Form.Width := 300;
      Form.Height := 200;
      Form.position := poScreenCenter;
      Form.Font.name := 'Tahoma';
      Form.ShowHint := true;


      Bevel := tbevel.create(form);
      Bevel.Shape := bsTopLine;
      Bevel.Height := 4;
      Bevel.align := alTop;
      bevel.parent := Form;


      Lab := TLabel.create(form); // óäàëÿòü íå íóæíî, Form åãî owner, ñàì óäàëèò
      Lab.caption := 'Óêàæèòå íà÷àëüíóþ äàòó:';
      //Lab.font.size := 10;
      Lab.Top := 30;
      Lab.Left := 30; //(Form.clientWidth - Lab.Width) div 2;
      Lab.parent := Form;


      Lab1 := TLabel.create(form); // óäàëÿòü íå íóæíî, Form åãî owner, ñàì óäàëèò
      Lab1.caption := 'Óêàæèòå êîíå÷íóþ äàòó:';
      //Lab.font.size := 10;
      Lab1.Top := Lab.Top + Lab.height + 20;
      Lab1.Left := 30; //(Form.clientWidth - Lab.Width) div 2;
      Lab1.parent := Form;



      EditDate := TDateTimePicker.create(Form);
      EditDate.Top := Lab.Top - (EditDAte.height - Lab.Height) div 2  ; //+Lab.Height + 10;
      EditDate.width := 100;
      EditDate.Left := Lab.LEft + maxvalue(Lab.Width,lab1.width) + 20; // (Form.clientWidth - Editdate.width) div 2;
      Editdate.parent := Form;

      Form.clientWidth := EditDate.Left + EditDate.width + 30;


      EditDate1 := TDateTimePicker.create(Form);
      EditDate1.Top := Lab1.Top - (EditDAte1.height - Lab1.Height) div 2  ; //+Lab.Height + 10;
      EditDate1.width := 100;
      EditDate1.Left := EditDate.LEft; // (Form.clientWidth - Editdate.width) div 2;
      Editdate1.parent := Form;
      Editdate1.date := date;


      Bevel1 := TBevel.create(Form);
      Bevel1.shape := bsTopLine;
      Bevel1.Height := 4;
      Bevel1.Width := Form.clientWidth - 30;
      Bevel1.Left := (Form.ClientWidth - Bevel1.Width) div 2;
      Bevel1.Top := editdate1.Top + editdate1.Height + 20;
      Bevel1.parent := Form;


      Button1 := Tbutton.create(Form);
      Button1.Caption := 'Ñòàðò';
      Button1.Width := 70;
      Button1.ModalResult := mrOk;
      Button1.Top := Bevel1.Top + 13;
      button1.Default := true;
      Button1.Parent := Form;



      Button2 := Tbutton.create(Form);
      Button2.Caption := 'Îòìåíà';
      Button2.Width := 70;
      Button2.ModalResult := mrCancel;
      Button2.Top := Button1.top;
      Button2.left := Form.ClientWidth - Button2.Width - 35;
      Button2.Cancel := true;
      Button2.Parent := Form;


      Button1.left := Button2.left - Button1.width - 5;


      Form.clientHeight := Button1.Top + Button1.height + 15;

    end;

    EditDate.date := date;

    if Form.ShowModal = mrOk
    then begin
      Directory := includetrailingpathdelimiter(getapplocaldatafolder) + 'journal1\';

     //GetFileList(Directory,FileLIst,'*.txt',false,false);
     //filelist.savetofile('c:\temp\1.txt');

      MissN := 0;
      MissT := 0;
      MissNN := 0;
      RecC := 0;
      RecAdd := 0;
      recadd1 := 0;


      tan1.databasename := 'dbkassa';
      tan1.tablename := 'rs_nakl';
      tan1.open;

      tat.databasename := 'dbkassa';
      tat.tablename := 'rs_tovar';
      tat.open;

      tat1.databasename := 'dbkassa';
      tat1.tablename := 'rs_tovar';
      tat1.open;


      taprt.databasename := 'dbkassa';
      taprt.tablename := 'pr_tovar';
      taprt.open;

      tatovar.databasename := 'dbkassa';
      tatovar.tablename := 'tovar';
      tatovar.open;

      with tmywait.create1('Èäåò âîññòàíîâëåíèå äàííûõ',selfscript) do
      begin
        progressbarmax := round(editdate1.date-editdate.date)+1;
        progressbarpos := 0;

        tat.indexfieldnames := 'id_nakl';


        tan1.last;
        lastid1 := tan1.fieldbyname('id').asinteger;

        tat1.last;
        lastid := tat1.fieldbyname('id').asinteger;


        for dt := editdate.date to editdate1.date do
        begin

          fname := directory + formatdatetime('yyyy_mm_dd',dt)+'.txt';
          if fileexists(fname)
          then begin
             advancedtext := datetostr(dt);

             ReadTXT(cds,fname);


             out_rows_found := cds.findfield('id_out_rows')<>nil;


             if out_rows_found
             then taprt.indexfieldnames := ''
             else taprt.indexfieldnames := 'kart';




             cds.first;
             while not cds.eof do
             begin
               inc(recc);

               id := strtointprotected(cds.fieldbyname('id_invoice').asstring);

               tan1.append;
               inc(lastid1);
               tan1.fieldbyname('id').asinteger := lastid1;
               tan1.fieldbyname('data').asdatetime := dt;
               tan1.fieldbyname('nomer').asinteger := strtointprotected(cds.fieldbyname('num_doc').asstring);
               tan1.fieldbyname('sklad').asinteger := strtointprotected(cds.fieldbyname('id_storage').asstring);
               tan1.fieldbyname('osnov').asstring := '*';
               tan1.post;

               //if tan1.findkey([ID])
               //then
               begin
                 //tat.setrangestart;
                 //tat.fieldbyname('id_nakl').asinteger := id;

                 //tat.setrangeend;
                 //tat.fieldbyname('id_nakl').asinteger := id;
                 //tat.applyrange;

                 //if tat.recordcount=0
                 //then
                 try
                   //id1 := strtointprotected(cds.fieldbyname('id').asstring);
                   //if (id1<>0) or not tat.locate('ID',id1,0)
                   //then
                   begin


                     //if not tat1.findkey([id1]) // åñëè òàêîãî ID1 âîîáùå íåò â áàçå, òî åãî è âñòàâèì, èíà÷å íàéäåííûé ïåðåäâèíåì, à  ýòîò ñíîâà âñòàâèì
                     //then begin
                     //  inc(recadd);
                     //end
                     //else
                     begin
                       //inc(recadd1);
                       //inc(lastid);
                       //id1 := lastid;
                       //tat1.edit;
                       //tat1.fieldbyname('id').asinteger := lastid;
                       //tat1.post;
                     end;


                     inc(lastid);

                     tat1.append;
                     tat1.fieldbyname('id').asinteger := lastid;

                     tat1.fieldbyname('id_nakl').asinteger := lastid1;
                     tat1.fieldbyname('nn').asstring  := cds.fieldbyname('code').asstring;
                     tat1.fieldbyname('kol').asfloat := strtofloatprotected(cds.fieldbyname('amount').asstring);
                     tat1.fieldbyname('cena').asfloat := strtofloatprotected(cds.fieldbyname('price').asstring);

                     if out_rows_found
                     then begin
                       id1 := strtointprotected(cds.fieldbyname('id_in_rows').asstring);
                       if taprt.findkey([id1])
                          and (taprt.fieldbyname('nn').asstring = cds.fieldbyname('code').asstring)
                          and (taprt.fieldbyname('sklad').asinteger = strtointprotected(cds.fieldbyname('id_storage').asstring))
                       then begin
                         tat1.fieldbyname('id_pr').asinteger := id1;
                         tat1.fieldbyname('nds').asinteger := taprt.fieldbyname('ndsout').asinteger;
                       end;
                     end

                     else begin

                       id1 := strtointprotected(cds.fieldbyname('card').asstring);
                       if taprt.findkey([id1])
                       then begin
                          while not taprt.eof
                               and (taprt.fieldbyname('kart').asinteger = id1)
                               and (taprt.fieldbyname('nn').asstring = cds.fieldbyname('code').asstring)
                               and (taprt.fieldbyname('sklad').asinteger <>cds.fieldbyname('id_storage').asinteger)
                          do
                            taprt.next;
                       end;

                       if not taprt.eof and (taprt.fieldbyname('kart').asinteger = id1)
                       then begin
                         tat1.fieldbyname('id_pr').asinteger := taprt.fieldbyname('id').asinteger;
                         tat1.fieldbyname('nds').asinteger := taprt.fieldbyname('ndsout').asinteger;
                       end;
                     end;

                     tat1.post;


                   end;
                   //else inc(misst);

                 finally
                   //tat.cancelrange;
                 end;
               end;
               //else inc(MissN);

               cds.next;
               application.processmessages;
             end;

          end;
          //else createhinti(fname);

          incprogress;
        end;
      end;

      CreateHinti('Îáðàáîòàíî: '+inttostr(recc)+', Äîáàâëåíî :' +inttostr(recc)
                 // ' (èç íèõ ïåðåñåêëèñü:'+inttostr(recadd1)+')'+
                 // ', Ïðîïóùåííî íàéäåííûõ íàêëàäíûõ: '+inttostr(missn)
                  {', Ïðîïóùåíî ñòðîê (íàéäåíû â áàçå): '+inttostr(misst)}
                  ,'Âñòàâêà äàííûõ',0);
    end;
  finally
    Form.free;
    filelist.free;

  end;


end.
