unit U_ZdyAttachSX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  ComObj, Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB,
  ADODB, cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, Menus, cxLookAndFeelPainters, cxButtons, cxContainer, cxImage,
  cxDBEdit, cxLookAndFeels, cxNavigator;

type
  FdDy = record
    inc: integer; //客户端套接字句柄
    FDdys: string[32]; //客户端套接字
    FdDysName: string[32]; //客户端套接字
  end;

  TfrmZdyAttachSX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    DEFstr6: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label1: TLabel;
    ZdyName: TEdit;
    Label2: TLabel;
    OpenDialog1: TOpenDialog;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2DEFstr3: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Pan_image: TPanel;
    Picture4: TcxDBImage;
    DSImage: TDataSource;
    ADOQueryImage: TADOQuery;
    Label5: TLabel;
    Filler: TEdit;
    DEFstr3: TEdit;
    Label9: TLabel;
    DEFstr2: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    Panel2: TPanel;
    Label4: TLabel;
    RadioGroup1: TRadioGroup;
    cxButton1: TcxButton;
    cxButton2: TcxButton;
    v2Ssel: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V2Column1: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    V2Column10: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Label6: TLabel;
    Label7: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CP_Sub: TClientDataSet;
    DS_CP: TDataSource;
    ADOQueryTmp: TADOQuery;
    V1Column1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    Label8: TLabel;
    Label10: TLabel;
    XSCode: TEdit;
    CPXiLie: TEdit;
    Bgroup: TComboBox;
    Label11: TLabel;
    ToolButton1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure DEFstr6Change(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure FormResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton1Click(Sender: TObject);
  private
    fFlileFlag: string;
    procedure InitImage();
    procedure InitSub();
    { Private declarations }
    procedure InitGrid();
  public
    canshu1: string;
    forderType: string;
    dFdDy: array[0..20] of FdDy;
  end;

var
  frmZdyAttachXSC: TfrmZdyAttachSX;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZdyAttInputCP;

{$R *.dfm}
procedure TfrmZdyAttachSX.InitImage();
begin
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    if trim(CDS_HZ.fieldbyname('FOrdATID').AsString) <> '' then
    begin
      sql.Add('where WBID=' + quotedstr(trim(CDS_HZ.fieldbyname('FOrdATID').AsString)));
    end
    else
      sql.Add('where 1<>1');
    //ShowMessage(SQL.Text);
    open;
  end;
end;

procedure TfrmZdyAttachSX.InitGrid();
begin
//  ShowMessage(forderType);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      sql.Add(',RKQty=(select Sum(B.Qty) from CK_BanCP_CR B where B.CRFlag=''入库'' and B.C_Color=A.DEFstr3 ');
      sql.Add(' and (B.C_CodeName=A.ZdyName or B.C_CodeName=A.defNote3) and B.CRType=''检验入库'' and convert(varchar(4),B.CRTime,120)=convert(varchar(4),getdate(),120))');
      sql.Add(' from KH_Zdy_Attachment A ');
      SQL.Add(' where A.Type=''XS'' ');
      if forderType <> null then
      begin
        SQL.Add(' and A.Bgroup=' + quotedstr(forderType));
      end;

//      sql.Add(' and A.FillTIme>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
//      sql.Add(' and A.FillTIme<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''');
      sql.Add(' order by zdyName,dbo.getNum(Defstr3) ');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmZdyAttachSX.InitSub();
begin

  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_Zdy_Attachment_Sub ');
    sql.Add('where ATID =' + quotedstr((CDS_HZ.fieldbyname('ATID').AsString)));
    open;
   // ShowMessage(SQL.Text);
  end;
  SCreateCDS20(ADOQueryTmp, CP_Sub);
  SInitCDSData20(ADOQueryTmp, CP_Sub);
end;

procedure TfrmZdyAttachSX.FormDestroy(Sender: TObject);
begin
  frmZdyAttachXSC := nil;
end;

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

procedure TfrmZdyAttachSX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('产品资料13', Tv2, '客户自定义');
  Close;
end;

procedure TfrmZdyAttachSX.FormShow(Sender: TObject);
begin
  ReadCxGrid('产品资料13', Tv2, '客户自定义');
  v2Column6.Visible := false;
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 90;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  if canshu1 = '管理' then
  begin
    v2Column6.Visible := true;
  end;
  fFlileFlag := UserDataFlag + 'YP';
  //InitGrid();
end;

procedure TfrmZdyAttachSX.TBRafreshClick(Sender: TObject);
begin
  Panel1.SetFocus;
  InitGrid();
end;

procedure TfrmZdyAttachSX.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmZdyAttachSX.DEFstr6Change(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmZdyAttachSX.cxButton2Click(Sender: TObject);
begin
  Panel2.Visible := false;
end;

procedure TfrmZdyAttachSX.cxButton1Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if CDS_HZ.IsEmpty then
    exit;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(CDS_HZ.fieldbyname('ZDYCODE').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);
    exit;
  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(RadioGroup1.Items[RadioGroup1.ItemIndex]) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
 //   Rm2.DefaultCopies:=strtointdef(trim(ComboBox1.Text),1);
   // RM1.ShowReport;
    RM1.PrintReport;
  end
  else
    Application.MessageBox(PChar('没有找到文件' + fPrintFile), '提示', 0);

end;

procedure TfrmZdyAttachSX.Tv2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  InitImage();
end;

procedure TfrmZdyAttachSX.FormResize(Sender: TObject);
begin
  Pan_image.Left := self.Width - Pan_image.Width - 30;
  Pan_image.Top := self.Height - Pan_image.Height - 80;
end;

procedure TfrmZdyAttachSX.FormCreate(Sender: TObject);
begin
  canshu1 := Trim(DParameters1);
end;

procedure TfrmZdyAttachSX.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmZdyAttachSX.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, false);
end;

procedure TfrmZdyAttachSX.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
end;

procedure TfrmZdyAttachSX.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult := 1;
end;

procedure TfrmZdyAttachSX.ToolButton1Click(Sender: TObject);
begin
  ModalResult := 1;
end;

end.

