unit U_MDChkFL;

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;

type
  TfrmMDChkFL = 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;
    Panel4: TPanel;
    Panel10: TPanel;
    Image2: TImage;
    N3: TMenuItem;
    v1Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v3Column13: TcxGridDBColumn;
    v3Column21: TcxGridDBColumn;
    v3Column22: TcxGridDBColumn;
    v3Column23: TcxGridDBColumn;
    v1PSName: TcxGridDBColumn;
    v3Column24: TcxGridDBColumn;
    ToolButton3: TToolButton;
    cxTabControl2: TcxTabControl;
    ToolButton4: TToolButton;
    ToolButton7: TToolButton;
    ComboBox4: TComboBox;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    ToolButton8: TToolButton;
    Panel6: TPanel;
    Button3: TButton;
    Button4: TButton;
    ToolButton9: TToolButton;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    ToolButton10: TToolButton;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Label1: TLabel;
    XSSNO: TEdit;
    ClientDataSet33: TClientDataSet;
    ClientDataSet66: TClientDataSet;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Button2: TButton;
    CheckBox1: TCheckBox;
    Label14: TLabel;
    Label15: TLabel;
    cxGrid4: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Button1: TButton;
    DataSource5: TDataSource;
    ClientDataSet5: TClientDataSet;
    cxGridPopupMenu5: TcxGridPopupMenu;
    v5Column8: TcxGridDBColumn;
    v5Column11: TcxGridDBColumn;
    v5Column12: TcxGridDBColumn;
    v5Column13: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure N1Click(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 ToolButton4Click(Sender: TObject);
    procedure ToolButton7Click(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 ToolButton9Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure v1Column4PropertiesEditValueChanged(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FMHId:String;
    procedure InitGrid();
    function SaveData():Boolean;
    procedure InitGridMD();
    function YFHZData(CWFactoryNo,CWFactoryName:String):Boolean;
    function DelYFHZData():Boolean;
    function  HXYFK(FFactoryName:String):Boolean;
  public
    { Public declarations }
  end;

var
  frmMDChkFL: TfrmMDChkFL;

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

{$R *.dfm}

procedure TfrmMDChkFL.FormDestroy(Sender: TObject);
begin
  frmMDChkFL:=nil;
end;

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

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

procedure TfrmMDChkFL.Image2Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmMDChkFL.Panel10MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmMDChkFL.N1Click(Sender: TObject);
begin
  Panel4.Left:=cxSplitter1.Left+20;
  Panel4.Top:=Panel1.Top+150;
  Panel4.Visible:=True;
end;

procedure TfrmMDChkFL.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(',YSFKDate=(select Top 1 YC.CRTime from YF_Money_CR YC inner join YF_Money_CR_HX HX  ');
      sql.Add('           on YC.YFID=HX.FKID where HX.YFID=B.YFID)');
      sql.Add(',YSFKMoney=(select Top 1 YC.Money from YF_Money_CR YC inner join YF_Money_CR_HX HX  ');
      sql.Add('           on YC.YFID=HX.FKID where HX.YFID=B.YFID)');
      sql.Add(',KCMoney=(select KCMoney from YF_Money_KC KC where KC.CRID=B.CRID)');
      sql.Add('from JYOrder_Main_MD_HZ A ');
      sql.Add(' left join YF_Money_CR B on A.MHID=B.MHID where');
      if ComboBox4.ItemIndex=0 then
      begin
        sql.Add(' 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(' 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(' 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(' A.DuiZhangTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.DuiZhangTime<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
      end;

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

function TfrmMDChkFL.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 TfrmMDChkFL.ToolButton5Click(Sender: TObject);
begin
  if cxTabControl2.TabIndex<>0 then Exit;
  SaveData();
end;

procedure TfrmMDChkFL.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,'码单核查');
  ReadCxGrid('Tv2',Tv2,'码单核查');
  ReadCxGrid('Tv3',Tv3,'码单核查');
  InitGrid();
end;

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

procedure TfrmMDChkFL.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 TfrmMDChkFL.InitGridMD();
var
  fsj:String;
begin
  fsj:='';
  if ((Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)='')
     and
     (Trim(ClientDataSet1.fieldbyname('PSName').AsString)='')) then
  begin
    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:='';
      Parameters.ParamByName('WSql').Value:=' and 1<>1';
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet3);
    SInitCDSData20(ADOQueryTemp,ClientDataSet3);
    Exit;
  end;
  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:='';
    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.FactoryName,'''')='''+Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)+''''+
               ' Or 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;}
    Parameters.ParamByName('WSql').Value:=fsj;
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);
end;
procedure TfrmMDChkFL.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 TfrmMDChkFL.ToolButton1Click(Sender: TObject);
begin
  if cxTabControl2.TabIndex>1 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
        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:='已核对';
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,ClientDataSet1,'JYOrder_Main_MD_HZ',2);
          Post;
        end;
        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;
            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 TfrmMDChkFL.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 TfrmMDChkFL.MenuItem1Click(Sender: TObject);
var
  fsj:string;
begin

end;


procedure TfrmMDChkFL.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  fsj,FName:String;
begin
  Label15.Caption:=Trim(ClientDataSet1.FieldByName('KCMoney').AsString)+'('+
                  Trim(ClientDataSet1.FieldByName('FactoryName').AsString)+')';
  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;
  

end;

procedure TfrmMDChkFL.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 TfrmMDChkFL.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 TfrmMDChkFL.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 TfrmMDChkFL.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 TfrmMDChkFL.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 TfrmMDChkFL.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('Money').Value;
  end else
  begin
    FPS:=FPS-ClientDataSet3.fieldbyname('PS').Value;
    FQty:=FQty-ClientDataSet3.fieldbyname('Qty').Value;
    FMoney:=FMoney-ClientDataSet3.fieldbyname('Money').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 TfrmMDChkFL.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;
  if Trim(ADOQueryTemp.fieldbyname('MDType').AsString)='扣款' then
  begin
    Application.MessageBox('不能删除扣款码单!','提示',0);
    Exit;
  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('delete JYOrder_Main_MD_HZ where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    ExecSQL;
  end;
  ClientDataSet1.Delete;
end;

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

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

procedure TfrmMDChkFL.ToolButton4Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if cxTabControl2.TabIndex<>1 then Exit;
  BegDate.SetFocus;
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;FtyMoney',VarArrayOf([True,Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('工厂金额不能为空!','提示',0);
    Exit;
  end;
  {if Trim(ClientDataSet1.fieldbyname('FtyMoney').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
      First;
      while ClientDataSet1.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_MD_HZ Set DuiZhangTime=Getdate(),');
          SQL.Add('DuiZhangPerson='''+Trim(DName)+''',DuiZhangStatus=''已对账'' ' );
          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;
        YFHZData(Trim(ClientDataSet1.fieldbyname('FactoryNo').AsString),
                 Trim(ClientDataSet1.fieldbyname('FactoryName').AsString));
        HXYFK(Trim(ClientDataSet1.fieldbyname('FactoryName').AsString));
        ClientDataSet1.Delete;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet1.EnableControls;
  except
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;
function TfrmMDChkFL.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 TfrmMDChkFL.ToolButton7Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if cxTabControl2.TabIndex<>2 then Exit;
  BegDate.SetFocus;
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;ChkStatus',VarArrayOf([True,'待处理']),[loPartialKey]) then
  begin
    Application.MessageBox('数据有争议不能执行操作!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;ChkStatus',VarArrayOf([True,'处理中']),[loPartialKey]) then
  begin
    Application.MessageBox('数据正在处理中不能执行操作!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;ChkStatus',VarArrayOf([True,'处理完成']),[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 ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR_HX A inner join YF_Money_CR B on A.YFID=B.YFID');
          sql.Add(' inner join JYOrder_Main_MD_HZ C on B.MHID=C.MHID');
          sql.Add(' where C.MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          ClientDataSet1.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已有付款记录，不能撤销!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_MD_HZ Set DuiZhangTime=Null,');
          SQL.Add('DuiZhangPerson=Null,DuiZhangStatus=Null,FtyQty=Null,FtyPrice=Null,FtyMoney=Null,ChaEMoney=Null ' );
          sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          ExecSQL;
        end;
        DelYFHZData();
        ClientDataSet1.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet1.EnableControls;
  except
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmMDChkFL.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  TfrmMDChkFL.HXYFK(FFactoryName:String):Boolean;
var
  factoryname,FFKMoney,maxno:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select AA.* from(');
    sql.Add(' select CRTIme,YFID, ');
    sql.Add(' DHXMoney=isnull(Money,0)-isnull((select isnull(Sum(HXMoney),0) from YF_Money_CR_HX B where B.FKID=A.YFID),0)');
    sql.Add(' from YF_Money_CR A');
    SQL.Add(' where FactoryName='''+Trim(FFactoryName)+'''');
    sql.Add(' and A.CRTYpe=''付款登记'' )AA');
    sql.Add(' where isnull(DHXMoney,0)>0');
    sql.Add(' order by CRTime');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet66);
  SInitCDSData20(ADOQueryTemp,ClientDataSet66);
  with ClientDataSet66 do
  begin
    First;
    while not eof do
    begin
      FFKMoney:=ClientDataSet66.fieldbyname('DHXMoney').AsString;
      if StrToFloat(FFKMoney)>0 then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select A.*,YingFuMoney=Money-YiFuMoney ');
          sql.Add(' ,DZMoney=(select isnull(Money,0) from JYOrder_Main_MD_HZ HZ where HZ.MHID=A.MHID)');
          sql.Add(' ,DZChaEMoney=(select isnull(ChaEMoney,0) from JYOrder_Main_MD_HZ HZ where HZ.MHID=A.MHID)');
          sql.Add(' from YF_Money_CR A ');
          sql.Add(' where FactoryName='''+Trim(FFactoryName)+'''');
          //SQL.Add(' and CRID='+Trim(ClientDataSet2.fieldbyname('CRID').AsString));
          sql.Add(' and CRType=''应付款登记'' ');
          sql.Add(' and (Money-YiFuMoney)<>0');
          sql.Add(' order by CRTime,Money');
          Open;
        end;
        SCreateCDS20(ADOQueryTemp,ClientDataSet33);
        SInitCDSData20(ADOQueryTemp,ClientDataSet33);
        with ClientDataSet33 do
        begin
          First;
          while not Eof do
          begin
            if ClientDataSet33.FieldByName('DZMoney').Value<0 then
            begin
              if ClientDataSet33.FieldByName('DZChaEMoney').Value<>0 then
              begin
                Next;
              end;
            end;
            if ClientDataSet33.FieldByName('YingFuMoney').Value<=StrToFloat(FFKMoney) then  //如果应付金额小于等于付款金额
            begin
              if StrToFloat(FFKMoney)>0 then
              begin
                if GetLSNo(ADOQueryCmd,maxno,'YH','YF_Money_CR_HX',4,1)=False then
                begin
                  Result:=False;
                  ADOQueryCmd.Connection.RollbackTrans;
                  Application.MessageBox('取核销流水号失败!','提示',0);
                  Exit;
                end;
                with ADOQueryCmd do
                begin
                  Close;
                  sql.Clear;
                  sql.Add('select * from YF_Money_CR_HX where 1<>1');
                  Open;
                end;
                with ADOQueryCmd do
                begin
                  Append;
                  FieldByName('YHID').Value:=Trim(maxno);
                  FieldByName('YFID').Value:=Trim(ClientDataSet33.fieldbyname('YFID').AsString);
                  FieldByName('FKID').Value:=Trim(ClientDataSet66.fieldbyname('YFID').AsString);
                  FieldByName('HXMoney').Value:=ClientDataSet33.FieldByName('YingFuMoney').Value;
                  FieldByName('Filler').Value:=Trim(DName);
                  Post;
                end;

                with ADOQueryCmd do
                begin
                  Close;
                  sql.Clear;
                  sql.Add('Update YF_Money_CR Set YiFuMoney=(select isnull(Sum(HXMoney),0) from YF_Money_CR_HX A where A.YFID=YF_Money_CR.YFID)');
                  sql.Add(' where YFID='''+Trim(ClientDataSet33.fieldbyname('YFID').AsString)+'''');
                  ExecSQL;
                end;
                {ClientDataSet2.Locate('YFID',Trim(ClientDataSet3.fieldbyname('YFID').AsString),[]);
                with ClientDataSet2 do
                begin
                  Edit;
                  FieldByName('YiFuMoney').Value:=ClientDataSet2.FieldByName('Money').Value;
                  FieldByName('YFYMoney').Value:=0;
                  post;
                end;  }
                FFKMoney:=FloatToStr(StrToFloat(FFKMoney)-ClientDataSet33.FieldByName('YingFuMoney').Value);
                ClientDataSet33.Delete;
              end else
              begin
                ClientDataSet33.Last;
              end;
            end else
            //如果应付金额大于付款金额
            begin
              if StrToFloat(FFKMoney)=0 then
              begin
                ClientDataSet33.Last;
              end;
              if GetLSNo(ADOQueryCmd,maxno,'YH','YF_Money_CR_HX',4,1)=False then
              begin
                Result:=False;
                ADOQueryCmd.Connection.RollbackTrans;
                Application.MessageBox('取核销流水号失败!','提示',0);
                Exit;
              end;
              with ADOQueryCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('select * from YF_Money_CR_HX where 1<>1');
                Open;
              end;
              with ADOQueryCmd do
              begin
                Append;
                FieldByName('YHID').Value:=Trim(maxno);
                FieldByName('YFID').Value:=Trim(ClientDataSet33.fieldbyname('YFID').AsString);
                FieldByName('FKID').Value:=Trim(ClientDataSet66.fieldbyname('YFID').AsString);
                FieldByName('HXMoney').Value:=FFKMoney;
                FieldByName('Filler').Value:=Trim(DName);
                Post;
              end;
              with ADOQueryCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('Update YF_Money_CR Set YiFuMoney=(select isnull(Sum(HXMoney),0) from YF_Money_CR_HX A where A.YFID=YF_Money_CR.YFID)');
                sql.Add(' where YFID='''+Trim(ClientDataSet33.fieldbyname('YFID').AsString)+'''');
                ExecSQL;
              end;
              {ClientDataSet2.Locate('YFID',Trim(ClientDataSet3.fieldbyname('YFID').AsString),[]);
              with ClientDataSet2 do
              begin
                Edit;
                FieldByName('YiFuMoney').Value:=FieldByName('YiFuMoney').Value+strtofloat(FFKMoney);
                FieldByName('YFYMoney').Value:=FieldByName('Money').Value-FieldByName('YiFuMoney').Value;
                post;
              end; }
              FFKMoney:='0';
              ClientDataSet33.Delete;
              ClientDataSet33.Last;
            end;
          end;
        end;
        Result:=True;
      end;
      Next;
    end;
  end;



end;

function TfrmMDChkFL.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 FactoryName='''+Trim(CWFactoryName)+'''');
    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 FactoryName='''+Trim(CWFactoryName)+'''');
    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 TfrmMDChkFL.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 TfrmMDChkFL.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 TfrmMDChkFL.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('Money').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 TfrmMDChkFL.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:='已核对';
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,ClientDataSet1,'JYOrder_Main_MD_HZ',2);
          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 TfrmMDChkFL.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 TfrmMDChkFL.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 Trim(ClientDataSet1.fieldbyname('OrderNo').AsString)='' then
  begin
    Application.MessageBox('订单号不能为空!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_HZ ');
    sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
    Open;
  end;
  if Trim(ADOQueryTemp.fieldbyname('OrderNo').AsString)='' 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 TfrmMDChkFL.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 TfrmMDChkFL.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,ClientDataSet1);
  SInitCDSData20(ADOQueryMain,ClientDataSet1);
end;

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

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

procedure TfrmMDChkFL.ToolButton9Click(Sender: TObject);
begin
  if cxTabControl2.TabIndex<>2 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;}
  if ClientDataSet1.Locate('SSel;ChkStatus',VarArrayOf([True,'处理中']),[loPartialKey]) then
  begin
    Application.MessageBox('数据正在处理中不能执行操作!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;ChkStatus',VarArrayOf([True,'处理完成']),[loPartialKey]) then
  begin
    Application.MessageBox('数据已处理完成不能执行操作!','提示',0);
    Exit;
  end;
  {if Trim(ADOQueryTemp.FieldByName('ChkStatus').AsString)='处理中'  then
  begin
    Application.MessageBox('数据正在处理中不能执行操作!','提示',0);
    Exit;
  end;
  if Trim(ADOQueryTemp.FieldByName('ChkStatus').AsString)='处理完成'  then
  begin
    Application.MessageBox('数据已处理完成不能执行操作!','提示',0);
    Exit;
  end;}
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  ClientDataSet1.DisableControls;
  with ClientDataSet1 do
  begin
    First;
    while ClientDataSet1.Locate('SSel',True,[]) do
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('UPdate JYOrder_Main_MD_HZ Set ChkStatus=''待处理'',SSTime=Getdate(),SSPerson='''+Trim(DName)+'''');
        sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
        sql.Add('Update YF_Money_CR Set Valid=''N'' ');
        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=(select CRID from YF_Money_CR AA where AA.MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+''')');
        ExecSQL;
      end;
      Edit;
      FieldByName('SSel').Value:=False;
      FieldByName('ChkStatus').Value:='待处理';
      FieldByName('SSTime').Value:=Now;
      Post;
    end;
  end;
  ClientDataSet1.EnableControls;
end;

procedure TfrmMDChkFL.ToolButton10Click(Sender: TObject);
begin
  if cxTabControl2.TabIndex<>2 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;}
  if ClientDataSet1.Locate('SSel;ChkStatus',VarArrayOf([True,'处理中']),[loPartialKey]) then
  begin
    Application.MessageBox('数据正在处理中不能执行操作!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel;ChkStatus',VarArrayOf([True,'处理完成']),[loPartialKey]) then
  begin
    Application.MessageBox('数据已处理完成不能执行操作!','提示',0);
    Exit;
  end;
  {if Trim(ADOQueryTemp.FieldByName('ChkStatus').AsString)='处理中'  then
  begin
    Application.MessageBox('数据正在处理中不能执行操作!','提示',0);
    Exit;
  end;
  if Trim(ADOQueryTemp.FieldByName('ChkStatus').AsString)='处理完成'  then
  begin
    Application.MessageBox('数据已处理完成不能执行操作!','提示',0);
    Exit;
  end;}
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  ClientDataSet1.DisableControls;
  with ClientDataSet1 do
  begin
    First;
    while ClientDataSet1.Locate('SSel',True,[]) do
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('UPdate JYOrder_Main_MD_HZ Set ChkStatus=Null,SSTime=Null,SSPerson=Null');
        sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
        sql.Add('Update YF_Money_CR Set Valid=''Y'' ');
        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=(select CRID from YF_Money_CR AA where AA.MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+''')');
        ExecSQL;
      end;
      Edit;
      FieldByName('SSel').Value:=False;
      FieldByName('ChkStatus').Value:='';
      FieldByName('SSTime').Value:=Null;
      Post;
    end;
  end;
  ClientDataSet1.EnableControls;
end;

procedure TfrmMDChkFL.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 TfrmMDChkFL.CheckBox1Click(Sender: TObject);
begin
  PS.Text:='';
  Qty.Text:='';
  Money.Text:='';
  Button2.Click;
end;

procedure TfrmMDChkFL.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(orderNo.Text)='' then Exit;
    if Length(Trim(orderNo.Text))<3 then Exit;
    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(',YSFKDate=(select Top 1 YC.CRTime from YF_Money_CR YC inner join YF_Money_CR_HX HX  ');
      sql.Add('           on YC.YFID=HX.FKID where HX.YFID=B.YFID)');
      sql.Add(',YSFKMoney=(select Top 1 YC.Money from YF_Money_CR YC inner join YF_Money_CR_HX HX  ');
      sql.Add('           on YC.YFID=HX.FKID where HX.YFID=B.YFID)');
      sql.Add(',KCMoney=(select KCMoney from YF_Money_KC KC where KC.CRID=B.CRID)');
      sql.Add('from JYOrder_Main_MD_HZ A ');
      sql.Add(' left join YF_Money_CR B on A.MHID=B.MHID where 1=1');
      if cxTabControl2.TabIndex=0 then
      begin
        sql.Add(' and isnull(A.Status,'''')='''' ');
      end else
      if cxTabControl2.TabIndex<3 then
      begin
        sql.Add(' and isnull(A.Status,'''')<>'''' ');
      end;
      if cxTabControl2.TabIndex=1 then
      begin
        sql.Add(' and isnull(A.DuiZhangStatus,'''')='''' ');
      end else
      if cxTabControl2.TabIndex=2 then
      begin
        sql.Add(' and isnull(A.DuiZhangStatus,'''')=''已对账'' ');
      end;
      sql.Add(' and A.OrderNo like '''+'%'+Trim(orderNo.Text)+'%'+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmMDChkFL.Button1Click(Sender: TObject);
begin
  if cxTabControl2.TabIndex<2 then Exit;
  Panel4.Visible:=True;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select YMR.* ');
    SQL.Add(',BQHXMoney=(select isnull(Sum(HXMoney),0) from YF_Money_CR_HX BB where exists( select * from');
    sql.Add('(select C.FKID,B.YFID ');
    sql.Add('from JYOrder_Main_MD_HZ A ');
    sql.Add(' inner join YF_Money_CR B on A.MHID=B.MHID ');
    sql.Add(' inner join YF_Money_CR_HX C on B.YFID=C.YFID');
    sql.Add(' where A.FactoryName='''+Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)+'''');
    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;
    if cxTabControl2.TabIndex=0 then
    begin
      sql.Add(' and isnull(A.Status,'''')='''' ');
    end else
    if cxTabControl2.TabIndex<3 then
    begin
      sql.Add(' and isnull(A.Status,'''')<>'''' ');
    end;
    if cxTabControl2.TabIndex=1 then
    begin
      sql.Add(' and isnull(A.DuiZhangStatus,'''')='''' ');
    end else
    if cxTabControl2.TabIndex=2 then
    begin
      sql.Add(' and isnull(A.DuiZhangStatus,'''')=''已对账'' ');
    end;
    SQL.Add(' )VVV where VVV.FKID=BB.FKID and VVV.YFID=BB.YFID and VVV.FKID=YMR.YFID)');
    sql.Add(')');
    sql.Add(' ,HXMoney=isnull((select isnull(Sum(HXMoney),0) from YF_Money_CR_HX BB where BB.FKID=YMR.YFID),0)');
    sql.Add(' ,DHXMoney=isnull(YMR.Money,0)-isnull((select isnull(Sum(HXMoney),0) from YF_Money_CR_HX BB where BB.FKID=YMR.YFID),0)');
    sql.Add(' from YF_Money_CR YMR where exists(select * from ');
    sql.Add('(select C.FKID ');
    sql.Add('from JYOrder_Main_MD_HZ A ');
    sql.Add(' inner join YF_Money_CR B on A.MHID=B.MHID ');
    sql.Add(' inner join YF_Money_CR_HX C on B.YFID=C.YFID');
    sql.Add(' where A.FactoryName='''+Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)+'''');
    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;

    if cxTabControl2.TabIndex=0 then
    begin
      sql.Add(' and isnull(A.Status,'''')='''' ');
    end else
    if cxTabControl2.TabIndex<3 then
    begin
      sql.Add(' and isnull(A.Status,'''')<>'''' ');
    end;
    if cxTabControl2.TabIndex=1 then
    begin
      sql.Add(' and isnull(A.DuiZhangStatus,'''')='''' ');
    end else
    if cxTabControl2.TabIndex=2 then
    begin
      sql.Add(' and isnull(A.DuiZhangStatus,'''')=''已对账'' ');
    end;
    SQL.Add(' )VV where VV.FKID=YMR.YFID)');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet5);
  SInitCDSData20(ADOQueryTemp,ClientDataSet5);
end;

end.
