unit U_MLMange_CX;

interface

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

type
  TfrmMLMange_CX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryTree: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Panel1: TPanel;
    Label2: TLabel;
    MLNo: TEdit;
    ToolButton2: TToolButton;
    Label1: TLabel;
    MLName: TEdit;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    TVBPName: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid4: TcxGrid;
    TV3: TcxGridDBTableView;
    V2BPSeHao: TcxGridDBColumn;
    V2BPCol: TcxGridDBColumn;
    V2CBPColEng: TcxGridDBColumn;
    V2BPNote: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    DataSource3: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Order_GX: TClientDataSet;
    DataSource4: TDataSource;
    cxGridPopupMenu3: TcxGridPopupMenu;
    ML_Col: TClientDataSet;
    DataSource5: TDataSource;
    cxGridPopupMenu4: TcxGridPopupMenu;
    CDS_Sub: TClientDataSet;
    V2XHNo: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    V2BPGYSName: TcxGridDBColumn;
    V2BPPrice: TcxGridDBColumn;
    Panel4: TPanel;
    cxGridPopupMenu5: TcxGridPopupMenu;
    DataSource6: TDataSource;
    Ord_HX: TClientDataSet;
    cxGrid5: TcxGrid;
    tv5: TcxGridDBTableView;
    v2ZDYCode: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2defstr3: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    ADOQuery1: TADOQuery;
    tv5Ssel: TcxGridDBColumn;
    V3Ssel: TcxGridDBColumn;
    Panel6: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1GSNo: TcxGridDBColumn;
    v1GYName: TcxGridDBColumn;
    v1MLNo: TcxGridDBColumn;
    v1MLName: TcxGridDBColumn;
    v1MLMF: TcxGridDBColumn;
    v1MLKZ: TcxGridDBColumn;
    v1MLType: TcxGridDBColumn;
    v1MLDLNo: TcxGridDBColumn;
    v1GYLX: TcxGridDBColumn;
    v1GYLXName: TcxGridDBColumn;
    v1DLHXK: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel3: TPanel;
    Label8: TLabel;
    Panel5: TPanel;
    cxGrid2: TcxGrid;
    TV4: TcxGridDBTableView;
    V1Ssel: TcxGridDBColumn;
    V1BPCode: TcxGridDBColumn;
    V1BPName: TcxGridDBColumn;
    V1BPMF: TcxGridDBColumn;
    V1BPKZ: TcxGridDBColumn;
    V1BPJS: TcxGridDBColumn;
    V1BPWS: TcxGridDBColumn;
    v1BPMiDu: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel7: TPanel;
    Label3: TLabel;
    Panel8: TPanel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure MLNoChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure TFHClick(Sender: TObject);
    procedure ToZHADDClick(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure TJSClick(Sender: TObject);
    procedure TV4CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }
    PState:Integer;
    FTopID:String;
    procedure InitTree();
    procedure InitGrid();
    procedure ReadINIFile();

  public
    FMXCPID:string;
    { Public declarations }
  end;

var
  frmMLMange_CX: TfrmMLMange_CX;

implementation
uses
  U_DataLink,U_Fun,U_CPAdd,U_FileUp,U_MLManage_LR,U_BPZdy_LR,U_BPZdy_SH,U_MLManage_FHLR,U_MLManage_ZHLR,U_BPZdy_HXK;

{$R *.dfm}

procedure TfrmMLMange_CX.InitTree();
begin
  
end;

procedure TfrmMLMange_CX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from ML_Manage ');
      sql.Add('where MLID='''+trim(FMXCPID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMLMange_CX.FormDestroy(Sender: TObject);
begin
  frmMLMange_CX:=nil;
end;

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

procedure TfrmMLMange_CX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('客户拿样',Tv1,'样品管理');
  if  DirectoryExists(ExtractFileDir('E:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209',sw_hide);
  Close;
end;
procedure TfrmMLMange_CX.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete ML_Manage where MLID='''+Trim(CDS_Main.fieldbyname('MLID').AsString)+'''');
    sql.Add('delete ML_Manage_PB where MLID='''+Trim(CDS_Main.fieldbyname('MLID').AsString)+'''');
    sql.Add('delete ML_Manage_GX where MLID='''+Trim(CDS_Main.fieldbyname('MLID').AsString)+'''');
    ExecSQL;
  end;
  CDS_Main.Delete;
end;

procedure TfrmMLMange_CX.FormShow(Sender: TObject);
begin
  InitGrid();
  ReadCxGrid('样品列表',Tv1,'样品管理');
end;

procedure TfrmMLMange_CX.cxDBTreeList1DblClick(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmMLMange_CX.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmMLMange_CX.MLNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmMLMange_CX.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel('客户拿样',cxGrid1);
end;

procedure TfrmMLMange_CX.ReadINIFile();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

procedure TfrmMLMange_CX.ToolButton4Click(Sender: TObject);
begin
  frmBPZDY_LR:=TfrmBPZDY_LR.Create(self);
  with frmBPZDY_LR do
  begin
    flag:='PBName';
    flagName:='坯布';
    Panel2.Visible:=false;
    cxGrid3.Visible:=false;
    cxGrid1.Align:=alClient;
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmMLMange_CX.ToolButton5Click(Sender: TObject);
begin
  frmBPZDY_LR:=TfrmBPZDY_LR.Create(self);
  with frmBPZDY_LR do
  begin
    flag:='GXName';
    flagName:='工序';
    V1BPMF1.Visible:=false;
    V1BPKZ.Visible:=false;
    V1BPJS.Visible:=false;
    //V1BPWS.Visible:=false;
    v1BPMiDu.Visible:=false;
    cxGrid3.Visible:=false;
    V2BPGYSName.Caption:='编号';
    V2BPPrice.Caption:='名称';
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmMLMange_CX.ToolButton6Click(Sender: TObject);
begin
  frmBPZDY_LR:=TfrmBPZDY_LR.Create(self);
  with frmBPZDY_LR do
  begin
    flag:='MHName';
    flagName:='棉花';
    V1BPJS.Visible:=false;
    //V1BPWS.Visible:=false;
    v1BPMiDu.Visible:=false;
    cxGrid3.Visible:=false;
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmMLMange_CX.ToolButton7Click(Sender: TObject);
begin
  frmBPZDY_LR:=TfrmBPZDY_LR.Create(self);
  with frmBPZDY_LR do
  begin
    flag:='XXName';
    flagName:='绣线';
    V1BPJS.Visible:=false;
    //V1BPWS.Visible:=false;
    v1BPMiDu.Caption:='颜色';
    V1BPMF1.Caption:='供应商';
    V1BPKZ.Visible:=false;
    Panel2.Visible:=false;
    cxGrid3.Visible:=false;
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmMLMange_CX.ToolButton8Click(Sender: TObject);
begin
  frmBPZDY_LR:=TfrmBPZDY_LR.Create(self);
  with frmBPZDY_LR do
  begin
    flag:='WFBDY';
    flagName:='无纺布';
    V1BPMF1.Visible:=false;
    V1BPKZ.Visible:=false;
    V1BPJS.Visible:=false;
    //V1BPWS.Visible:=false;
    v1BPMiDu.Visible:=false;
    cxGrid3.Visible:=false;
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmMLMange_CX.ToolButton9Click(Sender: TObject);
begin
  frmBPZDY_SH:=TfrmBPZDY_SH.Create(self);
  with frmBPZDY_SH do
  begin
    flag:='SHK';
    flagName:='色号库';
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmMLMange_CX.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var FMLDLNo,FDLHXK:string;
begin
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,isnull(B.BPCode,C.MLNo) BPCode,isnull(B.BPName,C.MLName) BPName');
    sql.Add(' ,isnull(B.BPMF,C.MLMF) BPMF,isnull(B.BPKZ,C.MLKZ) BPKZ');
    sql.Add(',B.BPJS,B.BPWS,B.BPMiDu,B.BPUnit,C.MLDLNo,C.DLHXK');
    SQL.Add(' from ML_Manage_PB A left join BP_ZDY B on B.BPID=A.BPID ');
    sql.Add(' left join ML_Manage C on C.MLID=A.BPID');
    sql.Add(' where A.MLID='''+Trim(CDS_Main.fieldbyname('MLID').AsString)+'''');
    //ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,CDS_Sub);
  SInitCDSData20(ADOQueryCmd,CDS_Sub);
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.*,C.* from ML_Manage_GX A inner join BP_ZDY B on B.BPID=A.BPID ');
    sql.Add('inner join BP_ZDY_Sub C on C.BPSubID=A.BPSubID');
    sql.Add(' where A.MLID='''+Trim(CDS_Main.fieldbyname('MLID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,Order_GX);
  SInitCDSData20(ADOQueryCmd,Order_GX);
  //if CDS_Main.FieldByName('MLType').AsString<>'复合' then
  if CDS_Main.fieldbyname('MLDLNo').AsString='' then
  FMLDLNo:='无色号'
  else
  FMLDLNo:=CDS_Main.fieldbyname('MLDLNo').AsString;
  if CDS_Main.fieldbyname('DLHXK').AsString='' then
  FDLHXK:='无花型'
  else
  FDLHXK:=CDS_Main.fieldbyname('DLHXK').AsString;
  begin
    //if CDS_Main.FieldByName('MLType').AsString<>'组合' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from BP_ZDY_Sub ');
        sql.Add('where BPCode='''+Trim(FMLDLNo)+''' and BPflag=''SHK''');
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryCmd,ML_Col);
      SInitCDSData20(ADOQueryCmd,ML_Col);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select A.*,B.* from BP_ZDY_Sub A ');
      Sql.Add('inner join KH_Zdy_Attachment B on A.HXATID=B.ATID and B.Type=''CPHX''');
      sql.Add(' where A.BPCode='''+trim(FDLHXK)+'''');
      open;
    end;
    SCreateCDS20(ADOQueryCmd,Ord_HX);
    SInitCDSData20(ADOQueryCmd,Ord_HX);
  end;
end;

procedure TfrmMLMange_CX.TFHClick(Sender: TObject);
begin
  try
    frmMLManage_FHLR:=TfrmMLManage_FHLR.Create(Application);
    with frmMLManage_FHLR do
    begin
      FCYID:='';
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmMLManage_FHLR.Free;
  end;
end;

procedure TfrmMLMange_CX.ToZHADDClick(Sender: TObject);
begin
  frmMLManage_ZHLR:=TfrmMLManage_ZHLR.Create(self);
  with frmMLManage_ZHLR do
  begin
    FCYID:='';
    if ShowModal=1 then
    begin
      Self.InitGrid();
    end;
    free;
  end;
end;

procedure TfrmMLMange_CX.ToolButton10Click(Sender: TObject);
begin
  {if RadioGroup1.ItemIndex=-1 then
  begin
    application.MessageBox('请选择工序','提示');
    exit;
  end;}
  frmBPZDY_HXK:=TfrmBPZDY_HXK.Create(self);
  with frmBPZDY_HXK do
  begin
    //flag:='HXK'+inttostr(RadioGroup1.ItemIndex);
    //flagName:=RadioGroup1.Items.Strings[RadioGroup1.ItemIndex]+'花型库';
    flag:='HXK';
    flagName:='花型库';
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmMLMange_CX.TJSClick(Sender: TObject);
begin
  frmBPZDY_LR:=TfrmBPZDY_LR.Create(self);
  with frmBPZDY_LR do
  begin
    flag:='JSName';
    flagName:='胶水';
    Panel2.Visible:=false;
    cxGrid3.Visible:=false;
    cxGrid1.Align:=alClient;
    V1BPJS.Visible:=false;
    v1BPMiDu.Visible:=false;
    V1BPKZ.Visible:=false;
    V1BPMF1.Visible:=false;
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmMLMange_CX.TV4CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var FMLDLNo,FDLHXK,FCode:String;
begin
  //if CDS_Main.FieldByName('MLType').AsString='复合' then
  begin
    with ADOQuery1 do
    begin
      Close;
      SQL.Clear;
      sql.Add('select A.* from BP_ZDY_Sub A inner join ML_Manage B on B.MLDLNo=A.BPCode and B.GYType=''染色''');
      sql.Add(' where '''+Trim(CDS_Sub.fieldbyname('BPCode').AsString)+''' like ''%''+B.MLNo+''%'' and A.BPflag=''SHK''');
      //ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQuery1,ML_Col);
    SInitCDSData20(ADOQuery1,ML_Col);
    with ADOQuery1 do
    begin
      Close;
      SQL.Clear;
      sql.Add('select A.* ');
      sql.Add(',IFTP=(case when isnull((select Top 1 B.CYID from XD_File B where B.CYID=A.BPSubID),'''')='''' then '''' else ''是'' end)');
      sql.Add(' from BP_ZDY_Sub A inner join ML_Manage B on B.DLHXK=A.BPCode and B.GYType=''印花''');
      sql.Add(' where '''+trim(CDS_Sub.fieldbyname('BPCode').AsString)+''' like ''%''+B.MLNo+''%'' and A.BPflag=''HXK''');
      //ShowMessage(sql.Text);
      open;
    end;
    SCreateCDS20(ADOQuery1,Ord_HX);
    SInitCDSData20(ADOQuery1,Ord_HX);
  end;
end;

end.
