unit U_MLConTract_NXSel;

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, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmMLConTract_NXSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: 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;
    ConNoHZ: TEdit;
    Label12: TLabel;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    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;
    Ncx: TMenuItem;
    v1YWConNo: TcxGridDBColumn;
    Button9: TButton;
    KHNameHZ: TEdit;
    Button10: TButton;
    Button11: TButton;
    SYRYWY: TEdit;
    SYRYWFZR: TEdit;
    v1Ssel: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1ConCode: TcxGridDBColumn;
    v1ConCodeName: TcxGridDBColumn;
    v1ConGY: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1ConColor: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v1ConColNo: TcxGridDBColumn;
    v1ConKHColNo: TcxGridDBColumn;
    v1ConHX: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1ConPrice: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1ConMoney: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v1ConNote: TcxGridDBColumn;
    v1IFFZ: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource4: TDataSource;
    ADOQuery1: TADOQuery;
    Order_Sub: TClientDataSet;
    TV2Column1: TcxGridDBColumn;
    PopupMenu3: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(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 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 N7Click(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure NcxClick(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure KHNameHZKeyPress(Sender: TObject; var Key: Char);
    procedure ChkZTChange(Sender: TObject);
    procedure BegDateChange(Sender: TObject);
    procedure KHNameHZChange(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton3Click(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_NXSel: TfrmMLConTract_NXSel;

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_NXSel.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_NXSel.SetStatus();
begin
  tchk.Visible := false;
  tnochk.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_NXSel.FormDestroy(Sender: TObject);
begin
  frmMLConTract_NXSel := nil;
end;

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

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

procedure TfrmMLConTract_NXSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('内销合同信息选择', Tv1, '生产指示单管理');
  WriteCxGrid('内销合同信息选择子表', Tv2, '生产指示单管理');
end;

procedure TfrmMLConTract_NXSel.InitGrid();
var
  fwhere, Pwhere: string;
begin
  ToolBar1.SetFocus;
  Pwhere := SGetFilters(Panel1, 1, 2);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.add('select  * from ( ');
      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.KHNo=A.KHNo)');
      sql.Add(',KHNameHZ=(select Top 1 isnull(B.KHNameJC,'''')+'';''+isnull(B.KHName,'''')+'';''+isnull([dbo].[getPinYin](B.KHName),'''')+'';''+B.KHNo from KH_Main B where B.KHNo=A.KHNo)');
      sql.Add(',SYRNameJC=(select Top 1 B.KHNameJC from KH_Main B where B.KHName=A.SYRName)');
      sql.Add(', ConNoHZ=isnull(A.ConNo,'''') +'';''+isnull(A.KHConNo,'''')+'';''+isnull(A.YWConNo,'''')');
      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;
//      if canshu1 <> '高权限' then
//      begin
//        SQL.Add(' and (A.SYRYWY=''' + trim(DName) + ''' or A.filler=''' + trim(DName) + ''')');
//      end;

      case cxTabControl1.TabIndex of
        1:
          begin
            sql.Add(' and isnull(A.ConType,'''')=''销售内销''');
          end;
        2:
          begin
            sql.Add(' and isnull(A.ConType,'''')=''加工内销''');
          end;
        3:
          begin
            sql.Add(' and isnull(A.ConType,'''')=''销售外销''');
          end;
        4:
          begin
            sql.Add(' and isnull(A.ConType,'''')=''加工外销''');
          end;
      end;

      sql.Add(' and A.Chkstatus<>''已作废''');

      sql.add(' ) AA');
      if Pwhere <> '' then
        sql.Add(' where ' + Pwhere);

      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMLConTract_NXSel.InitForm();
begin
  ReadCxGrid('内销合同信息选择', Tv1, '生产指示单管理');
  ReadCxGrid('内销合同信息选择子表', Tv2, '生产指示单管理');
  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;

function TfrmMLConTract_NXSel.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ML_OrderSubidNew B');
      sql.Add(' where exists(select * from ML_ConSub A where A.CSID=B.ConSubId');
      sql.Add(' and A.MLConID=''' + Trim(Order_Main.fieldbyname('MLConID').AsString) + ''')');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已生成生产计划单不能作废数据!', '提示', 0);
      Exit;
    end;
    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_NXSel.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '面料染色计划单');
end;

procedure TfrmMLConTract_NXSel.TBPrintClick(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName, FConType: string;
  i, j: Integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_HTInfo :MLConID');
    Parameters.ParamByName('MLConID').Value := Trim(Order_Main.fieldbyname('MLConID').AsString);
    Open;
  end;
  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  {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;}
  FConType := Trim(Order_Main.fieldbyname('ConType').AsString);
  if Pos('内销', FConType) > 0 then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\面料内销合同.rmf';
  end;
  if Pos('外销', FConType) > 0 then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\面料外销合同.rmf';
  end;
  if FileExists(fPrintFile) then
  begin
   // RMVariables['QRBARCODE'] := fImagePath;
    //RMVariables['SYRName']:=Trim(SYRName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

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

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

procedure TfrmMLConTract_NXSel.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_NXSel.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmMLConTract_NXSel.N1Click(Sender: TObject);
begin
  SelOKNo(Order_sub, True);
end;

procedure TfrmMLConTract_NXSel.N2Click(Sender: TObject);
begin
  SelOKNo(Order_sub, false);
end;

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

procedure TfrmMLConTract_NXSel.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;
        end;
        next;
      end;
    end;
    InitGrid();
    application.MessageBox('订单审核成功！', '提示信息');

  except
    application.MessageBox('订单审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmMLConTract_NXSel.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;

        end;
        next;
      end;
    end;
    InitGrid();
    application.MessageBox('订单撤销审核成功！', '提示信息');

  except
    application.MessageBox('订单撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmMLConTract_NXSel.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_NXSel.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_NXSel.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_NXSel.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_NXSel.Image3Click(Sender: TObject);
begin
  cxGroupBox2.Visible := false;
end;

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

procedure TfrmMLConTract_NXSel.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_NXSel.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_NXSel.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_NXSel.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_NXSel.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_NXSel.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_NXSel.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_NXSel.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.KHNameHZ.Text := Trim(CDS_HZ.fieldbyname('KHNameJC').AsString);

      end;
    end;
  finally
    frmKHList_CX.Free;
  end;
  InitGrid();
end;

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

    end;
    free;
  end;
  InitGrid();
end;

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

    end;
    free;
  end;
  InitGrid();
end;

procedure TfrmMLConTract_NXSel.KHNameHZKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid();
end;

procedure TfrmMLConTract_NXSel.ChkZTChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMLConTract_NXSel.BegDateChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMLConTract_NXSel.KHNameHZChange(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_NXSel.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *');
    sql.Add(',IFFZ=(select count(B.WBID) from TP_File B where B.WBID=A.CSID and B.TFType=''合同明细'')');
    SQL.Add(' from ML_ConSub A where 1=1 ');
    sql.Add(' and MLConID=''' + Trim(Order_Main.fieldbyname('MLConID').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);
end;

procedure TfrmMLConTract_NXSel.ToolButton3Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Order_Sub.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ModalResult := 1;
end;

end.

