unit U_MCGKCList;

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;

type
  TfrmMCGKCList = 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;
    KHNameJC: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column3: TcxGridDBColumn;
    labMYType: TLabel;
    PopupMenu1: TPopupMenu;
    CheckBox1: TCheckBox;
    Button2: TButton;
    Label3: TLabel;
    JCDanWeiJC: TEdit;
    Button1: TButton;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N3: TMenuItem;
    MenuItem2: TMenuItem;
    N4: TMenuItem;
    Label2: TLabel;
    Label1: TLabel;
    MYName: TEdit;
    Note: TEdit;
    Button3: TButton;
    ImageList1: TImageList;
    KHNo: TEdit;
    JCKHNo: TEdit;
    Label5: TLabel;
    CGName: TEdit;
    ToolButton2: TToolButton;
    v1MYName: TcxGridDBColumn;
    v1MYMF: TcxGridDBColumn;
    Label7: TLabel;
    MYKZ: TEdit;
    Tv1Column1: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    N1: TMenuItem;
    N2: TMenuItem;
    Tv1Column3: TcxGridDBColumn;
    DataSource2: TDataSource;
    CDS_Sub: TClientDataSet;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DataSource3: TDataSource;
    CDS_3: TClientDataSet;
    TV3Column1: TcxGridDBColumn;
    Tv1Column4: 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 SpeedButton2Click(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 ToolButton2Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure MYNameKeyPress(Sender: TObject; var Key: Char);
  private
    canshu1, FFKHName: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure Initimage();
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmMCGKCList: TfrmMCGKCList;

implementation

uses
  U_DataLink, U_RTFun, U_QCRKInPut, U_ZDYHelp, U_LLRKInPut, U_GYSList,
  U_LLRKInPutMH_MCG, U_LLRKInPutMH, U_LLRKInPutX, U_LLRKInPut_CG,
  U_LLRKInPutMH_CG, U_BPZdy_LRM, U_LLRKInPutMH_MTHCK, U_LLRKInPutMH_MPD,
  U_CGMian;

{$R *.dfm}

procedure TfrmMCGKCList.FormDestroy(Sender: TObject);
begin
  frmMCGKCList := nil;
end;

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

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

procedure TfrmMCGKCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('棉采购出库1', Tv1, '贸易生产管理');
  Close;
end;

procedure TfrmMCGKCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_MainKC ');
      sql.Add('@WSql=' + quotedstr(Trim('where 1=1')));
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  FFKHName := '';
end;

procedure TfrmMCGKCList.InitForm();
begin
  BegDate.Date := SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  ReadCxGrid('棉采购出库1', Tv1, '贸易生产管理');
  InitGrid();
end;

procedure TfrmMCGKCList.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 TfrmMCGKCList.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=''' + trim(Order_Main.fieldbyname('MYID').AsString) + ''' ');
          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 from 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 TfrmMCGKCList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

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

procedure TfrmMCGKCList.TBAddClick(Sender: TObject);
begin
  try
    frmLLRKInPutMH_MCG := TfrmLLRKInPutMH_MCG.Create(Application);
    with frmLLRKInPutMH_MCG do
    begin
      PState := 0;
      FMainId := '';
      FSubid := '';
      FMYType := '采购棉';
      FMYTypeFlag := 'CGH';
      labMYType.Caption := '(棉)采购';
      FML := '棉';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_MCG.Free;
  end;
end;

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

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

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

procedure TfrmMCGKCList.ToolButton1Click(Sender: TObject);
begin
  try
    frmLLRKInPutMH_MCG := TfrmLLRKInPutMH_MCG.Create(Application);
    with frmLLRKInPutMH_MCG do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('RKNo').AsString);
      FSubid := Trim(Self.Order_Main.fieldbyname('MYID').AsString);
      FMYType := Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag := Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      TBSave.Visible := False;
      labMYType.Caption := '(棉)采购';
      PopupMenu1.Items.Clear;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_MCG.Free;
  end;
end;

procedure TfrmMCGKCList.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 TfrmMCGKCList.N2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  with Order_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := false;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmMCGKCList.N1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  with Order_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := true;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

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

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

procedure TfrmMCGKCList.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 TfrmMCGKCList.SpeedButton2Click(Sender: TObject);
begin
  try
    frmLLRKInPut_CG := TfrmLLRKInPut_CG.Create(Application);
    with frmLLRKInPut_CG do
    begin
      PState := 0;
      FMainId := '';
      FMYType := '采购面料';
      FMYTypeFlag := 'CGM';
      labMYType.Caption := '(面料)采购';
      FML := '面料';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CG.Free;
  end;
end;

procedure TfrmMCGKCList.SpeedButton4Click(Sender: TObject);
begin

  try
    frmLLRKInPutMH_CG := TfrmLLRKInPutMH_CG.Create(Application);
    with frmLLRKInPutMH_CG do
    begin
      PState := 0;
      FMainId := '';
      FMYType := '采购棉';
      FMYTypeFlag := 'CGH';
      labMYType.Caption := '(棉)采购';
      FML := '棉';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_CG.Free;
  end;
end;

procedure TfrmMCGKCList.SpeedButton5Click(Sender: TObject);
begin

  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 TfrmMCGKCList.SpeedButton3Click(Sender: TObject);
begin

  try
    frmLLRKInPut_CG := TfrmLLRKInPut_CG.Create(Application);
    with frmLLRKInPut_CG do
    begin
      PState := 0;
      FMainId := '';
      FMYType := '采购线';
      FMYTypeFlag := 'CGX';
      labMYType.Caption := '(线)采购';
      FML := '线';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CG.Free;
  end;
end;

procedure TfrmMCGKCList.SpeedButton6Click(Sender: TObject);
begin

  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 TfrmMCGKCList.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 TfrmMCGKCList.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 TfrmMCGKCList.Button3Click(Sender: TObject);
begin
  frmBPZDY_LRM := TfrmBPZDY_LRM.create(self);
  with frmBPZDY_LRM do
  begin
    flag := 'MName';
    flagName := '棉';
    ViewFlag := true;
    Panel4.Align := alClient;
    ToolBar3.Visible := false;
    Button1.Visible := False;
    if ShowModal = 1 then
    begin
      MYName.text := Trim(ClientDataSet1.fieldbyname('BPName').AsString);
    end;
    free;
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_MainKC ');
      sql.Add('@WSql=' + quotedstr(Trim('where MYName=''' + Trim(MYName.text) + '''')));
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  FFKHName := '';
  TBFind.Click;
end;

procedure TfrmMCGKCList.Initimage();
begin

end;

procedure TfrmMCGKCList.ToolButton2Click(Sender: TObject);
var
  FKHName, FKHNO: string;
begin
  try
    frmLLRKInPutMH_MTHCK := TfrmLLRKInPutMH_MTHCK.Create(Self);
    with frmLLRKInPutMH_MTHCK do
    begin
      PState := 0;
      FMainId := '';
      FMYType := '退货棉';
      FMYTypeFlag := 'CGH';
      labMYType.Caption := '(棉)退货';
      FML := '棉';
      frmLLRKInPutMH_MTHCK.KHName.Text := Trim(Order_Main.fieldbyname('KHName').AsString);
      frmLLRKInPutMH_MTHCK.KHNo.Text := Trim(Order_Main.fieldbyname('KHNo').AsString);
      frmLLRKInPutMH_MTHCK.MYName.Text := Trim(Order_Main.fieldbyname('MYName').AsString);
      frmLLRKInPutMH_MTHCK.MYCode.Text := Trim(Order_Main.fieldbyname('MYCode').AsString);
      frmLLRKInPutMH_MTHCK.MYKZ.Text := Trim(Order_Main.fieldbyname('MYKZ').AsString);
      frmLLRKInPutMH_MTHCK.MYMF.Text := Trim(Order_Main.fieldbyname('MYMF').AsString);
      frmLLRKInPutMH_MTHCK.GFOrderNo.Text := Trim(Order_Main.fieldbyname('GFOrderNo').AsString);
      frmLLRKInPutMH_MTHCK.PS.Text := Trim(Order_Main.fieldbyname('PS').AsString);
      frmLLRKInPutMH_MTHCK.MDJChang.Text := Trim(Order_Main.fieldbyname('MDJChang').AsString);
      frmLLRKInPutMH_MTHCK.MDKGFlag.Checked := Order_Main.fieldbyname('MDKGFlag').AsBoolean;
      frmLLRKInPutMH_MTHCK.MDMFlag.Checked := Order_Main.fieldbyname('MDMFlag').AsBoolean;
      frmLLRKInPutMH_MTHCK.HSFlag.Checked := Order_Main.fieldbyname('HSFlag').AsBoolean;
      frmLLRKInPutMH_MTHCK.BHSFlag.Checked := Order_Main.fieldbyname('BHSFlag').AsBoolean;
      frmLLRKInPutMH_MTHCK.MYMDPrice.Text := Order_Main.fieldbyname('MYMDPrice').asstring;
      frmLLRKInPutMH_MTHCK.Price.Text := floattostr(Order_Main.fieldbyname('Price').asfloat);
      frmLLRKInPutMH_MTHCK.FFZMYID := Order_Main.fieldbyname('FZMYID').asstring;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_MTHCK.Free;
  end;
end;

procedure TfrmMCGKCList.N5Click(Sender: TObject);
var
  FFMYType: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmLLRKInPutMH_MCG := TfrmLLRKInPutMH_MCG.Create(Application);
    with frmLLRKInPutMH_MCG do
    begin
      PState := 3;
      FMainId := Trim(Self.Order_Main.fieldbyname('RKNo').AsString);
      FSubid := Trim(Self.Order_Main.fieldbyname('MYID').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();
      end;
    end;
  finally
    frmLLRKInPutMH_MCG.Free;
  end;
end;

procedure TfrmMCGKCList.N6Click(Sender: TObject);
var
  FFMYType: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(Order_Main.fieldbyname('CRType').AsString) = '采购入库' then
  begin
    try
      frmLLRKInPutMH_MCG := TfrmLLRKInPutMH_MCG.Create(Application);
      with frmLLRKInPutMH_MCG do
      begin
        PState := 2;
        FMainId := Trim(Self.Order_Main.fieldbyname('RKNo').AsString);
        FSubid := Trim(Self.Order_Main.fieldbyname('MYID').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();
        end;
      end;
    finally
      frmLLRKInPutMH_MCG.Free;
    end;
  end
  else if Trim(Order_Main.fieldbyname('CRType').AsString) = '盘点入库' then
  begin
    try
      frmLLRKInPutMH_MPD := TfrmLLRKInPutMH_MPD.Create(Application);
      with frmLLRKInPutMH_MPD do
      begin
        PState := 2;
        FMainId := Trim(Self.Order_Main.fieldbyname('RKNo').AsString);
        FSubid := Trim(Self.Order_Main.fieldbyname('MYID').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();
        end;
      end;
    finally
      frmLLRKInPutMH_MPD.Free;
    end;
  end;

end;

procedure TfrmMCGKCList.ToolButton3Click(Sender: TObject);
var
  FKHName: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TbForBatch where DName=' + quotedstr(Trim(DName)));
    SQL.Add('and MainType=' + quotedstr('棉采购'));
//    ShowMessage(sql.text);
    ExecSQL;
  end;

  with Order_Main do
  begin
    First;
    while Order_Main.Locate('SSel', True, []) = True do
    begin
      if FKHName = '' then
      begin
        FKHName := Trim(Self.Order_Main.fieldbyname('KHName').AsString);
      end;
      with ADOQuery2 do
      begin
        Close;
        sql.Clear;
        sql.Add(' insert into TbForBatch(DName,MainType,Value1,Value2,PSHZ,QtyHZ,Filltime) ');
        sql.Add(' values( ' + quotedstr(Trim(DName)) + ',' + quotedstr('棉采购') + ',' + quotedstr(Trim(Order_Main.fieldbyname('MYName').AsString)) + '');
        sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('MYKZ').AsString)) + ',' + Trim(Order_Main.fieldbyname('PS').AsString) + ',' + Trim(Order_Main.fieldbyname('JHQty').AsString) + ',getdate()' + ')');
//        ShowMessage(sql.text);
        ExecSQL;
      end;

      if Trim(Self.Order_Main.fieldbyname('KHName').AsString) <> Trim(FKHName) then
      begin
        Application.MessageBox('不能选择多个供应商', '提示', 0);
        with Order_Main do
        begin
          DisableControls;
          first;
          while not eof do
          begin
            edit;
            fieldbyname('ssel').Value := FALSE;
            post;
            next;
          end;
          First;
          EnableControls;
        end;
        Exit;
      end;
      Order_Main.edit;
      Order_Main.fieldbyname('ssel').Value := FALSE;
    end;

  end;

  frmCGMian := TfrmCGMian.create(self);
  with frmCGMian do
  begin
    PState := 0;
    GYSName.text := FKHName;
//    GYSName.Enabled := false;
    if showmodal = 1 then
    begin
      InitGrid();
    end;
    free;
  end;
end;

procedure TfrmMCGKCList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('	select MYMF,LX,CRFlag,sum(PS) AS PS,JHQty=(select Sum(X.Qty)*-1 from ML_OrderSubidNew X where X.NewMLID=AA.MainID ),sum(CKQty) AS CKQty,MLOrderNo from ( ');
      sql.Add(' select MYMF,LX=''真实'',CRFlag,MainID ');
      sql.Add(' ,(CRQtyFlag*PS) AS PS,(CRQtyFlag*Qty) AS CKQty,MLOrderNo=(select MLOrderNo from ML_OrderMainNew B where B.NewMLID=A.MainID )  ');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' where 1=1  ');
      sql.Add(' and isnull(MYType,'''') in (''采购棉'',''盘点棉'') and CRFlag=''出库''  ');
      sql.Add(' and A.CRTime>=''2023-02-28''    ');
      sql.Add(' and A.MYName=''' + Trim(Order_Main.fieldbyname('MYName').AsString) + '''');
      sql.Add(' and A.MYKZ=''' + Trim(Order_Main.fieldbyname('MYKZ').AsString) + '''');
      sql.Add(' and isnull(A.KHName,'''')=''' + Trim(Order_Main.fieldbyname('KHName').AsString) + '''');
      sql.Add('  )AA GROUP BY MYMF,LX,MLOrderNo,CRFlag,MainID ');
      sql.Add(' UNION all   ');
      sql.Add(' select MYMF=B.MLMF,LX=''虚拟'',CRFlag=''出库'' ');
      sql.Add(' ,PS=0,JHQty=(select Sum(X.Qty)*-1 from ML_OrderSubidNew X where X.NewMLID=A.NewMLID and X.Filltime >=''2023-02-28''),CKQty=0,MLOrderNo   ');
      sql.Add(' from ML_OrderMainNew A INNER join ML_OrderZuHe B on A.NewMLID=B.NewMLID and MLType=''棉'' and SSGS =''自己'' and  A.Filltime >=''2023-02-28''  ');
      sql.Add(' where A.OrdDate >=''2023-02-28''   ');
      sql.Add(' and B.MLName=''' + Trim(Order_Main.fieldbyname('MYName').AsString) + '''');
      sql.Add(' and B.MLKZ=''' + Trim(Order_Main.fieldbyname('MYKZ').AsString) + '''');
      sql.Add(' and isnull(B.GYSName,'''')=''' + Trim(Order_Main.fieldbyname('KHNameJC').AsString) + '''');
      sql.Add(' and not exists(select JYID from JTJY_New X where X.NewMLID=A.NewMLID and JYType=''成品'') ');
//      ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_Sub);
    SInitCDSData20(ADOQueryTemp, CDS_Sub);
  finally
    ADOQueryTemp.EnableControls;
  end;

  try
    ADOQuery1.DisableControls;
    with ADOQuery1 do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select MYMF,LX=''真实'',CRFlag ');
      sql.Add(' ,(CRQtyFlag*PS) AS PS,(CRQtyFlag*Qty) AS RKQty,CRTime  ');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' where 1=1  ');
      sql.Add(' and isnull(MYType,'''') in (''采购棉'',''盘点棉'') and CRFlag=''入库''  ');
      sql.Add(' and A.CRTime>=''2023-02-28''    ');
      sql.Add(' and A.MYName=''' + Trim(Order_Main.fieldbyname('MYName').AsString) + '''');
      sql.Add(' and A.MYKZ=''' + Trim(Order_Main.fieldbyname('MYKZ').AsString) + '''');
      sql.Add(' and A.KHName=''' + Trim(Order_Main.fieldbyname('KHName').AsString) + '''');
//      ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS20(ADOQuery1, CDS_3);
    SInitCDSData20(ADOQuery1, CDS_3);
  finally
    ADOQuery1.EnableControls;
  end;
end;

procedure TfrmMCGKCList.MYNameKeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = #13) and (Trim(MYName.text) <> '') then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.Add(' select isnull(MYKZ,'''') AS MYKZ,isnull(MYName,'''') AS MYName,isnull(KHName,'''') AS KHName,isnull(KHNameJC,'''') AS KHNameJC,sum(PS) AS PS,Sum(Qty) AS Qty from ( ');
        sql.Add('select  AA.*,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=AA.KHName),LX=''真实库存'' ');
        sql.Add(' from ( ');
        sql.Add(' select isnull(MYKZ,'''') AS MYKZ, isnull(MYName,'''') AS MYName,isnull(KHName,'''') AS KHName,sum(CRQtyFlag*PS) AS PS,sum(CRQtyFlag*Qty) AS Qty ');
        sql.Add(' from CK_MYSC_CR A ');
        sql.Add(' where 1=1     ');
        sql.Add(' and isnull(MYType,'''') in (''采购棉'',''盘点棉'')  ');
        sql.Add(' and A.CRTime>=''2023-02-28'' ');
        sql.Add(' and A.MYName=''' + trim(MYName.text) + '''');
        sql.Add(' GROUP BY isnull(MYKZ,'''') ,isnull(MYName,''''),isnull(KHName,'''')  ');
        sql.Add(' )AA     ');

        sql.Add(' UNION all   ');

        sql.Add(' select isnull(AA.MYKZ,'''') AS MYKZ,isnull(AA.MYName,'''') AS MYName,isnull(AA.KHName,'''') AS KHName,PS=0,Qty=sum(AA.Qty),isnull(AA.KHNameJC,'''') AS KHNameJC,LX=''虚拟库存'' from (  ');
        sql.Add(' select MYKZ=B.MLKZ,MYName=B.MLName,KHName=(select Top 1 X.KHName from KH_Main X  ');
        sql.Add(' where isnull(X.KHFlag,'''')=''GYS'' and isnull(X.Valid,'''')=''Y'' and X.KHNameJC=B.GYSName),PS=0,Qty=(select Sum(X.Qty)*-1 from ML_OrderSubidNew X where X.NewMLID=A.NewMLID) ');
        sql.Add(' ,KHNameJC=B.GYSName,MLOrderNo');
        sql.Add(' from ML_OrderMainNew A INNER join ML_OrderZuHe B on A.NewMLID=B.NewMLID and MLType=''棉'' and SSGS =''自己'' --and isnull(MLName,'''')<>''''    ');
        sql.Add(' where A.OrdDate >=''2023-02-28''  ');
        sql.Add(' and B.MLName=''' + trim(MYName.text) + '''');
        sql.Add(' and not exists(select JYID from JTJY_New X where X.NewMLID=A.NewMLID and JYType=''成品'')  ');
        sql.Add(' )AA      ');
        sql.Add(' GROUP BY isnull(AA.MYKZ,''''),isnull(AA.MYName,''''),isnull(AA.KHName,''''),isnull(AA.KHNameJC,'''') ');
        sql.Add(')AAA GROUP BY isnull(MYKZ,''''),isnull(MYName,''''),isnull(KHName,''''),isnull(KHNameJC,'''')   ');
//        ShowMessage(sql.text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    FFKHName := '';
    TBFind.Click;
  end
  else if (Key = #13) and (Trim(MYName.text) = '') then
  begin
    TBRafresh.Click;
  end;
end;

end.

