unit U_SDManage;

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, cxPC, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, cxDropDownEdit, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmSDmanage = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    TBfind: TToolButton;
    Label1: TLabel;
    factoryName: TEdit;
    ADOQueryMain: TADOQuery;
    TBexcel: TToolButton;
    RM1: TRMGridReport;
    RMDB_MD: TRMDBDataSet;
    Label3: TLabel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TBAdd: TToolButton;
    v2Column9: TcxGridDBColumn;
    TBEdit: TToolButton;
    WXFPNO: TEdit;
    v2Column6: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    SDNO: TEdit;
    Label7: TLabel;
    v2Column3: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    ToolButton1: TToolButton;
    cxTabControl1: TcxTabControl;
    TBChk: TToolButton;
    TBNochk: TToolButton;
    v2Column17: TcxGridDBColumn;
    TBPrint: TToolButton;
    AdoPrint: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column13: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    v1YSKeZhong: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    CDS_SUB: TClientDataSet;
    DS_SUB: TDataSource;
    Tv1Column3: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Label4: TLabel;
    kxtype: TEdit;
    Tv2Column2: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Label8: TLabel;
    CheckBox2: TCheckBox;
    Label5: TLabel;
    BZTYPE: TEdit;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    TV3Column3: TcxGridDBColumn;
    TV3Column4: TcxGridDBColumn;
    TV3Column5: TcxGridDBColumn;
    TV3Column6: TcxGridDBColumn;
    TV3Column7: TcxGridDBColumn;
    TV3Column8: TcxGridDBColumn;
    TV3Column9: TcxGridDBColumn;
    TV3Column10: TcxGridDBColumn;
    CDS_DC: TClientDataSet;
    DS_DC: TDataSource;
    ToolButton2: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBfindClick(Sender: TObject);
    procedure TBexcelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FeeTypeChange(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBChkClick(Sender: TObject);
    procedure TBNochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Tv2Column2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    procedure SetStatus();
    function YSData(): Boolean;
  public
    fSYRName: string;
    fflag: integer;
    canshu1: string;
    { Public declarations }

  end;

var
  frmSDmanage: TfrmSDmanage;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_SDMANAGEINPUT, U_FjList10;

{$R *.dfm}
function TfrmSDmanage.YSData(): Boolean;
var
  CRID, YFID, Price, PriceUnit, OrderUnit, FComTaiTou: string;
begin
  Result := False;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(CDS_HZ.fieldbyname('factoryname').asstring) + '''');
    sql.Add(' and YFDefFlag1=0 ');
    Open;
  end;
  if not ADOQueryTemp.IsEmpty 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(CDS_HZ.fieldbyname('factoryname').asstring);
      FieldByName('ZdyStr1').Value := '应收收';
      Post;
    end;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete from YF_Money_CR where MainID=''' + Trim(CDS_HZ.fieldbyname('CRNO').asstring) + ''' and YFName=''扣损''  and isnull(mainID,'''')<>'''' ');
    execsql;
  end;

  if GetLSNo(ADOQueryCmd, YFID, 'CS', 'YF_Money_CR', 3, 1) = False then
  begin
    Application.MessageBox('取应收最大号失败!', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where 1=2');
    Open;
  end;
  with ADOQueryCmd do
  begin
    Append;
    FieldByName('YFID').Value := Trim(YFID);
    FieldByName('YFTypeId').Value := Trim(CDS_HZ.fieldbyname('CRNO').asstring);
    FieldByName('CRID').Value := StrToInt(CRID);
    FieldByName('Filler').Value := Trim(DName);
    FieldByName('CRType').Value := '应收款登记';
    FieldByName('CRFlag').Value := '应收收';
    FieldByName('QtyFlag').Value := 1;
    FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('factoryName').asstring);
    FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CDS_HZ.fieldbyname('CRTime').AsDateTime));
    FieldByName('YFType').Value := '自动生成';
    FieldByName('Price').Value := 0;
    FieldByName('PS').Value := -1 * 1;
    FieldByName('Qty').Value := -1 * CDS_HZ.fieldbyname('Qty').AsFloat;
    FieldByName('Price').Value := CDS_HZ.fieldbyname('Price').AsFloat;
    FieldByName('money').Value := CDS_HZ.fieldbyname('money').AsFloat;
    FieldByName('BBmoney').Value := CDS_HZ.fieldbyname('money').AsFloat;
    FieldByName('P_CodeName').Value := CDS_HZ.fieldbyname('C_CodeName').AsString;
    FieldByName('P_Color').Value := CDS_HZ.fieldbyname('C_Color').AsString;
    FieldByName('HuiLv').Value := 1;
    FieldByName('BZType').Value := '人民币';
    FieldByName('QtyUnit').Value := CDS_HZ.fieldbyname('QtyUnit').AsString;
    FieldByName('ComTaiTou').Value := Trim(CDS_HZ.fieldbyname('factoryName').asstring);
    FieldByName('YFName').Value := '扣损';
    FieldByName('MainId').Value := Trim(CDS_HZ.fieldbyname('CRNO').asstring);
    FieldByName('subID').Value := '';
    FieldByName('SYRName').Value := '宏强现货';
    FieldByName('note').Value := CDS_HZ.fieldbyname('Note').AsString;
    FieldByName('status').Value := '0';
    Post;
  end;

  Result := True;
end;

procedure TfrmSDmanage.SetStatus();
begin
  TBedit.Visible := false;
  TBdel.Visible := false;
  TBchk.Visible := false;
  TBnochk.Visible := false;

  if canshu1 = '业务员登记' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          TBedit.Visible := true;
          TBdel.Visible := true;
        end;
      1:
        begin
        end;
      2:
        begin
        end;
    end;
  end
  else
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          TBedit.Visible := true;
          TBdel.Visible := true;
          TBchk.Visible := true;
        end;
      1:
        begin
          TBnochk.Visible := true;
        end;
      2:
        begin
        end;
    end;
  end;

end;

procedure TfrmSDmanage.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      SQL.Clear;
      SQL.Add('SELECT * FROM (');
      sql.Add(' select A.*   ');
      SQL.Add(',WHXJE=MONEY-isnull((SELECT SUM(HXNUM) FROM yf_money_cr_SUB B WHERE B.YFID=A.YFID),0)');
      sql.Add(',STUFF(( SELECT  '', '' +convert(VARCHAR, kxtype) FROM YF_Money_CR_SUB b where b.yfid=a.yfid FOR XML PATH('''')), 1, 1, '''') AS kxtype');
      sql.Add(' from yf_money_cr A where  ');

      sql.Add(' CRtype=''收款登记'' AND SKTYPE=''水单''');
      if cxTabControl1.TabIndex < 2 then
      begin
        sql.Add('and status=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
      end;
      if CheckBox1.Checked = True then
      begin
        sql.Add('and ZSKP>0');
      end;

      SQL.Add(')AA ');

//      sql.Add('or WHXJE>10');
      if CheckBox2.Checked = True then
      begin
        sql.Add('where WHXJE>10');
      end
      else
      begin
        sql.Add('  where CRTime>=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + ''' ');
        sql.Add('and CRTime<''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
        sql.Add('or WHXJE>10');
      end;

      sql.Add('ORDER BY FILLTIME DESC');
//      ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);

  finally
    ADOQueryMain.EnableControls;
    TBfind.Click;
  end;
end;

procedure TfrmSDmanage.FormDestroy(Sender: TObject);
begin
  frmSDmanage := nil;
end;

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

procedure TfrmSDmanage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption + Tv2.Name, Tv2);
  WriteCxGrid(Self.Caption + Tv1.Name, Tv1);
  Close;
end;

procedure TfrmSDmanage.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete YF_Money_CR where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
      sql.Add('delete YF_Money_CR_Sub where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmSDmanage.FormShow(Sender: TObject);
begin
  ReadCxGrid(Self.Caption + Tv2.Name, Tv2);
  ReadCxGrid(Self.Caption + Tv1.Name, Tv1);
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime - 30;

  SetStatus();
  initGrid();
end;

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

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

procedure TfrmSDmanage.TBexcelClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid2);
end;

procedure TfrmSDmanage.TBAddClick(Sender: TObject);
var
  flzid: string;
begin
  try
    frmSDMANAGEINPUT := TfrmSDMANAGEINPUT.Create(Application);
    with frmSDMANAGEINPUT do
    begin
      fkeyNo := '';
      fflag := self.fflag;
      fSyrname := self.fSYRName;
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
        flzid := fkeyNo;

      end;
    end;
  finally
    frmSDMANAGEINPUT.Free;
  end;
  if flzid <> '' then
  begin
    CDS_HZ.locate('yfid', flzid, []);
  end;
end;

procedure TfrmSDmanage.FeeTypeChange(Sender: TObject);
begin
  TBfind.Click;
end;

procedure TfrmSDmanage.TBEditClick(Sender: TObject);
var
  flzid: string;
begin
  if CDS_HZ.IsEmpty then
    exit;
  try
    frmSDMANAGEINPUT := TfrmSDMANAGEINPUT.Create(Application);
    with frmSDMANAGEINPUT do
    begin
      fkeyNo := Trim(Self.CDS_HZ.FieldByName('yfid').AsString);
      fflag := self.fflag;
      fSyrname := self.fSYRName;
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
        flzid := fkeyNo;
      end;
    end;
  finally
    frmSDMANAGEINPUT.Free;
  end;
  if flzid <> '' then
  begin
    CDS_HZ.locate('yfid', flzid, []);
  end;
end;

procedure TfrmSDmanage.ToolButton1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  try
    frmFjList10 := TfrmFjList10.Create(Application);
    with frmFjList10 do
    begin
    //  PState:=1;
   //   if Trim(canshu1)<>'高权限'  then cxButton1.Visible:=false;
      fkeyNO := Trim(Self.CDS_HZ.fieldbyname('CRNO').AsString);
      fType := '扣损信息';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmFjList10.Free;
  end;
end;

procedure TfrmSDmanage.TBChkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  ADOQueryCmd.Connection.BeginTrans;
  try

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update YF_Money_CR SET status=''1'' ');
      sql.Add(' ,Chker=''' + Trim(DName) + '''');
      sql.Add(' ,Chktime=''' + formatdateTIme('yyyy-MM-dd', CDS_HZ.fieldbyname('CRTime').AsDateTime) + '''');
      sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
      execsql;

    end;

    ADOQueryCmd.Connection.CommitTrans;
   // application.MessageBox('审核撤销成功！','提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmSDmanage.TBNochkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update YF_Money_CR SET status=''0'',Chker=null,Chktime=null  ');
      sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
      execsql;
    end;

   // application.MessageBox('审核撤销成功！','提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('审核撤销失败！', '提示信息', 0);
  end;
end;

procedure TfrmSDmanage.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  initGrid();
end;

procedure TfrmSDmanage.TBPrintClick(Sender: TObject);
var
  filepath: string;
  i: integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if CDS_HZ.IsEmpty then
    exit;

  filepath := ExtractFilePath(Application.ExeName) + 'report\成品扣损单.rmf';

  if not FileExists(Pchar(filepath)) then
  begin
    application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
    exit;
  end;

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select * from BP_Problem');
    sql.Add('where CRNo=' + quotedstr(trim(CDS_HZ.fieldbyname('CRNo').AsString)));
    open;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(AdoPrint.fieldbyname('CRNO').AsString);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;
  RMVariables['QRBARCODE'] := fImagePath;
  RMVariables['dtxz'] := FormatDateTime('yyyy-mm-dd', date());
  RMVariables['zdr'] := trim(DName);
  RM1.LoadFromFile(filepath);
  RM1.ShowReport;

end;

procedure TfrmSDmanage.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from YF_Money_CR_SUB A');
    sql.Add('where YFID=' + Quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_SUB);
  SInitCDSData20(ADOQueryTemp, CDS_SUB);
end;

procedure TfrmSDmanage.Tv2Column2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id: Integer;
begin
  id := TV2.GetColumnByFieldName('WHXJE').Index; //;-TV1.GroupedItemCount;

  if AViewInfo.GridRecord.Values[id] > 10 then
  begin
    ACanvas.Brush.Color := $008080FF;
  end;
end;

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

procedure TfrmSDmanage.CheckBox2Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmSDmanage.ToolButton2Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    Filtered := False;
    SQL.Clear;
    SQL.Add('SELECT * FROM (');
    sql.Add(' select A.*,B.kxtype AS KXTYPE2,SUBWXFPNO,MXORDERNO,MXMONEY,HXNUM,SPNOTE,MXNOTE   ');
    SQL.Add(',WHXJE=MONEY-isnull((SELECT SUM(HXNUM) FROM yf_money_cr_SUB C WHERE C.YFID=A.YFID),0)');
    sql.Add(',CKQTY=(SELECT SUM(QTY) FROM CK_BanCP_CR F INNER JOIN JYOrder_Main E ON F.MAINID=E.MAINID WHERE E.ORDERNO=B.MXORDERNO AND f.CRFlag=''出库'')');
    sql.Add(',CKkgqty=(SELECT sum(kgqty) FROM CK_BanCP_CR F INNER JOIN JYOrder_Main E ON F.MAINID=E.MAINID WHERE E.ORDERNO=B.MXORDERNO AND f.CRFlag=''出库'')');

    sql.Add(',STUFF(( SELECT  '', '' +convert(VARCHAR, kxtype) FROM YF_Money_CR_SUB  C where C.yfid=a.yfid FOR XML PATH('''')), 1, 1, '''') AS kxtype');
    sql.Add(' from yf_money_cr A INNER JOIN yf_money_cr_SUB B ON A.YFID=B.YFID where  ');

    sql.Add(' A.CRtype=''收款登记'' AND A.SKTYPE=''水单''');
    if cxTabControl1.TabIndex < 2 then
    begin
      sql.Add('and A.status=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
    end;
    if CheckBox1.Checked = True then
    begin
      sql.Add('and ZSKP>0');
    end;

    SQL.Add(')AA ');

//      sql.Add('or WHXJE>10');
    if CheckBox2.Checked = True then
    begin
      sql.Add('where WHXJE>10');
    end
    else
    begin
      sql.Add('  where CRTime>=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + ''' ');
      sql.Add('and CRTime<''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
      sql.Add('or WHXJE>10');
    end;

    sql.Add('ORDER BY FILLTIME DESC');

    Open;
  end;
  SCreateCDS20(ADOQueryCmd, CDS_DC);
  SInitCDSData20(ADOQueryCmd, CDS_DC);

  if ADOQueryCmd.Active = False then
    Exit;
  SDofilter(ADOQueryCmd, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryCmd, CDS_DC);
  SInitCDSData20(ADOQueryCmd, CDS_DC);

  TcxGridToExcel(self.Caption + '明细', cxgrid3);
end;

end.

