unit U_YFCRSave;

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
  TfrmYFCRSave = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName10: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2CRTime: TcxGridDBColumn;
    v2Qty: TcxGridDBColumn;
    v2QtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    v2Column1: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    VOrderNo: TcxGridDBColumn;
    v2Note: TcxGridDBColumn;
    v2P_CodeName: TcxGridDBColumn;
    v2ps: TcxGridDBColumn;
    v2P_Spec: TcxGridDBColumn;
    P_CodeName: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    P_spec: TEdit;
    ToolButton4: TToolButton;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    ToolButton5: TToolButton;
    CheckBox1: TCheckBox;
    ToolButton6: TToolButton;
    v2Column11: TcxGridDBColumn;
    Label6: TLabel;
    QtyStr: TEdit;
    Label10: TLabel;
    MoneyStr: TEdit;
    TBCopy: TToolButton;
    v2Column13: TcxGridDBColumn;
    Label12: TLabel;
    KPType: TComboBox;
    v2Column15: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    v2Column18: TcxGridDBColumn;
    v2SJQty: TcxGridDBColumn;
    Label1: TLabel;
    YunDanNo: TEdit;
    v2YunDanNo: TcxGridDBColumn;
    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 cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v2BatchNoPropertiesEditValueChanged(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v2Column7PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure FactoryName10KeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton6Click(Sender: TObject);
    procedure v2Column11CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure v2Column11PropertiesEditValueChanged(Sender: TObject);
    procedure TBCopyClick(Sender: TObject);
    procedure v2Column14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure YunDanNoKeyPress(Sender: TObject; var Key: Char);
    procedure v2P_CodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);

  private
    { Private declarations }

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

  end;

var
  frmYFCRSave: TfrmYFCRSave;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ZdyAttachGYS, U_SXCKNewList,U_RTFun,U_YCLView;

{$R *.dfm}
procedure TfrmYFCRSave.InitGrid();
begin
  ToolBar1.SetFocus;
  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(',FactoryName10=dbo.getPinYin(A.FactoryName)+RTrim(A.FactoryName)');
      sql.Add(' from YF_Money_CR A');
      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 TfrmYFCRSave.InitGridHZ();
begin
end;

procedure TfrmYFCRSave.FormDestroy(Sender: TObject);
begin
 // frmYFCR:=nil;
  frmYFCRSave:=nil;
end;

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

procedure TfrmYFCRSave.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应付款登记',Tv2,'财务管理');
  Close;
end;
procedure TfrmYFCRSave.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;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除失败!','提示',0);
  end;
end;

procedure TfrmYFCRSave.FormShow(Sender: TObject);
begin
  ReadCxGrid('应付款登记',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime;
  InitYFName();
  InitGrid();
end;
procedure TfrmYFCRSave.InitYFName();

begin

end;

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

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

function TfrmYFCRSave.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'YF','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_KC where FactoryName='''+Trim(CDS_HZ.fieldbyname('FactoryName').AsString)+''' ');
      sql.Add(' and KCType=''GYS'' ');
      Open;
    end;
    if ADOQueryCmd.IsEmpty=False then
    begin
      CRID:=ADOQueryCmd.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('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
        FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
        FieldByName('ZdyStr1').Value:='应付付';
        Post;
      end;
    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('CRID').Value:=StrToInt(CRID);
      //SSetSaveDataCDSNew(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',3);
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',7);
      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);
      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);
      FieldByName('CRID').Value:=StrToInt(CRID);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmYFCRSave.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);
      post;
    end;
  end else
  begin
    with CDS_HZ do
    begin
      Edit;
      FieldByName('HSPrice').Value:=Null;
      FieldByName('HSMoney').Value:=Null;
      FieldByName('JSMoney').Value:=StrToFloat(FMoney);
      post;
    end;
  end;


  
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmYFCRSave.cxGridDBColumn1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmYFCRSave.ToolButton3Click(Sender: TObject);
begin

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

procedure TfrmYFCRSave.cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  if Trim(CDS_HZ.FieldByName('YFType').AsString)='自动生成' then
  begin
    Application.MessageBox('自动生成的数据不能修改!','提示',0);
    Exit;
  end;
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFName';
      flagname:='项目及摘要';
      MainType:='付款';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('YFCode').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('YFName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYFCRSave.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 TfrmYFCRSave.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'应付款列表');
end;

procedure TfrmYFCRSave.v2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FBBMoney,FKCBB,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;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;

 
end;

procedure TfrmYFCRSave.v2BatchNoPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  
end;

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

procedure TfrmYFCRSave.ToolButton4Click(Sender: TObject);
begin
  frmSXCKNewList:=TfrmSXCKNewList.create(self) ;
  with frmSXCKNewList do
  begin
    show;
  end;
end;

procedure TfrmYFCRSave.v2Column7PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:Boolean;
begin
  mvalue:=TcxCheckBox(Sender).EditingValue;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('KPFlag').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update YF_Money_CR Set ');
    if mvalue=true then
    begin
      sql.Add(' KPFlag=1 ');
    end else
    begin
      sql.Add(' KPFlag=0 ');
    end;
    sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYFCRSave.v2Column8PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  if Trim(CDS_HZ.FieldByName('YFType').AsString)='自动生成' then
  begin
    Application.MessageBox('自动生成的数据不能修改!','提示',0);
    Exit;
  end;
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GSTT';
      flagname:='公司抬头';
      V1Name.Caption:='中文名称';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('ComTaiTou').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set ComTaiTou='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
          SQL.Add(',Editer='''+Trim(DName)+'''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYFCRSave.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;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set HSPrice=Price*(1+isnull(ShuiDian,0)*0.01)');
      sql.Add(',HSMoney=Price*(1+isnull(ShuiDian,0)*0.01)*Qty');
      sql.Add(',JSMoney=Price*(1+isnull(ShuiDian,0)*0.01)*Qty');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      sql.Add(' and isnull(KPType,'''')=''开票'' ');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set HSPrice=Null');
      sql.Add(',HSMoney=Null');
      sql.Add(',JSMoney=Money');
      sql.Add(',ShuiDian=Null');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      sql.Add(' and isnull(KPType,'''')=''不开票'' ');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmYFCRSave.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.*,QtyStr=dbo.[F_Get_ShuZiStr](A.Qty),MoneyStr=dbo.[F_Get_ShuZiStr](A.Money) ');
        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 TfrmYFCRSave.ToolButton6Click(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;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CR Set HSPrice=Price*(1+isnull(ShuiDian,0)*0.01)');
          sql.Add(',HSMoney=Price*(1+isnull(ShuiDian,0)*0.01)*Qty');
          sql.Add(',JSMoney=Price*(1+isnull(ShuiDian,0)*0.01)*Qty');
          sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          sql.Add(' and isnull(KPType,'''')=''开票'' ');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CR Set HSPrice=Null');
          sql.Add(',HSMoney=Null');
          sql.Add(',JSMoney=Money');
          sql.Add(',ShuiDian=Null');
          sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          sql.Add(' and isnull(KPType,'''')=''不开票'' ');
          ExecSQL;
        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 TfrmYFCRSave.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 TfrmYFCRSave.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 TfrmYFCRSave.TBCopyClick(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('YFTypeID').Value:=Null;
    FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
    FieldByName('DZFlag').Value:=False;
    Post;
  end;
  SaveData();
end;

procedure TfrmYFCRSave.v2Column14PropertiesButtonClick(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 TfrmYFCRSave.Tv2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Trim(CDS_HZ.fieldbyname('YFTYpe').AsString)='自动生成' then
  begin
    v2CRTime.Options.Editing:=False;
    v2Qty.Options.Editing:=False;
    v2YunDanNo.Options.Editing:=False;
    v2P_CodeName.Options.Editing:=False;
    v2P_Spec.Options.Editing:=False;
    v2ps.Options.Editing:=False;
    v2SJQty.Options.Editing:=False;
    v2QtyUnit.Options.Editing:=False;
  end else
  begin
    v2CRTime.Options.Editing:=True;
    v2Qty.Options.Editing:=True;
    v2YunDanNo.Options.Editing:=True;
    v2P_CodeName.Options.Editing:=True;
    v2P_Spec.Options.Editing:=True;
    v2ps.Options.Editing:=True;
    v2SJQty.Options.Editing:=True;
    v2QtyUnit.Options.Editing:=True;
  end;
end;

procedure TfrmYFCRSave.YunDanNoKeyPress(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.*,QtyStr=dbo.[F_Get_ShuZiStr](A.Qty),MoneyStr=dbo.[F_Get_ShuZiStr](A.Money) ');
        sql.Add(',FactoryName10=dbo.getPinYin(A.FactoryName)+RTrim(A.FactoryName)');
        sql.Add(' from YF_Money_CR A where YunDanNo like '''+'%'+Trim(YunDanNo.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 TfrmYFCRSave.v2P_CodeNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmYCLView:=TfrmYCLView.Create(Application);
    with frmYCLView do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('P_CodeName').Value:=Trim(frmYCLView.CDS_HZ.fieldbyname('SPName').AsString);
          FieldByName('P_Spec').Value:=Trim(frmYCLView.CDS_HZ.fieldbyname('SPSpec').AsString);
          FieldByName('QtyUnit').Value:=Trim(frmYCLView.CDS_HZ.fieldbyname('QtyUnit').AsString);
        end;
      end;
    end;
  finally
    frmYCLView.Free;
  end;
end;

end.


