unit U_TatMachInsp1;

interface

uses
  Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCalendar, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, RM_Common, RM_Class, RM_GridReport,
  RM_System, RM_Dataset, ADODB, DBClient, cxGridCustomPopupMenu, cxGridPopupMenu,
  ExtCtrls, ComCtrls, ToolWin, cxTextEdit, Buttons, cxSplitter, cxCheckBox,
  MovePanel, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, Menus,
  U_BaseList,   dxDateRanges,
  dxBarBuiltInMenu, cxContainer, cxImage, cxDBEdit, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, ShellAPI, IdFTP,
  FireDAC.Stan.StorageXML, cxPC, XMLDoc, XMLIntf, DateUtils, cxMaskEdit,
  cxButtonEdit,  dxScrollbarAnnotations;

type
  TfrmTatMachInsp1 = class(TfrmBaseList)
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    GPM_1: TcxGridPopupMenu;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    RMDB_1: TRMDBDataSet;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    ADOQueryPrint: TADOQuery;
    GPM_2: TcxGridPopupMenu;
    ADOQueryMainDSC: TADOQuery;
    RM1: TRMGridReport;
    ADOTmp: TADOQuery;
    ScrollBox1: TScrollBox;
    SpeedButton13: TSpeedButton;
    SpeedButton14: TSpeedButton;
    SpeedButton15: TSpeedButton;
    SpeedButton16: TSpeedButton;
    SpeedButton17: TSpeedButton;
    SpeedButton18: TSpeedButton;
    SpeedButton21: TSpeedButton;
    SpeedButton22: TSpeedButton;
    SpeedButton23: TSpeedButton;
    SpeedButton24: TSpeedButton;
    SpeedButton25: TSpeedButton;
    SpeedButton26: TSpeedButton;
    SpeedButton29: TSpeedButton;
    SpeedButton30: TSpeedButton;
    SpeedButton31: TSpeedButton;
    SpeedButton32: TSpeedButton;
    SpeedButton33: TSpeedButton;
    SpeedButton34: TSpeedButton;
    SpeedButton37: TSpeedButton;
    SpeedButton38: TSpeedButton;
    SpeedButton39: TSpeedButton;
    SpeedButton40: TSpeedButton;
    SpeedButton41: TSpeedButton;
    SpeedButton42: TSpeedButton;
    SpeedButton45: TSpeedButton;
    SpeedButton46: TSpeedButton;
    SpeedButton47: TSpeedButton;
    SpeedButton48: TSpeedButton;
    SpeedButton50: TSpeedButton;
    SpeedButton51: TSpeedButton;
    SpeedButton54: TSpeedButton;
    SpeedButton55: TSpeedButton;
    SpeedButton56: TSpeedButton;
    SpeedButton57: TSpeedButton;
    SpeedButton58: TSpeedButton;
    SpeedButton59: TSpeedButton;
    SpeedButton62: TSpeedButton;
    SpeedButton63: TSpeedButton;
    SpeedButton64: TSpeedButton;
    SpeedButton65: TSpeedButton;
    SpeedButton66: TSpeedButton;
    SpeedButton67: TSpeedButton;
    SpeedButton70: TSpeedButton;
    SpeedButton71: TSpeedButton;
    SpeedButton72: TSpeedButton;
    SpeedButton73: TSpeedButton;
    SpeedButton74: TSpeedButton;
    SpeedButton75: TSpeedButton;
    SpeedButton19: TSpeedButton;
    SpeedButton20: TSpeedButton;
    SpeedButton27: TSpeedButton;
    SpeedButton28: TSpeedButton;
    SpeedButton35: TSpeedButton;
    SpeedButton36: TSpeedButton;
    SpeedButton43: TSpeedButton;
    SpeedButton44: TSpeedButton;
    SpeedButton52: TSpeedButton;
    SpeedButton53: TSpeedButton;
    SpeedButton60: TSpeedButton;
    SpeedButton61: TSpeedButton;
    SpeedButton68: TSpeedButton;
    SpeedButton69: TSpeedButton;
    SpeedButton76: TSpeedButton;
    SpeedButton77: TSpeedButton;
    SpeedButton78: TSpeedButton;
    SpeedButton79: TSpeedButton;
    SpeedButton80: TSpeedButton;
    SpeedButton81: TSpeedButton;
    SpeedButton82: TSpeedButton;
    SpeedButton83: TSpeedButton;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    ADOQueryImage: TADOQuery;
    DataSource2: TDataSource;
    IdFTP1: TIdFTP;
    Panel4: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel3: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    MovePanel1: TMovePanel;
    Label17: TLabel;
    Label18: TLabel;
    lblLenUnit1: TLabel;
    CIFBeg: TEdit;
    Button1: TButton;
    Button4: TButton;
    CIFEnd: TEdit;
    CIFQty: TEdit;
    RadioGroup1: TRadioGroup;
    Button2: TButton;
    Tv3Column4: TcxGridDBColumn;
    cxStyle4: TcxStyle;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    lblCIFName: TLabel;
    Label2: TLabel;
    Label8: TLabel;
    CIFNumber: TEdit;
    Panel9: TPanel;
    Panel2: TPanel;
    BTPrint: TButton;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    Panel10: TPanel;
    Label14: TLabel;
    Label25: TLabel;
    Label1: TLabel;
    PCId: TEdit;
    CodeName: TEdit;
    edtScan: TEdit;
    ToolButton4: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    Label13: TLabel;
    BatchNo: TEdit;
    Label4: TLabel;
    Label11: TLabel;
    InputWeight: TEdit;
    Tare: TEdit;
    Label3: TLabel;
    lblRuleConversion: TLabel;
    Coefficient: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    tv2CDType: TcxGridDBColumn;
    tv2CDWZ: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    Tv2CDQty: TcxGridDBColumn;
    Tv2CDReason: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    GPM_3: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Label22: TLabel;
    Grade: TComboBox;
    Button3: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure BTPrintClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure SpeedButton13Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure edtScanKeyPress(Sender: TObject; var Key: Char);
    procedure CIFBegClick(Sender: TObject);
    procedure CIFEndChange(Sender: TObject);
    procedure MJstr3KeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure edtScanClick(Sender: TObject);
    procedure NetWeightKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure lblLenUnitClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
    IsXiuGai: string;
    FFromRollBarcode, FCIID, FBCIOID, FOperationStatus, FCIFName, FWorkshop: string;
    FOrderNo, FMainId, FSubId, FPCId, FConNo, FConMId, FConSId, FPurNo, FPurMId, FPurSId: string;
    FLoom, FBatchNO, FLenUnit, FC_Code, FC_Name, FC_Spec, FC_Width, FC_GramWeight, FC_Composition: string;
    FC_Color, FC_ColorNo, FC_StyleNo, FC_Pattern, FC_CustPattern, FC_ColorDepth: string;
    FFtyPCId, FGangNo: string;
    FPieceNo: Integer;
    IsCommopen: boolean;
    procedure InitCDPanel(); //刷新疵点选项
    procedure InitCDGrid(MCIID: string); //刷新疵点登记信息
    procedure InitCDGridCX(MCIID: string); //刷新疵点查询信息
    procedure InitJYGrid(); //刷新检验信息
    function CheckData(CKType: string): Boolean; //校验数据
    function SaveData(): Boolean; //保存数据

    procedure ClearGlobal(); //清除全局ID
    procedure PrtData(MCIID: string);  //打印卷标签

    procedure GteInspData(MDataId: string);  //扫描流程卡

  public
    fmanage: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmTatMachInsp1: TfrmTatMachInsp1;
  newh, newh1: hwnd;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_iniParam, U_EmployeeSel;

{$R *.dfm}

procedure TfrmTatMachInsp1.GteInspData(MDataId: string);
begin
  if Pos('TPC', trim(edtScan.Text)) > 0 then
  begin
    FOperationStatus := '布票';
  end
  else if Pos('MC', trim(edtScan.Text)) > 0 then
  begin
    FOperationStatus := '检验指示单';
  end
  else
  begin
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Tat_Cloth_IO where StkId=''' + Trim(MDataId) + ''' and IOFlag=''入库''');
      Open;
    end;
    if not ADOTmp.IsEmpty then
    begin
      Application.MessageBox('已产生入库数据不能操作!', '提示', 0);
      Exit;
    end;
    FOperationStatus := '卷条码';
  end;

  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('EXEC P_Tat_Insp_Get_Data @DataId=''' + Trim(MDataId) + '''');
    sql.Add(', @DataType=''' + Trim(FOperationStatus) + '''');
    Open;
  end;

  if ADOTmp.IsEmpty = False then
  begin
    FConNo := Trim(ADOTmp.fieldbyname('ConNo').AsString);
    FConMId := Trim(ADOTmp.fieldbyname('ConMId').AsString);
    FConSId := Trim(ADOTmp.fieldbyname('ConSId').AsString);
    FOrderNo := Trim(ADOTmp.fieldbyname('OrderNo').AsString);
    FMainId := Trim(ADOTmp.fieldbyname('MainId').AsString);
    FSubId := Trim(ADOTmp.fieldbyname('SubId').AsString);
    FPurNo := Trim(ADOTmp.fieldbyname('PurNo').AsString);
    FPurMId := Trim(ADOTmp.fieldbyname('PurMId').AsString);
    FPurSId := Trim(ADOTmp.fieldbyname('PurSId').AsString);
    FBCIOID := Trim(ADOTmp.fieldbyname('BCIOID').AsString);
    FPCId := Trim(ADOTmp.fieldbyname('PCId').AsString);

    if IsXiuGai = '修改' then
      FCIID := Trim(ADOTmp.fieldbyname('CIID').AsString)
    else
      FFromRollBarcode := Trim(ADOTmp.fieldbyname('CIID').AsString);
    FWorkshop := Trim(ADOTmp.fieldbyname('Workshop').AsString);
    FC_Code := Trim(ADOTmp.fieldbyname('C_Code').AsString);
    FC_Name := Trim(ADOTmp.fieldbyname('C_Name').AsString);
    FC_Spec := Trim(ADOTmp.fieldbyname('C_Spec').AsString);
    FC_Color := Trim(ADOTmp.fieldbyname('C_Color').AsString);
    FC_ColorNo := Trim(ADOTmp.fieldbyname('C_ColorNo').AsString);
    FC_Width := Trim(ADOTmp.fieldbyname('C_Width').AsString);
    FC_GramWeight := Trim(ADOTmp.fieldbyname('C_GramWeight').AsString);
    FC_Pattern := Trim(ADOTmp.fieldbyname('C_Pattern').AsString);
    FLoom := Trim(ADOTmp.fieldbyname('Loom').AsString);
    FBatchNO := Trim(ADOTmp.fieldbyname('BatchNO').AsString);
    Coefficient.Text := '0';

    CodeName.Text := FC_Name;
    BatchNO.Text := FBatchNO;
    if Trim(FBCIOID) = '' then
      PCId.Text := FPCId
    else
      PCId.Text := FBCIOID;
  end
  else
  begin
    application.MessageBox('条码号不存在！', '提示信息', MB_ICONERROR);
    ClearGlobal();
  end;

  InitJYGrid();
  InitCDGrid(FCIID);
  edtScan.Text := '';

end;

function TfrmTatMachInsp1.CheckData(CKType: string): Boolean;
var
  mvalue: Double;
begin
  try
    if (Trim(FPCId) = '') and (Trim(FBCIOID) = '') then
      raise Exception.Create('请扫描流程卡！');

    if Trim(CKType) = '提交' then
    begin

//      if trim(lblLenUnit.Caption) = '' then
//        raise Exception.Create('长度单位不能为空!');


    end;
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatMachInsp1.ClearGlobal();
begin
  FOrderNo := '';
  FConNo := '';
  FConMId := '';
  FConSId := '';
  FMainId := '';
  FSubId := '';
  FPCId := '';
  FC_CustPattern := '';
  FC_ColorDepth := '';
  FFtyPCId := '';
  FGangNo := '';
  FC_Code := '';
  FC_Name := '';
  FBatchNO := '';
  FC_Spec := '';
  FC_Color := '';
  FC_ColorNo := '';
  FC_StyleNo := '';
  FC_Width := '';
  FC_GramWeight := '';
  FC_Pattern := '';
  FCIID := '';
  FFromRollBarcode := '';
  FWorkshop := '';
  Coefficient.Text := '';
  Tare.Text := '';
  InputWeight.Text := '';
  FLenUnit := '';
  FLoom := '';
  FC_Composition := '';
  FBCIOID := '';
  FPurNo := '';
  FPurMId := '';
  FPurSId := '';
end;

procedure TfrmTatMachInsp1.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTatMachInsp1 := nil;
end;

procedure TfrmTatMachInsp1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;

  Action := caFree;
end;

procedure TfrmTatMachInsp1.InitCDGrid(MCIID: string);
begin

  with ADOTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from Tat_Cloth_Inspect_Flaw where CIID=' + quotedstr(Trim(MCIID)));
    Open;
  end;
  SCreateCDS(ADOTmp, CDS_2);
  SInitCDSData(ADOTmp, CDS_2);
end;

procedure TfrmTatMachInsp1.InitCDGridCX(MCIID: string);
begin
  with ADOTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from Tat_Cloth_Inspect_Flaw where CIID=' + quotedstr(Trim(MCIID)));
    Open;
  end;
  SCreateCDS(ADOTmp, CDS_1);
  SInitCDSData(ADOTmp, CDS_1);
end;

procedure TfrmTatMachInsp1.InitJYGrid();
var
  MNetWeight, MJYLen: double;
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*');
    sql.Add('from Tat_Cloth_Inspect A  ');

    if FPCId <> '' then
      sql.Add(' where PCId=''' + Trim(FPCId) + '''')
    else
      sql.Add(' where BCIOID=''' + Trim(FBCIOID) + '''');

//    sql.Add('and convert(varchar(10),filltime,120)=convert(varchar(10),getdate(),120)');
    sql.Add('order by filltime desc');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS(ADOQueryMain, CDS_3);
  SInitCDSData(ADOQueryMain, CDS_3);

end;

procedure TfrmTatMachInsp1.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(Self.Caption) + 'Tv1', Tv1, '贸易检验管理');
  ReadCxGrid(trim(Self.Caption) + 'Tv2', Tv2, '贸易检验管理');
  ReadCxGrid(trim(Self.Caption) + 'Tv3', Tv3, '贸易检验管理');
  fFlileFlag := UserDataFlag + 'HX';
  InitCDPanel();
  InitCDGrid(FCIID);
  if IsINIFile() then
    ReadINIFile()
  else
    WriteINIFile;

  edtScan.SetFocus;

end;

procedure TfrmTatMachInsp1.InitCDPanel();
var
  AA: array[0..100] of string;
  i, j: Integer;
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select ZDYName from KH_Zdy where Type=''WFBCD''and MainType=''梭织'' order by ZDYNO ');
    Open;
  end;
  if ADOTmp.IsEmpty then
  begin
    Application.MessageBox('没有定义疵点！', '提示', 0);
    Exit;
  end;
  with ADOTmp do
  begin
    First;
    i := 0;
    while not Eof do
    begin
      AA[i] := Trim(fieldbyname('ZDYName').AsString);
      i := i + 1;
      Next;
    end;
  end;
  i := i - 1;
  if i > 69 then
  begin
    i := 69;
  end;
  for j := 0 to i do
  begin
    with ScrollBox1 do
    begin
      TSpeedButton(Controls[j]).Visible := True;
      TSpeedButton(Controls[j]).Hint := AA[j];
      if Length(AA[j]) > 4 then
      begin
        TSpeedButton(Controls[j]).Caption := Copy(Trim(AA[j]), 1, 4) + #13 + Copy(Trim(AA[j]), 5, Length(AA[j]) - 4);
      end
      else
        TSpeedButton(Controls[j]).Caption := AA[j];
    end;
  end;
end;

procedure TfrmTatMachInsp1.Tv3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('Grade').Index] = '次品' then
    ACanvas.Brush.Color := clRed;
end;

procedure TfrmTatMachInsp1.Tv3MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if CDS_3.isEmpty then
    exit;

  InitCDGridCX(CDS_3.FieldByName('CIID').AsString);
end;

function TfrmTatMachInsp1.SaveData(): Boolean;
var
  maxno, MCIID, strsql: string;
  MInputLen, MMeter, MYardage: Double;
  MInputWeight, MGrossWeight, MNetWeight, MTare: Double;
begin
  if FOperationStatus = '卷条码' then
  begin
    MCIID := FCIID;
  end
  else
  begin
    MCIID := '';
  end;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Tat_Cloth_Inspect where CIID=''' + Trim(MCIID) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(MCIID) = '' then
      begin
        if not GetLSNo(ADOTmp, maxno, 'ZJ' + Trim(SCXFlag), 'Tat_Cloth_Inspect', 4, 1) then
          raise Exception.Create('获取条码失败！');
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('CIID').Value := Trim(maxno);
      end
      else
      begin
        maxno := Trim(MCIID);
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTmp);
      end;

      FieldByName('InputWeight').Value := 0;
      FieldByName('GrossWeight').Value := 0;
      FieldByName('NetWeight').Value := 0;
      FieldByName('Tare').Value := 0;

      FieldByName('InputLen').Value := 0;
      FieldByName('Meter').Value := 0;
      FieldByName('Yardage').Value := 0;
      FieldByName('LenUnit').Value := TRIM('M');
      /////////////////////////           保存长度         /////////////////////////
      FieldByName('Coefficient').Value := StrToFloatdef(Coefficient.text, 0);

      FieldByName('Grade').Value := Grade.text;

      FieldByName('PieceNo').Value := 0;

      FieldByName('OrderNo').value := Trim(FOrderNo);
      FieldByName('MainId').value := Trim(FMainId);
      FieldByName('SubId').value := Trim(FSubId);
      FieldByName('ConNo').value := Trim(FConNo);
      FieldByName('ConMId').value := Trim(FConMId);
      FieldByName('ConSId').value := Trim(FConSId);

      FieldByName('BCIOID').value := Trim(FBCIOID);
      FieldByName('PurNo').value := Trim(FPurNo);
      FieldByName('PurMId').value := Trim(FPurMId);
      FieldByName('PurSId').value := Trim(FPurSId);

      FieldByName('Workshop').value := Trim(FWorkshop);
      FieldByName('CIIOFlag').Value := '未入库';
      FieldByName('PCId').value := FPCId;
      FieldByName('C_Code').value := FC_Code;
      FieldByName('C_Name').value := FC_Name;
      FieldByName('BatchNO').value := FBatchNO;
      FieldByName('Loom').value := FLoom;

      FieldByName('C_Spec').value := FC_Spec;
      FieldByName('C_Color').value := FC_Color;
      FieldByName('C_ColorNo').value := FC_ColorNo;
      FieldByName('C_StyleNo').value := FC_StyleNo;
      FieldByName('C_Width').value := FC_Width;
      FieldByName('C_GramWeight').value := FC_GramWeight;
      FieldByName('C_Pattern').value := FC_Pattern;
      FieldByName('C_CustPattern').value := FC_CustPattern;

      FieldByName('CIMachNo').Value := Trim(SCXFlag);
      FieldByName('FromRollBarcode').Value := Trim(FFromRollBarcode);
      Post;
    end;

    MCIID := Trim(maxno);   //卷条码
    FCIID := Trim(maxno);



//////////////////       保存疵点信息       //////////////////
    with CDS_2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_2.fieldbyname('CIFID').AsString) = '' then
        begin
          if not GetLSNo(ADOTmp, maxno, 'JC', 'Tat_Cloth_Inspect_Flaw', 5, 1) then
            raise Exception.Create('获取疵点失败！');
        end
        else
        begin
          maxno := Trim(CDS_2.fieldbyname('CIFID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Tat_Cloth_Inspect_Flaw  ');
          sql.Add(' where CIFID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_2.fieldbyname('CIFID').AsString) = '' then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv2, CDS_2, 'Tat_Cloth_Inspect_Flaw', 0);
          FieldByName('CIID').Value := Trim(MCIID);
          FieldByName('CIFID').Value := Trim(maxno);
          Post;
        end;
        CDS_2.Edit;
        CDS_2.FieldByName('CIID').Value := Trim(MCIID);
        CDS_2.FieldByName('CIFID').Value := Trim(maxno);
        Next;
      end;
    end;
//////////////////       保存疵点信息       //////////////////
///
//////////////////       更新卷号疵点信息       //////////////////
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Tat_Insp_Up @CIID=''' + trim(MCIID) + '''');
      sql.Add(',@InspType=' + QuotedStr(FOperationStatus));
      execsql;
    end;
//////////////////       更新卷号疵点信息       //////////////////
//////////////////       更新进度       //////////////////
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Tat_Need_Up(UType,UDataId) values(''机台检验'',' + quotedstr(maxno) + ')  ');
      execsql;
    end;
//////////////////       更新进度       //////////////////
///
    ADOCmd.Connection.CommitTrans;

    Result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    Result := False;
  end;
end;

procedure TfrmTatMachInsp1.BTPrintClick(Sender: TObject);
var
  fPrintFile: string;
  mvalue: Double;
  i: Integer;
begin
  edtScan.SetFocus;
  if CheckData('提交') = False then
    exit;

  if SaveData() then
  begin
    InputWeight.Text := '';
    IsXiuGai := '';
    PrtData(FCIID);

  end;

  if Trim(FPCId) = '' then
    FOperationStatus := '检验指示单'
  else
    FOperationStatus := '布票';

  InitJYGrid();
  InitCDGrid('');
  FCIID := '';
end;

procedure TfrmTatMachInsp1.PrtData(MCIID: string);
var
  fPrintFile, FLBName: string;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' EXEC P_Tat_Insp_Prt_Lab ');
    SQL.Add(' @CIID=''' + Trim(MCIID) + '''');
    Open;
  end;
  if ADOQueryPrint.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  end;

  FLBName := '梭织疵点标签';
  ExportFtErpFile(FLBName + '.rmf', ADOTmp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + FLBName + '.rmf';

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

procedure TfrmTatMachInsp1.SpeedButton1Click(Sender: TObject);
var
  fsj: string;
begin
  fsj := Trim(TSpeedButton(Sender).Hint);
  if Trim(fsj) = '' then
    Exit;
  if TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Name = 'KuangHao' then
    Exit;
  fsj := Trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text := fsj + Trim(TSpeedButton(Sender).Caption);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelectAll;
end;

procedure TfrmTatMachInsp1.SpeedButton12Click(Sender: TObject);
var
  fsj: string;
begin
  fsj := Trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);
  if Trim(fsj) = '' then
    Exit;
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text := Copy(fsj, 1, Length(fsj) - 1);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelectAll;
end;

procedure TfrmTatMachInsp1.SpeedButton13Click(Sender: TObject);
var
  i: Integer;
begin
  if CheckData('疵点') = False then
    exit;

  FCIFName := Trim(TSpeedButton(Sender).Hint);
  MovePanel1.Visible := True;
  lblCIFName.Caption := Trim(FCIFName);

  //显示数字键盘
  with Panel3 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint := Trim(CIFBeg.Name);
      end;
    end;
  end;

end;

procedure TfrmTatMachInsp1.Button1Click(Sender: TObject);
var
  mvalue: Double;
begin

  if Trim(CIFBeg.Text) <> '' then
  begin
    if Trim(CIFEnd.Text) <> '' then
    begin
      if StrToFloat(CIFEnd.Text) < StrToFloat(CIFBeg.Text) then
      begin
        Application.MessageBox('疵点位置录入错误!', '提示', 0);
        Exit;
      end;
    end;
  end;
  if CIFQty.Text <> '' then
  begin
    if TryStrToFloat(CIFQty.Text, mvalue) = False then
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end;
  with CDS_2 do
  begin
    Append;
    if RadioGroup1.ItemIndex = 5 then
      FieldByName('CIFName').Value := '连续性' + Trim(FCIFName)
    else if RadioGroup1.ItemIndex = 4 then
      FieldByName('CIFName').Value := '整匹' + Trim(FCIFName)
    else
      FieldByName('CIFName').Value := Trim(FCIFName);
    FieldByName('CIFbeg').Value := Trim(CIFBeg.Text);
    FieldByName('CIFEnd').Value := Trim(CIFEnd.Text);
    FieldByName('CIFPoints').Value := 0;

    if RadioGroup1.ItemIndex > -1 then
    begin
      if RadioGroup1.ItemIndex < 3 then
      begin
        FieldByName('CIFPoints').Value := RadioGroup1.ItemIndex + 1;
      end
      else
      begin
        FieldByName('CIFPoints').Value := 4;
      end;
    end;
    FieldByName('CIFQty').Value := StrToFloatdef(trim(CIFQty.Text), 0);

    Post;
  end;
  CIFBeg.Text := '';
  CIFEnd.Text := '';
  CIFQty.Text := '';
  MovePanel1.Visible := False;
end;

procedure TfrmTatMachInsp1.edtScanKeyPress(Sender: TObject; var Key: Char);
begin

  if Key = #13 then
  begin
    if trim(edtScan.Text) = '' then
      exit;
    ClearGlobal();
    GteInspData(edtScan.Text);
    InitJYGrid();
  end;

end;

procedure TfrmTatMachInsp1.CIFBegClick(Sender: TObject);
var
  i: Integer;
begin
  with Panel3 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint := Trim(TEdit(Sender).Name);
      end;
    end;
  end;
end;

procedure TfrmTatMachInsp1.CIFEndChange(Sender: TObject);
var
  mvalue: Double;
begin
  if Trim(CIFBeg.Text) <> '' then
  begin
    if TryStrToFloat(CIFBeg.Text, mvalue) = False then
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    Exit;
  end;
  if Trim(CIFEnd.Text) <> '' then
  begin
    if TryStrToFloat(CIFEnd.Text, mvalue) = False then
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    Exit;
  end;
  CIFQty.Text := FloatToStr(StrToFloat(CIFEnd.Text) - StrToFloat(CIFBeg.Text));
end;

procedure TfrmTatMachInsp1.MJstr3KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    SelectNext(ActiveControl as TWinControl, True, True);
  end;
end;

procedure TfrmTatMachInsp1.Button2Click(Sender: TObject);
begin
  RadioGroup1.ItemIndex := -1;
end;

procedure TfrmTatMachInsp1.Button3Click(Sender: TObject);
begin
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete Tat_Cloth_Inspect_Flaw where CIFID=''' + Trim(CDS_2.fieldbyname('CIFID').AsString) + '''');
    ExecSQL;
  end;

  CDS_2.Delete;
end;

procedure TfrmTatMachInsp1.edtScanClick(Sender: TObject);
var
  i: Integer;
begin
  Panel3.Visible := True;
  with Panel3 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint := Trim(TEdit(Sender).Name);
      end;
    end;
  end;
end;

procedure TfrmTatMachInsp1.NetWeightKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    BTPrint.Click;
  end;
end;

procedure TfrmTatMachInsp1.FormCreate(Sender: TObject);
begin
  inherited;
  IsCommopen := false;
end;

procedure TfrmTatMachInsp1.lblLenUnitClick(Sender: TObject);
begin
//  if Trim(lblLenUnit.Caption) = 'M' then
//    SetUnit('Y')
//  else
//    SetUnit('M');
end;

procedure TfrmTatMachInsp1.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible := false;
end;

procedure TfrmTatMachInsp1.ToolButton3Click(Sender: TObject);
begin
  if CDS_3.IsEmpty then
    Exit;
  PrtData(CDS_3.fieldbyname('CIID').AsString);

end;

procedure TfrmTatMachInsp1.ToolButton4Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := '梭织';
      flag := 'WFBCD';
      flagname := '疵点';
      fnote := True;
      V1Note.Caption := '英文名称';
      if ShowModal = 1 then
      begin
        Self.InitCDPanel();
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTatMachInsp1.ToolButton5Click(Sender: TObject);
begin
  if CDS_3.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  ADOCmd.Connection.BeginTrans;
  try
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Tat_Need_Up(UType,UDataId) values(''检验删除'',' + quotedstr(Trim(CDS_3.fieldbyname('CIID').AsString)) + ')  ');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Tat_Insp_Del ');
      Sql.Add('@CIIDS=' + quotedstr(Trim(CDS_3.fieldbyname('CIID').AsString)));
      Sql.Add(',@DCode=' + Quotedstr(DCode));
      Sql.Add(',@DName=' + Quotedstr(DName));
      Open;
    end;
    if ADOCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOCmd.Connection.CommitTrans;
    CDS_3.Delete;
    application.MessageBox('删除成功！', '提示信息');
  except
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatMachInsp1.ToolButton6Click(Sender: TObject);
begin
  if CDS_3.IsEmpty then
    Exit;
  IsXiuGai := '修改';
  GteInspData(CDS_3.fieldbyname('CIID').AsString);

end;

procedure TfrmTatMachInsp1.ToolButton7Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption) + 'Tv1', Tv1, '贸易检验管理');
  WriteCxGrid(trim(Self.Caption) + 'Tv2', Tv2, '贸易检验管理');
  WriteCxGrid(trim(Self.Caption) + 'Tv3', Tv3, '贸易检验管理');

end;

procedure TfrmTatMachInsp1.ToolButton8Click(Sender: TObject);
begin
  Close;
end;

end.

