unit U_TatClothInfoInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ADODB, DBClient, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, ComCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, cxTextEdit,
  cxButtonEdit, StdCtrls, ExtCtrls, cxCurrencyEdit, BtnEdit, U_BaseList,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, cxPC, System.ImageList, Vcl.ImgList, U_BaseInput,
  dxScrollbarAnnotations, cxContainer, cxMemo, cxMaskEdit, dxCore, dxCoreClasses,
  dxHashUtils, dxSpreadSheetCore, dxSpreadSheetCoreFormulas,
  dxSpreadSheetCoreHistory, dxSpreadSheetCoreStyles, dxSpreadSheetCoreStrs,
  dxSpreadSheetConditionalFormatting, dxSpreadSheetConditionalFormattingRules,
  dxSpreadSheetClasses, dxSpreadSheetContainers, dxSpreadSheetFormulas,
  dxSpreadSheetHyperlinks, dxSpreadSheetFunctions, dxSpreadSheetStyles,
  dxSpreadSheetGraphics, dxSpreadSheetPrinting, dxSpreadSheetTypes,
  dxSpreadSheetUtils, dxSpreadSheetFormattedTextUtils, dxSpreadSheet, cxCheckBox,
  dxSkinsCore, dxSkinsDefaultPainters, cxImageList, dxSkinOffice2013White,
  dxSkinSharpPlus, dxSkinWXI, Vcl.Menus, cxDropDownEdit, cxImage, cxDBEdit;

type
  TfrmTatClothInfoInput = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label5: TLabel;
    Label3: TLabel;
    ToolButton6: TToolButton;
    BCIID: TEdit;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ADOConnection1: TADOConnection;
    ImageList1: TImageList;
    Label55: TLabel;
    cxPageControl3: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    GroupBox1: TGroupBox;
    Y_Note_PLJ: TMemo;
    Panel1: TPanel;
    cxPageControl2: TcxPageControl;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxTabSheet2: TcxTabSheet;
    GroupBox2: TGroupBox;
    Y_Note_PLW: TMemo;
    Panel2: TPanel;
    cxPageControl1: TcxPageControl;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column10: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxTabSheet3: TcxTabSheet;
    CDS_4: TClientDataSet;
    DS_4: TDataSource;
    GPM_1: TcxGridPopupMenu;
    GPM_2: TcxGridPopupMenu;
    GPM_4: TcxGridPopupMenu;
    ToolButton11: TToolButton;
    ThroughHealdOrder: TcxMemo;
    C_Code: TcxTextEdit;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    C_Name: TcxTextEdit;
    C_YarnQty: TcxTextEdit;
    C_Width: TcxTextEdit;
    C_ReedNo: TcxTextEdit;
    C_GramWeight: TcxTextEdit;
    C_FinishLatitudeDensity: TcxTextEdit;
    TV1Column7: TcxGridDBColumn;
    Tv2Column7: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column6: TcxGridDBColumn;
    Tv2Column8: TcxGridDBColumn;
    lbl1: TLabel;
    lbl2: TLabel;
    WB_Code: TcxButtonEdit;
    WB_Name: TcxTextEdit;
    cxTabSheet4: TcxTabSheet;
    cxTabSheet5: TcxTabSheet;
    CKF: TMemo;
    TV1Column11: TcxGridDBColumn;
    Tv2Column11: TcxGridDBColumn;
    Label11: TLabel;
    Label12: TLabel;
    C_Color: TcxTextEdit;
    C_ColorNo: TcxTextEdit;
    Label13: TLabel;
    C_Pattern: TcxTextEdit;
    Label14: TLabel;
    C_StyleNo: TcxTextEdit;
    Label15: TLabel;
    C_Spec: TcxTextEdit;
    Label17: TLabel;
    C_Composition: TcxTextEdit;
    Tv2Column12: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    ADOQueryTemp2: TADOQuery;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    PM_2: TPopupMenu;
    MenuItem1: TMenuItem;
    MFDW: TcxComboBox;
    Label20: TLabel;
    C_FinishLongitudeDensity: TcxTextEdit;
    LPDW: TcxComboBox;
    TV1Column14: TcxGridDBColumn;
    Tv2Column14: TcxGridDBColumn;
    Label19: TLabel;
    GKBH: TcxTextEdit;
    Label22: TLabel;
    C_ReedWidth: TcxTextEdit;
    Label23: TLabel;
    C_KCQTY: TcxTextEdit;
    Label24: TLabel;
    BSQTY: TcxTextEdit;
    Label25: TLabel;
    DCRQTY: TcxTextEdit;
    Label26: TLabel;
    WB_ReedNote: TcxTextEdit;
    Label27: TLabel;
    ZYQTY: TcxTextEdit;
    Label4: TLabel;
    WYAMI: TcxTextEdit;
    Label16: TLabel;
    DAYQTY: TcxTextEdit;
    Label18: TLabel;
    LBQTY: TcxTextEdit;
    Panel3: TPanel;
    cxDBImage1: TcxDBImage;
    Panel4: TPanel;
    Label21: TLabel;
    ATTNAME1: TcxButtonEdit;
    DS_ImgHX: TDataSource;
    Qry_ImgHX: TADOQuery;
    Panel5: TPanel;
    Label28: TLabel;
    attname2: TcxButtonEdit;
    cxDBImage2: TcxDBImage;
    Qry_ImgHX2: TADOQuery;
    DataSource1: TDataSource;
    DS_ImgHX2: TDataSource;
    GKDW: TcxComboBox;
    LoomType: TcxComboBox;
    Label31: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label32: TLabel;
    ZS: TcxTextEdit;
    TV1YarnFactor: TcxGridDBColumn;
    Tv2YarnFactor: TcxGridDBColumn;
    Label33: TLabel;
    Label34: TLabel;
    TV1Column3: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Label35: TLabel;
    Label36: TLabel;
    WB_YarnKCQty: TcxTextEdit;
    BCRFQty: TcxTextEdit;
    Label38: TLabel;
    Label39: TLabel;
    Label40: TLabel;
    Label41: TLabel;
    Label42: TLabel;
    Label43: TLabel;
    C_Kind: TcxComboBox;
    Label37: TLabel;
    Tv2Y_Composition: TcxGridDBColumn;
    Tv2FF: TcxGridDBColumn;
    TV1Y_Composition: TcxGridDBColumn;
    TV1FF: TcxGridDBColumn;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYColorKeyPress(Sender: TObject; var Key: Char);
    procedure CYMFExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CYJGGYBtnDnClick(Sender: TObject);
    procedure C_WidthBtnDnClick(Sender: TObject);
    procedure C_WidthBtnUpClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);

    procedure ToolButton11Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure WB_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure C_ReedWidthPropertiesEditValueChanged(Sender: TObject);
    procedure TV1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure C_YarnQtyPropertiesEditValueChanged(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MFDWPropertiesEditValueChanged(Sender: TObject);
    procedure C_FinishLongitudeDensityPropertiesEditValueChanged(Sender: TObject);
    procedure C_CompositionPropertiesEditValueChanged(Sender: TObject);
    procedure Panel4Click(Sender: TObject);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure attname2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV1Column10PropertiesEditValueChanged(Sender: TObject);
    procedure C_ReedWidthExit(Sender: TObject);
    procedure cxGridDBColumn3PropertiesEditValueChanged(Sender: TObject);
  private
    canshu1: string;
    Fint: Integer;
    procedure InitGrid();
    procedure InitImage();
    procedure JSJS(); //计算经丝
    procedure JSWS(); //计算纬丝
    procedure JSKZ(); //计算克重
    function SaveData(): Boolean;
    procedure UpdateC_Code();
    { Private declarations }
  public
    FBCIID, FCTID, JSGG, WSGG, FC_Spec: string;
    WSZS, JSZS: string;
    CopyInt: Integer;

    { Public declarations }
  end;

var
  frmTatClothInfoInput: TfrmTatClothInfoInput;

implementation

uses
  U_DataLink, U_HttpFun, U_RTFun, U_iniParam, U_ZDYHelp, U_AttachmentUpload,
  U_YarnInfoSel, U_WBSpecSel, U_PictureUpload, U_LogInput;

{$R *.dfm}
procedure TfrmTatClothInfoInput.InitImage();
begin
  if attname1.Text <> '' then
  begin
    with Qry_ImgHX do
    begin
      close;
      sql.Clear;
      sql.Add('select * from TP_File A');
      sql.Add('where isnull(WBID,'''')<>'''' and wbid=' + quotedstr(Trim(attname1.Text)));
      SQL.Add('AND TFTYPE=''ZC'' ');
//    ShowMessage(SQL.Text);
      open;
    end;

  end;
  if attname2.Text <> '' then
  begin
    with Qry_ImgHX2 do
    begin
      close;
      sql.Clear;
      sql.Add('select * from TP_File A');
      sql.Add('where isnull(WBID,'''')<>'''' and wbid=' + quotedstr(Trim(attname2.Text)));
      SQL.Add('AND TFTYPE=''qC'' ');
//    ShowMessage(SQL.Text);
      open;
    end;

  end;
end;

procedure TfrmTatClothInfoInput.InitGrid();
var
  myStream: TMemoryStream;
  mSql: string;
begin

  mSql := 'select distinct name=A.mctype from Bs_Machine A   ';
  SInitTcxComBoxBySql(ADOQueryTemp, LoomType, false, mSql);
  myStream := TMemoryStream.Create();

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  from BS_Cloth_Yarn A ');
    sql.Add(' where YRType=''经丝'' and A.BCIID=''' + Trim(FBCIID) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  from BS_Cloth_Yarn A ');
    sql.Add(' where YRType=''纬丝'' and A.BCIID=''' + Trim(FBCIID) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
  if FBCIID <> '' then
  begin
    CDS_2.Edit;
    CDS_2.FieldByName('Y_Loss').Value := 4.2;
    CDS_2.Post;
  end;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from BS_Cloth_Info where BCIID=''' + Trim(FBCIID) + '''');
    Open;
  end;
  SCSHData(ADOQueryTemp, ScrollBox1, 2);
  SCSHData(ADOQueryTemp, ScrollBox1, 0);

  CKF.Text := Trim(ADOQueryTemp.FieldByName('CKF').AsString);

  Y_Note_PLJ.Text := Trim(ADOQueryTemp.FieldByName('Y_Note_PLJ').AsString);
  Y_Note_PLW.Text := Trim(ADOQueryTemp.FieldByName('Y_Note_PLW').AsString);
  ATTNAME1.Text := Trim(ADOQueryTemp.FieldByName('ATTNAME1').AsString);
  ATTNAME2.Text := Trim(ADOQueryTemp.FieldByName('ATTNAME2').AsString);

  //////////////////////////            保存起综              //////////////////////////
//  with ADOQueryTemp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add(' select *  from BS_Cloth_Info_Excel where BCIID=''' + Trim(FBCIID) + '''');
//    open;
//  end;
//
//  if not ADOQueryTemp.IsEmpty then
//  begin
//
//    TBlobField(ADOQueryTemp.FieldByName('ExcelData')).SaveToStream(myStream);
//    myStream.Position := 0;
//    dxSpreadSheet1.LoadFromStream(myStream);
//  end
//  else
//  begin
//
//    with ADOQueryTemp2 do
//    begin
//      close;
//      sql.Clear;
//      sql.Add(' select *  from BS_Cloth_Info_Excel2 ');
//      open;
//    end;
//    TBlobField(ADOQueryTemp2.FieldByName('ExcelData')).SaveToStream(myStream);
//    myStream.Position := 0;
//    dxSpreadSheet1.LoadFromStream(myStream);
//  end;

  if FBCIID = '' then
  begin
    with CDS_1 do
    begin
      Append;
      FieldByName('RowNo').Value := 'A';
      Post;
      Append;
      FieldByName('RowNo').Value := 'B';
      Post;
      Append;
      FieldByName('RowNo').Value := 'C';
      Post;
      Append;
      FieldByName('RowNo').Value := 'D';
      Post;
      Append;
      FieldByName('RowNo').Value := 'E';
      Post;
      Append;
      FieldByName('RowNo').Value := 'F';
      Post;

    end;
    with CDS_2 do
    begin
      Append;
      FieldByName('RowNo').Value := 'A';
      FieldByName('Y_Loss').Value := 4.2;
      Post;
      Append;
      FieldByName('RowNo').Value := 'B';
      Post;
      Append;
      FieldByName('RowNo').Value := 'C';
      Post;
      Append;
      FieldByName('RowNo').Value := 'D';
      Post;
      Append;
      FieldByName('RowNo').Value := 'E';
      Post;
      Append;
      FieldByName('RowNo').Value := 'F';
      Post;

    end;

    myStream.Free;
  end;

  /////////////////刷新起综工艺单信息/////////////////////////
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_CLoth_QZ where BCIID=''' + Trim(FBCIID) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_4);
  SInitCDSData(ADOQueryTemp, CDS_4);


 /////////////////刷新起综工艺单信息/////////////////////////
 ///
end;

procedure TfrmTatClothInfoInput.JSJS();
var
  GS, ZGS, MF, DS, WM, TW: double;
begin
//  if CDS_1.IsEmpty then
//    exit;
//  with CDS_1 do
//  begin
//    DisableControls;
//    first;
//    while not eof do
//    begin
//
//      with CDS_1 do
//      begin
//
//        if FieldByName('YarnQty').Value = null then
//        begin
//          GS := 0;   //根数
//        end
//        else
//        begin
//          GS := FieldByName('YarnQty').Value;   //根数
//        end;
//
//        if FieldByName('CycleYarnQty').Value = null then
//        begin
//          ZGS := 0; //循环根数
//        end
//        else
//        begin
//          ZGS := FieldByName('CycleYarnQty').Value; //循环根数
//        end;
//
//        MF := strtofloatdef(C_ReedWidth.text, 0);    //筘幅
//
//        TW := strtofloatdef(C_YarnQty.text, 0);    //头纹
//
//        if FieldByName('TestDenier').Value = null then
//        begin
//          DS := 0;     //D数
//        end
//        else
//        begin
//          DS := FieldByName('TestDenier').Value;     //D数
//        end;
//
//        WM := strtofloatdef(C_FinishLatitudeDensity.text, 0); //纬密
//      end;
//      if ZGS <> 0 then
//      begin
//        with CDS_1 do
//        begin
//          Edit;
//          FieldByName('MeterQty').Value := roundfloat(GS / ZGS * TW * DS / 9000, 2);
//          Post;
//        end;
//      end;
//      next;
//    end;
//    First;
//    EnableControls;
//  end;

  JSGG := '';
  with CDS_1 do
  begin
    first;
    while not eof do
    begin
      if Trim(CDS_1.fieldbyname('Z_Number').AsString) <> '' then
      begin
        if POS(Trim(CDS_1.fieldbyname('Z_Number').AsString), JSGG) = 0 then
        begin
          JSGG := JSGG + Trim(CDS_1.fieldbyname('Z_Number').AsString) + '+';
        end;
      end;
      next;
    end;
    First;
  end;

  JSGG := Copy(JSGG, 1, Length(JSGG) - 1);

end;

procedure TfrmTatClothInfoInput.JSWS();
var
  GS, ZGS, MF, DS, WM: double;
begin
//  if CDS_2.IsEmpty then
//    exit;
//  with CDS_2 do
//  begin
//    DisableControls;
//    first;
//    while not eof do
//    begin
//
//      with CDS_2 do
//      begin
//
//        if FieldByName('YarnQty').Value = null then
//        begin
//          GS := 0;   //根数
//        end
//        else
//        begin
//          GS := FieldByName('YarnQty').Value;   //根数
//        end;
//
//        if FieldByName('CycleYarnQty').Value = null then
//        begin
//          ZGS := 0; //循环根数
//        end
//        else
//        begin
//          ZGS := FieldByName('CycleYarnQty').Value; //循环根数
//        end;
//
//        MF := strtofloatdef(C_ReedWidth.text, 0) + 15;    //筘幅
//
//        if FieldByName('TestDenier').Value = null then
//        begin
//          DS := 0;     //D数
//        end
//        else
//        begin
//          DS := FieldByName('TestDenier').Value;     //D数
//        end;
//
//        WM := strtofloatdef(C_FinishLatitudeDensity.text, 0); //纬密
//      end;
//      if ZGS <> 0 then
//      begin
//        with CDS_2 do
//        begin
//          Edit;
//          FieldByName('MeterQty').Value := roundfloat(GS / ZGS * MF * DS * WM / 9000, 2);
//          Post;
//        end;
//      end;
//      next;
//    end;
//    First;
//    EnableControls;
//  end;


  WSGG := '';
  with CDS_2 do
  begin
    first;
    while not eof do
    begin
      if Trim(CDS_2.fieldbyname('Z_Number').AsString) <> '' then
      begin
        if POS(Trim(CDS_2.fieldbyname('Z_Number').AsString), WSGG) = 0 then
        begin
          WSGG := WSGG + Trim(CDS_2.fieldbyname('Z_Number').AsString) + '+';
        end;
      end;
      next;
    end;
    First;
  end;

  WSGG := Copy(WSGG, 1, Length(WSGG) - 1);
end;

procedure TfrmTatClothInfoInput.MenuItem1Click(Sender: TObject);
begin
  with CDS_2 do
  begin
    CDS_2.edit;
    FieldByName('Y_Code').Value := null;
    FieldByName('Y_Name').Value := null;
    FieldByName('Y_Spec').Value := null;
    FieldByName('Y_Color').Value := null;
    FieldByName('YarnQty').Value := null;
    FieldByName('CycleYarnQty').Value := null;
    FieldByName('FF').Value := null;
    FieldByName('ND').Value := null;
    FieldByName('Dacron').Value := null;
    FieldByName('Cotton').Value := null;
    FieldByName('Linen').Value := null;
    FieldByName('Brand').Value := null;
    FieldByName('TestDenier').Value := null;
    FieldByName('Y_Composition').Value := null;
    FieldByName('MeterQty').Value := null;
    FieldByName('PTNo').Value := null;

    FieldByName('Z_Number').Value := null;
    FieldByName('YarnFactor').Value := null;
    FieldByName('Y_Loss').Value := null;
    FieldByName('NOTE').Value := null;
    Post;
  end;
end;

procedure TfrmTatClothInfoInput.MFDWPropertiesEditValueChanged(Sender: TObject);
begin
  JSKZ();
  UpdateC_Code();
end;

procedure TfrmTatClothInfoInput.N1Click(Sender: TObject);
begin
  with CDS_1 do
  begin
    CDS_1.edit;
    FieldByName('Y_Code').Value := null;
    FieldByName('Y_Name').Value := null;
    FieldByName('Y_Spec').Value := null;
    FieldByName('Y_Color').Value := null;
    FieldByName('YarnQty').Value := null;
    FieldByName('CycleYarnQty').Value := null;
    FieldByName('FF').Value := null;
    FieldByName('ND').Value := null;
    FieldByName('Dacron').Value := null;
    FieldByName('Cotton').Value := null;
    FieldByName('Linen').Value := null;
    FieldByName('Brand').Value := null;
    FieldByName('TestDenier').Value := null;
    FieldByName('Y_Composition').Value := null;
    FieldByName('MeterQty').Value := null;
    FieldByName('PTNo').Value := null;

    FieldByName('Z_Number').Value := null;
    FieldByName('QFYARNQTY').Value := null;
    FieldByName('YarnFactor').Value := null;
    FieldByName('NOTE').Value := null;
    Post;
  end;
end;

procedure TfrmTatClothInfoInput.Panel4Click(Sender: TObject);
begin
  try
    frmPictureUpload := TfrmPictureUpload.create(self);
    with frmPictureUpload do
    begin
      fFlileFlag := UserDataFlag + 'HX';
      FDataId := ATTNAME1.Text;
      FPictureName := ATTNAME1.Text;
      FWidth := 450;
      FHeight := 600;
      FTFType := 'ZC';
      if showmodal = 1 then
      begin
        ATTNAME1.Text := frmPictureUpload.FPictureName;
      end;
    end;
  finally
    frmPictureUpload.free;
  end;
  initimage();

end;

procedure TfrmTatClothInfoInput.JSKZ();
var
  SumJS, SumWS: double;
  FJinSha, FWeiSha: string;
begin
//  C_GramWeight.SetFocus;
//
//  if Tv1.DataController.Summary.FooterSummaryValues[0] <> null then
//  begin
//    SumJS := Tv1.DataController.Summary.FooterSummaryValues[0];
//  end
//  else
//  begin
//    SumJS := 0;
//  end;
//
//  if Tv2.DataController.Summary.FooterSummaryValues[0] <> null then
//  begin
//    SumWS := Tv2.DataController.Summary.FooterSummaryValues[0];
//  end
//  else
//  begin
//    SumWS := 0;
//  end;
//
//  C_GramWeight.text := floattostr(roundfloat((SumJS + SumWS) / 3, 2));
  /////////////生成规格 /////////////

//  FC_Spec := '';
//  FC_Spec := C_Composition.text + ' (' + JSGG + ')*(' + WSGG + ') ' + C_FinishLongitudeDensity.text + '*' + C_FinishLatitudeDensity.text + ' ' + C_Width.text + MFDW.text;
//  C_Spec.text := trim(FC_Spec);

  FC_Spec := '';
  FC_Spec := JSGG + 'S*' + WSGG + 'S';
  C_Spec.text := trim(FC_Spec);

  /////////////生成规格 /////////////
end;

procedure TfrmTatClothInfoInput.FormShow(Sender: TObject);
begin

  readCxGrid(self.Caption + 'TV11', Tv1, '样品管理');
  readCxGrid(self.Caption + 'TV22', Tv2, '样品管理');
//  readCxGrid(self.Caption + 'TV44', Tv4, '样品管理');

  JSZS := '';
  WSZS := '';
  InitGrid();

  JSJS();
  JSWS();
  JSKZ();

  if CopyInt = 1 then
  begin
    FBCIID := '';
    BCIID.text := '';
    C_Code.text := '';
    ATTNAME1.Text := '';
    ATTNAME2.Text := '';
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('BCIID').Value := '';
        FieldByName('YRId').Value := '';
        Post;
        Next;
      end;
    end;

    with CDS_2 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('BCIID').Value := '';
        FieldByName('YRId').Value := '';
        Post;
        Next;
      end;
    end;

  end;
  InitImage();
end;

procedure TfrmTatClothInfoInput.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmTatClothInfoInput.UpdateC_Code();
var
  mJSDH, mZS, mC_Kind: string;
  mC_Code: string;
begin
  if FBCIID = '' then
  begin
    CDS_1.First;
    while not CDS_1.Eof do
    begin
      if CDS_1.FieldByName('Y_Code').AsString <> '' then
      begin
        mZS := CDS_1.FieldByName('Z_Number').AsString + 'S';
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.add('select * from BS_Yarn_Info_CF where Y_Code = ' + quotedStr(Trim(CDS_1.FieldByName('Y_Code').AsString)));
          sql.Add(' order By CFID');
          Open;
        end;
        ADOQueryCmd.First;
        while not ADOQueryCmd.Eof do
        begin
          mJSDH := mJSDH + ADOQueryCmd.FieldByName('DH').AsString;
          ADOQueryCmd.Next;
        end;
        if mC_Code = '' then
        begin
          mC_Code := mJSDH + '-' + mZS;
        end
        else
        begin
          mC_Code := mC_Code + '-' + mJSDH + '-' + mZS;
        end;
        mJSDH := '';

      end;
      CDS_1.Next;
    end;

    if C_Width.Text <> '' then
    begin
      if mC_Code = '' then
      begin
        mC_Code := C_Width.Text;
      end
      else
      begin
        mC_Code := mC_Code + '-' + C_Width.Text;
      end;
    end;

    if C_Kind.Text <> '' then
    begin
      if C_Kind.Text = '色织布' then
      begin
        mC_Kind := 'A';
      end
      else if C_Kind.Text = '白坯' then
      begin
        mC_Kind := 'B';
      end;

      if mC_Code = '' then
      begin
        mC_Code := mC_Kind;
      end
      else
      begin
        mC_Code := mC_Code + '-' + mC_Kind;
      end;
    end;

    C_Code.Text := mC_Code;
  end;

end;

function TfrmTatClothInfoInput.SaveData(): Boolean;
var
  maxId, maxpbno, maxQZID, HZYRId, JsonStr: string;
  myStream: TADOBlobStream;
  myStream1: TMemoryStream;
begin

  try
    ADOQueryCmd.Connection.BeginTrans;

    if Trim(FBCIID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'Y', 'BS_Cloth_Info', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

      BCIID.Text := trim(maxId);
    end
    else
    begin
      maxId := Trim(FBCIID);
    end;

    try
      if FBCIID <> '' then
      begin

        frmLogInput := TfrmLogInput.Create(Application);
        with frmLogInput do
        begin
          fC_Code := Self.C_Code.Text;
          fmaxId := maxId;
          fflag := '修改';
          if ShowModal = 1 then
          begin

          end;
        end;

      end;
    finally
      frmLogInput.Free;
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BS_Cloth_Info where BCIID=''' + Trim(FBCIID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FBCIID) = '' then
      begin
        Append;
        FieldByName('CTID').Value := Trim(FCTID);
        FieldByName('FILLID').Value := Trim(DCode);
        FieldByName('FILLER').Value := Trim(DName);
        FieldByName('FILLTIME').Value := SGetServerDateTime(ADOQueryTemp);
      end
      else
      begin
        Edit;
        FieldByName('EDITER').Value := Trim(DName);
        FieldByName('EDITTIME').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('BCIID').Value := Trim(maxId);

      FieldByName('CKF').Value := Trim(CKF.text);

      FieldByName('Y_Note_PLJ').Value := Trim(Y_Note_PLJ.text);
      FieldByName('Y_Note_PLW').Value := Trim(Y_Note_PLW.text);
      FieldByName('ATTNAME1').Value := Trim(ATTNAME1.text);
      FieldByName('ATTNAME2').Value := Trim(ATTNAME2.text);
      if trim(C_Code.Text) = '' then
        C_Code.Text := Trim(maxId);
      RTSetsavedata(ADOQueryCmd, 'BS_Cloth_Info', ScrollBox1, 2);
      Post;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from BS_Cloth_Info where C_Code=''' + Trim(C_Code.Text) + '''');
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result := False;
        Application.MessageBox('编号重复！', '提示', 0);
        Exit;
      end;
    end;
    //////////////////////////            保存经丝配比表              //////////////////////////
    with CDS_1 do
    begin
      First;
      HZYRId := '';
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('YRId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'RJ', 'BS_Cloth_Yarn', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_1.fieldbyname('YRId').AsString);
        end;

        HZYRId := HZYRId + ';' + maxpbno;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from BS_Cloth_Yarn where ');
          sql.Add('  YRId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_1.fieldbyname('YRId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('YRType').Value := '经丝';
          FieldByName('BCIID').Value := Trim(maxId);
          FieldByName('YRId').Value := Trim(maxpbno);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'BS_Cloth_Yarn', 0);
          Post;
        end;
        CDS_1.Edit;
        CDS_1.FieldByName('BCIID').Value := Trim(maxId);
        CDS_1.FieldByName('YRId').Value := Trim(maxpbno);

        Next;
      end;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('update BS_Cloth_Yarn set BCIID = rtrim(BCIID) + ''删'' where  YRType =''经丝'' and BCIID=''' + Trim(maxId) + '''');
      sql.Add('and  YRId not in (select RTValue from [dbo].[F_Tool_SplitString](''' + Trim(HZYRId) + '''');
      sql.Add(', '';'' ))');
      ExecSQL;
    end;




//////////////////////////            保存经丝配比表              //////////////////////////
//////////////////////////            保存纬丝配比表              //////////////////////////
    with CDS_2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_2.fieldbyname('YRId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'RJ', 'BS_Cloth_Yarn', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取配比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_2.fieldbyname('YRId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from BS_Cloth_Yarn where ');
          sql.Add('  YRId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_2.fieldbyname('YRId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('YRType').Value := '纬丝';
          FieldByName('BCIID').Value := Trim(maxId);
          FieldByName('YRId').Value := Trim(maxpbno);
          RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_2, 'BS_Cloth_Yarn', 0);
          Post;
        end;
        CDS_2.Edit;
        CDS_2.FieldByName('BCIID').Value := Trim(maxId);
        CDS_2.FieldByName('YRId').Value := Trim(maxpbno);

        Next;
      end;
    end;
//////////////////////////            保存纬丝配比表              //////////////////////////
//////////////////////////            保存起综              //////////////////////////
//    with CDS_4 do
//    begin
//      First;
//      while not Eof do
//      begin
//        if Trim(CDS_4.fieldbyname('QZID').AsString) = '' then
//        begin
//          if GetLSNo(ADOQueryTemp, maxQZID, 'QZ', 'BS_CLoth_QZ', 4, 1) = False then
//            raise Exception.Create('取配比子流水号失败！');
//
//        end
//        else
//        begin
//          maxQZID := Trim(CDS_4.fieldbyname('QZID').AsString);
//        end;
//        with ADOQueryCmd do
//        begin
//          Close;
//          SQL.Clear;
//          sql.Add('select * from BS_CLoth_QZ where ');
//          sql.Add('  QZID=''' + Trim(maxQZID) + '''');
//          Open;
//        end;
//        with ADOQueryCmd do
//        begin
//          if Trim(CDS_4.fieldbyname('QZID').AsString) = '' then
//          begin
//            Append;
//          end
//          else
//          begin
//            Edit;
//          end;
//          FieldByName('BCIID').Value := Trim(maxId);
//          FieldByName('QZID').Value := Trim(maxQZID);
//          RTSetSaveDataCDS(ADOQueryCmd, Tv4, CDS_4, 'BS_CLoth_QZ', 0);
//          Post;
//        end;
//        CDS_4.Edit;
//        CDS_4.FieldByName('BCIID').Value := Trim(maxId);
//        CDS_4.FieldByName('QZID').Value := Trim(maxQZID);
//
//        Next;
//      end;
//    end;
//////////////////////////            保存起综              //////////////////////////
//    dxSpreadSheet1.SaveToFile('D:\FilesExcel.xlsx');
//    with ADOQueryCmd do
//    begin
//      close;
//      sql.Clear;
//      sql.Add(' select *  from BS_Cloth_Info_Excel where BCIID=''' + Trim(maxId) + '''');
//      open;
//    end;
//    with ADOQueryCmd do
//    begin
//      if ADOQueryCmd.IsEmpty then
//      begin
//        Append;
//      end
//      else
//      begin
//        Edit;
//      end;
//      FieldByName('BCIID').Value := maxId;
//      myStream := TADOBlobStream.Create(TBlobField(ADOQueryCmd.FieldByName('ExcelData')), bmWrite);
//
//      myStream1 := TMemoryStream.Create();
//      myStream1.LoadFromFile('D:\FilesExcel.xlsx');
//
//      tblobfield(FieldByName('xlsx')).LoadFromStream(myStream1);
//
//      dxSpreadSheet1.SaveToStream(myStream);
//      myStream.Free;
//      Post;
//    end;
//    if pos('接口异常', JsonStr) > 0 then
//    begin
//      raise Exception.Create(pchar(JsonStr));
//    end;
//
//    if pos('ResultList', JsonStr) = 0 then
//    begin
//      Application.MessageBox(pchar('获取库存失败:' + JsonGetChildObject(JsonStr, 'Info')), '提示', 0);
//    end
//    else
//    begin
//      JsonToCHDA(JsonStr, 'ResultList', CDS_2);
//    end;

    ADOQueryCmd.Connection.CommitTrans;
    FBCIID := maxId;
//    JsonStr := RTGetRequest('http://139.224.118.114:8081/execltoimg/' + trim(maxId));
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmTatClothInfoInput.CYJGGYBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
end;

procedure TfrmTatClothInfoInput.ToolButton10Click(Sender: TObject);
begin
  if CDS_4.IsEmpty then
    exit;
  if CDS_4.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要作废数据吗？作废后数据不能恢复。', '提示', 32 + 4) <> IDYES then
    Exit;
  CDS_4.DisableControls;
  with CDS_4 do
  begin
    First;
    while not Eof do
    begin
      if CDS_4.FieldByName('SSel').AsBoolean = True then
      begin
        if Trim(CDS_4.fieldbyname('QZID').AsString) <> '' then
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete BS_CLoth_QZ where QZID=''' + Trim(CDS_4.fieldbyname('QZID').AsString) + '''');
            ExecSQL;
          end;
        CDS_4.Delete;
      end
      else
        Next;

    end;

  end;
  CDS_4.EnableControls;
end;

procedure TfrmTatClothInfoInput.ToolButton11Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + 'TV11', Tv1, '样品管理');
  WriteCxGrid(self.Caption + 'TV22', Tv2, '样品管理');
//  WriteCxGrid(self.Caption + 'TV44', Tv4, '样品管理');
end;

procedure TfrmTatClothInfoInput.ToolButton1Click(Sender: TObject);
var
  MC_Code: string;
begin
  C_Name.SetFocus;
  if C_Name.Text = '' then
  begin
    Application.MessageBox('产品名称不能为空！', '提示', 0);
    Exit;
  end;

  if MFDW.Text = '' then
  begin
    Application.MessageBox('门幅单位不能为空！', '提示', 0);
    Exit;
  end;
  if C_Width.Text = '' then
  begin
    Application.MessageBox('门幅不能为空！', '提示', 0);
    Exit;
  end;

  if C_FinishLongitudeDensity.Text = '' then
  begin
    Application.MessageBox('经密不能为空！', '提示', 0);
    Exit;
  end;
  if C_FinishLatitudeDensity.Text = '' then
  begin
    Application.MessageBox('纬密不能为空！', '提示', 0);
    Exit;
  end;

  if WYAMI.Text = '' then
  begin
    Application.MessageBox('纬牙密不能为空！', '提示', 0);
    Exit;
  end;

  if WB_Code.Text = '' then
  begin
    Application.MessageBox('整经工艺编号不能为空！', '提示', 0);
    Exit;
  end;
  if WB_Name.Text = '' then
  begin
    Application.MessageBox('整经工艺名称不能为空！', '提示', 0);
    Exit;
  end;
  if C_YarnQty.Text = '' then
  begin
    Application.MessageBox('总头纹不能为空！', '提示', 0);
    Exit;
  end;
  if GKBH.Text = '' then
  begin
    Application.MessageBox('钢筘编号不能为空！', '提示', 0);
    Exit;
  end;
  if C_ReedNo.Text = '' then
  begin
    Application.MessageBox('筘号不能为空！', '提示', 0);
    Exit;
  end;
  if C_ReedWidth.Text = '' then
  begin
    Application.MessageBox('筘幅不能为空！', '提示', 0);
    Exit;
  end;
  if DCRQTY.Text = '' then
  begin
    Application.MessageBox('地传入数不能为空！', '提示', 0);
    Exit;
  end;
  if DAYQTY.Text = '' then
  begin
    Application.MessageBox('日产量不能为空！', '提示', 0);
    Exit;
  end;
  if LoomType.Text = '' then
  begin
    Application.MessageBox('机型不能为空！', '提示', 0);
    Exit;
  end;

  // 遍历检查经丝数据 (CDS_1)
  CDS_1.First;
  while not CDS_1.Eof do
  begin
  // 只有当Y_Code有值时才检查YarnFactor
    if (CDS_1.FieldByName('Y_Code').AsString <> '') and (CDS_1.FieldByName('Y_Code').AsString <> null) then
    begin
      if (CDS_1.FieldByName('YarnFactor').AsString = '') or (CDS_1.FieldByName('YarnFactor').AsString = null) then
      begin
        Application.MessageBox('经丝纱线系数不能为空！', '提示', 0);
        Exit; // 直接结束整个方法
      end;
    end;
    CDS_1.Next;
  end;

// 遍历检查纬丝数据 (CDS_2)
  CDS_2.First;
  while not CDS_2.Eof do
  begin
  // 只有当Y_Code有值时才检查YarnFactor
    if (CDS_2.FieldByName('Y_Code').AsString <> '') and (CDS_2.FieldByName('Y_Code').AsString <> null) then
    begin
      if (CDS_2.FieldByName('YarnFactor').AsString = '') or (CDS_2.FieldByName('YarnFactor').AsString = null) then
      begin
        Application.MessageBox('纬丝纱线系数不能为空！', '提示', 0);
        Exit; // 直接结束整个方法
      end;
    end;
    CDS_2.Next;
  end;

  if trim(C_Code.Text) = '' then
  begin

    if GetLSNo(ADOQueryCmd, MC_Code, 'C', 'BS_Cloth_Info', 4, 0) = False then
    begin
      Application.MessageBox('取最成品编号失败!', '提示', 0);
      Exit;
    end;
    C_Code.Text := MC_Code;
  end;

  JSJS();
  JSWS();
  JSKZ();

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmTatClothInfoInput.attname2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmPictureUpload := TfrmPictureUpload.create(self);
    with frmPictureUpload do
    begin
      fFlileFlag := UserDataFlag + 'HX';
      FDataId := AttName2.Text;
      FPictureName := AttName2.Text;
      FWidth := 600;
      FHeight := 600;
      FTFType := 'QC';
      if showmodal = 1 then
      begin
        AttName2.Text := frmPictureUpload.FPictureName;
      end;
    end;
  finally
    frmPictureUpload.free;
  end;
  initimage();
end;

procedure TfrmTatClothInfoInput.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmPictureUpload := TfrmPictureUpload.create(self);
    with frmPictureUpload do
    begin
      fFlileFlag := UserDataFlag + 'HX';
      FDataId := AttName1.Text;
      FPictureName := AttName1.Text;
      FWidth := 600;
      FHeight := 600;
      FTFType := 'ZC';
      if showmodal = 1 then
      begin
        AttName1.Text := frmPictureUpload.FPictureName;
      end;
    end;
  finally
    frmPictureUpload.free;
  end;
  initimage();
end;

procedure TfrmTatClothInfoInput.cxGridDBColumn1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Y_Name';
      flagname := '原料名称';
      if ShowModal = 1 then
      begin
        Self.CDS_2.Edit;
        Self.CDS_2.FieldByName('Y_Name').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTatClothInfoInput.cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with self.CDS_1 do
        begin
          Edit;

          FieldByName('Y_Code').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Code').Value;
          FieldByName('Y_Name').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Name').Value;

//          FieldByName('Y_Spec').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Spec').Value;
          FieldByName('Y_Color').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Color').Value;

          FieldByName('FF').Value := frmYarnInfoSel.CDS_1.fieldbyname('FF').Value;
//          FieldByName('ND').Value := frmYarnInfoSel.CDS_1.fieldbyname('ND').Value;
          FieldByName('YarnFactor').Value := 0.059;

//          FieldByName('Brand').Value := frmYarnInfoSel.CDS_1.fieldbyname('Brand').Value;
          FieldByName('Y_Composition').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Composition').Value;
          FieldByName('TestDenier').Value := frmYarnInfoSel.CDS_1.fieldbyname('Denier').Value;
          FieldByName('Z_Number').Value := frmYarnInfoSel.CDS_1.fieldbyname('ZS').Value;
          Post;
          TV1Column10PropertiesEditValueChanged(Sender);
        end;

        JSJS();
        JSWS();
        JSKZ();
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;

end;

procedure TfrmTatClothInfoInput.cxGridDBColumn3PropertiesEditValueChanged(Sender: TObject);
begin
  UpdateC_Code();
end;

procedure TfrmTatClothInfoInput.cxGridDBColumn4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Y_Name';
      flagname := '原料名称';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName('Y_Name').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmTatClothInfoInput.C_CompositionPropertiesEditValueChanged(Sender: TObject);
begin
  JSKZ();
end;

procedure TfrmTatClothInfoInput.C_FinishLongitudeDensityPropertiesEditValueChanged(Sender: TObject);
begin
  JSKZ();
end;

procedure TfrmTatClothInfoInput.C_ReedWidthExit(Sender: TObject);
var
  mvalue, FFieldName: string;
  GS, ZGS, MF, DS, WM, fsh: double;
begin
//  with CDS_2 do
//  begin
//    first;
//    while not eof do
//    begin
//      with CDS_2 do
//      begin
//
//        if FieldByName('YarnQty').Value = null then
//        begin
//          GS := 0;   //根数
//        end
//        else
//        begin
//          GS := FieldByName('YarnQty').Value;   //根数
//        end;
//
//        if FieldByName('Y_loss').Value = null then
//        begin
//          fsh := 0;   //损耗
//        end
//        else
//        begin
//          fsh := FieldByName('Y_loss').Value;   //损耗
//        end;
//
//        if FieldByName('CycleYarnQty').Value = null then
//        begin
//          ZGS := 0; //循环根数
//        end
//        else
//        begin
//          ZGS := FieldByName('CycleYarnQty').Value; //循环根数
//        end;
//
//        MF := strtofloatdef(C_ReedWidth.text, 0);    //筘幅
//
//        if FieldByName('Z_Number').Value = null then
//        begin
//          DS := 0;     //支
//        end
//        else
//        begin
//          DS := FieldByName('Z_Number').Value;     //支数
//        end;
//
//        WM := strtofloatdef(C_FinishLatitudeDensity.text, 0); //纬密
//      end;
//      if (ZGS <> 0) and (DS <> 0) then
//      begin
//        with CDS_2 do
//        begin
//          Edit;
//          FieldByName('MeterQty').Value := roundfloat(WM * (MF + fsh) * 0.059 / DS * GS / ZGS * 1.08, 2);
//          Post;
//        end;
//      end;
//      next;
//    end;
//
//  end;
end;

procedure TfrmTatClothInfoInput.C_ReedWidthPropertiesEditValueChanged(Sender: TObject);
begin
  JSJS();
  JSWS();
  JSKZ();
  UpdateC_Code();
end;

procedure TfrmTatClothInfoInput.C_WidthBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmTatClothInfoInput.C_WidthBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTatClothInfoInput.C_YarnQtyPropertiesEditValueChanged(Sender: TObject);
begin
  JSJS();
  JSWS();
end;

procedure TfrmTatClothInfoInput.CYMFExit(Sender: TObject);
begin
//  if pos('"', trim(CYMF.Text)) = 0 then
//  begin
//    if pos('CM', trim(CYMF.Text)) = 0 then
//      CYMF.Text := trim(CYMF.Text) + 'CM';
//  end;

end;

procedure TfrmTatClothInfoInput.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
//  LaYangDate.DateTime := SGetServerDateTime(ADOQueryTemp);
end;

procedure TfrmTatClothInfoInput.ToolButton6Click(Sender: TObject);
begin
  if trim(C_Code.Text) = '' then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      FEditAuthority := True;
      fkeyNO := trim(C_Code.Text);
      fType := '成品档案';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;
end;

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

procedure TfrmTatClothInfoInput.ToolButton9Click(Sender: TObject);
var
  i, j: Integer;
begin
  for j := 0 to 5 do
  begin
    i := CDS_4.RecordCount;
    i := i + 1;
    with CDS_4 do
    begin
      Append;
      FieldByName('SSEL').Value := FALSE;
      FieldByName('XuNO').Value := i;
      Post;
    end;
  end;
end;

procedure TfrmTatClothInfoInput.TV1Column10PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  mZ_Number, ZGS: double;
  mYarnFactor, mZS: double;
begin
  if not CDS_1.IsEmpty then
  begin
    CDS_1.First;
    while (CDS_1.FieldByName('Y_Code').asString <> '') and (CDS_1.FieldByName('Y_Code').asString <> null) do
    begin
//      mvalue := TcxTextEdit(Sender).EditingText;
//      FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

      with CDS_1 do
      begin
//        Edit;
//        FieldByName(FFieldName).Value := mvalue;
//        Post;

        if FieldByName('QFYARNQTY').Value = null then
        begin
          ZGS := 0;   //QUANFU根数
        end
        else
        begin
          ZGS := FieldByName('QFYARNQTY').Value;   //全服根数
        end;

        if FieldByName('Z_Number').Value = null then
        begin
          mZ_Number := 0; //支数
        end
        else
        begin
          mZ_Number := FieldByName('Z_Number').Value; //支数
        end;

        if FieldByName('YarnFactor').Value = null then
        begin
          mYarnFactor := 0; //纱线系数
        end
        else
        begin
          mYarnFactor := FieldByName('YarnFactor').Value; //纱线系数
        end;

        mZS := strtofloatdef(ZS.Text, 0);

      end;
      if mZ_Number <> 0 then
      begin
        with CDS_1 do
        begin
          Edit;
          FieldByName('MeterQty').Value := roundfloat((1 + mZS / 100) * ZGS * mYarnFactor / mZ_Number, 2);
          Post;
        end;
      end;
      CDS_1.next;
    end;

  end;
  UpdateC_Code();
end;

procedure TfrmTatClothInfoInput.TV1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  GS, ZGS, MF, DS, WM, TW: double;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

//  if mvalue = '' then
//  begin
//    mvalue := '0';
//  end;
//
//  with CDS_1 do
//  begin
//    Edit;
//    FieldByName(FFieldName).Value := mvalue;
//    Post;
//
//    if FieldByName('YarnQty').Value = null then
//    begin
//      GS := 0;   //根数
//    end
//    else
//    begin
//      GS := FieldByName('YarnQty').Value;   //根数
//    end;
//
//    if FieldByName('CycleYarnQty').Value = null then
//    begin
//      ZGS := 0; //循环根数
//    end
//    else
//    begin
//      ZGS := FieldByName('CycleYarnQty').Value; //循环根数
//    end;
//
//    MF := strtofloatdef(C_ReedWidth.text, 0);    //筘幅
//
//    if FieldByName('TestDenier').Value = null then
//    begin
//      DS := 0;     //D数
//    end
//    else
//    begin
//      DS := FieldByName('TestDenier').Value;     //D数
//    end;
//
//    WM := strtofloatdef(C_FinishLatitudeDensity.text, 0); //纬密
//
//    TW := strtofloatdef(C_YarnQty.text, 0); //头纹
//  end;
//  if ZGS <> 0 then
//  begin
//    with CDS_1 do
//    begin
//      Edit;
//      FieldByName('MeterQty').Value := roundfloat(GS / ZGS * TW * DS / 9000, 2);
//      Post;
//    end;
//  end;
//  JSKZ();
end;

procedure TfrmTatClothInfoInput.Tv2Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  GS, ZGS, MF, DS, WM, fsh: double;
  mYarnFactor: double;
begin

//  mvalue := TcxTextEdit(Sender).EditingText;
//  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if not CDS_2.IsEmpty then
  begin
    CDS_2.First;
    while (CDS_2.FieldByName('Y_Code').asString <> '') and (CDS_2.FieldByName('Y_Code').asString <> null) do
    begin
      with CDS_2 do
      begin
//    Edit;
//    FieldByName(FFieldName).Value := mvalue;
//    Post;

        if FieldByName('YarnQty').Value = null then
        begin
          GS := 0;   //根数
        end
        else
        begin
          GS := FieldByName('YarnQty').Value;   //根数
        end;

        if FieldByName('Y_loss').Value = null then
        begin
          fsh := 0;   //损耗
        end
        else
        begin
          fsh := FieldByName('Y_loss').Value;   //损耗
        end;

        if FieldByName('CycleYarnQty').Value = null then
        begin
          ZGS := 0; //循环根数
        end
        else
        begin
          ZGS := FieldByName('CycleYarnQty').Value; //循环根数
        end;

        MF := strtofloatdef(C_ReedWidth.text, 0);    //筘幅

        if FieldByName('Z_Number').Value = null then
        begin
          DS := 0;     //支
        end
        else
        begin
          DS := FieldByName('Z_Number').Value;     //支数
        end;

        WM := strtofloatdef(C_FinishLatitudeDensity.text, 0); //纬密

        if FieldByName('YarnFactor').Value = null then
        begin
          mYarnFactor := 0; //纱线系数
        end
        else
        begin
          mYarnFactor := FieldByName('YarnFactor').Value; //纱线系数
        end;
      end;
      if (ZGS <> 0) and (DS <> 0) then
      begin
        with CDS_2 do
        begin
          Edit;
          FieldByName('MeterQty').Value := roundfloat(WM * (MF + 4.2) * mYarnFactor / DS * GS / ZGS, 2);
          Post;
        end;
      end;
      CDS_2.Next;
    end;
  end;

  JSKZ();
end;

procedure TfrmTatClothInfoInput.v2Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with self.CDS_2 do
        begin
          Edit;
          FieldByName('Y_Code').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Code').Value;
          FieldByName('Y_Name').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Name').Value;

          FieldByName('Y_Color').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Color').Value;

          FieldByName('FF').Value := frmYarnInfoSel.CDS_1.fieldbyname('FF').Value;
//          FieldByName('ND').Value := frmYarnInfoSel.CDS_1.fieldbyname('ND').Value;
          FieldByName('YarnFactor').Value := 0.059;

//          FieldByName('Brand').Value := frmYarnInfoSel.CDS_1.fieldbyname('Brand').Value;
          FieldByName('Y_Composition').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Composition').Value;
          FieldByName('TestDenier').Value := frmYarnInfoSel.CDS_1.fieldbyname('Denier').Value;
          FieldByName('Z_Number').Value := frmYarnInfoSel.CDS_1.fieldbyname('ZS').Value;
          Post;
          Tv2Column1PropertiesEditValueChanged(Sender);
        end;

        JSJS();
        JSWS();
        JSKZ();
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;

end;

procedure TfrmTatClothInfoInput.WB_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmWBSpecSel := TfrmWBSpecSel.Create(Application);
    with frmWBSpecSel do
    begin
      if ShowModal = 1 then
      begin
        Self.WB_Code.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_Code').AsString);
        Self.WB_Name.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_Name').AsString);

        Self.C_YarnQty.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_YarnQty').AsString);
        Self.C_ReedNo.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_ReedNo').AsString);
        Self.C_ReedWidth.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_Width').AsString);
        DCRQTY.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('DCRQTY').AsString);
        C_KCQTY.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('C_KCQTY').AsString);
        WB_ReedNote.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_ReedNote').AsString);
        BSQTY.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('BSQTY').AsString);
        GKBH.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('GKBH').AsString);

        BCRFQty.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('BCRFQty').AsString);
        WB_YarnKCQty.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_YarnKCQty').AsString);

        with ADOQuery1 do
        begin
          Close;
          sql.Clear;
          sql.Add(' select * from Tat_WB_Formula ');
          sql.Add(' where WBSID=''' + Trim(frmWBSpecSel.CDS_1.fieldbyname('WBSID').AsString) + '''');
          sql.Add(' Order by RowNo ');
          Open;
        end;

        self.CDS_1.EmptyDataSet;
        self.ADOQuery1.First;

        while not ADOQuery1.eof do
        begin
          if (ADOQuery1.FieldByName('RowNo').AsString = 'A') or (ADOQuery1.FieldByName('RowNo').AsString = 'B') or (ADOQuery1.FieldByName('RowNo').AsString = 'C') or (ADOQuery1.FieldByName('RowNo').AsString = 'D') or (ADOQuery1.FieldByName('RowNo').AsString = 'E') or (ADOQuery1.FieldByName('RowNo').AsString = 'F') then
          begin
            with self.CDS_1 do
            begin
              APpend;
              FieldByName('RowNo').Value := ADOQuery1.fieldbyname('RowNo').Value;

              FieldByName('Y_Code').Value := ADOQuery1.fieldbyname('Y_Code').Value;
              FieldByName('Y_Name').Value := ADOQuery1.fieldbyname('Y_Name').Value;

              FieldByName('Y_Color').Value := ADOQuery1.fieldbyname('Y_Color').Value;
              FieldByName('Y_Composition').Value := ADOQuery1.fieldbyname('Y_Composition').Value;

              FieldByName('YarnQty').Value := ADOQuery1.fieldbyname('YarnQty').Value;
              FieldByName('CycleYarnQty').Value := ADOQuery1.fieldbyname('CycleYarnQty').Value;

              FieldByName('YarnFactor').Value := ADOQuery1.fieldbyname('YarnFactor').Value;

              FieldByName('TestDenier').Value := ADOQuery1.fieldbyname('Denier').Value;

              FieldByName('Z_Number').Value := ADOQuery1.fieldbyname('ZS').Value;

              FieldByName('QFYARNQTY').Value := ADOQuery1.fieldbyname('QFYARNQTY').Value;
              FieldByName('MeterQty').Value := ADOQuery1.fieldbyname('MeterQty').Value;

              Post;
            end;
          end;

          ADOQuery1.next;
        end;
        JSJS();
        JSWS();
        JSKZ();
      end;
    end;
  finally
    frmWBSpecSel.Free;
  end;
end;

end.

