unit U_YFCR_SH;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxPC, cxCurrencyEdit, Menus, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmYFCR_SH = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    v2Column1: TcxGridDBColumn;
    VBZ: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    VBB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    VOrderNo: TcxGridDBColumn;
    v2PayMent: TcxGridDBColumn;
    v2Note: TcxGridDBColumn;
    v2C_CodeName: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2ssel: TcxGridDBColumn;
    P_CodeName: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    P_spec: TEdit;
    CheckBox1: TCheckBox;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    ToolButton4: TToolButton;
    v2OrderNO: TcxGridDBColumn;
    v2ComTaiTou: TcxGridDBColumn;
    Label8: TLabel;
    OrderNO: TEdit;
    Label9: TLabel;
    ComTaiTou: TEdit;
    YFName: TEdit;
    NOChk: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ChkNote: TEdit;
    Label17: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label16: TLabel;
    cxTabControl2: TcxTabControl;
    v2GCPrice: TcxGridDBColumn;
    v2GCMoney: TcxGridDBColumn;
    v2SHStatus: TcxGridDBColumn;
    Label10: TLabel;
    note: TEdit;
    v2ConNoHZ: TcxGridDBColumn;
    Label11: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v2PSPropertiesEditValueChanged(Sender: TObject);
    procedure v2BatchNoPropertiesEditValueChanged(Sender: TObject);
    procedure v2PayMentPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2T_qtyPropertiesEditValueChanged(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure v2T_PricePropertiesEditValueChanged(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v2OrderNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure NOChkClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure v2sselPropertiesChange(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
    procedure Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
  private
    { Private declarations }

    procedure InitGridHZ();
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmYFCR_SH: TfrmYFCR_SH;

implementation

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

{$R *.dfm}

procedure TfrmYFCR_SH.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' select SHStatus=(case when status=''0'' then ''未提交'' when status=''1'' then ''未审核'' else ''已审核'' end)');
      sql.add(' ,KPStatusH=(case when isnull(KPstatus,'''')='''' then ''未开票'' else ''开票完成'' end),A.*');
      sql.Add(',ConNoHZ=[dbo].[F_Get_Order_SubStr]((select B.Mainid from JYOrder_Main B where B.OrderNo=A.OrderNo),''ConNoHZ'')');
      sql.Add(' from YF_Money_CR A');
      sql.Add('where  A.CRType=''应付款登记''  ');
      //sql.add(' and A.KPStatus=''开票完成''');
      sql.add(' and A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(' and A.CRTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      //sql.add(' and A.GCPrice<>0');
      if cxTabControl1.TabIndex = 0 then
        sql.Add(' and A.status=''1'' ');
      if cxTabControl1.TabIndex = 1 then
      begin
        sql.Add(' and A.Status=''2'' ');
      end;
      if cxTabControl1.TabIndex = 2 then
      begin
        sql.Add(' and (A.Status=''1'' or A.Status=''2'') ');
      end;
      if cxtabControl2.TabIndex = 0 then
        sql.Add(' and isnull(A.YFType,'''')=''自动生成'' ');
      if cxtabControl2.TabIndex = 1 then
        sql.Add(' and isnull(A.YFType,'''')<>''自动生成'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
    label14.Caption := '0';
    label16.Caption := '0';
  finally
    ADOQueryMain.EnableControls;
    //ToolButton2.Click;
  end;
end;

procedure TfrmYFCR_SH.InitGridHZ();
begin
end;

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

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

procedure TfrmYFCR_SH.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 TfrmYFCR_SH.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 TfrmYFCR_SH.FormShow(Sender: TObject);
begin
  ReadCxGrid('应付款登记', Tv2, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := SGetServerDate(ADOQueryTemp) - 7;
  InitGrid();
end;

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

procedure TfrmYFCR_SH.ToolButton2Click(Sender: TObject);
var
  sql: string;
begin
  Label14.Caption := '0';
  Label16.Caption := '0';
  if ADOQueryMain.Active then
  begin
    sql := SGetFilters(Panel1, 1, 2);
    SDofilter(ADOQueryMain, sql);
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

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

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

function TfrmYFCR_SH.SaveData(): Boolean;
var
  maxId, CRID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd, maxId, 'YF', 'YF_Money_CR', 4, 1) = False then
    begin
      Result := False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！', '提示', 0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(CDS_HZ.fieldbyname('FactoryName').AsString) + ''' ');
      Open;
    end;
    if ADOQueryCmd.IsEmpty = False then
    begin
      CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
    end
    else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YF_Money_CRID ');
        Open;
      end;
      CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_KC where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('CRID').Value := StrToInt(CRID);
        FieldByName('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
        FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
        FieldByName('ZdyStr1').Value := '应付付';
        Post;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value := Trim(maxId);
      FieldByName('CRID').Value := StrToInt(CRID);
      //SSetSaveDataCDSNew(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',3);
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('CRType').Value := '应付款登记';
      FieldByName('CRFlag').Value := '应付付';
      FieldByName('QtyFlag').Value := 1;
      FieldByName('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('CRTime').Value := formatdateTIme('yyyy-MM-dd', CDS_HZ.fieldbyname('CRTime').AsDateTime);
      FieldByName('status').Value := '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 TfrmYFCR_SH.cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YCL_SG_HZ ');
    if Trim(mvalue) <> '' then
    begin
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
    end
    else
    begin
      sql.Add(' Set ' + FFieldName + '=NULL');
    end;
    sql.Add(',Editer=''' + Trim(DName) + '''');
    sql.Add(',Edittime=getdate() ');
    sql.Add(' where SubId=''' + Trim(CDS_HZ.fieldbyname('SubId').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmYFCR_SH.cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FQty, FPrice, FMoney, FHuiLv, FBBMoney, FKCBB, FFieldName: string;
begin
 { if Trim(CDS_HZ.FieldByName('YFType').AsString)='自动生成' then
  begin
    Application.MessageBox('自动生成的数据不能修改!','提示',0);
    Exit;
  end;}
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    if Trim(DParameters2) = '人民币' then
    begin
      FieldByName('HuiLv').Value := 1;
    end;
    Post;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(BBMoney,0) BBMoney from YF_Money_CR where YFID=''' + cds_hz.fieldbyname('YFID').AsString + '''');
    Open;
  end;
  FKCBB := ADOQueryTemp.fieldbyname('BBMoney').AsString;

  FQty := Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty) = '' then
    FQty := '0';

  FPrice := Trim(CDS_HZ.fieldbyname('Price').AsString);
  if Trim(FPrice) = '' then
    FPrice := '0';
  FQty := Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty) = '' then
    FQty := '0';

  FMoney := Trim(CDS_HZ.fieldbyname('Money').AsString);
  if Trim(FMoney) = '' then
    FMoney := '0';

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

  {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 TfrmYFCR_SH.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 TfrmYFCR_SH.ToolButton3Click(Sender: TObject);
begin

  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Append;
          FieldByName('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
          FieldByName('CRTime').Value := SGetServerDate(ADOQueryTemp);
          FieldByName('BZType').Value := 'RMB';
          FieldByName('HuiLv').Value := 1;
          Post;
        end;
        Self.SaveData();
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  tv2.Controller.EditingController.ShowEdit();
{ try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FactoryNo1Name';
      flagname:='供方';
      MainType:='坯布厂';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Append;
          FieldByName('FactoryNo').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('FactoryName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
          Post;
        end;
        Self.SaveData();
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  }
end;

procedure TfrmYFCR_SH.cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if Trim(CDS_HZ.FieldByName('YFType').AsString) = '自动生成' then
  begin
    Application.MessageBox('自动生成的数据不能修改!', '提示', 0);
    Exit;
  end;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YFName';
      flagname := '项目及摘要';
      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 TfrmYFCR_SH.cxGridDBColumn6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrderUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('QtyUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set QtyUnit=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.Add(',Editer=''' + Trim(DName) + '''');
          sql.Add(',Edittime=getdate() ');
          sql.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYFCR_SH.v2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'BZType';
      flagname := '币种';
      if ShowModal = 1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('BZType').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set BZType=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.Add(',Editer=''' + Trim(DName) + '''');
          sql.Add(',Edittime=getdate() ');
          sql.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

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

procedure TfrmYFCR_SH.v2PSPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set ' + FFieldName + '=' + Trim(mvalue));
    sql.Add(',Editer=''' + Trim(DName) + '''');
    sql.Add(',Edittime=getdate() ');
    sql.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmYFCR_SH.v2BatchNoPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
    sql.Add(',Editer=''' + Trim(DName) + '''');
    sql.Add(',Edittime=getdate() ');
    sql.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmYFCR_SH.v2PayMentPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PayMent';
      flagname := '支付方式';
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('PayMent').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set PayMent=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.Add(',Editer=''' + Trim(DName) + '''');
          sql.Add(',Edittime=getdate() ');
          sql.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

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

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

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

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

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

    ExecSQL;
  end;
end;

procedure TfrmYFCR_SH.CheckBox1Click(Sender: TObject);
begin
  Label14.Caption := '0';
  Label16.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
        Label14.Caption := floattostr(strtofloat(Label14.Caption) + fieldbyname('Qty').AsCurrency);
        Label16.Caption := floattostr(strtofloat(Label16.Caption) + fieldbyname('Money').AsCurrency);
      end;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

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

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

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

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

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

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

    ExecSQL;
  end;
end;

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

  Tchk.Enabled := false;
  TNochk.Enabled := false;
  TBDel.Enabled := false;
//  Ttj.Enabled:=false;
//  TnoTj.Enabled:=false;
  for i := 0 to tv2.ColumnCount - 1 do
  begin
    if uppercase(tv2.Columns[i].DataBinding.FieldName) <> 'SSEL' then
      tv2.Columns[i].Options.Editing := false;
  end;

  if fmanage = '高权限' then
  begin
    if cxTabControl1.TabIndex = 0 then
    begin
      Tchk.Enabled := true;
      //TBDel.Enabled:=true;
      for i := 0 to tv2.ColumnCount - 1 do
      begin
        tv2.Columns[i].Options.Editing := true;
      end;
    end;
    if cxTabControl1.TabIndex = 1 then
    begin
      TNochk.Enabled := true;
    end;

  end
  else
  begin
    if cxTabControl1.TabIndex = 0 then
    begin
      //Tchk.Enabled:=true;
      TBDel.Enabled := true;
      for i := 0 to tv2.ColumnCount - 1 do
      begin
        tv2.Columns[i].Options.Editing := true;
      end;
    end;
    if cxTabControl1.TabIndex = 1 then
    begin

    end;
  end;
  InitGrid();
end;

procedure TfrmYFCR_SH.TchkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
//  with CDS_HZ do
//  begin
//    first;
//    while not eof do
//    begin
//      if fieldbyname('Ssel').AsBoolean=true then
//      begin
//        if fieldbyname('Price').AsCurrency<>fieldbyname('GCPrice').AsCurrency then
//        begin
//          if application.MessageBox('我司单价与工厂单价不同,不能审核','提示',1)=2 then exit;
//        end;
//      end;
//      next;
//    end;
//  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean = true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET status=''2'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('操作成功！', '提示信息', 0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYFCR_SH.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=''1'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

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

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

procedure TfrmYFCR_SH.v2OrderNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      FFInt := 1;
      //frmProductOrderList.v1Column4.Visible:=True;
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
      //    FieldByName('MainId').Value:=Trim(frmProductOrderNewList.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('OrderNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('P_CodeName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPrtCodeName').AsString);
          FieldByName('P_Spec').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('Mprtspec').AsString);
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate YF_Money_CR ');
            sql.Add(' Set OrderNO=''' + Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNO').AsString) + '''');
            sql.Add(' ,P_CodeName=''' + Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPrtCodeName').AsString) + '''');
            sql.Add(' ,P_Spec=''' + Trim(frmProductOrderListSel.Order_Main.fieldbyname('Mprtspec').AsString) + '''');
            sql.Add(',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 TfrmYFCR_SH.NOChkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if chknote.Text = '' then
  begin
    if application.MessageBox('确定审核备注为空吗', '提示', 1) = 2 then
      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'',ChkStatus=''不通过'',chknote=''' + trim(chknote.Text) + ''' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

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

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

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

procedure TfrmYFCR_SH.v2sselPropertiesChange(Sender: TObject);
begin
  toolbar1.SetFocus;
  HJ(CDS_HZ, Label14, CDS_HZ.fieldbyname('Qty').AsCurrency);
  HJ(CDS_HZ, Label16, CDS_HZ.fieldbyname('Money').AsCurrency);
end;

procedure TfrmYFCR_SH.cxTabControl2Change(Sender: TObject);
begin
  initgrid();
end;

procedure TfrmYFCR_SH.Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id, id10: Integer;
begin
  if cxtabcontrol1.TabIndex = 0 then
  begin
    id := Tv2.GetColumnByFieldName('GCPrice').Index;
    if (ARecord.Values[id] = '0') or (ARecord.Values[id] = NULL) then
    begin
      AStyle := DataLink_YSYF1.SkyBlue;
    end;
  end;
end;

end.

