unit U_SKCR_YF;

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, RM_e_Xls;

type
  TfrmSKCR_YF = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    ConPerson1: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RMDB_PB: TRMDBDataSet;
    Label3: TLabel;
    CustomerNoName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    V1OrdDate: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2CustomerNoName: TcxGridDBColumn;
    v2ConNo: TcxGridDBColumn;
    TCK: TToolButton;
    Label4: TLabel;
    ConNo: TEdit;
    v2ConPerson1: TcxGridDBColumn;
    v2PBZMoney: TcxGridDBColumn;
    v2JGZMoney: TcxGridDBColumn;
    v2YSRMBMoney: TcxGridDBColumn;
    v2ZCRMBMoney: TcxGridDBColumn;
    v2MLMoney: TcxGridDBColumn;
    TPrint: TToolButton;
    v2YSUSDMoney: TcxGridDBColumn;
    v2SKUSDMoney: TcxGridDBColumn;
    v2SKRMBMoney: TcxGridDBColumn;
    v2ZCUSDMoney: TcxGridDBColumn;
    Order_PB: TClientDataSet;
    Order_FYZC: TClientDataSet;
    Order_RF: TClientDataSet;
    Order_XS: TClientDataSet;
    Order_SK: TClientDataSet;
    RMDB_FYZC: TRMDBDataSet;
    RMD_RF: TRMDBDataSet;
    RMDB_XS: TRMDBDataSet;
    RMDB_SK: TRMDBDataSet;
    ADOQuery1: TADOQuery;
    RMXLSExport1: TRMXLSExport;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    ADOQuery4: TADOQuery;
    ADOQuery5: TADOQuery;
    RM1: TRMGridReport;
    v2YGMoney: TcxGridDBColumn;
    v2HTUSDMoney: TcxGridDBColumn;
    v2HTRMBMoney: TcxGridDBColumn;
    v2MPRTType: TcxGridDBColumn;
    Label5: TLabel;
    MPRTType: TComboBox;
    THTCX: TToolButton;
    cxTabControl1: TcxTabControl;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn1PropertiesEditValueChanged(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 ConNoChange(Sender: TObject);
    procedure v2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure v2DerateMoneyPropertiesEditValueChanged(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v2ComTaitouPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TCKClick(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure TPrintClick(Sender: TObject);
    procedure THTCXClick(Sender: TObject);
  private
    { Private declarations }

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

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

  end;

var
  frmSKCR_YF: TfrmSKCR_YF;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ProductOrderList, U_ProductOrderNewList,
  U_ZdyAttachment,U_ProductOrderListSel, U_JYOrderYS, U_JYOrderCon_CX;

{$R *.dfm}

Procedure TfrmSKCR_YF.SetStatus();
var
  i:integer;
begin

end;

procedure TfrmSKCR_YF.InitGrid();
var ZDY:string;
begin
  ZDY:=' and OrdDate>='''+Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime))+''''
       +' and OrdDate<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+''' ';
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    if cxTabControl1.TabIndex=0 then
    begin
     sql.Add('exec P_YFFK_TJHZCX :ZDY');
    end;
    if cxTabControl1.TabIndex=1 then
    begin
     sql.Add('exec P_YFFK_HZCX :ZDY');
    end;
    Parameters.ParamByName('ZDY').Value:=ZDY;
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDS_HZ);
  SInitCDSData20(ADOQueryMain,CDS_HZ);
end;

procedure TfrmSKCR_YF.InitGridHZ();
begin
end;

procedure TfrmSKCR_YF.FormDestroy(Sender: TObject);
begin
  frmSKCR_YF:=nil;
end;

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

procedure TfrmSKCR_YF.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('订单毛利',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_YF.FormShow(Sender: TObject);
begin
  ReadCxGrid('订单毛利',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=SGetServerDate(ADOQueryTemp)-15;
  //SetStatus();
  //InitGrid();
end;

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

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

procedure TfrmSKCR_YF.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_YF.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<>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('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
      FieldByName('YFDefFlag1').Value:=fFlag;
      FieldByName('status').Value:=0;
      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_YF.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_YF.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_YF.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_YF.cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFName';
      flagname:='项目及摘要';
      if Trim(DParameters3)='项目分开' then
        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_YF.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_YF.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_YF.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'订单利润表');
end;

procedure TfrmSKCR_YF.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:=Trim(CDS_HZ.fieldbyname('BBMoney').AsString);
  if Trim(FBBMoney)='' then FBBMoney:='0'; }

  FBBMoney:=FloatToStr(StrToFloat(FMoney)*StrToFloat(FHuiLv));
  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));
    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_YF.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_YF.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_YF.ConNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmSKCR_YF.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_YF.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_YF.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmSKCR_YF.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_YF.TCKClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  FrmJYOrderYS:=TFrmJYOrderYS.create(self);
  with FrmJYOrderYS do
  begin
    FConnO:=trim(CDS_HZ.fieldbyname('ConNO').AsString);
    FYWY:=trim(CDS_HZ.fieldbyname('ConPerson1').AsString);
    FKHName:=trim(CDS_HZ.fieldbyname('CustomerNoName').AsString);
    FMaoLMoney:=trim(CDS_HZ.fieldbyname('MLMoney').AsString);
    FYGMoney:=trim(CDS_HZ.fieldbyname('YGMoney').AsString);
    if cxTabControl1.TabIndex=0 then
    FormID:='0';
    if cxTabControl1.TabIndex=1 then
    FormID:='1';
    if showmodal=1 then
    begin
      initgrid();
    end;
    free;
  end;
end;

procedure TfrmSKCR_YF.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  FName:string;
begin
  FName:=tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if trim(FName)='ConNo' then
  begin
    THTCX.Click;
  end
  else
  begin
    TCK.Click;
  end;
end;

procedure TfrmSKCR_YF.FormCreate(Sender: TObject);
begin
  cxGrid2.Align:=alClient;
end;

procedure TfrmSKCR_YF.ConNoKeyPress(Sender: TObject; var Key: Char);
var ZDY:string;
begin
  if key=#13 then
  begin
    ZDY:=' and ConNO like '''+'%'+trim(ConNO.Text)+'%'+''' ';
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add('exec P_YFFK_HZCX :ZDY');
      Parameters.ParamByName('ZDY').Value:=ZDY;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

procedure TfrmSKCR_YF.TPrintClick(Sender: TObject);
var fPrintFile:string;
    FPBPS,FPBQty,FPBMoney:double;
    FRFPS,FRFQty,FRFMoney:double;
    FRFHXPS,FRFHXQty,FRFHXMoney:double;
    FPS,FQty,FRMBMoney,FUSDMoney:double;
    FSKRMBMoney,FSKUSDMoney:double;
begin
  if CDS_HZ.IsEmpty then exit;
  with Adoquery1 do
  begin
    close;
    sql.clear;
    sql.add('select AAA.PBChang,AAA.QtyUnit,AAA.ConNo,AAA.CRTime,AAA.Price,AAA.P_CodeName,TTPS=cast(sum(Qty/DJRKQty*PiQty) as decimal(18,0)) ');
    sql.add(',TTQty=cast(sum(Qty/DJRKQty*PBQty) as decimal(18,2)),PBMoney=cast(sum(Qty/DJRKQty*PBQty)*Price as decimal(18,2)) ');
    sql.add('from (select AA.PBChang,AA.QtyUnit,C.ConNo,AA.CRTime,AA.Price,AA.P_CodeName,C.Qty,AA.PiQty,AA.Qty PBQty,AA.DJRKQty ');
    sql.add('from (select B.PBChang,B.QtyUnit,B.CRTime,B.Price,isnull(M.Mainid,W.Mainid) Mainid,B.P_CodeName,isnull(Q.PiQty,B.PS) PiQty,isnull(Q.Qty,B.Qty) Qty ');
    sql.add(',DJRKQty=(select Sum(P.Qty) from CK_SXPB_CR B inner join CK_SXPB_CR P on P.FZSPID=B.SPID where B.ORDMainIdRK=isnull(M.MainId,W.Mainid) ');
    sql.add(' and P.CRFlag=''出库'' and isnull(B.CKName,'''')=''待检布'' and isnull(B.CRType,'''') in (''加工完成'',''采购入库'')) ');
    sql.add('from YF_Money_CR B inner join JYOrder_Main W on B.OrderNo=W.OrderNo ');
    sql.add('left join CK_SXPB_CR Q on B.YFTypeId=Q.FZSPID and isnull(Q.CKName,'''')=''坯布'' and Q.CRType=''加工出库'' ');
    sql.add('left join JYOrder_Main M on Q.OrdMainidCK=M.Mainid ');
    sql.add('where isnull(B.YFName,'''')=''坯布虚拟费'' ');
    if cxTabControl1.TabIndex=0 then
    sql.Add('and isnull(B.status,''0'')<>''0'' ');
    if cxTabControl1.TabIndex=1 then
    sql.Add('and isnull(B.status,''0'')=''2'' ');
    sql.add('union all ');
    sql.add('select D.FactoryName PBChang,D.QtyUnit,D.CRTime,D.GCPrice Price,isnull(M.Mainid,W.Mainid) Mainid,D.P_CodeName,isnull(F.PiQty,D.ps) piQty,isnull(F.Qty,D.Qty) Qty ');
    sql.add(',DJRKQty=(select Sum(P.Qty) from CK_SXPB_CR B inner join CK_SXPB_CR P on P.FZSPID=B.SPID where B.ORDMainIdRK=isnull(M.MainId,W.Mainid) ');
    sql.add(' and P.CRFlag=''出库'' and isnull(B.CKName,'''')=''待检布'' and isnull(B.CRType,'''') in (''加工完成'',''采购入库'')) ');
    sql.add('from YF_Money_CR D inner join JYOrder_Main W on D.OrderNo=W.OrderNo ');
    sql.add('left join CK_SXPB_CR F on D.YFTypeId=F.FZSPID and isnull(F.CRType,'''') in (''加工出库'',''检验出库'') ');
    sql.add('left join JYOrder_Main M on F.OrdMainidCK=M.Mainid ');
    sql.add('where isnull(D.YFName,'''')=''坯布费'' ');
    if cxTabControl1.TabIndex=0 then
    sql.Add('and isnull(D.status,''0'')<>''0'' ');
    if cxTabControl1.TabIndex=1 then
    sql.Add('and isnull(D.status,''0'')=''2'' ');
    sql.add(') AA inner join CK_SXPB_CR C on C.ORDMainidRK=AA.Mainid and isnull(C.CKName,'''')=''待检布'' and isnull(C.CRFlag,'''')=''出库'' ');
    sql.add('and exists(select Q.SPID from CK_SXPB_CR Q where Q.SPID=C.FZSPID and isnull(Q.CRType,'''') in (''加工完成'',''采购入库''))) AAA  ');
    sql.add('where AAA.ConNo='''+trim(CDS_HZ.fieldbyname('ConNO').AsString)+''' ');
    sql.add('group by AAA.PBChang,AAA.QtyUnit,AAA.ConNo,AAA.CRTime,AAA.Price,AAA.P_CodeName');
    open;
  end;
  SCreateCDS20(Adoquery1,Order_PB);
  SInitCDSData20(Adoquery1,Order_PB);
  FPBPS:=0;FPBQty:=0;FPBMoney:=0;
  with Order_PB do
  begin
    first;
    while not eof do
    begin
      FPBPS:=FPBPS+fieldbyname('TTPS').AsFloat;
      FPBQty:=FPBQty+fieldbyname('TTQty').AsFloat;
      FPBMoney:=FPBMoney+fieldbyname('PBMoney').AsFloat;
      next;
    end;
  end;
  with AdoQuery1 do
  begin
    close;
    sql.clear;
    sql.add('select AAA.*,SH=(case when PBQty=0 then 0 else cast((PBQty-JGQty)/PBQty as decimal(18,2)) end) ');
    sql.add('from (select D.ConNo,AA.CRTime,AA.YFName,AA.QtyUnit,AA.P_CodeName,AA.P,AA.PRTColor,JGPS=cast(sum(D.Qty/DJRKQty*AA.PS) as decimal(18,0)),JGQty=cast(sum(D.Qty/DJRKQty*AA.Qty) as decimal(18,2)),');
    sql.add('AA.GCPrice,AA.FactoryName,JGMoney=cast(sum(GCPrice*D.Qty/DJRKQty*AA.Qty) as decimal(18,2)),PBQty=cast(sum(D.Qty/DJRKQty*TPQty) as decimal(18,2)) ');
    sql.add('from (select convert(varchar(10),A.CRTime,120) CRTime,A.YFName,A.QtyUnit,A.OrderNo,A.P_CodeName,A.PS P,Sum(A.ps) PS,sum(A.Qty) Qty,A.GCPrice,A.FactoryName,(select P.PRTColor from JYOrder_Sub P where P.SubId=A.ZSDSubid) PRTColor,A.ZSDSubid Subid,');
    sql.add('TPQty=(select SUM(Qty) from CK_SXPB_CR F where F.ORDSubIdCK=A.ZSDSubid and isnull(F.CKName,'''')=''坯布'' and isnull(F.CRType,'''')=''加工出库''),');
    sql.add('DJRKQty=(select SUM(P.Qty) from CK_SXPB_CR F inner join CK_SXPB_CR P on P.FZSPID=F.SPID where F.ORDSubIdRK=A.ZSDSubid and P.CRFlag=''出库'' and F.FactoryName=A.FactoryName ');
    sql.add('and isnull(F.CKName,'''')=''待检布'' and isnull(F.CRType,'''') in (''加工完成'',''回修完成'')) ');
    sql.add('from YF_Money_CR A ');
    sql.add('where isnull(A.YFName,'''') in (''加工费'',''回修完成费'') ');
    if cxTabControl1.TabIndex=0 then
    sql.Add('and isnull(A.status,''0'')<>''0'' ');
    if cxTabControl1.TabIndex=1 then
    sql.Add('and isnull(A.status,''0'')=''2'' ');
    Sql.add('and not exists (select N.SPID from CK_SXPB_CR N where N.SPID=A.Mainid and isnull(N.CKName,'''') in (''坯布'',''纱线'',''外加工'')) ');
    sql.add('group by convert(varchar(10),A.CRTime,120),A.YFName,A.QtyUnit,A.OrderNo,A.P_CodeName,A.PS,A.GCPrice,A.FactoryName,A.ZSDSubid ');
    sql.add('union all ');
    sql.add('select convert(varchar(10),CRTime,120) CRTime,YFName,QtyUnit,OrderNo,P_CodeName,cast(0 as decimal(18,2)) P,PS=cast(0 as decimal(18,0)),Qty=cast(0 as decimal(18,2)) ');
    sql.add(',cast(0 as decimal(18,2)) GCPrice,cast('''' as varchar(50)) FactoryName,cast('''' as varchar(50)) PRTColor,cast('''' as varchar(50)) Subid,');
    sql.add('TPQty=cast(0 as decimal(18,2)),DJRKQty=cast(0 as decimal(18,2)) ');
    sql.add('from YF_Money_CR where 1=2 ');
    sql.Add(') AA ');
    sql.add('inner join CK_SXPB_CR D on D.ordSubidRK=AA.Subid and isnull(D.CKName,'''')=''待检布'' and isnull(D.CRFlag,'''')=''出库'' and D.FactoryName=AA.FactoryName ');
    sql.add('inner join CK_SXPB_CR Q on D.FZSPID=Q.SPID and isnull(Q.CRFlag,'''')=''入库'' and isnull(Q.CRType,'''') in (''加工完成'',''回修完成'') ');
    sql.add('where D.ConNo='''+trim(CDS_HZ.fieldbyname('ConNO').AsString)+''' ');
    sql.add('group by D.ConNo,AA.CRTime,AA.YFName,AA.QtyUnit,AA.P_CodeName,AA.P,AA.PRTColor,AA.GCPrice,AA.FactoryName) AAA');
    sql.Add(' order by AAA.CRTime,AAA.GCPrice');
    open;
  end;
  SCreateCDS20(Adoquery1,Order_RF);
  SInitCDSData20(Adoquery1,Order_RF);
  FRFPS:=0;FRFQty:=0;FRFMoney:=0;
  with Order_RF do
  begin
    first;
    while not eof do
    begin
      FRFPS:=FRFPS+fieldbyname('JGPS').AsFloat;
      FRFQty:=FRFQty+fieldbyname('JGQty').AsFloat;
      FRFMoney:=FRFMoney+fieldbyname('JGMoney').AsFloat;
      if FieldByName('YFName').Value='回修完成费' then
      begin
        FRFHXPS:=FRFHXPS+fieldbyname('JGPS').AsFloat;
        FRFHXQty:=FRFHXQty+fieldbyname('JGQty').AsFloat;
        FRFHXMoney:=FRFHXMoney+fieldbyname('JGMoney').AsFloat;
      end;
      next;
    end;
  end;
  with AdoQuery1 do
  begin
    close;
    sql.clear;
    sql.add('select A.ConNo,A.CRTime,A.QtyUnit,isnull(A.P_CodeName,E.PrtCodeName) P_CodeName,isnull(E.PRTColor,E.SOrdDefStr4) PRTColor,A.BZType,A.Price,sum(A.ps) PS,sum(A.Qty) Qty');
    sql.add(',RMBMoney=(case when (A.BZType<>''$'') and (A.BZType<>''USD'') then sum(Money) else 0 end)');
    sql.add(',USDMoney=(case when (A.BZType<>''￥'') and (A.BZType<>''RMB'') then sum(Money) else 0 end)');
    sql.add('from YF_Money_CR A ');
    sql.add('left join JYOrderCon_Sub E on E.Subid=A.Conid ');
    sql.add('where A.ConNo='''+trim(CDS_HZ.fieldbyname('ConNO').AsString)+''' and A.CRType=''应收款登记'' ');
    sql.add(' and (((A.YFName=''成品销售金额'' or Isnull(A.FeeType,'''')=''其他费用'') ');
    if cxTabControl1.TabIndex=0 then
    sql.Add('and isnull(A.status,''0'')<>''0'' ');
    if cxTabControl1.TabIndex=1 then
    sql.Add('and isnull(A.status,''0'')=''2'' ');
    sql.add(' ) or (A.YFName<>''成品销售金额'' and A.YFName=''坯布销售金额'' ');
    if cxTabControl1.TabIndex=0 then
    sql.Add('and isnull(A.status,''0'')<>''0'' ');
    if cxTabControl1.TabIndex=1 then
    sql.Add('and isnull(A.status,''0'')=''2'' ');
    sql.Add(')) ');
    sql.add('group by A.ConNo,A.CRTime,A.QtyUnit,A.P_CodeName,E.PrtCodeName,E.PRTColor,E.SOrdDefStr4,A.BZType,A.Price');
    open;
  end;
  SCreateCDS20(Adoquery1,Order_XS);
  SInitCDSData20(Adoquery1,Order_XS);
  FPS:=0;FQty:=0;FRMBMoney:=0;FUSDMoney:=0;
  with Order_XS do
  begin
    first;
    while not eof do
    begin
      FPS:=FPS+fieldbyname('PS').asfloat;
      FQty:=FQty+fieldbyname('Qty').asfloat;
      FRMBMoney:=FRMBMoney+fieldbyname('RMBMoney').asfloat;
      FUSDMoney:=FUSDMoney+fieldbyname('USDMoney').asfloat;
      next;
    end;
  end;
  with AdoQuery1 do
  begin
    close;
    sql.clear;
    sql.add('select B.ConNo,A.CRTime,A.BZType,A.note,A.ComTaitou');
    sql.add(',Huilv=isnull((select top 1 C.HuiLV from JYOrder_HuiLV C where A.CRTime>=C.BegDate and A.CRTime<isnull(C.EndDate,A.CRTime+1) and A.BZType<>''￥'' and A.BZType<>''RMB'' and C.GSType=A.GSType),1)');
    sql.add(',RMBMoney=cast((sum(E.RLMoney)*isnull((select TOP 1 C.HuiLV From JYOrder_HuiLV C where A.CRTime>=C.BegDate and A.CRTime<isnull(C.EndDate,A.CRTime+1) and A.BZType<>''￥'' and A.BZType<>''RMB'' and C.GSType=A.GSType),1)) as decimal(18,2))');
    sql.add(',USDMoney=(case when (A.BZType<>''￥'') and (A.BZType<>''RMB'') then sum(E.RLMoney) else 0 end)');
    sql.add('from YF_Money_CR A ');
    sql.add('inner join JYOrderYSK E on E.YFID=A.YFID ');
    sql.add('inner join JYOrderCon_Main B on B.Mainid=E.Mainid ');
    sql.add('where B.ConNo='''+trim(CDS_HZ.fieldbyname('ConNO').AsString)+''' and A.CRType=''收款登记'' ');
    sql.add('group by B.ConNo,A.CRTime,A.BZType,A.note,A.ComTaitou,A.GSType');
    open;
  end;
  SCreateCDS20(Adoquery1,Order_SK);
  SInitCDSData20(Adoquery1,Order_SK);
  FSKRMBMoney:=0;FSKUSDMoney:=0;
  with Order_SK do
  begin
    first;
    while not eof do
    begin
      FSKRMBMoney:=FSKRMBMoney+fieldbyname('RMBMoney').asfloat;
      FSKUSDMoney:=FSKUSDMoney+fieldbyname('USDMoney').asfloat;
      next;
    end;
  end;
  with AdoQuery1 do
  begin
    close;
    sql.clear;
    sql.add('select A.ConNo,A.CRTime,A.YFName,A.BZType,A.Note ');
    sql.add(',Huilv=isnull((select top 1 C.HuiLV from JYOrder_HuiLV C where A.CRTime>=C.BegDate and A.CRTime<isnull(C.EndDate,A.CRTime+1) and A.BZType<>''￥'' and A.BZType<>''RMB'' and C.GSType=A.GSType),1)');
    sql.add(',RMBMoney=cast((sum(A.Money)*isnull((select TOP 1 C.HuiLV From JYOrder_HuiLV C where A.CRTime>=C.BegDate and A.CRTime<isnull(C.EndDate,A.CRTime+1) and A.BZType<>''￥'' and A.BZType<>''RMB'' and C.GSType=A.GSType),1)) as decimal(18,2))');
    sql.add(',USDMoney=(case when (A.BZType<>''￥'') and (A.BZType<>''RMB'') then sum(A.Money) else 0 end) ');
    sql.add('from YF_Money_CR A ');
    sql.add('where A.ConNo='''+trim(CDS_HZ.fieldbyname('ConNO').AsString)+''' and A.CRType=''应付款登记'' and isnull(FeeType,'''')=''虚拟费用'' ');
    sql.add('group by A.ConNo,A.CRTime,A.YFName,A.BZType,A.GSType,A.Note');
    open;
  end;
  SCreateCDS20(Adoquery1,Order_FYZC);
  SInitCDSData20(Adoquery1,Order_FYZC);
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\订单利润表.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['FConNo']:=trim(CDS_HZ.fieldbyname('ConNO').asstring);
    RMVariables['FConPerson1']:=trim(CDS_HZ.fieldbyname('ConPerson1').asstring);
    RMVariables['FCustomerNoName']:=trim(CDS_HZ.fieldbyname('CustomerNoName').asstring);
    RMVariables['MLMoney']:=CDS_HZ.fieldbyname('MLMoney').AsFloat;
    RMVariables['YGMoney']:=CDS_HZ.fieldbyname('YGMoney').AsFloat;
    RMVariables['FPBPS']:=FPBPS;
    RMVariables['FPBQty']:=FPBQty;
    RMVariables['FPBMoney']:=FPBMoney;
    RMVariables['FRFPS']:=FRFPS;
    RMVariables['FRFQty']:=FRFQty;
    RMVariables['FRFMoney']:=FRFMoney;
    RMVariables['FPS']:=FPS;
    RMVariables['FQty']:=FQty;
    RMVariables['FRMBMoney']:=FRMBMoney;
    RMVariables['FUSDMoney']:=FUSDMoney;
    RMVariables['FSKRMBMoney']:=FSKRMBMoney;
    RMVariables['FSKUSDMoney']:=FSKUSDMoney;
    RMVariables['FRFHXPS']:=FRFHXPS;
    RMVariables['FRFHXQty']:=FRFHXQty;
    RMVariables['FRFHXMoney']:=FRFHXMoney;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\订单利润表.rmf'),'提示',0);
  end;
end;

procedure TfrmSKCR_YF.THTCXClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  frmJYorderConCX:=TfrmJYorderConCX.create(self);
  with frmJYorderConCX do
  begin
    formid:='3';
    FConNO:=trim(CDS_HZ.fieldbyname('ConNO').AsString);
    if showmodal=1 then
    begin
      
    end;
    free;
  end;
end;

end.
