unit U_XYZMAKELIST;

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, Registry;

type
  TfrmXYZMAKEList = 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;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    ClientDataSet1: TClientDataSet;
    Label6: TLabel;
    XKZH: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    Label5: TLabel;
    XORDERNO: TEdit;
    TBCopy: TToolButton;
    RMXLSExport1: TRMXLSExport;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    TBView: TToolButton;
    Label2: TLabel;
    XKHNAME: TEdit;
    cxGrid2: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    tv2Column1: TcxGridDBColumn;
    tv2Column2: TcxGridDBColumn;
    tv2Column3: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    Label4: TLabel;
    XYNO: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    XTBYQ: TcxGridDBColumn;
    XXYZZT: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    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 TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure CRTypeChange(Sender: TObject);
   
    procedure XORDERNOKeyPress(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 TBSSClick(Sender: TObject);
    procedure TBSSCXClick(Sender: TObject);
    procedure TBHDClick(Sender: TObject);
    procedure TBHDCXClick(Sender: TObject);
    procedure TBSHClick(Sender: TObject);
    procedure TBSHCXClick(Sender: TObject);
    procedure TBAllClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure huodaiBtnClick(Sender: TObject);
    procedure B7DaoHuoGangBtnClick(Sender: TObject);
    procedure BtnEditA1BtnClick(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ZordernoChange(Sender: TObject);
    procedure ZordernoKeyPress(Sender: TObject; var Key: Char);


//    procedure img1Click(Sender: TObject);
  private
    canshu2: string;
    FDate: TDateTime;
    procedure InitGrid();
    function DelData(): Boolean;
    function GetShellFolders(strDir: string): string;
    procedure InitGridSql(var fsj: string);
    procedure InitPrtData();
    { Private declarations }
  public
    canshu1, canshu3: string;
    FDY: string;
    { Public declarations }
  end;

//var
  //frmBaoGuanList: TfrmBaoGuanList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_Fun, U_XYZInPut;

{$R *.dfm}

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

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

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

procedure TfrmXYZMAKEList.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 * from (');
      sql.Add('select A.* ');
//      SQL.Add(',ZORDERNO=cast((select B.ORDERNO+'';'' from JYOrder_BaoGuan_SUB B where B.BGID=A.BGID  AND SVALID=''Y'' for xml path('''')) as varchar(200))');
//      SQL.Add(',ZGCNAME=cast((select B.GCNAME+'';'' from JYOrder_BaoGuan_SUB B where B.BGID=A.BGID  AND SVALID=''Y'' for xml path('''')) as varchar(200))');
//      SQL.Add(',ZC3BGNameEng=cast((select B.C3BGNameEng+'';'' from JYOrder_BaoGuan_SUB B where B.BGID=A.BGID AND SVALID=''Y'' for xml path('''')) as varchar(200))');
//      SQL.Add(',ZBGCODENAME=cast((select B.BGCODENAME+'';'' from JYOrder_BaoGuan_SUB B where B.BGID=A.BGID AND SVALID=''Y'' for xml path('''')) as varchar(200))');

      sql.Add(' from XYZ_MAIN A   ');
      sql.Add(' where A.Valid=''Y''  ');
//      sql.Add(fwhere);


      sql.Add(')AA WHERE 1=1');
      sql.Add(fwhere);

      sql.Add(' and XSDRQ>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
      sql.Add(' and XSDRQ<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      SQL.Add('ORDER BY XSDRQ DESC');
      
//     ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmXYZMAKEList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('报关主表' + self.Caption, Tv1, '报关管理7');

  Close;
end;

procedure TfrmXYZMAKEList.FormShow(Sender: TObject);
begin
  ReadCxGrid('报关主表' + self.Caption, Tv1, '报关管理7');

  canshu1 := Trim(DParameters1);
//  ShowMessage(canshu1);

  InitGrid();

end;

procedure TfrmXYZMAKEList.TBExportClick(Sender: TObject);
var
  fPrintFile, FTaiTou: string;
  EngMoney, LBName, fImagePath2: string;
begin
  if ADOQueryMain.IsEmpty then
    exit;
//  SelExportData(Tv1, ADOQueryMain, '报关资料列表');
//  TcxGridToExcel(Trim(CDS_Main.fieldbyname('A4FPNO').AsString) + Trim(CDS_Main.fieldbyname('A5ConNO').AsString), cxGrid1);

  FDY := '1';

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\报关资料列表.rmf';
  ExportFtErpFile('报关资料列表.rmf', ADOQueryPrint);

  with ADOQueryPrint do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' exec P_Print_DCBGD @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))) + ',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));

    Open;
  end;
  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  if FileExists(fPrintFile) then
  begin
    if FDY = '0' then
    begin
      RM1.LoadFromFile(fPrintFile);
      Sleep(1000);
      RM1.ShowReport;
    end
    else if FDY = '1' then
    begin
      RM1.LoadFromFile(fPrintFile);
      fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label0001.xls';
      if FileExists(fImagePath2) then
        DeleteFile(fImagePath2);                                  //label.xls
//      fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\报关资料列表.xls';
      fImagePath2 := GetShellFolders('Desktop') + '\报关资料列表.xls';
      RM1.PrepareReport;
      Sleep(1000);
      RM1.ExportTo(RMXLSExport1, fImagePath2);
    end;

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

end;

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

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

procedure TfrmXYZMAKEList.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    InitGrid();
  end;
end;

function TfrmXYZMAKEList.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 XYZ_MAIN Set Valid=''N''');
          sql.Add(',Editer=''' + Trim(DName) + ''',EditTime=getdate()');
          sql.Add(' where XYId=''' + Trim(CDS_Main.fieldbyname('XYId').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 TfrmXYZMAKEList.TBAddClick(Sender: TObject);
begin
  try
    frmXYZInPut := TfrmXYZInPut.Create(Application);
    with frmXYZInPut do
    begin
      FBCId := '';
      canshu3 := self.canshu3;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmXYZInPut.Free;
  end;
end;

procedure TfrmXYZMAKEList.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  try
    frmXYZInPut := TfrmXYZInPut.Create(Application);
    with frmXYZInPut do
    begin
      FBCId := Trim(CDS_Main.fieldbyname('XYID').AsString);

      canshu3 := self.canshu3;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmXYZInPut.Free;
  end;
end;

procedure TfrmXYZMAKEList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmXYZMAKEList.cxTabControl1Change(Sender: TObject);
begin

  InitGrid();
end;

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



procedure TfrmXYZMAKEList.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 TfrmXYZMAKEList.XORDERNOKeyPress(Sender: TObject; var Key: Char);
var
  fsj: string;
begin
  if Key = #13 then
  begin
    InitGrid();

  end;
end;

procedure TfrmXYZMAKEList.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 TfrmXYZMAKEList.TBCopyClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
 // if cxTabControl1.TabIndex<>0 then Exit;
  try
    frmXYZInPut := TfrmXYZInPut.Create(Application);
    with frmXYZInPut do
    begin
      FBCId := Trim(CDS_Main.fieldbyname('XYID').AsString);
      CopyStr := '99';
      //TBDel.Visible:=False;
      //TBAdd.Visible:=False;
      canshu3 := self.canshu3;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmXYZInPut.Free;
  end;
end;

procedure TfrmXYZMAKEList.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 TfrmXYZMAKEList.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 TfrmXYZMAKEList.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 TfrmXYZMAKEList.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 TfrmXYZMAKEList.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 TfrmXYZMAKEList.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 TfrmXYZMAKEList.TBViewClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  //if cxTabControl1.TabIndex<>0 then Exit;
  try
    frmXYZInPut := TfrmXYZInPut.Create(Application);
    with frmXYZInPut do
    begin
      FBCId := Trim(CDS_Main.fieldbyname('XYID').AsString);
      TBSave.Visible := False;

      canshu3 := self.canshu3;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmXYZInPut.Free;
  end;
end;

procedure TfrmXYZMAKEList.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 TfrmXYZMAKEList.TBSSClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行送审操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_BaoGuan_Main Set Status=''已送审'',SStime=getdate(),SChker=''' + Trim(DName) + '''');
          sql.Add(' where BGID=''' + Trim(CDS_Main.fieldbyname('BGID').AsString) + '''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmXYZMAKEList.TBSSCXClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_BaoGuan_Main ');
          sql.Add(' where BGID=''' + Trim(CDS_Main.fieldbyname('BGID').AsString) + '''');
          Open;
        end;
        if Trim(ADOQueryTemp.fieldbyname('OKPerson').AsString) <> '' then
        begin
          CDS_Main.EnableControls;
          Application.MessageBox('已核对不能撤销!', '提示', 0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
  if Application.MessageBox('确定要执行送审撤销操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_BaoGuan_Main Set Status=''待送审'',SStime=Null,SChker=Null');
          sql.Add(' where BGID=''' + Trim(CDS_Main.fieldbyname('BGID').AsString) + '''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmXYZMAKEList.TBHDClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行核对操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_BaoGuan_Main Set Status=''已核对'',OKTime=getdate(),OKPerson=''' + Trim(DName) + '''');
          sql.Add(' where BGID=''' + Trim(CDS_Main.fieldbyname('BGID').AsString) + '''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmXYZMAKEList.TBHDCXClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_BaoGuan_Main ');
          sql.Add(' where BGID=''' + Trim(CDS_Main.fieldbyname('BGID').AsString) + '''');
          Open;
        end;
        if Trim(ADOQueryTemp.fieldbyname('Chker').AsString) <> '' then
        begin
          CDS_Main.EnableControls;
          Application.MessageBox('已审核不能撤销!', '提示', 0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
  if Application.MessageBox('确定要执行核对撤销操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_BaoGuan_Main Set Status=''已送审'',OKtime=Null,OKPerson=Null');
          sql.Add(' where BGID=''' + Trim(CDS_Main.fieldbyname('BGID').AsString) + '''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmXYZMAKEList.TBSHClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行审核操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_BaoGuan_Main Set Status=''已审核'',ChkTime=getdate(),Chker=''' + Trim(DName) + '''');
          sql.Add(' where BGID=''' + Trim(CDS_Main.fieldbyname('BGID').AsString) + '''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmXYZMAKEList.TBSHCXClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行审核撤销操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_BaoGuan_Main Set Status=''已送审'',ChkTime=Null,Chker=Null');
          sql.Add(' where BGID=''' + Trim(CDS_Main.fieldbyname('BGID').AsString) + '''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmXYZMAKEList.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 TfrmXYZMAKEList.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  FDY := '0';

end;

procedure TfrmXYZMAKEList.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 TfrmXYZMAKEList.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 TfrmXYZMAKEList.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 TfrmXYZMAKEList.Button7Click(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;

  if canshu3 = '业务员' then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '(业务).rmf';
    ExportFtErpFile(Trim(LBName) + '(业务).rmf', ADOQueryPrint);
  end
  else
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
    ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryPrint);
  end;

  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('bgid').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;
  if LBName = '开票二连打' then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Print_BGD4 ');

      SQL.ADD('@DCODE=''' + Trim(DCode) + '''');
//     ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryPrint, CDS_Print);
    SInitCDSData20(ADOQueryPrint, CDS_Print);
  end
  else
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Print_BGD2 ');

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

  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    if FDY = '0' then
    begin
      RM1.LoadFromFile(fPrintFile);
      Sleep(1000);
      RM1.ShowReport;
    end
    else if FDY = '1' then
    begin
      RM1.LoadFromFile(fPrintFile);
      fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label0001.xls';
      if FileExists(fImagePath2) then
        DeleteFile(fImagePath2);                                  //label.xls
      fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\' + Trim(CDS_Main.fieldbyname('orderno').AsString) + '.xls';
      ShowMessage(fImagePath2);
      RM1.PrepareReport;
      Sleep(1000);
      RM1.ExportTo(RMXLSExport1, fImagePath2);
    end;

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

end;

procedure TfrmXYZMAKEList.ToolButton3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_BaoGuan_Main Set bgStatus=NULL');

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

procedure TfrmXYZMAKEList.ZordernoChange(Sender: TObject);
begin
  TBFind.Click();
end;

procedure TfrmXYZMAKEList.ZordernoKeyPress(Sender: TObject; var Key: Char);
begin
//  2123
end;

end.

