unit U_CKProductBCPOutListHZ;

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, RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System,
  RM_GridReport, cxCheckBox, Menus, MovePanel, RM_E_llPDF, cxCalendar, Math,
  Clipbrd, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, 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
  TfrmCKProductBCPOutListHZ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    MPRTCodeName: TEdit;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    Label10: TLabel;
    CkOrdNo: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    DS_JuanPRT: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column15: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    conNo: TEdit;
    Label15: TLabel;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM3: TRMGridReport;
    v1Column24: TcxGridDBColumn;
    Label22: TLabel;
    KHName: TEdit;
    Label24: TLabel;
    v1Column13: TcxGridDBColumn;
    Label1: TLabel;
    CRType: TComboBox;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ComboBox1: TComboBox;
    CDS_Label: TClientDataSet;
    v1Column14: TcxGridDBColumn;
    Label9: TLabel;
    MPRTCode: TEdit;
    ToolButton2: TToolButton;
    v1Column1: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    Tv1Column1: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure CPTypeChange(Sender: TObject);
    procedure CRTypeChange(Sender: TObject);
    procedure edtSMNOKeyPress(Sender: TObject; var Key: Char);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure PrintMD(MStatus: string);
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmCKProductBCPOutListHZ: TfrmCKProductBCPOutListHZ;

implementation

uses
  U_DataLink, U_Fun, U_SysLogHelp, U_CKProductBCPOutListHZFS;

{$R *.dfm}
procedure TfrmCKProductBCPOutListHZ.PrintMD(MStatus: string);
var
  fImagePath2, fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);

  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;

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * ');
    sql.Add(' from CK_BanCP_CR A  ');
    sql.Add(' where  A.CRFlag=''出库''  ');
    sql.Add('and crflag=''出库'' and CKOrdNo=' + quotedstr(Trim(CDS_Main.fieldbyname('CKOrdNo').AsString)));
    sql.Add('and SQCKCS=' + quotedstr(trim(CDS_Main.fieldbyname('SQCKCS').asstring)));
    open;
  end;
  FMainID := '';

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQuery1 do
    begin
      First;
      while not Eof do
      begin

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

        ADOQuery1.NEXT;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;

  if CDS_Label.Locate('LMName', trim(ComboBox1.Text), []) then
  begin

//    with ADOQueryPrint do
//    begin
//      Close;
//      sql.Clear;
//      sql.add('exec ' + trim(CDS_Label.fieldbyname('LMSql').AsString) + ' @Code=' + quotedstr(Trim(DCode)));
////      ShowMessage(SQL.Text);
//      Open;
//    end;
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec ' + trim(CDS_Label.fieldbyname('LMSql').AsString) + ' @CKOrdNo=' + quotedstr(Trim(CDS_Main.fieldbyname('CKZDH').AsString)));
      Open;
    end;
    SCreateCDS20(ADOQueryPrint, CDS_PRT);
    SInitCDSData20(ADOQueryPrint, CDS_PRT);
    if (ComboBox1.Text = '印花竖码单(米重量)') or (ComboBox1.Text = 'AZ') then
    begin
      with ADOQueryPrint do
      begin
        Close;
        sql.Clear;
        sql.add('exec P_Print_CKMD_DXHZ3 @code=' + quotedstr(Trim(DCode)));
        Open;
      end;
      SCreateCDS20(ADOQueryPrint, CDS_HZ);
      SInitCDSData20(ADOQueryPrint, CDS_HZ);
    end
    else
    begin
      with ADOQueryPrint do
      begin
        Close;
        sql.Clear;
        sql.add('exec P_Print_CKMD_DXHZ @code=' + quotedstr(Trim(DCode)));
        Open;
      end;
      SCreateCDS20(ADOQueryPrint, CDS_HZ);
      SInitCDSData20(ADOQueryPrint, CDS_HZ);
    end;
  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    if MStatus = 'Excel' then
    begin
      RM3.LoadFromFile(fPrintFile);
      fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label0001.xls';
      if FileExists(fImagePath2) then
        DeleteFile(fImagePath2);
      fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label.xls';
      RM3.PrepareReport;
      RM3.ExportTo(RMXLSExport1, fImagePath2);
    end
    else
    begin
      RM3.LoadFromFile(fPrintFile);
      RMVariables['ZDR'] := trim(DName);
      RM3.ShowReport;
    end;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;
end;

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

procedure TfrmCKProductBCPOutListHZ.FormCreate(Sender: TObject);
begin
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 20;
end;

procedure TfrmCKProductBCPOutListHZ.InitGrid();
var
  fwhere, Pwhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  Pwhere := SGetFilters(Panel1, 1, 2);

  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' where ' + trim(Pwhere);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' select AAA.*    ');
      sql.Add(' ,CKZDH=AAA.CKOrdNo+''-''+ISNULL(cast(AAA.SQCKCS as varchar(10)),'''')  ');
      sql.Add(' ,GuiXing=(select GuiXing from FHSQ_Main X where X.fsid=AAA.CKOrdNo)    ');
      sql.Add('    from (  ');
      sql.Add(' select AA.*    ');
      sql.Add(' ,B.orderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ,B.conNo,B.KHconNo,B.lidan,B.ywy ');
      sql.Add(' from (  ');
      sql.Add(' select count(A.MJID) PS,SUM(A.NETKGQty) NETKGQty,SUM(A.KGQty) KGQty, SUM(A.Qty) Qty, SUM(A.MJYLen) MJYLen,A.CKOrdNo,A.SQCKCS,A.MainId    ');
      sql.Add('   ,convert(varchar(10),A.CRTime,120) CRTime,A.CRType,A.CDDanwei,bs=(select  COUNT(DISTINCT BAOID) FROM CK_BanCP_CR D WHERE D.MAINID=A.MAINID');
      SQL.Add('AND A.CKOrdNo=D.CKOrdNo AND convert(varchar(10),A.CRTime,120)=convert(varchar(10),D.CRTime,120)AND D.CRFlag=''出库''');
      SQL.Add('AND A.CRType=D.CRType AND A.SQCKCS=D.SQCKCS AND A.CDDanwei=D.CDDANWEI)');
      sql.Add(' from CK_BanCP_CR A  ');
      sql.Add(' where  A.CRFlag=''出库''  ');
      sql.Add(' and   A.CRTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
      sql.Add(' and  A.CRTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
      sql.Add(' GROUP BY A.CKOrdNo,A.SQCKCS,A.MainId ,A.CDDanwei,convert(varchar(10),A.CRTime,120),A.CRType  ) AA  ');
      sql.Add('   inner join JYOrder_Main B on AA.MainId=B.MainId   ');
      sql.Add('  ) AAA   ');
      sql.Add(fwhere);

      SQL.Add('ORDER BY CRTime DESC');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;

end;

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

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

procedure TfrmCKProductBCPOutListHZ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '成品仓库');
  Close;
end;

procedure TfrmCKProductBCPOutListHZ.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid(trim(self.Caption), Tv1, '成品仓库');
  fsj := 'select distinct(LMName) name from Lbael_Map where LMType=''出库码单汇总''  ';
  SInitComBoxBySql(ADOQueryCmd, ComboBox1, true, fsj);

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    Sql.Add('select * from Lbael_Map where LMType=''出库码单汇总''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Label);
  SInitCDSData20(ADOQueryTemp, CDS_Label);
end;

procedure TfrmCKProductBCPOutListHZ.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  SelExportData(Tv1, ADOQueryMain, '');
end;

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

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

procedure TfrmCKProductBCPOutListHZ.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmCKProductBCPOutListHZ.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  try
    frmCKProductBCPOutListHZFS := TfrmCKProductBCPOutListHZFS.Create(Application);
    with frmCKProductBCPOutListHZFS do
    begin
      FCKOrdNo := Self.CDS_Main.fieldbyname('CKOrdNo').AsString;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmCKProductBCPOutListHZFS.Free;
  end;
end;

procedure TfrmCKProductBCPOutListHZ.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmCKProductBCPOutListHZ.CPTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCKProductBCPOutListHZ.CRTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCKProductBCPOutListHZ.edtSMNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

  end;
end;

procedure TfrmCKProductBCPOutListHZ.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPOutListHZ := nil;
end;

procedure TfrmCKProductBCPOutListHZ.ToolButton1Click(Sender: TObject);
begin
  PrintMD('Report');
end;

procedure TfrmCKProductBCPOutListHZ.ToolButton2Click(Sender: TObject);
begin
  PrintMD('Excel');
end;

end.

