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, cxCheckComboBox, cxDropDownEdit, Menus, RM_e_Xls, MovePanel,
  cxLookAndFeels, cxLookAndFeelPainters, dxBarBuiltInMenu, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

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;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TBEdit: TToolButton;
    VBB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2ssel: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    TBAdd: TToolButton;
    CDS_YFS: TClientDataSet;
    DS_YFS: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MXOrderNo: TEdit;
    Label4: TLabel;
    CPName: TEdit;
    ToolButton3: TToolButton;
    RMXLSExport1: TRMXLSExport;
    ADOQueryPrint: TADOQuery;
    v2Column5: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column20: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    ToolButton4: TToolButton;
    MovePanel1: TMovePanel;
    Label5: TLabel;
    kkreason: TEdit;
    Label7: TLabel;
    KKMONEY: TEdit;
    Label8: TLabel;
    KKNOTE: TEdit;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    FactoryName: TComboBox;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Label9: TLabel;
    KKQTY: TEdit;
    Label10: TLabel;
    KKPRICE: TEdit;
    ToolButton5: TToolButton;
    Label11: TLabel;
    KKPS: TEdit;
    Label12: TLabel;
    xiaogangfei: TEdit;
    Label13: TLabel;
    shangjifei: TEdit;
    prtcolor: TEdit;
    Label14: TLabel;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    Tv2Column5: TcxGridDBColumn;
    Label15: TLabel;
    GANGNO: TEdit;
    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 ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure YFGSTypeChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure v2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure KKQTYChange(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure kkreasonKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
    CANSHU1: string;
  end;

var
  frmYFCR: TfrmYFCR;

implementation

uses
  U_DataLink, U_Fun, U_YFKInput, U_JXFPHXList;        

{$R *.dfm}

procedure TfrmYFCR.SetStatus();
var
  i: integer;
begin
  Tchk.Visible := false;
  TNochk.Visible := false;
  TBDel.Visible := false;
  if fmanage = '高权限' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          Tchk.Visible := true;
          TBDel.Visible := true;
        end;
      1:
        begin
          TNochk.Visible := true;
        end;
    end;
  end
  else
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          TBDel.Visible := true;
          Tchk.Visible := true;
        end;
      1:
        begin
          TNochk.Visible := true;
        end;
    end;
  end;
end;

procedure TfrmYFCR.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *,MXMONEYX=(ISNULL(QTY,0)*ISNULL(PRICE,0))  ');
    SQL.Add(',MXMONEYZ=(ISNULL(B.QTY,0)*ISNULL(B.PRICE,0)+isnull(B.xiaogangfei,0)+isnull(B.shangjifei,0))');
    SQL.ADD(' ,sktt=(SELECT SELLNAME FROM SalesContract_Main');
    SQL.Add(' WHERE ConNo=(SELECT ConNo FROM JYOrder_Main WHERE OrderNo=B.MXOrderNo)   ) ');

    SQL.Add(',GANGNO=(SELECT GANGNO FROM CK_SXPB_CR E WHERE B.YFPZNO=E.SPID)');

    sql.Add('from YF_Money_CR A inner join YF_Money_CR_sub B ON A.YFID=B.YFID');
    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=''应付款登记''  ');
    if CANSHU1 <> '管理' then
    begin
      SQL.Add('AND (a.FILLER =' + QuotedStr(Trim(dname)));
      sql.Add('or (select lidan from JYOrder_Main c where c.orderno=b.mxorderno)=' + QuotedStr(Trim(dname)));
      sql.Add('or (select ywy from JYOrder_Main c where c.orderno=b.mxorderno)=' + QuotedStr(Trim(dname)));
      sql.Add(')');
    end;
    if cxTabControl1.TabIndex < 2 then
      sql.Add(' and A.SHstatus=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_HZ);
  SInitCDSData20(ADOQueryMain, CDS_HZ);
end;

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

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

procedure TfrmYFCR.TBCloseClick(Sender: TObject);
begin
//  WriteCxGrid(trim(self.caption) + 'Tv1', Tv1, '财务管理');
  WriteCxGrid(trim(self.caption) + 'Tv2', Tv2, '财务管理6');
  Close;
end;

procedure TfrmYFCR.TBDelClick(Sender: TObject);
var
  FBBMoney, CRID: string;
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  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 then
        begin
          with ADOQueryTemp do
          begin
            close;
            sql.Clear;
            sql.Add('select * from FD_Invoicesub  where DZDNO=' + QuotedStr(CDS_HZ.fieldbyname('YFsID').AsString));
            Open;
          end;
          if ADOQueryTemp.IsEmpty = false then
          begin
            Application.MessageBox('数据已对账，禁止删除!', '提示', 0);
            ADOQueryCmd.Connection.RollbackTrans;
            Exit;
          end;
          if Fieldbyname('kxtype').AsString = '扣款' then
          begin
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
//              sql.Add('delete YF_Money_CR where YFID=' + Quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
              sql.Add('delete YF_Money_CR_Sub where YFSID=''' + Trim(CDS_HZ.fieldbyname('YFSID').AsString) + '''');
              sql.Add('UPDATE YF_MONEY_CR SET Money=(SELECT SUM(MXMONEY) FROM YF_Money_CR_sub a where a.yfid=YF_MONEY_CR.yfid)');
              sql.Add(',bbmoney=(SELECT SUM(MXMONEY) FROM YF_Money_CR_sub a where a.yfid=YF_MONEY_CR.yfid)');
              SQL.Add('where yfid=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
              execsql;
            end;
          end
          else
          begin
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('delete YF_Money_CR where YFID=' + Quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
              sql.Add('delete YF_Money_CR_Sub where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');

              sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
              sql.Add(' ' + quotedstr(trim(DName)));
              sql.Add(',getdate() ');
              sql.Add(',' + quotedstr(trim(self.Caption)));
              sql.Add(',' + quotedstr(trim('删除应付款')));
              sql.Add(',' + quotedstr(trim('单号:' + trim(CDS_HZ.FieldByName('YFPZNO').AsString))));
              sql.Add(',' + quotedstr(trim('成功')));
              sql.Add(')');
              execsql;
            end;
          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);
var
  fsj: string;
begin
//  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '财务管理6');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime - 30;

  fsj := 'select distinct(FactoryName) Name from YF_Money_CR where CRType=''应付款登记''  order by FactoryName ';
  SInitComBoxBySql(ADOQueryCmd, FactoryName, False, fsj);
//  fsj := 'select distinct(YFGSType) name from YF_Money_CR where CRType=''应付款登记'' ';
//  SInitComBoxBySql(ADOQueryCmd, YFGSType, False, fsj);

  SetStatus();
  InitGrid();
end;

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

procedure TfrmYFCR.ToolButton2Click(Sender: TObject);
var
  sql: string;
begin
  if ADOQueryMain.Active then
  begin
    sql := SGetFilters(Panel1, 1, 2);
    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
  MovePanel1.Visible := False;
end;

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

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

procedure TfrmYFCR.TBAddClick(Sender: TObject);
begin
  try
    frmYFKInput := TfrmYFKInput.Create(self);
    with frmYFKInput do
    begin
      FYFID := '';
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmYFKInput.free;
  end;
end;

procedure TfrmYFCR.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := true;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmYFCR.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := false;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmYFCR.TBEditClick(Sender: TObject);
begin
  try
    frmYFKInput := TfrmYFKInput.Create(self);
    with frmYFKInput do
    begin
      FYFID := Trim(CDS_HZ.fieldbyname('YFID').AsString);
      ToolButton1.Visible := false;
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmYFKInput.free;
  end;
end;

procedure TfrmYFCR.FormCreate(Sender: TObject);
begin
  fmanage := trim(DParameters1);
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;
  try
    ADOQueryCmd.Connection.BeginTrans;
    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('update YF_Money_CR SET SHstatus=''1''  ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  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 then
        begin
          if Fieldbyname('HZFPHX').AsFloat > 0 then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            EnableControls;
            application.MessageBox('不能撤销已核销数据！', '提示信息', 0);
            exit;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET  shstatus=''0'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYFCR.Tv1DblClick(Sender: TObject);
begin
  frmJXFPHXList := TfrmJXFPHXList.create(self);
  with frmJXFPHXList do
  begin
    FTONO := TRIM(CDS_YFS.Fieldbyname('YFSID').AsString);
    FFromNo := '';
    if showmodal = 1 then
    begin

    end;
    free;
  end;
end;

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

procedure TfrmYFCR.ToolButton3Click(Sender: TObject);
var
  filepath: string;
begin
  if CDS_HZ.IsEmpty then
    exit;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR A ');
    sql.add('left join YF_Money_CR_SUB B on A.yfid=B.yiid');
    sql.Add('where A.YFID=' + Quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
    Open;
  end;

  try

    filepath := ExtractFilePath(Application.ExeName) + 'report\应付款入库单.rmf';
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;

  finally

  end;

end;

procedure TfrmYFCR.ToolButton4Click(Sender: TObject);
begin
  if cds_hz.IsEmpty = True then
    Exit;
  MovePanel1.Visible := True;
  if Trim(CDS_HZ.fieldbyname('KKPRICE').AsString) = '' then
  begin
    KKPRICE.Text := '0';
  end
  else
  begin
    KKPRICE.Text := Trim(CDS_HZ.fieldbyname('KKPRICE').AsString);
  end;

  if Trim(CDS_HZ.fieldbyname('KKQTY').AsString) = '' then
  begin
    KKQTY.Text := '0';
  end
  else
  begin
    KKQTY.Text := Trim(CDS_HZ.fieldbyname('KKPRICE').AsString);
  end;

  kkreason.Text := Trim(CDS_HZ.fieldbyname('KKREASON').AsString);
  KKMONEY.Text := Trim(CDS_HZ.fieldbyname('KKMONEY').AsString);
  KKNOTE.Text := Trim(CDS_HZ.fieldbyname('KKNOTE').AsString);
  Edit1.Text := Trim(CDS_HZ.fieldbyname('YFSID').AsString);
  Edit2.Text := Trim(CDS_HZ.fieldbyname('YFID').AsString);

end;

procedure TfrmYFCR.Button1Click(Sender: TObject);
var
  MaxYFSID: string;
begin

  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Do_SCTK2');
        sql.Add('@yfsid=''' + Trim(Edit1.TEXT) + '''');
        sql.Add(',@yfid=''' + Trim(Edit2.TEXT) + '''');
        sql.Add(',@KKPS=' + FloatToStr(strtofloatdef(KKPS.Text, 0)));
        sql.Add(',@xiaogangfei=' + FloatToStr(strtofloatdef(xiaogangfei.Text, 0)));
        sql.Add(',@shangjifei=' + FloatToStr(strtofloatdef(shangjifei.Text, 0)));
        sql.Add(',@money=' + FloatToStr(strtofloatdef(KKMONEY.Text, 0)));
        sql.Add(',@price=' + FloatToStr(strtofloatdef(kkprice.Text, 0)));
        sql.Add(',@qty=' + FloatToStr(strtofloatdef(kkqty.Text, 0)));
        sql.Add(',@kkly=' + quotedstr(Trim(KKREASON.Text)));
        sql.Add(',@KKNOTE=' + quotedstr(Trim(KKNOTE.Text)));
        sql.Add(',@prtcolor=' + quotedstr(Trim(prtcolor.Text)));
//        ShowMessage(sql.Text);
        ExecSQL;
      end;
//      with ADOQueryCmd do
//      begin
//        Close;
//        sql.Clear;
//        sql.Add('Update YF_Money_CR_Sub set KKREASON=' + quotedstr(Trim(KKREASON.Text)));
//        sql.Add(',KKPRICE=' + Trim(KKPRICE.Text));
//        sql.Add(',KKQTY=' + Trim(KKQTY.Text));
//        sql.Add(',KKMONEY=' + Trim(KKMONEY.Text));
//        sql.Add(',kknote=' + quotedstr(Trim(KKNOTE.Text)));
//        sql.Add(' where YFSID=''' + Trim(Edit1.TEXT) + '''');
//
//        sql.Add('Update YF_Money_CR_Sub set MXMONEY=MXMONEY-KKMONEY');
//        sql.Add(' where YFSID=''' + Trim(Edit1.TEXT) + '''');
//
//        sql.Add('Update YF_Money_CR set MONEY=(SELECT SUM(MXMONEY) FROM YF_Money_CR_Sub A WHERE A.YFID=YF_Money_CR.YFID group by yfid)');
//        sql.Add(' ,BBMONEY=(SELECT SUM(MXMONEY) FROM YF_Money_CR_Sub A WHERE A.YFID=YF_Money_CR.YFID group by yfid)');
//        sql.Add(' where YFID=''' + Trim(Edit2.TEXT) + '''');
//        ExecSQL;
//      end;

    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('录入成功！', '提示', 0);
  except

    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('录入异常！', '提示', 0);
  end;
  MovePanel1.Visible := False;
  InitGrid();
end;

procedure TfrmYFCR.v2Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if CDS_HZ.FieldByName('crtype').AsString = '应付款登记' then
  begin
    mvalue := TcxTextEdit(Sender).EditingText;
    FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := mvalue;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update YF_Money_CR_Sub set  ' + FFieldName + '=' + QuotedStr(Trim(mvalue)));
      sql.Add('where YFSID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFSID').AsString)));

      ExecSQL;
    end;
    tv2.Controller.EditingController.ShowEdit();
//    InitGrid();
  end;
end;

procedure TfrmYFCR.KKQTYChange(Sender: TObject);
var
  FQTY, FPRICE, FMONEY, fXGF, FSJF: Double;
begin
  FQTY := StrToFloatDEF(KKQTY.Text, 0);
  FPRICE := StrToFloatDEF(KKPRICE.Text, 0);
  fXGF := StrToFloatDEF(xiaogangfei.Text, 0);
  FSJF := StrToFloatDEF(SHANGJIFEI.Text, 0);
  FMONEY := RoundFLOAT((FQTY * FPRICE + fXGF + FSJF), 2);
  KKMONEY.Text := FloatToStr(FMONEY);
//  FQTY := StrToFloatDEF(KKQTY.Text, 0);
end;

procedure TfrmYFCR.ToolButton5Click(Sender: TObject);
begin
  if cds_hz.IsEmpty = True then
    Exit;
  if cxTabControl1.TabIndex <> 0 then
  begin
    Exit;
  end;
  MovePanel1.Visible := True;
  Edit1.Text := Trim(CDS_HZ.fieldbyname('YFSID').AsString);
  Edit2.Text := Trim(CDS_HZ.fieldbyname('YFID').AsString);
  prtcolor.Text := Trim(CDS_HZ.fieldbyname('cpcolor').AsString);
  kkreason.Text := '';
  KKPS.Text := '0';
  KKQTY.Text := '0';
  KKPRICE.Text := '0';
  xiaogangfei.text := '0';
  shangjifei.Text := '0';
  KKMONEY.Text := '0';
  KKNOTE.Text := '';
end;

procedure TfrmYFCR.kkreasonKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    SelectNext(ActiveControl as TWinControl, True, True);
  end;
end;

end.

