unit U_LLRKList;

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, cxNavigator, dxSkinsCore,
  dxSkinDarkRoom, dxSkinOffice2013White, dxSkinSharpPlus, dxSkinSpringTime,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSilver,
  dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmLLRKList = 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;
    DS_Main: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    TbLook: 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;
    v1MYColor: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label5: TLabel;
    MYColor: TEdit;
    labMYType: TLabel;
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    Image2: TImage;
    v1MYType: TcxGridDBColumn;
    Label4: TLabel;
    MYType: TEdit;
    Button1: TButton;
    Panel3: TPanel;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    Image1: TImage;
    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;
    N9: TMenuItem;
    v1PJKG: TcxGridDBColumn;
    Label6: TLabel;
    Filler: TComboBox;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Ssel: TcxGridDBColumn;
    TV2MYType: TcxGridDBColumn;
    TV2KHName: TcxGridDBColumn;
    TV2PS: TcxGridDBColumn;
    TV2KgQty: TcxGridDBColumn;
    Tv2Qty: TcxGridDBColumn;
    TV2Note: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    TV3Ssel: TcxGridDBColumn;
    TV3CRTime: TcxGridDBColumn;
    TV3MYType: TcxGridDBColumn;
    TV3KHName: TcxGridDBColumn;
    TV3KHNameJC: TcxGridDBColumn;
    TV3MYName: TcxGridDBColumn;
    TV3MYColor: TcxGridDBColumn;
    TV3MYColorNo: TcxGridDBColumn;
    TV3SJKZ: TcxGridDBColumn;
    TV3Note: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DS_2: TDataSource;
    DS_3: TDataSource;
    CDS_2: TClientDataSet;
    CDS_3: TClientDataSet;
    DS_Sub: TDataSource;
    CDS_Sub: TClientDataSet;
    TV2KHNo: TcxGridDBColumn;
    TV3MLOrderNo: TcxGridDBColumn;
    TV3KHCode: TcxGridDBColumn;
    TV3PBBatchNo: TcxGridDBColumn;
    TV3GYSNo: TcxGridDBColumn;
    TV3KHNameJCDH: TcxGridDBColumn;
    TV3RCGangNo: TcxGridDBColumn;
    TV3filler: TcxGridDBColumn;
    TV3JH: TcxGridDBColumn;
    TV3JKGQTY: TcxGridDBColumn;
    TV3JMQTY: TcxGridDBColumn;
    TV3MYMF: TcxGridDBColumn;
    TV3MYKZ: TcxGridDBColumn;
    TV3RCColor: TcxGridDBColumn;
    TV3ZKZT: TcxGridDBColumn;
    TV3CDZSD: TcxGridDBColumn;
    TV3KW: TcxGridDBColumn;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridLevel3: TcxGridLevel;
    TV4RCGangNo: TcxGridDBColumn;
    TV4JH: TcxGridDBColumn;
    TV4KgQty: TcxGridDBColumn;
    TV4MQty: TcxGridDBColumn;
    ADOQuery3: TADOQuery;
    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 TbLookClick(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure SpeedButton7Click(Sender: TObject);
    procedure SpeedButton8Click(Sender: TObject);
    procedure SpeedButton9Click(Sender: TObject);
    procedure SpeedButton10Click(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 MenuItem4Click(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);
    procedure N9Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure CheckBox1Click(Sender: TObject);
  private
    canshu1: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure Initimage();
    procedure InitFiller();
    procedure InitGridByCustomer();
    procedure InitMXGrid();
    procedure InitSubGrid();
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmLLRKList: TfrmLLRKList;

implementation

uses
  U_DataLink, U_RTFun, U_QCRKInPut, U_ZDYHelp, U_LLRKInPut, U_LLRKInPutMH,
  U_LLRKInPutX, U_LLRKMXInPut, U_Fun, U_KHListSelJJ;

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

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

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

procedure TfrmLLRKList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('来料入库汇总主表', Tv1, '贸易生产管理');
  WriteCxGrid('来料入库汇总子表', TV4, '贸易生产管理');
  WriteCxGrid('来料入库客户表', TV2, '贸易生产管理');
  WriteCxGrid('来料入库明细表', TV3, '贸易生产管理');
  Close;
end;

procedure TfrmLLRKList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(',PJKG=cast((case when isnull(PS,0)<>0 then KgQty/PS else 0 end) as decimal(18,1))');
      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(C.WBID) from TP_File C where C.WBID=A.MYID and C.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,A.KHName');
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
    InitSubGrid();
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmLLRKList.InitGridByCustomer();
begin
  try
    ADOQuery1.DisableControls;
    with ADOQuery1 do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' SELECT A.KHName,A.KHCode,A.MYType,SUM(A.PS) as PS,SUM(A.Qty) AS Qty,SUM(A.KgQty) AS KgQty ');
      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(A.CRType,'''')=''来料入库'' ');
      sql.Add(' GROUP BY  A.KHName, A.KHCode, A.MYType ');
      sql.Add(' ORDER BY MIN(A.CRTime),A.KHName');
      Open;
    end;
    SDofilter(ADOQuery1, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQuery1, CDS_2);
    SInitCDSData20(ADOQuery1, CDS_2);
  finally
    ADOQuery1.EnableControls;
  end;
end;

procedure TfrmLLRKList.InitMXGrid();
begin
  try
    ADOQuery3.DisableControls;
    with ADOQuery3 do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select AA.* ');
      sql.Add(',KHNameHZ=dbo.getpinyin(isnull(AA.KHNameJC,''''))+AA.GYSNo+isnull(AA.KHNameJC,'''')');
      sql.Add('from(');
      sql.Add(' select A.*,D.KGQTY as JKGQTY ,D.MQTY as JMQTY ,D.MXID,D.CDZSD,D.Fsubid,D.CMXID,D.ZKZT,D.JH,D.KW');
      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.KHNO=A.GYSNo)');
      sql.Add(',KHNameJCDH=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHNO=A.ToGYSNo)');
      sql.Add(',KHNameHZZ=dbo.getpinyin(isnull(KM.KHNameJC,''''))+B.KHNO+isnull(KM.KHNameJC,'''')');
      sql.Add(',KHNameJCZ=KM.KHNameJC,B.MLOrderNo,KHNOKH=B.KHNO');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' left join CK_MYSC_CR_MX D on D.MYID=A.MYID');
      sql.Add(' left join ML_OrderMainNew B on A.MainId=B.NewMLID');
      sql.Add(' left join KH_Main KM on KM.KHNO=B.KHNO ');
      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(')AA');
      sql.Add(' order by AA.CRTime,AA.KHName');
//      ShowMessage(SQL.Text);
      Open;
    end;

    SDofilter(ADOQuery3, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQuery3, CDS_3);
    SInitCDSData20(ADOQuery3, CDS_3);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmLLRKList.InitSubGrid();
var
  MYID: string;
begin
  if Order_Main.IsEmpty then
    exit;

  try
    ADOQueryTemp.DisableControls;
    MYID := Order_Main.FieldByName('MYID').AsString;

    with ADOQueryTemp do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('SELECT * FROM CK_MYSC_CR_MX WHERE MYID = ''' + MYID + '''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_Sub);
    SInitCDSData20(ADOQueryTemp, CDS_Sub);
  finally
    ADOQueryTemp.EnableControls;
  end;

end;

procedure TfrmLLRKList.InitFiller();
begin
  with ADOQuery2 do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.add(' select DISTINCT A.filler AS Name');
    sql.Add(' from CK_MYSC_CR A 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,'''')=''来料入库'' ');
//    ShowMessage(SQL.Text);
    Open;
  end;

  filler.Items.Clear; //清空
  filler.Items.Add(Trim(''));

  while not ADOQuery2.eof do
  begin
    filler.Items.Add(ADOQuery2.fieldbyname('name').AsString);
    ADOQuery2.next;
  end;
end;

procedure TfrmLLRKList.InitForm();
begin
  BegDate.Date := SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);

  ReadCxGrid('来料入库汇总主表', Tv1, '贸易生产管理');
  ReadCxGrid('来料入库汇总子表', TV4, '贸易生产管理');
  ReadCxGrid('来料入库客户表', TV2, '贸易生产管理');
  ReadCxGrid('来料入库明细表', TV3, '贸易生产管理');
  
  InitGrid();
  InitFiller();
  InitGridByCustomer();
  InitMXGrid();
end;

procedure TfrmLLRKList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit
  else
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  end;

  if ADOQuery1.Active = False then
    Exit
  else
  begin
    SDofilter(ADOQuery1, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQuery1, CDS_2);
    SInitCDSData20(ADOQuery1, CDS_2);
  end;

  if ADOQuery3.Active = False then
    Exit
  else
  begin
    SDofilter(ADOQuery3, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQuery3, CDS_3);
    SInitCDSData20(ADOQuery3, CDS_3);
  end;
end;

function TfrmLLRKList.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.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

procedure TfrmLLRKList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  InitMXGrid();
  InitGridByCustomer();
end;

procedure TfrmLLRKList.TBAddClick(Sender: TObject);
begin
  Panel2.Visible := True;
end;

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

procedure TfrmLLRKList.TbLookClick(Sender: TObject);
var
  FFMYType: string;
begin
  FFMYType := Trim(Order_Main.fieldbyname('MYType').AsString);
  if Trim(FFMYType) = '面料' then
  begin
    try
      frmLLRKInPut := TfrmLLRKInPut.Create(Application);
      with frmLLRKInPut 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.TBSave.Visible := False;
        if ShowModal = 1 then
        begin
//           TBRafresh.Click;
        end;
      end;
    finally
      frmLLRKInPut.Free;
    end;
  end
  else if Trim(FFMYType) = '底布' then
  begin
    try
      frmLLRKInPut := TfrmLLRKInPut.Create(Application);
      with frmLLRKInPut 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.TBSave.Visible := False;
        FML := '底布';
        if ShowModal = 1 then
        begin
//           TBRafresh.Click;
        end;
      end;
    finally
      frmLLRKInPut.Free;
    end;
  end
  else if Trim(FFMYType) = '棉' then
  begin
    try
      frmLLRKInPutMH := TfrmLLRKInPutMH.Create(Application);
      with frmLLRKInPutMH 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.TBSave.Visible := False;
        FML := '棉';
        if ShowModal = 1 then
        begin
//           TBRafresh.Click;
        end;
      end;
    finally
      frmLLRKInPutMH.Free;
    end;
  end
  else if Trim(FFMYType) = '无纺布' then
  begin
    try
      frmLLRKInPutMH := TfrmLLRKInPutMH.Create(Application);
      with frmLLRKInPutMH 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.TBSave.Visible := False;
        FML := '无纺布';
        if ShowModal = 1 then
        begin
//           TBRafresh.Click;
        end;
      end;
    finally
      frmLLRKInPutMH.Free;
    end;
  end
  else if Trim(FFMYType) = '线' then
  begin
    try
      frmLLRKInPutX := TfrmLLRKInPutX.Create(Application);
      with frmLLRKInPutX 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.TBSave.Visible := False;
        FML := '线';
        if ShowModal = 1 then
        begin
//           TBRafresh.Click;
        end;
      end;
    finally
      frmLLRKInPutX.Free;
    end;
  end;
end;

procedure TfrmLLRKList.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit
  else
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  end;

  if ADOQuery1.Active = False then
    Exit
  else
  begin
    SDofilter(ADOQuery1, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQuery1, CDS_2);
    SInitCDSData20(ADOQuery1, CDS_2);
  end;

  if ADOQuery3.Active = False then
    Exit
  else
  begin
    SDofilter(ADOQuery3, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQuery3, CDS_3);
    SInitCDSData20(ADOQuery3, CDS_3);
  end;

end;

procedure TfrmLLRKList.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.N1Click(Sender: TObject);
var
  FFMYType: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  FFMYType := Trim(Order_Main.fieldbyname('MYType').AsString);
  if Trim(FFMYType) = '面料' then
  begin
    try
      frmLLRKInPut := TfrmLLRKInPut.Create(Application);
      with frmLLRKInPut 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);
        FML := '面料';
        if ShowModal = 1 then
        begin
          TBRafresh.Click;
        end;
      end;
    finally
      frmLLRKInPut.Free;
    end;
  end
  else if Trim(FFMYType) = '底布' then
  begin
    try
      frmLLRKInPut := TfrmLLRKInPut.Create(Application);
      with frmLLRKInPut 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);
        FML := '底布';
        if ShowModal = 1 then
        begin
          TBRafresh.Click;
        end;
      end;
    finally
      frmLLRKInPut.Free;
    end;
  end
  else if Trim(FFMYType) = '棉' then
  begin
    try
      frmLLRKInPutMH := TfrmLLRKInPutMH.Create(Application);
      with frmLLRKInPutMH 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);
        FML := '棉';
        if ShowModal = 1 then
        begin
          TBRafresh.Click;
        end;
      end;
    finally
      frmLLRKInPutMH.Free;
    end;
  end
  else if Trim(FFMYType) = '无纺布' then
  begin
    try
      frmLLRKInPutMH := TfrmLLRKInPutMH.Create(Application);
      with frmLLRKInPutMH 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);
        FML := '无纺布';
        if ShowModal = 1 then
        begin
          TBRafresh.Click;
        end;
      end;
    finally
      frmLLRKInPutMH.Free;
    end;
  end
  else if Trim(FFMYType) = '线' then
  begin
    try
      frmLLRKInPutX := TfrmLLRKInPutX.Create(Application);
      with frmLLRKInPutX 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);
        FML := '线';
        if ShowModal = 1 then
        begin
          TBRafresh.Click;
        end;
      end;
    finally
      frmLLRKInPutX.Free;
    end;
  end;
end;

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

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

procedure TfrmLLRKList.Image2Click(Sender: TObject);
begin
  Panel2.Visible := False;
end;

procedure TfrmLLRKList.SpeedButton1Click(Sender: TObject);
begin
  Panel2.Visible := False;
  try
    frmLLRKInPut := TfrmLLRKInPut.Create(Application);
    with frmLLRKInPut do
    begin
      PState := 0;
      FMainId := '';
      FMYType := '面料';
      FMYTypeFlag := 'LML';
      frmLLRKInPut.labMYType.Caption := '客户来料面料';
      FML := '面料';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut.Free;
  end;
end;

procedure TfrmLLRKList.SpeedButton2Click(Sender: TObject);
begin
  Panel2.Visible := False;
  try
    frmLLRKInPut := TfrmLLRKInPut.Create(Application);
    with frmLLRKInPut do
    begin
      PState := 0;
      FMainId := '';
      FMYType := '底布';
      FMYTypeFlag := 'LDL';
      frmLLRKInPut.labMYType.Caption := '客户来料底布';
      FML := '底布';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut.Free;
  end;
end;

procedure TfrmLLRKList.SpeedButton4Click(Sender: TObject);
begin
  Panel2.Visible := False;
  try
    frmLLRKInPutMH := TfrmLLRKInPutMH.Create(Application);
    with frmLLRKInPutMH do
    begin
      PState := 0;
      FMainId := '';
      FMYType := '棉';
      FMYTypeFlag := 'LMH';
      frmLLRKInPutMH.labMYType.Caption := '客户来料棉';
      FML := '棉';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH.Free;
  end;
end;

procedure TfrmLLRKList.SpeedButton5Click(Sender: TObject);
begin
  Panel2.Visible := False;
  try
    frmLLRKInPutMH := TfrmLLRKInPutMH.Create(Application);
    with frmLLRKInPutMH do
    begin
      PState := 0;
      FMainId := '';
      FMYType := '无纺布';
      FMYTypeFlag := 'LWF';
      frmLLRKInPutMH.labMYType.Caption := '来料无纺布';
      FML := '无纺布';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH.Free;
  end;
end;

procedure TfrmLLRKList.SpeedButton3Click(Sender: TObject);
begin
  Panel2.Visible := False;
  try
    frmLLRKInPutX := TfrmLLRKInPutX.Create(Application);
    with frmLLRKInPutX do
    begin
      PState := 0;
      FMainId := '';
      FMYType := '线';
      FMYTypeFlag := 'LX';
      frmLLRKInPutX.labMYType.Caption := '来料线';
      FML := '线';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutX.Free;
  end;
end;

procedure TfrmLLRKList.Image1Click(Sender: TObject);
begin
  panel3.Visible := false;
end;

procedure TfrmLLRKList.Button1Click(Sender: TObject);
begin
  Panel3.Visible := true;
end;

procedure TfrmLLRKList.SpeedButton6Click(Sender: TObject);
begin
  MYType.Text := '面料';
  panel3.Visible := false;
end;

procedure TfrmLLRKList.SpeedButton7Click(Sender: TObject);
begin
  MYType.Text := '底布';
  panel3.Visible := false;
end;

procedure TfrmLLRKList.SpeedButton8Click(Sender: TObject);
begin
  MYType.Text := '线';
  panel3.Visible := false;
end;

procedure TfrmLLRKList.SpeedButton9Click(Sender: TObject);
begin
  MYType.Text := '棉';
  panel3.Visible := false;
end;

procedure TfrmLLRKList.SpeedButton10Click(Sender: TObject);
begin
  MYType.Text := '无纺布';
  panel3.Visible := false;
end;

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

procedure TfrmLLRKList.N4Click(Sender: TObject);
var
  FFMYType: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  FFMYType := Trim(Order_Main.fieldbyname('MYType').AsString);
  if Trim(FFMYType) = '面料' then
  begin
    try
      frmLLRKInPut := TfrmLLRKInPut.Create(Application);
      with frmLLRKInPut do
      begin
        PState := 2;
        FMainId := Trim(Self.Order_Main.fieldbyname('MYId').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.Free;
    end;
  end
  else if Trim(FFMYType) = '底布' then
  begin
    try
      frmLLRKInPut := TfrmLLRKInPut.Create(Application);
      with frmLLRKInPut do
      begin
        PState := 2;
        FMainId := Trim(Self.Order_Main.fieldbyname('MYId').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.Free;
    end;
  end
  else if Trim(FFMYType) = '棉' then
  begin
    try
      frmLLRKInPutMH := TfrmLLRKInPutMH.Create(Application);
      with frmLLRKInPutMH do
      begin
        PState := 2;
        FMainId := Trim(Self.Order_Main.fieldbyname('MYId').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
      frmLLRKInPutMH.Free;
    end;
  end
  else if Trim(FFMYType) = '无纺布' then
  begin
    try
      frmLLRKInPutMH := TfrmLLRKInPutMH.Create(Application);
      with frmLLRKInPutMH do
      begin
        PState := 2;
        FMainId := Trim(Self.Order_Main.fieldbyname('MYId').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
      frmLLRKInPutMH.Free;
    end;
  end
  else if Trim(FFMYType) = '线' then
  begin
    try
      frmLLRKInPutX := TfrmLLRKInPutX.Create(Application);
      with frmLLRKInPutX do
      begin
        PState := 2;
        FMainId := Trim(Self.Order_Main.fieldbyname('MYId').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
      frmLLRKInPutX.Free;
    end;
  end;
end;

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

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

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

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

procedure TfrmLLRKList.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.N6Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('Ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\来料入库码单.rmf';
  RMXLSExport2 := TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(',PJKG=cast((case when isnull(PS,0)<>0 then KgQty/PS else 0 end) as decimal(18,1))');
    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.N7Click(Sender: TObject);
begin
  SelOKNo(Order_Main, true);
end;

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

procedure TfrmLLRKList.Button2Click(Sender: TObject);
begin
  try
    frmKHListSelJJ := TfrmKHListSelJJ.Create(Application);
    with frmKHListSelJJ do
    begin
      frmKHListSelJJ.canshu2 := '高权限';
      if ShowModal = 1 then
      begin
        Self.KHName.Text := Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
        //TBFind.Click;
      end;
    end;
  finally
    frmKHListSelJJ.Free;
  end;
end;

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

procedure TfrmLLRKList.N9Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('Ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\入库标签.rmf';
  RMXLSExport2 := TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(',PJKG=cast((case when isnull(PS,0)<>0 then KgQty/PS else 0 end) as decimal(18,1))');
    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.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSubGrid();
end;

procedure TfrmLLRKList.CheckBox1Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

end.

