unit U_TatPlanInPut;

interface

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

type
  TfrmTatPlanInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Label5: TLabel;
    Label8: TLabel;
    OrdPiece: TcxTextEdit;
    Label13: TLabel;
    OrdQty: TcxTextEdit;
    CDS_2: TClientDataSet;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    DS_2: TDataSource;
    Label47: TLabel;
    lbl2: TLabel;
    Label7: TLabel;
    OrderNo: TcxTextEdit;
    ToolButton8: TToolButton;
    Label35: TLabel;
    ConMId: TcxTextEdit;
    ConSId: TcxTextEdit;
    cxPageControl3: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    GroupBox1: TGroupBox;
    Y_Note_PLJ: TMemo;
    cxTabSheet2: TcxTabSheet;
    GroupBox2: TGroupBox;
    Y_Note_PLW: TMemo;
    cxTabSheet3: TcxTabSheet;
    Panel1: TPanel;
    cxPageControl2: TcxPageControl;
    Panel2: TPanel;
    cxPageControl1: TcxPageControl;
    DS_QZ: TDataSource;
    CDS_QZ: TClientDataSet;
    OrdType: TcxComboBox;
    DlyDate: TcxDateEdit;
    OrdDate: TcxDateEdit;
    ConNo: TcxButtonEdit;
    CustName: TcxButtonEdit;
    GPM_1: TcxGridPopupMenu;
    GPM_2: TcxGridPopupMenu;
    cxTabSheet4: TcxTabSheet;
    cxTabSheet5: TcxTabSheet;
    CKF: TMemo;
    ADOQuery1: TADOQuery;
    ADOQueryMain: TADOQuery;
    Label3: TLabel;
    Label6: TLabel;
    Label55: TLabel;
    Label9: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    lbl1: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Label32: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    Label36: TLabel;
    Label37: TLabel;
    Label38: TLabel;
    ThroughHealdOrder: TcxMemo;
    C_Name: TcxTextEdit;
    C_YarnQty: TcxTextEdit;
    C_Width: TcxTextEdit;
    C_ReedNo: TcxTextEdit;
    C_GramWeight: TcxTextEdit;
    C_FinishLatitudeDensity: TcxTextEdit;
    WB_Code: TcxButtonEdit;
    WB_Name: TcxTextEdit;
    C_Color: TcxTextEdit;
    C_ColorNo: TcxTextEdit;
    C_Pattern: TcxTextEdit;
    C_StyleNo: TcxTextEdit;
    C_Spec: TcxTextEdit;
    C_Composition: TcxTextEdit;
    MFDW: TcxComboBox;
    C_FinishLongitudeDensity: TcxTextEdit;
    LPDW: TcxComboBox;
    GKBH: TcxTextEdit;
    C_ReedWidth: TcxTextEdit;
    C_KCQTY: TcxTextEdit;
    BSQTY: TcxTextEdit;
    DCRQTY: TcxTextEdit;
    WB_ReedNote: TcxTextEdit;
    ZYQTY: TcxTextEdit;
    WYAMI: TcxTextEdit;
    DAYQTY: TcxTextEdit;
    LBQTY: TcxTextEdit;
    GKDW: TcxComboBox;
    LoomType: TcxComboBox;
    Label39: TLabel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    TV1Column7: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    TV1Y_Composition: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    Tv2Column7: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    Tv2Column13: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    Tv2Column6: TcxGridDBColumn;
    Tv2Column14: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column11: TcxGridDBColumn;
    Tv2Column8: TcxGridDBColumn;
    Tv2Column12: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel5: TPanel;
    Label28: TLabel;
    attname2: TcxButtonEdit;
    cxDBImage2: TcxDBImage;
    Panel4: TPanel;
    Label4: TLabel;
    ATTNAME1: TcxButtonEdit;
    cxDBImage1: TcxDBImage;
    Qry_ImgHX: TADOQuery;
    DS_ImgHX: TDataSource;
    DS_ImgHX2: TDataSource;
    Qry_ImgHX2: TADOQuery;
    C_CODE: TcxButtonEdit;
    Label10: TLabel;
    ZS: TcxTextEdit;
    TV1BatchNO: TcxGridDBColumn;
    Tv2BatchNO: TcxGridDBColumn;
    Label14: TLabel;
    Label19: TLabel;
    TV1YarnFactor: TcxGridDBColumn;
    Tv2YarnFactor: TcxGridDBColumn;
    Label1: TLabel;
    Label29: TLabel;
    Label40: TLabel;
    Label42: TLabel;
    BCRFQty: TcxTextEdit;
    Label41: TLabel;
    Label43: TLabel;
    Label44: TLabel;
    Label45: TLabel;
    Label46: TLabel;
    Label48: TLabel;
    WB_YarnKCQty: TcxTextEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure cxGridDBColumn1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    //procedure OrdPieceLenChange(Sender: TObject);
    //procedure OrdPieceChange(Sender: TObject);
    procedure C_CodeKeyPress(Sender: TObject; var Key: Char);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure MCTypeBtnUpClick(Sender: TObject);
    procedure cxGridDBColumn4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
//    procedure OurCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OurCoNameDblClick(Sender: TObject);
//    procedure FtyCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure C_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton8Click(Sender: TObject);
    //procedure OrdPiecePropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure Tv2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure C_YarnQtyPropertiesEditValueChanged(Sender: TObject);
    procedure WB_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);

    procedure OrdQtyPropertiesChange(Sender: TObject);
    procedure TV1Column14PropertiesEditValueChanged(Sender: TObject);
    procedure C_ReedWidthExit(Sender: TObject);
    procedure ATTNAME1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure attname2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure C_CODEDblClick(Sender: TObject);
    procedure TV1BatchNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv2BatchNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);

  private
    FXS, FGridFlag: Integer;
    FUpdating: Boolean;
    FLastChanged: TObject;
    procedure InitData();
    procedure InitImage();
    procedure Initexcel();
    procedure CALCYarnQty();
    procedure JSJS(); //计算经丝
    procedure JSWS(); //计算纬丝
    procedure JSKZ(); //计算克重
    function SaveData(): Boolean;
    procedure InitClothYarn(MC_Code: string);
    { Private declarations }
  public
    PState, FCopyInt: Integer;
    FConNo, FMainId, FSubId: string;
    fStatus: Integer; //0新增, 1修改
    { Public declarations }
  end;

var
  frmTatPlanInPut: TfrmTatPlanInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_TatClothInfoSel, U_CompanySel, U_YarnInfoSel,
  U_TradeSalesContractSel, U_WBSpecSel, U_TatGYSel, U_PictureUpload,
  U_YarnStkSel;

{$R *.dfm}
procedure TfrmTatPlanInPut.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 TfrmTatPlanInPut.InitClothYarn(MC_Code: string);
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from Bs_Cloth_Info A');
    sql.Add(' where  A.C_Code=''' + Trim(MC_Code) + '''');
//    showMessage(sql.text);
    Open;
  end;
  ConNo.tag := 9;
  OrdPiece.tag := 9;
  OrdQty.tag := 9;
  ConMId.tag := 9;
  ConSId.tag := 9;
//  showMessage(C_GramWeight.text);
  SCSHData(ADOQueryTemp, ScrollBox1, 2);


//  showMessage(C_GramWeight.text);

  ZS.Text := ADOQueryTemp.FieldByName('ZS').AsString;
  attname1.Text := ADOQueryTemp.fieldbyname('attname1').AsString;
  attname2.Text := ADOQueryTemp.fieldbyname('attname2').AsString;
  ConNo.tag := 2;
  OrdPiece.tag := 2;
  OrdQty.tag := 2;
  ConMId.tag := 2;
  ConSId.tag := 2;
//  Self.C_Code.Text := ADOQueryTemp.fieldbyname('C_Code').AsString;
//  Self.C_Name.Text := ADOQueryTemp.fieldbyname('C_Name').AsString;
//  Self.C_Spec.Text := ADOQueryTemp.fieldbyname('C_Spec').AsString;
//  Self.C_FinishLatitudeDensity.Text := ADOQueryTemp.fieldbyname('C_FinishLatitudeDensity').AsString;
//
//  Self.C_GramWeight.Text := ADOQueryTemp.fieldbyname('C_GramWeight').AsString;
//  Self.C_ReedNo.Text := ADOQueryTemp.fieldbyname('C_ReedNo').AsString;
//  Self.C_YarnQty.Text := ADOQueryTemp.fieldbyname('C_YarnQty').AsString;
//
//  Self.LoomType.Text := ADOQueryTemp.fieldbyname('LoomType').AsString;
//  Self.C_ReedWidth.Text := ADOQueryTemp.fieldbyname('C_ReedWidth').AsString;
//
//  Self.C_Color.Text := ADOQueryTemp.fieldbyname('C_Color').AsString;

//  Self.QZF.Text := ADOQueryTemp.fieldbyname('QZF').AsString;
//  Self.CZF.Text := ADOQueryTemp.fieldbyname('CZF').AsString;
//  Self.CKF.Text := ADOQueryTemp.fieldbyname('CKF').AsString;
//  Self.Y_Note_PLJ.Text := ADOQueryTemp.fieldbyname('Y_Note_PLJ').AsString;
//  Self.Y_Note_PLW.Text := ADOQueryTemp.fieldbyname('Y_Note_PLW').AsString;
//
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,MainId=cast('''' as varchar(30)),SubId=cast('''' as varchar(30)) from BS_Cloth_Yarn A inner join Bs_Cloth_Info B on A.BCIID=B.BCIID');
    sql.Add(' where A.YRType=''经丝'' and B.C_Code=''' + Trim(MC_Code) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);
  ClearCDSColumn(CDS_1, ['YRId', 'SubId', 'MainId']);
//
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,MainId=cast('''' as varchar(30)),SubId=cast('''' as varchar(30))   from BS_Cloth_Yarn A inner join Bs_Cloth_Info B on A.BCIID=B.BCIID');
    sql.Add(' where A.YRType=''纬丝'' and B.C_Code=''' + Trim(MC_Code) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
  ClearCDSColumn(CDS_2, ['YRId', 'SubId', 'MainId']);
//
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,MainId=cast('''' as varchar(30)),SubId=cast('''' as varchar(30))   from BS_Cloth_QZ A inner join Bs_Cloth_Info B on A.BCIID=B.BCIID');
    sql.Add(' where B.C_Code=''' + Trim(MC_Code) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_QZ);
  SInitCDSData(ADOQueryTemp, CDS_QZ);
  ClearCDSColumn(CDS_QZ, ['QZID', 'SubId', 'MainId']);
  InitImage();
end;

procedure TfrmTatPlanInPut.ATTNAME1PropertiesButtonClick(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 TfrmTatPlanInPut.attname2PropertiesButtonClick(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
        AttName2.Text := frmPictureUpload.FPictureName;
      end;
    end;
  finally
    frmPictureUpload.free;
  end;
  initimage();
end;

procedure TfrmTatPlanInPut.CALCYarnQty();
var
  MYarnQty, MOrdQty, MWidth, MMachLatitudeDensity: Double;
begin
//  MYarnQty := STRTOFLOATDEF(C_YarnQty.Text, 0);
//  MOrdQty := STRTOFLOATDEF(OrdQty.Text, 0);
//  MWidth := STRTOFLOATDEF(StringReplace(C_Width.Text, 'CM', '', [rfReplaceAll, rfIgnoreCase]), 0);
//  MMachLatitudeDensity := STRTOFLOATDEF(C_MachLatitudeDensity.Text, 0);
//  with CDS_1 do
//  begin
//    DisableControls;
//    First;
//    while not Eof do
//    begin
//      Edit;
//      FieldByName('Y_Qty').Value := MYarnQty * MOrdQty * FieldByName('Y_Ratio').AsFloat / 100 / 9000 * FieldByName('Denier').AsFloat / 1000;
//      Next;
//    end;
//    EnableControls;
//  end;
//
//  with CDS_2 do
//  begin
//    DisableControls;
//    First;
//    while not Eof do
//    begin
//      Edit;
//      FieldByName('Y_Qty').Value := MMachLatitudeDensity * MOrdQty * MWidth * FieldByName('Y_Ratio').AsFloat / 100 / 9000 * FieldByName('Denier').AsFloat / 1000;
//      Next;
//    end;
//    EnableControls;
//  end;

end;

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

procedure TfrmTatPlanInPut.InitData();
var
  PBNO, PBFlag: string;
//  i: Integer;
//  // 用于保存所有列的事件
//  ColumnEvents: array of TNotifyEvent;
begin

//  // 1. 保存 Tv2 所有列的 OnEditValueChanged 事件
//  SetLength(ColumnEvents, Tv2.ColumnCount);
//  for i := 0 to Tv2.ColumnCount - 1 do
//  begin
//    if Assigned(Tv2.Columns[i].Properties) then
//      ColumnEvents[i] := Tv2.Columns[i].Properties.OnEditValueChanged;
//  end;
//
//  // 2. 禁用所有列的事件
//  for i := 0 to Tv2.ColumnCount - 1 do
//  begin
//    if Assigned(Tv2.Columns[i].Properties) then
//      Tv2.Columns[i].Properties.OnEditValueChanged := nil;
//  end;

//  Tv1.BeginUpdate;
//  Tv2.BeginUpdate;

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  from Tat_Plan_Yarn A ');
    sql.Add(' where YRType=''经丝'' and A.MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  from Tat_Plan_Yarn A ');
    sql.Add(' where YRType=''纬丝'' and A.MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryMain, CDS_2);
  SInitCDSData(ADOQueryMain, CDS_2);

  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from Tat_Plan_Main A where A.MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHData(ADOQueryMain, ScrollBox1, 0);

  CustName.Properties.LookupItems.Text := Trim(ADOQueryMain.FieldByName('CustNo').AsString);
  attname1.Text := Trim(ADOQueryMain.FieldByName('attname1').AsString);
  attname2.Text := Trim(ADOQueryMain.FieldByName('attname2').AsString);
  CKF.Text := Trim(ADOQueryMain.FieldByName('CKF').AsString);

  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from Tat_Plan_Sub A where A.MainId=''' + Trim(FMainId) + '''');
    Open;
//    ShowMessage(sql.Text) ;
  end;
  SCSHData(ADOQueryMain, ScrollBox1, 2);
  Y_Note_PLJ.Text := ADOQueryMain.FieldByName('Y_Note_PLJ').AsString;

  Y_Note_PLW.Text := ADOQueryMain.FieldByName('Y_Note_PLW').AsString;


    /////////////////刷新起综工艺单信息/////////////////////////
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Tat_Plan_QZ where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryMain, CDS_QZ);
  SInitCDSData(ADOQueryMain, CDS_QZ);

 /////////////////刷新起综工艺单信息/////////////////////////
  if FMainId = '' 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';
      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;

  end;

  if FCopyInt = 1 then
  begin
    FMainId := '';
    OrderNo.text := '';

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

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

  end;
  if PState = 1 then
  begin
    ConNo.enabled := false;
//    ScrollBox1.Enabled := False;
  end;
  InitImage();
//  for i := 0 to Tv2.ColumnCount - 1 do
//  begin
//    if Assigned(Tv2.Columns[i].Properties) then
//      Tv2.Columns[i].Properties.OnEditValueChanged := ColumnEvents[i];
//  end;
//
//  Tv1.EndUpdate;
//  Tv2.EndUpdate;
end;

procedure TfrmTatPlanInPut.Initexcel();
var
  myStream: TMemoryStream;
begin

//  myStream := TMemoryStream.Create();
//
//  with ADOQueryTemp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add(' select * from BS_Cloth_Info_Excel A INNER JOIN Bs_Cloth_Info B on A.BCIID =B.BCIID where B.C_Code=''' + Trim(C_Code.text) + '''');
//    open;
//  end;
//
//  if not ADOQueryTemp.IsEmpty then
//  begin
//
//    TBlobField(ADOQueryTemp.FieldByName('ExcelData')).SaveToStream(myStream);
//    myStream.Position := 0;
//    dxSpreadSheet1.LoadFromStream(myStream);
//  end
//  else
//  begin
////    dxSpreadSheet1.ClearAll;
//  end;
//
//  myStream.Free;
end;

procedure TfrmTatPlanInPut.FormCreate(Sender: TObject);
var
  FDate: TDateTime;
begin
  FUpdating := False;
  FDate := SGetServerDateTime(ADOQueryTemp);
  OrdDate.Date := FDate;
  DlyDate.Date := FDate;
end;

procedure TfrmTatPlanInPut.FormShow(Sender: TObject);
var
  mSql: string;
begin

  ReadCxGrid('坯布生产计划单录入11', Tv1, '坯布生产管理');
  ReadCxGrid('坯布生产计划单录入22', Tv2, '坯布生产管理');
  mSql := 'select distinct name=A.mctype from Bs_Machine A   ';
  SInitTcxComBoxBySql(ADOQueryTemp, LoomType, false, mSql);
  InitData();
  InitImage();
  JSJS(); //计算经丝
  JSWS(); //计算纬丝
  JSKZ();
//  Initexcel();
end;

function TfrmTatPlanInPut.SaveData(): Boolean;
var
  maxno, maxsubno, maxpbno, maxpzno, maxsjno, maxQZID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxno, 'SZ', 'Tat_Plan_Main', 4, 1) = False then
        raise Exception.Create('生成流水号异常！');

      OrderNo.Text := maxno;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
//////////////////////////            保存主表              //////////////////////////
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Tat_Plan_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('MainId').Value := Trim(maxno);
      FieldByName('attname1').Value := Trim(attname1.text);
      FieldByName('attname2').Value := Trim(attname2.text);
      FieldByName('CKF').Value := Trim(CKF.text);

      FieldByName('custNo').Value := Trim(custName.Properties.LookupItems.Text);
      RTSetsavedata(ADOQueryCmd, 'Tat_Plan_Main', ScrollBox1, 0);
      Post;
    end;
//////////////////////////            保存主表              //////////////////////////
///
//////////////////////////            保存子表              //////////////////////////
    if Trim(FSubId) = '' then
    begin
      if GetLSNo(ADOQueryTemp, maxsubno, 'SZS', 'Tat_Plan_Sub', 4, 1) = False then
        raise Exception.Create('取子流水号失败！');

    end
    else
    begin
      maxsubno := Trim(FSubId);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Tat_Plan_Sub where ');
      sql.Add('  SubId=''' + Trim(maxsubno) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FSubId) = '' then
        Append
      else
        Edit;
      FieldByName('MainId').Value := Trim(maxno);
      FieldByName('SubId').Value := Trim(maxsubno);

      RTSetsavedata(ADOQueryCmd, 'Tat_Plan_Sub', ScrollBox1, 2);
      FieldByName('Y_Note_PLJ').Value := Trim(Y_Note_PLJ.Text);

      FieldByName('Y_Note_PLW').Value := Trim(Y_Note_PLW.Text);

      Post;
    end;
//////////////////////////            保存子表              //////////////////////////
//////////////////////////            保存经丝配比表              //////////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('YRId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'RJ', 'Tat_Plan_Yarn', 4, 1) = False then
            raise Exception.Create('取配比子流水号失败！');

        end
        else
        begin
          maxpbno := Trim(CDS_1.fieldbyname('YRId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Tat_Plan_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('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(maxsubno);
          FieldByName('YRId').Value := Trim(maxpbno);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'Tat_Plan_Yarn', 0);
          Post;
        end;
        CDS_1.Edit;
        CDS_1.FieldByName('SubId').Value := Trim(maxsubno);
        CDS_1.FieldByName('MainId').Value := Trim(maxno);
        CDS_1.FieldByName('YRId').Value := Trim(maxpbno);

        Next;
      end;
    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', 'Tat_Plan_Yarn', 4, 1) = False then
            raise Exception.Create('取配比子流水号失败！');

        end
        else
        begin
          maxpbno := Trim(CDS_2.fieldbyname('YRId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Tat_Plan_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('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(maxsubno);
          FieldByName('YRId').Value := Trim(maxpbno);
          RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_2, 'Tat_Plan_Yarn', 0);
          Post;
        end;
        CDS_2.Edit;
        CDS_2.FieldByName('SubId').Value := Trim(maxsubno);
        CDS_2.FieldByName('MainId').Value := Trim(maxno);
        CDS_2.FieldByName('YRId').Value := Trim(maxpbno);

        Next;
      end;
    end;
//////////////////////////            保存纬丝配比表              //////////////////////////
               //////////////////////////
    ADOQueryCmd.Connection.CommitTrans;
    FMainId := Trim(maxno);
    FSubId := Trim(maxsubno);
    Result := True;
  except
    Result := False;
    CDS_1.EnableControls;
    CDS_2.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

  end;
end;

procedure TfrmTatPlanInPut.TBSaveClick(Sender: TObject);
var
  FReal: Double;
  maxConNo: string;
begin
  OrdDate.SetFocus;
  CALCYarnQty();

  if Trim(CustName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空!', '提示', 0);
    Exit;
  end;

  if Trim(OrdType.Text) = '' then
  begin
    Application.MessageBox('生产类型不能为空!', '提示', 0);
    Exit;
  end;

  if Trim(C_Name.Text) = '' then
  begin
    Application.MessageBox('产品名称不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(C_Code.Text) = '' then
  begin
    Application.MessageBox('产品编号不能为空!', '提示', 0);
    Exit;
  end;

  if Trim(C_Width.Text) = '' then
  begin
    Application.MessageBox('上机门幅不能为空!', '提示', 0);
    Exit;
  end;

  if Trim(OrdQty.Text) = '' then
  begin
    Application.MessageBox('总数量不能为空!', '提示', 0);
    Exit;
  end;

  if Trim(OrdPiece.Text) = '' then
  begin
    Application.MessageBox('匹数不能为空!', '提示', 0);
    Exit;
  end;

  if TryStrToFloat(OrdQty.Text, FReal) = False then
  begin
    Application.MessageBox('总数量非法数字!', '提示', 0);
    Exit;
  end;

  if TryStrToFloat(OrdPiece.Text, FReal) = False 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 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;
//  with ADOQueryTemp do
//  begin
//    close;
//    SQL.Clear;
//    sql.Add('select * from Tat_Plan_Sub where C_Code = ' + QuotedStr(C_Code.Text));
//    Open;
//  end;
//  if (ADOQueryTemp.RecordCount <> 0) and (FStatus = 0) then
//  begin
//    Application.MessageBox('坯布编号重复！', '提示', 0);
//    Exit;
//  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

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

procedure TfrmTatPlanInPut.ToolButton8Click(Sender: TObject);
begin
  WriteCxGrid('坯布生产计划单录入11', Tv1, '坯布生产管理');
  WriteCxGrid('坯布生产计划单录入22', Tv2, '坯布生产管理');
end;

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

    end;

  end;
  CDS_QZ.EnableControls;
end;

procedure TfrmTatPlanInPut.TV1BatchNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmYarnStkSel := TfrmYarnStkSel.Create(Application);
    with frmYarnStkSel do
    begin
      FstkName := '纱线';
      if ShowModal = 1 then
      begin
        frmYarnStkSel.CDS_Main.DisableControls;

        with Self.CDS_1 do
        begin
          edit;
          FieldByName('BatchNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('BatchNo').Value;
        end;

        frmYarnStkSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmYarnStkSel.Free;
  end;

end;

procedure TfrmTatPlanInPut.TV1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  AColumn: TcxGridColumn;
  AFieldName: string;
begin
  if Assigned(ACellViewInfo) then
  begin
    AColumn := ACellViewInfo.Item as TcxGridColumn;
    AFieldName := AColumn.DataBinding.FilterFieldName;
    if AFieldName = 'MeterQty' then
    begin
      TV1Column1.Styles.Header := DataLink_TatPlan.grid_head_blue;
      TV1Column2.Styles.Header := DataLink_TatPlan.grid_head_blue;
      TV1Column6.Styles.Header := DataLink_TatPlan.grid_head_blue;
      TV1Column14.Styles.Header := DataLink_TatPlan.grid_head_blue;
      C_YarnQty.Style.Color := clMoneyGreen;
    end
    else
    begin
      TV1Column1.Styles.Header := nil;
      TV1Column2.Styles.Header := nil;
      TV1Column6.Styles.Header := nil;
      TV1Column14.Styles.Header := nil;
      C_YarnQty.Style.Color := clWhite;
    end;
  end;
end;

procedure TfrmTatPlanInPut.TV1Column14PropertiesEditValueChanged(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 * 0.059 / mZ_Number, 2);
          Post;
        end;
      end;
      CDS_1.next;
    end;

  end;

end;

procedure TfrmTatPlanInPut.TV1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  GS, ZGS, MF, DS, WM, TW, SUNHAO: double;
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('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;
//
//    if FieldByName('SH').Value = null then
//    begin
//      SUNHAO := 0; //损耗
//    end
//    else
//    begin
//      SUNHAO := FieldByName('SH').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 * (1 + SUNHAO / 100) / 9000, 2);
//      Post;
//    end;
//  end;
//  JSKZ();
end;

procedure TfrmTatPlanInPut.Tv2BatchNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmYarnStkSel := TfrmYarnStkSel.Create(Application);
    with frmYarnStkSel do
    begin
      FstkName := '纱线';
      if ShowModal = 1 then
      begin
        frmYarnStkSel.CDS_Main.DisableControls;

        with Self.CDS_1 do
        begin
          edit;
          FieldByName('BatchNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('BatchNo').Value;
        end;

        frmYarnStkSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmYarnStkSel.Free;
  end;

end;

procedure TfrmTatPlanInPut.Tv2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  AColumn: TcxGridColumn;
  AFieldName: string;
begin
  if Assigned(ACellViewInfo) then
  begin
    AColumn := ACellViewInfo.Item as TcxGridColumn;
    AFieldName := AColumn.DataBinding.FilterFieldName;
    if AFieldName = 'MeterQty' then
    begin
      TV2Column1.Styles.Header := DataLink_TatPlan.grid_head_blue;
      TV2Column2.Styles.Header := DataLink_TatPlan.grid_head_blue;
      TV2Column6.Styles.Header := DataLink_TatPlan.grid_head_blue;
      TV2Column14.Styles.Header := DataLink_TatPlan.grid_head_blue;
      C_ReedWidth.Style.Color := clMoneyGreen;
      C_FinishLatitudeDensity.Style.Color := clMoneyGreen;
    end
    else
    begin
      TV2Column1.Styles.Header := nil;
      TV2Column2.Styles.Header := nil;
      TV2Column6.Styles.Header := nil;
      TV2Column14.Styles.Header := nil;
      C_ReedWidth.Style.Color := clWhite;
      C_FinishLatitudeDensity.Style.Color := clWhite;
    end;
  end;
end;

procedure TfrmTatPlanInPut.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);

  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) * 0.059 / DS * GS / ZGS, 2);
      Post;
    end;
  end;
  JSKZ();
end;

procedure TfrmTatPlanInPut.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if (Trim(Self.C_Code.Text) = '') or (Trim(Self.C_Name.text) = '') then
  begin
    ShowMessage('请先选择产品信息');
    Exit;
  end;
  try
    frmTatGYSel := TfrmTatGYSel.Create(Application);
    with frmTatGYSel do
    begin
      C_Code.Text := Self.C_Code.Text;
      C_Name.Text := Self.C_Name.Text;
      if ShowModal = 1 then
      begin

        Self.orderno.Tag := 99;
        Self.conno.Tag := 99;

//        with ADOQueryTemp do
//        begin
//          Close;
//          SQL.Clear;
//          sql.Add('select * from Tat_Plan_Main A where A.MainId=''' + Trim(Self.GYNo.text) + '''');
//          Open;
//        end;
        SCSHData(ADOQueryTemp, ScrollBox1, 0);

//        Self.attname1.Text := Trim(ADOQueryTemp.FieldByName('attname1').AsString);
//        Self.attname2.Text := Trim(ADOQueryTemp.FieldByName('attname2').AsString);
        Self.CKF.Text := Trim(ADOQueryTemp.FieldByName('CKF').AsString);

//        with ADOQueryTemp do
//        begin
//          Close;
//          SQL.Clear;
//          sql.Add('select * from Tat_Plan_Sub A where A.MainId=''' + Trim(Self.GYNo.text) + '''');
//          Open;
//        end;

        SCSHData(ADOQueryTemp, ScrollBox1, 2);

        Self.OrdQty.Text := Trim(ADOQueryTemp.FieldByName('OrdQty').AsString);
        Self.OrdPiece.Text := Trim(ADOQueryTemp.FieldByName('OrdPiece').AsString);

        Self.Y_Note_PLJ.Text := ADOQueryTemp.FieldByName('Y_Note_PLJ').AsString;
        Self.Y_Note_PLW.Text := ADOQueryTemp.FieldByName('Y_Note_PLW').AsString;

//        with ADOQueryTemp do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('select A.*  from Tat_Plan_Yarn A ');
//          sql.Add(' where YRType=''经丝'' and A.MainId=''' + Trim(Self.GYNo.text) + '''');
//          Open;
//        end;
//        SCreateCDS(ADOQueryTemp, self.CDS_1);
//        SInitCDSData(ADOQueryTemp, self.CDS_1);
//
//        with ADOQueryTemp do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('select A.*  from Tat_Plan_Yarn A ');
//          sql.Add(' where YRType=''纬丝'' and A.MainId=''' + Trim(Self.GYNo.text) + '''');
//          Open;
//        end;
//        SCreateCDS(ADOQueryTemp, self.CDS_2);
//        SInitCDSData(ADOQueryTemp, self.CDS_2);

//        Self.OrderNo.text := '';

        with self.CDS_1 do
        begin
          First;
          while not Eof do
          begin
            Edit;
            FieldByName('MainID').Value := '';
            FieldByName('SubID').Value := '';
            FieldByName('YRId').Value := '';
            Post;
            Next;
          end;
        end;

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

      end;
    end;
  finally
    frmTatGYSel.Free;

    Self.orderno.Tag := 0;
    Self.conno.Tag := 2;
  end;
end;

procedure TfrmTatPlanInPut.cxGridDBColumn1PropertiesButtonClick(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('Brand').Value := frmYarnInfoSel.CDS_1.fieldbyname('Brand').Value;
          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('Denier').Value := frmYarnInfoSel.CDS_1.fieldbyname('Denier').Value;
          Post;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

procedure TfrmTatPlanInPut.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_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('Z_Number').Value := frmYarnInfoSel.CDS_1.fieldbyname('ZS').Value;
//          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('YarnFactor').Value := 0.059;

          Post;
        end;

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

procedure TfrmTatPlanInPut.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;
end;

procedure TfrmTatPlanInPut.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);    //筘幅
//
//        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;
end;

//procedure TfrmTatPlanInPut.JSKZ();
//var
//  SumJS, SumWS: double;
//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));
//end;

//procedure TfrmTatPlanInPut.JSKZ();
//var
//  SumJS, SumWS, ReedWidthValue: Double;
//  ErrCode: Integer;
//begin
//  C_GramWeight.SetFocus;
//  SumJS := IfThen(VarIsNull(Tv1.DataController.Summary.FooterSummaryValues[0]),
//                 0,
//                 Tv1.DataController.Summary.FooterSummaryValues[0]);
//
//  SumWS := IfThen(VarIsNull(Tv2.DataController.Summary.FooterSummaryValues[0]),
//                 0,
//                 Tv2.DataController.Summary.FooterSummaryValues[0]);
//  Val(Trim(C_ReedWidth.Text), ReedWidthValue, ErrCode);
//  if (ErrCode = 0) and (ReedWidthValue > 0) then
//    C_GramWeight.Text := FormatFloat('0.00', (SumJS + SumWS) / ReedWidthValue)
//  else
//    C_GramWeight.Text := '0.00';
//end;

procedure TfrmTatPlanInPut.JSKZ();
var
  SumJS, SumWS, ReedWidthValue: Double;
  ErrCode: Integer;
  IsInch: Boolean;
begin
//  C_GramWeight.SetFocus;
//
//  if Tv1.DataController.Summary.FooterSummaryValues[0] <> NULL then
//  begin
//    SumJS := IfThen(VarIsNull(Tv1.DataController.Summary.FooterSummaryValues[0]), 0, Tv1.DataController.Summary.FooterSummaryValues[0]);
//
//  end;
//  if Tv2.DataController.Summary.FooterSummaryValues[0] <> NULL then
//  begin
//    SumWS := IfThen(VarIsNull(Tv2.DataController.Summary.FooterSummaryValues[0]), 0, Tv2.DataController.Summary.FooterSummaryValues[0]);
//  end;
//
//  IsInch := ContainsText(C_ReedWidth.Text, '"');
//  Val(Trim(StringReplace(C_ReedWidth.Text, '"', '', [rfReplaceAll, rfIgnoreCase])), ReedWidthValue, ErrCode);
//  if (ErrCode = 0) and (ReedWidthValue > 0) then
//  begin
//    if IsInch then
//    begin
//      C_GramWeight.Text := FloatToStr(roundfloat(((SumJS + SumWS) / ReedWidthValue * 2.54 * 100), 0));
//    end
//    else
//    begin
//      C_GramWeight.Text := FloatToStr(roundfloat(((SumJS + SumWS) / ReedWidthValue * 100), 0));
//    end;
//
//  end
//  else
//    C_GramWeight.Text := '0.00';
end;

procedure TfrmTatPlanInPut.cxGridDBColumn4PropertiesButtonClick(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
          Append;
          FieldByName('Brand').Value := frmYarnInfoSel.CDS_1.fieldbyname('Brand').Value;
          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('Denier').Value := frmYarnInfoSel.CDS_1.fieldbyname('Denier').Value;

          Post;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

procedure TfrmTatPlanInPut.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_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('Z_Number').Value := frmYarnInfoSel.CDS_1.fieldbyname('ZS').Value;
//          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('YarnFactor').Value := 0.059;

          Post;
        end;

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

procedure TfrmTatPlanInPut.v2Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin

      if ShowModal = 1 then
      begin
        with Self.CDS_1 do
        begin
          Edit;
          FieldByName('GYSName').Value := Trim(CDS_1.fieldbyname('CoName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTatPlanInPut.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);
        Self.DCRQTY.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('DCRQTY').AsString);
        Self.C_KCQTY.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('C_KCQTY').AsString);
        Self.WB_ReedNote.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_ReedNote').AsString);
        Self.BSQTY.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('BSQTY').AsString);
        Self.GKBH.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('GKBH').AsString);
        Self.WB_YarnKCQty.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_YarnKCQty').AsString);
        Self.BCRFQty.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('BCRFQty').AsString);
        Self.Y_Note_PLJ.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_YarnArrNote').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
          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('TestDenier').Value := ADOQuery1.fieldbyname('Denier').Value;

            FieldByName('FF').Value := ADOQuery1.fieldbyname('FF').Value;
            FieldByName('ND').Value := ADOQuery1.fieldbyname('ND').Value;

            FieldByName('YarnFactor').Value := 0.059;
            Post;
          end;

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

//procedure TfrmTatPlanInPut.OrdPieceLenChange(Sender: TObject);
//var
//  FOrdQty, FOrdPieceLen: Double;
//begin
//  FOrdQty := StrToFloatDef(OrdQty.Text, 0);
//  FOrdPieceLen := StrToFloatDef(OrdPieceLen.Text, 0);
//  if FOrdPieceLen > 0 then
//    OrdPiece.Text := IntToStr(Ceil(FOrdQty / FOrdPieceLen));
//end;
//
//procedure TfrmTatPlanInPut.OrdPieceChange(Sender: TObject);
//var
//  FOrdQty, FOrdPieceLen, FOrdPiece: Double;
//begin
//  FOrdPieceLen := StrToFloatDef(OrdPieceLen.Text, 0);
//  FOrdPiece := StrToFloatDef(OrdPiece.Text, 0);
//  if FOrdPiece > 0 then
//    OrdQty.Text := FloatToStr(FOrdPieceLen * FOrdPiece)
//  else
//    OrdQty.Text := '0';
//end;
//
//procedure TfrmTatPlanInPut.OrdPiecePropertiesEditValueChanged(Sender: TObject);
//begin
//  OrdQty.text := floattostr(strtofloatdef(OrdPiece.text, 0) * strtofloatdef(OrdPieceLen.text, 0));
//end;

procedure TfrmTatPlanInPut.OrdQtyPropertiesChange(Sender: TObject);    //zong shu liang
begin
  //UpdateCalculations(Sender);
end;

procedure TfrmTatPlanInPut.OurCoNameDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
  TcxButtonEdit(Sender).Text := '';
end;

procedure TfrmTatPlanInPut.C_CODEDblClick(Sender: TObject);
begin
  C_CODE.Text := '';
end;

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

procedure TfrmTatPlanInPut.C_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmTatClothInfoSel := TfrmTatClothInfoSel.Create(Application);
    with frmTatClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
        InitClothYarn(frmTatClothInfoSel.CDS_1.fieldbyname('C_Code').AsString);
        Initexcel();
      end;
    end;
  finally
    frmTatClothInfoSel.Free;
  end;
end;

procedure TfrmTatPlanInPut.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 TfrmTatPlanInPut.C_YarnQtyPropertiesEditValueChanged(Sender: TObject);
begin
  JSJS();
  JSWS();
  JSKZ();
end;

procedure TfrmTatPlanInPut.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  i, j: Integer;
begin
//  if Key = #13 then
//  begin
//
//    if Edit1.Text <> '' then
//    begin
//      i := CDS_QZ.RecordCount;
//      i := i + 1;
//      with CDS_QZ do
//      begin
//        Append;
//        FieldByName('SSEL').Value := FALSE;
//        FieldByName('XuNO').Value := i;
//        FieldByName('DZZQZGL').Value := Trim(Edit1.Text);
//        Post;
//      end;
//      Edit1.Text := '';
//    end;
//
//  end;
end;

procedure TfrmTatPlanInPut.MCTypeBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TBtnEditC(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
        TBtnEditC(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

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

procedure TfrmTatPlanInPut.ConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  FConMainId, FMPRTCode: string;
begin
  try
    frmTradeSalesContractSel := TfrmTradeSalesContractSel.Create(Application);
    with frmTradeSalesContractSel do
    begin
      fconNo := self.ConNo.Text;
      fC_Code := self.C_Code.Text;
      FLX := '梭织生产单';

      if ShowModal = 1 then
      begin

        self.CustName.Properties.LookupItems.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('BuyNo').AsString);
        self.CustName.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('BuyName').AsString);

        self.ConNo.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConNo').AsString);
        self.ConMId.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConMId').AsString);
        self.ConSId.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConSId').AsString);

        self.C_Code.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Code').AsString);

        self.C_Width.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Width').AsString);

        self.C_GramWeight.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_GramWeight').AsString);

        self.C_Color.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Color').AsString);
        self.OrdQty.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConQty').AsString);
        InitClothYarn(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Code').AsString);
        Initexcel();
      end;
    end;
  finally
    frmTradeSalesContractSel.Free;
  end;
end;

procedure TfrmTatPlanInPut.CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        CustName.Properties.LookupItems.Text := Trim(CDS_1.fieldbyname('CoCode').AsString);
        CustName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

end.

