unit U_YFMXlistMonDayGQXEY;

interface

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

type
  TfrmYFMXlistMonDayGQXEY = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxCRTime: TcxGridDBColumn;
    cxMoneyYe: TcxGridDBColumn;
    cxQty: TcxGridDBColumn;
    cxQtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Price: TcxGridDBColumn;
    VMoney: TcxGridDBColumn;
    v2BBMoney: TcxGridDBColumn;
    v2MPRTCodeName: TcxGridDBColumn;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    ToolButton3: TToolButton;
    RMXLSExport1: TRMXLSExport;
    v2Column1: TcxGridDBColumn;
    v2note: TcxGridDBColumn;
    Panetime: TPanel;
    v2Column2: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    Panel2: TPanel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Panel3: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    FBegDate: TDateTimePicker;
    FEndDate: TDateTimePicker;
    Panel4: TPanel;
    Label5: TLabel;
    factoryName: TBtnEditA;
    RadioGroup1: TRadioGroup;
    PopupMenu1: TPopupMenu;
    N6: TMenuItem;
    N7: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    v2Column3: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    Panel7: TPanel;
    N8: TMenuItem;
    N9: TMenuItem;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    Panel6: TPanel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Label15: TLabel;
    Label16: TLabel;
    ToolButton2: TToolButton;
    v2Column11: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    Label22: TLabel;
    P_CodeName: TComboBox;
    Label23: TLabel;
    PType: TComboBox;
    Panel9: TPanel;
    LabGYS: TLabel;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    N10: TMenuItem;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure factoryNameBtnClick(Sender: TObject);
    procedure v2Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column3CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure ToFactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure N3Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure N9Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure v2PricePropertiesEditValueChanged(Sender: TObject);
    procedure N10Click(Sender: TObject);
  private
    { Private declarations }
    FFBegDate,FFEndDate:TDateTime;
    procedure InitGrid();

  public
    { Public declarations }
    RKFlag,FCYID:String;
    fType,FMX:string;
    PState:Integer;

  end;

var
  frmYFMXlistMonDayGQXEY: TfrmYFMXlistMonDayGQXEY;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_RTFun,U_ZdyAttachGYS, U_KHListSelJJ,
  U_GYSListSelJJ,U_GYSFKInPut,U_GYSFKListView;

{$R *.dfm}

procedure TfrmYFMXlistMonDayGQXEY.InitGrid();
var
  FYue,FDate,fsj:string;
  FInt:Integer;
begin
  if RadioGroup1.ItemIndex=0 then
  begin
    if cxTabControl1.TabIndex<11 then
    begin
      FInt:=cxTabControl1.TabIndex+2;
    end else
    begin
      FInt:=1;
    end;
    FYue:=Trim(IntToStr(FInt));
    if FInt<10 then
    begin
      FYue:='0'+Trim(IntToStr(FInt));
    end;
    if FInt<>1 then
    begin
      FDate:=Trim(FormatDateTime('yyyy',begdate.Date))+'-'+FYue;
    end else
    begin
      FDate:=Trim(FormatDateTime('yyyy',begdate.Date));
      FDate:=IntToStr(strtoint(FDate)+1);
      FDate:=FDate+'-'+FYue;
    end;

    if FMX='' then
    begin
      FBegDate.Date:=StrToDate(FDate+'-01');
      if FInt=12 then
        FEndDate.Date:=StrToDate(Trim(FormatDateTime('yyyy',begdate.Date))+'-12-31')
      else begin
        if FInt=1 then
        begin
          FEndDate.Date:=StrToDate(FDate+'-31');
        end else
        begin
          FEndDate.Date:=StrToDate(Trim(FormatDateTime('yyyy',begdate.Date))+'-'+Trim(IntToStr(FInt+1))+'-01')-1;
        end;
      end;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if RadioGroup1.ItemIndex=0 then
    begin
      sql.Add('exec  P_YFFK_Month_HJ :FDate,:factoryNo');
      Parameters.ParamByName('FDate').Value:=Trim(FDate);
      Parameters.ParamByName('factoryNo').Value:=Trim(factoryName.TxtCode);
    end else
    if RadioGroup1.ItemIndex=1 then
    begin
      sql.Add('exec  P_YFFK_Day_HJ :FBegDate,:FEndDate,:factoryNo');
      Parameters.ParamByName('FBegdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FBegDate.Date));
      Parameters.ParamByName('FEndDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FEndDate.Date+1));
      Parameters.ParamByName('factoryNo').Value:=Trim(factoryName.TxtCode);
    end;
    Open;
  end;
  //SCSHDataNew(ADOQueryTemp,GroupBox4,2);
  LabGYS.Caption:=Trim(factoryName.Text)+ ' 总业务金额：'+Trim(ADOQueryTemp.fieldbyname('YFMoneyLJ').AsString)
                                        + ' 今年业务金额：'+Trim(ADOQueryTemp.fieldbyname('YFMoneyYear').AsString)
                                        +'  欠款系数：'+Trim(ADOQueryTemp.fieldbyname('QKXS').AsString)
                                        +'→总应付款：'+Trim(ADOQueryTemp.fieldbyname('HZMoney').AsString);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      if RadioGroup1.ItemIndex=0 then
      begin
        sql.Add(' exec  P_Qry_YFk_Month :FDate,:factoryNo');
        Parameters.ParamByName('FDate').Value:=Trim(FDate);
        Parameters.ParamByName('factoryNo').Value:=Trim(factoryName.TxtCode);
      end else
      if RadioGroup1.ItemIndex=1 then
      begin
        sql.Add(' exec  P_Qry_YFk_Day :FBegDate,:FEndDate,:factoryNo');
        Parameters.ParamByName('FBegdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FBegDate.Date));
        Parameters.ParamByName('FEndDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FEndDate.Date+1));
        Parameters.ParamByName('factoryNo').Value:=Trim(factoryName.TxtCode);
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
    fsj:='select Code,Name from('+
         ' select Code=Null, Case when CRType=''应付款登记'' and ISNULL(YFName,'''')='''' then A.P_Codename '+
         ' when CRType=''应付款登记'' and ISNULL(YFName,'''')<>'''' then A.P_Codename '+
         ' when CRType=''期初应付款登记'' then ''期初账款'' '+
         ' when CRType=''付款登记'' then '''' '+
         ' when CRType=''供应商费用管理'' then A.YFName'+
         ' else '''' end as Name '+
         ' from YS_Money_CR A where A.factoryNo='''+Trim(factoryName.TxtCode)+'''';
    if RadioGroup1.ItemIndex=0 then
    begin
      fsj:=fsj+' and convert(varchar(7),A.CRTime,120)='''+Trim(FDate)+'''';
    end else
    if RadioGroup1.ItemIndex=1 then
    begin
      fsj:=fsj+' and A.CRTime>='''+Trim(Trim(FormatDateTime('yyyy-MM-dd',FBegDate.Date)))+''''+
               ' and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',FEndDate.Date+1))+'''';
    end;
    fsj:=fsj+')AA where isnull(Name,'''')<>'''' group by Code,Name ';
    SInitComBoxBySql(ADOQueryTemp,P_CodeName,False,fsj);

    fsj:='select Code,Name from('+
         ' select Code=Null, Case when CRType=''应付款登记'' then A.YFName  '+
         ' when CRType=''期初应付款登记'' then '''' '+
         ' when CRType=''付款登记'' then ''付款'''+
         ' when CRType=''供应商费用管理''  then FKType  '+
         ' else '''' end as Name '+
         ' from YS_Money_CR A where A.factoryNo='''+Trim(factoryName.TxtCode)+'''';
    if RadioGroup1.ItemIndex=0 then
    begin
      fsj:=fsj+' and convert(varchar(7),A.CRTime,120)='''+Trim(FDate)+'''';
    end else
    if RadioGroup1.ItemIndex=1 then
    begin
      fsj:=fsj+' and A.CRTime>='''+Trim(Trim(FormatDateTime('yyyy-MM-dd',FBegDate.Date)))+''''+
               ' and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',FEndDate.Date+1))+'''';
    end;
    fsj:=fsj+')AA where isnull(Name,'''')<>'''' group by Code,Name ';
    SInitComBoxBySql(ADOQueryTemp,PType,False,fsj);
  finally
    ADOQueryMain.EnableControls;
    Panetime.Visible:=false;
    screen.Cursor:=crdefault;
  end;
end;


procedure TfrmYFMXlistMonDayGQXEY.FormDestroy(Sender: TObject);
begin
  frmYFMXlistMonDayGQXEY:=nil;
end;

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

procedure TfrmYFMXlistMonDayGQXEY.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应付款对账单YX1DZ',Tv2,'财务管理');
  Close;
end;
procedure TfrmYFMXlistMonDayGQXEY.FormShow(Sender: TObject);
var
  fstr:string;
begin
  ReadCxGrid('应付款对账单YX1DZ',Tv2,'财务管理');

  if Trim(FMX)='' then
  begin
    begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
    fstr:=Trim(FormatDateTime('MM',begdate.Date));
    cxTabControl1.TabIndex:=StrToInt(fstr)-1;
    Panel3.Visible:=False;
  end else
  begin
    RadioGroup1.ItemIndex:=PState;
    if RadioGroup1.ItemIndex=0 then
    begin
      Panel2.Visible:=True;
      Panel3.Visible:=False;
      cxTabControl1.Visible:=True;
    end else
    if RadioGroup1.ItemIndex=1 then
    begin
      Panel2.Visible:=False;
      Panel3.Visible:=True;
      cxTabControl1.Visible:=False;
    end;
    InitGrid();
  end;
end;

procedure TfrmYFMXlistMonDayGQXEY.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  cxTabControl1.Tabs[11].Caption:='  1月（'+inttostr(strtoint(FormatDateTime('yyyy',begdate.Date))+1)+'年)';
  InitGrid();
end;

procedure TfrmYFMXlistMonDayGQXEY.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,self.Caption);
end;

procedure TfrmYFMXlistMonDayGQXEY.FormCreate(Sender: TObject);
begin
  cxGrid2.Align:=AlClient;
end;

procedure TfrmYFMXlistMonDayGQXEY.ToolButton3Click(Sender: TObject);
var
  filepath,fsj:string;
  mBegdate,mEnddate:string;
begin
  fsj:=cxTabControl1.Tabs[cxTabControl1.TabIndex].Caption;
  mbegdate:=FormatDateTime('yyyy',begdate.Date)+'年'+Trim(Copy(fsj,3,Length(fsj)-2))+'份';
  if CDS_HZ.IsEmpty then exit;
  try
    filepath:=ExtractFilePath(Application.ExeName) + 'report\应付款对账单YX.rmf';
    if not FileExists(Pchar(filepath))  then
    begin
      application.MessageBox(pchar('文件['+filepath+']不存在！'),'提示信息',MB_IConError);
      exit;
    end;
    RMVariables['dtxz'] :=FormatDateTime('yyyy-mm-dd',date());
    RMVariables['zdr'] :=trim(DName);
    RmVariables['begDate'] :=mbegdate;
    RmVariables['endNum'] :=CDS_HZ.fieldbyname('MoneyYe').AsString;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;

  finally

  end;

end;

procedure TfrmYFMXlistMonDayGQXEY.factoryNameBtnClick(Sender: TObject);
begin
  try
    frmGYSListSelJJ:=TfrmGYSListSelJJ.Create(Application);
    with frmGYSListSelJJ do
    begin
      frmGYSListSelJJ.canshu2:='高权限';
      if ShowModal=1 then
      begin
        Self.factoryName.Text:=Trim(frmGYSListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
        Self.factoryName.TxtCode:=Trim(frmGYSListSelJJ.CDS_HZ.fieldbyname('KHNO').AsString);
      end;
    end;
  finally
    frmGYSListSelJJ.Free;
  end;
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmYFMXlistMonDayGQXEY.v2Column3PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:Boolean;
begin
  mvalue:=TcxCheckBox(Sender).EditValue;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('DZFlag').Value:=mvalue;
  end;
  if mvalue=True then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set DZFlag=1,DZTime=getdate(),DZPerson='''+Trim(DName)+'''');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set DZFlag=0,DZTime=getdate(),DZPerson='''+Trim(DName)+'''');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
  end; 
end;

procedure TfrmYFMXlistMonDayGQXEY.v2Column3CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id:Integer;
begin
  Id:=TV2.GetColumnByFieldName('DZFlag').Index;//;-TV1.GroupedItemCount;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id]=True then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
end;

procedure TfrmYFMXlistMonDayGQXEY.ToFactoryNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

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

procedure TfrmYFMXlistMonDayGQXEY.RadioGroup1Click(Sender: TObject);
begin
  if RadioGroup1.ItemIndex=0 then
  begin
    Panel2.Visible:=True;
    Panel3.Visible:=False;
    cxTabControl1.Visible:=True;
  end else
  if RadioGroup1.ItemIndex=1 then
  begin
    Panel2.Visible:=False;
    Panel3.Visible:=True;
    cxTabControl1.Visible:=False;
  end;
  InitGrid();
end;

procedure TfrmYFMXlistMonDayGQXEY.N6Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,True);
end;

procedure TfrmYFMXlistMonDayGQXEY.N7Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,False);
end;

procedure TfrmYFMXlistMonDayGQXEY.N4Click(Sender: TObject);
var
  FDate:TDateTime;
begin
  if CDS_HZ.IsEmpty then Exit;
  if CDS_HZ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  ToolBar1.SetFocus;
  try
    Panel2.Visible:=True;
    Panel2.Refresh;
    FDate:=SGetServerDateTime(ADOQueryTemp);
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while CDS_HZ.Locate('SSel',True,[]) do
      begin
        if Trim(CDS_HZ.fieldbyname('ChkStatusP').AsString)<>'已审核' then
        begin
          if Trim(CDS_HZ.fieldbyname('YFID').AsString)<>'' then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
              Open;
            end;
            with ADOQueryCmd do
            begin
              Edit;
              RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YS_Money_CR',7);
              FieldByName('ChkerP').Value:=Trim(DName);
              FieldByName('ChkTimeP').Value:=FDate;
              FieldByName('ChkStatusP').Value:='已审核';
              FieldByName('ChkerQ').Value:=Trim(DName);
              FieldByName('ChkTimeQ').Value:=FDate;
              FieldByName('ChkStatusQ').Value:='已审核';
              Post;
            end;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update YS_Money_CR Set ');
              sql.Add('HSMoney=Money');
              sql.Add(',JSMoney=Money');
              sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
              ExecSQL;
            end;
            with CDS_HZ do
            begin
              Edit;
              FieldByName('ChkStatusP').Value:='已审核';
              {FieldByName('ChkerP').Value:=Trim(DName);
              FieldByName('ChkTimeP').Value:=FDate;
              FieldByName('ChkStatusQ').Value:='已审核';
              FieldByName('ChkerQ').Value:=Trim(DName);
              FieldByName('ChkTimeQ').Value:=FDate;  }
              FieldByName('SSel').Value:=False;
              Post;
            end;
          end else
          begin
            with CDS_HZ do
            begin
              Edit;
              FieldByName('SSel').Value:=False;
              Post;
            end;
          end;

        end else
        begin
          with CDS_HZ do
          begin
            Edit;
            FieldByName('SSel').Value:=False;
            Post;
          end;
        end;
      end;
    end;
    CDS_HZ.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    TBRafresh.Click;
    Panel2.Visible:=False;
  except
    Panel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmYFMXlistMonDayGQXEY.N5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if CDS_HZ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  ToolBar1.SetFocus;
  try
    Panel2.Visible:=True;
    Panel2.Refresh;
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while CDS_HZ.Locate('SSel',True,[]) do
      begin
        if Trim(CDS_HZ.fieldbyname('ChkStatusP').AsString)<>'未审核' then
        begin
          if Trim(CDS_HZ.fieldbyname('YFID').AsString)<>'' then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
              Open;
            end;
            with ADOQueryCmd do
            begin
              Edit;
              FieldByName('ChkerP').Value:=Null;
              FieldByName('ChkTimeP').Value:=Null;
              FieldByName('ChkStatusP').Value:='未审核';
              FieldByName('ChkerQ').Value:=Null;
              FieldByName('ChkTimeQ').Value:=Null;
              FieldByName('ChkStatusQ').Value:='未审核';
              Post;
            end;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update YS_Money_CR Set ');
              sql.Add('HSMoney=Null');
              sql.Add(',JSMoney=Null');
              sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
              ExecSQL;
            end;
            with CDS_HZ do
            begin
              Edit;
              FieldByName('ChkStatusP').Value:='未审核';
             { FieldByName('ChkerP').Value:=Null;
              FieldByName('ChkTimeP').Value:=Null;
              FieldByName('ChkStatusQ').Value:='未审核';
              FieldByName('ChkerQ').Value:=Null;
              FieldByName('ChkTimeQ').Value:=Null;}
              FieldByName('SSel').Value:=False;
              Post;
            end;
          end else
          begin
            with CDS_HZ do
            begin
              Edit;
              FieldByName('SSel').Value:=False;
              Post;
            end;
          end;
        end else
        begin
          with CDS_HZ do
          begin
            Edit;
            FieldByName('SSel').Value:=False;
            Post;
          end;
        end;

      end;
    end;
    CDS_HZ.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    TBRafresh.Click;
    Panel2.Visible:=False;
  except
    Panel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmYFMXlistMonDayGQXEY.N1Click(Sender: TObject);
var
  FDate:TDateTime;
  DWYFID:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  ToolBar1.SetFocus;
  try
    FDate:=SGetServerDateTime(ADOQueryTemp);
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(CDS_HZ.fieldbyname('YFID').AsString)<>'' then
    begin
      DWYFID:=Trim(CDS_HZ.fieldbyname('YFID').AsString);
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Edit;
        RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YS_Money_CR',7);
        FieldByName('ChkerP').Value:=Trim(DName);
        FieldByName('ChkTimeP').Value:=FDate;
        FieldByName('ChkStatusP').Value:='已审核';
        FieldByName('ChkerQ').Value:=Trim(DName);
        FieldByName('ChkTimeQ').Value:=FDate;
        FieldByName('ChkStatusQ').Value:='已审核';
        Post;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CR Set ');
        sql.Add('HSMoney=Money');
        sql.Add(',JSMoney=Money');
        sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
        ExecSQL;
      end;
      with CDS_HZ do
      begin
        Edit;
        FieldByName('ChkStatusP').Value:='已审核';
        {FieldByName('ChkStatusQ').Value:='已审核';
        FieldByName('ChkTimeP').Value:=FDate;
        FieldByName('ChkTimeQ').Value:=FDate;
        FieldByName('ChkerP').Value:=Trim(DName);
        FieldByName('ChkerQ').Value:=Trim(DName); }
        Post;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    TBRafresh.Click;
    CDS_HZ.Locate('YFID',DWYFID,[]);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmYFMXlistMonDayGQXEY.N2Click(Sender: TObject);
var
  DWYFID:String;
begin
  if CDS_HZ.IsEmpty then Exit;
  ToolBar1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(CDS_HZ.fieldbyname('YFID').AsString)<>'' then
    begin
      DWYFID:=Trim(CDS_HZ.fieldbyname('YFID').AsString);
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Edit;
        FieldByName('ChkerP').Value:=Null;
        FieldByName('ChkTimeP').Value:=Null;
        FieldByName('ChkStatusP').Value:='未审核';
        FieldByName('ChkerQ').Value:=Null;
        FieldByName('ChkTimeQ').Value:=Null;
        FieldByName('ChkStatusQ').Value:='未审核';
        Post;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CR Set ');
        sql.Add('HSMoney=Null');
        sql.Add(',JSMoney=Null');
        sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
        ExecSQL;
      end;
      with CDS_HZ do
      begin
        Edit;
        FieldByName('ChkStatusP').Value:='未审核';
        {FieldByName('ChkStatusQ').Value:='未审核';
        FieldByName('ChkerQ').Value:=Null;
        FieldByName('ChkTimeQ').Value:=Null;
        FieldByName('ChkerP').Value:=Null;
        FieldByName('ChkTimeP').Value:=Null;}
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    TBRafresh.Click;
    CDS_HZ.Locate('YFID',DWYFID,[]);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmYFMXlistMonDayGQXEY.Tv2StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id:Integer;
begin
  Id:=Tv2.GetColumnByFieldName('ChkStatusP').Index;
  if ARecord.Values[id]='已审核' then exit;
  AStyle:=DataLink_Money.SkyBlue;
end;

procedure TfrmYFMXlistMonDayGQXEY.N3Click(Sender: TObject);
var
  DWYFID:string;
begin
  try
    frmGYSFKInPut:=TfrmGYSFKInPut.Create(Application);
    with frmGYSFKInPut do
    begin
      FMainId:='';
      frmGYSFKInPut.FactoryName.Text:=Self.factoryName.Text;
      frmGYSFKInPut.FactoryName.TxtCode:=Self.factoryName.TxtCode;
      if ShowModal=1 then
      begin
        DWYFID:=Trim(frmGYSFKInPut.FMainId);
        Self.TBRafresh.Click;
        Self.CDS_HZ.Locate('YFID',DWYFID,[]);
      end;
    end;
  finally
    frmGYSFKInPut.Free;
  end;
end;

procedure TfrmYFMXlistMonDayGQXEY.N8Click(Sender: TObject);
var
  DWYFID:string;
begin
  if Trim(CDS_HZ.fieldbyname('CRType').AsString)<>'付款登记' then Exit;
  DWYFID:=Trim(CDS_HZ.fieldbyname('YFID').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFID='''+Trim(DWYFID)+''' and  isnull(ChkerP,'''')<>'''' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('数据已审核不能修改!','提示',0);
    Exit;
  end;
  try
    frmGYSFKInPut:=TfrmGYSFKInPut.Create(Application);
    with frmGYSFKInPut do
    begin
      FMainId:=Trim(Self.CDS_HZ.fieldbyname('YFID').AsString);
      if ShowModal=1 then
      begin
        Self.TBRafresh.Click;
        Self.CDS_HZ.Locate('YFID',DWYFID,[]);
      end;
    end;
  finally
    frmGYSFKInPut.Free;
  end;
end;

procedure TfrmYFMXlistMonDayGQXEY.N9Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Trim(CDS_HZ.fieldbyname('CRType').AsString)<>'付款登记' then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFID='''+Trim(Self.CDS_HZ.fieldbyname('YFID').AsString)+''' and  isnull(ChkerP,'''')<>'''' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('数据已审核不能删除!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add(' insert into YS_Money_CR_DelLog ');
      sql.Add(' select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      sql.Add(' Update YS_Money_CR_DelLog Set DelTime=getdate(),DelPerson='''+Trim(DName)+'''');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      sql.Add(' delete YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.Delete;
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除失败!','提示',0);
  end;
end;

procedure TfrmYFMXlistMonDayGQXEY.ToolButton2Click(Sender: TObject);
var
  FName,FDate,FYue:string;
  FInt:Integer;
begin
  if CDS_HZ.IsEmpty then Exit;
  begin
    try
      frmGYSFKListView:=TfrmGYSFKListView.Create(Application);
      with frmGYSFKListView do
      begin
        FKHNO:=Trim(factoryName.TxtCode);
       // frmGYSFKListView.begdate.Date:=Self.FFBegDate;
        //frmGYSFKListView.Enddate.Date:=Self.FFEndDate;
        FactoryNameGYS.Text:=Trim(factoryName.Text);
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmGYSFKListView.Free;
    end;
  end;

end;

procedure TfrmYFMXlistMonDayGQXEY.P_CodeNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel4,1,2));
  SCreateCDS20(ADOQueryMain,CDS_HZ);
  SInitCDSData20(ADOQueryMain,CDS_HZ);
end;

procedure TfrmYFMXlistMonDayGQXEY.v2PricePropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FMoney,FPrice:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    with CDS_HZ do
    begin
      Edit;
      FieldByName('Price').Value:=Null;
      Post;
    end;
  end else
  begin
    with CDS_HZ do
    begin
      Edit;
      FieldByName('Price').Value:=mvalue;
      Post;
    end;
  end;
  FQty:=Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty)='' then FQty:='0';
  FPrice:=Trim(CDS_HZ.fieldbyname('Price').AsString);
  if Trim(FPrice)='' then FPrice:='0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('YFMoney').Value:=StrToFloat(FQty)*StrToFloat(FPrice);
    Post;
  end;
end;

procedure TfrmYFMXlistMonDayGQXEY.N10Click(Sender: TObject);
var
  FPrice,FMoney,FNote:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  FPrice:=Trim(CDS_HZ.fieldbyname('Price').AsString);
  FMoney:=Trim(CDS_HZ.fieldbyname('YFMoney').AsString);
  FNote:=Trim(CDS_HZ.fieldbyname('Note').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YS_Money_CR Set ');
    if Trim(FPrice)<>'' then
    begin
      sql.Add(' Price='+FPrice);
    end else
    begin
      sql.Add(' Price=Null');
    end;
    sql.Add(' ,Note='''+Trim(FNote)+'''');
    if Trim(FMoney)<>'' then
    begin
      sql.Add(' ,Money='+FMoney);
      sql.Add(' ,JSMoney='+FMoney);
      sql.Add(' ,HSMoney='+FMoney);
    end else
    begin
      sql.Add(' ,Money=Null');
      sql.Add(' ,JSMoney=Null');
      sql.Add(' ,HSMoney=Null');
    end;
    sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
    ExecSQL;
  end;
end;

end.
