unit U_WXFPInPutWGL;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxDropDownEdit, cxCalendar, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxSplitter;

type
  TfrmWXFPInPutWGL = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ClientDataSet2: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Label1: TLabel;
    FPNO: TEdit;
    Label2: TLabel;
    FPDate: TDateTimePicker;
    ChengJiaoType: TBtnEditA;
    Label3: TLabel;
    FromPlace: TBtnEditA;
    Label4: TLabel;
    ToPlace: TBtnEditA;
    Label5: TLabel;
    BankName: TBtnEditA;
    Label6: TLabel;
    Label7: TLabel;
    GHDanWei: TEdit;
    Label8: TLabel;
    XHDanWei: TEdit;
    Label9: TLabel;
    XYZNO: TEdit;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Panel2: TPanel;
    Panel3: TPanel;
    Label10: TLabel;
    OrderNo: TEdit;
    Button1: TButton;
    cxGrid6: TcxGrid;
    Tv6: TcxGridDBTableView;
    v6Column5: TcxGridDBColumn;
    v6BCOrderNo: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v6Column10: TcxGridDBColumn;
    v6Column11: TcxGridDBColumn;
    v6Column8: TcxGridDBColumn;
    v6Column9: TcxGridDBColumn;
    Tv6Column2: TcxGridDBColumn;
    v6Column2: TcxGridDBColumn;
    v6Column3: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    cxSplitter2: TcxSplitter;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    v6Column6: TcxGridDBColumn;
    v6Column1: TcxGridDBColumn;
    v6Column12: TcxGridDBColumn;
    DataSource3: TDataSource;
    CDS_HZ: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v6Column4: TcxGridDBColumn;
    v6Column7: TcxGridDBColumn;
    v6Column13: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column20PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ChengJiaoTypeBtnClick(Sender: TObject);
    procedure FromPlaceBtnClick(Sender: TObject);
    procedure ToPlaceBtnClick(Sender: TObject);
    procedure BankNameBtnClick(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv6CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv6CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    procedure GetMaxConNO();
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FMainId,FConNo,FConType,FYLType:String;

    { Public declarations }
  end;

var
  frmWXFPInPutWGL: TfrmWXFPInPutWGL;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_BGHZSelList;

{$R *.dfm}

procedure TfrmWXFPInPutWGL.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('外销发票录入',Tv1,'发票管理');
end;

procedure TfrmWXFPInPutWGL.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.* ');
    SQL.Add(' from FP_XiaoShou A');
    sql.Add(' inner join FP_XiaoShou_Sub B on A.FXID=B.FXID');
    sql.Add(' where A.FXID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  SCSHDataNew(ADOQuery1,Panel1,0);

end;

procedure TfrmWXFPInPutWGL.ZDYHelp(FButn:TcxButtonEdit;LType:string);
begin
end;

procedure TfrmWXFPInPutWGL.FormShow(Sender: TObject);
begin
  ReadCxGrid('外销发票录入',Tv1,'发票管理');
  FPDate.DateTime:=SGetServerDate(ADOTemp);
  InitData();
end;
procedure TfrmWXFPInPutWGL.GetMaxConNO();
begin

end;
function TfrmWXFPInPutWGL.SaveData():Boolean;
var
  maxno,maxsubNo:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'FX','FP_XiaoShou',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from FP_XiaoShou where FXID='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('FXID').Value:=Trim(maxno);
      RTSetsavedata(ADOCmd,'FP_XiaoShou',Panel1,0);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
     // FieldByName('Note').Value:=Trim(Note.Text);
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from FP_XiaoShou where FPNo='''+Trim(FPNo.Text)+'''');
      Open;
    end;
    if ADOCmd.RecordCount>1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('发票号重复!','提示',0);
      Exit;
    end;

    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('FSID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxsubNo,'FS','FP_XiaoShou_Sub',4,1)=False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取规则流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxsubNo:=Trim(Order_Sub.fieldbyname('FSID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from FP_XiaoShou_Sub where FSID='''+Trim(Order_Sub.fieldbyname('FSID').AsString)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('FSID').AsString)='' then
          begin
            Append;
          end
          else begin
            Edit;
          end;
          FieldByName('FXID').Value:=Trim(maxno);
          FieldByName('FSID').Value:=Trim(maxsubNo);
          FieldByName('BGID').Value:=Order_Sub.fieldbyname('BGID').Value;
          FieldByName('BCID').Value:=Order_Sub.fieldbyname('BCID').Value;
          FieldByName('NQID').Value:=Order_Sub.fieldbyname('NQID').Value;
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'FP_XiaoShou_Sub',0) ;
          Post;
        end;
        Edit;
        FieldByName('FSID').Value:=Trim(maxsubNo);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    ADOCmd.Connection.CommitTrans;
    FMainId:=Trim(maxno);
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmWXFPInPutWGL.TBSaveClick(Sender: TObject);
var
  FPPrice,PRTPrice,BCMoney,FPQty,ChaMoney:string;
begin
  ToolBar1.SetFocus;
  if Trim(FPNO.Text)='' then
  begin
    Application.MessageBox('发票号不能为空!','提示',0);
    Exit;
  end;
  if Trim(GHDanWei.Text)='' then
  begin
    Application.MessageBox('购货单位不能为空!','提示',0);
    Exit;
  end;
  if Trim(XHDanWei.Text)='' then
  begin
    Application.MessageBox('销货单位不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if Order_Sub.Locate('Qty',null,[]) then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('Price',null,[]) then
  begin
    Application.MessageBox('单价不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('Money',null,[]) then
  begin
    Application.MessageBox('金额不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
   // ModalResult:=1;
  end;

end;

procedure TfrmWXFPInPutWGL.ToolButton3Click(Sender: TObject);
var
  FSQDate,FGHDanWei,FXHDanWei:string;
begin
  FSQDate:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOTemp)));
  try
    frmBGHZSelList:=TfrmBGHZSelList.Create(Application);
    with frmBGHZSelList do
    begin
      if ShowModal=1 then
      begin
        with frmBGHZSelList.CDS_HZ do
        begin
          First;
          while not Eof do
          begin
            if frmBGHZSelList.CDS_HZ.FieldByName('SSel').AsBoolean=True then
            begin
              if  frmBGHZSelList.CDS_HZ.FieldByName('WXFPMoney').Value=0 then
              begin
                FGHDanWei:=Trim(frmBGHZSelList.CDS_HZ.fieldbyname('KHName').AsString);
                FXHDanWei:=Trim(frmBGHZSelList.CDS_HZ.fieldbyname('FPDanWei').AsString);
                with Self.Order_Sub do
                begin
                  Append;
                  FieldByName('BGId').Value:=frmBGHZSelList.CDS_HZ.FieldByName('BGId').Value;
                  FieldByName('BCID').Value:=frmBGHZSelList.CDS_HZ.FieldByName('BCID').Value;
                  FieldByName('NQID').Value:=frmBGHZSelList.CDS_HZ.FieldByName('NQID').Value;
                  FieldByName('OrderNo').Value:=frmBGHZSelList.CDS_HZ.FieldByName('OrderNo').Value;
                  FieldByName('BGName').Value:=frmBGHZSelList.CDS_HZ.FieldByName('BGName').Value;
                  FieldByName('QtyUnit').Value:=frmBGHZSelList.CDS_HZ.FieldByName('NQUnit').Value;
                  FieldByName('SGHDanWei').Value:=frmBGHZSelList.CDS_HZ.FieldByName('KHName').Value;
                  FieldByName('SXHDanWei').Value:=frmBGHZSelList.CDS_HZ.FieldByName('FPDanWei').Value;
                  if Trim(frmBGHZSelList.CDS_HZ.FieldByName('NQUnit').AsString)='M' then
                  begin
                    FieldByName('Qty').Value:=frmBGHZSelList.CDS_HZ.FieldByName('NQMQty').Value;
                  end else
                  begin
                    FieldByName('Qty').Value:=frmBGHZSelList.CDS_HZ.FieldByName('NQkgQty').Value;
                  end;
                  FieldByName('Price').Value:=frmBGHZSelList.CDS_HZ.FieldByName('BGPrice').Value;
                  FieldByName('Money').Value:=frmBGHZSelList.CDS_HZ.FieldByName('BGMoney').Value;
                  FieldByName('FPDate').Value:=FSQDate;
                  //Post;
                end;
              end;

            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmBGHZSelList.Free;
  end;
  if Trim(GHDanWei.Text)='' then
    GHDanWei.Text:=Trim(FGHDanWei);
  if Trim(XHDanWei.Text)='' then
    XHDanWei.Text:=Trim(FXHDanWei);
end;

procedure TfrmWXFPInPutWGL.ToolButton4Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('FSId').AsString)<>'' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select isnull(sum(HXMoney),0) HXMoney from FP_Money_HX where FSID='''+Trim(Order_Sub.fieldbyname('FSID').AsString)+'''');
      sql.Add(' and isnull(Type,'''')=''核销内部发票'' ');
      Open;
    end;
    if ADOTemp.FieldByName('HXMoney').Value<>0 then
    begin
      Application.MessageBox('已有核销数据不能删除!','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete FP_XiaoShou_Sub where FSId='''+Trim(Order_Sub.fieldbyname('FSId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmWXFPInPutWGL.v1Column3PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FName:String;
begin
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then mvalue:='0';
  with Order_Sub do
  begin
    Edit;
    FieldByName(FName).Value:=mvalue;
    Post;
  end;
  FQty:=Order_Sub.fieldbyname('Qty').AsString;
  FPrice:=Order_Sub.fieldbyname('Price').AsString;
  FMoney:=Order_Sub.fieldbyname('Money').AsString;
  if Trim(FQty)='' then FQty:='0';
  if Trim(FPrice)='' then FPrice:='0';
  if Trim(FMoney)='' then FMoney:='0';
  if Trim(FName)<>'Money' then
  begin
    with Order_Sub do
    begin
      Edit;
      FieldByName('Money').Value:=StrToFloat(FQty)*StrToFloat(FPrice);
      Post;
    end;
  end else
  begin
    if Trim(FQty)<>'0' then
    begin
      with Order_Sub do
      begin
        Edit;
        FieldByName('Price').Value:=StrToFloat(FMoney)*1.000000/StrToFloat(FQty);
        Post;
      end;
    end else
    begin
      with Order_Sub do
      begin
        Edit;
        FieldByName('Price').Value:=Null;
        Post;
      end;
    end;
  end;
end;

procedure TfrmWXFPInPutWGL.v1Column20PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GSTT';
      flagname:='报关单位';
      V1ZdyStr1.Visible:=True;
      V1ZdyStr2.Visible:=True;
      V1ZdyStr3.Visible:=True;
      V1ZdyStr4.Visible:=True;
      V1ZdyStr1.Caption:='简称';
      V1ZdyStr2.Caption:='电话';
      V1ZdyStr3.Caption:='传真';
      V1ZdyStr4.Caption:='地址';
      frmZDYHelp.Align:=alClient;
      if ShowModal=1 then
      begin
        with Order_Sub do
        begin
          Edit;
          FieldByName('GHDanWei').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWXFPInPutWGL.ChengJiaoTypeBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ChengJiaoType';
      flagname:='成交方式';
      if ShowModal=1 then
      begin
        ChengJiaoType.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWXFPInPutWGL.FromPlaceBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ChuKouKA';
      flagname:='出口口岸';
      if ShowModal=1 then
      begin
        FromPlace.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWXFPInPutWGL.ToPlaceBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YunDiG';
      flagname:='运抵国';
      if ShowModal=1 then
      begin
        ToPlace.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWXFPInPutWGL.BankNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WXBankName';
      flagname:='银行名称';
      if ShowModal=1 then
      begin
        BankName.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWXFPInPutWGL.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(OrderNo.Text))<3 then Exit;
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.NQMQty,B.NQKgQty,B.NQYQty,B.BGPrice,B.BGMoney,B.NQUnit,C.BCOrderNo ');
      sql.Add(',KHName=(select CustomerNoName from JYOrder_Main BB where BB.OrderNo=A.OrderNo)');
      SQL.Add(',DHXMoney=A.Money-isnull(A.HXMoney,0)');
      SQL.Add(' ,BCHXMoney=(select HXMoney from FP_Money_HX HX where HX.NBFMID=A.FMID  ');
      SQL.Add('  and HX.FSID='''+Trim(Order_Sub.fieldbyname('FSID').AsString)+''')');
      sql.Add('from FP_GYSNB A');
      sql.Add('inner join JYOrder_BG_CKName_Qty B on A.NQID=B.NQID');
      sql.Add(' inner join JYOrder_BG_CK C on B.BCId=C.BCId');
      sql.Add(' inner join JYOrder_BG D on B.BGId=D.BGId');
      SQL.Add(' where  A.OrderNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
      Open;
    end;
    SCreateCDS20(ADOTemp,CDS_HZ);
    SInitCDSData20(ADOTemp,CDS_HZ);
  end;
end;

procedure TfrmWXFPInPutWGL.Tv6CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_HZ.IsEmpty then Exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(',BCHXMoney=(select HXMoney from FP_Money_HX B where B.GYSFMID=A.FMID ');
    sql.Add(' and B.NBFMID='''+Trim(CDS_HZ.fieldbyname('FMID').AsString)+''')');
    sql.Add(' from FP_GYSNB A');
    sql.Add(' where exists(select * from FP_Money_HX HX where HX.GYSFMID=A.FMID');
    sql.Add(' and HX.NBFMID='''+Trim(CDS_HZ.fieldbyname('FMID').AsString)+''')');
    Open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet2);
  SInitCDSData20(ADOTemp,ClientDataSet2);
end;

procedure TfrmWXFPInPutWGL.Tv6CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with Self.Order_Sub do
  begin
    Append;
    FieldByName('BGId').Value:=CDS_HZ.FieldByName('BGId').Value;
    FieldByName('BCID').Value:=CDS_HZ.FieldByName('BCID').Value;
    FieldByName('NQID').Value:=CDS_HZ.FieldByName('NQID').Value;
    FieldByName('OrderNo').Value:=CDS_HZ.FieldByName('OrderNo').Value;
    FieldByName('BGName').Value:=CDS_HZ.FieldByName('BGName').Value;
    FieldByName('QtyUnit').Value:=CDS_HZ.FieldByName('NQUnit').Value;
    FieldByName('SGHDanWei').Value:=CDS_HZ.FieldByName('KHName').Value;
    FieldByName('SXHDanWei').Value:=CDS_HZ.FieldByName('GHDanWei').Value;
    if Trim(CDS_HZ.FieldByName('NQUnit').AsString)='M' then
    begin
      FieldByName('Qty').Value:=CDS_HZ.FieldByName('NQMQty').Value;
    end else
    begin
      FieldByName('Qty').Value:=CDS_HZ.FieldByName('NQkgQty').Value;
    end;
    FieldByName('Price').Value:=CDS_HZ.FieldByName('BGPrice').Value;
    FieldByName('Money').Value:=CDS_HZ.FieldByName('BGMoney').Value;
    FieldByName('FPDate').Value:=SGetServerDate(ADOTemp);
    //Post;
  end;
  GHDanWei.Text:=CDS_HZ.FieldByName('KHName').Value;
  XHDanWei.Text:=CDS_HZ.FieldByName('GHDanWei').Value
end;

procedure TfrmWXFPInPutWGL.Button1Click(Sender: TObject);
var
  maxno,FFHID:string;
begin
  if Order_Sub.IsEmpty then Exit;
  if CDS_HZ.IsEmpty then Exit;
  if Trim(Order_Sub.FieldByName('FSID').AsString)='' then
  begin
    Application.MessageBox('数据未保存!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要核销吗？','提示',32+4)<>IDYES then exit;
  try
    ADOCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from FP_Money_HX where NBFMID='''+Trim(CDS_HZ.fieldbyname('FMID').AsString)+'''');
          sql.Add(' and FSID='''+Trim(Order_Sub.fieldbyname('FSID').AsString)+'''');
          open;
        end;
        FFHID:=Trim(ADOTemp.fieldbyname('FHID').AsString);
        if Trim(FFHID)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'FH','FP_Money_HX',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取发票核销最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(FFHID);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from FP_Money_HX where FHID='''+Trim(FFHID)+'''');
          open;
        end;
        with ADOCmd do
        begin
          if Trim(FFHID)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
          end;
          FieldByName('FHID').Value:=Trim(maxno);
          FieldByName('NBFMID').Value:=Trim(CDS_HZ.fieldbyname('FMID').AsString);
          FieldByName('FSID').Value:=Trim(Order_Sub.fieldbyname('FSID').AsString);
          FieldByName('FXID').Value:=Trim(FMainId);
          if Trim(CDS_HZ.fieldbyname('BCHXMoney').AsString)='' then
          begin
            FieldByName('HXMoney').Value:=0;
          end else
          begin
            FieldByName('HXMoney').Value:=CDS_HZ.fieldbyname('BCHXMoney').Value;
          end;
          FieldByName('Type').Value:='核销内部发票';
          Post;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' Update FP_GYSNB Set HXMoney=(select Sum(HXMoney) from FP_Money_HX A');
          sql.Add(' where A.NBFMID=FP_GYSNB.FMID and Isnull(A.Type,'''')=''核销内部发票'')');
          sql.Add(' where FMID='''+Trim(CDS_HZ.fieldbyname('FMID').AsString)+'''');
          ExecSQL;
        end;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功!','提示',0);
    Exit;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmWXFPInPutWGL.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.NQMQty,B.NQKgQty,B.NQYQty,B.BGPrice,B.BGMoney,B.NQUnit,C.BCOrderNo ');
    sql.Add(',KHName=(select CustomerNoName from JYOrder_Main BB where BB.OrderNo=A.OrderNo)');
    SQL.Add(',DHXMoney=A.Money-isnull(A.HXMoney,0)');
    SQL.Add(' ,BCHXMoney=(select HXMoney from FP_Money_HX HX where HX.NBFMID=A.FMID  ');
    SQL.Add('  and HX.FSID='''+Trim(Order_Sub.fieldbyname('FSID').AsString)+''')');
    sql.Add('from FP_GYSNB A');
    sql.Add('inner join JYOrder_BG_CKName_Qty B on A.NQID=B.NQID');
    sql.Add(' inner join JYOrder_BG_CK C on B.BCId=C.BCId');
    sql.Add(' inner join JYOrder_BG D on B.BGId=D.BGId');
    SQL.Add(' where exists(select * from FP_Money_HX FMH where FMH.NBFMID=A.FMID  ');
    SQL.Add(' and isnull(FMH.FSID,'''')='''+Trim(Order_Sub.fieldbyname('FSID').AsString)+''' ');
    sql.Add(' and isnull(FMH.Type,'''')=''核销内部发票'' and isnull(FMH.HXMoney,0)<>0)');
    Open;
  end;
  SCreateCDS20(ADOTemp,CDS_HZ);
  SInitCDSData20(ADOTemp,CDS_HZ);
end;

end.
