unit U_BaoGuanListSBYS;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, cxCheckBox, RM_Common,
  RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, Menus,
  cxCalendar, cxButtonEdit, cxTextEdit, cxPC, BtnEdit, cxSplitter;

type
  TfrmBaoGuanListSBYS = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ClientDataSet1: TClientDataSet;
    Label6: TLabel;
    A4FPNO: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column4: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    Label5: TLabel;
    A6PONO: TEdit;
    Label8: TLabel;
    A5ConNO: TEdit;
    v1Column15: TcxGridDBColumn;
    Label3: TLabel;
    A1ChuKouShang: TEdit;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column29: TcxGridDBColumn;
    v1Column30: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolButton7: TToolButton;
    RMXLSExport1: TRMXLSExport;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    ToolButton11: TToolButton;
    ToolButton12: TToolButton;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure CRTypeChange(Sender: TObject);
    procedure A4FPNOKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure A6PONOKeyPress(Sender: TObject; var Key: Char);
    procedure A5ConNOKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    canshu1:string;
    canshu2:string;
    FDate:TDateTime;
    procedure InitGrid();
    function DelData():Boolean;
    procedure InitSubGrid();
    procedure InitGridSql(var fsj:String);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmBaoGuanListSBYS: TfrmBaoGuanListSBYS;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,U_BaoGuanInPut;

{$R *.dfm}

procedure TfrmBaoGuanListSBYS.FormDestroy(Sender: TObject);
begin
  frmBaoGuanListSBYS:=nil;
end;

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

procedure TfrmBaoGuanListSBYS.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
end;

procedure TfrmBaoGuanListSBYS.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.* ');
      sql.Add(' from JYOrder_BaoGuan_Main A  ');
      sql.Add(' where Valid=''Y'' ');
      {if Trim(canshu1)<>'高权限' then
      begin
        SQL.Add(' and Filler='''+Trim(DName)+'''');
      end;}
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(SChker,'''')='''' ');
      end else
      begin
        sql.Add(' and BGDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and BGDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
        if cxTabControl1.TabIndex=1 then
        begin
          sql.Add(' and isnull(SChker,'''')<>'''' and isnull(Chker,'''')='''' ');
        end else
        if cxTabControl1.TabIndex=2 then
        begin
          sql.Add('  and isnull(Chker,'''')<>'''' ');
        end;
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBaoGuanListSBYS.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmBaoGuanListSBYS.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmBaoGuanListSBYS.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('报关主表SBYS',Tv1,'报关管理');
  WriteCxGrid('报关明细SBYS',Tv1,'报关管理');
  Close;
end;

procedure TfrmBaoGuanListSBYS.FormShow(Sender: TObject);
begin
  ReadCxGrid('报关主表SBYS',Tv1,'报关管理');
  ReadCxGrid('报关明细SBYS',Tv1,'报关管理');
end;

procedure TfrmBaoGuanListSBYS.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('报关资料列表',cxGrid1);
end;

procedure TfrmBaoGuanListSBYS.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmBaoGuanListSBYS.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

procedure TfrmBaoGuanListSBYS.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;
procedure TfrmBaoGuanListSBYS.InitSubGrid();
begin
  if CDS_Main.IsEmpty=False then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_BaoGuan_Sub where BGID='''+Trim(CDS_Main.fieldbyname('BGID').AsString)+'''');
      sql.Add(' and SValid=''Y'' ');
      Open;
    end;
  end else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_BaoGuan_Sub where 1=2');
      Open;
    end;
  end;

  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

function TfrmBaoGuanListSBYS.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while CDS_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_BaoGuan_Main Set Valid=''N'',C7BGMoneyHZ=Null,E1BZQtyHZ=Null, E2ChiMaQtyHZ=Null,E3MaoZHZ=Null,E4JingZHZ=Null');
          sql.Add(',Editer='''+Trim(DName)+''',EditTime=getdate()');
          sql.Add(' where BGId='''+Trim(CDS_Main.fieldbyname('BGId').AsString)+'''');
          sql.Add('UPdate JYOrder_BaoGuan_Sub Set SValid=''N'',SEditer='''+Trim(DName)+''',SEditTime=getdate()  where BGId='''+Trim(CDS_Main.fieldbyname('BGId').AsString)+'''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;
procedure TfrmBaoGuanListSBYS.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmBaoGuanListSBYS.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;



procedure TfrmBaoGuanListSBYS.CRTypeChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmBaoGuanListSBYS.A4FPNOKeyPress(Sender: TObject; var Key: Char);
var
  fsj:String;
begin
  if Key=#13 then
  begin
    if Trim(A4FPNO.Text)='' then Exit;
    fsj:=' and isnull(A.A4FPNO,'''') like '''+'%'+Trim(A4FPNO.Text)+'%'+'''';
    InitGridSql(fsj);
  end;
end;
procedure TfrmBaoGuanListSBYS.InitGridSql(var fsj:String);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.* ');
      sql.Add(' from JYOrder_BaoGuan_Main A  ');
      sql.Add(' where 1=1 ');
     { if Trim(canshu1)<>'高权限' then
      begin
        SQL.Add(' and FillerCode='''+Trim(DCode)+'''');
      end;}
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(SChker,'''')='''' ');
      end else
      begin
        if cxTabControl1.TabIndex=1 then
        begin
          sql.Add(' and isnull(SChker,'''')<>'''' and isnull(Chker,'''')='''' ');
        end else
        if cxTabControl1.TabIndex=2 then
        begin
          sql.Add('  and isnull(Chker,'''')<>'''' ');
        end;
      end;
      sql.Add(fsj);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBaoGuanListSBYS.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  InitSubGrid();
end;

procedure TfrmBaoGuanListSBYS.A6PONOKeyPress(Sender: TObject;
  var Key: Char);
var
  fsj:String;
begin
  if Key=#13 then
  begin
    if Trim(A6PONO.Text)='' then Exit;
    fsj:=' and isnull(A.A6PONO,'''') like '''+'%'+Trim(A6PONO.Text)+'%'+'''';
    InitGridSql(fsj);
  end;
end;

procedure TfrmBaoGuanListSBYS.A5ConNOKeyPress(Sender: TObject;
  var Key: Char);
var
  fsj:String;
begin
  if Key=#13 then
  begin
    if Trim(A5ConNO.Text)='' then Exit;
    fsj:=' and isnull(A.A5ConNO,'''') like '''+'%'+Trim(A5ConNO.Text)+'%'+'''';
    InitGridSql(fsj);
  end;
end;

procedure TfrmBaoGuanListSBYS.ToolButton7Click(Sender: TObject);
var
   fPrintFile:string;
   EngMoney,BZZH:string;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\报关资料.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.* ');
    sql.add(' ,A1ChuKouShangEng=(select Note from KH_Zdy where Type=''A1ChuKouShang'' and ZdyName=A.A1ChuKouShang )');
    sql.add(' ,B6ChuYunGangEng=(select Note from KH_Zdy where Type=''B6ChuYunGang'' and ZdyName=A.B6ChuYunGang )');
    sql.Add(' from JYOrder_BaoGuan_Main A inner join JYOrder_BaoGuan_Sub B on A.BGId=B.BGId ');
    sql.Add(' where A.BGId='''+Trim(CDS_Main.fieldbyname('BGId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  RMXLSExport1:= TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\报关资料.rmf'),'提示',0);
  end;
end;

procedure TfrmBaoGuanListSBYS.ToolButton8Click(Sender: TObject);
var
   fPrintFile:string;
   EngMoney,BZZH:string;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\报关合同.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.* ');
    sql.add(' ,A1ChuKouShangEng=(select Note from KH_Zdy where Type=''A1ChuKouShang'' and ZdyName=A.A1ChuKouShang )');
    sql.add(' ,B6ChuYunGangEng=(select Note from KH_Zdy where Type=''B6ChuYunGang'' and ZdyName=A.B6ChuYunGang )');
    sql.Add(',HTDate=(select ORDDate from JYOrderCon_Main F where F.ConNo=A.A5ConNO)');
    sql.Add(',KHNameEng=(select KHNameEng from ZH_KH_Info KH where KH.KHNameJC=');
    sql.Add('           (select CustomerNoName from JYOrderCon_Main F where F.ConNo=A.A5ConNO ))');
    sql.Add(' from JYOrder_BaoGuan_Main A inner join JYOrder_BaoGuan_Sub B on A.BGId=B.BGId ');
    sql.Add(' where A.BGId='''+Trim(CDS_Main.fieldbyname('BGId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  RMXLSExport1:= TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\报关合同.rmf'),'提示',0);
  end;
end;

procedure TfrmBaoGuanListSBYS.ToolButton9Click(Sender: TObject);
var
   fPrintFile:string;
   EngMoney,BZZH:string;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\报关发票.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.* ');
    sql.add(' ,A1ChuKouShangEng=(select Note from KH_Zdy where Type=''A1ChuKouShang'' and ZdyName=A.A1ChuKouShang )');
    sql.add(' ,B6ChuYunGangEng=(select Note from KH_Zdy where Type=''B6ChuYunGang'' and ZdyName=A.B6ChuYunGang )');
    sql.Add(',HTDate=(select ORDDate from JYOrderCon_Main F where F.ConNo=A.A5ConNO)');
    sql.Add(',KHNameEng=(select KHNameEng from ZH_KH_Info KH where KH.KHNameJC=');
    sql.Add('           (select CustomerNoName from JYOrderCon_Main F where F.ConNo=A.A5ConNO ))');
    sql.Add(' from JYOrder_BaoGuan_Main A inner join JYOrder_BaoGuan_Sub B on A.BGId=B.BGId ');
    sql.Add(' where A.BGId='''+Trim(CDS_Main.fieldbyname('BGId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  RMXLSExport1:= TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\报关发票.rmf'),'提示',0);
  end;
end;

procedure TfrmBaoGuanListSBYS.ToolButton10Click(Sender: TObject);
var
   fPrintFile:string;
   EngMoney,BZZH:string;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\报关装箱单.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,Case when C5BGUnit=''MTR'' then C4BGQty else Null end as C4BGQtyJS ');
    sql.add(' ,A1ChuKouShangEng=(select Note from KH_Zdy where Type=''A1ChuKouShang'' and ZdyName=A.A1ChuKouShang )');
    sql.add(' ,B6ChuYunGangEng=(select Note from KH_Zdy where Type=''B6ChuYunGang'' and ZdyName=A.B6ChuYunGang )');
    sql.Add(',HTDate=(select ORDDate from JYOrderCon_Main F where F.ConNo=A.A5ConNO)');
    sql.Add(',KHNameEng=(select KHNameEng from ZH_KH_Info KH where KH.KHNameJC=');
    sql.Add('           (select CustomerNoName from JYOrderCon_Main F where F.ConNo=A.A5ConNO ))');
    sql.Add(' from JYOrder_BaoGuan_Main A inner join JYOrder_BaoGuan_Sub B on A.BGId=B.BGId ');
    sql.Add(' where A.BGId='''+Trim(CDS_Main.fieldbyname('BGId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  RMXLSExport1:= TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\报关装箱单.rmf'),'提示',0);
  end;
end;

procedure TfrmBaoGuanListSBYS.ToolButton11Click(Sender: TObject);
var
   fPrintFile:string;
   EngMoney,BZZH:string;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\报关单.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,Case when C5BGUnit=''MTR'' then C4BGQty else Null end as C4BGQtyJS ');
    sql.Add(',Case when C5BGUnit<>''KG'' then  E4JingZ else Null End As E4JingZJS');
    sql.Add(',Case when C5BGUnit<>''KG'' then  ''KGS'' else Null End As JZUnit');
    sql.Add(',Case when C5BGUnit=''KG'' then  ''KGS'' else C5BGUnit End As C5BGUnitJS');
    sql.add(' ,A1ChuKouShangEng=(select Note from KH_Zdy where Type=''A1ChuKouShang'' and ZdyName=A.A1ChuKouShang )');
    sql.add(' ,B6ChuYunGangEng=(select Note from KH_Zdy where Type=''B6ChuYunGang'' and ZdyName=A.B6ChuYunGang )');
    sql.Add(',HTDate=(select ORDDate from JYOrderCon_Main F where F.ConNo=A.A5ConNO)');
    sql.Add(',KHNameEng=(select KHNameEng from ZH_KH_Info KH where KH.KHNameJC=');
    sql.Add('           (select CustomerNoName from JYOrderCon_Main F where F.ConNo=A.A5ConNO ))');
    sql.Add(' from JYOrder_BaoGuan_Main A inner join JYOrder_BaoGuan_Sub B on A.BGId=B.BGId ');
    sql.Add(' where A.BGId='''+Trim(CDS_Main.fieldbyname('BGId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  RMXLSExport1:= TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\报关单.rmf'),'提示',0);
  end;
end;

procedure TfrmBaoGuanListSBYS.ToolButton12Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  try
    frmBaoGuanInPut:=TfrmBaoGuanInPut.Create(Application);
    with frmBaoGuanInPut do
    begin
      FBCId:=Trim(CDS_Main.fieldbyname('BGID').AsString);
      TBSave.Visible:=False;
      ToolBar2.Visible:=False;
      Panel1.Enabled:=False;
      Tv1.OptionsData.Editing:=False;
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmBaoGuanInPut.Free;
  end;
end;

procedure TfrmBaoGuanListSBYS.ToolButton3Click(Sender: TObject);
var
   fPrintFile:string;
   EngMoney,BZZH:string;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\申报要素.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,Case when C5BGUnit=''MTR'' then C4BGQty else Null end as C4BGQtyJS ');
    sql.Add(',Case when C5BGUnit<>''KG'' then  E4JingZ else Null End As E4JingZJS');
    sql.Add(',Case when C5BGUnit<>''KG'' then  ''KGS'' else Null End As JZUnit');
    sql.Add(',Case when C5BGUnit=''KG'' then  ''KGS'' else C5BGUnit End As C5BGUnitJS');
    sql.add(' ,A1ChuKouShangEng=(select Note from KH_Zdy where Type=''A1ChuKouShang'' and ZdyName=A.A1ChuKouShang )');
    sql.add(' ,B6ChuYunGangEng=(select Note from KH_Zdy where Type=''B6ChuYunGang'' and ZdyName=A.B6ChuYunGang )');
    sql.Add(',HTDate=(select ORDDate from JYOrderCon_Main F where F.ConNo=A.A5ConNO)');
    sql.Add(',KHNameEng=(select KHNameEng from ZH_KH_Info KH where KH.KHNameJC=');
    sql.Add('           (select CustomerNoName from JYOrderCon_Main F where F.ConNo=A.A5ConNO ))');
    sql.Add(' from JYOrder_BaoGuan_Main A inner join JYOrder_BaoGuan_Sub B on A.BGId=B.BGId ');
    sql.Add(' where A.BGId='''+Trim(CDS_Main.fieldbyname('BGId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  RMXLSExport1:= TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\申报要素.rmf'),'提示',0);
  end;
end;

end.
