unit U_DyeInspSet;

interface

uses
  Windows, Messages, StrUtils, 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, cxDropDownEdit, BtnEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, cxContainer, dxCore, cxDateUtils, cxImage,
  cxDBEdit, IdExplicitTLSClientServerBase, RM_Preview, cxPC, U_BaseHelp,
  System.ImageList, Vcl.ImgList, RTComboBox, RM_BarCode, RM_e_Xls;

type
  TfrmDyeInspSet = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ToolButton3: TToolButton;
    ScrollBox1: TScrollBox;
    Label14: TLabel;
    Label1: TLabel;
    cxPageControl1: TcxPageControl;
    cxTabSheet4: TcxTabSheet;
    cxTabSheet3: TcxTabSheet;
    RMPreview2: TRMPreview;
    RMPreview1: TRMPreview;
    RuleConversion: TComboBox;
    Label3: TLabel;
    Coefficient: TEdit;
    Label4: TLabel;
    ADOQueryLabel: TADOQuery;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    ADO_1: TADOQuery;
    RM2: TRMGridReport;
    RMDB_2: TRMDBDataSet;
    cxTabSheet1: TcxTabSheet;
    DS_Label: TDataSource;
    CDS_Label: TClientDataSet;
    RMDB_Label: TRMDBDataSet;
    RMLabel: TRMGridReport;
    Panel1: TPanel;
    Panel2: TPanel;
    cxGridLabel: TcxGrid;
    TVLabel: TcxGridDBTableView;
    TVLabellabelId: TcxGridDBColumn;
    TVLabellabeltype: TcxGridDBColumn;
    TVLabellabelCaption: TcxGridDBColumn;
    TVLabellabelFile: TcxGridDBColumn;
    cxGridLabelLevel1: TcxGridLevel;
    RMPreview3: TRMPreview;
    Panel4: TPanel;
    Label9: TLabel;
    Label10: TLabel;
    LabelCaption: TEdit;
    LabelType: TRTComboBox;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Tadd: TToolButton;
    Tupd: TToolButton;
    Tdel: TToolButton;
    ToolButton4: TToolButton;
    RMBarCodeObject1: TRMBarCodeObject;
    RMXLSExport1: TRMXLSExport;
    RMDataDictionary1: TRMDataDictionary;
    ADO_Down: TADOQuery;
    TVLabelColumn1: TcxGridDBColumn;
    TVLabelColumn2: TcxGridDBColumn;
    LabVolume: TEdit;
    LabPackage: TEdit;
    Label5: TLabel;
    AddWet: TEdit;
    Label6: TLabel;
    Label8: TLabel;
    Label11: TLabel;
    RuleBeforeTare: TEdit;
    RuleLenUnit: TComboBox;
    RuleLaterTare: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TupdClick(Sender: TObject);
    procedure TdelClick(Sender: TObject);
    procedure TaddClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure LabelTypeChange(Sender: TObject);
    procedure TVLabelCellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
    procedure InitLabel();
    function SaveLabel(MLabelID: string): Boolean;
    procedure InitLabVolume();
    procedure InitLabPackage();
  public
    FDataID: string;
  end;

var
  frmDyeInspSet: TfrmDyeInspSet;

implementation

uses
  U_DataLink, U_RTFun, U_LabelInput;

{$R *.dfm}

function TfrmDyeInspSet.SaveLabel(MLabelID: string): Boolean;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from DYE_Label');
      sql.Add('where LabelID=' + Quotedstr(MLabelID));
      Open;
      Edit;
      fieldByName('Editer').value := DName;
      FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
//      RMLabel.SaveToBlobField(TBlobField(FieldByName('LabelFile')));
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := false;
    application.MessageBox('保存标签模板出错!', '警告信息', 0);
  end;
end;

procedure TfrmDyeInspSet.InitLabel();
begin
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add(' select * from DYE_Label');
    sql.Add(' order by labeltype,labelCaption');
    Open;
  end;
  SDofilter(ADOQueryMain, SGetFilters(Panel4, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_Label);
  SInitCDSData(ADOQueryMain, CDS_Label);
end;

procedure TfrmDyeInspSet.InitLabVolume();
begin

  with ADOQueryLabel do
  begin
    close;
    sql.Clear;
    sql.Add('select * from DYE_Label');
    sql.Add('where labelCaption=' + quotedstr(trim(LabVolume.Text)));
    Open;
  end;
  if ADOQueryLabel.IsEmpty then
    exit;
  with RM1 do
  begin
    LoadFromBlobField(tblobfield(ADOQueryLabel.fieldbyname('labelFile')));
    Preview := RMPreview1;
    ShowReport;
  end;
end;

procedure TfrmDyeInspSet.LabelTypeChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel4, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Label);
    SInitCDSData(ADOQueryMain, CDS_Label);
  end;

end;

procedure TfrmDyeInspSet.InitLabPackage();
begin
  with ADOQueryLabel do
  begin
    close;
    sql.Clear;
    sql.Add('select * from DYE_Label');
    sql.Add('where labelCaption=' + quotedstr(trim(LabPackage.Text)));
    Open;
  end;
  if ADOQueryLabel.IsEmpty then
    exit;
  with RM2 do
  begin
    LoadFromBlobField(tblobfield(ADOQueryLabel.fieldbyname('labelFile')));
    Preview := RMPreview2;
    ShowReport;
  end;
end;

procedure TfrmDyeInspSet.InitGrid();
begin

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select top 1 * from DYE_Cloth_Inspect_Rule A  ');
    sql.Add('where EXISTS(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FDataID) + ','','') X where X.RTValue=A.RuleID)  ');
    Open;
  end;
  SCSHData(ADOQueryTemp, ScrollBox1, 2);

end;

procedure TfrmDyeInspSet.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyeInspSet := nil;
end;

procedure TfrmDyeInspSet.cxPageControl1Change(Sender: TObject);
begin
  case cxPageControl1.ActivePageIndex of
    1:
      begin
        InitLabVolume();
      end;
    2:
      begin
        InitLabPackage();
      end;
  end;
end;

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

procedure TfrmDyeInspSet.TaddClick(Sender: TObject);
begin
  try
    frmLabelInput := TfrmLabelInput.create(self);
    with frmLabelInput do
    begin
      FLabelId := '';
      if showModal = 1 then
      begin
        InitLabel();
      end;
    end;
  finally
    frmLabelInput.Free;
  end;
end;

procedure TfrmDyeInspSet.TBCloseClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmDyeInspSet.FormShow(Sender: TObject);
begin
  inherited;
  with ADO_1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_BS_Label_Get_Test');
    Open;
  end;
  Initlabel();
  InitGrid();
end;

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

procedure TfrmDyeInspSet.TdelClick(Sender: TObject);
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.Add('delete dye_Label');
      sql.Add('where labelId=' + quotedstr(CDS_Label.fieldByName('LabelID').asString));

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('标签删除')));
      sql.Add(',' + quotedstr(trim('标签名称：' + trim(CDS_Label.fieldByName('labelCaption').asString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execSql;
    end;
    CDS_Label.Delete;
  except
    application.MessageBox('删除失败!', '警告信息', 0);
  end;

end;

function TfrmDyeInspSet.SaveData(): Boolean;
var
  MaxId, MaxSubId, FCoCode, MaxCFID: string;
  FStrs: TStringList;
  i: Integer;
begin
  FStrs := TStringList.Create();
  ExtractStrings([','], [' '], PChar(FDataID), FStrs);

  try
    ADOQueryCmd.Connection.BeginTrans;

    for i := 0 to FStrs.Count - 1 do
    begin
      MaxId := FStrs[i];

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from DYE_Cloth_Inspect_Rule where RuleID=''' + Trim(MaxId) + '''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        if ADOQueryCmd.IsEmpty then
        begin
          Append;
          FieldByName('Filler').Value := Trim(DName);
        end
        else
        begin
          Edit;
          FieldByName('Editer').Value := Trim(DName);
          FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
        end;
        FieldByName('RuleID').Value := Trim(MaxId);
        RTSetsavedata(ADOQueryCmd, 'DYE_Cloth_Inspect_Rule', ScrollBox1, 2);
        Post;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    FStrs.Free;
    Result := True;
  except
    Result := false;
    ADOQueryCmd.Connection.RollbackTrans;
    FStrs.Free;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmDyeInspSet.ToolButton1Click(Sender: TObject);
begin
  if CDS_Label.IsEmpty then
    exit;
  LabVolume.Text := trim(CDS_Label.fieldbyname('labelCaption').asstring);
end;

procedure TfrmDyeInspSet.ToolButton2Click(Sender: TObject);
begin
  if CDS_Label.IsEmpty then
    exit;
  LabPackage.Text := trim(CDS_Label.fieldbyname('labelCaption').asstring);
end;

procedure TfrmDyeInspSet.ToolButton3Click(Sender: TObject);
begin

  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
  end;
end;

procedure TfrmDyeInspSet.ToolButton4Click(Sender: TObject);
var
  fFileName, fPrintFile: string;
  MLabelID, MLabelCaption: string;
begin
  if CDS_Label.IsEmpty then
    exit;
  MLabelID := TRIM(CDS_Label.fieldByName('LabelID').asString);
  MLabelCaption := TRIM(CDS_Label.fieldByName('LabelCaption').asString);
  ExportFtErpFile(MLabelCaption + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'report\' + MLabelCaption + '.rmf';

  if not FileExists(fPrintFile) then
    CopyFile(PChar(ExtractFilePath(Application.ExeName) + 'Report\模板标签.rmf'), PChar(fPrintFile), False);

  with RMLabel do
  begin
    RMLabel.Clear;
    RMDB_Label.DataSet := nil;
    Dictionary.FieldAliases.Clear;
    Dictionary.FieldAliases['RMDB_Label'] := '标签数据';
    RMDB_Label.DataSet := ADO_1;
    LoadFromFile(fPrintFile);
    application.ProcessMessages;
    DesignReport();
//    RMLabel.SaveToFile(fPrintFile);
    SaveLabel(MLabelID);
    ExportToFtErp(fPrintFile, ADOQueryCmd);
  end;
end;

procedure TfrmDyeInspSet.TupdClick(Sender: TObject);
begin
  if CDS_Label.IsEmpty then
    exit;
  try
    frmLabelInput := TfrmLabelInput.create(self);
    with frmLabelInput do
    begin
      fLabelId := CDS_Label.fieldByName('LabelId').AsString;
      if showModal = 1 then
      begin
        InitLabel();
      end;
    end;
  finally
    frmLabelInput.Free;
  end;
end;

procedure TfrmDyeInspSet.TVLabelCellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  fPrintFile: string;
begin
  if CDS_Label.IsEmpty then
    exit;
  ExportFtErpFile(TRIM(CDS_Label.fieldByName('LabelCaption').asString) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + CDS_Label.fieldbyname('LabelCaption').AsString + '.rmf';
  if not FileExists(fPrintFile) then
    CopyFile(PChar(ExtractFilePath(Application.ExeName) + 'Report\模板标签.rmf'), PChar(fPrintFile), False);

  with RMLabel do
  begin
    RMLabel.LoadFromFile(fPrintFile);
    Preview := RMPreview3;
    ShowReport;
  end;
end;

end.

