unit U_YFCR;

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

type
  TfrmYFCR = 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;
    FactoryName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    v2Column1: TcxGridDBColumn;
    VBZ: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    VBB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    VOrderNo: TcxGridDBColumn;
    v2PayMent: TcxGridDBColumn;
    v2Note: TcxGridDBColumn;
    v2C_CodeName: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2ssel: TcxGridDBColumn;
    P_CodeName: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    P_spec: TEdit;
    Edit1: TEdit;
    Label7: TLabel;
    Button1: TButton;
    CheckBox1: TCheckBox;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    ToolButton4: TToolButton;
    v2OrderNO: TcxGridDBColumn;
    v2ComTaiTou: TcxGridDBColumn;
    v2MaoQty: TcxGridDBColumn;
    v2Qty: TcxGridDBColumn;
    v2PBNote: TcxGridDBColumn;
    Label8: TLabel;
    OrderNO: TEdit;
    Label9: TLabel;
    ComTaiTou: TEdit;
    YFName: TEdit;
    v2P_Color: TcxGridDBColumn;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    v2ChkStatus: TcxGridDBColumn;
    TKP: TToolButton;
    TCXKP: TToolButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v2chknote: TcxGridDBColumn;
    v2GCPrice: TcxGridDBColumn;
    v2GCMoney: TcxGridDBColumn;
    cxTabControl2: TcxTabControl;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    ConNoHZ: TEdit;
    v2ConNoHZ: 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 FactoryNameChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure cxGridDBColumn8PropertiesEditValueChanged(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 v2Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v2PSPropertiesEditValueChanged(Sender: TObject);
    procedure v2BatchNoPropertiesEditValueChanged(Sender: TObject);
    procedure v2PayMentPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2T_qtyPropertiesEditValueChanged(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure v2T_PricePropertiesEditValueChanged(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v2OrderNOPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2ComTaiTouPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2P_ColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2sselPropertiesChange(Sender: TObject);
    procedure Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure TKPClick(Sender: TObject);
    procedure TCXKPClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure v2KPTypePropertiesEditValueChanged(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
    procedure v2C_CodeNamePropertiesEditValueChanged(Sender: TObject);
    procedure v2Column4PropertiesEditValueChanged(Sender: TObject);
    procedure VYBPropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }

    procedure InitGridHZ();
    procedure InitGrid();
    function  SaveData():Boolean;

  public
    fFlag:integer;
    { Public declarations }
    RKFlag,FCYID,fmanage:String;

  end;

var
  frmYFCR: TfrmYFCR;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ProductOrderList, U_ProductOrderNewList,
  U_PbProductList, U_ZdyAttachGYS, U_SXCKNewList, U_ProductOrderListSel;

{$R *.dfm}


procedure TfrmYFCR.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered:=False;
      sql.Add(' select P_Color=isnull((select C.PRTColor from JYOrder_Sub C where C.Subid=A.ZSDSubid),(select C.PRTColor from CK_SXPB_CR B inner join JYOrder_Sub C on C.Subid=B.OrdSubIdRK ');
      sql.add('where B.SPID=A.MainID and isnull(B.CKName,'''')=''待检布'' and A.YFName<>''机物料费用'' and A.YFName<>''机物料退货费''))');
      SQL.ADD(' ,KPStatusH=(case when isnull(KPstatus,'''')='''' then ''未开票'' else ''开票完成'' end),A.*');
      sql.Add(',ConNoHZ=[dbo].[F_Get_Order_SubStr]((select B.Mainid from JYOrder_Main B where B.OrderNo=A.OrderNo),''ConNoHZ'')');
      sql.Add(' from YF_Money_CR A');
      sql.Add(' where A.CRTime>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
      sql.Add(' and A.CRTime<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      sql.Add(' and A.CRType=''应付款登记'' ');
      sql.add(' and isnull(FeeType,'''')<>''虚拟费用''');
      IF cxTabControl1.TabIndex<3 then
      sql.Add(' and isnull(A.status,''0'')='''+inttostr(cxTabControl1.TabIndex)+''' ');
      if cxtabControl2.TabIndex=0 then
      sql.Add(' and isnull(A.YFType,'''')=''自动生成'' ');
      if cxtabControl2.TabIndex=1 then
      sql.Add(' and isnull(A.YFType,'''')<>''自动生成'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
    Label12.Caption:='0';
    Label13.Caption:='0';
    Label17.Caption:='0';
    label19.Caption:='0';
  finally
    ADOQueryMain.EnableControls;
    //ToolButton2.Click;
  end;
end;

procedure TfrmYFCR.InitGridHZ();
begin
end;

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

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

procedure TfrmYFCR.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应付款登记ww',Tv2,'财务管理');
  {with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CK_YP_CR where isnull(CRTime,'''')='''' and CYID='''+Trim(FCYID)+''' and CRType='''+Trim(RKFlag)+'''');
    ExecSQL;
  end;}
  Close;
end;
procedure TfrmYFCR.TBDelClick(Sender: TObject);
var
  FBBMoney,CRID:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  if cxtabcontrol1.TabIndex<>0 then exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  with CDS_HZ do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').asboolean=true then
      begin
        if fieldbyname('YFName').AsString='机物料费用' then
        begin
          application.MessageBox('机物料费用不能删除','提示');
          exit;
        end;
        if fieldbyname('YFName').AsString='成品退货费' then
        begin
          application.MessageBox('成品退货费不能删除','提示');
          exit;
        end;
        if fieldbyname('YFName').AsString='成品回修费' then
        begin
          application.MessageBox('成品回修不能删除','提示');
          exit;
        end;
      end;
      next;
    end;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('delete YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmYFCR.FormShow(Sender: TObject);
begin
  ReadCxGrid('应付款登记ww',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=SGetServerDate(ADOQueryTemp)-7;
  TKP.Visible:=False;
  TCXKP.Visible:=False;
  InitGrid();
end;

procedure TfrmYFCR.TBRafreshClick(Sender: TObject);
begin
  toolbar1.SetFocus;
  InitGrid();
end;

procedure TfrmYFCR.ToolButton2Click(Sender: TObject);
var
  sql:string;
begin
  Label12.Caption:='0';
  Label13.Caption:='0';
  Label17.Caption:='0';
  Label19.Caption:='0';
  if ADOQueryMain.Active then
  begin
    sql:=SGetFilters(Panel1,1,2);
   { if sql='' THEN
    begin
      if trim(ComboBox1.Text)='加工费' then
      begin
        sql:=' LX like ''%加工%'' ';
      end
      else if trim(ComboBox1.Text)='坯布费' then
      begin
        sql:=' LX ='''' AND  ';
      end;
    end
    else
    begin
      if trim(ComboBox1.Text)='加工费' then
      begin
        sql:=sql+' AND LX like ''%加工%'' ';
      end
      else if trim(ComboBox1.Text)='坯布费' then
      begin
        sql:=sql+' AND LX = '''' ';
      end;
    end;   }
    SDofilter(ADOQueryMain,sql);
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

procedure TfrmYFCR.FactoryNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYFCR.Button2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(CDS_HZ.fieldbyname('SubID').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete YCL_SG_HZ where SubID='''+Trim(CDS_HZ.fieldbyname('SubID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

function TfrmYFCR.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)+''' ');
      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);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='应付款登记';
      FieldByName('CRFlag').Value:='应付付';
      FieldByName('QtyFlag').Value:=1;
      FieldByName('HuiLv').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);
      FieldByName('status').Value:='0';
      fieldbyname('BZType').Value:='￥';
      FieldByName('KPType').Value:=Trim(CDS_HZ.fieldbyname('KPType').AsString);
      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 TfrmYFCR.cxGridDBColumn8PropertiesEditValueChanged(
  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;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YCL_SG_HZ ');
    if Trim(mvalue)<>'' then
    begin
      sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
    end else
    begin
      sql.Add(' Set '+FFieldName+'=NULL');
    end;
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where SubId='''+Trim(CDS_HZ.fieldbyname('SubId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYFCR.cxGridDBColumn5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FHuiLv,FBBMoney,FKCBB,FFieldName:String;
begin
  {if Trim(CDS_HZ.FieldByName('YFType').AsString)='自动生成' then
  begin
    Application.MessageBox('自动生成的数据不能修改!','提示',0);
    Exit;
  end;}
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  {if FFieldName='MaoQty' then
  begin
    if Trim(mvalue)<>'0' then
    begin
      if CDS_HZ.FieldByName('JZQty').AsFloat>strtofloat(mvalue)-CDS_HZ.FieldByName('ps').AsFloat*0.3 then
      begin
        application.MessageBox('毛重不能小于净重加纸管的重量','提示');
        with CDS_HZ do
        begin
          Edit;
          FieldByName(FFieldName).Value:=NULL;
        end;
        exit;
      end;
    end;
  end;}
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    FieldByName('HuiLv').Value:=1;
    if trim(FFieldName)='MaoQty' then
    begin
      fieldbyname('Qty').AsString:=Trim(mvalue);
    end;
    Post;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(BBMoney,0) BBMoney from YF_Money_CR where YFID='''+cds_hz.fieldbyname('YFID').AsString+'''');
    Open;
  end;
  FKCBB:=ADOQueryTemp.fieldbyname('BBMoney').AsString;

  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';

  FHuiLv:=Trim(CDS_HZ.fieldbyname('HuiLv').AsString);
  if Trim(FHuiLv)='' then FHuiLv:='0';

  if Trim(FFieldName)<>'Money' then
  begin
    if Trim(FFieldName)<>'HuiLv' then
    begin
      FBBMoney:=FloatToStr(StrToFloat(FQty)*StrToFloat(FPrice)*StrToFloat(FHuiLv));
      FMoney:=FloatToStr(StrToFloat(FQty)*StrToFloat(FPrice));
    end else
    begin
      FBBMoney:=FloatToStr(StrToFloat(FMoney)*StrToFloat(FHuiLv));
    end;

  end else
  begin
    FBBMoney:=FloatToStr(StrToFloat(FMoney)*StrToFloat(FHuiLv));
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(FMoney);
    FieldByName('BBMoney').Value:=StrToFloat(FBBMoney);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    if Trim(FFieldName)<>'Money' then
    begin
      sql.Add(',Money='''+trim(CDS_HZ.fieldbyname('Money').AsString)+'''');
    end;
    if trim(FFieldName)='MaoQty' then
    begin
      sql.add(',Qty='+FQty);
    end;
    sql.Add(',BBMoney='+FBBMoney);
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');

    SQL.Add('Update YF_Money_KC Set KCBBMoney=KCBBMoney-'+FKCBB+'+'+FBBMoney);
    sql.Add(' where CRID='''+Trim(CDS_HZ.fieldbyname('CRID').AsString)+'''');
    ExecSQL;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmYFCR.cxGridDBColumn1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  if Trim(CDS_HZ.FieldByName('YFType').AsString)='自动生成' then
  begin
    Application.MessageBox('自动生成的数据不能修改!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    if Trim(mvalue)<>'' then
    begin
      sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
    end else
    begin
      sql.Add(' Set '+FFieldName+'=NULL');
    end;
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmYFCR.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('ZdyName').AsString);
          FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
          FieldByName('BZType').Value:='RMB';
          fieldbyname('KPType').Value:='不开';
          FieldByName('HuiLv').Value:=1;
          Post;
        end;
        Self.SaveData();
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmYFCR.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:=trim(DName)+'应付';
      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;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set YFCode='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
          sql.Add(' ,YFName='''+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 TfrmYFCR.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);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set QtyUnit='''+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 TfrmYFCR.v2Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='BZType';
      flagname:='币种';
      if ShowModal=1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('BZType').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set BZType='''+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 TfrmYFCR.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'应付款列表');
end;

procedure TfrmYFCR.v2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FBBMoney,FKCBB,FFieldName:String;
begin
  if Trim(CDS_HZ.FieldByName('YFType').AsString)='自动生成' then
  begin
    Application.MessageBox('自动生成的数据不能修改!','提示',0);
    Exit;
  end;
  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;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYFCR.v2PSPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,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;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYFCR.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;
    if FFieldName='GCPrice' then
    begin
      if Trim(mvalue)='' then mvalue:='0';
    end;
    if FFieldName='GCMoney' then
    begin
      if Trim(mvalue)='' then mvalue:='0';
    end;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    if FFieldName='GCPrice' then
    fieldbyname('GCMoney').Value:=fieldbyname('GCPrice').Value*fieldbyname('Qty').AsFloat;
    if FFieldName='GCMoney' then
    begin
      if fieldbyname('Qty').AsFloat<>0 then
      fieldbyname('GCPrice').Value:=fieldbyname('GCMoney').Value/fieldbyname('Qty').AsFloat;
    end;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
    if FFieldName='GCPrice' then
    begin
      sql.add(',GCMoney='''+trim(CDS_HZ.fieldbyname('GCMoney').AsString)+'''');
    end;
    if FFieldName='GCMoney' then
    begin
      sql.add(',GCPrice='''+trim(CDS_HZ.fieldbyname('GCPrice').AsString)+'''');
    end;
    if CDS_HZ.fieldbyname('GCMoney').AsFloat<>0 then
    sql.add(',BBMoney='''+trim(CDS_HZ.fieldbyname('GCMoney').AsString)+'''');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYFCR.v2PayMentPropertiesButtonClick(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:='PayMent';
      flagname:='支付方式';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('PayMent').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set PayMent='''+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 TfrmYFCR.v2T_qtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FHuiLv,FBBMoney,FKCBB,FFieldName,TMoney:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  TMoney:= format('%.2f',[CDS_HZ.Fieldbyname('T_Price').AsFloat*strtofloat(mvalue)]);

  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);

    FieldByName('T_Money').Value:=trim(TMoney);
    if Trim(DParameters2)='人民币' then
    begin
      FieldByName('HuiLv').Value:=1;
    end;
    Post;
  end;



  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));


    sql.Add(',T_Money='+TMoney);
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');

    ExecSQL;
  end;
end;

procedure TfrmYFCR.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 TfrmYFCR.CheckBox1Click(Sender: TObject);
begin
  Label12.Caption:='0';
  Label13.Caption:='0';
  IF CDS_HZ.IsEmpty then exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=checkbox1.Checked;
      if checkbox1.Checked=true then
      begin
        Label12.Caption:=floattostr(strtofloat(Label12.Caption)+fieldbyname('Qty').AsCurrency);
        Label13.Caption:=floattostr(strtofloat(Label13.Caption)+fieldbyname('Money').AsCurrency);
      end;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmYFCR.v2T_PricePropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FHuiLv,FBBMoney,FKCBB,FFieldName,TMoney:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  TMoney:= format('%.2f',[CDS_HZ.Fieldbyname('T_Qty').AsFloat*strtofloat(mvalue)]);

  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);

    FieldByName('T_Money').Value:=trim(TMoney);
    if Trim(DParameters2)='人民币' then
    begin
      FieldByName('HuiLv').Value:=1;
    end;
    Post;
  end;



  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));


    sql.Add(',T_Money='+TMoney);
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');

    ExecSQL;
  end;
end;

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

  Tchk.Enabled:=false;
  TNochk.Enabled:=false;
  TKP.Visible:=False;
  TCXKP.Visible:=False;
  //TBDel.Enabled:=false;
//  Ttj.Enabled:=false;
//  TnoTj.Enabled:=false;
  for i:=0 to tv2.ColumnCount-1 do
  begin
    IF uppercase(tv2.Columns[i].DataBinding.FieldName)<>'SSEL' then
    tv2.Columns[i].Options.Editing:=false;
  end;
  if (cxTabControl1.TabIndex>0) and (cxTabControl1.TabIndex<3) then
  begin
    TKP.Visible:=true;
    TCXKP.Visible:=true;
  end;
  IF fmanage='高权限' then
  begin
    if cxTabControl1.TabIndex=0 then
    begin
      Tchk.Enabled:=true;
      //TBDel.Enabled:=true;
      for i:=0 to tv2.ColumnCount-1 do
      begin
        tv2.Columns[i].Options.Editing:=true;
      end;
    end;
    if cxTabControl1.TabIndex=1 then
    begin
      TNochk.Enabled:=true;
    end;
  end
  else
  begin
    if cxTabControl1.TabIndex=0 then
    begin
      //Tchk.Enabled:=true;
      TBDel.Enabled:=true;
      for i:=0 to tv2.ColumnCount-1 do
      begin
        tv2.Columns[i].Options.Editing:=true;
      end;
    end;
    if cxTabControl1.TabIndex=1 then
    begin

    end;
  end;
  if cxTabControl1.TabIndex=1 then
  begin
    v2GCPrice.Options.Editing:=true;
  end;
  v2ComTaiTou.Options.Editing:=true;
  InitGrid();
end;

procedure TfrmYFCR.TchkClick(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  with CDS_HZ do
  begin
    first;
    while not eof do
    begin
      if cds_HZ.FieldByName('Ssel').AsBoolean=true then
      begin
        {if cds_HZ.FieldByName('KPType').AsString='' then
        begin
          application.MessageBox('开票类型不能为空','提示');
          exit;
        end;}
        {if cds_HZ.FieldByName('GCPrice').asfloat=cds_HZ.FieldByName('Price').asfloat then
        begin
          application.messagebox('公司单价与工厂单价不相等,不能提交','提示');
          exit;
        end;
        }
        if cds_HZ.FieldByName('QtyUnit').AsString='' then
        begin
          application.MessageBox('数量单位不能为空','提示');
          exit;
        end;
        if cds_HZ.FieldByName('ComTaiTou').AsString='' then
        begin
          application.MessageBox('公司抬头不能为空','提示');
          exit;
        end;
        {if cds_HZ.FieldByName('YFName').AsString='原料费' then
        begin
          if cds_HZ.FieldByName('OrderNO').AsString='' then
          begin
            application.MessageBox('指示单号不能为空','提示');
            exit;
          end;
        end;}
      end;
      next;
    end;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET status=''1'',ChkStatus='''' ');
            sql.Add('where YFID ='+quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('操作成功！','提示信息',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmYFCR.TnochkClick(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;

  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET status=''0'' ');
            sql.Add('where YFID ='+quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            sql.Add(' and status=''1''');
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('操作成功！','提示信息',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

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

procedure TfrmYFCR.v2OrderNOPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmProductOrderListSel:=TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      FFInt:=1;
      //frmProductOrderList.v1Column4.Visible:=True;
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('ZSDSubid').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('Subid').AsString);
          FieldByName('OrderNo').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('P_CodeName').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPrtCodeName').AsString);
          FieldByName('P_Spec').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('Mprtspec').AsString);
        end;
        with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate YF_Money_CR ');
            sql.Add(' Set OrderNO='''+Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNO').AsString)+'''');
            sql.Add(' ,P_CodeName='''+Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPrtCodeName').AsString)+'''');
            sql.Add(' ,P_Spec='''+Trim(frmProductOrderListSel.Order_Main.fieldbyname('Mprtspec').AsString)+'''');
            sql.Add(' ,ZSDSubid='''+Trim(frmProductOrderListSel.Order_Main.fieldbyname('Subid').AsString)+'''');
            SQL.Add(' where YFId='''+Trim(self.CDS_HZ.fieldbyname('YFId').AsString)+'''');
            ExecSQL;
          end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;  
end;

procedure TfrmYFCR.v2ComTaiTouPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ComTaiTou';
      flagname:='公司抬头';
      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 TfrmYFCR.v2P_ColorPropertiesButtonClick(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:='P_Color';
      flagname:='颜色';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('P_Color').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set P_Color='''+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 TfrmYFCR.v2sselPropertiesChange(Sender: TObject);
begin
  toolbar1.SetFocus;
  HJ(CDS_HZ,Label12,CDS_HZ.fieldbyname('Qty').AsCurrency);
  HJ(CDS_HZ,Label13,CDS_HZ.fieldbyname('Money').AsCurrency);
  HJ(CDS_HZ,Label17,CDS_HZ.fieldbyname('GCMoney').AsCurrency);
  HJ(CDS_HZ,Label19,CDS_HZ.fieldbyname('ps').AsCurrency);
end;

procedure TfrmYFCR.Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView;
  ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
  out AStyle: TcxStyle);
var
  id,id10:Integer;
begin
  Id:=Tv2.GetColumnByFieldName('ChkStatus').Index;
  if ARecord.Values[id]='不通过' then
  begin
    AStyle:=DataLink_YSYF.Red;
  end;
end;

procedure TfrmYFCR.TKPClick(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;

  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET KPStatus=''开票完成'' ');
            sql.Add('where YFID ='+quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('操作成功！','提示信息',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmYFCR.TCXKPClick(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;

  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET KPStatus='''' ');
            sql.Add('where YFID ='+quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('操作成功！','提示信息',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmYFCR.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,true);
end;

procedure TfrmYFCR.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,false);
end;

procedure TfrmYFCR.v2KPTypePropertiesEditValueChanged(Sender: TObject);
var mvalue,FFieldName:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('KPType').Value:=Trim(mvalue);
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set KPType='''+Trim(mvalue)+'''');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYFCR.cxTabControl2Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYFCR.v2C_CodeNamePropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FBBMoney,FKCBB,FFieldName:String;
begin
  if Trim(CDS_HZ.FieldByName('YFType').AsString)='自动生成' then
  begin
    Application.MessageBox('自动生成的数据不能修改!','提示',0);
    Exit;
  end;
  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;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYFCR.v2Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FBBMoney,FKCBB,FFieldName:String;
begin
  if Trim(CDS_HZ.FieldByName('YFType').AsString)='自动生成' then
  begin
    Application.MessageBox('自动生成的数据不能修改!','提示',0);
    Exit;
  end;
  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;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYFCR.VYBPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FBBMoney,FKCBB,FFieldName:String;
begin
  if Trim(CDS_HZ.FieldByName('YFType').AsString)='自动生成' then
  begin
    Application.MessageBox('自动生成的数据不能修改!','提示',0);
    Exit;
  end;
  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;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    SQL.Add(',BBMoney='+Trim(mvalue));
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

end.
