unit U_ZZYSCRSave;

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, cxDropDownEdit;

type
  TfrmZZYSCRSave = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName10: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    V2CRTime: TcxGridDBColumn;
    V2YFName: TcxGridDBColumn;
    V2Qty: TcxGridDBColumn;
    V2QtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    v2Price: TcxGridDBColumn;
    V2Money: TcxGridDBColumn;
    v2FactoryName: TcxGridDBColumn;
    VOrderNo: TcxGridDBColumn;
    v2Note: TcxGridDBColumn;
    v2P_CodeName: TcxGridDBColumn;
    v2PS: TcxGridDBColumn;
    v2P_Spec: TcxGridDBColumn;
    P_CodeName: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    ComTaiTou: TEdit;
    Edit1: TEdit;
    Label7: TLabel;
    Button1: TButton;
    ToolButton4: TToolButton;
    v2Column5: TcxGridDBColumn;
    YFName: TComboBox;
    v2Column7: TcxGridDBColumn;
    Label8: TLabel;
    P_Spec: TEdit;
    v2P_Code: TcxGridDBColumn;
    ToolButton5: TToolButton;
    CheckBox1: TCheckBox;
    Label9: TLabel;
    YFTypeID: TEdit;
    ToolButton6: TToolButton;
    Label6: TLabel;
    P_Code: TEdit;
    ToolButton7: TToolButton;
    v2Column3: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    Label10: TLabel;
    QtyStr: TEdit;
    Label11: TLabel;
    MoneyStr: TEdit;
    ToolButton8: TToolButton;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    Label12: TLabel;
    Label13: TLabel;
    SQGCCode: TEdit;
    SQBatchNoHZ: TEdit;
    v2Column4: TcxGridDBColumn;
    Label14: TLabel;
    HSType: TComboBox;
    v2Column6: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    ToolButton9: 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 ToolButton2Click(Sender: TObject);
    procedure FactoryName10Change(Sender: TObject);
    procedure cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure YFNameChange(Sender: TObject);
    procedure v2Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure FactoryName10KeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure v2PriceCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure v2Column11CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure v2Column11PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ToolButton9Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGridHZ();
    procedure InitGrid();
    function  SaveData():Boolean;
    procedure InitYFName();
  public
    fFlag:integer;
    { Public declarations }
    RKFlag,FCYID,fmanage:String;

  end;

var
  frmZZYSCRSave: TfrmZZYSCRSave;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ProductOrderList, U_ProductOrderNewList,
  U_PbProductList, U_ZdyAttachment, U_SXCKNewList, U_ProductOrderListSel
  ,U_ContractSelList,U_RTFun,U_PBCKNewList,U_SXCKXSOutList,U_PBTHNewList;

{$R *.dfm}



procedure TfrmZZYSCRSave.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered:=False;
      sql.Add(' select A.* ,QtyStr=dbo.[F_Get_ShuZiStr](A.Qty),MoneyStr=dbo.[F_Get_ShuZiStr](A.Money)');
      sql.Add(',SCPrice=(select Top 1 Price from YF_Money_CR YS where YS.P_Code=A.P_Code and YS.YFID<>A.YFID ');
      sql.Add(' and YS.FactoryName=A.FactoryName');
      sql.Add(' and YS.CRType=''应收款登记'' and isnull(YS.Price,0)<>0 and YS.CRTime<=A.CRTime order by CRTime desc)');
      sql.Add(',SCKHName=(select Top 1 FactoryName from YF_Money_CR YS where YS.P_Code=A.P_Code and YS.YFID<>A.YFID ');
      sql.Add(' and YS.FactoryName=A.FactoryName');
      sql.Add(' and YS.CRType=''应收款登记'' and isnull(YS.Price,0)<>0 and YS.CRTime<=A.CRTime order by CRTime desc)');
      sql.Add(',SCDate=(select Top 1 CRTime from YF_Money_CR YS where YS.P_Code=A.P_Code and YS.YFID<>A.YFID ');
      sql.Add(' and YS.FactoryName=A.FactoryName');
      sql.Add(' and YS.CRType=''应收款登记'' and isnull(YS.Price,0)<>0 and YS.CRTime<=A.CRTime order by CRTime desc)');
      sql.Add(',FactoryName10=dbo.getPinYin(A.FactoryName)+RTrim(A.FactoryName)');
      sql.Add(',SQGCCode=isnull(B.SQGCCode,CM.GCCode),SQBatchNoHZ=isnull(B.SQBatchNoHZ,CS.BatchNoHZ)');
      sql.Add(' from YF_Money_CR A');
      sql.Add(' left join FHSQ_Main B on A.YFTypeID=B.FSID');
      sql.Add(' left join CLoth_TH TH on A.YFTypeID=TH.THID ');
      sql.Add(' left join CLoth_Sub CS on TH.MainId=CS.MainId ');
      sql.Add(' left join CLoth_Main CM on TH.MainId=CM.MainId ');
      if CheckBox1.Checked then
      begin
        sql.Add('where A.CRTime>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
        sql.Add(' and CRTime<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      end else
      begin
        sql.Add('where A.CRTime>=''2000-01-01'' ');
        sql.Add(' and CRTime<''2500-01-01'' ');
      end;
      sql.Add(' and A.CRType=''应收款登记''  ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;
procedure TfrmZZYSCRSave.InitGridHZ();
begin
end;

procedure TfrmZZYSCRSave.FormDestroy(Sender: TObject);
begin
 // frmYFCR:=nil;
  frmZZYSCRSave:=nil;
end;

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

procedure TfrmZZYSCRSave.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应收款登记LJ2',Tv2,'财务管理');
  Close;
end;
procedure TfrmZZYSCRSave.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(' insert into YF_Money_CR_DelLog ');
      sql.Add(' select * from YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      sql.Add(' Update YF_Money_CR_DelLog Set DelTime=getdate(),DelPerson='''+Trim(DName)+'''');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      sql.Add(' delete YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate FHSQ_Main Set YSID=Null where FSID='''+Trim(CDS_HZ.fieldbyname('YFTypeId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate CLoth_TH Set YSID=Null,THPrice=Null,THMoney=Null where THID='''+Trim(CDS_HZ.fieldbyname('YFTypeId').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除失败!','提示',0);
  end;

end;

procedure TfrmZZYSCRSave.FormShow(Sender: TObject);
begin
  ReadCxGrid('应收款登记LJ2',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
  InitYFName();
  InitGrid();
end;
procedure TfrmZZYSCRSave.InitYFName();
var
  fsj:string;
begin
  fsj:=' select distinct(YFName) Name,Code=''''  from YF_Money_CR where CRType=''应收款登记'' and isnull(YFName,'''')<>'''' ';
  SInitComBoxBySql(ADOQueryTemp,YFName,False,fsj);
end;

procedure TfrmZZYSCRSave.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
  InitYFName();
end;

procedure TfrmZZYSCRSave.ToolButton2Click(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 TfrmZZYSCRSave.FactoryName10Change(Sender: TObject);
begin
  ToolButton2.Click;
end;

function TfrmZZYSCRSave.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'YS','YF_Money_CR',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 where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value:=Trim(maxId);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='应收款登记';
      FieldByName('CRFlag').Value:='应收收';
      FieldByName('QtyFlag').Value:=1;
      FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',7);
      FieldByName('CRTime').Value:=formatdateTIme('yyyy-MM-dd',CDS_HZ.fieldbyname('CRTime').AsDateTime);
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('YFId').Value:=Trim(maxId);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmZZYSCRSave.cxGridDBColumn5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FShuiDian,FHSPrice,FHSMoney,FJSMoney,FFieldName:String;
begin

  mvalue:=TcxTextEdit(Sender).EditingText;
  {if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;  }
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    if Trim(mvalue)<>'' then
      FieldByName(FFieldName).Value:=Trim(mvalue)
    else
      FieldByName(FFieldName).Value:=Null;
    Post;
  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';
  FQty:=Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty)='' then FQty:='0';

  FMoney:=Trim(CDS_HZ.fieldbyname('Money').AsString);
  if Trim(FMoney)='' then FMoney:='0';

  if Trim(FFieldName)<>'Money' then
  begin
    FMoney:=FloatToStr(StrToFloat(FQty)*StrToFloat(FPrice));
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(FMoney);
    Post;
  end;
  FShuiDian:=Trim(CDS_HZ.fieldbyname('ShuiDian').AsString);
  if Trim(FShuiDian)='' then FShuiDian:='0';
 // if StrToFloat(FShuiDian)<>0 then
  if Trim(CDS_HZ.fieldbyname('ShuiDian').AsString)<>'' then
  begin
    with CDS_HZ do
    begin
      Edit;
      FieldByName('HSPrice').Value:=StrToFloat(FPrice)*(1+StrToFloat(FShuiDian)*0.01);
      FieldByName('HSMoney').Value:=StrToFloat(FPrice)*(1+StrToFloat(FShuiDian)*0.01)*StrToFloat(FQty);
      FieldByName('JSMoney').Value:=StrToFloat(FPrice)*(1+StrToFloat(FShuiDian)*0.01)*StrToFloat(FQty);
      FieldByName('HSType').Value:='含税';
      post;
    end;
  end else
  begin
    with CDS_HZ do
    begin
      Edit;
      FieldByName('HSPrice').Value:=Null;
      FieldByName('HSMoney').Value:=Null;
      FieldByName('HSType').Value:='不含税';
      FieldByName('JSMoney').Value:=StrToFloat(FMoney);
      post;
    end;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmZZYSCRSave.ToolButton3Click(Sender: TObject);
begin

  try
    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Append;
          FieldByName('FactoryNo').Value:=Trim(frmZdyAttachment.CDS_HZ.fieldbyname('ZdyCode').AsString);
          FieldByName('FactoryName').Value:=Trim(frmZdyAttachment.CDS_HZ.fieldbyname('ZdyNameZ').AsString);
          FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
          Post;
        end;
        Self.SaveData();
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmZZYSCRSave.cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('QtyUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmZZYSCRSave.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'应付款列表');
end;

procedure TfrmZZYSCRSave.Button1Click(Sender: TObject);
begin

  IF CDS_HZ.IsEmpty then exit;

  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        If fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR ');
            sql.Add('where YFID='+quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            open;
            edit;
            Fieldbyname('Price').Value:=strtofloatdef(edit1.Text,0);
            Fieldbyname('Money').Value:=format('%.2f',[Fieldbyname('Qty').AsFloat * strtofloatdef(edit1.Text,0) ]);
            Fieldbyname('BBMoney').Value:=format('%.2f',[Fieldbyname('Qty').AsFloat * strtofloatdef(edit1.Text,0) ]);
            post;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('数据保存失败！','提示信息',0);
  end;

end;

procedure TfrmZZYSCRSave.cxTabControl1Change(Sender: TObject);
var
  i:integer;
begin


end;

procedure TfrmZZYSCRSave.ToolButton4Click(Sender: TObject);
begin
  frmPBCKNewList:=TfrmPBCKNewList.create(self) ;
  with frmPBCKNewList do
  begin
    show;
  end;
end;

procedure TfrmZZYSCRSave.YFNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmZZYSCRSave.v2Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FHTaiTou';
      flagname:='公司抬头';
      if ShowModal=1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('ComTaiTou').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmZZYSCRSave.v2Column6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('FactoryNo').Value:=Trim(frmZdyAttachment.CDS_HZ.fieldbyname('ZdyCode').AsString);
          FieldByName('FactoryName').Value:=Trim(frmZdyAttachment.CDS_HZ.fieldbyname('ZdyNameZ').AsString);
          //Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set FactoryNo='''+Trim(frmZdyAttachment.CDS_HZ.fieldbyname('ZdyCode').AsString)+'''');
          sql.Add(' ,FactoryName='''+Trim(frmZdyAttachment.CDS_HZ.fieldbyname('ZdyNameZ').AsString)+'''');
          SQL.Add(',Editer='''+Trim(DName)+'''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where YFId='''+Trim(self.CDS_HZ.fieldbyname('YFId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmZZYSCRSave.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  begdate.SetFocus;
  if Application.MessageBox('确定要保存当前选中的数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',7);
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmZZYSCRSave.FactoryName10KeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(FactoryName10.Text)='' then Exit;
    ToolBar1.SetFocus;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        SQL.Clear;
        Filtered:=False;
        sql.Add(' select A.* ');
        sql.Add(',FactoryName10=dbo.getPinYin(A.FactoryName)+RTrim(A.FactoryName)');
        sql.Add(' from YF_Money_CR A where FactoryName='''+Trim(FactoryName10.Text)+'''');
        if CheckBox1.Checked then
        begin
          sql.Add(' and A.CRTime>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
          sql.Add(' and A.CRTime<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
        end;
        sql.Add(' and A.CRType=''应收款登记''  ');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,CDS_HZ);
      SInitCDSData20(ADOQueryMain,CDS_HZ);
    finally
      ADOQueryMain.EnableControls;
      ToolButton2.Click;
    end;
  end;
end;

procedure TfrmZZYSCRSave.ToolButton6Click(Sender: TObject);
begin
  frmSXCKXSOutList:=TfrmSXCKXSOutList.create(self) ;
  with frmSXCKXSOutList do
  begin
    show;
  end;
end;

procedure TfrmZZYSCRSave.ToolButton7Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  begdate.SetFocus;
  if Application.MessageBox('确定要批量保存当前页面的所有数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',7);
          FieldByName('Editer').Value:=Trim(DName);
          FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          Post;
        end;
        Next;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmZZYSCRSave.v2PriceCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id,Id10:Integer;
begin
  Id:=TV2.GetColumnByFieldName('SCPrice').Index;
  Id10:=TV2.GetColumnByFieldName('Price').Index;
  if AViewInfo.GridRecord.Values[Id]=null then Exit;
  if AViewInfo.GridRecord.Values[Id]<>AViewInfo.GridRecord.Values[Id10] then
  begin
    ACanvas.Font.Color:=clBlue;
    ACanvas.Font.Style:=[fsbold];
  end;

end;

procedure TfrmZZYSCRSave.v2Column11CustomDrawCell(
  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 TfrmZZYSCRSave.v2Column11PropertiesEditValueChanged(
  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 TfrmZZYSCRSave.ToolButton8Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  CopyAddRow(Tv2,CDS_HZ);
  with Self.CDS_HZ do
  begin
    Edit;
    FieldByName('YFID').Value:=Null;
    FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
    FieldByName('DZFlag').Value:=False;
    FieldByName('YFTypeID').Value:=Null;
    Post;
  end;
  SaveData();
end;

procedure TfrmZZYSCRSave.Tv2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Trim(CDS_HZ.fieldbyname('YFTypeId').AsString)<>'' then
  begin
    V2CRTime.Options.Focusing:=False;
    v2FactoryName.Options.Focusing:=False;
    v2PS.Options.Focusing:=False;
    V2Qty.Options.Focusing:=False;
    v2Price.Options.Focusing:=False;
    V2QtyUnit.Options.Focusing:=False;
    V2Money.Options.Focusing:=False;
    v2P_Code.Options.Focusing:=False;
    v2P_CodeName.Options.Focusing:=False;
    v2P_Spec.Options.Focusing:=False;
  end else
  begin
    V2CRTime.Options.Focusing:=True;
    v2FactoryName.Options.Focusing:=True;
    v2PS.Options.Focusing:=True;
    V2Qty.Options.Focusing:=True;
    v2Price.Options.Focusing:=True;
    V2QtyUnit.Options.Focusing:=True;
    V2Money.Options.Focusing:=True;
    v2P_Code.Options.Focusing:=True;
    v2P_CodeName.Options.Focusing:=True;
    v2P_Spec.Options.Focusing:=True;
  end;
end;

procedure TfrmZZYSCRSave.ToolButton9Click(Sender: TObject);
begin
  frmPBTHNewList:=TfrmPBTHNewList.create(self) ;
  with frmPBTHNewList do
  begin
    show;
  end;
end;

end.
