unit U_ShuttleClothSCInPut;

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,RichEdit;

type
  TfrmClothSCInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    DS_YarnJX: TDataSource;
    CDS_YarnJX: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label14: TLabel;
    Label18: TLabel;
    Label22: TLabel;
    Label5: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label13: TLabel;
    Label16: TLabel;
    Label26: TLabel;
    Label31: TLabel;
    Label42: TLabel;
    Label44: TLabel;
    Label45: TLabel;
    Label47: TLabel;
    Label54: TLabel;
    Label43: TLabel;
    QDDate: TDateTimePicker;
    SYRName: TcxButtonEdit;
    DeliveryDate: TDateTimePicker;
    P_Color: TEdit;
    PiQty: TEdit;
    Qty: TEdit;
    LabName: TComboBox;
    KHConNo: TEdit;
    BatchNo: TEdit;
    P_Spec: TRichEdit;
    P_KZ: TEdit;
    P_MF: TEdit;
    P_Code: TcxButtonEdit;
    P_CodeName: TEdit;
    KHName: TcxButtonEdit;
    YWY: TcxButtonEdit;
    Label7: TLabel;
    OrderNo: TEdit;
    ConNo: TcxButtonEdit;
    ConSubId: TEdit;
    Ku_Hao: TEdit;
    Label9: TLabel;
    Ku_NeiQty: TEdit;
    Label15: TLabel;
    Ku_BianQty: TEdit;
    Label17: TLabel;
    MF_Nei: TEdit;
    Label19: TLabel;
    MF_Bian: TEdit;
    Label20: TLabel;
    MF_Wai: TEdit;
    Label23: TLabel;
    JX_Nei: TEdit;
    Label25: TLabel;
    JX_Bian: TEdit;
    Label27: TLabel;
    JX_Wai: TEdit;
    Label21: TLabel;
    Label24: TLabel;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxTabSheet4: TcxTabSheet;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    V2Column1: TcxGridDBColumn;
    CDS_YarnWX: TClientDataSet;
    DS_YarnWX: TDataSource;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ScrollBox2: TScrollBox;
    Label11: TLabel;
    Label28: TLabel;
    Label6: TLabel;
    PiLen: TEdit;
    Label10: TLabel;
    Label12: TLabel;
    SJ_WM: TEdit;
    PB_NW: TEdit;
    Label29: TLabel;
    Label30: TLabel;
    FS_JM: TEdit;
    FS_WM: TEdit;
    Label32: TLabel;
    FS_WF: TEdit;
    Label33: TLabel;
    Label34: TLabel;
    FS_CFen: TEdit;
    Label35: TLabel;
    ConNoType: TComboBox;
    QI_1: TEdit;
    QI_2: TEdit;
    QI_3: TEdit;
    QI_4: TEdit;
    QI_5: TEdit;
    QI_6: TEdit;
    QI_7: TEdit;
    QI_8: TEdit;
    Zong_9: TEdit;
    Zong_10: TEdit;
    Zong_11: TEdit;
    Zong_12: TEdit;
    Zong_13: TEdit;
    Zong_14: TEdit;
    Zong_15: TEdit;
    Zong_16: TEdit;
    QI_17: TEdit;
    QI_18: TEdit;
    QI_19: TEdit;
    QI_20: TEdit;
    QI_21: TEdit;
    QI_22: TEdit;
    QI_23: TEdit;
    QI_24: TEdit;
    Zong_25: TEdit;
    Zong_26: TEdit;
    Zong_27: TEdit;
    Zong_28: TEdit;
    Zong_29: TEdit;
    Zong_30: TEdit;
    Zong_31: TEdit;
    Zong_32: TEdit;
    Qi_33: TEdit;
    Qi_34: TEdit;
    Qi_35: TEdit;
    Qi_36: TEdit;
    Qi_37: TEdit;
    Qi_38: TEdit;
    Qi_39: TEdit;
    Qi_40: TEdit;
    Zong_41: TEdit;
    Zong_42: TEdit;
    Zong_43: TEdit;
    Zong_44: TEdit;
    Zong_45: TEdit;
    Zong_46: TEdit;
    Zong_47: TEdit;
    Zong_48: TEdit;
    cxTabSheet3: TcxTabSheet;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    CDS_QZ: TClientDataSet;
    DS_QZ: TDataSource;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    P_Note: TMemo;
    ChuanZong: TMemo;
    cxTabSheet5: TcxTabSheet;
    ScrollBox3: TScrollBox;
    Y_Note_PLJ: TMemo;
    Y_Note_PLW: TMemo;
    YWBian: TMemo;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure C_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure C_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure KHNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure PiKgQtyChange(Sender: TObject);
    procedure C_CodeKeyPress(Sender: TObject; var Key: Char);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure cxGridDBColumn2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ScrollBox3MouseWheel(Sender: TObject; Shift: TShiftState;
      WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  private
    FXS, Fint,FRcore: Integer;
    procedure InitData();
    function CheckData(): Boolean;
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    procedure RichEditLineSpacing( //设置编辑框的行距
    mRichEdit: TRichEdit; //Rich编辑框
    mLineSpacingRule: Byte //行距 0~2
    );
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, FCopyInt: Integer;
    FMainId, FConNo, PType, FSubId: string;
    { Public declarations }
  end;

var
  frmClothSCInPut: TfrmClothSCInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_ZdyAttachGYS,
  U_CompressionFun, U_FjList_RZ, U_ZdyAttachment, U_CPSel;

{$R *.dfm}

procedure TfrmClothSCInPut.RichEditLineSpacing( //设置编辑框的行距
mRichEdit: TRichEdit; //Rich编辑框
mLineSpacingRule: Byte //行距 0~2
);
var
vParaFormat2: ParaFormat2;
begin
  if not Assigned(mRichEdit) then Exit;
  FillChar(vParaFormat2, SizeOf(vParaFormat2), 0);
  vParaFormat2.cbSize := SizeOf(ParaFormat2);
  vParaFormat2.dwMask := PFM_LINESPACING;
  vParaFormat2.bLineSpacingRule := mLineSpacingRule;
  vParaFormat2.dyLineSpacing := Ord(True);
  mRichEdit.Perform(EM_SETPARAFORMAT, 0, Longint(@vParaFormat2));
end;

function TfrmClothSCInPut.CheckData(): Boolean;
var
  mvalue, FReal: Double;
  maxConNo: string;
begin
  try

    if Trim(ConNo.Text) = '' then
      raise Exception.Create('合同号不能为空!');

    if Trim(OrderNo.Text) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxConNo, Trim(ConNo.Text + 'G'), 'PD_Plan_Main', 2, 0) = False then
      begin
        raise Exception.Create('生成流水号异常！!');
      end;
      OrderNo.Text := maxConNo;
    end;

//    if Trim(p_CodeName.Text) = '' then
//      raise Exception.Create('产品名称不能为空!');

    if Trim(p_Code.Text) = '' then
      raise Exception.Create('产品编号不能为空!');

    if Trim(LabName.Text) = '' then
      raise Exception.Create('标签格式不能为空!');

    if QDDate.Checked = False then
      raise Exception.Create('下单日期不能为空!');

    if Trim(Qty.Text) <> '' then
    begin
      if TryStrToFloat(Qty.Text, FReal) = False then
        raise Exception.Create('订单数量非法数字!');
    end;

    if Trim(PiQty.Text) = '' then
      raise Exception.Create('匹数不能为空!');

    if TryStrToFloat(PiQty.Text, FReal) = False then
      raise Exception.Create('匹数非法数字!');

      if Trim(PiLen.Text) = '' then
      raise Exception.Create('匹长不能为空!');

    if TryStrToFloat(PiLen.Text, FReal) = False then
      raise Exception.Create('匹长非法数字!');


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

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

procedure TfrmClothSCInPut.InitData();
var
  PBNO, PBFlag: string;
  FXHInt: Integer;
begin

/////////////////刷新产品信息/////////////////////////
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.* from PD_Plan_Main A inner join PD_Plan_Sub B on A.MainId=B.MainId');
    if PState = 1 then
    begin
      sql.Add(' where A.MainId=' + Quotedstr(Trim(FMainId)));
    end;
    if PState = 0 then
    begin
      sql.Add('  where 1<>1 ');
    end;
   //ShowMessage(SQL.Text);
    Open;
  end;
  SCSHData(ADOQueryTemp, ScrollBox1, 1);
  SCSHData(ADOQueryTemp, ScrollBox1, 2);
  SCSHData(ADOQueryTemp, ScrollBox2, 2);
  SCSHData(ADOQueryTemp, ScrollBox3, 2);
 /////////////////刷新产品信息/////////////////////////

 /////////////////刷新穿综信息/////////////////////////
// with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from PD_Plan_CZ where MainId=''' + Trim(FMainId) + '''');
//    Open;
//  end;
//  SCreateCDS20(ADOQueryTemp, CDS_Sub);
//  SInitCDSData20(ADOQueryTemp, CDS_Sub);
//   if CDS_Sub.IsEmpty then
//    begin
//      for FXHInt := 1 to 5 do
//      begin
//        with CDS_Sub do
//        begin
//          Append;
//          fieldbyname('XHao').value := FXHInt;
//        end;
//      end;
//    end;
 /////////////////刷新穿综信息/////////////////////////

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

 /////////////////刷新经丝/////////////////////////
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from PD_Plan_Yarn where PYType=''经丝'' and MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_YarnJX);
  SInitCDSData20(ADOQueryTemp, CDS_YarnJX);
  Y_Note_PLJ.Text := Trim(ADOQueryTemp.FieldByName('Y_Note_PL').AsString);
  if PState = 0 then
  begin
    with CDS_YarnJX do
    begin
      Append;
      FieldByName('PYXH').Value := '甲经';
      Post;
      Append;
      FieldByName('PYXH').Value := '乙经';
      Post;
      Append;
      FieldByName('PYXH').Value := '丙经';
      Post;
      Append;
      FieldByName('PYXH').Value := '丁经';
      Post;
      Append;
      FieldByName('PYXH').Value := '戊经';
      Post;
      Append;
      FieldByName('PYXH').Value := '己经';
      Post;
      Append;
      FieldByName('PYXH').Value := '庚经';
      Post;
    end;
  end;
 /////////////////刷新经丝/////////////////////////

 /////////////////刷新纬丝/////////////////////////

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from PD_Plan_Yarn where PYType=''纬丝'' and MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_YarnWX);
  SInitCDSData20(ADOQueryTemp, CDS_YarnWX);
  Y_Note_PLW.Text := Trim(ADOQueryTemp.FieldByName('Y_Note_PL').AsString);
  if PState = 0 then
  begin
    with CDS_YarnWX do
    begin
      Append;
      FieldByName('PYXH').Value := '甲纬';
      Post;
      Append;
      FieldByName('PYXH').Value := '乙纬';
      Post;
      Append;
      FieldByName('PYXH').Value := '丙纬';
      Post;
      Append;
      FieldByName('PYXH').Value := '丁纬';
      Post;
      Append;
      FieldByName('PYXH').Value := '戊纬';
      Post;
      Append;
      FieldByName('PYXH').Value := '己纬';
      Post;
      Append;
      FieldByName('PYXH').Value := '庚纬';
      Post;
    end;
  end;
 /////////////////刷新纬丝/////////////////////////

  if PState = 0 then
  begin
    QDDate.DateTime := SGetServerDate(ADOQueryTemp);
    QDDate.Checked := True;
    DeliveryDate.Date := QDDate.DateTime;
  end;

  if PState = 1 then
  begin
    ConNo.Enabled := False;
    OrderNo.Enabled := False;
  end;

  if FCopyInt = 1 then
  begin
    FMainId := '';
    ConNo.Text := '';
    ConNo.Enabled := True;
    OrderNo.Enabled := True;
    OrderNo.Text := '';
/////////////////清除原料ID/////////////////////////
    CDS_YarnJX.DisableControls;
    with CDS_YarnJX do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        FieldByName('PYId').Value := '';
        Post;
        Next;
      end;
    end;
    CDS_YarnJX.EnableControls;

    CDS_YarnWX.DisableControls;
    with CDS_YarnWX do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        FieldByName('PYId').Value := '';
        Post;
        Next;
      end;
    end;
    CDS_YarnWX.EnableControls;
/////////////////清除原料ID/////////////////////////


  end;
end;

procedure TfrmClothSCInPut.ZDYHelp(FButn: TcxButtonEdit; LType: string);
begin

end;

procedure TfrmClothSCInPut.FormShow(Sender: TObject);

begin
 ToolBar2.Visible:=False;
  InitData();
//  Y_Note_PLW.SelectAll;
//  RichEditLineSpacing(Y_Note_PLW, 0);
//  Y_Note_PLJ.SelectAll;
//  RichEditLineSpacing(Y_Note_PLJ, 0);
//  YWBian.SelectAll;           
//  RichEditLineSpacing(YWBian, 0);
end;

function TfrmClothSCInPut.SaveData(): Boolean;
var
  maxno, maxsubno, maxpbno, maxpzno, maxsjno,czmaxno,Qzmaxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

///////////////////////////////////保存主表//////////////////////////////
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxno, 'SM', 'PD_Plan_Main', 4, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('生成流水号异常！', '提示', 0);
        exit;
      end;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from PD_Plan_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxno);
      RTSetsavedata(ADOQueryCmd, 'PD_Plan_Main', ScrollBox1, 1);
      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      Post;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from PD_Plan_Main where OrderNo=''' + Trim(OrderNo.Text) + '''');
      Open;
    end;
    if ADOQueryTemp.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('织造单号重复!', '提示', 0);
      Exit;
    end;

///////////////////////////////////保存主表//////////////////////////////

///////////////////////////////////保存子表//////////////////////////////
    if Trim(FSubId) = '' then
    begin
      if GetLSNo(ADOQueryTemp, maxsubno, 'SS', 'PD_Plan_Sub', 4, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取子流水号失败！', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxsubno := Trim(FSubId);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from PD_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, 'PD_Plan_Sub', ScrollBox1, 2);
      RTSetsavedata(ADOQueryCmd, 'PD_Plan_Sub', ScrollBox2, 2);
      RTSetsavedata(ADOQueryCmd, 'PD_Plan_Sub', ScrollBox3, 2);
      Post;
    end;
///////////////////////////////////保存子表//////////////////////////////

///////////////////////////////////经丝组合//////////////////////////////
    if FRcore=1 then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete PD_Plan_Yarn where SubId=''' + Trim(maxsubno) + '''');
        ExecSQL;
      end;
    end;

    with CDS_YarnJX do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_YarnJX.fieldbyname('PYId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'PY', 'PD_Plan_Yarn', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('经丝组合流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_YarnJX.fieldbyname('PYId').AsString);
        end;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from PD_Plan_Yarn where ');
          sql.Add('  PYId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_YarnJX.fieldbyname('PYId').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('PYType').Value := '经丝';
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(maxsubno);
          FieldByName('PYID').Value := Trim(maxpbno);
          FieldByName('Y_Note_PL').Value := Trim(Y_Note_PLJ.Text);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_YarnJX, 'PD_Plan_Yarn', 0);
          Post;
        end;
        CDS_YarnJX.Edit;
        CDS_YarnJX.FieldByName('SubId').Value := Trim(maxsubno);
        CDS_YarnJX.FieldByName('MainId').Value := Trim(maxno);
        CDS_YarnJX.FieldByName('PYId').Value := Trim(maxpbno);
        Next;
      end;
    end;
///////////////////////////////////经丝组合//////////////////////////////

///////////////////////////////////纬丝组合//////////////////////////////
    with CDS_YarnWX do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_YarnWX.fieldbyname('PYId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'PY', 'PD_Plan_Yarn', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('纬丝组合流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_YarnWX.fieldbyname('PYId').AsString);
        end;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from PD_Plan_Yarn where ');
          sql.Add('  PYId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_YarnWX.fieldbyname('PYId').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('PYType').Value := '纬丝';
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(maxsubno);
          FieldByName('PYID').Value := Trim(maxpbno);
          FieldByName('Y_Note_PL').Value := Trim(Y_Note_PLW.Text);
          RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_YarnWX, 'PD_Plan_Yarn', 0);
          Post;
        end;
        CDS_YarnJX.Edit;
        CDS_YarnJX.FieldByName('SubId').Value := Trim(maxsubno);
        CDS_YarnJX.FieldByName('MainId').Value := Trim(maxno);
        CDS_YarnJX.FieldByName('PYId').Value := Trim(maxpbno);
        Next;
      end;
    end;
///////////////////////////////////纬丝组合//////////////////////////////

/////////////////////////////////穿综组合//////////////////////////////
//    with CDS_Sub do
//    begin
//      First;
//      while not Eof do
//      begin
//        if Trim(CDS_Sub.fieldbyname('CZID').AsString) = '' then
//        begin
//          if GetLSNo(ADOQueryCmd, czmaxno, 'CZ', 'PD_Plan_CZ', 4, 1) = False then
//          begin
//            ADOQueryCmd.Connection.RollbackTrans;
//            Application.MessageBox('取子流水号失败！', '提示', 0);
//            Exit;
//          end;
//        end
//        else
//        begin
//          czmaxno := Trim(CDS_Sub.fieldbyname('CZID').AsString);
//        end;
//        with ADOQueryCmd do
//        begin
//          Close;
//          SQL.Clear;
//          sql.Add('select * from PD_Plan_CZ where CZID=''' + Trim(czmaxno) + '''');
//          Open;
//        end;
//        with ADOQueryCmd do
//        begin
//          if Trim(CDS_Sub.fieldbyname('CZID').AsString) = '' then
//            Append
//          else
//            Edit;
//          FieldByName('MainId').Value := Trim(maxno);
//          FieldByName('SubId').Value := Trim(maxsubno);
//          FieldByName('CZID').Value := Trim(czmaxno);
//          RTSetSaveDataCDS(ADOQueryCmd, Tv3, CDS_Sub, 'PD_Plan_CZ', 0);
//          Post;
//        end;
//        CDS_Sub.Edit;
//        CDS_Sub.FieldByName('CZID').Value := Trim(czmaxno);
//
//        Next;
//      end;
//    end;
/////////////////////////////////穿综组合//////////////////////////////

///////////////////////////////////起综工艺单//////////////////////////////
    with CDS_QZ do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_QZ.fieldbyname('QZID').AsString) = '' then
        begin
          if GetLSNo(ADOQueryCmd, Qzmaxno, 'QZ', 'PD_Plan_QZ', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Qzmaxno := Trim(CDS_QZ.fieldbyname('QZID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from PD_Plan_QZ where QZID=''' + Trim(Qzmaxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_QZ.fieldbyname('QZID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(maxsubno);
          FieldByName('QZID').Value := Trim(Qzmaxno);
          RTSetSaveDataCDS(ADOQueryCmd, Tv4, CDS_QZ, 'PD_Plan_QZ', 0);
          Post;
        end;
        CDS_QZ.Edit;
        CDS_QZ.FieldByName('QZID').Value := Trim(Qzmaxno);

        Next;
      end;
    end;
///////////////////////////////////起综工艺单//////////////////////////////
    ADOQueryCmd.Connection.CommitTrans;

    FMainId := Trim(maxno);
    FSubId := Trim(maxsubno);
    Result := True;
  except
    Result := False;

    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmClothSCInPut.TBSaveClick(Sender: TObject);
var
  FReal: Double;
  maxConNo: string;
begin
  QDDate.SetFocus;

  if CheckData() = False then
  begin

    exit;
  end;

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

procedure TfrmClothSCInPut.C_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
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 TfrmClothSCInPut.C_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCPSel := TfrmCPSel.Create(Application);
    with frmCPSel do
    begin
      if ShowModal = 1 then
      begin
        p_Code.Text := frmCPSel.CDS_Main.fieldbyname('CYNo').AsString;
        p_CodeName.Text := frmCPSel.CDS_Main.fieldbyname('CYName').AsString;
        p_Spec.Text := frmCPSel.CDS_Main.fieldbyname('CYSpec').AsString;
        p_Color.Text := frmCPSel.CDS_Main.fieldbyname('CYColor').AsString;
        p_MF.Text := frmCPSel.CDS_Main.fieldbyname('CYMF').AsString;
        p_KZ.Text := frmCPSel.CDS_Main.fieldbyname('CYKZ').AsString;

        /////////////////刷新经丝/////////////////////////
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select B.* from CP_YDang A LEFT JOIN CP_Plan_Yarn B ON B.Y_CYID=A.CYID ');
          SQL.Add(' where B.PYType=''经丝'' and A.CYNo=''' + Trim(frmCPSel.CDS_Main.Fieldbyname('CYNo').AsString) + '''');
          
          Open;
        end;
        if not ADOQueryTemp.IsEmpty then
        begin
          SCreateCDS20(ADOQueryTemp, CDS_YarnJX);
          SInitCDSData20(ADOQueryTemp, CDS_YarnJX);
          Y_Note_PLJ.Text := Trim(ADOQueryTemp.FieldByName('Y_Note_PL').AsString);
          if (PState=1) and (FCopyInt<>1)then
          begin
           FRcore:=1;
          end;
        end;


       /////////////////刷新经丝/////////////////////////

       /////////////////刷新纬丝/////////////////////////
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select B.* from CP_YDang A LEFT JOIN CP_Plan_Yarn B ON B.Y_CYID=A.CYID ');
          SQL.Add(' where B.PYType=''纬丝'' and A.CYNo=''' + Trim(frmCPSel.CDS_Main.Fieldbyname('CYNo').AsString) + '''');
          Open;
        end;
        if not ADOQueryTemp.IsEmpty then
        begin
          SCreateCDS20(ADOQueryTemp, CDS_YarnWX);
          SInitCDSData20(ADOQueryTemp, CDS_YarnWX);
          Y_Note_PLW.Text := Trim(ADOQueryTemp.FieldByName('Y_Note_PL').AsString);
          if (PState=1) and (FCopyInt<>1)then
          begin
           FRcore:=1;
          end;
        end;

       /////////////////刷新纬丝/////////////////////////

        /////////////////清除原料ID/////////////////////////

          CDS_YarnJX.DisableControls;
          with CDS_YarnJX do
          begin
            First;
            while not Eof do
            begin
              Edit;
              FieldByName('MainId').Value := '';
              FieldByName('SubId').Value :='';
              FieldByName('PYId').Value := '';
              Post;
              Next;
            end;
          end;
          CDS_YarnJX.EnableControls;

          CDS_YarnWX.DisableControls;
          with CDS_YarnWX do
          begin
            First;
            while not Eof do
            begin
              Edit;
              FieldByName('MainId').Value := '';
              FieldByName('SubId').Value :='';
              FieldByName('PYId').Value := '';
              Post;
              Next;
            end;
          end;
          CDS_YarnWX.EnableControls;
         /////////////////清除原料ID/////////////////////////

      end;
    end;
  finally
    frmCPSel.Free;
  end;
end;

procedure TfrmClothSCInPut.KHNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        KHName.Text := Trim(CDS_HZ.fieldbyname('Name').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmClothSCInPut.PiKgQtyChange(Sender: TObject);
var
  FC_Qty, FPiKgQty: Double;
begin
//  FC_Qty := StrToFloatDef(C_Qty.Text, 0);
//  FPiKgQty := StrToFloatDef(PiKgQty.Text, 0);
//  if FPiKgQty > 0 then
//    PiQty.Text := IntToStr(Ceil(FC_Qty / FPiKgQty))
//  else
//    PiQty.Text := '0';
end;

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

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

procedure TfrmClothSCInPut.cxGridDBColumn2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXYCLName_JS';
      flagname := '经丝名称';
      if ShowModal = 1 then
      begin
        Self.CDS_YarnJX.Edit;
        Self.CDS_YarnJX.FieldByName('Y_Name').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothSCInPut.cxGridDBColumn6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXYCLName_WS';
      flagname := '纬丝名称';
      if ShowModal = 1 then
      begin
        Self.CDS_YarnWX.Edit;
        Self.CDS_YarnWX.FieldByName('Y_Name').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothSCInPut.cxPageControl1Change(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex = 3 then
  begin
    ToolBar2.Visible:=True;
  end
  else
  ToolBar2.Visible:=False;

end;

procedure TfrmClothSCInPut.ScrollBox3MouseWheel(Sender: TObject;
  Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean);
begin
  if WheelDelta < 0 then
    ScrollBox3.Perform(WM_VSCROLL,SB_LINEDOWN,0)
  else
    ScrollBox3.Perform(WM_VSCROLL,SB_LINEUP,0);
end;

end.

