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, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, U_BaseInput, System.ImageList,
  Vcl.ImgList, Vcl.Menus, Vcl.DBCtrls, cxGroupBox, cxCheckBox,
  dxScrollbarAnnotations;

type
  TfrmTatPlanInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label2: TLabel;
    OrdDate: TDateTimePicker;
    Label3: TLabel;
    Label14: TLabel;
    ADOQueryTemp: TADOQuery;
    Label18: TLabel;
    Label22: TLabel;
    ADOQueryCmd: TADOQuery;
    Label5: TLabel;
    DlyDate: TDateTimePicker;
    Label8: TLabel;
    Label12: TLabel;
    OrdPieceLen: TEdit;
    OrdPiece: TEdit;
    Label13: TLabel;
    OrdQty: TEdit;
    Label16: TLabel;
    CDS_2: TClientDataSet;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    DS_2: TDataSource;
    Label26: TLabel;
    Label31: TLabel;
    OrdSNote: TRichEdit;
    Label47: TLabel;
    C_GramWeight: TEdit;
    C_Width: TEdit;
    C_Name: TEdit;
    Label43: TLabel;
    lbl2: TLabel;
    CustName: TBtnEditC;
    C_Code: TBtnEditC;
    ConNo: TBtnEditC;
    Saleser: TEdit;
    OurCoName: TBtnEditC;
    Label7: TLabel;
    OrderNo: TEdit;
    C_Spec: TRichEdit;
    OrdType: TComboBox;
    Label4: TLabel;
    FtyCoName: TBtnEditC;
    ToolButton8: TToolButton;
    Label6: TLabel;
    Label9: TLabel;
    C_FinishLatitudeDensity: TEdit;
    C_MachLatitudeDensity: TEdit;
    Label10: TLabel;
    C_FinishLongitudeDensity: TEdit;
    Label11: TLabel;
    C_YarnQty: TEdit;
    Label15: TLabel;
    Label17: TLabel;
    C_LatitudeSpec: TEdit;
    C_LongitudeSpec: TEdit;
    Label19: TLabel;
    C_ReedNo: TEdit;
    Label23: TLabel;
    C_MachLongitudeDensity: TEdit;
    Label24: TLabel;
    C_ReedQty1: TEdit;
    C_ReedQty2: TEdit;
    Label25: TLabel;
    Label27: TLabel;
    C_YarnQty2: TEdit;
    C_YarnQty1: TEdit;
    Label28: TLabel;
    Label29: TLabel;
    C_Width2: TEdit;
    C_Width1: TEdit;
    Label30: TLabel;
    Label32: TLabel;
    C_FinishGramWeight: TEdit;
    Label33: TLabel;
    C_FinishWidth: TEdit;
    PageSetupDialog1: TPageSetupDialog;
    cxPageControl3: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    Panel7: TPanel;
    Label35: TLabel;
    WB_2: TEdit;
    WB_1: TEdit;
    Label34: TLabel;
    WB_4: TEdit;
    WB_3: TEdit;
    Label36: TLabel;
    WB_6: TEdit;
    WB_5: TEdit;
    Label37: TLabel;
    WB_8: TEdit;
    WB_7: TEdit;
    Label38: TLabel;
    WB_10: TEdit;
    WB_9: TEdit;
    Label39: TLabel;
    WB_12: TEdit;
    WB_11: TEdit;
    Label40: TLabel;
    WB_14: TEdit;
    WB_13: TEdit;
    Label41: TLabel;
    WB_16: TEdit;
    WB_15: TEdit;
    Label42: TLabel;
    WB_18: TEdit;
    WB_17: TEdit;
    Label44: TLabel;
    WB_20: TEdit;
    WB_19: TEdit;
    Label45: TLabel;
    WB_22: TEdit;
    WB_21: TEdit;
    Label46: TLabel;
    WB_24: TEdit;
    WB_23: TEdit;
    Label48: TLabel;
    WB_26: TEdit;
    WB_25: TEdit;
    Label49: TLabel;
    WB_28: TEdit;
    WB_27: TEdit;
    Label50: TLabel;
    WB_30: TEdit;
    WB_29: TEdit;
    Label51: TLabel;
    WB_32: TEdit;
    WB_31: TEdit;
    Label52: TLabel;
    WB_34: TEdit;
    WB_33: TEdit;
    Label53: TLabel;
    WB_36: TEdit;
    WB_35: TEdit;
    Label54: TLabel;
    WB_38: TEdit;
    WB_37: TEdit;
    Label55: TLabel;
    WB_40: TEdit;
    WB_39: TEdit;
    Label56: TLabel;
    chuanzong: TRichEdit;
    cxTabSheet3: TcxTabSheet;
    cxTabSheet4: TcxTabSheet;
    Panel1: TPanel;
    cxPageControl2: TcxPageControl;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel3: TPanel;
    Panel4: TPanel;
    Label20: TLabel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton5: TToolButton;
    cxGroupBox1: TcxGroupBox;
    JXPAILIE: TRichEdit;
    TV1Column1: TcxGridDBColumn;
    Panel2: TPanel;
    cxPageControl1: TcxPageControl;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel5: TPanel;
    Panel6: TPanel;
    Label21: TLabel;
    ToolBar3: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton6: TToolButton;
    cxGroupBox2: TcxGroupBox;
    WXPAILIE: TRichEdit;
    TV1Column2: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Label57: TLabel;
    Label58: TLabel;
    Label59: TLabel;
    Label60: TLabel;
    Label61: TLabel;
    WB_42: TEdit;
    WB_41: TEdit;
    WB_44: TEdit;
    WB_43: TEdit;
    WB_46: TEdit;
    WB_45: TEdit;
    WB_48: TEdit;
    WB_47: TEdit;
    WB_50: TEdit;
    WB_49: TEdit;
    Label62: TLabel;
    Label63: TLabel;
    WB_52: TEdit;
    WB_51: TEdit;
    WB_54: TEdit;
    WB_53: TEdit;
    Label64: TLabel;
    Label65: TLabel;
    WB_56: TEdit;
    WB_55: TEdit;
    WB_58: TEdit;
    WB_57: TEdit;
    Label66: TLabel;
    Label67: TLabel;
    Label68: TLabel;
    WB_60: TEdit;
    WB_59: TEdit;
    WB_62: TEdit;
    WB_61: TEdit;
    WB_64: TEdit;
    WB_63: TEdit;
    Label69: TLabel;
    Label70: TLabel;
    WB_66: TEdit;
    WB_65: TEdit;
    WB_68: TEdit;
    WB_67: TEdit;
    Label71: TLabel;
    Label72: TLabel;
    WB_70: TEdit;
    WB_69: TEdit;
    WB_72: TEdit;
    WB_71: TEdit;
    cxTabSheet2: TcxTabSheet;
    Panel8: TPanel;
    Label73: TLabel;
    Edit1: TEdit;
    CDS_3: TClientDataSet;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    DS_3: TDataSource;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    Button1: TButton;
    TV1Column3: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    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 CustNameBtnUpClick(Sender: TObject);
    procedure C_CodeBtnUpClick(Sender: TObject);
    procedure ConNoBtnUpClick(Sender: TObject);
    procedure CustNameBtnDnClick(Sender: TObject);
    procedure OurCoNameBtnUpClick(Sender: TObject);
    procedure FtyCoNameBtnUpClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure cxGridDBColumn4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormCreate(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure N3Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cxGridDBColumn4PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
  private
    FXS, FGridFlag: Integer;
    procedure InitData();
    procedure CALCYarnQty();
    function SaveData(): Boolean;

    procedure InitClothYarn(MC_Code: string);
    { Private declarations }
  public
    PState, FCopyInt: Integer;
    FConNo, FMainId, FSubId: string;
    { Public declarations }
  end;

var
  frmTatPlanInPut: TfrmTatPlanInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_TatClothInfoSel, U_CompanySel, U_YarnInfoSel;

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

  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);

  CALCYarnQty();
end;

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

    end;

  end;
  CDS_3.EnableControls;
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;
begin
  if PState = 1 then
  begin
    ConNo.enabled := false;
  end;

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

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from Tat_Plan_Sub A where A.MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHData(ADOQueryTemp, ScrollBox1, 2);
  SCSHData(ADOQueryTemp, PANEL7, 2);
  JXPAILIE.Text := ADOQueryTemp.FieldByName('JXPAILIE').AsString;
  WXPAILIE.Text := ADOQueryTemp.FieldByName('WXPAILIE').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(FMainId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);
  if ADOQueryTemp.IsEmpty then
  begin
    with CDS_1 do
    begin
      Append;
      FieldByName('Y_NO').Value := 'A';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'B';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'C';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'D';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'E';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'F';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'G';
      Post;
    end;
  end;
  with ADOQueryTemp 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(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
  if ADOQueryTemp.IsEmpty then
  begin
    with CDS_2 do
    begin
      Append;
      FieldByName('Y_NO').Value := 'A';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'B';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'C';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'D';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'E';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'F';
      Post;
      Append;
      FieldByName('Y_NO').Value := 'G';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from tat_Plan_QZ where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_3);
  SInitCDSData(ADOQueryTemp, CDS_3);
end;

procedure TfrmTatPlanInPut.FormCreate(Sender: TObject);
begin
  inherited;
  OrdDate.DateTime := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmTatPlanInPut.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmTatPlanInPut.SaveData(): Boolean;
var
  maxno, maxsubno, maxpbno, maxpzno, maxsjno, QZMAXNO: 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);
      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);

      FieldByName('JXPAILIE').Value := Trim(JXPAILIE.Text);
      FieldByName('WXPAILIE').Value := Trim(WXPAILIE.Text);
      RTSetsavedata(ADOQueryCmd, 'Tat_Plan_Sub', ScrollBox1, 2);
      RTSetsavedata(ADOQueryCmd, 'Tat_Plan_Sub', PANEL7, 2);
      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, Tv2, 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;
//////////////////////////            保存纬丝配比表              //////////////////////////

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

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

//  if Trim(OrdQty.Text) = '' then
//  begin
//    Application.MessageBox('总数量不能为空!', '提示', 0);
//    Exit;
//  end;
//  if Trim(OrdPieceLen.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(OrdPiecelen.Text, FReal) = False then
  begin
    Application.MessageBox('匹长非法数字!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(OrdPiece.Text, FReal) = False then
  begin
    Application.MessageBox('匹数非法数字!', '提示', 0);
    Exit;
  end;

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

procedure TfrmTatPlanInPut.ToolButton1Click(Sender: TObject);
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;
//          FieldByName('Y_Ratio').Value := 0;
          Post;
        end;

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

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

end;

procedure TfrmTatPlanInPut.ToolButton3Click(Sender: TObject);
begin

  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with self.CDS_2 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;
          FieldByName('Y_Ratio').Value := 0;
          Post;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

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

procedure TfrmTatPlanInPut.ToolButton5Click(Sender: TObject);
begin
  OrdDate.SetFocus;
  CALCYarnQty();
end;

procedure TfrmTatPlanInPut.ToolButton6Click(Sender: TObject);
begin
  OrdDate.SetFocus;
  CALCYarnQty();
end;

procedure TfrmTatPlanInPut.cxGridDBColumn1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  ToolBar1.SetFocus();
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          with self.CDS_2 do
          begin
            edit;
            FieldByName('Y_Name').Value := self.CDS_2.FieldByName('Y_Name').AsString + frmYarnInfoSel.CDS_1.fieldbyname('Y_Name').Value;
            post;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

procedure TfrmTatPlanInPut.cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMxMoney: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with cds_2 do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;

  with cds_2 do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);

    Post;
  end;
  tv2.Controller.EditingController.ShowEdit();

end;

procedure TfrmTatPlanInPut.cxGridDBColumn4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  ToolBar1.SetFocus();
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          with self.CDS_1 do
          begin
            edit;
            FieldByName('Y_Name').Value := self.CDS_1.FieldByName('Y_Name').AsString + frmYarnInfoSel.CDS_1.fieldbyname('Y_Name').Value;
            post;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

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

procedure TfrmTatPlanInPut.cxGridDBColumn4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMxMoney: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with cds_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;

  with cds_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);

    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

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.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.OurCoNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin
        OurCoName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
        OurCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        FtyCoName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
        FtyCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

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.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.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  i, j: Integer;
begin
  if Key = #13 then
  begin

    begin
      if Edit1.Text <> '' then
      begin
        i := CDS_3.RecordCount;
        i := i + 1;
        with CDS_3 do
        begin
          Append;
          FieldByName('SSEL').Value := FALSE;
          FieldByName('XuNO').Value := i;
          FieldByName('DZZQZGL').Value := Trim(Edit1.Text);
          Post;
        end;
        Edit1.Text := '';
      end;

    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.N1Click(Sender: TObject);
begin
  inherited;
  SelOKNoFiler(tv3, True);
end;

procedure TfrmTatPlanInPut.N2Click(Sender: TObject);
begin
  inherited;
  SelOKNoFiler(tv3, false);
end;

procedure TfrmTatPlanInPut.N3Click(Sender: TObject);
var
  i, j, t, fxh: Integer;
  FQZGLV, t1: string;
begin
  if CDS_3.IsEmpty then
    Exit;
  if CDS_3.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  t1 := '1';
  if InputQuery('复制次数', ' 请输入复制次数到 ', t1) then
  begin

  end
  else
  begin
    exit;
  end;
  t := StrToIntdef(t1, 1);

  CDS_3.DisableControls;
  for j := 0 to t - 1 do
  begin
    with CDS_3 do
    begin
      First;
      while not Eof do
      begin
        if CDS_3.fieldbyname('SSEL').AsBoolean then
        begin
          FQZGLV := CDS_3.FIELDBYNAME('DZZQZGL').AsString;
          fxh := CDS_3.FIELDBYNAME('xuno').AsInteger;
          i := CDS_3.RecordCount;
          i := i + 1;

          with CDS_3 do
          begin
            Append;
            FieldByName('SSEL').Value := FALSE;
            FieldByName('XuNO').Value := i;
            FieldByName('DZZQZGL').Value := FQZGLV;
            Post;
          end;
          CDS_3.locate('XuNO', fxh, []);
        end;
        Next;

        CDS_3.EnableControls;
      end;

    end;

  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.FtyCoNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        FtyCoName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
        FtyCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTatPlanInPut.ConNoBtnUpClick(Sender: TObject);
begin
//  try
//    frmContractListNXSel := TfrmContractListNXSel.Create(Application);
//    with frmContractListNXSel do
//    begin
//      if ShowModal = 1 then
//      begin
//        self.ConNo.Text := frmContractListNXSel.Order_Main.fieldbyname('ConNo').AsString; //合同号
//        self.Custname.Text := frmContractListNXSel.Order_Main.fieldbyname('CustomerNoName').AsString; //客户
//        self.YWY.Text := frmContractListNXSel.Order_Main.fieldbyname('ConPerson1').AsString; //下单员
//        self.KHConNo.Text := frmContractListNXSel.Order_Main.fieldbyname('KHConNo').AsString; //客户
//        self.ConMainId.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('MainId').AsString; //ConMainId
//        self.ConSubId.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('SubID').AsString; //ConSubId
//
//        self.C_Code.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('PrtCode').AsString; //编号
//        self.C_Name.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('PrtCodeName').AsString; //品名
//        self.C_Spec.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('prtspec').AsString;   //规格
//        self.C_Color.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('PRTColor').AsString;   //颜色
//        self.MF.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('prtmf').AsString;    //门幅
//        self.KZ.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('prtkz').AsString;   //克重
//
//
//        self.OrdPiece.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('pishu').AsString;  //匹数
//        self.OrdPieceWeight.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('OrdTare').AsString;  //匹重
//        self.OrdQty.Text := frmContractListNXSel.ClientDataSet2.fieldbyname('PRTOrderQty').AsString;  //总重量
//      end;
//    end;
//  finally
//    frmContractListNXSel.Free;
//  end;

end;

procedure TfrmTatPlanInPut.C_CodeBtnUpClick(Sender: TObject);
begin
  try
    frmTatClothInfoSel := TfrmTatClothInfoSel.Create(Application);
    with frmTatClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
        Self.C_Code.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_Code').AsString;
        Self.C_Name.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_Name').AsString;
        Self.C_Width.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_Width').AsString;
        Self.C_GramWeight.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_GramWeight').AsString;

        Self.C_MachLatitudeDensity.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_MachLatitudeDensity').AsString;

        Self.C_FinishLatitudeDensity.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_FinishLatitudeDensity').AsString;
        Self.C_FinishLongitudeDensity.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_FinishLongitudeDensity').AsString;

        Self.C_LatitudeSpec.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_LatitudeSpec').AsString;
        Self.C_LongitudeSpec.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_LongitudeSpec').AsString;

        Self.C_YarnQty.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_YarnQty').AsString;
        Self.C_ReedNo.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_ReedNo').AsString;

//        InitClothYarn(frmTatClothInfoSel.CDS_1.fieldbyname('C_Code').AsString);
      end;
    end;
  finally
    frmTatClothInfoSel.Free;
  end;
end;

procedure TfrmTatPlanInPut.CustNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).TxtCode := '';
  TBtnEditC(Sender).Text := '';
end;

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

end;

end.

