unit U_XCGRKList_TH;

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, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxButtonEdit, cxDropDownEdit, cxPC, Buttons, cxContainer,
  cxGroupBox, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP,ShellAPI, ImgList,StrUtils, cxLookAndFeels, cxLookAndFeelPainters,
  dxSkinsCore,    
   dxSkinDarkRoom,  
    
     
    
    
    
    
   
   dxSkinOffice2013White, 
     dxSkinSharpPlus,
   dxSkinSpringTime,  
   dxSkinsDefaultPainters, 
     dxSkinscxPCPainter,
  cxNavigator;
  
type
  TfrmXCGRKList_TH = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    v1Column1: TcxGridDBColumn;
    Label34: TLabel;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    KHNameJC: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column17: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    MYName: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label1: TLabel;
    Note: TEdit;
    labMYType: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CheckBox1: TCheckBox;
    Button2: TButton;
    v1MYMDPrice: TcxGridDBColumn;
    v1MYMDMoney: TcxGridDBColumn;
    v1JCDanWeiJC: TcxGridDBColumn;
    Label3: TLabel;
    JCDanWeiJC: TEdit;
    Button1: TButton;
    v1MYColor: TcxGridDBColumn;
    v1MYColorNo: TcxGridDBColumn;
    Label4: TLabel;
    filler: TEdit;
    v1filler: TcxGridDBColumn;
    N3: TMenuItem;
    cxGroupBox2: TcxGroupBox;
    ListView1: TListView;
    Panel12: TPanel;
    Label8: TLabel;
    Button3: TButton;
    Panel13: TPanel;
    Image1: TImage;
    WJName: TEdit;
    Button7: TButton;
    WJPach: TEdit;
    Button8: TButton;
    Panel3: TPanel;
    IdFTP1: TIdFTP;
    ImageList1: TImageList;
    Image3: TImage;
    v1CIFFJ: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    N4: TMenuItem;
    Panel4: TPanel;
    Image4: TImage;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    v1MYCode: TcxGridDBColumn;
    KHNo: TEdit;
    JCKHNo: TEdit;
    v1GFOrderNo: TcxGridDBColumn;
    CGName: TEdit;
    Label5: TLabel;
    v1Column4: 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 TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure Image3Click(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure Image4Click(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure Initimage();
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmXCGRKList_TH: TfrmXCGRKList_TH;

implementation
uses
  U_DataLink,U_RTFun, U_QCRKInPut,U_ZDYHelp,U_LLRKInPut,U_GYSList,U_LLRKInPutMH_MCG,U_LLRKInPutMH_DXCG,
  U_LLRKInPutMH,U_LLRKInPutX,U_LLRKInPut_CG,U_LLRKInPutMH_CG,U_BPZdy_LRX,U_LLRKInPutMH_XTH;

{$R *.dfm}

procedure TfrmXCGRKList_TH.FormDestroy(Sender: TObject);
begin
  frmXCGRKList_TH:=nil;
end;

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

procedure TfrmXCGRKList_TH.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  canshu1:=Trim(DParameters1);
end;

procedure TfrmXCGRKList_TH.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('线采购入库2',Tv1,'贸易生产管理');
  Close;
end;

procedure TfrmXCGRKList_TH.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.add(',IFFZ=(select Count(B.WBID) from TP_File B where B.WBID=A.RKNo and B.TFType=''采购线'')');
      sql.Add(',KHNameJC=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.KHName)');
      sql.Add(',JCDanWeiJC=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.JCDanWei)');
      sql.Add(',KHNo=(select Top 1 B.KHNo from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.KHName)');
      sql.Add(',JCKHNo=(select Top 1 B.KHNo from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.JCDanWei)');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' where 1=1 ');
      if CheckBox1.Checked=False then
      begin
        sql.Add(' and A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',now))+'''');
        sql.Add(' and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',now+1))+'''');
        BegDate.Visible:=false;
        EndDate.Visible:=false;
      end
      else
      begin
        sql.Add(' and A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
        BegDate.Visible:=true;
        EndDate.Visible:=true;
      end;
      sql.Add(' and isnull(CRType,'''')=''采购入库'' ');
      SQL.Add(' and isnull(MYType,'''')=''采购线''');
      sql.add(' order by CRTime,MYColorNo');
      //ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    TBFind.Click;
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmXCGRKList_TH.InitForm();
begin
  BegDate.Date:=SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('线采购入库2',Tv1,'贸易生产管理');
  InitGrid();
end;

procedure TfrmXCGRKList_TH.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 TfrmXCGRKList_TH.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' delete from YS_Money_CR where YFTypeId in (select B.MYID ');
          SQL.Add(' from CK_MYSC_CR B where B.RKNO='''+trim(Order_Main.fieldbyname('RKNO').AsString)+''') ');
          sql.Add(' delete from TP_File where WBID='''+trim(Order_Main.fieldbyname('RKNO').AsString)+''' ');
          sql.Add(' and TFType=''采购线''');
          sql.Add(' insert into CK_MYSC_CR_Log  ');
          sql.Add(' select * from CK_MYSC_CR where RKNO='''+Trim(Order_Main.fieldbyname('RKNO').AsString)+'''');
          sql.Add(' UPdate CK_MYSC_CR_Log Set DelTime=getdate(),DelerCode='''+Trim(DCode)+''',Deler='''+Trim(DName)+'''');
          sql.Add(' where RKNO='''+Trim(Order_Main.fieldbyname('RKNO').AsString)+'''');
          sql.Add(' Delete CK_MYSC_CR  where RKNO='''+Trim(Order_Main.fieldbyname('RKNO').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmXCGRKList_TH.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(Self.Caption,cxGrid1);
end;

procedure TfrmXCGRKList_TH.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  TBFind.Click;
end;

procedure TfrmXCGRKList_TH.TBAddClick(Sender: TObject);
begin
  Panel4.Visible:=true;
end;

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

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

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

procedure TfrmXCGRKList_TH.ToolButton1Click(Sender: TObject);
var
  FFMYType:String;
begin
  if Order_Main.IsEmpty then Exit;
  if Self.Order_Main.fieldbyname('MYTypeFlag').AsString='CGDX' then
  begin
    frmLLRKInPutMH_DXCG:=TfrmLLRKInPutMH_DXCG.Create(Application);
    with frmLLRKInPutMH_DXCG do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('RKNo').AsString);
      FMYType:=Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag:=Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      TBSave.Visible:=False;
      labMYType.Caption:='(底线)入库单';
      Panel5.Visible:=false;
      PopupMenu1.Items.Clear;
      if ShowModal=1 then
      begin
        InitGrid();
        Order_Main.Locate('RKNo',FMainId,[]);
      end;
      Free;
    end;
  end
  else
  begin
    frmLLRKInPutMH_XTH:=TfrmLLRKInPutMH_XTH.Create(Application);
    with frmLLRKInPutMH_XTH do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('RKNo').AsString);
      FMYType:=Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag:=Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      labMYType.Caption:='(面线)入库单';
      TBSave.Visible:=False;
      Panel5.Visible:=false;
      PopupMenu1.Items.Clear;
      if ShowModal=1 then
      begin
        InitGrid();
        Order_Main.Locate('RKNo',FMainId,[]);
      end;
      Free;
    end;
  end;
end;

procedure TfrmXCGRKList_TH.CustomerNoNameChange(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 TfrmXCGRKList_TH.N2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR A where A.YFTypeId in (select B.MYID ');
    SQL.Add(' from CK_MYSC_CR B where B.RKNO='''+trim(Order_Main.fieldbyname('RKNO').AsString)+''')');
    sql.Add(' and isnull(ChkStatusP,'''')=''已审核'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=false then
  begin
    Application.MessageBox('应付款已审核,不能操作','提示');
    exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //Order_Main.Delete;
  end;
end;

procedure TfrmXCGRKList_TH.N1Click(Sender: TObject);
var
  FFMYType:String;
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR A where A.YFTypeId in (select B.MYID ');
    SQL.Add(' from CK_MYSC_CR B where B.RKNO='''+trim(Order_Main.fieldbyname('RKNO').AsString)+''')');
    sql.Add(' and isnull(ChkStatusP,'''')=''已审核'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=false then
  begin
    Application.MessageBox('应付款已审核,不能操作','提示');
    exit;
  end;
  if Self.Order_Main.fieldbyname('MYTypeFlag').AsString='CGDX' then
  begin
    frmLLRKInPutMH_DXCG:=TfrmLLRKInPutMH_DXCG.Create(Application);
    with frmLLRKInPutMH_DXCG do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('RKNo').AsString);
      FMYType:=Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag:=Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      labMYType.Caption:='(底线)入库单';
      Panel5.Visible:=false;
      if ShowModal=1 then
      begin
        InitGrid();
        Order_Main.Locate('RKNo',FMainId,[]);
      end;
      Free;
    end;
  end
  else
  begin
    frmLLRKInPutMH_XTH:=TfrmLLRKInPutMH_XTH.Create(Application);
    with frmLLRKInPutMH_XTH do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('RKNo').AsString);
      FMYType:=Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag:=Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      labMYType.Caption:='(面线)入库单';
      Panel5.Visible:=false;
      if ShowModal=1 then
      begin
        InitGrid();
        Order_Main.Locate('RKNo',FMainId,[]);
      end;
      Free;
    end;
  end;
end;

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

procedure TfrmXCGRKList_TH.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmXCGRKList_TH.ToolButton4Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('Chker').AsString)='' then exit;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\检测报告.rmf';
  RMXLSExport2:= TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Test_Report where TRId='''+Trim(Order_Main.fieldbyname('TRId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['OrderUnit']:=Order_Main.fieldbyname('OrderUnit').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检测报告.rmf'),'提示',0);
  end;
end;

procedure TfrmXCGRKList_TH.Tv1DblClick(Sender: TObject);
begin
  if Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName='IFFZ' then
  begin
    cxGroupBox2.Visible:=true;
    Initimage();
  end
  else
  ToolButton1.Click;
end;

procedure TfrmXCGRKList_TH.SpeedButton4Click(Sender: TObject);
begin
  Panel4.Visible:=False;
  try
    frmLLRKInPutMH_DXCG:=TfrmLLRKInPutMH_DXCG.Create(Application);
    with frmLLRKInPutMH_DXCG do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='采购线';
      FMYTypeFlag:='CGDX';
      labMYType.Caption:='(底线)入库单';
      FML:='底线';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_DXCG.Free;
  end;
end;

procedure TfrmXCGRKList_TH.SpeedButton5Click(Sender: TObject);
begin
  Panel4.Visible:=False;
  try
    frmLLRKInPutMH_CG:=TfrmLLRKInPutMH_CG.Create(Application);
    with frmLLRKInPutMH_CG do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='采购无纺布';
      FMYTypeFlag:='CGW';
      labMYType.Caption:='(无纺布)采购';
      FML:='无纺布';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_CG.Free;
  end;
end;

procedure TfrmXCGRKList_TH.SpeedButton3Click(Sender: TObject);
begin
  Panel4.Visible:=False;
  try
    frmLLRKInPutMH_XTH:=TfrmLLRKInPutMH_XTH.Create(Application);
    with frmLLRKInPutMH_XTH do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='退货线';
      FMYTypeFlag:='CGMX';
      labMYType.Caption:='(面线)退货单';
      FML:='面线';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_XTH.Free;
  end;
end;

procedure TfrmXCGRKList_TH.SpeedButton6Click(Sender: TObject);
begin
  Panel4.Visible:=False;
  try
    frmLLRKInPut_CG:=TfrmLLRKInPut_CG.Create(Application);
    with frmLLRKInPut_CG do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='采购成品';
      FMYTypeFlag:='CGC';
      labMYType.Caption:='采购(成品)';
      FML:='成品';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CG.Free;
  end;
end;

procedure TfrmXCGRKList_TH.Button2Click(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text:='线厂';
      KHType.TxtCode:='线厂';
      if ShowModal=1 then
      begin
        Self.KHNameJC.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
        TBFind.Click;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmXCGRKList_TH.Button1Click(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text:='绣花厂';
      KHType.TxtCode:='绣花厂';
      if ShowModal=1 then
      begin
        Self.JCDanWeiJC.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
        TBFind.Click;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmXCGRKList_TH.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('RKNo').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 TfrmXCGRKList_TH.Button3Click(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('RKNo').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;
      Panel4.Caption:='正在上传数据，请稍等...';
      Panel4.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('RKNo').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('RKNo').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('RKNo').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;
      Panel4.Visible:=false;
      Initimage();
    end;
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！','提示信息',0);
  end;
end;

procedure TfrmXCGRKList_TH.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('RKNo').AsString)+'''');
    sql.Add('and TFType=''采购线''');
    sql.Add('and FileName='+quotedstr(trim(fFileName)));
    execsql;
  end;
  Panel12.Visible:=False;
  Initimage();

end;

procedure TfrmXCGRKList_TH.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('RKNo').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;
    Panel4.Caption:='正在上传数据，请稍等...';
    Panel4.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('RKNo').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('RKNo').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('RKNo').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;
    Panel4.Visible:=false;
    Initimage();
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！','提示信息',0);
  end;
end;

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

procedure TfrmXCGRKList_TH.N3Click(Sender: TObject);
begin
  cxGroupBox2.Visible:=true;
end;

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

procedure TfrmXCGRKList_TH.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

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

procedure TfrmXCGRKList_TH.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;
    Button2.Visible:=True;
    Button7.Visible:=True;
    Panel12.Refresh;
  end;
end;

procedure TfrmXCGRKList_TH.MenuItem2Click(Sender: TObject);
var
  fFileName:string;
begin
  if listView1.SelCount<1 then exit;
  try
    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('RKNo').AsString)+'''');
      sql.Add('and TFType=''采购线''');
      sql.Add('and FileName='+quotedstr(trim(fFileName)));
      execsql;
    end;
    Initimage();
  except
  end;
end;

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

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

procedure TfrmXCGRKList_TH.Image4Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

end.
