unit U_ZdyAttachmentBJ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  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, cxDBLookupComboBox, RM_e_Xls, RM_E_llPDF, cxPC, ComObj;

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

  TfrmZdyAttachmentCB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    Label3: TLabel;
    custNo: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_Main: TDataSource;
    CDS_Main: TClientDataSet;
    tbADD: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    tbEdit: TToolButton;
    v2DEFStr7: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    Label5: TLabel;
    begdate: TDateTimePicker;
    Label6: TLabel;
    enddate: TDateTimePicker;
    v2Column4: TcxGridDBColumn;
    custName: TEdit;
    Label2: TLabel;
    ToolButton5: TToolButton;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMllPDFExport1: TRMllPDFExport;
    RMXLSExport1: TRMXLSExport;
    RMDBDataSet1: TRMDBDataSet;
    v2Column28: TcxGridDBColumn;
    DS_HP: TDataSource;
    CDS_HP: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    tbCek: TToolButton;
    tbCekNo: TToolButton;
    cv2Column3: TcxGridDBColumn;
    cv2Column4: TcxGridDBColumn;
    cv2Column5: TcxGridDBColumn;
    cv2Column6: TcxGridDBColumn;
    cv2Column7: TcxGridDBColumn;
    cv2Column8: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Edit1: TEdit;
    ComboBox1: TComboBox;
    Label1: TLabel;
    P_Code: TEdit;
    ToolButton4: TToolButton;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    ToolButton6: TToolButton;
    OpenDialog1: TOpenDialog;
    v2Column11: TcxGridDBColumn;
    Label4: TLabel;
    P_Codename: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    P_MF: TEdit;
    P_KZ: TEdit;
    P_CF: TEdit;
    Label10: TLabel;
    P_SPEC: TEdit;
    Label11: TLabel;
    P_COLOR: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure custNoChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure tbEditClick(Sender: TObject);
    procedure tbADDClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure cxTabControl1Change(Sender: TObject);
    procedure tbCekClick(Sender: TObject);
    procedure tbCekNoClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private

    { Private declarations }
    procedure InitHistoryPrice(FCode: string);
    procedure InitGrid();
    procedure SetStatus();
  public
    dFdDy: array[0..30] of FdDy; //客户端连接数组
    fkhType: string;
    Fmanage: string;
    canshu1: string;
  end;

implementation

uses
  U_DataLink, U_Fun10, U_Fun, U_ZDYHelp, U_ZdyAttachmentBJ_Edit;

{$R *.dfm}
procedure TfrmZdyAttachmentCB.SetStatus();
begin
  tbADD.Visible := False;
  tbEdit.Visible := False;
  tbCek.Visible := False;
  tbCekNo.Visible := False;
  TBDel.Visible := False;
  case cxTabControl1.TabIndex of
    0:
      begin
        if Trim(CanShu1) = '管理' then
        begin
          tbADD.Visible := True;
          tbEdit.Visible := True;
          tbCek.Visible := True;
          TBDel.Visible := True;
        end;
        if Trim(CanShu1) = '录入' then
        begin
          tbADD.Visible := True;
          tbEdit.Visible := True;
          TBDel.Visible := True;
        end;
      end;
    1:
      begin
        if Trim(CanShu1) = '管理' then
        begin
          tbCekNo.Visible := True;
        end;

      end;
    2:
      begin

      end;
  end;
end;

procedure TfrmZdyAttachmentCB.InitHistoryPrice(FCode: string);
begin

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select top 5 A.* from KH_CP_BJ A');
    sql.Add('where P_Code=' + quotedstr(trim(FCode)));
    if canshu1 <> '管理' then
    begin
      sql.Add(' and  custName  in (select Y.CoName from TbForBatch X inner join Company Y on X.Value1=Y.Dept');
      sql.Add('where X.MainType=''部门查询'' and X.DName=' + quotedstr(trim(DName)) + ') ');
    end;
    sql.Add('order by CRTime desc ');
    open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_HP);
  SInitCDSData20(ADOQueryTemp, CDS_HP);
end;

procedure TfrmZdyAttachmentCB.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add('   exec P_Select_Dept  ' + quotedstr(trim(DName)));
      ExecSQL;
    end;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      SQL.Clear;
      sql.Add('select A.* from KH_CP_BJ A');
      sql.Add(' where A.CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.DateTime) + '''');
      sql.Add(' and A.CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.DateTime + 1) + '''');
      if cxTabControl1.TabIndex < 3 then
      begin
        sql.Add(' and  isnull(A.status,''0'')=''' + inttostr(cxTabControl1.TabIndex) + '''');
      end;
      if trim(canshu1) = '录入' then
      begin
        sql.Add(' and  custName  in (select Y.CoName from TbForBatch X inner join Company Y on X.Value1=Y.Dept');
        sql.Add('where X.MainType=''部门查询'' and X.DName=' + quotedstr(trim(DName)) + ') ');
      end;

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

procedure TfrmZdyAttachmentCB.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + '1', Tv2, '报价录入');
  Close;
end;

procedure TfrmZdyAttachmentCB.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete KH_CP_BJ where BJNO=''' + Trim(CDS_Main.fieldbyname('BJNO').AsString) + '''');
    ExecSQL;
  end;
  initgrid();
end;

procedure TfrmZdyAttachmentCB.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption + '1', Tv2, '报价录入');
  SetStatus();
  InitGrid();
end;

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

procedure TfrmZdyAttachmentCB.ToolButton2Click(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 TfrmZdyAttachmentCB.custNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmZdyAttachmentCB.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid2);
 // SelExportData(Tv2,ADOQueryMain,'客户资料列表');
end;

procedure TfrmZdyAttachmentCB.tbEditClick(Sender: TObject);
begin
  if CDS_main.IsEmpty then
    exit;
  try
    frmZdyAttachmentBJ_Edit := TfrmZdyAttachmentBJ_Edit.Create(Application);
    with frmZdyAttachmentBJ_Edit do
    begin
      fkeyNo := self.cds_main.fieldbyname('BJNO').asstring;
      fType := 1;
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttachmentBJ_Edit.Free;
  end;
end;

procedure TfrmZdyAttachmentCB.tbADDClick(Sender: TObject);
begin
  try
    frmZdyAttachmentBJ_Edit := TfrmZdyAttachmentBJ_Edit.Create(Application);
    with frmZdyAttachmentBJ_Edit do
    begin
      fkeyNO := '';
      fType := 0;
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttachmentBJ_Edit.Free;
  end;
end;

procedure TfrmZdyAttachmentCB.FormCreate(Sender: TObject);
begin
  enddate.Date := date();
  begdate.Date := strtodate(formatdateTime('yyyy-MM', enddate.Date) + '-01');
end;

procedure TfrmZdyAttachmentCB.ToolButton5Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno: string;
  i, j: Integer;
begin
  if CDS_Main.IsEmpty then
    Exit;
  ExportFtErpFile('报价信息表.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\报价信息表.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,  ');
    sql.Add('PrtImage=(select Top 1 FilesOther from TP_File X where X.WBID=A.P_Code) ');
    sql.Add(' from KH_CP_BJ A  ');
    sql.Add(' Where A.BJNO=''' + Trim(CDS_Main.fieldbyname('BJNO').AsString) + '''');
    Open;
  end;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.DefaultCopies := 1;
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + trim(fPrintFile)), '提示', 0);
    exit;
  end;

end;

procedure TfrmZdyAttachmentCB.Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitHistoryPrice(trim(CDS_Main.fieldbyname('P_Code').AsString));
end;

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

procedure TfrmZdyAttachmentCB.tbCekClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  KH_CP_BJ SET status=''1'',Chktime=getdate(),Chker= ' + quotedstr(trim(DName)));
      sql.Add('where BJNO=' + quotedstr(trim(CDS_Main.fieldbyname('BJNO').AsString)));
      execsql;
    end;
    application.MessageBox('审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmZdyAttachmentCB.tbCekNoClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  KH_CP_BJ SET status=''0'',Chktime=null ,	Chker=null  ');
      sql.Add('where BJNO=' + quotedstr(trim(CDS_Main.fieldbyname('BJNO').AsString)));
      execsql;
    end;
    application.MessageBox('撤销审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmZdyAttachmentCB.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin

  if CDS_Main.IsEmpty then
    Exit;

  if trim(ComboBox1.Text) = '' then
  begin
    Application.MessageBox('样品标签不能为空!', '提示', 0);
    exit;
  end;

  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(ComboBox1.Text) + '.rmf';

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select *  from CP_YDang where CYNo=''' + Trim(CDS_Main.fieldbyname('P_Code').AsString) + '''');
    sql.Add('and  isnull(CYColor,'''')=isnull(' + quotedstr(Trim(CDS_Main.fieldbyname('P_COLOR').AsString)) + ','''')');
    Open;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('CYID').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;
  RMVariables['QRBARCODE'] := fImagePath;

  if FileExists(fPrintFile) then
  begin
    RM1.ShowPrintDialog := False;
    RM1.LoadFromFile(fPrintFile);
    RM1.DefaultCopies := StrToIntdef(Edit1.Text, 1);
    RM1.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    Exit;
  end;
end;

procedure TfrmZdyAttachmentCB.ToolButton4Click(Sender: TObject);
begin
  if CDS_main.IsEmpty then
    exit;
  try
    frmZdyAttachmentBJ_Edit := TfrmZdyAttachmentBJ_Edit.Create(Application);
    with frmZdyAttachmentBJ_Edit do
    begin
      fkeyNo := self.cds_main.fieldbyname('BJNO').asstring;
      fType := 1;
      canshu1 := self.canshu1;
      Tsave.Visible := False;
      Panel5.Visible := False;
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttachmentBJ_Edit.Free;
  end;
end;

procedure TfrmZdyAttachmentCB.ToolButton6Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, ExcelRowCount: integer;
  maxId: string;
begin

  if GetLSNo(ADOQueryCmd, maxId, 'BJ', 'KH_CP_BJ', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;

  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;

    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    k := 0;
    for i := 1 to 30 do
    begin
      if trim(excelApp.Cells[1, i].value) = '' then
        continue;


      //遍历界面绑定界面中显示的字段和名称
      for j := 0 to Tv2.ColumnCount - 1 do
      begin
        if trim(excelApp.Cells[1, i].value) = trim(Tv2.Columns[j].Caption) then
        begin
          dFdDy[k].inc := i;
          dFdDy[k].FDdys := trim(Tv2.Columns[j].DataBinding.FieldName);
          dFdDy[k].FdDysName := trim(Tv2.Columns[j].Caption);
        end;
      end;

      k := k + 1;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  ADOQueryCmd.Connection.BeginTrans;
  try
    for i := 2 to ExcelRowCount do
    begin

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from KH_CP_BJ where 1=2');
        open;
      end;

      ADOQueryCmd.Append;
      //添加第i条数据
      for j := 1 to k - 1 do
      begin
        if trim(dFdDy[j].FDdys) <> '' then
          ADOQueryCmd.fieldbyname(dFdDy[j].FDdys).Value := excelApp.Cells[i, dFdDy[j].inc].Value;
      end;

      ADOQueryCmd.FieldByName('Filler').Value := Trim(DName);
      ADOQueryCmd.FieldByName('BJNO').Value := Trim(maxId) + inttostr(i);
      ADOQueryCmd.FieldByName('BJID').Value := 1;
      ADOQueryCmd.Post;

    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.CommitTrans;

    InitGrid();
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存数据错误！', '错误信息', MB_ICONERROR);
    exit;
  end;
end;

end.

