// ñêðèïò ãåíåðèðóåò àòðèáóòû êàðòî÷åê â ïðèõîäíûõ íàêëàäíûõ óêàçàííîãî äèàïàçîíà è óïàêîâûâàåò â arcsklad.zip
// ïåðåäàþòñÿ àòðèáóòû: øòðèõêîäû, ñðîêè ãîäíîñòè

const ExportDir = 'c:\mail\out\';

const FName = 'cardattr.db';

procedure CopyTable1(Fromtable :string ; ToDir :string);
var table :ttable;
    table1 :ttable;
begin
  table := ttable.create(nil);
  try
    table1 := ttable.create(table);

    table.databasename := 'dbkassa';

    table.close;
    table.tablename := FromTable;
    table.open;

    //table1.close;
    //table1.tablename := toDir + table.tablename;
    //table1.batchmove(table,BATCOPY);
    BDEBatchMoveToTMP(table, toDir + table.tablename);

  finally
    table.free
  end;
end;


function GenCardsAttribPackage(ToDir :string; pMinData :tdatetime; pMaxData :TDatetime) :string;
var Query :TQuery;
begin
  todir := includetrailingpathdelimiter(ToDir);
  result := ToDir + 'arcsklad.zip';
  forcedirectories(ToDir);
  ClearDir(ToDir);

  Query :=TQuery.create(nil);
  try
    Query.databasename := 'dbkassa';
    Query.sql.text := 'select b.id,b.id_extern,b.nn,b.kart,a.data,a.nomer,a.sklad'+
                      ' ,b.barcode,b.srok'+
                      ' from pr_nakl a join pr_tovar b on a.id = b.id_nakl '+
                      ' where b.id_extern<>0 and b.id_extern is not null '+
                      ' and  a.data >= :p_mindata and a.data <= :p_maxdata'+
                      ' and cast(b.kart as varchar(30))<>barcode'; // òîëüêî çàâîäñêèå íàñ èíòåðåñóþò.

    Query.parambyname('p_mindata').asdatetime := pmindata;
    Query.parambyname('p_maxdata').asdatetime := pmaxdata;
    Query.open;

    BDEBatchMoveToTMP(Query, ToDir+fname);
    ZipAdd(result,fname,ToDir );
    deletefile(todir+fname);

  finally
    Query.free;
  end;

  //ZipAddDir(result,ToDir, ToDir);
end;


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

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

begin


  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;


      (*
      Lab2 := TLabel.create(form); // óäàëÿòü íå íóæíî, Form åãî owner, ñàì óäàëèò
      Lab2.caption := 'Ñêëàä:';
      Lab2.Top := Lab1.Top + Lab1.Height + 20;
      Lab2.Left := 20; //(Form.clientWidth - Lab.Width) div 2;
      Lab2.parent := Form;

      Edit := TEdit.create(Form);
      Edit.name := consteditstoragename;

      if idstorage<>0
      then Edit.Text := asstring(BDEQueryValue('select name from agents where id = :id',[idstorage]))
      else Edit.text := '';


      Edit.left := Lab2.Left + Lab2.Width + 10;
      Edit.Top := Lab2.Top - (Edit.height - Lab2.Height) div 2  ; //+Lab.Height + 10;
      Edit.Width := EditDate.Left + EditDAte.Width - Edit.Left;
      Edit.Enabled := false;
      Edit.Parent := Form;

      ButtonAg := TButton.create(Form);
      ButtonAg.Caption := 'Âûáðàòü';
      ButtonAg.top := Edit.Top + Edit.Height +8 ;
      //ButtonAg.height := Edit.height;
      ButtonAg.Width := 60;
      ButtonAg.Hint := 'Âûáðàòü èç ñïèñêà';
      ButtonAg.Left := EditDate.Left + EditDAte.Width - ButtonAg.Width;
      //ButtonAg.Width := EditDate.Left + EditDAte.Width - ButtonAg.Left;
      ButtonAg.onClick := 'Button1Click';

      ButtonAg.Parent := Form;

      ButtonAg.enabled := idstorage = 0;

      *)

      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;

    while Form.ShowModal = mrOk
    do begin
        GenCardsAttribPackage(includetrailingpathdelimiter(Exportdir),
                           Editdate.date,
                           EditDate1.date);

        CreateHIntI('Ýêñïîðò çàâåðøåí');
        break;
    end;
  finally
    form.free;
  end;
end.
