unit U_YX_WorkGS;

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
  TfrmYX_WorkGS = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: 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;
    v1RZDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1RZBiaoTi: TcxGridDBColumn;
    Label4: TLabel;
    PopupMenu1: TPopupMenu;
    ToolButton1: TToolButton;
    Label12: TLabel;
    MPRTKZ: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1RZNote: TcxGridDBColumn;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    Label6: TLabel;
    DataSource3: TDataSource;
    Order_Print: TClientDataSet;
    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;
    N7: TMenuItem;
    RZBiaoTiPY: TEdit;
    RZNote: TEdit;
    Panel3: TPanel;
    v1filler: TcxGridDBColumn;
    Label1: TLabel;
    Filler: TComboBox;
    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 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 Button1Click(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 N5Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure ListView1DblClick(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
  frmYX_WorkGS: TfrmYX_WorkGS;

implementation
uses
  U_DataLink,U_MLOrderInPut,U_Fun, U_FjList_RZ,U_YX_WorkRZLR,U_YX_WorkGSLR;
{$R *.dfm}
procedure TfrmYX_WorkGS.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('RZID').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 TfrmYX_WorkGS.SetStatus();
begin
  
end;

procedure TfrmYX_WorkGS.FormDestroy(Sender: TObject);
begin
  frmYX_WorkGS:=nil;
end;

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

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

procedure TfrmYX_WorkGS.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('公司资料',Tv1,'生产指示单管理');
end;

procedure TfrmYX_WorkGS.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.add('select A.* ');
      sql.Add(',RZBiaoTiPY=dbo.getPinYin(A.RZBiaoTi)+A.RZBiaoTI');
      sql.add(',IFFZ=(select Count(B.WBID) from TP_File B where B.WBID=A.RZID and B.TFType=''公司'')');
      sql.Add(' from YX_WorkRZ A where isnull(A.SCType,'''')=''公司'' ');
      if CheckBox1.Checked=true then
      begin
        BegDate.Visible:=true;
        enddate.Visible:=true;
        sql.Add('and A.RZDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''' ');
        sql.Add('and A.RZDate<'''+Trim(FormatDateTime('yyyy-MM-dd',endDate.DateTime+1))+''' ');
      end
      else
      begin
        BegDate.Visible:=false;
        enddate.Visible:=false;
        sql.Add('and A.RZDate>='''+Trim(FormatDateTime('yyyy',BegDate.DateTime)+'-01-01')+''' ');
        sql.Add('and A.RZDate<'''+Trim(FormatDateTime('yyyy-MM-dd',endDate.DateTime+1))+''' ');
      end;
      {if canshu1<>'高权限' then
      begin
        SQL.Add(' and (A.RZRenWu='''+trim(DName)+''' or A.filler='''+trim(DName)+''')');
      end;}
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmYX_WorkGS.InitForm();
begin
  ReadCxGrid('公司资料',Tv1,'生产指示单管理');
  BegDate.DateTime:=StrToDate(FormatDateTime('yyyy',SGetServerDate10(ADOQueryTemp))+'-01-01');
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  SInitComBoxBySql(ADOQueryCmd,Filler,false,'select distinct name=filler from YX_WorkRZ');
  IF trim(DdataBase)='查询' then
  begin
    TBadd.Visible:=false;
  end;
  SetStatus();
  InitGrid();
end;

procedure TfrmYX_WorkGS.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 TfrmYX_WorkGS.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete YX_WorkRZ where RZID='''+Trim(Order_Main.fieldbyname('RZID').AsString)+'''');
      sql.Add('delete TP_File where TFType=''公司'' and WBID='''+Trim(Order_Main.fieldbyname('RZID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

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

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

procedure TfrmYX_WorkGS.TBAddClick(Sender: TObject);
begin
  try
    frmYX_WorkGSLR:=TfrmYX_WorkGSLR.Create(Application);
    with frmYX_WorkGSLR do
    begin
      PState:=0;
      FMainId:='';
      canshu1:=self.canshu1;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmYX_WorkGSLR.Free;
  end;
end;

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

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

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

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

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

procedure TfrmYX_WorkGS.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmYX_WorkGSLR:=TfrmYX_WorkGSLR.Create(Application);
    with frmYX_WorkGSLR do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('RZID').AsString);
      RZBiaoTi.Enabled:=false;
      RZNote.Enabled:=false;
      TBSave.Visible:=false;
      RZDate.Enabled:=false;
      canshu1:=self.canshu1;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmYX_WorkGSLR.Free;
  end;
end;

procedure TfrmYX_WorkGS.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_OrderMain A ');
    sql.Add('inner Join ML_OrderSubid B on A.MLID=B.MLID ');
    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_OrderMain A ');
          sql.Add('inner Join ML_OrderSubid B on A.MLID=B.MLID ');
          sql.Add('where MLID='''+Trim(Order_Main.fieldbyname('MLID').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 TfrmYX_WorkGS.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 TfrmYX_WorkGS.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('RZID').AsString)+'''');
    sql.Add(' and TFType=''公司''');
    sql.Add('and FileName='+quotedstr(trim(fFileName)));
    execsql;
  end;
  Initimage();
end;

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

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

procedure TfrmYX_WorkGS.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('RZID').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,'RZ','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('RZID').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('RZID').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('RZID').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();
    end;
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！','提示信息',0);
  end;
end;

procedure TfrmYX_WorkGS.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('RZID').AsString)+'''');
    sql.Add(' and TFType=''公司''');
    sql.Add('and FileName='+quotedstr(trim(fFileName)));
    execsql;
  end;
  Panel12.Visible:=False;
  Initimage();
end;

procedure TfrmYX_WorkGS.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('RZID').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('RZID').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('RZID').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('RZID').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 TfrmYX_WorkGS.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;
  if Trim(canshu1)<>'高权限' then
  begin
    if now-Order_Main.fieldbyname('filltime').AsDateTime>1 then
    begin
      Application.MessageBox('超过一天不能删除','提示',0);
      Exit;
    end;
  end;
  try
    frmYX_WorkGSLR:=TfrmYX_WorkGSLR.Create(Application);
    with frmYX_WorkGSLR do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('RZID').AsString);
      canshu1:=self.canshu1;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmYX_WorkGSLR.Free;
  end;
end;

procedure TfrmYX_WorkGS.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 Trim(canshu1)<>'高权限' then
  begin
    if now-Order_Main.fieldbyname('filltime').AsDateTime>1 then
    begin
      Application.MessageBox('超过一天不能删除','提示',0);
      Exit;
    end;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

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

end.
