unit U_BGCWEndManage; //22

interface

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

type
  TfrmBGCWEndManage = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBEdit: TToolButton;
    TBPrint: TToolButton;
    ToolButton3: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    BCOrderNo: TEdit;
    ADOQueryTemp: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryPrint: TADOQuery;
    ClientDataSet7: TClientDataSet;
    cxGridPopupMenu7: TcxGridPopupMenu;
    DataSource7: TDataSource;
    DataSource11: TDataSource;
    ClientDataSet11: TClientDataSet;
    DataSource12: TDataSource;
    ClientDataSet12: TClientDataSet;
    cxGridPopupMenu11: TcxGridPopupMenu;
    cxGridPopupMenu12: TcxGridPopupMenu;
    DataSource19: TDataSource;
    ClientDataSet19: TClientDataSet;
    cxGridPopupMenu19: TcxGridPopupMenu;
    Panel6: TPanel;
    cxTabControl1: TcxTabControl;
    ToolButton1: TToolButton;
    cxGrid12: TcxGrid;
    Tv12: TcxGridDBTableView;
    vJCKMXColumn1: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    v12Column1: TcxGridDBColumn;
    v12Column2: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    cxGrid6: TcxGrid;
    Tv19: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v6Column1: TcxGridDBColumn;
    Tv19Column1: TcxGridDBColumn;
    Tv19Column2: TcxGridDBColumn;
    v6Column2: TcxGridDBColumn;
    v6Column3: TcxGridDBColumn;
    v6Column4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v12Column3: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    Tv11: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    v11Column1: TcxGridDBColumn;
    v11Column2: TcxGridDBColumn;
    v11Column3: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    RMDB19: TRMDBDataSet;
    RMDB12: TRMDBDataSet;
    v11Column4: TcxGridDBColumn;
    v11Column5: TcxGridDBColumn;
    v11Column6: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    ClientDataSet1: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    v1Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ToolButton3Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure Tv11FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure BCOrderNoChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
  private
    { Private declarations }
    canshu1,canshu2:string;
    procedure InitGrid();
    procedure InitGridMD();
  public
    { Public declarations }
  end;

var
  frmBGCWEndManage: TfrmBGCWEndManage;

implementation
uses
  U_DataLink,U_RTFun, U_ZDYHelp, U_ZDYHelpSel,U_ModuleNote,
  U_BGZLInput,U_BGJCKMXInput,U_BGJCKMXGYSInput;

{$R *.dfm}

procedure TfrmBGCWEndManage.FormDestroy(Sender: TObject);
begin
  frmBGCWEndManage:=nil;
end;

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

procedure TfrmBGCWEndManage.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='货物进出口报关明细';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmBGCWEndManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('Tv1s',Tv11,'报关管理');
  WriteCxGrid('Tv2s',Tv12,'报关管理');
  WriteCxGrid('Tv19s',Tv19,'报关管理');
  WriteCxGrid('Tv111s',Tv1,'报关管理');
  Close;
end;

procedure TfrmBGCWEndManage.FormShow(Sender: TObject);
begin
  EndDate.DateTime:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.DateTime-30;
  InitGrid();
  ReadCxGrid('Tv1s',Tv11,'报关管理');
  ReadCxGrid('Tv2s',Tv12,'报关管理');
  ReadCxGrid('Tv19s',Tv19,'报关管理');
  ReadCxGrid('Tv111s',Tv1,'报关管理');

end;

procedure TfrmBGCWEndManage.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,OrderNo=A.BCOrderNo, ');
      sql.Add(' LDFiller=(select Top 1 Filler from JYOrder_BG BG where BG.BGID=A.BGID),');
      sql.Add(' LDFillTime=(select Top 1 FillTime from JYOrder_BG BG where BG.BGID=A.BGID),');
      sql.Add(' HaiYunFee=(select Top 1 HaiYunFee from JYOrder_BG BG where BG.BGID=A.BGID),');
      sql.Add(' BaoFee=(select Top 1 BaoFee from JYOrder_BG BG where BG.BGID=A.BGID),');
      sql.Add(' ChengJiaoType=(select Top 1 ChengJiaoType from JYOrder_BG BG where BG.BGID=A.BGID),');
      sql.Add(' FValid=(select Top 1 Valid from JYOrder_BG BG where BG.BGID=A.BGID)');
      sql.Add(' from JYOrder_BG_CK A where 1=1 ');// where Valid=''Y''
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(ChkStatus,'''')=''已送审'' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(ChkStatus,'''')=''审核通过'' ');
        sql.Add(' and OKFlag=0');
      end else
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add(' and isnull(ChkStatus,'''')=''审核通过'' ');
        sql.Add(' and OKFlag=1');
      end;
      if cxTabControl1.TabIndex<>0 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;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet11);
    SInitCDSData20(ADOQueryMain,ClientDataSet11);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBGCWEndManage.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmBGCWEndManage.TBEditClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex=2 then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_CK where BCID='''+Trim(ClientDataSet11.fieldbyname('BCID').AsString)+'''');
    sql.Add(' and OKFlag=1');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已确认不能再次审核！','提示',0);
    Exit;
  end;
  try
    frmBGJCKMXInput:=TfrmBGJCKMXInput.Create(Application);
    with frmBGJCKMXInput do
    begin
      FBGID:=Trim(ClientDataSet11.fieldbyname('BGID').AsString);
      FBCID:=Trim(ClientDataSet11.fieldbyname('BCID').AsString);
      FOrderNo:='';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmBGJCKMXInput.Free;
  end;
end;

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

procedure TfrmBGCWEndManage.InitGridMD();
var
  fsj:string;
begin
  
end;
procedure TfrmBGCWEndManage.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
end;

procedure TfrmBGCWEndManage.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmBGCWEndManage.Tv11FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  //if ClientDataSet11.IsEmpty  then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_KPFee where BCID='''+Trim(ClientDataSet11.fieldbyname('BCID').AsString)+'''');
    open;
  end;
  SCreateCDS20(ADOQueryCmd,ClientDataSet12);
  SInitCDSData20(ADOQueryCmd,ClientDataSet12);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_CKName_Qty where BCID='''+Trim(ClientDataSet11.fieldbyname('BCID').AsString)+'''');
    open;
  end;
  SCreateCDS20(ADOQueryCmd,ClientDataSet19);
  SInitCDSData20(ADOQueryCmd,ClientDataSet19);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG where BGID='''+Trim(ClientDataSet11.fieldbyname('BGID').AsString)+'''');
    open;
  end;
  SCreateCDS20(ADOQueryCmd,ClientDataSet1);
  SInitCDSData20(ADOQueryCmd,ClientDataSet1);
end;

procedure TfrmBGCWEndManage.BCOrderNoChange(Sender: TObject);
begin
  
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,ClientDataSet11);
  SInitCDSData20(ADOQueryMain,ClientDataSet11);
end;

procedure TfrmBGCWEndManage.ToolButton1Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if ClientDataSet11.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_CK where BCID='''+Trim(ClientDataSet11.fieldbyname('BCID').AsString)+'''');
    sql.Add(' and OKFlag=1');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已确认不能撤销审核！','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate JYOrder_BG_CK Set ChkStatus=''已送审'',ChkTime=Null,Chker=Null ');
    sql.Add(' where BCID='''+Trim(ClientDataSet11.fieldbyname('BCID').AsString)+'''');
    ExecSQL;
  end;
  ClientDataSet11.Delete;
end;

procedure TfrmBGCWEndManage.TBPrintClick(Sender: TObject);
var
   fPrintFile:string;
   FMoney,FMQty,FKgQty,FBCID,FPrice,FMPrice,FKgPrice,FHZPrice:String;
begin
  FBCID:=Trim(ClientDataSet11.fieldbyname('BCID').AsString);
  FMoney:=Tv12.DataController.Summary.FooterSummaryTexts[0];
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(BFQty),0) KgQty from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+'''');
    sql.Add(' and PSXH=(select Max(PSXH) from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+''')');
    open;
  end;
  FKgQty:=Trim(ADOQueryTemp.fieldbyname('KgQty').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(BFMQty),0) MQty from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+'''');
    sql.Add(' and PSXH=(select Max(PSXH) from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+''')');
    open;
  end;
  FMQty:=Trim(ADOQueryTemp.fieldbyname('MQty').AsString);
  if StrToFloat(FMQty)>0 then
  begin
    FMPrice:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FMQty)))+'/M';
  end;
  if StrToFloat(FKgQty)>0 then
  begin
    FKgPrice:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FKgQty)))+'/Kg';
  end;
  if Trim(FMPrice)='' then
  begin
    FHZPrice:=FKgPrice;
  end else
  begin
    FHZPrice:=FMPrice;
    if Trim(FKgPrice)<>'' then
    begin
      FHZPrice:=FHZPrice+#13+FKgPrice;
    end;
  end;
  if Trim(ClientDataSet12.fieldbyname('BFUnit').AsString)='M' then
  begin
    if Trim(FMQty)<>'' then
    begin
      if StrToFloat(FMQty)>0 then
      begin
        FPrice:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FMQty)));
      end;
    end;
  end else
  begin
    if Trim(FKgQty)<>'' then
    begin
      if StrToFloat(FKgQty)>0 then
      begin
        FPrice:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FKgQty)));
      end;
    end;
  end;

  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\货物进出口报关明细表.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['BCOrderNo']:=ClientDataSet11.fieldbyname('BCOrderNo').Value;
    RMVariables['BGDate']:=ClientDataSet11.fieldbyname('BGDate').Value;
    RMVariables['CBMoney']:=Tv12.DataController.Summary.FooterSummaryValues[0];
    RMVariables['FMQty']:=FMQty;
    RMVariables['FKgQty']:=FKgQty;
    RMVariables['FPrice']:=FPrice;
    RMVariables['FMPrice']:=FMPrice;
    RMVariables['FKgPrice']:=FKgPrice;
    RMVariables['FHZPrice']:=FHZPrice;
    RMVariables['HaiYunFee']:=ClientDataSet1.fieldbyname('HaiYunFee').Value;
    RMVariables['ComTaiTou']:=ClientDataSet1.fieldbyname('ComTaiTou').Value;
    RMVariables['BaoFee']:=ClientDataSet1.fieldbyname('BaoFee').Value;
    RMVariables['YunDiG']:=ClientDataSet1.fieldbyname('YunDiG').Value;
    RMVariables['ChuKouKA']:=ClientDataSet1.fieldbyname('ChuKouKA').Value;
    RMVariables['HuoYuanPlace']:=ClientDataSet1.fieldbyname('HuoYuanPlace').Value;
    RMVariables['ChengJiaoType']:=ClientDataSet1.fieldbyname('ChengJiaoType').Value;
    RMVariables['LDY']:=ClientDataSet1.fieldbyname('Filler').Value;

    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\货物进出口报关明细表.rmf'),'提示',0);
  end;
 // Panel4.Visible:=True;
end;

end.
