unit U_SKCR_YWCX;

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

type
  TfrmSKCR_YWCX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    YFName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    VBZ: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    Label4: TLabel;
    OrderNo: TEdit;
    v2Column1: TcxGridDBColumn;
    v2DerateMoney: TcxGridDBColumn;
    v2ComTaitou: TcxGridDBColumn;
    Label10: TLabel;
    Label12: TLabel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    V1ConNo: TcxGridDBColumn;
    V1ConPerson1: TcxGridDBColumn;
    Order_RL: TClientDataSet;
    DataSource1: TDataSource;
    V1RLMoney: TcxGridDBColumn;
    V1RLDate: TcxGridDBColumn;
    v2YRLMoney: TcxGridDBColumn;
    v2chkstatus: TcxGridDBColumn;
    v2WRLMoney: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v2chknote: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    v2KHDKMoney: TcxGridDBColumn;
    v2SXMoney: TcxGridDBColumn;
    Label5: TLabel;
    v2GSType: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    Label7: TLabel;
    Money: TEdit;
    Label8: TLabel;
    note: TEdit;
    v2FKTaiTou: TcxGridDBColumn;
    Label9: TLabel;
    FKTaiTou: TEdit;
    v2IFKP: 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 v2Column4PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure VBBPropertiesEditValueChanged(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure v2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure v2DerateMoneyPropertiesEditValueChanged(Sender: TObject);
    procedure tchkClick(Sender: TObject);
    procedure tnochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v2ComTaitouPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2SselPropertiesChange(Sender: TObject);
    procedure Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure TSDFJClick(Sender: TObject);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure v2FKTypePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure CheckBox1Click(Sender: TObject);
    procedure MoneyChange(Sender: TObject);
    procedure v2FKTaiTouPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }
    lstPat: TStringList;
		
    procedure InitGridHZ();
    procedure InitGrid();
    function  SaveData():Boolean;
    Procedure SetStatus();
    procedure ReadINIFile10();
  public
    fflag:integer;
    { Public declarations }
    RKFlag,FCYID,fmanage:String;

  end;

var
  frmSKCR_YWCX: TfrmSKCR_YWCX;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ProductOrderList, U_ProductOrderNewList,
  U_ZdyAttachment, U_ProductOrderListSel,U_FjList10,U_SKCR_CX,U_YSCRPB_ZHYH;

{$R *.dfm}

Procedure TfrmSKCR_YWCX.SetStatus();
var
  i:integer;
begin
  for i:=0 to tv2.ColumnCount-1 do
  begin
    tv2.Columns[i].Options.Editing:=false;
  end;
end;

procedure TfrmSKCR_YWCX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select A.*,');
      sql.add('IFSD=cast((case when isnull((select T.TFID from TP_File T where T.WBID=A.YFID and T.TFType=''SD''),'''')='''' then 0 else 1 end) as bit)');
      sql.Add(' from YF_Money_CR A');
      sql.Add(' left join YF_Money_KC B on A.CRID=B.CRID');
      sql.Add(' where A.CRTime>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
      sql.Add(' and CRTime<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      sql.Add(' and A.CRType=''收款登记'' and A.YFDefFlag1='''+inttostr(fflag)+''' ');
      sql.Add(' and A.status=''2'' ');
      if Money.Text<>'' then
      begin
        sql.add('and A.Money like '''+'%'+trim(Money.Text)+'%'+'''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
    Label12.Caption:='0';
  finally
    ADOQueryMain.EnableControls;
    //ToolButton2.Click;
  end;
end;

procedure TfrmSKCR_YWCX.InitGridHZ();
begin
end;

procedure TfrmSKCR_YWCX.FormDestroy(Sender: TObject);
begin
  frmSKCR_YWCX:=nil;
end;

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

procedure TfrmSKCR_YWCX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('收款登记66',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 TfrmSKCR_YWCX.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=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 TfrmSKCR_YWCX.FormShow(Sender: TObject);
begin
  fflag:=0;
  ReadCxGrid('收款登记66',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=SGetServerDate(ADOQueryTemp)-7;
  if Trim(DParameters1)='无订单号' then
  begin
    //VOrderNo.Hidden:=True;
    //VOrderNo.Visible:=False;
    label4.Visible:=False;
    //OrderNo.Visible:=False;
  end;
  lstPat := TStringList.Create;
  SetStatus();
  InitGrid();
end;

procedure TfrmSKCR_YWCX.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmSKCR_YWCX.ToolButton2Click(Sender: TObject);
begin
  label12.Caption:='0';
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

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

procedure TfrmSKCR_YWCX.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 TfrmSKCR_YWCX.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'FK','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:='应收收';
     //   FieldByName('YFDefFlag1').Value:=fFlag;
        Post;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_CR where 1=2');
      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('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('YFName').Value:=Trim(CDS_HZ.fieldbyname('YFName').AsString);
      FieldByName('CRTime').Value:=formatdateTIme('yyyy-MM-dd',CDS_HZ.fieldbyname('CRTime').AsDateTime);
      FieldByName('YFDefFlag1').Value:=fFlag;
      FieldByName('status').Value:=0;
      fieldbyname('BZType').Value:=trim(CDS_HZ.fieldbyname('BZType').AsString);
      fieldbyname('SZYFID').Value:=Trim(CDS_HZ.fieldbyname('SZYFID').AsString);
      FieldByName('ComTaitou').Value:=Trim(CDS_HZ.fieldbyname('ComTaitou').AsString);
      FieldByName('Note').Value:=Trim(CDS_HZ.fieldbyname('Note').AsString);
      FieldByName('Money').Value:=CDS_HZ.fieldbyname('Money').AsFloat;
      FieldByName('HuiLv').Value:=1;
      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 TfrmSKCR_YWCX.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 TfrmSKCR_YWCX.cxGridDBColumn5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FHuiLv,FBBMoney,FKCBB,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  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';

  {FBBMoney:=Trim(CDS_HZ.fieldbyname('BBMoney').AsString);
  if Trim(FBBMoney)='' then FBBMoney:='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='+FMoney);
    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 TfrmSKCR_YWCX.cxGridDBColumn1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  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 TfrmSKCR_YWCX.ToolButton3Click(Sender: TObject);
begin
  try
    frmYSCRPB_ZHYH:=TfrmYSCRPB_ZHYH.Create(Application);
    with frmYSCRPB_ZHYH do
    begin
      canshu3:='收';
      ToolButton3.Visible:=false;
      TBDel.Visible:=false;
      if ShowModal=1 then
      begin
        with CDS_HZ do
        begin
          first;
          while not eof do
          begin
            if fieldbyname('Ssel').AsBoolean=true then
            begin
              with Self.CDS_HZ do
              begin
          Append;
          FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
          FieldByName('ComTaitou').Value:=Trim(CDS_HZ.fieldbyname('BankName').AsString);
          FieldByName('YFName').Value:=Trim(CDS_HZ.fieldbyname('YFName').AsString);
          FieldByName('Money').Value:=CDS_HZ.fieldbyname('SRMoney').AsFloat;
          FieldByName('CRTime').Value:=CDS_HZ.fieldbyname('CRTime').AsDateTime;
          fieldbyname('SZYFID').Value:=Trim(CDS_HZ.fieldbyname('YFID').AsString);
          fieldbyname('note').Value:=Trim(CDS_HZ.fieldbyname('note').AsString);
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:='￥';
          fieldbyname('filler').Value:=trim(DName);
          Post;
              end;
              Self.SaveData();
            end;
            next;
          end;
        end;
      end;
    end;
  finally
    frmYSCRPB_ZHYH.Free;
  end;
end;

procedure TfrmSKCR_YWCX.cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFName';
      flagname:='项目及摘要';
      MainType:='收款';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('YFCode').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('YFName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        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 TfrmSKCR_YWCX.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 TfrmSKCR_YWCX.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 TfrmSKCR_YWCX.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'付款列表');
end;

procedure TfrmSKCR_YWCX.v2Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FMoney,FHuiLv,FBBMoney,FKCBB,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  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;

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

  FBBMoney:=FloatToStr(StrToFloat(FMoney)*StrToFloat(FHuiLv));
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(FMoney);
    FieldByName('BBMoney').Value:=StrToFloat(FBBMoney);
    fieldbyname('SXMoney').Value:=fieldbyname('KHDKMoney').AsFloat-fieldbyname('Money').AsFloat;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    sql.Add(',BBMoney='+FBBMoney);
    sql.add(',SXMoney='''+trim(CDS_HZ.fieldbyname('SXMoney').AsString)+'''');
    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 TfrmSKCR_YWCX.v2Column1PropertiesButtonClick(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('MainId').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('OrderNo').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate YF_Money_CR ');
            sql.Add(' Set MainId='''+Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString)+'''');
            sql.Add(' ,OrderNo='''+Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString)+'''');
            SQL.Add(',Editer='''+Trim(DName)+'''');
            SQL.Add(',Edittime=getdate() ');
            SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
            ExecSQL;
          end;
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;}
end;

procedure TfrmSKCR_YWCX.VBBPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FKCBB:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  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;
  if Trim(mvalue)='' then mvalue:='0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('BBMoney').Value:=StrToFloat(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' set BBMoney='+mvalue);
    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+'-'+mvalue);
    sql.Add(' where CRID='''+Trim(CDS_HZ.fieldbyname('CRID').AsString)+'''');
    ExecSQL;
  end;
   tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmSKCR_YWCX.OrderNoChange(Sender: TObject);
begin
  if Length(Trim(OrderNo.Text))<3 then Exit;
  ToolButton2.Click;
end;

procedure TfrmSKCR_YWCX.v2Column1PropertiesEditValueChanged(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 YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
   tv2.Controller.EditingController.ShowEdit();
end;
procedure TfrmSKCR_YWCX.v2DerateMoneyPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  if Trim(mvalue)='' then mvalue:='0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('DerateMoney').Value:=StrToFloatdef(mvalue,0);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' set DerateMoney='+mvalue);
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
   tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmSKCR_YWCX.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=true then
        begin
          if fieldbyname('GSType').AsString='' then
          begin
            application.MessageBox('公私类型不能为空','提示');
            exit;
          end;
          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;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmSKCR_YWCX.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)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

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

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

procedure TfrmSKCR_YWCX.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 TfrmSKCR_YWCX.v2SselPropertiesChange(Sender: TObject);
begin
  toolbar1.SetFocus;
  HJ(CDS_HZ,Label12,CDS_HZ.fieldbyname('Money').AsCurrency);
end;

procedure TfrmSKCR_YWCX.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 TfrmSKCR_YWCX.ReadINIFile10();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

procedure TfrmSKCR_YWCX.TSDFJClick(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;
  frmFjList10:=TfrmFjList10.create(self);
  with frmFjList10 do
  begin
    fkeyNO:=trim(CDS_HZ.fieldbyname('YFID').AsString);
    fType:='SD';
    if showmodal=1 then
    begin
      initgrid();
    end;
    free;
  end;
end;

procedure TfrmSKCR_YWCX.Tv2CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_HZ.IsEmpty then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select M.*,Y.* from JYOrderYSK Y ');
    sql.add('inner join JYOrderCon_Main M on M.Mainid=Y.Mainid ');
    sql.add('where Y.YFID='''+Trim(CDS_HZ.fieldbyname('YFID').asstring)+'''');
    open;
  end;
  SCreateCDS20(ADOQueryCmd,Order_RL);
  SInitCDSData20(ADOQueryCmd,Order_RL);
end;

procedure TfrmSKCR_YWCX.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,True);
end;

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

procedure TfrmSKCR_YWCX.v2FKTypePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FKType1';
      flagname:='收款类型';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('FKType').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set FKType='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
          SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSKCR_YWCX.CheckBox1Click(Sender: TObject);
begin
  Label12.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('Money').AsCurrency);
      end;
      post;
      next;
    end;
    First;
    EnableControls;
  end;  
end;

procedure TfrmSKCR_YWCX.MoneyChange(Sender: TObject);
begin
  initgrid();
end;

procedure TfrmSKCR_YWCX.v2FKTaiTouPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FKTaiTou';
      flagname:='收款抬头';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('FKTaiTou').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set FKTaiTou='''+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;

end.
