unit U_YSFP_WXCX;

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

type
  TfrmYSFP_WXCX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    BankName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RMCD: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    KHName: TEdit;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TADD: TToolButton;
    Label4: TLabel;
    khConNo: TEdit;
    Label5: TLabel;
    BankAddress: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TUpdate: TToolButton;
    TPrint: TToolButton;
    Panel2: TPanel;
    ComboBox1: TComboBox;
    TFZ: TToolButton;
    TCK: TToolButton;
    Order_Print: TClientDataSet;
    CDS_Sub: TClientDataSet;
    DataSource1: TDataSource;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    v2FPNo: TcxGridDBColumn;
    v2khConNo: TcxGridDBColumn;
    v2StyleNo: TcxGridDBColumn;
    v2JQty: TcxGridDBColumn;
    v2MQty: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    v2FPPrice: TcxGridDBColumn;
    v2HYPrice: TcxGridDBColumn;
    VBZ: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v2P_CodeName: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2PRTColor: TcxGridDBColumn;
    v2PRTMF: TcxGridDBColumn;
    v2PRTKZ: TcxGridDBColumn;
    v2Note: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    V1HYMoney: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    TTJ: TToolButton;
    TCXTJ: TToolButton;
    V1filltime: TcxGridDBColumn;
    V1KHName: TcxGridDBColumn;
    V1Ssel: TcxGridDBColumn;
    TSsel: TToolButton;
    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 KHNameChange(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 v2Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v2PSPropertiesEditValueChanged(Sender: TObject);
    procedure v2BatchNoPropertiesEditValueChanged(Sender: TObject);
    procedure v2PayMentPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure khConNoChange(Sender: TObject);
    procedure VKPNOPropertiesEditValueChanged(Sender: TObject);
    procedure v2KPDMNoPropertiesEditValueChanged(Sender: TObject);
    procedure v2ZFTTPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxGridDBColumn3PropertiesEditValueChanged(Sender: TObject);
    procedure v2P_SpecPropertiesEditValueChanged(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure v2SselPropertiesChange(Sender: TObject);
    procedure TADDClick(Sender: TObject);
    procedure TUpdateClick(Sender: TObject);
    procedure TCKClick(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TFZClick(Sender: TObject);
    procedure TPrintClick(Sender: TObject);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TTJClick(Sender: TObject);
    procedure TCXTJClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TSselClick(Sender: TObject);
  private
    { Private declarations }

    procedure InitGridHZ();
    procedure InitGrid();
    function  SaveData():Boolean;
    procedure setstatus();
  public
    { Public declarations }
    RKFlag,FCYID:String;

  end;

var
  frmYSFP_WXCX: TfrmYSFP_WXCX;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ProductOrderList, U_ProductOrderNewList,U_YSFP_WXDJ,
  U_ZdyAttachment;

{$R *.dfm}
procedure TfrmYSFP_WXCX.setstatus();
begin
  
end;

procedure TfrmYSFP_WXCX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,B.*');
      sql.Add(' from JYOrderFPPI_Main A');
      sql.add(' inner join JYOrderFPPI_Sub B on A.FPID=B.FPID');
      sql.Add(' where A.filltime>='''+Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime))+''' ');
      sql.add('and A.filltime<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYSFP_WXCX.InitGridHZ();
begin
end;

procedure TfrmYSFP_WXCX.FormDestroy(Sender: TObject);
begin
  frmYSFP_WXCX:=nil;
end;

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

procedure TfrmYSFP_WXCX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应收款发票明细',Tv1,'财务管理');
  Close;
end;
procedure TfrmYSFP_WXCX.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(CDS_HZ.fieldbyname('FPID').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete JYOrderFPPI_Main where FPID='''+Trim(CDS_HZ.fieldbyname('FPID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmYSFP_WXCX.FormShow(Sender: TObject);
var
  fsj:string;
begin
  ReadCxGrid('应收款发票明细',Tv1,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-7;
  setstatus();
  InitGrid();
end;

procedure TfrmYSFP_WXCX.TBRafreshClick(Sender: TObject);
begin
  Toolbar1.SetFocus;
  InitGrid();
end;

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

procedure TfrmYSFP_WXCX.KHNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSFP_WXCX.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 TfrmYSFP_WXCX.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:=SGetServerDate(ADOQueryTemp);
      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 TfrmYSFP_WXCX.cxGridDBColumn8PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv1.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 TfrmYSFP_WXCX.cxGridDBColumn5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FHuiLv,FKPMoney,FKPNow,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv1.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(KPMoney,0) KPMoney from YF_Money_CR where YFID='''+cds_hz.fieldbyname('YFID').AsString+'''');
    Open;
  end;
  FKPMoney:=ADOQueryTemp.fieldbyname('KPMoney').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';
  FHuiLv:=Trim(CDS_HZ.fieldbyname('HuiLv').AsString);
  if Trim(FHuiLv)='' then FHuiLv:='1';
  if Trim(FFieldName)<>'KPMoney' then
  begin
    FKPNow:=FloatToStr(StrToFloat(FQty)*StrToFloat(FPrice)*StrToFloat(FHuiLv));
  end else
  begin
    FKPNow:=CDS_HZ.fieldbyname('KPMoney').AsString;
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('KPMoney').Value:=StrToFloat(FKPNow);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    if Trim(FFieldName)<>'KPMoney' then
    begin
      sql.Add(',KPMoney='+FKPNow);
    end;
    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 KCKPMoney=KCKPMoney-'+FKPMoney+'+'+FKPNow);
    sql.Add(' where CRID='''+Trim(CDS_HZ.fieldbyname('CRID').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYSFP_WXCX.cxGridDBColumn1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv1.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;
end;

procedure TfrmYSFP_WXCX.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 TfrmYSFP_WXCX.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 TfrmYSFP_WXCX.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 TfrmYSFP_WXCX.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'应付款列表');
end;

procedure TfrmYSFP_WXCX.v2Column7PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmProductOrderNewList:=TfrmProductOrderNewList.Create(Application);
    with frmProductOrderNewList do
    begin
      DParameters1:='高权限';
      //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(frmProductOrderNewList.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('ComTaiTou').Value:=Trim(frmProductOrderNewList.Order_Main.fieldbyname('OrdDefStr2').AsString);
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate YF_Money_CR ');
            sql.Add(' Set MainId='''+Trim(frmProductOrderNewList.Order_Main.fieldbyname('MainId').AsString)+'''');
            SQL.Add(',ComTaiTou='''+Trim(frmProductOrderNewList.Order_Main.fieldbyname('OrdDefStr2').AsString)+'''');
            //sql.Add(' ,SubId='''+Trim(frmProductOrderList.Order_Main.fieldbyname('SubId').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
    frmProductOrderNewList.Free;
  end;
end;

procedure TfrmYSFP_WXCX.v2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FBBMoney,FKCBB,FPrice,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv1.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(KPMoney,0) KPMoney from YF_Money_CR where YFID='''+cds_hz.fieldbyname('YFID').AsString+'''');
    Open;
  end;
  FKCBB:=ADOQueryTemp.fieldbyname('KPMoney').AsString;
  FBBMoney:=CDS_HZ.fieldbyname('KPMoney').AsString;
  if CDS_HZ.fieldbyname('Qty').AsCurrency<>0 then
  FPrice:=currtostr(Strtocurr(FBBMoney)/CDS_HZ.fieldbyname('Qty').AsCurrency)
  else
  FPrice:='0';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    SQL.ADD(',Price='''+trim(FPrice)+'''');
    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 KCKPMoney=KCKPMoney-'+FKCBB+'+'+FBBMoney);
    sql.Add(' where CRID='''+Trim(CDS_HZ.fieldbyname('CRID').AsString)+'''');
    ExecSQL;
  end;
  with cds_HZ do
  begin
    edit;
    fieldbyname('price').Value:=trim(FPrice);
  end;
end;

procedure TfrmYSFP_WXCX.v2PSPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv1.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 TfrmYSFP_WXCX.v2BatchNoPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv1.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 TfrmYSFP_WXCX.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 TfrmYSFP_WXCX.khConNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSFP_WXCX.VKPNOPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('KPNO').Value:=trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set KPNO='''+Trim(mvalue)+''' ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYSFP_WXCX.v2KPDMNoPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('KPDMNo').Value:=trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set KPDMNo='''+Trim(mvalue)+''' ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYSFP_WXCX.v2ZFTTPropertiesButtonClick(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(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSFP_WXCX.cxGridDBColumn3PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('P_CodeName').Value:=trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set P_CodeName='''+Trim(mvalue)+''' ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYSFP_WXCX.v2P_SpecPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('P_Spec').Value:=trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set P_Spec='''+Trim(mvalue)+''' ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmYSFP_WXCX.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Sub,True);
end;

procedure TfrmYSFP_WXCX.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Sub,false);
end;

procedure TfrmYSFP_WXCX.v2SselPropertiesChange(Sender: TObject);
begin
  Toolbar1.SetFocus;
  //HJ(CDS_HZ,Label12,CDS_HZ.fieldbyname('KPMoney').AsCurrency);
end;

procedure TfrmYSFP_WXCX.TADDClick(Sender: TObject);
begin
  frmYSFP_WXDJ:=TfrmYSFP_WXDJ.Create(self);
  with frmYSFP_WXDJ do
  begin
    FromID:='0';
    FFPID:='';
    if showmodal=1 then
    begin
      Initgrid();
    end;
    free;
  end;
end;

procedure TfrmYSFP_WXCX.TUpdateClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  frmYSFP_WXDJ:=TfrmYSFP_WXDJ.Create(self);
  with frmYSFP_WXDJ do
  begin
    FromID:='0';
    FFPID:=trim(CDS_HZ.fieldbyname('FPID').AsString);
    if showmodal=1 then
    begin
      Initgrid();
    end;
    free;
  end;
end;

procedure TfrmYSFP_WXCX.TCKClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  frmYSFP_WXDJ:=TfrmYSFP_WXDJ.Create(self);
  with frmYSFP_WXDJ do
  begin
    FromID:='0';
    FFPID:=trim(CDS_HZ.fieldbyname('FPID').AsString);
    TBSave.Visible:=False;
    ToolBar2.Visible:=False;
    if showmodal=1 then
    begin
      Initgrid();
    end;
    free;
  end;
end;

procedure TfrmYSFP_WXCX.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  TCK.Click;
end;

procedure TfrmYSFP_WXCX.TFZClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  frmYSFP_WXDJ:=TfrmYSFP_WXDJ.Create(self);
  with frmYSFP_WXDJ do
  begin
    FromID:='1';
    FFPID:=trim(CDS_HZ.fieldbyname('FPID').AsString);
    if showmodal=1 then
    begin
      Initgrid();
    end;
    free;
  end;
end;

procedure TfrmYSFP_WXCX.TPrintClick(Sender: TObject);
var
  fPrintFile,fZMFile:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('select A.*,B.* ');
    sql.add('from JYOrderFPPI_Main A ');
    sql.add('inner join JYOrderFPPI_Sub B on A.FPID=B.FPID ');
    sql.add('where A.FPID='''+trim(CDS_HZ.fieldbyname('FPID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,Order_Print);
  SInitCDSData20(ADOQueryTemp,Order_Print);
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(ComboBox1.Text)+'.rmf' ;
  if FileExists(fPrintFile) then
  begin
    if RMCD.CanExport=true then
    begin
      fZMFile:='C:\Users\Administrator\Desktop';
      if not DirectoryExists(fZMFile) then
      begin
        fZMFile:='C:\Documents and Settings\Administrator\桌面\'+trim(CDS_HZ.fieldbyname('HZConNO').AsString)+'.xls';
      end
      else
      begin
        fZMFile:='C:\Users\Administrator\Desktop\'+trim(CDS_HZ.fieldbyname('HZConNO').AsString)+'.XLS';
      end;
      RMCD.ExportTo(RMXLSExport1,fZMFile);
    end;
    RMCD.CanExport:=true;
    RMCD.LoadFromFile(fPrintFile);
    RMCD.ShowReport;
    RMCD.CanExport:=False;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\外销发票PI.rmf'),'提示',0);
  end;
end;

procedure TfrmYSFP_WXCX.Tv2CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  try
    ADOQueryCmd.DisableControls;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*');
      sql.Add(' from JYOrderFPPI_Sub A');
      sql.Add(' where A.FPID='''+Trim(CDS_HZ.fieldbyname('FPID').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryCmd,CDS_Sub);
    SInitCDSData20(ADOQueryCmd,CDS_Sub);
  finally
    ADOQueryCmd.EnableControls;
  end;
end;

procedure TfrmYSFP_WXCX.TTJClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('Ssel',true,[])=false then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  with CDS_HZ do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').AsBoolean=true then
      begin
        with ADOQueryMain do
        begin
          close;
          sql.Clear;
          sql.Add('update JYOrderFPPI_Main set chkstatus=''未审核'' ');
          sql.add('where FPID='''+trim(CDS_HZ.fieldbyname('FPID').AsString)+'''');
          execsql;
        end;
      end;
      next;
    end;
  end;
  initgrid();
  application.MessageBox('操作成功','提示');
end;

procedure TfrmYSFP_WXCX.TCXTJClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('Ssel',true,[])=false then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  with CDS_HZ do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').AsBoolean=true then
      begin
        with ADOQueryMain do
        begin
          close;
          sql.Clear;
          sql.Add('update JYOrderFPPI_Main set chkstatus='''' ');
          sql.add('where FPID='''+trim(CDS_HZ.fieldbyname('FPID').AsString)+'''');
          execsql;
        end;
      end;
      next;
    end;
  end;
  initgrid();
  application.MessageBox('操作成功','提示');
end;

procedure TfrmYSFP_WXCX.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  initgrid();
end;

procedure TfrmYSFP_WXCX.TSselClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then exit;
  Modalresult:=1;
end;

end.
