unit U_ClothContractListHZ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI, cxCheckBox,jpeg,IniFiles;

type
  TfrmClothContractListHZ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNo: TEdit;
    Label5: TLabel;
    C_Code: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    FactoryNoName: TEdit;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1Qty1: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    ConType: TComboBox;
    Label14: TLabel;
    ToolButton2: TToolButton;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    ComboBox1: TComboBox;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    v1Column13: TcxGridDBColumn;
    Panel16: TPanel;
    Panel2: TPanel;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxTabSheet3: TcxTabSheet;
    cxTabControl1: TcxTabControl;
    TBSSOk: TToolButton;
    TBSSNO: TToolButton;
    TBChkNO: TToolButton;
    TBChkOk: TToolButton;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    ToolButton8: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure TBSSOkClick(Sender: TObject);
    procedure TBSSNOClick(Sender: TObject);
    procedure TBChkOkClick(Sender: TObject);
    procedure TBChkNOClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
  private
    FInt,PFInt:Integer;
    canshu1:string;
    lstPat: TStringList;
		AJpeg: TJPEGImage;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure ReadINIFile10();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmClothContractListHZ: TfrmClothContractListHZ;

implementation
uses
  U_DataLink,U_RTFun,U_ClothContractInPutSXMX,U_ZDYHelp
  {U_ClothContractInPutSX,
  U_ProductOrderList,U_ZDYHelp,
  ,U_ClothContractInPutHZ,U_ClothContractInPutPB}, U_ModuleNote;

{$R *.dfm}

procedure TfrmClothContractListHZ.FormDestroy(Sender: TObject);
begin
  frmClothContractListHZ:=nil;
end;

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

procedure TfrmClothContractListHZ.TBCloseClick(Sender: TObject);
var
  FPath:String;
begin
  Close;
  WriteCxGrid('采购合同1',Tv1,'生产合同管理');
  FPath:='D:\Right1209\';
  if  DirectoryExists(ExtractFileDir(FPath)) then
    winexec('cmd /c rd /s /q D:\Right1209\',sw_hide);
end;

procedure TfrmClothContractListHZ.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,Money=B.ConQty*B.ConPrice from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
      sql.Add(' where A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      sql.Add(' and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
      {if Trim(DParameters1)<>'高权限' then
      begin
        sql.Add(' and A.Filler='''+Trim(DName)+'''');
      end;}
      if cxPageControl1.ActivePageIndex=0 then
      begin
        sql.Add(' and isnull(A.JYType,'''')='''' ');
      end else
      if cxPageControl1.ActivePageIndex=1 then
      begin
        sql.Add(' and isnull(A.JYType,'''')<>'''' ');
        if cxTabControl1.TabIndex=0 then
        begin
          sql.Add(' and isnull(ChkStatus,'''')='''' ');
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          sql.Add(' and isnull(ChkStatus,'''')=''已送审'' ');
        end else
        if cxTabControl1.TabIndex=2 then
        begin
          sql.Add(' and isnull(ChkStatus,'''')=''审核通过'' ');
        end;
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmClothContractListHZ.InitForm();
var
  FSJ:string;
begin
  
  ReadCxGrid('采购合同1',Tv1,'生产合同管理');
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
  FSJ:='select ZdyName Name,ZdyName Code from KH_Zdy where TYpe=''JGGX'' and ZdyName not like ''%回检%'' ';
  SInitComBoxBySql(ADOQueryTemp,ConType,False,Fsj);
  if Trim(canshu1)='高权限' then
  begin
    TBSSOk.Visible:=False;
    TBSSNO.Visible:=False;
    TBChkNO.Visible:=True;
    TBChkOk.Visible:=True;
    ToolButton7.Visible:=True;
  end else
  begin
    TBChkNO.Visible:=False;
    TBChkOk.Visible:=False;
    TBSSOk.Visible:=True;
    TBSSNO.Visible:=True;
    ToolButton7.Visible:=False;
  end;
end;

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

procedure TfrmClothContractListHZ.TBEditClick(Sender: TObject);
var
  FInt:Integer;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(canshu1)<>'高权限' then
  begin
    if cxPageControl1.ActivePageIndex<>1 then
    begin
      if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
      begin
        Application.MessageBox('不能操作他人数据!','提示',0);
        Exit;
      end;
    end else
    begin
      if cxTabControl1.TabIndex<>0 then Exit;
      FInt:=9;
    end;
  end;
  try
    frmClothContractInPutSXMX:=TfrmClothContractInPutSXMX.Create(Application);
    with frmClothContractInPutSXMX do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
      EditInt:=FInt;
      frmClothContractInPutSXMX.canshu1:=Trim(self.canshu1);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmClothContractInPutSXMX.Free;
  end;
end;

procedure TfrmClothContractListHZ.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(canshu1)<>'高权限' then
  begin
    if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
    begin
      Application.MessageBox('不能操作他人数据!','提示',0);
      Exit;
    end;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD where COnMainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=false then
  begin
    Application.MessageBox('已经录入码单不能删除数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmClothContractListHZ.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmClothContractListHZ.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('坯布合同订单列表',cxGrid1);
end;

procedure TfrmClothContractListHZ.TBPrintClick(Sender: TObject);
var
   fPrintFile,FConNoM,RPTName:string;

begin
  if Order_Main.IsEmpty then Exit;
  if Trim(ComboBox1.Text)='' then
  begin
    Application.MessageBox('报表名称不能为空!','提示',0);
    Exit;
  end;
  RPTName:='Report\'+Trim(ComboBox1.Text)+'.rmf';
 // fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\采购加工合同.rmf' ;
  fPrintFile:= ExtractFilePath(Application.ExeName) +RPTName;
  with ADOQueryTemp do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,Money=B.ConQty*B.ConPrice,C.*,D.KHName,D.ZKTelNo,D.ZKFax,D.ZKAddress  ');
    sql.Add(' ,C.ZdyName,C.ZdyStr1,C.ZdyStr2,C.ZdyStr3,C.ZdyStr4');
    sql.Add(' from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId');
    sql.Add(' left join KH_Zdy C on A.CompanyName=C.ZdyName and C.Type=''GSTT'' ');
    sql.Add(' left join ZH_KH_Info D on A.FactoryNo=D.ZKID');
    sql.Add(' where A.Mainid='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+RPTName),'提示',0);
  end;

end;

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

procedure TfrmClothContractListHZ.TBAddClick(Sender: TObject);
begin
  try
    frmClothContractInPutSXMX:=TfrmClothContractInPutSXMX.Create(Application);
    with frmClothContractInPutSXMX do
    begin
      PState:=0;
      FMainId:='';

      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmClothContractInPutSXMX.Free;
  end;
end;

procedure TfrmClothContractListHZ.ConNoChange(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 TfrmClothContractListHZ.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmClothContractListHZ.conPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNo.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;

      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select A.*,B.*,Money=B.C_Qty*B.Price from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
        sql.Add(' where A.ConNo like '''+'%'+Trim(ConNo.text)+'%'+'''');

        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmClothContractListHZ.cxPageControl1Change(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex=1 then
  begin
    cxPageControl1.Height:=46;
    if Trim(canshu1)='高权限' then
    begin
      cxTabControl1.TabIndex:=1;
    end;
  end else
  begin
    cxPageControl1.Height:=23;
  end;
  InitGrid();
end;

procedure TfrmClothContractListHZ.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmClothContractInPutSXMX:=TfrmClothContractInPutSXMX.Create(Application);
    with frmClothContractInPutSXMX do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
      PCopyInt:=1;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmClothContractInPutSXMX.Free;
  end;
end;

procedure TfrmClothContractListHZ.ToolButton2Click(Sender: TObject);
begin
  try
    frmClothContractInPutSXMX:=TfrmClothContractInPutSXMX.Create(Application);
    with frmClothContractInPutSXMX do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
      TBSave.Visible:=False;
      ToolBar2.Visible:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmClothContractInPutSXMX.Free;
  end;
end;

procedure TfrmClothContractListHZ.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='采购加工合同';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmClothContractListHZ.FormCreate(Sender: TObject);
begin
  lstPat := TStringList.Create;
  canshu1:=Trim(DParameters1);
  cxPageControl1.ActivePageIndex:=0;
end;

procedure TfrmClothContractListHZ.ToolButton4Click(Sender: TObject);
var
  i,j: Integer;
  PatFile,HZStr: String;
  FTPPath,FConNo,MaxNo:string;
begin
  {if Trim(canshu1)<>'高权限' then
  begin
    if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
    begin
      Application.MessageBox('不能操作他人数据!','提示',0);
      Exit;
    end;
  end; }
  lstPat.Clear;
  if ODPat.Execute then
  begin
    lstPat.AddStrings(ODPat.Files);
  end;

  if lstPat.Count > 0 then
  begin
    try
      ReadINIFile10();
      server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
      IdFTP1.Host :=server;//PicSvr;
      IdFTP1.Username := 'three';
      IdFTP1.Password := '641010';
      IdFTP1.Connect();
    except
      IdFTP1.Quit;
      Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
      Exit;
    end;
  end else
  begin
    Exit;
  end;
  Panel16.Visible:=True;
  Panel16.Refresh;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select isnull(Count(*),0) MM from XD_File where  ');
    SQL.Add(' filetype=''NXHT''');
    Open;
    j:=fieldbyname('MM').AsInteger;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    for i := 0 to lstPat.Count - 1 do
    begin
      PatFile := ExtractFileName(lstPat[i]);
      HZStr:=Copy(PatFile,(Pos('.',PatFile)+1),(Length(PatFile)-Pos('.',PatFile))  ) ;
      if IdFTP1.Connected then
      begin
        try
          {if not DirectoryExists('D:\图片\'+Trim(gDef1)) then
           ForceDirectories('D:\图片\'+Trim(gDef1)); }
         // IdFTP1.Put(lstPat[i], Trim('\SC')+'\'+Trim(PatFile));
          if GetLSNo(ADOQueryCmd,MaxNo,'NH','XD_File',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取文件最大号失败！','提示',0);
            Exit;
          end;
          IdFTP1.Put(lstPat[i], Trim('\NH')+'\'+Trim(MaxNo)+'.'+Trim(HZStr));
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            SQL.Add('select * from XD_File where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('XFID').Value:=Trim(MaxNo);
            FieldByName('CYID').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
            FieldByName('CYNO').Value:=Trim(Order_Main.fieldbyname('ConNo').AsString);
            FieldByName('filename').Value:=Trim(PatFile);
            FieldByName('FileDate').Value:=SGetServerDate(ADOQueryTemp);
            fieldbyname('FileType').value:=Trim('NXHT');
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('HZStr').Value:='.'+Trim(HZStr);
            Post;
          end;
        except
          //ADOQueryCmd.Connection.RollbackTrans;
          //Application.MessageBox('图片上传失败！','提示',0);
        end;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update Contract_Main Set FileFlag=1 where Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    if IdFTP1.Connected then IdFTP1.Quit;
    Panel16.Visible:=False;
    Application.MessageBox('上传成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('上传失败！','提示',0);
  end;
end;
procedure TfrmClothContractListHZ.ReadINIFile10();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

procedure TfrmClothContractListHZ.ToolButton5Click(Sender: TObject);
var
  fHandle:THandle;
  FInt:Integer;
  FFName,FPath:String;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.fieldbyname('FileFlag').AsBoolean=False then Exit;
  Panel2.Visible:=True;
  Panel2.Refresh;
 { }
  try
    ReadINIFile10();
    server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    if Length(server)<6 then
    begin
      server:='127.0.0.1';
    end;
    IdFTP1.Host :=server;//PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath:='D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from XD_File where CYID='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  FFName:=Trim(ADOQueryTemp.fieldbyname('XFID').AsString)+Trim(ADOQueryTemp.fieldbyname('HZStr').AsString);
  if FileExists(FPath+FFName)=false then
  begin
    IdFTP1.Get('NH\'+FFName,FPath+FFName);
  end;
  Panel2.Visible:=False;
  if IdFTP1.Connected then IdFTP1.Quit;
  if FileExists(FPath+FFName) then
  begin
    ShellExecute(Handle, 'open',PChar(FPath+FFName),'', '', SW_SHOWNORMAL);
  end;
  {FFName:=Trim(CDS_SC.fieldbyname('FileName').AsString);

  FFName:=FPath+FFName;
  if FileExists(FFName)=false then
  begin
    IdFTP1.Get('SC\'+Trim(CDS_SC.fieldbyname('FileName').AsString),
                   FPath+Trim(CDS_SC.fieldbyname('FileName').AsString)
                    );
  end;
  Panel2.Visible:=False;
  if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(FPath+Trim(CDS_SC.fieldbyname('FileName').AsString)),'', '', SW_SHOWNORMAL); }

end;

procedure TfrmClothContractListHZ.ToolButton6Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update Contract_Main Set FileFlag=0 where Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    sql.Add(' delete XD_File where CYID='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    ExecSQL;
  end;

end;

procedure TfrmClothContractListHZ.ToolButton7Click(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ConJYType';
      flagname:='紧急合同原因';
      if Trim(canshu1)<>'高权限' then
      begin
        frmZDYHelp.TBAdd.Visible:=False;
        frmZDYHelp.TBEdit.Visible:=False;
        frmZDYHelp.TBDel.Visible:=False;
      end;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractListHZ.TBSSOkClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex<>1 then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要送审数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('Update Contract_Main Set ChkStatus=''已送审'',SSPerson='''+Trim(DName)+'''');
    sql.Add(',SSTime=getdate() where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  Order_Main.Delete;
end;

procedure TfrmClothContractListHZ.TBSSNOClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex<>1 then Exit;
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and isnull(ChkStatus,'''')=''审核通过'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('紧急合同已审核通过不能撤销!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销送审数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('Update Contract_Main Set ChkStatus='''',SSPerson='''' ');
    sql.Add(',SSTime=Null where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  Order_Main.Delete;
end;

procedure TfrmClothContractListHZ.TBChkOkClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex<>1 then Exit;
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('Update Contract_Main Set ChkStatus=''审核通过'',Chker='''+Trim(DName)+'''');
    sql.Add(',ChkTIme=getdate() where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  Order_Main.Delete;
end;

procedure TfrmClothContractListHZ.TBChkNOClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex<>1 then Exit;
  if cxTabControl1.TabIndex<>2 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要执行审核撤销操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('Update Contract_Main Set ChkStatus=''已送审'',Chker='''' ');
    sql.Add(',ChkTIme=Null where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  Order_Main.Delete;
end;

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

procedure TfrmClothContractListHZ.ToolButton8Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('采购加工合同',cxGrid1);
end;

end.
