unit U_YFGLList;

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, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, MovePanel, cxCalc, Registry, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter;

type
  TfrmYFGLList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ClientDataSet1: TClientDataSet;
    Label6: TLabel;
    A4FPNO: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    Label5: TLabel;
    huodai: TEdit;
    RMXLSExport1: TRMXLSExport;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    Label2: TLabel;
    YWY: TEdit;
    cxGrid2: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    tv2Column1: TcxGridDBColumn;
    Label4: TLabel;
    orderno: TEdit;
    ComboBox1: TComboBox;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    TV1ZUSD: TcxGridDBColumn;
    TV1ZRMB: TcxGridDBColumn;
    TV1Column15: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Label1: TLabel;
    HUODAIFPNO: TEdit;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    RM2: TRMGridReport;
    RMDB_2: TRMDBDataSet;
    TV1Column17: TcxGridDBColumn;
    ToolButton8: 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 huodaiKeyPress(Sender: TObject; var Key: Char);
    procedure KHNameKeyPress(Sender: TObject; var Key: Char);
    procedure TBCopyClick(Sender: TObject);
    procedure TBBGZLClick(Sender: TObject);
    procedure TBHTClick(Sender: TObject);
    procedure TBFPClick(Sender: TObject);
    procedure TBZXDClick(Sender: TObject);
    procedure TBBGDClick(Sender: TObject);
    procedure TBViewClick(Sender: TObject);
    procedure TBSBYSClick(Sender: TObject);
    procedure TBAllClick(Sender: TObject);
    procedure YWYChange(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure huodaiBtnClick(Sender: TObject);
    procedure B7DaoHuoGangBtnClick(Sender: TObject);
    procedure BtnEditA1BtnClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton6Click(Sender: TObject);
    procedure ordernoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
  private
    canshu2: string;
    FDate: TDateTime;
    procedure InitGrid();
    function DelData(): Boolean;
    procedure InitSubGrid();
    procedure InitGridSql(var fsj: string);
    function GetShellFolders(strDir: string): string;
    procedure InitPrtData();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

//var
  //frmBaoGuanList: TfrmBaoGuanList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_BaoGuanInPut, U_Fun, U_YFGLInPut,
  U_LabelPrintFun;

{$R *.dfm}
function TfrmYFGLList.GetShellFolders(strDir: string): string;
const
  regPath = '\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders';
var
  Reg: TRegistry;
  strFolders: string;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey(regPath, false) then
    begin
      strFolders := Reg.ReadString(strDir);
    end;
  finally
    Reg.Free;
  end;
  result := strFolders;
end;

procedure TfrmYFGLList.FormDestroy(Sender: TObject);
begin
  //frmBaoGuanList:=nil;
end;

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

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

procedure TfrmYFGLList.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Pwhere := SGetFilters(Panel1, 1, 2);

  begin
    if trim(Pwhere) <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere);
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select A.* ');
      SQL.Add(',CHUANDATE2=(SELECT CHUANDATE FROM JHD_MAIN E WHERE E.VALID=''Y'' and E.A4FPNO=A.A4FPNO )');
      SQL.Add(',ZUSD=(SELECT SUM(FYMONEY)FROM FP_SUB B WHERE B.FPID=A.FPID AND B.FYBZ=''美元'')');
      sql.Add(',ZRMB=(SELECT SUM(FYMONEY)FROM FP_SUB B WHERE B.FPID=A.FPID AND B.FYBZ=''人民币'')');
      SQL.Add('FROM FP_MAIN A where 1=1 ');
      if ComboBox1.Text = '船期' then
      begin
        sql.Add(' and chuandate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and chuandate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');

      end
      else if combobox1.Text = '制单日期' then
      begin
        sql.Add(' and FILLTIME>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and FILLTIME<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');

      end;
      SQL.Add(fwhere);
//   ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmYFGLList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('报关主表' + self.Caption, Tv1, '运费管理');
//  WriteCxGrid('报关明细' + self.Caption, Tv2, '报关管理8');
  Close;
end;

procedure TfrmYFGLList.FormShow(Sender: TObject);
begin
  ReadCxGrid('报关主表' + self.Caption, Tv1, '运费管理');
//  ReadCxGrid('报关明细' + self.Caption, Tv2, '报关管理8');
//  canshu1 := Trim(DParameters1);
//  ShowMessage(canshu1);
  if canshu1 = '管理' then
  begin
    ToolButton3.Visible := true;
    ToolButton1.Visible := true;
    ToolButton2.Visible := true;
    ToolButton4.Visible := true;
    ToolButton5.Visible := true;
  end;

  InitGrid();
  RM1.CanExport := true;
end;

procedure TfrmYFGLList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
//  TcxGridToExcel(Trim(CDS_Main.fieldbyname('A4FPNO').AsString) + Trim(CDS_Main.fieldbyname('A5ConNO').AsString), cxGrid1);
end;

procedure TfrmYFGLList.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 TfrmYFGLList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmYFGLList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmYFGLList.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 TfrmYFGLList.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 TfrmYFGLList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYFGLList.cxTabControl1Change(Sender: TObject);
begin

  InitGrid();
end;

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

procedure TfrmYFGLList.A4FPNOKeyPress(Sender: TObject; var Key: Char);
var
  fsj: string;
begin

end;

procedure TfrmYFGLList.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;

      sql.Add(fsj);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

procedure TfrmYFGLList.huodaiKeyPress(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 TfrmYFGLList.KHNameKeyPress(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 TfrmYFGLList.TBCopyClick(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);
      CopyStr := '99';
      //TBDel.Visible:=False;
      //TBAdd.Visible:=False;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmBaoGuanInPut.Free;
  end;
end;

procedure TfrmYFGLList.InitPrtData();
begin
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add(' exec P_View_BaoGuanData :BGID ');
    Parameters.ParamByName('BGID').Value := Trim(CDS_Main.fieldbyname('BGId').AsString);
    Open;
  end;
  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
end;

procedure TfrmYFGLList.TBBGZLClick(Sender: TObject);
var
  fPrintFile: string;
  EngMoney, BZZH, FimageFile: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\报关资料.rmf';
  InitPrtData();
  FimageFile := ExtractFilePath(Application.ExeName) + 'Image\' + Trim(CDS_Print.FieldByName('A1ChuKouShang').AsString) + '.jpg';
  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RMVariables['ImageFile'] := trim(FimageFile);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\报关资料.rmf'), '提示', 0);
  end;
end;

procedure TfrmYFGLList.TBHTClick(Sender: TObject);
var
  fPrintFile: string;
  EngMoney, BZZH, FimageFile: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\报关合同.rmf';
  InitPrtData();
  FimageFile := ExtractFilePath(Application.ExeName) + 'Image\' + Trim(CDS_Print.FieldByName('A1ChuKouShang').AsString) + '.jpg';
  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RMVariables['ImageFile'] := trim(FimageFile);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\报关合同.rmf'), '提示', 0);
  end;
end;

procedure TfrmYFGLList.TBFPClick(Sender: TObject);
var
  fPrintFile: string;
  EngMoney, BZZH, FimageFile: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\报关发票.rmf';
  InitPrtData();
  FimageFile := ExtractFilePath(Application.ExeName) + 'Image\' + Trim(CDS_Print.FieldByName('A1ChuKouShang').AsString) + '.jpg';
  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RMVariables['ImageFile'] := trim(FimageFile);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\报关发票.rmf'), '提示', 0);
  end;
end;

procedure TfrmYFGLList.TBZXDClick(Sender: TObject);
var
  fPrintFile: string;
  EngMoney, BZZH, FimageFile: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\报关装箱单.rmf';
  InitPrtData();
  FimageFile := ExtractFilePath(Application.ExeName) + 'Image\' + Trim(CDS_Print.FieldByName('A1ChuKouShang').AsString) + '.jpg';
  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RMVariables['ImageFile'] := trim(FimageFile);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\报关装箱单.rmf'), '提示', 0);
  end;
end;

procedure TfrmYFGLList.TBBGDClick(Sender: TObject);
var
  fPrintFile: string;
  EngMoney, BZZH, FimageFile: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\报关单.rmf';
  InitPrtData();
  FimageFile := ExtractFilePath(Application.ExeName) + 'Image\' + Trim(CDS_Print.FieldByName('A1ChuKouShang').AsString) + '.jpg';
  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RMVariables['ImageFile'] := trim(FimageFile);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\报关单.rmf'), '提示', 0);
  end;
end;

procedure TfrmYFGLList.TBViewClick(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 TfrmYFGLList.TBSBYSClick(Sender: TObject);
var
  fPrintFile: string;
  EngMoney, BZZH, FimageFile: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\申报要素.rmf';
  InitPrtData();
  FimageFile := ExtractFilePath(Application.ExeName) + 'Image\' + Trim(CDS_Print.FieldByName('A1ChuKouShang').AsString) + '.jpg';
  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RMVariables['ImageFile'] := trim(FimageFile);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\申报要素.rmf'), '提示', 0);
  end;
end;

procedure TfrmYFGLList.TBAllClick(Sender: TObject);
var
  fPrintFile: string;
  EngMoney, BZZH, FimageFile, FZMFile: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\全部报关资料.rmf';
  InitPrtData();
  FimageFile := ExtractFilePath(Application.ExeName) + 'Image\' + Trim(CDS_Print.FieldByName('A1ChuKouShang').AsString) + '.jpg';
  //RMXLSExport1:= TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    if RM1.CanExport = true then
    begin
      FZMFile := 'C:\Users\Administrator\Desktop';
      if not DirectoryExists(FZMFile) then
      begin
        FZMFile := 'C:\Documents and Settings\Administrator\桌面\' + trim(CDS_Main.fieldbyname('A4FPNO').AsString) + ' ' + trim(CDS_Main.fieldbyname('A5ConNO').AsString) + '.xls';
      end
      else
      begin
        FZMFile := 'C:\Users\Administrator\Desktop\' + trim(CDS_Main.fieldbyname('A4FPNO').AsString) + ' ' + trim(CDS_Main.fieldbyname('A5ConNO').AsString) + '.XLS';
      end;
      RM1.ExportTo(RMXLSExport1, FZMFile);
    end;
    RM1.CanExport := true;
    RMVariables['ImageFile'] := trim(FimageFile);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
    RM1.CanExport := False;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\全部报关资料.rmf'), '提示', 0);
  end;
end;

procedure TfrmYFGLList.YWYChange(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 TfrmYFGLList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSubGrid();
end;

procedure TfrmYFGLList.huodaiBtnClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYFGLList.B7DaoHuoGangBtnClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYFGLList.BtnEditA1BtnClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmYFGLList.ToolButton1Click(Sender: TObject);
begin
  try
    frmYFGLInPut := TfrmYFGLInPut.Create(Application);
    with frmYFGLInPut do
    begin
      FBCId := trim(CDS_Main.fieldbyname('fpid').AsString);

      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmYFGLInPut.Free;
  end;
end;

procedure TfrmYFGLList.ToolButton2Click(Sender: TObject);
begin
  try

    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete fp_main where fpid=''' + Trim(CDS_Main.fieldbyname('fpid').AsString) + '''');
      sql.Add('delete fp_sub where fpid=''' + Trim(CDS_Main.fieldbyname('fpid').AsString) + '''');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('数据删除成功！', '提示', 0);
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;

    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmYFGLList.ComboBox1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYFGLList.ToolButton3Click(Sender: TObject);
begin
//  if CDS_Main.IsEmpty then
//    Exit;
  //if cxTabControl1.TabIndex<>0 then Exit;
  try
    frmYFGLInPut := TfrmYFGLInPut.Create(Application);
    with frmYFGLInPut do
    begin
      FBCId := '';

      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmYFGLInPut.Free;
  end;
end;

procedure TfrmYFGLList.ToolButton4Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要执行此操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update fp_main Set fkstatus=''已付款''   ');

        SQL.Add(' where fpid=''' + Trim(CDS_Main.fieldbyname('fpid').AsString) + '''');
        ExecSQL;
      end;
//      Order_Main.Delete;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功!', '提示', 0);
    initgrid();
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmYFGLList.ToolButton5Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要执行此操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update fp_main Set fkstatus=''未付款''   ');

        SQL.Add(' where fpid=''' + Trim(CDS_Main.fieldbyname('fpid').AsString) + '''');
        ExecSQL;
      end;
//      Order_Main.Delete;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功!', '提示', 0);
    initgrid();
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmYFGLList.TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if (AViewInfo.GridRecord.Values[TV1Column4.Index] = null) then
  begin
    ACanvas.font.Color := clred;
  end;
end;

procedure TfrmYFGLList.ToolButton6Click(Sender: TObject);
var
  fPrintFile, FTaiTou: string;
  EngMoney, LBName, fImagePath2: string;
begin
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\付款申请单.rmf';
  ExportFtErpFile('付款申请单.rmf', ADOQueryPrint);

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;

  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin

          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_Main.fieldbyname('FPID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;

          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('Update fp_main Set fkstatus=''已付款''   ');

            SQL.Add(' where fpid=''' + Trim(CDS_Main.fieldbyname('fpid').AsString) + '''');
            ExecSQL;
          end;
//      Order_Main.Delete;

        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Print_FKSQD ');

    SQL.ADD('@DCODE=''' + Trim(DCode) + '''');
//     ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  if FileExists(fPrintFile) then
  begin

    RM1.LoadFromFile(fPrintFile);
    Sleep(1000);
    RM1.ShowReport;

  end
  else
  begin
//    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\报关单.rmf'), '提示', 0);
    Application.MessageBox(PChar('没有找' + trim(fPrintFile)), '提示信息', 0);
    Exit;
  end;

end;

procedure TfrmYFGLList.ordernoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid();
end;

procedure TfrmYFGLList.ToolButton7Click(Sender: TObject);
var
  fPrintFile, fImagePath2: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if CDS_Main.IsEmpty then
    Exit;

  LBName := '运费导出单';
  ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryTemp);

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);

    fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label0001.xls';
    if FileExists(fImagePath2) then
      DeleteFile(fImagePath2);
    fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label.xls';
    RM2.PrepareReport;
    RM2.ExportTo(RMXLSExport1, fImagePath2);
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
end;

procedure TfrmYFGLList.ToolButton8Click(Sender: TObject);
var
  MPrintJson, JsonResult, msql, Pwhere: string;
begin

  if CDS_Main.IsEmpty then
    Exit;
  msql := ' select A.*,ZUSD=(SELECT SUM(FYMONEY)FROM FP_SUB B WHERE B.FPID=A.FPID AND B.FYBZ=''美元''),ZRMB=(SELECT SUM(FYMONEY)FROM FP_SUB B WHERE B.FPID=A.FPID AND B.FYBZ=''人民币'') ';
  msql := msql + 'FROM FP_MAIN A where 1=1   ';
  if ComboBox1.Text = '船期' then
  begin
    msql := msql + ' and chuandate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + ''' ';
    msql := msql + ' and chuandate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + ''' ';

  end
  else if combobox1.Text = '制单日期' then
  begin
    msql := msql + ' and FILLTIME>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + ''' ';
    msql := msql + ' and FILLTIME<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + ''' ';

  end;

  Pwhere := SGetFilters(Panel1, 1, 2);

  if trim(Pwhere) <> '' then
    msql := msql + ' and ' + trim(Pwhere);

  msql := msql + 'ORDER BY YWY desc, FILLTIME  ';

  MPrintJson := '{ "LBName": "按业务员导出","QrCodeField": "ssid","DConString": "' + DConString + '","DCode": "' + DCode + '","DName": "' + DName + '","IsPreview": true,"printerIndex": 0,"ExportFileType": "xls", "PrtArgs": [ {"SqlStr": "' + msql + '" }] }';

  FunPrint(Application, PChar(MPrintJson));

end;

end.

