unit U_NBFPInPutWGL;

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
  TfrmNBFPInPutWGL = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ClientDataSet1: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    Panel1: TPanel;
    Panel2: TPanel;
    Label9: TLabel;
    OrderNo: TEdit;
    cxGrid6: TcxGrid;
    Tv6: TcxGridDBTableView;
    v6Column5: TcxGridDBColumn;
    v6Column7: TcxGridDBColumn;
    v6BCOrderNo: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v6Column1: TcxGridDBColumn;
    Tv6Column1: TcxGridDBColumn;
    v6Column10: TcxGridDBColumn;
    v6Column11: TcxGridDBColumn;
    v6Column8: TcxGridDBColumn;
    v6Column9: TcxGridDBColumn;
    Tv6Column2: TcxGridDBColumn;
    v6Column2: TcxGridDBColumn;
    v6Column3: TcxGridDBColumn;
    v6Column12: TcxGridDBColumn;
    v6Column13: TcxGridDBColumn;
    v6Column4: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    cxSplitter2: TcxSplitter;
    DataSource3: TDataSource;
    CDS_HZ: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu3: TcxGridPopupMenu;
    Order_Main: TClientDataSet;
    DataSource4: TDataSource;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column1: TcxGridDBColumn;
    Button1: TButton;
    Button2: TButton;
    cxGridPopupMenu4: TcxGridPopupMenu;
    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 OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv6CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv6CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure v1Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button1Click(Sender: TObject);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    procedure GetMaxConNO();
    procedure InitGYSFP();
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FMainId,FConNo,FConType,FYLType:String;

    { Public declarations }
  end;

var
  frmNBFPInPutWGL: TfrmNBFPInPutWGL;

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

{$R *.dfm}

procedure TfrmNBFPInPutWGL.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('供应商发票录入',Tv1,'发票管理');
  WriteCxGrid('供应商发票录入6',Tv6,'发票管理');
  WriteCxGrid('供应商发票录入2',Tv2,'发票管理');
end;

procedure TfrmNBFPInPutWGL.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.* ');
    SQL.Add(' from FP_GYSNB A');
    sql.Add(' where A.FMID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  

end;

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

procedure TfrmNBFPInPutWGL.FormShow(Sender: TObject);
begin
  ReadCxGrid('供应商发票录入',Tv1,'发票管理');
  ReadCxGrid('供应商发票录入6',Tv6,'发票管理');
  ReadCxGrid('供应商发票录入2',Tv2,'发票管理');
  InitData();
end;
procedure TfrmNBFPInPutWGL.GetMaxConNO();
begin

end;
function TfrmNBFPInPutWGL.SaveData():Boolean;
var
  maxno:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('FMID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'FM','FP_GYSNB',4,1)=False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取规则流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('FMID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from FP_GYSNB where FMID='''+Trim(Order_Sub.fieldbyname('FMID').AsString)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('FMID').AsString)='' then
          begin
            Append;
          end
          else begin
            Edit;
          end;
          FieldByName('FMID').Value:=Trim(maxno);
          FieldByName('BGID').Value:=Order_Sub.fieldbyname('BGID').Value;
          FieldByName('BCID').Value:=Order_Sub.fieldbyname('BCID').Value;
          FieldByName('NQID').Value:=Order_Sub.fieldbyname('NQID').Value;
          //FieldByName('FactoryNo').Value:=Order_Sub.fieldbyname('FactoryNo').Value;
          FieldByName('FMType').Value:='NB';
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'FP_GYSNB',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;
          Post;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from FP_GYSNB where FPNO='''+Trim(Order_Sub.fieldbyname('FPNO').AsString)+'''');
          sql.Add(' and isnull(FMType,'''')=''NB'' ');
          Open;
        end;
        if ADOCmd.RecordCount>1 then
        begin
          Order_Sub.EnableControls;
          ADOCmd.Connection.RollbackTrans;
          Result:=False;
          Application.MessageBox(PChar('发票号:'+Trim(Order_Sub.fieldbyname('FPNO').AsString)+'重复!'),'',0);
          Exit;
        end;
        Edit;
        FieldByName('FMID').Value:=Trim(maxno);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmNBFPInPutWGL.TBSaveClick(Sender: TObject);
var
  FPPrice,PRTPrice,BCMoney,FPQty,ChaMoney:string;
begin
  ToolBar1.SetFocus;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if Order_Sub.Locate('FPNO',null,[]) then
  begin
    Application.MessageBox('发票号不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('FPDate',null,[]) then
  begin
    Application.MessageBox('发票日期不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('GHDanWei',null,[]) 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 TfrmNBFPInPutWGL.ToolButton3Click(Sender: TObject);
var
  FSQDate: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
              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('XHDanWei').Value:=frmBGHZSelList.CDS_HZ.FieldByName('ComTaiTou').Value;
                FieldByName('QtyUnit').Value:=frmBGHZSelList.CDS_HZ.FieldByName('NQUnit').Value;
                if Trim(frmBGHZSelList.CDS_HZ.FieldByName('NQUnit').AsString)='M' then
                begin
                  FieldByName('Qty').Value:=frmBGHZSelList.CDS_HZ.FieldByName('NQMQty').Value
                                            -frmBGHZSelList.CDS_HZ.FieldByName('FPQty').Value;
                end else
                begin
                  FieldByName('Qty').Value:=frmBGHZSelList.CDS_HZ.FieldByName('NQkgQty').Value
                                            -frmBGHZSelList.CDS_HZ.FieldByName('FPQty').Value;
                end;
                FieldByName('FPDate').Value:=FSQDate;
                //Post;
              end;
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmBGHZSelList.Free;
  end;
end;

procedure TfrmNBFPInPutWGL.ToolButton4Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('FMId').AsString)<>'' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select isnull(sum(HXMoney),0) HXMoney from FP_Money_HX where NBFMID='''+Trim(Order_Sub.fieldbyname('FMId').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_GYSNB where FMId='''+Trim(Order_Sub.fieldbyname('FMId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmNBFPInPutWGL.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 TfrmNBFPInPutWGL.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 TfrmNBFPInPutWGL.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(OrderNo.Text)<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        SQL.Clear;
        sql.Add(' select A.*,B.OrderNo,B.ComTaiTou,C.Chker,C.ChkTime,C.ChkStatus,C.BCOrderNo  ');
        sql.Add(' ,FPMoney=isnull((select Sum(Money) from FP_GYSNB AA where AA.NQID=A.NQID),0)');
        sql.Add(' ,FPQty=isnull((select Sum(Qty) from FP_GYSNB AA where AA.NQID=A.NQID),0)');
        sql.Add(',FPDanWei=(select Top 1 GHDanWei from FP_GYSNB AA where AA.NQID=A.NQID)');
        sql.Add(',KHName=(select CustomerNoName from JYOrder_Main BB where BB.OrderNo=B.OrderNo)');
        sql.Add(' ,WXFPMoney=isnull((select Sum(Money) from FP_XiaoShou_Sub AA where AA.NQID=A.NQID),0)');
        sql.Add(' ,WXFPQty=isnull((select Sum(Qty) from FP_XiaoShou_Sub AA where AA.NQID=A.NQID),0)');
        sql.Add(' from JYOrder_BG_CKName_Qty A  ');
        sql.Add(' inner join JYOrder_BG_CK C on A.BCId=C.BCId');
        sql.Add(' inner join JYOrder_BG B on A.BGId=B.BGId');
        SQL.Add(' where  B.OrderNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,CDS_HZ);
      SInitCDSData20(ADOQueryMain,CDS_HZ);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmNBFPInPutWGL.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('XHDanWei').Value:=Order_Main.FieldByName('GHDanWei').Value;
    FieldByName('GHDanWei').Value:=CDS_HZ.FieldByName('ComTaiTou').Value;
    FieldByName('QtyUnit').Value:=CDS_HZ.FieldByName('NQUnit').Value;
    if Trim(CDS_HZ.FieldByName('NQUnit').AsString)='M' then
    begin
      FieldByName('Qty').Value:=CDS_HZ.FieldByName('NQMQty').Value
                                -CDS_HZ.FieldByName('FPQty').Value;
    end else
    begin
      FieldByName('Qty').Value:=CDS_HZ.FieldByName('NQkgQty').Value
                                -CDS_HZ.FieldByName('FPQty').Value;
    end;
    FieldByName('FPDate').Value:=SGetServerDate(ADOTemp);
    //Post;
  end;
end;

procedure TfrmNBFPInPutWGL.Tv6CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  {with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select A.*,DHXMoney=A.Money-isnull(A.HXMoney,0),BCHXMoney=Cast(0 as decimal(18,2) )  ');
    sql.Add(' from FP_GYSNB A');
    sql.Add(' where A.BCID='''+Trim(CDS_HZ.fieldbyname('BCID').AsString)+''' and A.FMType=''GYS'' ');
    sql.Add(' union All');
    sql.Add(' select A.*,DHXMoney=A.Money-isnull(A.HXMoney,0),BCHXMoney=Cast(0 as decimal(18,2) )  ');
    sql.Add(' from FP_GYSNB A');
    sql.Add(' where  A.FMType=''GYS'' ');
    sql.Add(' and isnull(A.BCID,'''')<>'''+Trim(CDS_HZ.fieldbyname('BCID').AsString)+'''');
    SQL.Add(' and A.Money<>isnull(A.HXMoney,0)');
    sql.Add(' and A.FactoryName in(');
    sql.Add(' select A.FactoryName  ');
    sql.Add(' from FP_GYSNB A');
    sql.Add(' where A.BCID='''+Trim(CDS_HZ.fieldbyname('BCID').AsString)+''' and A.FMType=''GYS'' ');
    sql.Add(')');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main); }
end;

procedure TfrmNBFPInPutWGL.v1Column5PropertiesButtonClick(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('XHDanWei').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmNBFPInPutWGL.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select A.*,B.OrderNo,B.ComTaiTou,C.Chker,C.ChkTime,C.ChkStatus,C.BCOrderNo  ');
      sql.Add(' ,FPMoney=isnull((select Sum(Money) from FP_GYSNB AA where AA.NQID=A.NQID),0)');
      sql.Add(' ,FPQty=isnull((select Sum(Qty) from FP_GYSNB AA where AA.NQID=A.NQID),0)');
      sql.Add(',FPDanWei=(select Top 1 GHDanWei from FP_GYSNB AA where AA.NQID=A.NQID)');
      sql.Add(',KHName=(select CustomerNoName from JYOrder_Main BB where BB.OrderNo=B.OrderNo)');
      sql.Add(' ,WXFPMoney=isnull((select Sum(Money) from FP_XiaoShou_Sub AA where AA.NQID=A.NQID),0)');
      sql.Add(' ,WXFPQty=isnull((select Sum(Qty) from FP_XiaoShou_Sub AA where AA.NQID=A.NQID),0)');
      sql.Add(' from JYOrder_BG_CKName_Qty A  ');
      sql.Add(' inner join JYOrder_BG_CK C on A.BCId=C.BCId');
      sql.Add(' inner join JYOrder_BG B on A.BGId=B.BGId');
      SQL.Add(' where  B.OrderNo='''+Trim(Order_Sub.fieldbyname('OrderNo').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
  InitGYSFP();
end;

procedure TfrmNBFPInPutWGL.Button1Click(Sender: TObject);
var
  maxno,FFHID:string;
begin
  if Order_Sub.IsEmpty then Exit;
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Sub.FieldByName('FMID').AsString)='' then
  begin
    Application.MessageBox('数据未保存!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要核销吗？','提示',32+4)<>IDYES then exit;
  try
    ADOCmd.Connection.BeginTrans;
    with Order_Main 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(Order_Sub.fieldbyname('FMID').AsString)+'''');
          sql.Add(' and GYSFMID='''+Trim(Order_Main.fieldbyname('FMID').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('GYSFMID').Value:=Trim(Order_Main.fieldbyname('FMID').AsString);
          FieldByName('NBFMID').Value:=Trim(Order_Sub.fieldbyname('FMID').AsString);
          if Trim(Order_Main.fieldbyname('BCHXMoney').AsString)='' then
          begin
            FieldByName('HXMoney').Value:=0;
          end else
          begin
            FieldByName('HXMoney').Value:=Order_Main.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.GYSFMID=FP_GYSNB.FMID)');
          sql.Add(' where FMID='''+Trim(Order_Main.fieldbyname('FMID').AsString)+'''');
          ExecSQL;
        end;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    InitGYSFP();
    Application.MessageBox('操作成功!','提示',0);
    Exit;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;
procedure TfrmNBFPInPutWGL.InitGYSFP();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    {sql.Add(' select A.*,B.OrderNo,B.ComTaiTou,C.Chker,C.ChkTime,C.ChkStatus,  ');
    sql.Add(' FPMoney=(select Sum(Money) from FP_GYSNB AA where AA.BFID=A.BFID)');
    sql.Add(' from JYOrder_BG_KPFee A  ');
    sql.Add(' inner join JYOrder_BG_CK C on A.BCId=C.BCId');
    sql.Add(' inner join JYOrder_BG B on A.BGId=B.BGId');
    SQL.Add(' where A.BCID='''+Trim(CDS_HZ.fieldbyname('BCID').AsString)+'''');}
    sql.Add(' select A.*,DHXMoney=A.Money-isnull(A.HXMoney,0)');
    sql.Add(' ,BCHXMoney=(select HXMoney from FP_Money_HX HX where HX.GYSFMID=A.FMID  ');
    SQL.Add('  and HX.NBFMID='''+Trim(Order_Sub.fieldbyname('FMID').AsString)+''')');
    sql.Add(' from FP_GYSNB A');
    sql.Add(' where A.BCID='''+Trim(Order_Sub.fieldbyname('BCID').AsString)+''' and A.FMType=''GYS'' ');
    sql.Add(' and isnull(A.FPNO,'''') not like ''%QC%'' ');
    sql.Add(' and isnull(A.FPNO,'''') not like ''%TZ%'' ');
    sql.Add(' union All');
    sql.Add(' select A.*,DHXMoney=A.Money-isnull(A.HXMoney,0)  ');
    sql.Add(' ,BCHXMoney=(select HXMoney from FP_Money_HX HX where HX.GYSFMID=A.FMID  ');
    SQL.Add('  and HX.NBFMID='''+Trim(Order_Sub.fieldbyname('FMID').AsString)+''')');
    sql.Add(' from FP_GYSNB A');
    sql.Add(' where  A.FMType=''GYS'' ');
    sql.Add(' and isnull(A.FPNO,'''') not like ''%QC%'' ');
    sql.Add(' and isnull(A.FPNO,'''') not like ''%TZ%'' ');
    sql.Add(' and isnull(A.BCID,'''')<>'''+Trim(Order_Sub.fieldbyname('BCID').AsString)+'''');
    SQL.Add(' and A.Money<>isnull(A.HXMoney,0)');
    sql.Add(' and A.FactoryName in(');
    sql.Add(' select A.FactoryName  ');
    sql.Add(' from FP_GYSNB A');
    sql.Add(' where A.BCID='''+Trim(Order_Sub.fieldbyname('BCID').AsString)+''' and A.FMType=''GYS'' ');
    sql.Add(')');

    sql.Add(' union All');
    sql.Add(' select A.*,DHXMoney=A.Money-isnull(A.HXMoney,0)  ');
    sql.Add(' ,BCHXMoney=(select HXMoney from FP_Money_HX HX where HX.GYSFMID=A.FMID  ');
    SQL.Add('  and HX.NBFMID='''+Trim(Order_Sub.fieldbyname('FMID').AsString)+''')');
    sql.Add(' from FP_GYSNB A');
    sql.Add(' where  A.FMType=''GYS'' ');
    sql.Add(' and isnull(A.FPNO,'''') not like ''%QC%'' ');
    sql.Add(' and isnull(A.FPNO,'''') not like ''%TZ%'' ');
    SQL.Add(' and A.Money=isnull(A.HXMoney,0)');
    sql.Add(' and isnull(A.BCID,'''')='''' ');
    SQL.Add(' and exists(select * from FP_Money_HX FH where FH.GYSFMID=A.FMID');
    sql.Add('            and FH.NBFMID='''+Trim(Order_Sub.fieldbyname('FMID').AsString)+''')');
    //ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;
end.
