unit U_YarnList;

interface

uses
  Windows, Messages, SysUtils, StrUtils, 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;

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

  TfrmYarnList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    Y_CodeName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    Label1: TLabel;
    Y_Code: TEdit;
    ToolButton4: TToolButton;
    Label2: TLabel;
    DEFstr5: TComboBox;
    OpenDialog1: TOpenDialog;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    Panel2: TPanel;
    RadioGroup1: TRadioGroup;
    Label4: TLabel;
    cxButton1: TcxButton;
    cxButton2: TcxButton;
    v2Column17: TcxGridDBColumn;
    Panel3: TPanel;
    Label5: TLabel;
    edit_Price: TEdit;
    cxButton3: TcxButton;
    cxButton4: TcxButton;
    Label7: TLabel;
    Y_Spec: TEdit;
    procedure FormDestroy(Sender: TObject);
    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 Y_CodeNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure cxButton4Click(Sender: TObject);
    procedure cxButton3Click(Sender: TObject);
    procedure TBCBClick(Sender: TObject);
  private
    fFlileFlag: string;
    { Private declarations }
    procedure InitGrid();
    procedure InitImage();
    procedure LookImage(FileName: string);
  public
    fkhType: string;
    canshu1: string;
    dFdDy: array[0..20] of FdDy; //客户端连接数组
  end;

var
  frmYarnList: TfrmYarnList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_YarnInput;

{$R *.dfm}
procedure TfrmYarnList.LookImage(FileName: string);
begin

end;

procedure TfrmYarnList.InitImage();
begin

end;

procedure TfrmYarnList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select  *');
      sql.Add(' from BS_SX_YDang A where Y_Type=''SX'' ');
      sql.Add(' order by FillTime desc ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmYarnList.FormDestroy(Sender: TObject);
begin
  frmYarnInput := nil;
end;

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

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

procedure TfrmYarnList.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(CDS_HZ.fieldbyname('ATID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete BS_SX_YDang where ATID=''' + Trim(CDS_HZ.fieldbyname('ATID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmYarnList.FormShow(Sender: TObject);
begin
  ReadCxGrid('产品资料13', Tv2, '客户自定义');
  InitGrid();
end;

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

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

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

procedure TfrmYarnList.ToolButton4Click(Sender: TObject);
begin
  try
    frmYarnInput := TfrmYarnInput.Create(Application);
    with frmYarnInput do
    begin
      FCYID := Trim(Self.CDS_HZ.fieldbyname('ATID').AsString);
      fkhType := self.fkhType;
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmYarnInput.Free;
  end;
end;

procedure TfrmYarnList.ToolButton3Click(Sender: TObject);
begin
  try
    frmYarnInput := TfrmYarnInput.Create(Application);
    with frmYarnInput do
    begin
      FCYID := '';
      fkhType := self.fkhType;
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmYarnInput.Free;
  end;

end;

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

  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 20 do
    begin
      if trim(excelApp.Cells[1, i].value) = '' then
        break;
      if trim(excelApp.Cells[1, i].value) = '产品编号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ZdyCode';
        dFdDy[k].FdDysName := '产品编号';
      end;

      if trim(excelApp.Cells[1, i].value) = '产品名称' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ZdyName';
        dFdDy[k].FdDysName := '产品名称';
      end;

      if trim(excelApp.Cells[1, i].value) = '中文名称' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'defNote3';
        dFdDy[k].FdDysName := '中文名称';
      end;

      if trim(excelApp.Cells[1, i].value) = '颜色' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'DEFstr3';
        dFdDy[k].FdDysName := '颜色';
      end;
      if trim(excelApp.Cells[1, i].value) = '中文颜色' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'DEFstr6';
        dFdDy[k].FdDysName := '中文颜色';
      end;

      if trim(excelApp.Cells[1, i].value) = '类型' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'DEFstr5';
        dFdDy[k].FdDysName := '类型';
      end;
      if trim(excelApp.Cells[1, i].value) = '规格' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'DEFstr1';
        dFdDy[k].FdDysName := '规格';
      end;
      if trim(excelApp.Cells[1, i].value) = '成份' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'DEFstr2';
        dFdDy[k].FdDysName := '成份';
      end;
      k := k + 1;
    end;
  except
  end;

  ADOQueryCmd.Connection.BeginTrans;
  try
    for i := 2 to ExcelRowCount do
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'AT', 'KH_Zdy_Attachment', 4, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from KH_Zdy_Attachment where 1=2');
        open;
      end;
      ADOQueryCmd.Append;
      for j := 0 to k - 1 do
      begin
        ADOQueryCmd.fieldbyname(dFdDy[j].FDdys).Value := excelApp.Cells[i, dFdDy[j].inc].Value;
      end;
      ADOQueryCmd.FieldByName('ATID').Value := Trim(maxId);
      ADOQueryCmd.FieldByName('zdyCode').Value := Trim(maxId);
      ADOQueryCmd.FieldByName('khType').Value := Trim(fkhType);
      ADOQueryCmd.FieldByName('Type').Value := 'CP';
      ADOQueryCmd.FieldByName('Filler').Value := Trim(DName);
      ADOQueryCmd.Post;


 {     with ADOQueryTemp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from KH_Zdy_Attachment where ATID='''+trim(maxId)+''' ');
        open;
      end;

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from KH_Zdy where ZdyNo='''+Trim(maxId)+'''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        if ADOQueryCmd.IsEmpty then
        begin
          Append;
        end else
        begin
          Edit;
        end;
        FieldByName('ZdyNo').Value:=Trim(maxId);
        FieldByName('ZdyName').Value:=Trim(ADOQueryTemp.fieldbyname('ZdyName').AsString);
        FieldByName('Type').Value:='CP';
        FieldByName('MainType').Value:=Trim(ADOQueryTemp.fieldbyname('DEFstr5').AsString);
        Post;
      end;}
    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;
  end;
end;

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

procedure TfrmYarnList.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 TfrmYarnList.ToolButton6Click(Sender: TObject);
begin
  Panel2.Left := (self.Width - Panel2.Width) div 2;
  Panel2.Top := (self.Height - Panel2.Height) div 2 - 100;
  Panel2.Visible := true;
end;

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

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

procedure TfrmYarnList.cxButton4Click(Sender: TObject);
begin
  Panel3.Visible := false;
end;

procedure TfrmYarnList.cxButton3Click(Sender: TObject);
begin
  CDS_HZ.DisableControls;
  try
    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        if CDS_HZ.FieldByName('SSel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('update KH_Zdy_Attachment SET DEFFlt2=''' + floattostr(strtofloatdef(trim(edit_Price.Text), 0)) + ''' ');
            SQL.Add('where ATID=''' + Trim(CDS_HZ.fieldbyname('ATID').AsString) + '''');
            execsql;
          end;
        end;
        Next;
      end;
    end;
    CDS_HZ.EnableControls;
    Panel3.Visible := false;
    initGrid();
  except
    CDS_HZ.EnableControls;
    application.MessageBox('保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmYarnList.TBCBClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if CDS_HZ.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  edit_Price.Text := '';
  Panel3.Left := (self.Width - Panel3.Width) div 2;
  Panel3.Top := (self.Height - Panel3.Height) div 2 - 100;
  Panel3.Visible := true;
end;

end.

