// ýêñïîðò solaris-solaris èç îôèñà ñ âûáîðîì àïòå÷íîãî ïóíêòà èç äèàëîãà
// STDInOutCreateFile(Storage :integer; const DirName :string; FromDate :TDateTime; ToDate :TDateTime)

const OutputDir = '\\valentina-ivano\mail_xprice\out\ÀÏ3\';
//      Storage  = 0; // Ñêëàä, èíôîðìàöèÿ ïî êîòîðîìó ïåðåäàåòñÿ. Åñëè 0, òî íå ôèëüòðóåòñÿ

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

    IDStorage :integer;

    OutReasonsList :string;
    Table :TTable;

    Storages :array of integer;

    i :integer;

const
    consteditstoragename = 'storagename';

procedure Button1Click(Sender: TButton);
var NM :string;
    i :integer;
begin
  IDStorage := SElectOrg(atStorage);
  if IDStorage <> 0 then begin
    if vartostr(GetOrgParamValue(IDStorage,'CODE')) = ''
    then begin
      idStorage := 0;
      bvMessageError('Äàííûé êîíòðàãåíò íå àññîöèèðîâàí ñ ïî÷òîâûì êàòàëîãîì,'+#13+
       ' íåîáõîäèìî äàííîìó êîíòðàãåíòó â ïîëå "àëüòåðíàòèâíûé êîä"'+#13+
       'âïèñàòü èìÿ ïî÷òîâîãî ïîäêàòàëîãà');
    end
    else begin
       nm := vartostr(GetOrgParamVAlue(idStorage,'NAME'));
       { íå ðàáîòàåò
       for i := 0 to Form.ComponentCount-1 do begin
         if FOrm.Components[i].name = consteditstoragename
         then begin
           TEdit(form.COmponents[i]).text := nm;
         end;
       end;
       }
       Edit.text := nm;
    end;

  end;

end;


begin
  IDStorage := 660;

  setlength(storages,0);

  OutReasonsList := '';

  table := ttable.create(nil);
  try
    table.databasename := 'dbkassa';
    table.tablename := 'osnov';
    table.filter := 'ISCASH <> 1';
    Table.filtered := true;
    table.open;

    table.first;
    while not table.eof do
    begin
      if outreasonsList>'' then OutReasonsList := OutReasonsList + ',';
      OutReasonsList := OutReasonsList + QuotedStr(Table.fieldbyname('id').asstring);
      table.next;
    end;
  finally
    table.free
  end;

  //bvmessage(outreasonslist);
  //exit;




  Form := Tform.create(nil);
  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;

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

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


      Lab2 := TLabel.create(form); // óäàëÿòü íå íóæíî, Form åãî owner, ñàì óäàëèò
      Lab2.caption := 'Ñêëàä:';
      Lab2.Top := Lab.Top + Lab.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 := vartostr(GetOrgParamVAlue(idStorage,'NAME'))
      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;


      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 := ButtonAg.Top + ButtonAg.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
      if (IDStorage = 0)
      then begin
        if getconfirm('Âû íå óêàçàëè íè îäíîãî ñêëàäà.'+#13+'Îñóùåñòâèòü ïåðåäà÷ó ïî âñåì ñêëàäàì?') <> mrOk
        then continue;
      end;

      if idStorage <> 0
      then begin
        setlength(Storages,1);
        Storages[0] := idStorage;
      end
      else begin
        Table := TTAble.create(nil);
        try
          table.databasename := 'dbkassa';
          table.tablename := 'agents';
          table.open;
          table.first;
          table.filter := 'issklad = 1';
          table.filtered := true;
          table.first;
          while not table.eof do begin
            if table.fieldbyname('code').asstring>''
            then begin
              setlength(storages,length(storages)+1);
              storages[length(storages)-1] := table.fieldbyname('id').asinteger;
            end;
            table.next;
          end;


        finally
          table.free;
        end;
      end;

      (*
      for i := 0 to length(storages)-1 do
      begin
        createhint('Ýêñïîðò â ñêëàä ¹ '+inttostr(storages[i]));
      end;
      *)

      for i := 0 to length(storages)-1 do
      begin
        //createhint('Ýêñïîðò â ñêëàä ¹ '+inttostr(storages[i]));
        STDInOutCreateFile(storages[i],
                           includetrailingpathdelimiter(outputdir)+ vartostr(GetOrgParamValue(storages[i],'CODE')),
                           Editdate.date,
                           date(),
                           true,
                           false,
                           'DEFAULT',
                           OutReasonsList);
        //STDInOutCreateFile(Storage :integer; const DirName :string; FromDate :TDateTime; ToDate :TDateTime; CheckRef :boolean = true; CheckRefGetAll :Boolean = false; InReasons :string = 'DEFAULT'; OutReasons :string = 'DEFAULT')
        createhint('Ýêñïîðò â ñêëàä ¹ '+inttostr(storages[i])+' çàâåðøåí');
      end;
      //createhint('Îïåðàöèÿ ïåðåäà÷è çàâåðøåíà');
      break;

    end;
  finally
    Form.free;
  end;


end.
