unit U_YSCR;

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

type
  TfrmYSCR = 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;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TBADd: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    cxTabControl1: TcxTabControl;
    ToolButton3: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CDS_YFS: TClientDataSet;
    DS_YFS: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v2Column1: TcxGridDBColumn;
    Label5: TLabel;
    CPName: TEdit;
    Label7: TLabel;
    sktt: TEdit;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    v2Column19: TcxGridDBColumn;
    v2Column20: TcxGridDBColumn;
    v2Column21: TcxGridDBColumn;
    v2Column22: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    ToolButton4: TToolButton;
    FactoryName: TComboBox;
    Label1: TLabel;
    MXORDERNO: TEdit;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    MovePanel1: TMovePanel;
    Label4: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    kkreason: TEdit;
    KKMONEY: TEdit;
    KKNOTE: TEdit;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    KKQTY: TEdit;
    KKPRICE: TEdit;
    KKPS: TEdit;
    xiaogangfei: TEdit;
    shangjifei: TEdit;
    Tv2Column3: TcxGridDBColumn;
    Label15: TLabel;
    cpcode: TEdit;
    Tv2Column4: 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 TBADdClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure YFGSTypeChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure v2Column7PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column19PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column21PropertiesEditValueChanged(Sender: TObject);
    procedure KKQTYChange(Sender: TObject);
    procedure kkreasonKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
  public
    canshu1: string;
    canshu2: string;
    { Public declarations }
    FCYID: string;
    fmanage: string;
  end;

var
  frmYSCR: TfrmYSCR;

implementation

uses
  U_DataLink, U_Fun, U_SXCKNewList, U_YSSCList, U_YsScXsList, U_YSKInput,
  U_XXFPHXList;

{$R *.dfm}
procedure TfrmYSCR.SetStatus();
var
  i: integer;
begin

  Tchk.Visible := false;
  TNochk.Visible := false;
  TBDel.Visible := false;
  case cxTabControl1.TabIndex of
    0:
      begin
        Tchk.Visible := true;
        TBDel.Visible := true;
      end;
    1:
      begin
        TNochk.Visible := true;
      end;
  end;

//  IF fmanage='高权限' then
//  begin
//
//
//  end
//  else
//  begin
//
//  end;
end;

procedure TfrmYSCR.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
 
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select a.*,b.*,MXMONEYX=(ISNULL(B.QTY,0)*ISNULL(B.PRICE,0)),e.crtime as CKDATE  ');
      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(',CKDATE=(SELECT CRTIME FROM CK_SXPB_CR E WHERE E.SPID=B.YFPZNO )');
      sql.Add('from YF_Money_CR A inner join YF_Money_CR_sub B ON A.YFID=B.YFID');
      sql.Add('left join CK_SXPB_CR E on E.SPID=B.YFPZNO');
      sql.Add(' where ISNULL(e.crtime,A.CRTIME)>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(' and ISNULL(e.crtime,A.CRTIME)<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      sql.Add(' and A.CRType=''应收款登记''  ');
      if cxTabControl1.TabIndex < 2 then
        sql.Add(' and isnull(A.SHstatus,0)=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
      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 canshu2 <> '' then
      begin
        sql.Add('and isnull((SELECT SELLNAME FROM SalesContract_Main WHERE ConNo=(SELECT ConNo FROM JYOrder_Main WHERE OrderNo=B.MXOrderNo) ) ,A.SYRName )=' + QuotedStr(canshu2));
      end;
      SQL.Add('ORDER BY E.CRTIME');

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmYSCR.FormDestroy(Sender: TObject);
begin
  frmYSCR := nil;
end;

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

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

procedure TfrmYSCR.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=''' + 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
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSCR.FormShow(Sender: TObject);
var
  fsj: string;
begin

//  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '财务管理666');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime;
//  fsj := 'select distinct(YFGSType) name from YF_Money_CR where CRType=''应收款登记'' ';
//  SInitComBoxBySql(ADOQueryCmd, YFGSType, False, fsj);
  fsj := 'select distinct(FactoryName) Name from YF_Money_CR where CRType=''应收款登记''  order by FactoryName ';
  SInitComBoxBySql(ADOQueryCmd, FactoryName, False, fsj);
  SetStatus();
  InitGrid();
end;

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

procedure TfrmYSCR.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

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

procedure TfrmYSCR.Button2Click(Sender: TObject);
begin
  MovePanel1.Visible := False;
end;

procedure TfrmYSCR.TBADdClick(Sender: TObject);
begin
  try
    frmYSKInput := TfrmYSKInput.Create(self);
    with frmYSKInput do
    begin
      FYFID := '';
      canshu2 := SELF.canshu2;
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmYSKInput.free;
  end;
end;

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

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

procedure TfrmYSCR.FormCreate(Sender: TObject);
begin

  cxGrid2.Align := alclient;
     
  canshu2 := DParameters2;
end;

procedure TfrmYSCR.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;

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

procedure TfrmYSCR.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;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSCR.ToolButton3Click(Sender: TObject);
begin
  try
    frmYSKInput := TfrmYSKInput.Create(self);
    with frmYSKInput do
    begin
      FYFID := Trim(CDS_HZ.fieldbyname('YFID').AsString);
      canshu2 := SELF.canshu2;
      ToolButton1.Visible := false;
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmYSKInput.free;
  end;
end;

procedure TfrmYSCR.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 TfrmYSCR.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 TfrmYSCR.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from YF_Money_CR_SUB A');
    sql.Add('where YFID=' + Quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_YFS);
  SInitCDSData20(ADOQueryTemp, CDS_YFS);
end;

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

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

    end;
    free;
  end;
end;

procedure TfrmYSCR.ToolButton4Click(Sender: TObject);
begin
  if cds_hz.IsEmpty = True then
    Exit;

  if cxTabControl1.TabIndex <> 0 then
  begin
    Exit;
  end;
  MovePanel1.Visible := True;

//  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);
  kkreason.Text := '';
  KKPS.Text := '0';
  KKQTY.Text := '0';
  KKPRICE.Text := '0';
  xiaogangfei.text := '0';
  shangjifei.Text := '0';
  KKMONEY.Text := '0';
  KKNOTE.Text := '';
end;

procedure TfrmYSCR.Button1Click(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
//      First;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Do_SCTK');
        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)));


//        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(',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=(ISNULL(QTY,0)*ISNULL(PRICE,0)+isnull(xiaogangfei,0)+isnull(shangjifei,0))-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 TfrmYSCR.v2Column7PropertiesEditValueChanged(Sender: TObject);
var
  mvalues, FFieldName: string;
begin
  mvalues := TCXTextEdit(Sender).Text;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if mvalues = '' then
  begin
    mvalues := '';
  end;
  with CDS_HZ do
  begin
    edit;
    fieldbyname(FFieldName).Value := mvalues;
    post;
  end;
//  tv2.Controller.EditingController.ShowEdit();
//  if trim(mvalues) <> '' then
//  begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;

    sql.Add('Update YF_Money_CR_Sub set KKREASON=' + QuotedStr((mvalues)) + ' ');
    sql.Add(' where YFSID=''' + Trim((CDS_HZ.fieldbyname('yfsid').AsString)) + '''');
    ExecSQL;
  end;

  with CDS_HZ do
  begin
    Edit;
    CDS_HZ.fieldbyname('KKREASON').AsString := Trim(mvalues);
  end;

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

procedure TfrmYSCR.v2Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalues, FFieldName: string;
begin
  mvalues := TCXTextEdit(Sender).Text;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if mvalues = '' then
  begin
    mvalues := '0';
  end;
  with CDS_HZ do
  begin
    edit;
    fieldbyname(FFieldName).Value := mvalues;
    post;
  end;
  tv2.Controller.EditingController.ShowEdit();
//  if trim(mvalues) <> '' then
//  begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR_Sub set ');
    sql.Add('KKMONEY=' + Trim(mvalues));
    sql.Add(' where YFSID=''' + Trim(CDS_HZ.fieldbyname('yfsid').AsString) + '''');

    sql.Add('Update YF_Money_CR_Sub set MXMONEY=(ISNULL(QTY,0)*ISNULL(PRICE,0)+isnull(xiaogangfei,0)+isnull(shangjifei,0))-KKMONEY');
    sql.Add(' 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 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(CDS_HZ.fieldbyname('yfid').AsString) + '''');

    ExecSQL;
  end;
  with CDS_HZ do
  begin
    Edit;
    CDS_HZ.fieldbyname('KKMONEY').value := Trim(mvalues);
    CDS_HZ.fieldbyname('MXMONEY').value := StrToFloatDef(CDS_HZ.fieldbyname('MXMONEYZ').value, 0) - StrToFloatDef(mvalues, 0);
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmYSCR.v2Column19PropertiesEditValueChanged(Sender: TObject);
var
  mvalues, FFieldName: string;
begin
  mvalues := TCXTextEdit(Sender).Text;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if mvalues = '' then
  begin
    mvalues := '0';
  end;
  with CDS_HZ do
  begin
    edit;
    fieldbyname(FFieldName).Value := mvalues;
    post;
  end;
  tv2.Controller.EditingController.ShowEdit();
//  if trim(mvalues) <> '' then
//  begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR_Sub  ');
    SQL.Add(' Set ' + FFieldName + '=' + Trim(mvalues));
    sql.Add(' where YFSID=''' + Trim(CDS_HZ.fieldbyname('yfsid').AsString) + '''');

    sql.Add('Update YF_Money_CR_Sub set MXMONEY=(ISNULL(QTY,0)*ISNULL(PRICE,0)+isnull(xiaogangfei,0)+isnull(shangjifei,0))-KKMONEY');
    sql.Add(' 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 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(CDS_HZ.fieldbyname('yfid').AsString) + '''');

    ExecSQL;
  end;
  tv2.Controller.EditingController.ShowEdit();
  with CDS_HZ do
  begin
    Edit;
//    CDS_HZ.fieldbyname('KKMONEY').value := Trim(mvalues);
    CDS_HZ.fieldbyname('MXMONEYX').value := StrToFloatDef(CDS_HZ.fieldbyname('qty').asstring, 0) * StrToFloatDef(CDS_HZ.fieldbyname('price').asstring, 0);
    CDS_HZ.fieldbyname('MXMONEYZ').value := StrToFloatDef(CDS_HZ.fieldbyname('qty').asstring, 0) * StrToFloatDef(CDS_HZ.fieldbyname('price').asstring, 0) + StrToFloatDef(CDS_HZ.fieldbyname('xiaogangfei').asstring, 0) + StrToFloatDef(CDS_HZ.fieldbyname('shangjifei').asstring, 0);
    CDS_HZ.fieldbyname('MXMONEY').value := StrToFloatDef(CDS_HZ.fieldbyname('qty').asstring, 0) * StrToFloatDef(CDS_HZ.fieldbyname('price').asstring, 0) + StrToFloatDef(CDS_HZ.fieldbyname('xiaogangfei').asstring, 0) + StrToFloatDef(CDS_HZ.fieldbyname('shangjifei').asstring, 0) - StrToFloatDef(CDS_HZ.fieldbyname('kkmoney').asstring, 0);
  end;

end;

procedure TfrmYSCR.v2Column21PropertiesEditValueChanged(Sender: TObject);
var
  mvalues, FFieldName: string;
begin
  mvalues := TCXTextEdit(Sender).Text;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if mvalues = '' then
  begin
    mvalues := '0';
  end;
  with CDS_HZ do
  begin
    edit;
    fieldbyname(FFieldName).Value := mvalues;
    post;
  end;
  tv2.Controller.EditingController.ShowEdit();
//  if trim(mvalues) <> '' then
//  begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR_Sub  ');
    SQL.Add(' Set ' + FFieldName + '=' + QuotedStr((mvalues)));
    sql.Add(' where YFSID=''' + Trim(CDS_HZ.fieldbyname('yfsid').AsString) + '''');

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

end;

procedure TfrmYSCR.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);
end;

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

end.

