unit U_MDChkKKFL;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxButtonEdit,
  cxDropDownEdit, cxSplitter, cxCheckBox, cxCalendar, Menus,
  cxGridCustomPopupMenu, cxGridPopupMenu, DBClient, ADODB, cxPC, cxTextEdit,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport;

type
  TfrmMDChkKKFL = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    TBExport: TToolButton;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label2: TLabel;
    Label5: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    orderNo: TEdit;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn29: TcxGridDBColumn;
    v5Column6: TcxGridDBColumn;
    v5Column2: TcxGridDBColumn;
    v5Column7: TcxGridDBColumn;
    v5Column3: TcxGridDBColumn;
    v5Column4: TcxGridDBColumn;
    v5Column5: TcxGridDBColumn;
    v5Column10: TcxGridDBColumn;
    v5Column1: TcxGridDBColumn;
    v5Column9: TcxGridDBColumn;
    v77Column1: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    v1GenDan: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    V1OrderNo: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1ToFactoryName: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    cxSplitter2: TcxSplitter;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    v72Column1: TcxGridDBColumn;
    v72Column2: TcxGridDBColumn;
    v72Column3: TcxGridDBColumn;
    v72Column4: TcxGridDBColumn;
    v72Column5: TcxGridDBColumn;
    v72Column6: TcxGridDBColumn;
    v72Column7: TcxGridDBColumn;
    v72Column8: TcxGridDBColumn;
    v72Column9: TcxGridDBColumn;
    Label7: TLabel;
    FactoryName: TEdit;
    Panel3: TPanel;
    Label11: TLabel;
    PS: TEdit;
    Label12: TLabel;
    Qty: TEdit;
    Label13: TLabel;
    Money: TEdit;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    cxGridPopupMenu3: TcxGridPopupMenu;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQuery1: TADOQuery;
    Panel5: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column14: TcxGridDBColumn;
    v3Column20: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v3Column12: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    v3Column17: TcxGridDBColumn;
    v3Column18: TcxGridDBColumn;
    v3Column16: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    v3Column19: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    v3Column15: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    N3: TMenuItem;
    v1Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v3Column13: TcxGridDBColumn;
    v3Column21: TcxGridDBColumn;
    v3Column22: TcxGridDBColumn;
    v3Column23: TcxGridDBColumn;
    v1PSName: TcxGridDBColumn;
    v3Column24: TcxGridDBColumn;
    ToolButton3: TToolButton;
    cxTabControl2: TcxTabControl;
    ComboBox4: TComboBox;
    v1Column2: TcxGridDBColumn;
    Button2: TButton;
    v1Column4: TcxGridDBColumn;
    ToolButton8: TToolButton;
    Panel6: TPanel;
    Button3: TButton;
    Button4: TButton;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    v1Column18: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton7: TToolButton;
    v1Column9: TcxGridDBColumn;
    RM1: TRMGridReport;
    CDS_PRT: TClientDataSet;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    ADOQuery2: TADOQuery;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure V1OrderNoPropertiesEditValueChanged(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure v1Column7PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure v1FactoryNamePropertiesEditValueChanged(Sender: TObject);
    procedure v1PSNamePropertiesEditValueChanged(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure v3Column14PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
    procedure v1ToFactoryNamePropertiesEditValueChanged(Sender: TObject);
    procedure v72Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBFindClick(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure v1Column4PropertiesEditValueChanged(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure v72Column4PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
  private
    { Private declarations }
    FMHId:String;
    procedure InitGrid();
    function SaveData():Boolean;
    procedure InitGridMD();
    function YFHZData(CWFactoryNo,CWFactoryName:String):Boolean;
    function DelYFHZData():Boolean;
  public
    { Public declarations }
  end;

var
  frmMDChkKKFL: TfrmMDChkKKFL;

implementation
uses
  U_DataLink,U_RTFun, U_GYSList,U_ZDYHelp, U_ModuleNote;

{$R *.dfm}

procedure TfrmMDChkKKFL.FormDestroy(Sender: TObject);
begin
  frmMDChkKKFL:=nil;
end;

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

procedure TfrmMDChkKKFL.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('Tv1',Tv1,'KK码单核查');
  WriteCxGrid('Tv2',Tv2,'KK码单核查');
  WriteCxGrid('Tv3',Tv3,'KK码单核查');
  close;
end;

procedure TfrmMDChkKKFL.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,isnull(B.YiFuMoney,0) YiFuMoney,YingFuYMoney=isnull(B.Money,0)-isnull(B.YiFuMoney,0)  ');
      sql.Add('from JYOrder_Main_MD_HZ A ');
      sql.Add(' left join YF_Money_CR B on A.MHID=B.MHID where isnull(A.MDType,'''')=''扣款'' ');

      if ComboBox4.ItemIndex=0 then
      begin
        sql.Add(' and A.MHDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.MHDate<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
      end else
      if ComboBox4.ItemIndex=1 then
      begin
        sql.Add(' and A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
      end else
      if ComboBox4.ItemIndex=2 then
      begin
        sql.Add(' and A.YFDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.YFDate<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
      end else
      if ComboBox4.ItemIndex=3 then
      begin
        sql.Add(' and A.DuiZhangTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.DuiZhangTime<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
      end;

      sql.Add(' and isnull(A.Status,'''')<>'''' ');
      if cxTabControl2.TabIndex=0 then
      begin
        sql.Add(' and isnull(A.DuiZhangStatus,'''')='''' ');
      end else
      if cxTabControl2.TabIndex=1 then
      begin
        sql.Add(' and isnull(A.DuiZhangStatus,'''')=''已对账'' ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

function TfrmMDChkKKFL.SaveData():Boolean;
var
  maxno:String;
begin
  Result:=False;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxno,'MH','JYOrder_Main_MD_HZ',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取码单汇总号失败!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD_HZ where MHID='''+Trim(maxno)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('MHID').Value:=Trim(maxno);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('MHDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOQueryTemp)));
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    with ClientDataSet1 do
    begin
      Append;
      FieldByName('MHID').Value:=Trim(maxno);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('MHDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOQueryTemp)));
      Post;
    end;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行异常!','提示',0);
  end;
end;

procedure TfrmMDChkKKFL.ToolButton5Click(Sender: TObject);
begin
  if cxTabControl2.TabIndex<>0 then Exit;
  with ClientDataSet1 do
  begin
    Append;
    //FieldByName('MHID').Value:='';
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('MHDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOQueryTemp)));
    Post;
  end;
  //SaveData();
end;

procedure TfrmMDChkKKFL.FormShow(Sender: TObject);
begin
  {with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD Set MHID=Null where MDID in(');
    sql.Add('select MDID from JYOrder_Main_MD A where isnull(MHID,'''')<>'''' ');
    sql.Add('and not exists(select * from JYOrder_Main_MD_HZ B where ');
    sql.Add('B.MHID=A.MHID and B.ORDMainId=A.ORDMainId and isnull(B.OrderNo,'''')<>''''))');
    ExecSQL;
  end;   }
  cxTabControl2.TabIndex:=0;
  cxTabControl1.TabIndex:=0;
  EndDate.DateTime:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date;
  ReadCxGrid('Tv1',Tv1,'KK码单核查');
  ReadCxGrid('Tv2',Tv2,'KK码单核查');
  ReadCxGrid('Tv3',Tv3,'KK码单核查');
  InitGrid();
end;

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

procedure TfrmMDChkKKFL.V1OrderNoPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FOrderNo,fsj:String;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_HZ where isnull(MHID,'''')='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    Open;
  end;
  FOrderNo:=Trim(ADOQueryTemp.fieldbyname('OrderNo').AsString);
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('OrderNO').Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD where isnull(MHID,'''')='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    sql.Add(' and isnull(MHID,'''')<>'''' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    with ClientDataSet1 do
    begin
      Edit;
      FieldByName('OrderNO').Value:=Trim(FOrderNo);
      Post;
    end;
    Application.MessageBox(PChar('此条数据已存在核查码单不能更改订单号!'),'提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where OrderNo='''+Trim(mvalue)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    with ClientDataSet1 do
    begin
      Edit;
      FieldByName('OrderNO').Value:=Trim(FOrderNo);
      Post;
    end;
    Application.MessageBox(PChar('订单号:'+Trim(mvalue)+'不存在!'),'提示',0);
    Exit;
  end else
  begin
    with ClientDataSet1 do
    begin
      Edit;
      FieldByName('TaiTou').Value:=Trim(ADOQueryTemp.fieldbyname('ComTaiTou').AsString);
      FieldByName('MPRTCode').Value:=Trim(ADOQueryTemp.fieldbyname('MPRTCode').AsString)+
                                   Trim(ADOQueryTemp.fieldbyname('MPRTGY').AsString);
      FieldByName('MPRTGY').Value:=Trim(ADOQueryTemp.fieldbyname('MPRTGYHZ').AsString);
      FieldByName('ORDMainId').Value:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);

      Post;
    end;
  end;
  {fsj:='select distinct(PFGenDanPerson) Name,Code='''' from JYOrder_PCS_Sub where Mainid='''+Trim(ClientDataSet1.fieldbyname('ORDMainId').AsString)+'''';
  SInitCxGridComboBoxBySql(ADOQueryTemp,v1GenDan,fsj,0,False,'');
  fsj:='select distinct(FactoryName) Name,Code='''' from JYOrder_Main_MD where ORDMainid='''+Trim(ClientDataSet1.fieldbyname('ORDMainId').AsString)+'''';
  SInitCxGridComboBoxBySql(ADOQueryTemp,v1FactoryName,fsj,0,False,''); }
  
  InitGridMD();


end;
procedure TfrmMDChkKKFL.InitGridMD();
var
  fsj:String;
begin
  fsj:='';
  if ((Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)='')
     and
     (Trim(ClientDataSet1.fieldbyname('PSName').AsString)='')) then
  begin
    with ADOQuery2 do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_View_MDTJ_JS :begdate,:enddate,:orderno,:MDType,:WSql');
      Parameters.ParamByName('orderno').Value:=Trim(ClientDataSet1.fieldbyname('OrderNo').AsString);
      Parameters.ParamByName('MDType').Value:='';
      Parameters.ParamByName('WSql').Value:=' and 1<>1';
      Open;
    end;
    SCreateCDS20(ADOQuery2,ClientDataSet3);
    SInitCDSData20(ADOQuery2,ClientDataSet3);
    Exit;
  end;
  with ADOQuery2 do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_View_MDTJ_JS :begdate,:enddate,:orderno,:MDType,:WSql');
    Parameters.ParamByName('orderno').Value:=Trim(ClientDataSet1.fieldbyname('OrderNo').AsString);
    Parameters.ParamByName('MDType').Value:='';

    if cxTabControl1.TabIndex=0 then
    begin
      fsj:=' and isnull(MHID,'''')='''' ';
    end else
    if cxTabControl1.TabIndex=1 then
    begin
      fsj:=' and isnull(MHID,'''')='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''';
    end;
    if Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)<>'' then
    begin
      fsj:=fsj+' and isnull(JMM.DutyFactoryName,'''')='''+Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)+'''';
    end;
    {if Trim(ClientDataSet1.fieldbyname('PSName').AsString)<>'' then
    begin
      fsj:=fsj+' and isnull(JMM.PSName,'''')='''+Trim(ClientDataSet1.fieldbyname('PSName').AsString)+'''';
    end;}
    fsj:=fsj+'  '+  //and isnull(PF.CKName,'''')<>''调节仓库''
             ' and exists(select * from JYOrder_Main_MD_KK JMMK where JMMK.MDID=JMM.MDID)';

    //Parameters.ParamByName('WSql').Value:=fsj;
   // ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS20(ADOQuery2,ClientDataSet3);
  SInitCDSData20(ADOQuery2,ClientDataSet3);
end;
procedure TfrmMDChkKKFL.v1Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFName';
      flagName:='项目名称';
      fnote:=True;
      if ShowModal=1 then
      begin
        with Self.ClientDataSet1 do
        begin
          Edit;
          FieldByName('ItemName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMDChkKKFL.ToolButton1Click(Sender: TObject);
var
  maxNo:string;
begin
  if cxTabControl2.TabIndex<>0 then Exit;
  if ClientDataSet1.IsEmpty then Exit;
  BegDate.SetFocus;
  if Trim(ClientDataSet1.fieldbyname('OrderNo').AsString)='' then
  begin
    Application.MessageBox('订单号不能为空!','提示',0);
    Exit;
  end;
  if Trim(ClientDataSet1.fieldbyname('MHDate').AsString)='' then
  begin
    Application.MessageBox('工厂码单日期不能为空!','提示',0);
    Exit;
  end;
  if Trim(ClientDataSet1.fieldbyname('PSName').AsString)='' then
  begin
    Application.MessageBox('工序不能为空!','提示',0);
    Exit;
  end;
  if Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)='' then
  begin
    Application.MessageBox('供应商不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      //while ClientDataSet1.Locate('SSel',True,[]) do
      begin
        if Trim(ClientDataSet1.fieldbyname('MHID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'MH','JYOrder_Main_MD_HZ',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取码单汇总号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxNo:=Trim(ClientDataSet1.fieldbyname('MHID').AsString);
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD_HZ where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet1.fieldbyname('MHID').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            Edit;
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('MHID').Value:=Trim(maxNo);
          FieldByName('ORDMainid').Value:=Trim(ClientDataSet1.fieldbyname('ORDMainid').AsString);
          FieldByName('FactoryNo').Value:=Trim(ClientDataSet1.fieldbyname('FactoryNo').AsString);
          FieldByName('ToFactoryNo').Value:=Trim(ClientDataSet1.fieldbyname('ToFactoryNo').AsString);
          FieldByName('Status').Value:='已核对';
          FieldByName('MDType').Value:='扣款';
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,ClientDataSet1,'JYOrder_Main_MD_HZ',2);
          Post;
        end;
        Edit;
        FieldByName('MHID').Value:=Trim(maxNo);
        Post;
        if cxTabControl1.TabIndex=0 then
        begin
          if ClientDataSet3.IsEmpty=False then
          begin
            ClientDataSet3.DisableControls;
            with ClientDataSet3 do
            begin
              First;
              while Locate('SSel',True,[]) do
              begin
                with ADOQueryCmd do
                begin
                  Close;
                  sql.Clear;
                  sql.Add('Update JYOrder_Main_MD Set MHID='''+Trim(maxNo)+'''');
                  sql.Add(',HCFlag=1,HCDate=getdate(),HCPerson='''+Trim(DName)+'''');
                  sql.Add(' where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
                  ExecSQL;
                end;
                ClientDataSet3.Delete;
              end;
            end;
            ClientDataSet3.EnableControls;
          end;

        end;
        {if cxTabControl2.TabIndex=0 then
        begin
          ClientDataSet1.Delete;
        end; }
        ps.Text:='';
        Qty.Text:='';
        Money.Text:='';
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet1.EnableControls;
  except
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败！','提示',0);
  end;
end;

procedure TfrmMDChkKKFL.cxTabControl1Change(Sender: TObject);
begin
  if cxTabControl1.TabIndex=0 then
  begin

    N3.Visible:=False;
  end else
  begin
    //Tv3.OptionsSelection.CellSelect:=False;

    N3.Visible:=True;
  end;
  PS.Text:='';
  Qty.Text:='';
  Money.Text:='';
  InitGridMD();
end;

procedure TfrmMDChkKKFL.MenuItem1Click(Sender: TObject);
var
  fsj:string;
begin

end;


procedure TfrmMDChkKKFL.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  fsj,FName:String;
begin
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if cxTabControl2.TabIndex=2 then
  begin
    if Trim(FName)='Ssel' then
    begin
      Tv1.OptionsData.Editing:=True;
    end else
    begin
      Tv1.OptionsData.Editing:=False;
    end;
  end;
  if cxTabControl2.TabIndex>1 then exit;

  if Trim(FName)='GenDan' then
  begin
    fsj:='select distinct(PFGenDanPerson) Name,Code='''' from JYOrder_PCS_Sub where Mainid='''+Trim(ClientDataSet1.fieldbyname('ORDMainId').AsString)+'''';
    SInitCxGridComboBoxBySql(ADOQueryTemp,v1GenDan,fsj,0,False,'');
  end else
  if Trim(FName)='FactoryName' then
  begin
    fsj:='select distinct(FactoryName) Name,Code=FactoryNo from JYOrder_Main_MD '+
         ' where ORDMainid='''+Trim(ClientDataSet1.fieldbyname('ORDMainId').AsString)+''''+
         ' Group by FactoryName,FactoryNo';
    SInitCxGridComboBoxBySql(ADOQueryTemp,v1FactoryName,fsj,1,False,'');
  end else
  if Trim(FName)='ToFactoryName' then
  begin
    fsj:='select distinct(FactoryName) Name,Code=FactoryNo from JYOrder_Main_MD '+
         ' where ORDMainid='''+Trim(ClientDataSet1.fieldbyname('ORDMainId').AsString)+''''+
         ' Group by FactoryName,FactoryNo';
    SInitCxGridComboBoxBySql(ADOQueryTemp,v1ToFactoryName,fsj,1,False,'');
  end else
  if Trim(FName)='PSName' then
  begin
    fsj:='select PSName Name,Code='''' from JYOrder_Process where Mainid='''+Trim(ClientDataSet1.fieldbyname('ORDMainId').AsString)+''''
         +' order by PSXH ';
    SInitCxGridComboBoxBySql(ADOQueryTemp,v1PSName,fsj,0,False,'');
  end;
  if Trim(ClientDataSet1.fieldbyname('MHId').AsString)<>FMHId then
  begin
    ps.Text:='';
    Qty.Text:='';
    Money.Text:='';
    InitGridMD();
    FMHId:=Trim(ClientDataSet1.fieldbyname('MHID').AsString);
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main A inner join Contract_Sub B on A.Mainid=B.Mainid');
    sql.Add(' where A.ConNo='''+Trim(ClientDataSet1.fieldbyname('OrderNo').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);

end;

procedure TfrmMDChkKKFL.v1Column7PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('FKDays').Value:=mvalue;
    Post;
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('YFDate').Value:=FieldByName('MHDate').Value+FieldByName('FKDays').Value;
    Post;
  end;
end;

procedure TfrmMDChkKKFL.v1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:=Null;
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('MHDate').Value:=mvalue;
    Post;
  end;
  with ClientDataSet1 do
  begin
    Edit;
    if mvalue=Null then
    begin
      FieldByName('YFDate').Value:=Null;
    end else
    begin
      FieldByName('YFDate').Value:=FieldByName('MHDate').Value+FieldByName('FKDays').Value;
    end;
    Post;
  end;
end;

procedure TfrmMDChkKKFL.v1FactoryNamePropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('FactoryName').Value:=mvalue;
    FieldByName('FactoryNo').Value:=TA((v1FactoryName.Properties as TcxComboBoxProperties).Items.
       Objects[(v1FactoryName.Properties as TcxComboBoxProperties).Items.IndexOf(Trim(mvalue))]).S;
    Post;
  end;
  InitGridMD();
end;

procedure TfrmMDChkKKFL.v1PSNamePropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('PSName').Value:=mvalue;
    Post;
  end;
  InitGridMD();
end;

procedure TfrmMDChkKKFL.N3Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if cxTabControl1.TabIndex=1 then
    begin
      if ClientDataSet3.IsEmpty=False then
      begin
        ClientDataSet3.DisableControls;
        with ClientDataSet3 do
        begin
          First;
          while Locate('SSel',True,[]) do
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update JYOrder_Main_MD Set MHID=Null');
              sql.Add(',HCFlag=0,HCDate=Null,HCPerson=Null');
              sql.Add(' where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
              ExecSQL;
            end;
            ClientDataSet3.Delete;
          end;
        end;
        ClientDataSet3.EnableControls;
      end;

    end;

    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败！','提示',0);
  end;
end;

procedure TfrmMDChkKKFL.v3Column14PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,mvalueCS:Boolean;
  FPS,FQty,FMoney:Double;
begin
  //mvalueCS:=ClientDataSet3.FieldByName('SSel').AsBoolean;
  if CheckBox1.Checked=True then exit;
  if Trim(PS.Text)='' then
  begin
    PS.Text:='0';
  end;
  FPS:=StrToFloat(PS.Text);
  if Trim(Qty.Text)='' then
  begin
    Qty.Text:='0';
  end;
  FQty:=StrToFloat(Qty.Text);
  if Trim(Money.Text)='' then
  begin
    Money.Text:='0';
  end;
  FMoney:=StrToFloat(Money.Text);
  mvalue:=TcxCheckBox(Sender).EditingValue;
  if mvalue=True then
  begin
    FPS:=FPS+ClientDataSet3.fieldbyname('PS').Value;
    FQty:=FQty+ClientDataSet3.fieldbyname('Qty').Value;
    FMoney:=FMoney+ClientDataSet3.fieldbyname('TJXSMoney').Value;
  end else
  begin
    FPS:=FPS-ClientDataSet3.fieldbyname('PS').Value;
    FQty:=FQty-ClientDataSet3.fieldbyname('Qty').Value;
    FMoney:=FMoney-ClientDataSet3.fieldbyname('TJXSMoney').Value;
  end;
  PS.Text:=FloatToStr(FPS);
  Qty.Text:=FloatToStr(FQty);
  Money.Text:=FloatToStr(FMoney);
  {if cxTabControl1.TabIndex=0 then
  begin
    with ClientDataSet1 do
    begin
      Edit;
      if Trim(ClientDataSet1.fieldbyname('PS').AsString)='' then
      begin
        FieldByName('PS').Value:=0;
      end;
      if Trim(ClientDataSet1.fieldbyname('Qty').AsString)='' then
      begin
        FieldByName('Qty').Value:=0;
      end;
      if Trim(ClientDataSet1.fieldbyname('Money').AsString)='' then
      begin
        FieldByName('Money').Value:=0;
      end;
      Post;
    end;
    with ClientDataSet1 do
    begin
      Edit;
      FieldByName('').Value:=
      Post;
    end;
  end; }
end;

procedure TfrmMDChkKKFL.ToolButton6Click(Sender: TObject);
var
  FOrderNo,fsj:String;
begin
  if cxTabControl2.TabIndex>1 then Exit;
  if ClientDataSet1.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_HZ where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    Open;
  end;
  FOrderNo:=Trim(ADOQueryTemp.fieldbyname('OrderNo').AsString);
  if Trim(ForderNo)<>'' then
  begin
    fsj:='';
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_View_MDTJ_JS :begdate,:enddate,:orderno,:MDType,:WSql');
      Parameters.ParamByName('orderno').Value:=Trim(ClientDataSet1.fieldbyname('OrderNo').AsString);
      Parameters.ParamByName('MDType').Value:='';
      fsj:=' and isnull(MHID,'''')='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''';
      if Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)<>'' then
      begin
        fsj:=fsj+' and isnull(JMM.FactoryName,'''')='''+Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)+'''';
      end;
      if Trim(ClientDataSet1.fieldbyname('PSName').AsString)<>'' then
      begin
        fsj:=fsj+' and isnull(JMM.PSName,'''')='''+Trim(ClientDataSet1.fieldbyname('PSName').AsString)+'''';
      end;
      Parameters.ParamByName('WSql').Value:=fsj;
      Open;
    end;
    {with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
      Open;
    end;}
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('已有核查信息不能删除!','提示',0);
      Exit;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_Main_MD Set MHID=Null where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
        ExecSQL;
      end;
    end;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main_MD Set MHID=Null where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
      ExecSQL;
    end;
  end;

  if Application.MessageBox('确定要删除信息吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('insert into JYOrder_Main_MD_HZ_DelLog ');
    sql.Add('select * from JYOrder_Main_MD_HZ where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD_HZ_DelLog Set Note=Isnull(Note,'''')+''→删除人:''+'''+Trim(DName)+'''');
    sql.Add('+'' ''+Convert(Varchar(20),getdate(),120)');
    sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete JYOrder_Main_MD_HZ where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    ExecSQL;
  end;
  ClientDataSet1.Delete;
end;

procedure TfrmMDChkKKFL.TBExportClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  TcxGridToExcel('核查汇总单',cxGrid1);
end;

procedure TfrmMDChkKKFL.ToolButton2Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='码单核查与分类';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmMDChkKKFL.ToolButton3Click(Sender: TObject);
begin
  if cxTabControl2.TabIndex>1 then Exit;
  if ClientDataSet1.IsEmpty then exit;
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.ClientDataSet1 do
        begin
          Edit;
          FieldByName('FactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString) ;
          FieldByName('FactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

function TfrmMDChkKKFL.DelYFHZData():Boolean;
var
  CRID:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    Open;
  end;
  CRID:=Trim(ADOQueryTemp.fieldbyname('CRID').AsString);
  if ADOQueryTemp.IsEmpty then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set ');
    sql.Add(' KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'')');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'' )');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  {with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+Trim(CRID));
    ExecSQL;
  end; }
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete YF_Money_CR where  ');
    sql.Add(' MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    sql.Add(' Update JYOrder_Main_MD_HZ Set YFID=Null where  ');
    sql.Add(' MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    ExecSQL;
  end;
  Result:=True;
end;
procedure TfrmMDChkKKFL.cxTabControl2Change(Sender: TObject);
begin
  if cxTabControl2.TabIndex<2 then
  begin
    //Tv1.OptionsSelection.CellSelect:=True;
    Tv1.OptionsData.Editing:=True;
  end else
  begin
    //Tv3.OptionsSelection.CellSelect:=False;
    Tv1.OptionsData.Editing:=False;
  end;
  InitGrid();
end;
function TfrmMDChkKKFL.YFHZData(CWFactoryNo,CWFactoryName:String):Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryNo='''+Trim(CWFactoryNo)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(CWFactoryName);
      FieldByName('FactoryNo').Value:=Trim(CWFactoryNo);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryNo='''+Trim(CWFactoryNo)+'''');
    sql.Add(' and MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd,YFID,'ZC','YF_Money_CR',3,1)=False then
    begin
      Application.MessageBox('取应付最大号失败!','提示',0);
      Exit;
    end;
  end else
  begin
    YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID='''+Trim(YFID)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if ADOQueryTemp.IsEmpty then
      Append
    else
      Edit;
    FieldByName('YFID').Value:=Trim(YFID);
    FieldByName('MainId').Value:=Trim(ClientDataSet1.fieldbyname('OrdMainId').AsString);
    FieldByName('CRID').Value:=StrToInt(CRID);
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('CRType').Value:='应付款登记';
    FieldByName('CRFlag').Value:='应付付';
    FieldByName('QtyFlag').Value:=1;
    FieldByName('FactoryNo').Value:=Trim(CWFactoryNo);
    FieldByName('FactoryName').Value:=Trim(CWFactoryName);
    FieldByName('HZFactoryNo').Value:=Trim(CWFactoryNo);
    FieldByName('HZFactoryName').Value:=Trim(CWFactoryName);
    FieldByName('CRTime').Value:=ClientDataSet1.fieldbyname('YFDate').Value;
    FieldByName('Qty').Value:=ClientDataSet1.fieldbyname('Qty').Value;
    if Trim(ClientDataSet1.fieldbyname('PS').AsString)<>'' then
    begin
      FieldByName('PS').Value:=ClientDataSet1.fieldbyname('PS').Value;
    end else
    begin
      FieldByName('PS').Value:=0;
    end;

    FieldByName('Price').Value:=ClientDataSet1.fieldbyname('Price').Value;
    FieldByName('Money').Value:=ClientDataSet1.fieldbyname('Money').Value;
    FieldByName('BBMoney').Value:=ClientDataSet1.fieldbyname('Money').Value;
    //FieldByName('QtyUnit').Value:=Trim(ClientDataSet1.fieldbyname('QtyUnit').AsString);
    FieldByName('YFType').Value:='自动生成';
    FieldByName('HuiLv').Value:=1;
    FieldByName('BZType').Value:='人民币';
    FieldByName('YFName').Value:=Trim(ClientDataSet1.fieldbyname('ItemName').AsString);
    FieldByName('PSName').Value:=Trim(ClientDataSet1.fieldbyname('PSName').AsString);
    FieldByName('MHID').Value:=Trim(ClientDataSet1.fieldbyname('MHID').AsString);
   // FieldByName('KFType').Value:='正常';
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD_HZ Set YFID='''+Trim(YFID)+'''');
    sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set ');
    sql.Add(' KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'')');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'' )');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;

  Result:=True;
end;
procedure TfrmMDChkKKFL.v1ToFactoryNamePropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('ToFactoryName').Value:=mvalue;
    FieldByName('ToFactoryNo').Value:=TA((v1ToFactoryName.Properties as TcxComboBoxProperties).Items.
       Objects[(v1ToFactoryName.Properties as TcxComboBoxProperties).Items.IndexOf(Trim(mvalue))]).S;
    Post;
  end;
end;

procedure TfrmMDChkKKFL.v72Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,Qty,Price,FtyQty,FtyPrice:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName(FName).Value:=mvalue;
    Post;
  end;
  Qty:=Trim(ClientDataSet1.fieldbyname('Qty').AsString);
  if Trim(Qty)='' then
  begin
    Qty:='0';
  end;
  Price:=Trim(ClientDataSet1.fieldbyname('Price').AsString);
  if Trim(Price)='' then
  begin
    Price:='0';
  end;
  FtyQty:=Trim(ClientDataSet1.fieldbyname('FtyQty').AsString);
  if Trim(FtyQty)='' then
  begin
    FtyQty:='0';
  end;
  FtyPrice:=Trim(ClientDataSet1.fieldbyname('FtyPrice').AsString);
  if Trim(FtyPrice)='' then
  begin
    FtyPrice:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(Qty)*StrToFloat(Price);
    FieldByName('FtyMoney').Value:=StrToFloat(FtyQty)*StrToFloat(FtyPrice);
    FieldByName('ChaEMoney').Value:=StrToFloat(Qty)*StrToFloat(Price)-StrToFloat(FtyQty)*StrToFloat(FtyPrice);
    Post;
  end;
end;

procedure TfrmMDChkKKFL.Button2Click(Sender: TObject);
var
  mvalue,mvalueCS:Boolean;
  FPS,FQty,FMoney:Double;
  FMDID:String;
begin
  //mvalueCS:=ClientDataSet3.FieldByName('SSel').AsBoolean;

  PS.Text:='0';
  FPS:=StrToFloat(PS.Text);
  Qty.Text:='0';
  FQty:=StrToFloat(Qty.Text);
  Money.Text:='0';
  FMoney:=StrToFloat(Money.Text);
  if CheckBox1.Checked=False then
  begin
    if ClientDataSet3.IsEmpty then Exit;
    FMDID:=Trim(ClientDataSet3.fieldbyname('MDID').AsString);
    ClientDataSet3.DisableControls;
    with ClientDataSet3 do
    begin
      First;
      while not Eof do
      begin
        if ClientDataSet3.FieldByName('SSel').AsBoolean=True then
        begin
          FPS:=FPS+ClientDataSet3.fieldbyname('PS').Value;
          FQty:=FQty+ClientDataSet3.fieldbyname('Qty').Value;
          FMoney:=FMoney+ClientDataSet3.fieldbyname('TJXSMoney').Value;
        end;
        Next;
      end;
    end;
    ClientDataSet3.EnableControls;
    PS.Text:=FloatToStr(FPS);
    Qty.Text:=FloatToStr(FQty);
    Money.Text:=FloatToStr(FMoney);
    ClientDataSet3.Locate('MDID',FMDID,[]);
  end else
  begin
    if ClientDataSet1.IsEmpty then Exit;
    FMDID:=Trim(ClientDataSet1.fieldbyname('MHID').AsString);
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        if ClientDataSet1.FieldByName('SSel').AsBoolean=True then
        begin
          if Trim(ClientDataSet1.fieldbyname('PS').AsString)<>'' then
          begin
            FPS:=FPS+ClientDataSet1.fieldbyname('PS').Value;
          end else
          begin
            FPS:=FPS+0;
          end;
          //FPS:=FPS+ClientDataSet1.fieldbyname('PS').Value;
          FQty:=FQty+ClientDataSet1.fieldbyname('Qty').Value;
          FMoney:=FMoney+ClientDataSet1.fieldbyname('Money').Value;
        end;
        Next;
      end;
    end;
    ClientDataSet1.EnableControls;
    PS.Text:=FloatToStr(FPS);
    Qty.Text:=FloatToStr(FQty);
    Money.Text:=FloatToStr(FMoney);
    ClientDataSet1.Locate('MHID',FMDID,[]);
  end;

end;

procedure TfrmMDChkKKFL.ToolButton8Click(Sender: TObject);
begin
  if cxTabControl2.TabIndex>1 then Exit;
  if ClientDataSet1.IsEmpty then Exit;
  BegDate.SetFocus;
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;OrderNo',VarArrayOf([True,Null]),[loPartialKey]) then
  begin
    Application.MessageBox('订单号不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;MHDate',VarArrayOf([True,Null]),[loPartialKey]) then
  begin
    Application.MessageBox('工厂码单日期不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;PSName',VarArrayOf([True,Null]),[loPartialKey]) then
  begin
    Application.MessageBox('工序不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;FactoryName',VarArrayOf([True,Null]),[loPartialKey]) then
  begin
    Application.MessageBox('供应商不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while ClientDataSet1.Locate('SSel',True,[]) do
      begin
      
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD_HZ where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          FieldByName('ORDMainid').Value:=Trim(ClientDataSet1.fieldbyname('ORDMainid').AsString);
          FieldByName('FactoryNo').Value:=Trim(ClientDataSet1.fieldbyname('FactoryNo').AsString);
          FieldByName('ToFactoryNo').Value:=Trim(ClientDataSet1.fieldbyname('ToFactoryNo').AsString);
          FieldByName('Status').Value:='已核对';
          FieldByName('MDType').Value:='扣款';
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,ClientDataSet1,'JYOrder_Main_MD_HZ',2);
          FieldByName('Editer').Value:=Trim(DName);
          FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_MD_HZ Set Status=''已核对'' ');
          if Trim(ClientDataSet1.fieldbyname('FtyQty').AsString)<>'' then
          begin
            SQL.Add(',FtyQty='+Trim(ClientDataSet1.fieldbyname('FtyQty').AsString));
          end;
          if Trim(ClientDataSet1.fieldbyname('FtyPrice').AsString)<>'' then
          begin
            SQL.Add(',FtyPrice='+Trim(ClientDataSet1.fieldbyname('FtyPrice').AsString));
          end;
          if Trim(ClientDataSet1.fieldbyname('FtyMoney').AsString)<>'' then
          begin
            SQL.Add(',FtyMoney='+Trim(ClientDataSet1.fieldbyname('FtyMoney').AsString));
          end;
          if Trim(ClientDataSet1.fieldbyname('ChaEMoney').AsString)<>'' then
          begin
            SQL.Add(',ChaEMoney='+Trim(ClientDataSet1.fieldbyname('ChaEMoney').AsString));
          end;
          sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          ExecSQL;
        end;
        if cxTabControl2.TabIndex=0 then
        begin
          ClientDataSet1.Delete;
        end else
        begin
          ClientDataSet1.Edit;
          ClientDataSet1.FieldByName('SSel').Value:=False;
          ClientDataSet1.Post;
        end;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet1.EnableControls;
  except
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败！','提示',0);
  end;

end;

procedure TfrmMDChkKKFL.Button4Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if cxTabControl1.TabIndex=1 then
    begin
      if ClientDataSet3.IsEmpty=False then
      begin
        ClientDataSet3.DisableControls;
        with ClientDataSet3 do
        begin
          First;
          while Locate('SSel',True,[]) do
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update JYOrder_Main_MD Set MHID=Null');
              sql.Add(',HCFlag=0,HCDate=Null,HCPerson=Null');
              sql.Add(' where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
              ExecSQL;
            end;
            ClientDataSet3.Delete;
          end;
        end;

      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet3.EnableControls;
    ps.Text:='';
    Qty.Text:='';
    Money.Text:='';
  except
    ps.Text:='';
    Qty.Text:='';
    Money.Text:='';
    ClientDataSet3.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败！','提示',0);
  end;
end;

procedure TfrmMDChkKKFL.Button3Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  if ClientDataSet3.IsEmpty then Exit;
  if ClientDataSet3.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要核查吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if cxTabControl1.TabIndex=0 then
    begin
      if ClientDataSet3.IsEmpty=False then
      begin
        ClientDataSet3.DisableControls;
        with ClientDataSet3 do
        begin
          First;
          while Locate('SSel',True,[]) do
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update JYOrder_Main_MD Set MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
              sql.Add(',HCFlag=1,HCDate=getdate(),HCPerson='''+Trim(DName)+'''');
              sql.Add(' where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
              ExecSQL;
            end;
            ClientDataSet3.Delete;
          end;
        end;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ps.Text:='';
    Qty.Text:='';
    Money.Text:='';
    ClientDataSet3.EnableControls;
  except
    ps.Text:='';
    Qty.Text:='';
    Money.Text:='';
    ClientDataSet3.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('核查异常!','提示',0);
  end;
end;

procedure TfrmMDChkKKFL.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  {if Trim(ClientDataSet1.fieldbyname('MHId').AsString)<>FMHId then
  begin
    ps.Text:='';
    Qty.Text:='';
    Money.Text:='';
    InitGridMD();
    FMHId:=Trim(ClientDataSet1.fieldbyname('MHID').AsString);
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main A inner join Contract_Sub B on A.Mainid=B.Mainid');
    sql.Add(' where A.ConNo='''+Trim(ClientDataSet1.fieldbyname('OrderNo').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2); }
end;

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

procedure TfrmMDChkKKFL.orderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmMDChkKKFL.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmMDChkKKFL.v1Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue,mvalueCS:Boolean;
  FPS,FQty,FMoney:Double;
begin
  //mvalueCS:=ClientDataSet3.FieldByName('SSel').AsBoolean;
  if CheckBox1.Checked=False then exit;
  if Trim(PS.Text)='' then
  begin
    PS.Text:='0';
  end;
  FPS:=StrToFloat(PS.Text);
  if Trim(Qty.Text)='' then
  begin
    Qty.Text:='0';
  end;
  FQty:=StrToFloat(Qty.Text);
  if Trim(Money.Text)='' then
  begin
    Money.Text:='0';
  end;
  FMoney:=StrToFloat(Money.Text);
  mvalue:=TcxCheckBox(Sender).EditingValue;
  if mvalue=True then
  begin
    if Trim(ClientDataSet1.fieldbyname('PS').AsString)<>'' then
    begin
      FPS:=FPS+ClientDataSet1.fieldbyname('PS').Value;
    end else
    begin
      FPS:=FPS+0;
    end;

    FQty:=FQty+ClientDataSet1.fieldbyname('Qty').Value;
    FMoney:=FMoney+ClientDataSet1.fieldbyname('Money').Value;
  end else
  begin
    if Trim(ClientDataSet1.fieldbyname('PS').AsString)<>'' then
    begin
      FPS:=FPS-ClientDataSet1.fieldbyname('PS').Value;
    end else
    begin
      FPS:=FPS-0;
    end;
    //FPS:=FPS-ClientDataSet1.fieldbyname('PS').Value;
    FQty:=FQty-ClientDataSet1.fieldbyname('Qty').Value;
    FMoney:=FMoney-ClientDataSet1.fieldbyname('Money').Value;
  end;
  PS.Text:=FloatToStr(FPS);
  Qty.Text:=FloatToStr(FQty);
  Money.Text:=FloatToStr(FMoney);
end;

procedure TfrmMDChkKKFL.CheckBox1Click(Sender: TObject);
begin
  PS.Text:='';
  Qty.Text:='';
  Money.Text:='';
  Button2.Click;
end;

procedure TfrmMDChkKKFL.v72Column4PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,Qty,Price,FtyQty,FtyPrice,Money,FtyMoney:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName(FName).Value:=mvalue;
    Post;
  end;
  Qty:=Trim(ClientDataSet1.fieldbyname('Qty').AsString);
  if Trim(Qty)='' then
  begin
    Qty:='0';
  end;
  Money:=Trim(ClientDataSet1.fieldbyname('Money').AsString);
  if Trim(Money)='' then
  begin
    Money:='0';
  end;
  FtyMoney:=Trim(ClientDataSet1.fieldbyname('FtyMoney').AsString);
  if Trim(FtyMoney)='' then
  begin
    FtyMoney:='0';
  end;
  FtyMoney:=Trim(ClientDataSet1.fieldbyname('FtyMoney').AsString);
  if Trim(FtyMoney)='' then
  begin
    FtyMoney:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    if StrToFloat(Qty)<>0 then
    begin
      FieldByName('Price').Value:=StrToFloat(Money)*1.00/StrToFloat(Qty);
    end;
    {if StrToFloat(FtyQty)<>0 then
    begin
      FieldByName('FtyPrice').Value:=StrToFloat(FtyMoney)*1.00/StrToFloat(FtyQty);
    end;}
    FieldByName('ChaEMoney').Value:=StrToFloat(Money)-StrToFloat(FtyMoney);
    Post;
  end;
end;

procedure TfrmMDChkKKFL.ToolButton4Click(Sender: TObject);
var
  maxno:String;
begin
  if cxTabControl2.TabIndex<>0 then Exit;
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;MHID',VarArrayOf([True,Null]),[loPartialKey]) then
  begin
    Application.MessageBox('有未保存数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxno,'XS','JYOrder_Main_MD_HZ',2,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取协商书编号失败!','提示',0);
      Exit;
    end;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_MD_HZ Set XSSNO='''+Trim(maxno)+'''');
          sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          ExecSQL;
        end;
        Edit;
        FieldByName('SSel').Value:=False;
        FieldByName('XSSNO').Value:=Trim(maxno);
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet1.EnableControls;
    Application.MessageBox('操作成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    ClientDataSet1.EnableControls;
    Application.MessageBox('操作异常!','提示',0);
  end;

end;

procedure TfrmMDChkKKFL.ToolButton7Click(Sender: TObject);
var
   fPrintFile:string;
begin
  if ClientDataSet1.IsEmpty then Exit;
  if Trim(ClientDataSet1.fieldbyname('XSSNO').AsString)='' then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\协商书.rmf' ;
  with ADOQueryTemp do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add(' select A.*,B.MPRTKZ,B.MPRTCodeName  ');
    SQL.Add(',KHName=(select KHName from ZH_KH_Info where ZKID=A.FactoryNo)');
    sql.Add(',GSTT=(select Top 1 ZdyStr1 from KH_Zdy AA where AA.ZdyName=A.TaiTou)');
    sql.Add(' from JYOrder_Main_MD_HZ A');
    sql.Add(' inner join JYOrder_Main B on A.ORDMainid=B.MainId');
    sql.Add(' where A.XSSNO='''+Trim(ClientDataSet1.fieldbyname('XSSNO').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\协商书.rmf'),'提示',0);
  end;

end;

end.
