unit U_MLConTract_NX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxCurrencyEdit, cxImage,
  cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System,
  RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu, cxPC, cxSplitter,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP,ShellAPI,
  cxCheckBox, cxGroupBox, ImgList;

type
  TfrmMLConTract_NX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Tv1: TcxGridDBTableView;
    Cxgrid1Level1: TcxGridLevel;
    Cxgrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    v1OrdDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1KHNameJC: TcxGridDBColumn;
    Label4: TLabel;
    PopupMenu1: TPopupMenu;
    ToolButton1: TToolButton;
    Label9: TLabel;
    YWConNo: TEdit;
    Label12: TLabel;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    v1SYRYWY: TcxGridDBColumn;
    v1ZQty: TcxGridDBColumn;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    v1SYRNameJC: TcxGridDBColumn;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    Label6: TLabel;
    DataSource3: TDataSource;
    Order_Print: TClientDataSet;
    v1ZMoney: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGroupBox2: TcxGroupBox;
    Image3: TImage;
    ListView1: TListView;
    Panel12: TPanel;
    Label8: TLabel;
    Button4: TButton;
    Panel13: TPanel;
    Image1: TImage;
    WJName: TEdit;
    Button7: TButton;
    WJPach: TEdit;
    Button8: TButton;
    Panel2: TPanel;
    ODPat: TOpenDialog;
    ImageList1: TImageList;
    IdFTP2: TIdFTP;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N3: TMenuItem;
    MenuItem2: TMenuItem;
    N4: TMenuItem;
    CheckBox1: TCheckBox;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    v1ChkZT: TcxGridDBColumn;
    Label3: TLabel;
    ChkZT: TComboBox;
    v1SYRYWFZR: TcxGridDBColumn;
    Label5: TLabel;
    Panel3: TPanel;
    Image5: TImage;
    Button6: TButton;
    Button2: TButton;
    Button5: TButton;
    Button3: TButton;
    Ncx: TMenuItem;
    v1YWConNo: TcxGridDBColumn;
    KHNo: TEdit;
    Button9: TButton;
    KHNameJC: TEdit;
    Button10: TButton;
    Button11: TButton;
    SYRYWY: TEdit;
    SYRYWFZR: TEdit;
    v1Ssel: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure MLOrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure Image3Click(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Image4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Image5Click(Sender: TObject);
    procedure NcxClick(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure KHNameJCChange(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure SetStatus();
    procedure Initimage();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    canshu1,canshu2,canshu3:string;
    fFlileFlag:string;
    { Public declarations }
  end;

var
  frmMLConTract_NX: TfrmMLConTract_NX;

implementation
uses
  U_DataLink,U_MLOrderInPut,U_Fun, U_FjList_RZ,U_MLOrderInPut_SQJH,U_KHList_CX,U_ZDYHelp_YW,
  U_MLOrderInPut_JGNX,U_MLOrderInPut_XSWX,U_MLOrderInPut_JGWX;
{$R *.dfm}
procedure TfrmMLConTract_NX.Initimage();
var
  ListItem: TListItem;
  Flag: Cardinal;
  info: SHFILEINFOA;
  Icon: TIcon;
begin
  ListView1.Items.Clear;
  with adoqueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select fileName from TP_File ');
    sql.Add('where WBID='''+trim(Order_Main.fieldbyname('MLConID').AsString)+''' ');
    sql.Add('and TFType=''面料合同''');
    open;
    if not IsEmpty then
    begin
      while not eof do
      begin
        with ListView1 do
        begin
           LargeImages := ImageList1;
           Icon := TIcon.Create;
           ListItem := Items.Add;
           Listitem.Caption := trim(fieldbyname('fileName').AsString);
           Flag := (SHGFI_LARGEICON or SHGFI_ICON or SHGFI_USEFILEATTRIBUTES);
           SHGetFileInfo(Pchar(trim(fieldbyname('fileName').AsString)), 0, info, Sizeof(info), Flag);
           Icon.Handle := info.hIcon;
           ImageList1.AddIcon(Icon);
           ListItem.ImageIndex := ImageList1.Count - 1;
        end;
        next;
      end;
    end;
  end;
end;

procedure TfrmMLConTract_NX.SetStatus();
begin
  tchk.Visible:=false;
  tnochk.Visible:=false;
  TBPrint.Visible:=false;
  Ncx.Visible:=false;
  N5.Visible:=true;
  N7.Visible:=true;
  if (Trim(canshu1)='高权限') and (trim(canshu2)='')  then
  begin
    tchk.Visible:=true;
    tnochk.Visible:=true;
    Ncx.Visible:=true;
  end
  else
  if (Trim(canshu1)='高权限') and (trim(canshu2)='查询')  then
  begin
    tbadd.Visible:=false;
    N5.Visible:=False;
    N7.Visible:=false;
  end;
end;

procedure TfrmMLConTract_NX.FormDestroy(Sender: TObject);
begin
  frmMLConTract_NX:=nil;
end;

procedure TfrmMLConTract_NX.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmMLConTract_NX.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
  canshu1:=trim(DParameters1);
  canshu2:=trim(DdataBase);
end;

procedure TfrmMLConTract_NX.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('内销合同信息登记11',Tv1,'生产指示单管理');
end;

procedure TfrmMLConTract_NX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.add('select A.* ');
      sql.Add(',ChkZT=(case when isnull(ChkStatus,''0'')=''0'' then ''未审核'' else ''已审核'' end)');
      sql.Add(',IFFZ=(select count(B.WBID) from TP_File B where B.WBID=A.MLConID and B.TFType=''面料合同'')');
      sql.Add(',ZQty=(select Sum(B.Qty) from ML_ConSub B where B.MLConID=A.MLConID)');
      sql.Add(',ZMoney=(select Sum(B.ConMoney) from ML_ConSub B where B.MLConID=A.MLConID)');
      sql.Add(',KHNameJC=(select Top 1 B.KHNameJC from KH_Main B where B.KHName=A.KHName)');
      sql.Add(',SYRNameJC=(select Top 1 B.KHNameJC from KH_Main B where B.KHName=A.SYRName)');
      sql.Add(',KHNo=(select Top 1 B.KHNo from KH_Main B where B.KHName=A.KHName)');
      sql.Add(' from ML_ConMain A where 1=1 ');
      if CheckBox1.Checked=true then
      begin
        BegDate.Visible:=true;
        enddate.Visible:=true;
        sql.Add('and A.OrdDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''' ');
        sql.Add('and A.OrdDate<'''+Trim(FormatDateTime('yyyy-MM-dd',endDate.DateTime+1))+''' ');
      end
      else
      begin
        BegDate.Visible:=false;
        enddate.Visible:=false;
        sql.Add('and A.OrdDate>='''+Trim(FormatDateTime('yyyy',BegDate.DateTime)+'-01-01')+''' ');
        sql.Add('and A.OrdDate<'''+Trim(FormatDateTime('yyyy-MM-dd',endDate.DateTime+1))+''' ');
      end;
      if canshu1<>'高权限' then
      begin
        SQL.Add(' and (A.SYRYWY='''+trim(DName)+''' or A.filler='''+trim(DName)+''')');
      end;
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(A.ConType,'''')=''内销''');
      end;
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(A.ConType,'''')=''外销''');
      end;
      sql.Add(' and A.Chkstatus<>''作废''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    TBFind.Click;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMLConTract_NX.InitForm();
begin
  ReadCxGrid('内销合同信息登记11',Tv1,'生产指示单管理');
  BegDate.DateTime:=StrToDate(FormatDateTime('yyyy',SGetServerDate10(ADOQueryTemp))+'-01-01');
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  IF trim(DdataBase)='查询' then
  begin
    TBadd.Visible:=false;
  end;

  SetStatus();
  InitGrid();
end;

procedure TfrmMLConTract_NX.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

function TfrmMLConTract_NX.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update ML_ConMain set Chkstatus=''已作废'' where MLConID='''+Trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据作废异常！','提示',0);
  end;
end;

procedure TfrmMLConTract_NX.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'面料染色计划单');
end;

procedure TfrmMLConTract_NX.TBPrintClick(Sender: TObject);
begin
  Panel4.Left:=(self.Width - Panel4.Width) div 2;
  Panel4.Visible:=true;
end;

procedure TfrmMLConTract_NX.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMLConTract_NX.TBAddClick(Sender: TObject);
begin
  Panel3.Visible:=true;
end;

procedure TfrmMLConTract_NX.MLOrderNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmMLConTract_NX.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmMLConTract_NX.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName='IFFZ' then
  begin
    cxGroupBox2.Visible:=true;
    Initimage();
  end
  else
  ToolButton1.Click;
end;

procedure TfrmMLConTract_NX.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMLConTract_NX.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmMLConTract_NX.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,false);
end;

procedure TfrmMLConTract_NX.N2Click(Sender: TObject);
begin
 SelOKNo(Order_Main,True);
end;

procedure TfrmMLConTract_NX.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmMLOrderInPut_SQJH:=TfrmMLOrderInPut_SQJH.Create(Application);
    with frmMLOrderInPut_SQJH do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MLConID').AsString);
      ToolBar2.Visible:=False;
      TBSave.Visible:=False;
      ScrollBox1.Enabled:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmMLOrderInPut_SQJH.Free;
  end;
end;

procedure TfrmMLConTract_NX.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmMLConTract_NX.Button1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno,LBName,SYRName:string;
   i,j:Integer;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('Ssel',True,[])=False then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('select A.*,B.* from ML_ConMain A ');
    sql.Add('inner Join ML_ConSub B on A.MLConID=B.MLConID ');
    sql.Add('where 1=2');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,Order_Print);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      if FieldByName('Ssel').AsBoolean=true then
      begin
        with ADOQueryPrint do
        begin
          Close;
          sql.Clear;
          sql.add('select A.*,B.* from ML_ConMain A ');
          sql.Add('inner Join ML_ConSub B on A.MLConID=B.MLConID ');
          sql.Add('where MLConID='''+Trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
          Open;
        end;
        SInitCDSData20(ADOQueryPrint,Order_Print);
      end;
      next;
    end;
  end;
  Order_Main.EnableControls;

  {try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(ADOQueryPrint.fieldbyname('OrderNo').AsString);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp' ;
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    exit;
  end;}

  fPrintFile:= ExtractFilePath(Application.ExeName) +'Report\面料购销合同.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE']:=fImagePath;
    //RMVariables['SYRName']:=Trim(SYRName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(LBName)+'.rmf'),'提示',0);
  end;
end;

procedure TfrmMLConTract_NX.Image2Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmMLConTract_NX.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmMLConTract_NX.TchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.Locate('Ssel',True,[])=false then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if FieldByName('Ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  ML_ConMain SET Chkstatus=''已审核'',chker='''+trim(DName)+''',chkTime=getdate() ');
            sql.Add('where MLConID='+quotedstr(trim(Order_Main.fieldbyname('MLConID').AsString)));
            execsql;
          end;
          Order_Main.Delete;
        end
        else
        next;
      end;
    end;
    application.MessageBox('订单审核成功！','提示信息');
    //TBRafresh.Click;
  except
   application.MessageBox('订单审核失败！','提示信息',0);
  end;
end;

procedure TfrmMLConTract_NX.TnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.Locate('Ssel',True,[])=false then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if FieldByName('Ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update ML_ConMain SET Chkstatus='''',chker='''',chkTime=null ');
            sql.Add('where MLConID='+quotedstr(trim(Order_Main.fieldbyname('MLConID').AsString)));
            execsql;
          end;
          Order_Main.Delete;
        end
        else
        next;
      end;
    end;
    application.MessageBox('订单撤销审核成功！','提示信息');
    //TBRafresh.Click;
  except
    application.MessageBox('订单撤销审核失败！','提示信息',0);
  end;
end;

procedure TfrmMLConTract_NX.MenuItem1Click(Sender: TObject);
var
  OpenDiaLog: TOpenDialog;
  fFileName:string;
  fFilePath:string;
begin
  OpenDiaLog := TOpenDialog.Create(Self);
  if OpenDiaLog.Execute then
  begin
    fFilePath:=OpenDiaLog.FileName;
    fFileName:=ExtractFileName(OpenDiaLog.FileName);
    Panel12.Visible:=True;
    WJName.Text:=Trim(fFileName);
    WJName.Hint:=Trim(fFileName);
    WJPach.Text:=fFilePath;
    Button8.Visible:=False;
    Button4.Visible:=True;
    Button7.Visible:=True;
    Panel12.Refresh;
  end;
end;

procedure TfrmMLConTract_NX.N3Click(Sender: TObject);
var
  fFileName:string;
begin
  if listView1.SelCount<1 then exit;
  fFileName:=ListView1.Selected.Caption;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete  from TP_File ');
    sql.Add('where WBID='''+trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
    sql.Add('and TFType=''面料合同''');
    sql.Add('and FileName='+quotedstr(trim(fFileName)));
    execsql;
  end;
  Initimage();
  InitGrid();
end;

procedure TfrmMLConTract_NX.N4Click(Sender: TObject);
var
  SaveDialog: TSaveDialog;
  fFileName:string;
  fFilePath:string;
begin
  if listView1.SelCount<1 then exit;
  try
    fFileName:=ListView1.Selected.Caption;
    SaveDialog := TSaveDialog.Create(Self);
    SaveDialog.FileName:=fFileName;
    if SaveDialog.Execute then
    begin
      Panel2.Caption:='正在保存数据，请稍等...';
      Panel2.Visible:=true;
      application.ProcessMessages;
      fFilePath:=SaveDialog.FileName;
      try
        IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI','SERVER','FTP地址','127.0.0.1');;
        IdFTP1.Username := 'three';
		    IdFTP1.Password := '641010';
        IdFTP1.Connect();
      except
        ;
      end;

      if IdFTP1.Connected then
      begin
        Panel2.Caption:='正在下载数据，请稍等...';
        Panel2.Visible:=true;
        application.ProcessMessages;
        try
         IdFTP1.Get('FJ\'+ Trim(fFileName), fFilePath,false, true);
        except
          Panel2.Visible:=false;
          Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
          IdFTP1.Quit;
          Exit;
        end;
      end else
      begin
        Panel2.Visible:=false;
        Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
        IdFTP1.Quit;
        Exit;
      end;
     Panel2.Visible:=false;
     if IdFTP1.Connected then IdFTP1.Quit;
    end;
  except
    Panel2.Visible:=false;
  end;
end;

procedure TfrmMLConTract_NX.MenuItem2Click(Sender: TObject);
begin
  if listView1.SelCount<1 then exit;
  Panel12.Visible:=True;
  Button4.Visible:=False;
  Button7.Visible:=False;
  Button8.Visible:=True;
  Panel12.Refresh;
  WJName.Text:=Trim(ListView1.Selected.Caption);
  WJName.Hint:=Trim(ListView1.Selected.Caption);
end;

procedure TfrmMLConTract_NX.Image3Click(Sender: TObject);
begin
  cxGroupBox2.Visible:=false;
end;

procedure TfrmMLConTract_NX.Image1Click(Sender: TObject);
begin
  Panel12.Visible:=false;
end;

procedure TfrmMLConTract_NX.Button4Click(Sender: TObject);
var
  fFileName:string;
  fFilePath:string;
  maxNo:string;
begin
  try
    adoqueryCmd.Connection.BeginTrans;
    begin
      fFilePath:=WJPach.Text;
      fFileName:=WJName.Text;
      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select TFId from TP_File ');
        sql.Add('where WBID='''+trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
        sql.Add(' and TFType=''面料合同''');
        sql.Add(' and FileName='+quotedstr(trim(fFileName)));
        open;
      end;
      IF ADOQueryCmd.IsEmpty=False  then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('此附件名称已存在，请修改文件名，继续上传！','提示信息',MB_ICONERROR);
        exit;
      end;
      Panel12.Visible:=False;
      Panel2.Caption:='正在上传数据，请稍等...';
      Panel2.Visible:=true;
      application.ProcessMessages;

      if GetLSNo(ADOQueryCmd,maxNo,'MD','TP_File',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！','提示',0);
        Exit;
      end;

      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('delete from TP_File ');
        sql.Add('where WBID='''+trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
        sql.Add('and TFType=''面料合同''');
        sql.Add('and FileName='+quotedstr(trim(fFileName)));
        execsql;
      end;
      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from TP_File ');
        sql.Add('where WBID='''+trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
        sql.Add('and TFType=''面料合同''');
        sql.Add('and FileName='+quotedstr(trim(fFileName)));
        open;
        append;
        fieldbyname('TFID').Value:=trim(maxNO);
        fieldbyname('WBID').Value:=trim(Order_Main.fieldbyname('MLConID').AsString);
        fieldbyname('TFType').Value:='面料合同';
        fieldbyname('FileName').Value:=trim(fFileName);
        post;
      end;
      if fFilePath <> '' then
      begin
        try
          IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
          IdFTP1.Username := 'three';
          IdFTP1.Password := '641010';
          IdFTP1.Connect();
          IdFTP1.Put(fFilePath, 'FJ\' + Trim(fFileName));
          IdFTP1.Quit;
        except
          IdFTP1.Quit;
          Application.MessageBox('上传文件失败，请检查文件服务器！', '提示', MB_ICONWARNING);
        end;
      end;
      IdFTP1.Quit;
      Panel2.Visible:=false;
      Initimage();
      InitGrid();
    end;
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！','提示信息',0);
  end;
end;

procedure TfrmMLConTract_NX.Button8Click(Sender: TObject);
var
  fFileName:String;
begin
  fFileName:=WJName.Hint;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('UPdate TP_File Set FileName='''+Trim(WJName.Text)+'''');
    sql.Add('where WBID='''+trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
    sql.Add('and TFType=''面料合同''');
    sql.Add('and FileName='+quotedstr(trim(fFileName)));
    execsql;
  end;
  Panel12.Visible:=False;
  Initimage();
end;

procedure TfrmMLConTract_NX.Button7Click(Sender: TObject);
var
  fFileName:string;
  fFilePath:string;
  maxNo:string;
begin
  try
    adoqueryCmd.Connection.BeginTrans;
    fFilePath:=WJPach.Text;
    fFileName:=WJName.Hint;
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select TFId from TP_File ');
      sql.Add('where WBID='''+trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
      sql.Add('and TFType=''面料合同''');
      sql.Add('and FileName='+quotedstr(trim(fFileName)));
      open;
      IF not adoqueryCmd.IsEmpty then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('此附件名称已存在，请修改文件名，继续上传！','提示信息',MB_ICONERROR);
        exit;
      end;
    end;
    Panel12.Visible:=False;
    Panel2.Caption:='正在上传数据，请稍等...';
    Panel2.Visible:=true;
    application.ProcessMessages;

    if GetLSNo(ADOQueryCmd,maxNo,'FJ','TP_File',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;

    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from TP_File ');
      sql.Add('where WBID='''+trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
      sql.Add('and TFType=''面料合同''');
      sql.Add('and FileName='+quotedstr(trim(fFileName)));
      execsql;
    end;
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from TP_File ');
      sql.Add('where WBID='''+trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
      sql.Add('and TFType=''面料合同''');
      sql.Add('and FileName='+quotedstr(trim(fFileName)));
      open;
      append;
      fieldbyname('TFID').Value:=trim(maxNO);
      fieldbyname('WBID').Value:=trim(Order_Main.fieldbyname('MLConID').AsString);
      fieldbyname('TFType').Value:='面料合同';
      fieldbyname('FileName').Value:=trim(fFileName);
      post;
    end;
    if fFilePath <> '' then
    begin
      try
        IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
        IdFTP1.Username := 'three';
        IdFTP1.Password := '641010';
        IdFTP1.Connect();
        IdFTP1.Put(fFilePath, 'FJ\' + Trim(fFileName));
        IdFTP1.Quit;
      except
        IdFTP1.Quit;
        Application.MessageBox('上传文件失败，请检查文件服务器！', '提示', MB_ICONWARNING);
      end;
    end;
    IdFTP1.Quit;
    Panel2.Visible:=false;
    Initimage();
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！','提示信息',0);
  end;
end;

procedure TfrmMLConTract_NX.Image4Click(Sender: TObject);
begin
  Panel3.Visible:=false;
end;

procedure TfrmMLConTract_NX.Button2Click(Sender: TObject);
begin
  try
    frmMLOrderInPut_JGNX:=TfrmMLOrderInPut_JGNX.Create(Application);
    with frmMLOrderInPut_JGNX do
    begin
      PState:=0;
      FMainId:='';
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmMLOrderInPut_JGNX.Free;
  end;
  Panel3.Visible:=false;
end;

procedure TfrmMLConTract_NX.N5Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if ((Trim(DParameters1)<>'高权限') and (UpperCase(Trim(Order_Main.fieldbyname('Filler').AsString))<>UpperCase(Trim(DName)))) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  try
    frmMLOrderInPut_SQJH:=TfrmMLOrderInPut_SQJH.Create(Application);
    with frmMLOrderInPut_SQJH do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MLConID').AsString);
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin
        initgrid();
      end;
    end;
  finally
    frmMLOrderInPut_SQJH.Free;
  end;
end;

procedure TfrmMLConTract_NX.N6Click(Sender: TObject);
begin
  Panel4.Left:=(self.Width - Panel4.Width) div 2;
  Panel4.Visible:=true;
end;

procedure TfrmMLConTract_NX.N7Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if (UpperCase(Trim(Order_Main.fieldbyname('Filler').AsString))<>UpperCase(Trim(DName))) and (Trim(canshu1)<>'高权限')  then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    InitGrid();
  end;
end;

procedure TfrmMLConTract_NX.Button6Click(Sender: TObject);
begin
  try
    frmMLOrderInPut_SQJH:=TfrmMLOrderInPut_SQJH.Create(Application);
    with frmMLOrderInPut_SQJH do
    begin
      PState:=0;
      FMainId:='';
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmMLOrderInPut_SQJH.Free;
  end;
  Panel3.Visible:=false;
end;

procedure TfrmMLConTract_NX.ListView1DblClick(Sender: TObject);
var
  sFieldName:string;
  fileName:string;
begin
  if ListView1.Items.Count<1 THEN EXIT;

  if listView1.SelCount<1 then exit;
  sFieldName:=leftbstr(ExtractFilePath(Application.ExeName),1)+':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
  CreateDirectory(pchar(sFieldName),nil);

  fileName:=ListView1.Selected.Caption;

  sFieldName:=sFieldName+'\'+trim(fileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    IdFTP1.Username := 'three';
		IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin
    Panel2.Caption:='正在下载数据，请稍等...';
    Panel2.Visible:=true;
    application.ProcessMessages;
    try
      IdFTP1.Get('FJ\'+ Trim(fileName), sFieldName,true, false);
    except
      Panel2.Visible:=false;
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Panel2.Visible:=false;
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  Panel2.Visible:=false;
   if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(sFieldName),'', '', SW_SHOWNORMAL);
end;

procedure TfrmMLConTract_NX.Button5Click(Sender: TObject);
begin
  try
    frmMLOrderInPut_XSWX:=TfrmMLOrderInPut_XSWX.Create(Application);
    with frmMLOrderInPut_XSWX do
    begin
      PState:=0;
      FMainId:='';
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmMLOrderInPut_XSWX.Free;
  end;
  Panel3.Visible:=false;
end;

procedure TfrmMLConTract_NX.Button3Click(Sender: TObject);
begin
  try
    frmMLOrderInPut_JGWX:=TfrmMLOrderInPut_JGWX.Create(Application);
    with frmMLOrderInPut_JGWX do
    begin
      PState:=0;
      FMainId:='';
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmMLOrderInPut_JGWX.Free;
  end;
  Panel3.Visible:=false;
end;

procedure TfrmMLConTract_NX.Image5Click(Sender: TObject);
begin
  Panel3.Visible:=false;
end;

procedure TfrmMLConTract_NX.NcxClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if (UpperCase(Trim(Order_Main.fieldbyname('Filler').AsString))<>UpperCase(Trim(DName))) and (Trim(canshu1)<>'高权限')  then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update ML_ConMain set Chkstatus='''' where MLConID='''+Trim(Order_Main.fieldbyname('MLConID').AsString)+'''');
      ExecSQL;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！','提示',0);
  end;
end;

procedure TfrmMLConTract_NX.Button9Click(Sender: TObject);
begin
  try
    frmKHList_CX:=TfrmKHList_CX.Create(Application);
    with frmKHList_CX do
    begin
      cxGrid3.Visible:=false;
      if ShowModal=1 then
      begin
        Self.KHNameJC.Text:=Trim(CDS_HZ.fieldbyname('KHNameJC').AsString);
        self.KHNo.Text:=Trim(CDS_HZ.fieldbyname('KHNo').AsString);
      end;
    end;
  finally
    frmKHList_CX.Free;
  end;
  self.TBFind.click;
end;

procedure TfrmMLConTract_NX.KHNameJCChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmMLConTract_NX.Button10Click(Sender: TObject);
begin
  frmZDYHelpYW:=TfrmZDYHelpYW.Create(self);
  with frmZDYHelpYW do
  begin
    if ShowModal=1 then
    begin
      SYRYWY.Text:=Trim(ClientDataSet1.fieldbyname('UserName').AsString);
      TBFind.Click;
    end;
    free;
  end;
end;

procedure TfrmMLConTract_NX.Button11Click(Sender: TObject);
begin
  frmZDYHelpYW:=TfrmZDYHelpYW.Create(self);
  with frmZDYHelpYW do
  begin
    if ShowModal=1 then
    begin
      SYRYWFZR.Text:=Trim(ClientDataSet1.fieldbyname('UserName').AsString);
      TBFind.Click;
    end;
    free;
  end;
end;

end.
