unit U_MPZKCListNew;

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
  TfrmMPZKCListNew = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    CheckBox1: TCheckBox;
    Label3: TLabel;
    JCDanWeiJC: TEdit;
    Button1: TButton;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N3: TMenuItem;
    MenuItem2: TMenuItem;
    N4: TMenuItem;
    Label2: TLabel;
    Label1: TLabel;
    Note: TEdit;
    ImageList1: TImageList;
    KHNo: TEdit;
    JCKHNo: TEdit;
    Label5: TLabel;
    CGName: TEdit;
    v1MYName: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    N1: TMenuItem;
    N2: TMenuItem;
    DataSource2: TDataSource;
    CDS_Sub: TClientDataSet;
    DataSource3: TDataSource;
    CDS_3: TClientDataSet;
    Tv1Column4: TcxGridDBColumn;
    MYName: TComboBox;
    ToolButton1: TToolButton;
    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 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);
    procedure MYNameChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    canshu1, FFKHName: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure Initimage();
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    FCode, FName, FKZ, FGYS: string;
    FMianQty: Double;

    { Public declarations }
  end;

var
  frmMPZKCListNew: TfrmMPZKCListNew;

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, U_MCGKCListNew, U_MCP;

{$R *.dfm}

procedure TfrmMPZKCListNew.FormDestroy(Sender: TObject);
begin
  frmMPZKCListNew := nil;
end;

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

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

procedure TfrmMPZKCListNew.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('棉品名库存', Tv1, '棉仓库');
  Close;
end;

procedure TfrmMPZKCListNew.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_MainKC1 ');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  FFKHName := '';

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select distinct(isnull(MYName,'''')) AS name  from CK_MYSC_CR  where isnull(MYType,'''') in (''采购棉'',''盘点棉'') ');
    sql.Add(' and CRTime>=''2023-06-01'' and CRFLag=''入库''     ');
    Open;
  end;
  MYName.Items.Clear; //清空
  MYName.Items.Add(Trim(''));
  while not ADOQueryCmd.eof do
  begin
    MYName.Items.Add(ADOQueryCmd.fieldbyname('name').AsString);
    ADOQueryCmd.next;
  end;

end;

procedure TfrmMPZKCListNew.InitForm();
begin
  BegDate.Date := SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  ReadCxGrid('棉品名库存', Tv1, '棉仓库');
  InitGrid();
end;

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

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

procedure TfrmMPZKCListNew.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 TfrmMPZKCListNew.FormShow(Sender: TObject);
begin
  InitForm();
end;

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

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

procedure TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.Initimage();
begin

end;

procedure TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.ToolButton3Click(Sender: TObject);
var
  FKHName, GYSMName, CPMF: string;
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.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 CDS_Sub do
  begin
    First;
    while CDS_Sub.Locate('SSel', True, []) = True do
    begin
      if FKHName = '' then
      begin
        FKHName := Trim(Self.CDS_Sub.fieldbyname('KHNameJC').AsString);
      end;

      with ADOQuery2 do
      begin
        Close;
        sql.Clear;
        sql.Add(' select top 1 CPName,BPMF from M_LX A INNER JOIN M_ZDY B on A.MLXID=B.CPID INNER JOIN M_GYS C on A.GYSID =C.GYSID ');
        sql.Add(' where C.GYSJC=''' + Trim(Self.CDS_Sub.fieldbyname('KHNameJC').AsString) + '''');
        sql.Add(' and KHMName =''' + Trim(Self.Order_Main.fieldbyname('MYName').AsString) + '''');
        sql.Add(' and BPKZ =''' + Trim(Self.Order_Main.fieldbyname('MYKZ').AsString) + '''');
//        ShowMessage(sql.text);
        open;
      end;
      GYSMName := Trim(ADOQuery2.fieldbyname('CPName').AsString);
      CPMF := Trim(ADOQuery2.fieldbyname('BPMF').AsString);

      with ADOQuery2 do
      begin
        Close;
        sql.Clear;
        sql.Add(' insert into TbForBatch(DName,MainType,Value1,Value2,Value3,Value4,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)) + ',' + quotedstr(Trim(GYSMName)) + ',' + quotedstr(Trim(CPMF)) + ',' + Trim(CDS_Sub.fieldbyname('PS').AsString) + ',' + Trim(CDS_Sub.fieldbyname('Qty').AsString) + ',getdate()' + ')');
//        ShowMessage(sql.text);
        ExecSQL;
      end;

      if Trim(Self.CDS_Sub.fieldbyname('KHNameJC').AsString) <> Trim(FKHName) then
      begin
        Application.MessageBox('不能选择多个供应商', '提示', 0);
        with CDS_Sub do
        begin
          DisableControls;
          first;
          while not eof do
          begin
            edit;
            fieldbyname('ssel').Value := FALSE;
            post;
            next;
          end;
          First;
          EnableControls;
        end;
        Exit;
      end;
      CDS_Sub.edit;
      CDS_Sub.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 TfrmMPZKCListNew.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_MianKC_MXNew ');
      sql.Add(' @MYKZ=' + quotedstr(Trim(Order_Main.fieldbyname('MYKZ').AsString)));
      sql.Add(',@MYName=' + quotedstr(Trim(Order_Main.fieldbyname('MYName').AsString)));
//      ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_Sub);
    SInitCDSData20(ADOQueryTemp, CDS_Sub);
  finally
    ADOQueryTemp.EnableControls;
  end;

end;

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

procedure TfrmMPZKCListNew.MYNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmMPZKCListNew.Tv1DblClick(Sender: TObject);
begin
  try
    frmMCGKCListNew := TfrmMCGKCListNew.Create(Self);
    with frmMCGKCListNew do
    begin
      FMYName := Trim(self.Order_Main.fieldbyname('MYName').AsString);
      Label2.Caption := Trim(self.Order_Main.fieldbyname('MYName').AsString);
      if ShowModal = 1 then
      begin
        FName := Trim(self.Order_Main.fieldbyname('MYName').AsString);
        FCode := Trim(frmMCGKCListNew.Order_Main.fieldbyname('MYCode').AsString);
        FKZ := Trim(frmMCGKCListNew.Order_Main.fieldbyname('MYKZ').AsString);
        FGYS := Trim(frmMCGKCListNew.CDS_Sub.fieldbyname('KHNameJC').AsString);
        self.ModalResult := 1;
      end;
    end;
  finally
    frmMCGKCListNew.Free;
  end;
end;

procedure TfrmMPZKCListNew.ToolButton1Click(Sender: TObject);
var
  maxno, FBPID: string;
begin
  frmMCP := TfrmMCP.create(self);
  with frmMCP do
  begin
    flag := 'MName';
    flagName := '棉';
    ViewFlag := true;
    Panel4.Align := alClient;
    ToolBar3.Visible := false;
    ToolBar2.Enabled := false;
    Button1.Visible := False;
    if ShowModal = 1 then
    begin

      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add(' select * from M_ZDY A INNER JOIN M_LX B on A.CPID=B.MLXID INNER JOIN M_GYS C on B.GYSID =C.GYSID left JOIN KH_Main D on C.GYSNO =D.KHNO');
        sql.Add(' where A.BPID =''' + Trim(ClientDataSet1.fieldbyname('BPID').AsString) + '''');
        open;
      end;

      GetLSNo(ADOQueryCmd, maxno, 'MH', 'CK_MYSC_CR', 3, 1);
      with ADOQueryTemp do
      begin
        close;
        sql.Clear;
        sql.Add('insert into CK_MYSC_CR');
        sql.Add('(MYType,MYTypeFlag,MYID,CRTime,CRQtyFlag,CRFlag,CRType,MYCode,MYName,MYMF,MYKZ,Qty');
        sql.Add(',KHNo,KHName,Filler,FillerCode,Filltime,JCDanWei,JCNo,SJLX) values(');
        sql.Add(quotedstr(trim('盘点棉')));
        sql.Add(',' + quotedstr(trim('CGH')));
        sql.Add(',' + quotedstr(trim(maxno)));
        sql.Add(',CONVERT(VARCHAR(10),getdate(),120)  ');
        sql.Add(',1');
        sql.Add(',' + quotedstr(trim('入库')));
        sql.Add(',' + quotedstr(trim('盘点入库')));
        sql.Add(',' + quotedstr(trim(ADOQuery1.fieldbyname('BPCode').AsString)));
        sql.Add(',' + quotedstr(trim(ADOQuery1.fieldbyname('BPName').AsString)));
        sql.Add(',' + quotedstr(trim(ADOQuery1.fieldbyname('BPMF').AsString)));
        sql.Add(',' + quotedstr(trim(ADOQuery1.fieldbyname('BPKZ').AsString)));
        sql.Add(',0');
        sql.Add(',' + quotedstr(trim(ADOQuery1.fieldbyname('GYSNo').AsString)));
        sql.Add(',' + quotedstr(trim(ADOQuery1.fieldbyname('KHName').AsString)));
        sql.Add(', ' + quotedstr(trim(DName)));
        sql.Add(', ' + quotedstr(trim(DCode)));
        sql.Add(',getdate() ');
        sql.Add(',' + quotedstr(trim('绍兴宇昊绣品有限公司')));
        sql.Add(',' + quotedstr(trim('GS02')));
        sql.Add(',' + quotedstr(trim('新')));
        sql.Add(')');
//        showmessage(sql.text);
        execsql;
      end;
    end;
    free;
  end;

  TBRafresh.Click;
end;

end.

