unit U_HCList;

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, MovePanel, cxCheckBox,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport;

type
  TfrmHCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    C_CodeName: TEdit;
    C_Color: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    Label5: TLabel;
    conNO: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    Label8: TLabel;
    KZ: TEdit;
    Label9: TLabel;
    MF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    MovePanel2: TMovePanel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label10: TLabel;
    AOrddefstr1: TEdit;
    Label11: TLabel;
    AOrddefstr4: TEdit;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    v1Column17: TcxGridDBColumn;
    Tprint: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    CheckBox1: TCheckBox;
    v1Column18: 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 C_CodeNameChange(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo;
      var ADone: Boolean);
    procedure TprintClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    fckname:string;
    { Public declarations }
  end;

var
  frmHCList: TfrmHCList;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmHCList.FormDestroy(Sender: TObject);
begin
  frmHCList:=nil;
end;

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

procedure TfrmHCList.FormCreate(Sender: TObject);
begin
  cxGrid2.Align:=alClient;
  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmHCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin

      Close;
      sql.Clear;
      Filtered:=False;
      sql.Add('select AA.*,Num_Rate=case when (AOrdQty1=0 OR JY_Qty=0) then 0 else (JY_Qty-AOrdQty1)/AOrdQty1*100.00 end ');
      sql.Add('from( ');
      sql.Add('select A.*,hC_ps=A.AordQty2,JY_ps=(select count(MainID) from WFB_MJJY X where X.APID=A.APID),');
      sql.Add('JY_Qty=(select sum(MJLen) from WFB_MJJY X where X.APID=A.APID), ');
      sql.Add('RK_RollNum=(select sum(RollNum) from CK_BanCP_CR X where X.APID=A.APID and X.CRType=''检验入库''),');
      sql.Add('RK_Qty=(select sum(Qty) from CK_BanCP_CR X where X.APID=A.APID and X.CRType=''检验入库'') ');
      sql.Add('from JYOrder_sub_anPai A   ');
      SQL.Add('WHERE ADefDate1>='''+formatdateTime('yyyy-MM-dd',BegDate.DateTime)+''' ');
      SQL.Add('AND ADefDate1<'''+formatdateTime('yyyy-MM-dd',enddate.DateTime+1)+''' ');
      sql.Add('AND A.CKName='''+trim(fckName)+''' ');
      sql.Add(' ) AA ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmHCList.TBRafreshClick(Sender: TObject);
begin
  //BegDate.SetFocus;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible:=False;
end;

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

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

procedure TfrmHCList.FormShow(Sender: TObject);
begin

  ReadCxGrid(self.Caption,Tv1,'成品仓库');
  
  //InitGrid();
end;

procedure TfrmHCList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel(self.Caption,cxGrid2);
end;

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

procedure TfrmHCList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
begin
 { IF AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('Num_Rate').Index]>10.0 then
  begin
    ACanvas.Brush.Color:=clSkyBlue;
  end
  else IF AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('Num_Rate').Index]<-3.0 then
  begin
    ACanvas.Brush.Color:=clred;
  end;
 }
end;

procedure TfrmHCList.Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo;
  var ADone: Boolean);
var
      FValue:   string;
      FBounds:   TRect;
  begin
      FBounds   :=   AViewInfo.Bounds;
      if   (AViewInfo   is   TcxGridIndicatorRowItemViewInfo)   then
      begin
          ACanvas.FillRect(FBounds);
          ACanvas.DrawComplexFrame(FBounds,   clBtnHighlight,   clBtnShadow,   [bBottom,   bLeft,   bRight],   1);
          FValue   :=IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index+1);
          InflateRect(FBounds,   -1,   -1) ;
          ACanvas.Font.Color   :=   clBlack;
          ACanvas.Brush.Style   :=   bsClear;
          ACanvas.DrawText(FValue,   FBounds,   cxAlignCenter   or   cxAlignTop);
          ADone   :=   True;
      end;
 end;


procedure TfrmHCList.TprintClick(Sender: TObject);
var
  fPrintFile:string;
  Txt,fImagePath:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;

  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\检验指示单标签.rmf' ;
  if not FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检验指示单标签.rmf'),'提示',0);
    Exit;
  end;
  with CDS_Main do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      IF Fieldbyname('ssel').AsBoolean then
      begin

      with ADOQueryPrint do
      begin
        close;
        sql.Clear;
        sql.Add('select A.*               ');
        sql.Add(',CPKW=(select Top 1 zdyCode from KH_Zdy_Attachment X where X.DEFstr1=A.C_CodeName and X.DEFstr2=A.C_Color)');
        sql.Add(',CPNUM=(select count(CRID) from CK_BanCP_KC X where X.C_CodeName=A.C_CodeName and X.C_Color=A.C_Color)');
        sql.Add('from JYOrder_Sub_AnPai A ');
        sql.Add('where Apid='+quotedstr(trim(CDS_Main.fieldbyname('apid').AsString)));
        open;
        if  isempty then
        begin
          CDS_Main.EnableControls;
          application.MessageBox('此回仓单未保存，不能保存','提示信息',0);
          exit;
        end;
        end;
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(ADOQueryPrint.fieldbyname('APID').AsString);
          fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
          if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
          CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
          if FileExists(fImagePath) then DeleteFile(fImagePath);
          Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
        except
          application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
          CDS_Main.EnableControls;
          exit;
        end;

        RMVariables['QRBARCODE']:=fImagePath;
        RM1.LoadFromFile(fPrintFile);
       // RM1.ShowReport;
        RM1.PrintReport;
   {     if CheckBox1.Checked then   RM1.ShowReport
        else
        begin
        //  RM1.DefaultCopies:=strtointdef(trim(edit2.Text),1);
          RM1.PrintReport;
        end;    }
      end;
      next;
    end;
    EnableControls;
    First;
  end;

end;

procedure TfrmHCList.CheckBox1Click(Sender: TObject);
begin
  IF  CDS_Main.IsEmpty then exit;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=checkbox1.Checked;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

end.
