unit U_LLRKList_CP;

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
  TfrmLLRKList_CP = 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;
    KHName: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    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;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label5: TLabel;
    MYColor: TEdit;
    labMYType: TLabel;
    v1MYType: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label3: TLabel;
    JCDanWei: TEdit;
    v1HWType: TcxGridDBColumn;
    N3: TMenuItem;
    v1MXHZPS: TcxGridDBColumn;
    v1MXHZKgQty: TcxGridDBColumn;
    v1MXHZQty: TcxGridDBColumn;
    N4: TMenuItem;
    v1JCDanWei: TcxGridDBColumn;
    cxGroupBox2: TcxGroupBox;
    Image3: TImage;
    ListView1: TListView;
    Panel12: TPanel;
    Label8: TLabel;
    Button4: TButton;
    Panel13: TPanel;
    Image4: TImage;
    WJName: TEdit;
    Button7: TButton;
    WJPach: TEdit;
    Button8: TButton;
    Panel4: TPanel;
    N5: TMenuItem;
    v1IFFZ: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    ImageList1: TImageList;
    v1SJKZ: TcxGridDBColumn;
    N6: TMenuItem;
    v1Ssel: TcxGridDBColumn;
    N7: TMenuItem;
    N8: TMenuItem;
    Button2: TButton;
    Button3: TButton;
    Button5: TButton;
    Button6: TButton;
    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 Tv1DblClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure Image3Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Image4Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(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
  frmLLRKList_CP: TfrmLLRKList_CP;

implementation
uses
  U_DataLink,U_RTFun, U_QCRKInPut,U_ZDYHelp,U_LLRKInPut,U_LLRKInPutMH,U_LLRKInPutX,U_LLRKMXInPut,U_Fun
  ,U_KHListSelJJ,U_LLRKInPut_CP,U_LLRKInPut_CPZS,U_GYSList;

{$R *.dfm}                                                  
procedure TfrmLLRKList_CP.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('MYID').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 TfrmLLRKList_CP.FormDestroy(Sender: TObject);
begin
  frmLLRKList_CP:=nil;
end;

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

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

procedure TfrmLLRKList_CP.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('成品入库111',Tv1,'贸易生产管理');
  Close;
end;

procedure TfrmLLRKList_CP.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      SQL.Add(',KHNameQC=(select Top 1 B.KHName from KH_Main B ');
      sql.add(' where isnull(KHFlag,'''')=''KH'' and isnull(B.Valid,'''')=''Y'' and B.KHNameJC=A.KHName)');
      sql.add(',IFFZ=(select Count(B.WBID) from TP_File B where B.WBID=A.MYID and B.TFType=''成品入库'')');
      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(' order by A.CRTime');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    TBFind.Click;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmLLRKList_CP.InitForm();
begin
  BegDate.Date:=SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('成品入库111',Tv1,'贸易生产管理');
  InitGrid();
end;

procedure TfrmLLRKList_CP.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 TfrmLLRKList_CP.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(' insert into CK_MYSC_CR_Log  ');
          sql.Add(' select * from CK_MYSC_CR where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
          sql.Add(' UPdate CK_MYSC_CR_Log Set DelTime=getdate(),DelerCode='''+Trim(DCode)+''',Deler='''+Trim(DName)+'''');
          sql.Add(' where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
          sql.Add('Delete CK_MYSC_CR  where MYId='''+Trim(Order_Main.fieldbyname('MYId').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 TfrmLLRKList_CP.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(Self.Caption,cxGrid1);
end;

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

procedure TfrmLLRKList_CP.TBAddClick(Sender: TObject);
begin
  try
    frmLLRKInPut_CP:=TfrmLLRKInPut_CP.Create(Application);
    with frmLLRKInPut_CP do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='成品';
      FMYTypeFlag:='WJCP';
      labMYType.Caption:='成品面料';
      FML:='成品';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CP.Free;
  end;
end;

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

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

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

procedure TfrmLLRKList_CP.ToolButton1Click(Sender: TObject);
var
  FFMYType:String;
begin
  try
    frmLLRKInPut_CP:=TfrmLLRKInPut_CP.Create(Application);
    with frmLLRKInPut_CP do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MYId').AsString);
      FMYType:=Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag:=Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      frmLLRKInPut_CP.TBSave.Visible:=False;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CP.Free;
  end;
end;

procedure TfrmLLRKList_CP.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 TfrmLLRKList_CP.N2Click(Sender: TObject);
var i:integer;
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生明细数据，不能删除!','提示',0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    i:=0;
    while not eof do
    begin
      if FieldByName('Ssel').AsBoolean=True then
      begin
        i:=i+1;
      end;
      next;
    end;
  end;
  Order_Main.EnableControls;
  if Application.MessageBox(PChar('确定要删除'+inttostr(i)+'条数据吗？'),'提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //Order_Main.Delete;
  end;
end;

procedure TfrmLLRKList_CP.N1Click(Sender: TObject);
var
  FFMYType:String;
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmLLRKInPut_CP:=TfrmLLRKInPut_CP.Create(Application);
    with frmLLRKInPut_CP 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);
      FML:='成品';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CP.Free;
  end;
end;

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

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

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

procedure TfrmLLRKList_CP.N3Click(Sender: TObject);
begin
  try
    frmLLRKMXInPut:=TfrmLLRKMXInPut.Create(Application);
    with frmLLRKMXInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('RKNo').AsString);
      if ShowModal=1 then
      begin
        initgrid();
      end;
    end;
  finally
    frmLLRKMXInPut.Free;
  end;
end;

procedure TfrmLLRKList_CP.N4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmLLRKInPut_CP:=TfrmLLRKInPut_CP.Create(Application);
    with frmLLRKInPut_CP do
    begin
      PState:=2;
      FMainId:=Trim(Self.Order_Main.fieldbyname('RKNo').AsString);
      FMYType:=Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag:=Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      FML:='成品';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CP.Free;
  end;
end;

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

procedure TfrmLLRKList_CP.N5Click(Sender: TObject);
begin
  cxGroupBox2.Visible:=true;
end;

procedure TfrmLLRKList_CP.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('MYID').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('MYID').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('MYID').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('MYID').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 TfrmLLRKList_CP.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('MYID').AsString)+'''');
    sql.Add('and TFType=''成品入库''');
    sql.Add('and FileName='+quotedstr(trim(fFileName)));
    execsql;
  end;
  Panel12.Visible:=False;
  Initimage();
end;

procedure TfrmLLRKList_CP.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('MYID').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('MYID').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('MYID').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('MYID').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 TfrmLLRKList_CP.Image4Click(Sender: TObject);
begin
  Panel12.Visible:=false;
end;

procedure TfrmLLRKList_CP.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 TfrmLLRKList_CP.MenuItem2Click(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('MYID').AsString)+'''');
    sql.Add('and TFType=''成品入库''');
    sql.Add('and FileName='+quotedstr(trim(fFileName)));
    execsql;
  end;
  Initimage();
end;

procedure TfrmLLRKList_CP.MenuItem3Click(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 TfrmLLRKList_CP.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 TfrmLLRKList_CP.N6Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\成品入库码单.rmf';
  RMXLSExport2:= TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    SQL.Add(',KHNameQC=(select Top 1 B.KHName from KH_Main B ');
    sql.add(' where isnull(KHFlag,'''')=''KH'' and isnull(B.Valid,'''')=''Y'' and B.KHNameJC=A.KHName)');
    sql.add(',IFFZ=(select Count(B.WBID) from TP_File B where B.WBID=A.MYID and B.TFType=''成品入库'')');
    sql.Add(' from CK_MYSC_CR A  ');
    sql.Add(' where MYId='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
    sql.Add(' and isnull(A.CRType,'''')=''成品入库'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not eof do
    begin
      if FieldByName('Ssel').AsBoolean=true then
      begin
        SSetEditDataCDSNew(Order_Main,CDS_PRT);
      end;
      next;
    end;
  end;
  Order_Main.EnableControls;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\成品入库码单.rmf'),'提示',0);
  end;
end;

procedure TfrmLLRKList_CP.N7Click(Sender: TObject);
begin
  SelOKNo(Order_Main,true);
end;

procedure TfrmLLRKList_CP.N8Click(Sender: TObject);
begin
  SelOKNo(Order_Main,false);
end;

procedure TfrmLLRKList_CP.Button2Click(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text:='坯布厂';
      KHType.TxtCode:='坯布厂';
      if ShowModal=1 then
      begin
        Self.KHName.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHName').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmLLRKList_CP.Button3Click(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='LHJCDanWei'+trim(Order_Main.fieldbyname('MYTypeFlag').AsString);
    flagName:='来货单位';
    MainType:=Trim(KHName.Text);
    if ShowModal=1 then
    begin
      JCDanWei.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    end;
    free;
  end;
end;

procedure TfrmLLRKList_CP.Button5Click(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='MLMYName'+trim(Order_Main.fieldbyname('MYTypeFlag').AsString);
    flagName:='货物品名';
    MainType:=Trim(KHName.Text);
    if ShowModal=1 then
    begin
      MYName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    end;
    free;
  end;
end;

procedure TfrmLLRKList_CP.Button6Click(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='MLMYColorNo'+trim(Order_Main.fieldbyname('MYTypeFlag').AsString);
    flagName:='色号颜色';
    fnote:=true;
    V1Note.Caption:='颜色';
    V1Name.Caption:='色号';
    MainType:=Trim(KHName.Text);
    if ShowModal=1 then
    begin
      MYColor.Text:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
    end;
    free;
  end;
end;

end.
