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, Menus;

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

  TfrmZdyAttachmentCB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    Label3: TLabel;
    custNo: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_Main: TDataSource;
    CDS_Main: TClientDataSet;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2DEFStr7: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    Label5: TLabel;
    begdate: TDateTimePicker;
    Label6: TLabel;
    enddate: TDateTimePicker;
    v2Column4: TcxGridDBColumn;
    custName: TEdit;
    Label2: TLabel;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    RMDBDataSet1: TRMDBDataSet;
    v2Column28: TcxGridDBColumn;
    DS_HP: TDataSource;
    CDS_HP: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxTabControl1: TcxTabControl;
    cv2Column3: TcxGridDBColumn;
    cv2Column4: TcxGridDBColumn;
    cv2Column5: TcxGridDBColumn;
    cv2Column6: TcxGridDBColumn;
    cv2Column7: TcxGridDBColumn;
    cv2Column8: TcxGridDBColumn;
    Label1: TLabel;
    P_Code: TEdit;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    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;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    Label12: TLabel;
    Label13: TLabel;
    payment: TEdit;
    ysfs: TEdit;
    v2Column16: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    v2Column18: TcxGridDBColumn;
    v2Column19: TcxGridDBColumn;
    v2Column20: TcxGridDBColumn;
    v2Column21: TcxGridDBColumn;
    v2Column22: TcxGridDBColumn;
    v2Column23: TcxGridDBColumn;
    yxdate: TcxGridDBColumn;
    v2Column25: TcxGridDBColumn;
    v2Column26: TcxGridDBColumn;
    v2Column27: TcxGridDBColumn;
    v2Column29: TcxGridDBColumn;
    v2Column30: TcxGridDBColumn;
    v2Column31: TcxGridDBColumn;
    v2Column24: TcxGridDBColumn;
    v2Column32: TcxGridDBColumn;
    Label14: TLabel;
    BJNO: TEdit;
    Label15: TLabel;
    CheckBox1: TCheckBox;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    btn1: TToolButton;
    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 FormCreate(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure tbCekClick(Sender: TObject);
    procedure tbCekNoClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure Tv2DblClick(Sender: TObject);
  private

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

var
  frmZdyAttachmentCB: TfrmZdyAttachmentCB;

implementation

uses
  U_DataLink, U_Fun10, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmZdyAttachmentCB.InitHistoryPrice(FCode: string);
begin

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select  A.* ');
    if trim(custName.Text) <> '' then
    begin
      SQL.Add(',KKK=(case when custName=' + quotedstr(trim(custName.Text)));
      SQL.Add(' then 0 else 1 end)');
    end;
    SQL.Add('from KH_CP_BJ A');
    sql.Add('where P_Code=' + quotedstr(trim(FCode)));
    sql.Add('and BJXJ= ''报价'' ');
    if (canshu1 <> '管理') and (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;
    if trim(custName.Text) <> '' then
    begin
      SQL.Add('order by KKK,CRTime desc');
    end
    else
    begin
      SQL.Add('order by CRTime desc');
    end;
    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.BJXJ = ''报价''');
      sql.Add(' and A.CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.DateTime) + '''');
      sql.Add(' and A.CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.DateTime + 1) + '''');
      if cxTabControl1.TabIndex < 2 then
      begin
        sql.Add(' and  isnull(A.status,''0'')=''' + inttostr(cxTabControl1.TabIndex) + '''');
      end;
      if trim(canshu1) = '录入' then
      begin
        sql.Add('  and [dbo].[IsByKH](A.custname,' + quotedstr(trim(DNAME)) + ')>0 ');
      end;
//      if trim(DName) <> 'ADMIN' then
//      begin
//        sql.Add('  and  A.custName IN (SELECT OC.CoName FROM  (SELECT X.UserName,C.CoName FROM OperatorAuthority X LEFT JOIN Company C ON C.CoID=X.DataId WHERE X.UserName =' + quotedstr(trim(DName)) + ') OC)');
//      end;
//       showmessage(sql.Text);
      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;
  CDS_Main.Delete;
end;

procedure TfrmZdyAttachmentCB.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption + '1', Tv2, '寄样-报价录入');

  InitGrid();
end;

procedure TfrmZdyAttachmentCB.TBRafreshClick(Sender: TObject);
begin

  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.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('   exec P_Print_XJB  ' + Trim(CDS_Main.fieldbyname('P_Codename').AsString));
//    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) + '''');
    showmessage(SQL.Text);
    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.cxTabControl1Change(Sender: TObject);
begin

  InitGrid();
end;

procedure TfrmZdyAttachmentCB.tbCekClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  try
    with CDS_Main do
    begin
      DisableControls;
      while CDS_Main.Locate('SSel', true, []) do
      begin
        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('审核成功！', '提示信息');
        try
          ADOQueryMain.DisableControls;
          with ADOQueryMain do
          begin
            Close;
            SQL.Clear;
            sql.Add('   exec P_Select_Dept  ' + quotedstr(trim(DName)));
            ExecSQL;
          end;
          InitGrid();
        finally
          ADOQueryMain.EnableControls;
        end;
      end;
      EnableControls;
    end;
    application.MessageBox('审核成功！', '提示信息');

  except
    CDS_Main.EnableControls;
    application.MessageBox('审核失败！', '提示信息', 0);
  end;

//  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('审核成功！', '提示信息');
//    try
//      ADOQueryMain.DisableControls;
//      with ADOQueryMain do
//      begin
//        Close;
//        SQL.Clear;
//        sql.Add('   exec P_Select_Dept  ' + quotedstr(trim(DName)));
//        ExecSQL;
//      end;
////      InitGrid();
//    finally
//      ADOQueryMain.EnableControls;
//    end;
//  except
//    application.MessageBox('审核失败！', '提示信息', 0);
//  end;

end;

procedure TfrmZdyAttachmentCB.tbCekNoClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  try
    with CDS_Main do
    begin
      DisableControls;
      First;
      while CDS_Main.Locate('SSel', true, []) do
      begin
        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;
        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    application.MessageBox('撤销审核成功！', '提示信息');
    InitGrid();
  except
    CDS_Main.EnableControls;
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;

//  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('撤销审核成功！', '提示信息');
//    InitGrid();
//  except
//    application.MessageBox('撤销审核失败！', '提示信息', 0);
//  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 40 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('BJXJ').Value := '报价';
      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;

procedure TfrmZdyAttachmentCB.CheckBox1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, CheckBox1.Checked);
end;

procedure TfrmZdyAttachmentCB.btn1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

procedure TfrmZdyAttachmentCB.Tv2DblClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

end.

