unit U_ItemizedListInPut;

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, cxCalendar, StrUtils,
  cxCurrencyEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxDropDownEdit,
  RM_Common, RM_Preview, RM_System, RM_Class, RM_GridReport, RM_Dataset, Menus,
  cxSplitter, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCheckBox,
  dxDateRanges, dxBarBuiltInMenu, cxPC, System.ImageList, Vcl.ImgList,
  U_BaseInput, cxImage, cxDBEdit, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdExplicitTLSClientServerBase, IdFTP, ShellAPI,
  dxScrollbarAnnotations, cxImageList, dxSkinsCore, dxSkinsDefaultPainters,
  dxCore, cxDateUtils, Clipbrd, cxDBLookupComboBox, System.JSON, uSZHN_JSON,
  dxCoreGraphics;

type
  TfrmItemizedListInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ADOQueryLabel: TADOQuery;
    ADO_I1: TADOQuery;
    DS_I1: TDataSource;
    ToolButton3: TToolButton;
    IdFTP1: TIdFTP;
    ADO_I2: TADOQuery;
    DS_I2: TDataSource;
    ToolBar2: TToolBar;
    ToolButton4: TToolButton;
    btnAdd: TToolButton;
    ToolButton2: TToolButton;
    Panel2: TPanel;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column11: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    Tv1TISPiece: TcxGridDBColumn;
    Tv1TISQty: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label14: TLabel;
    TIMNo: TcxTextEdit;
    TIMNote: TMemo;
    OopCoName: TcxButtonEdit;
    TIMDate: TcxDateEdit;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ADO_2: TADOQuery;
    ADO_YQ: TADOQuery;
    ADO_1: TADOQuery;
    Label4: TLabel;
    TIMType: TcxComboBox;
    Tv1Column5: TcxGridDBColumn;
    DS_PM: TDataSource;
    ADO_PM: TADOQuery;
    ADO_YS: TADOQuery;
    DS_YS: TDataSource;
    ToolButton5: TToolButton;
    Tv1Column6: TcxGridDBColumn;
    DS_RCPM: TDataSource;
    ADO_RCPM: TADOQuery;
    ToolButton1: TToolButton;
    Tv1Column9: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure HYTTBtnUpClick(Sender: TObject);
    procedure KYTTBtnUpClick(Sender: TObject);
    procedure v1OrderUnitPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TechnicsBtnUpClick(Sender: TObject);
    procedure TechnicsBtnDnClick(Sender: TObject);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure OurCoNameDblClick(Sender: TObject);
    procedure ShippingAddressPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Tv1Column7PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1Column6PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    FSCXDH: string;
    FShowKeyid: integer;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function SavesubData(): Boolean;
    function getTMMNo(fconNo: string): string;
    procedure SetXH();
    procedure initGrid1(MTIMID: string);
    procedure HotyKey(var msg: TMessage); message WM_HOTKEY;    //快捷键
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FTIMID, FFTMMID, FTMMNo, MTMSId: string;
    FName, FColor, FColorNo: string;
    FXS: Integer;
    canshu1: string;
    fFlileFlag: string;
    fordtype: string;
    FSaveFlag: Boolean;
    { Public declarations }
  end;

var
  frmItemizedListInPut: TfrmItemizedListInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RPFun, U_CustomFun, U_RTFun, U_CompanySel,
  U_ClothInfoSel, U_ItemizedListQtyInPut;


{$R *.dfm}
procedure TfrmItemizedListInPut.initGrid1(MTIMID: string);
var
  SqlStr: string;
begin
  SqlStr := ' select *  from Trade_ItemizedList_Sub where TIMID = ' + QUOTEDSTR(Trim(MTIMID));
  InitCDSData(ADO_1, CDS_1, Tv1, SqlStr, '', 'TISID');
end;

procedure TfrmItemizedListInPut.HotyKey(var msg: TMessage);
begin
//  if msg.LParamHi = VK_LEFT then
//  begin
//    tsave.click;
//  end;
//  if msg.LParamHi = VK_UP then
//  begin
//    ToolButton7.click;
//  end;
//  if msg.LParamHi = VK_RIGHT then
//  begin
//    ToolButton8.click;
//  end;
  if msg.LParamHi = VK_DOWN then
  begin
    with TV1.DataController.DataSource.DataSet do
    begin
      Next;
      if Eof then
      begin
        btnAdd.click;
      end;
    end;

  end;
//  if msg.LParamHi = VK_ADD then
//  begin
//    ToolButton3.click;
//  end;
//  if msg.LParamHi = VK_SUBTRACT then
//  begin
//    TClose.click;
//  end;

end;

procedure TfrmItemizedListInPut.SetXH();
var
  i: Integer;
begin
  with CDS_1 do
  begin
    First;
    i := 1;
    while not Eof do
    begin
      with CDS_1 do
      begin
        Edit;
        FieldByName('XHNoTemp').Value := i;
        Post;
      end;
      i := i + 1;
      Next;
    end;
  end;

  with CDS_1 do
  begin
    CDS_1.IndexFieldNames := '';
    First;
    i := 1;
    while not Eof do
    begin
      with CDS_1 do
      begin
        Edit;
        FieldByName('SerialNo').Value := FieldByName('XHNoTemp').AsInteger;
        Post;
      end;
      i := i + 1;
      Next;
    end;
    CDS_1.IndexFieldNames := 'SerialNo';
  end;

end;

function TfrmItemizedListInPut.getTMMNo(fconNo: string): string;
var
  str1: string;
  i: integer;
begin
  str1 := trim(fconNo) + '01';
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select TIMID from Trade_Plan_Main where TIMNo=' + quotedstr(trim(str1)));
    open;
  end;
  if not ADOTemp.IsEmpty then
  begin
    for i := 1 to 99 do
    begin
      str1 := trim(fconNo) + rightstr(inttostr(100 + i), 2);
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select TIMID from Trade_Plan_Main where TIMNo=' + quotedstr(trim(str1)));
        open;
      end;
      if ADOCmd.IsEmpty then
        break;
    end;
  end;
  result := str1;
end;

procedure TfrmItemizedListInPut.TBCloseClick(Sender: TObject);
begin

  ModalResult := 1;

end;

procedure TfrmItemizedListInPut.InitData();
var
  FDate: TDateTime;
begin
  FDate := SGetServerDateTime(ADOTemp);
  TIMDate.Date := FDate;

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Trade_ItemizedList_Main where TIMID=''' + Trim(FTIMID) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 0);
  OopCoName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('CustNo').AsString);
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *  from Trade_ItemizedList_Sub where 1=1 ');
    if PState = 1 then
    begin
      sql.Add(' and TIMID=''' + Trim(FTIMID) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1<>1');
    end;
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_1);
  SInitCDSData(ADOTemp, CDS_1);

  if PState = 0 then
  begin
    TIMDate.Date := SGetServerDate(ADOTemp);
    TIMNo.Text := '';
//    TIMNo.Enabled := True;
  end;
//  if CopyInt = 99 then
//  begin
//    PState := 0;
//    FTIMID := '';
//    TIMNo.Text := '';
////    TIMNo.Enabled := True;
//    TIMDate.Date := FDate;
//    with CDS_1 do
//    begin
//      First;
//      while not Eof do
//      begin
//        Edit;
//        FieldByName('TIMID').Value := '';
//        FieldByName('TMSId').Value := '';
//        Post;
//        Next;
//      end;
//    end;
//
//  end;
end;

procedure TfrmItemizedListInPut.ZDYHelp(FButn: TcxButtonEdit; LType: string);
var
  FType, ZDYName, FText: string;
begin
end;

procedure TfrmItemizedListInPut.FormShow(Sender: TObject);
var
  i: Integer;
begin
  inherited;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' exec P_BS_CloInfo_Hot ');
    ExecSQL;
  end;
  readCxGrid(TRIM(SELF.Caption) + 'Tv1', Tv1, '生产指示单管理');
  readCxGrid(TRIM(SELF.Caption) + 'Tv2', Tv1, '生产指示单管理');
  TIMNo.Text := '';
  fFlileFlag := UserDataFlag + 'HX';
  InitData();
  with ADO_PM do
  begin
    Close;
    sql.Clear;
    sql.Add('  SELECT C_Code,C_Name,qtyunit FROM Bs_Cloth_Info order by  HotQty desc ');
    Open;
  end;
  with ADO_YS do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT A.C_Code,B.C_Color,B.C_ColorNo FROM  Bs_Cloth_Info A inner join BS_Cloth_Info_Sub B on A.BCIID=B.BCIID ');
    sql.Add(' where  1=2  ');
    Open;
  end;

  with ADO_RCPM do
  begin
    Close;
    sql.Clear;
    sql.Add('  SELECT C_GYSNAME FROM BS_Cloth_Info_GYSCP');
    Open;
  end;

  FShowKeyid := GlobalAddAtom('mykey_show');

//  RegisterHotKey(handle, FShowKeyid, 0, VK_LEFT); //左
//  RegisterHotKey(handle, FShowKeyid, 0, VK_UP); //上
//  RegisterHotKey(handle, FShowKeyid, 0, VK_RIGHT); //右
  RegisterHotKey(handle, FShowKeyid, 0, VK_DOWN); //下
//  RegisterHotKey(handle, FShowKeyid, 0, VK_ADD); //+
//  RegisterHotKey(handle, FShowKeyid, 0, VK_SUBTRACT); //-

end;

function TfrmItemizedListInPut.SaveData(): Boolean;
var
  maxno, Smaxno, maxTMMNo, MaxFNo, MFtyPCId, MSJQZ: string;
begin

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select SJQZ=format(SYSDATETIME(),''yyMMddhhmm'')');
    Open;
    MSJQZ := Trim(ADOTemp.FieldByName('SJQZ').AsString);
  end;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FTIMID) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'DD', 'Trade_ItemizedList_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败!');
    end
    else
    begin
      maxno := Trim(FTIMID);
    end;

    if TIMNo.Text = '' then
      TIMNo.Text := maxno;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Trade_ItemizedList_Main where TIMID=''' + Trim(FTIMID) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FTIMID) = '' then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd, 'Trade_ItemizedList_Main', ScrollBox1, 0);
      FieldByName('TIMID').Value := Trim(maxno);
      FieldByName('OopCoNo').Value := Trim(OopCoName.Properties.LookupItems.Text);

      Post;
    end;

/////////////////////               保存子表            ///////////////////////////
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('TISId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, Smaxno, 'JS', 'Trade_ItemizedList_Sub', 4, 1) = False then
            raise Exception.Create('取子流水号失败!');
        end
        else
        begin
          Smaxno := Trim(CDS_1.fieldbyname('TISId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Trade_ItemizedList_Sub where TIMID=''' + Trim(maxno) + '''');
          sql.Add(' and TISId=''' + Trim(Smaxno) + '''');
          Open;
        end;

        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then
            Append
          else
            Edit;

          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Trade_ItemizedList_Sub', 0);
          if Trim(CDS_1.FieldByName('FtyPCId').AsString) = '' then
          begin
            if GetLSNo(adotemp, MFtyPCId, MSJQZ, 'FtyPCId', 3, 0) = False then
              raise Exception.Create('取缸号失败!');
            FieldByName('FtyPCId').Value := MFtyPCId;
          end;
          FieldByName('TIMID').Value := Trim(maxno);
          FieldByName('TISId').Value := Trim(Smaxno);
          Post;
        end;
        Next;
      end;
    end;
/////////////////////               保存子表           ///////////////////////////


    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Trade_ItemizedList_Main where TIMNo=''' + Trim(TIMNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
      raise Exception.Create('订单号重复!');

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.FtyPCId from Trade_ItemizedList_Sub A where TIMID<>''' + Trim(maxno) + '''');
      sql.Add(' and A.FtyPCId in( select X.FtyPCId from Trade_ItemizedList_Sub X where TIMID=''' + Trim(maxno) + ''' and isnull(X.FtyPCId,'''')<>'''' ) ');
      Open;
    end;
    if not ADOCmd.IsEmpty then
      raise Exception.Create(pchar('缸号' + trim(ADOCmd.FieldByName('FtyPCId').AsString) + '重复!'));

    with ADOCmd do
    begin
      Close;
      Sql.Clear;

      Sql.Add('exec P_BS_Cloth_UpdateByItemizedList ');
      Sql.Add(' @TIMId=' + quotedstr(Trim(maxno)));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;

    if ADOCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOCmd.FieldByName('ShowMsg').AsString)));

    ADOCmd.Connection.CommitTrans;

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

function TfrmItemizedListInPut.SavesubData(): Boolean;
var
  maxno, Smaxno, maxTMMNo, MaxFNo, MFtyPCId, MSJQZ: string;
begin

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select SJQZ=format(SYSDATETIME(),''yyMMddhhmm'')');
    Open;
    MSJQZ := Trim(ADOTemp.FieldByName('SJQZ').AsString);
  end;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FTIMID) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'DD', 'Trade_ItemizedList_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败!');
    end
    else
    begin
      maxno := Trim(FTIMID);
    end;

    if TIMNo.Text = '' then
      TIMNo.Text := maxno;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Trade_ItemizedList_Main where TIMID=''' + Trim(FTIMID) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FTIMID) = '' then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd, 'Trade_ItemizedList_Main', ScrollBox1, 0);
      FieldByName('TIMID').Value := Trim(maxno);
      FieldByName('OopCoNo').Value := Trim(OopCoName.Properties.LookupItems.Text);

      Post;
    end;

/////////////////////               保存子表            ///////////////////////////
    with CDS_1 do
    begin
      DisableControls;
//      First;
//      while not Eof do
//      begin
      if Trim(CDS_1.fieldbyname('TISId').AsString) = '' then
      begin
        if GetLSNo(ADOCmd, Smaxno, 'JS', 'Trade_ItemizedList_Sub', 4, 1) = False then
          raise Exception.Create('取子流水号失败!');
      end
      else
      begin
        Smaxno := Trim(CDS_1.fieldbyname('TISId').AsString);
      end;
      with ADOCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from Trade_ItemizedList_Sub where TIMID=''' + Trim(maxno) + '''');
        sql.Add(' and TISId=''' + Trim(Smaxno) + '''');
        Open;
      end;

      with ADOCmd do
      begin
        if ADOCmd.IsEmpty then
          Append
        else
          Edit;

        RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Trade_ItemizedList_Sub', 0);
        if Trim(CDS_1.FieldByName('FtyPCId').AsString) = '' then
        begin
          if GetLSNo(adotemp, MFtyPCId, MSJQZ, 'FtyPCId', 3, 0) = False then
            raise Exception.Create('取缸号失败!');
          FieldByName('FtyPCId').Value := MFtyPCId;
        end;
        FieldByName('TIMID').Value := Trim(maxno);
        FieldByName('TISId').Value := Trim(Smaxno);
        Post;
      end;
//        Next;
//      end;
    end;
/////////////////////               保存子表           ///////////////////////////

    ADOCmd.Connection.CommitTrans;

    FTIMID := Trim(maxno);

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

procedure TfrmItemizedListInPut.TBSaveClick(Sender: TObject);
var
  MAXNO1: string;
begin
  ToolBar1.SetFocus;

  if CDS_1.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_1.Locate('TISUnit', null, []) then
  begin
    Application.MessageBox('单位不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('TISPiece', null, []) then
  begin
    Application.MessageBox('匹数不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('TISPiece', 0, []) then
  begin
    Application.MessageBox('匹数不能0!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('TISQty', 0, []) then
  begin
    Application.MessageBox('数量不能0!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('C_ColorNo', null, []) then
  begin
    Application.MessageBox('色号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('C_Name', null, []) then
  begin
    Application.MessageBox('色布名称不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_1.Locate('Price', null, []) then
  begin
    Application.MessageBox('单价不能为空!', '提示', 0);
    Exit;
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    FSaveFlag := True;
//    with ADOCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('delete Trade_ItemizedList_Sub where ISNULL(TISPiece, 0)+ISNULL(TISQty, 0) =0 and TIMID=' + QuotedStr(Trim(FTIMID)));
//      ExecSQL;
//    end;

//    CDS_1.EmptyDataSet;
//    PState := 0;
//    FTIMID := '';
//    OopCoName.Text := '';
//    TIMNo.Text := '';
//    OrdNote.Text := '';

  end;
end;

procedure TfrmItemizedListInPut.btnAddClick(Sender: TObject);
var
  Smaxno: string;
  MSerialNo: Double;
begin

//  CopyAddRow(Tv1, cds_1);

  if GetLSNo(ADOCmd, Smaxno, 'JS', 'Trade_ItemizedList_Sub', 4, 1) = False then
  begin
    Application.MessageBox('取子流水号失败!', '提示', 0);
    Exit;
  end;

  with cds_1 do
  begin
    DisableControls;
    MSerialNo := FieldByName('SerialNo').AsFloat + 0.1;
    Append;
    FieldByName('SerialNo').Value := MSerialNo;
    FieldByName('TISId').Value := Smaxno;
    FieldByName('Price').Value := 0;
    FieldByName('TISPiece').Value := 0;
    FieldByName('TISQty').Value := 0;
    FieldByName('TISUnit').Value := 'M';
    Post;

    SetXH();
    EnableControls;
  end;

end;

procedure TfrmItemizedListInPut.ToolButton1Click(Sender: TObject);
var
  MFiltration: string;
  MPrintJson, MTISIds: string;
  JSONObject: TJSONObject; // JSON类
begin
//  if FTIMID = '' then
//  begin
//    Application.MessageBox('请先点击保存再点击打印!', '提示', 0);
//    Exit;
//  end;

  if (not FSaveFlag) and (PState = 0) then
  begin
    Application.MessageBox('请先点击保存再点击打印!', '提示', 0);
    Exit;
  end;

  MTISIds := SelCDSKey(CDS_1, ['TISId'])[0];
  with ADOQueryBaseTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select Filtration= SUBSTRING(CAST((select distinct '','' +A.BCIOID from BS_Cloth_IO A    ');
    sql.Add(' where A.IOFlag=''入库''  ');
    sql.Add(' and exists(SELECT * from [dbo].[F_Tool_SplitString](' + quotedstr(MTISIds) + ','','') SP where SP.RTValue=A.TISId )  ');
    sql.Add(' for xml path('''') ) AS VARCHAR(max)) , 2, 8000)');
    open;
  end;

  MFiltration := Trim(ADOQueryBaseTemp.FieldByName('Filtration').AsString);

  MPrintJson := '              {"LMType": "TradeFinishClothInPrt1"    ';
  MPrintJson := MPrintJson + ' ,"PreviewPrint": true      ';
  MPrintJson := MPrintJson + ' ,"PrtArgs": [ { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '"  } ] }    ';
  JSONObject := TJSONObject.ParseJSONValue(FromPrintFr3(Application, PChar(DConString), PChar(MPrintJson))) as TJSONObject;
  if JSONObject.B['succsee'] then
  begin
    try
      ADOCmd.Connection.BeginTrans;
      with ADOCmd do
      begin
        Close;
        Sql.Clear;

        sql.add('update Trade_ItemizedList_Sub set  PrtCount=PrtCount+1');
        sql.add('where  exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(MTISIds) + ','','') X where X.RTVALUE=Trade_ItemizedList_Sub.TISId  )');

        ExecSQL;
      end;

      ADOCmd.Connection.CommitTrans;

    except
      ADOCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

    end;
  end;
end;

procedure TfrmItemizedListInPut.ToolButton2Click(Sender: TObject);
begin
  begin
    if CDS_1.IsEmpty then
      Exit;
    if CDS_1.Locate('SSel', True, []) = False then
    begin
      Application.MessageBox('没有选择数据!', '提示', 0);
      Exit;
    end;

    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    try
      CDS_1.DisableControls;
      ADOCmd.Connection.BeginTrans;
      while CDS_1.Locate('SSel', true, []) do
      begin
        if Trim(CDS_1.fieldbyname('TISId').AsString) <> '' then
        begin
          with ADOCmd do
          begin
            Close;
            Sql.Clear;
            Sql.Add('exec P_Trade_ItemizedList_Sub_Del ');
            Sql.Add(' @TISId=' + quotedstr(trim(CDS_1.FieldByName('TISId').AsString)));
            Sql.Add(',@DCode=' + Quotedstr(DCode));
            Sql.Add(',@DName=' + Quotedstr(DName));
            Open;
          end;

          if ADOCmd.FieldByName('intReturn').AsInteger = -1 then
          begin
            raise Exception.Create(pchar(trim(ADOCmd.FieldByName('ShowMsg').AsString)));
          end;
        end;
        CDS_1.Delete;
      end;
      ADOCmd.Connection.CommitTrans;
      CDS_1.EnableControls;
    except
      ADOCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
      CDS_1.EnableControls;
    end;
  end;

end;

procedure TfrmItemizedListInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(TRIM(SELF.Caption) + 'Tv1', Tv1, '生产指示单管理');
end;

procedure TfrmItemizedListInPut.ToolButton4Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
begin
  inherited;
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin

      if ShowModal = 1 then
      begin

//       while CDS_1.locate('SSel', true, []) do
        begin

          if self.CDS_1.locate('C_Code', trim(CDS_1.Fieldbyname('C_Code').AsString), []) then
          begin
            CDS_1.Edit;
            CDS_1.fieldbyname('SSel').value := false;
//            continue;
          end;

          i := cds_1.RecordCount;

          with CDS_3 do
          begin

            DisableControls;
            First;

            while not eof do
            begin

              if FieldByName('SSEL').AsBoolean then
              begin
                i := i + 1;
                if GetLSNo(ADOTemp, maxno, 'JS', 'Trade_ItemizedList_Sub', 4, 1) = False then
                begin
                  ADOCmd.Connection.RollbackTrans;
                  Application.MessageBox('取子流水号失败！', '提示', 0);
                  Exit;
                end;
                with self.CDS_1 do
                begin
                  append;
                  FieldByName('SSel').Value := false;
                  FieldByName('SerialNo').Value := IntToStr(i);
                  FieldByName('TISId').Value := Trim(maxno);
                  FieldByName('TMSUnit').Value := 'M';
                  Fieldbyname('C_Code').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
                  Fieldbyname('C_Name').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
                  Fieldbyname('C_Spec').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
                  Fieldbyname('C_Composition').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
                  Fieldbyname('C_Width').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
                  Fieldbyname('C_GramWeight').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);

//            Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
//            Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Color').AsString);
//            Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Pattern').AsString);

                  Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_ColorNo').AsString);
                  Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_Color').AsString);
                  Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_Pattern').AsString);
//                  Fieldbyname('C_StyleNo').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_StyleNo').AsString);
//                  Fieldbyname('SerialNo').value := 999;
                end;
              end;
              Next;
            end;
            EnableControls;
          end;

//          with self.CDS_1 do
//          begin
//            append;
//            FieldByName('SSel').Value := false;
//            FieldByName('SerialNo').Value := IntToStr(i);
//            FieldByName('TISId').Value := Trim(maxno);
//            FieldByName('TMSUnit').Value := 'M';
//            Fieldbyname('C_Code').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
//            Fieldbyname('C_Name').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
//            Fieldbyname('C_Spec').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
//            Fieldbyname('C_Composition').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
//            Fieldbyname('C_Width').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
//            Fieldbyname('C_GramWeight').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
//
//            Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
//            Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Color').AsString);
//            Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Pattern').AsString);
//
//          end;
//          CDS_1.Edit;
//          CDS_1.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;

end;

procedure TfrmItemizedListInPut.ToolButton5Click(Sender: TObject);
var
  MFiltration: string;
  MPrintJson: string;
  JSONObject: TJSONObject; // JSON类
begin
//  if FTIMID = '' then
//  begin
//    Application.MessageBox('请先点击保存再点击打印!', '提示', 0);
//    Exit;
//  end;
  if (not FSaveFlag) and (PState = 0) then
  begin
    Application.MessageBox('请先点击保存再点击打印!', '提示', 0);
    Exit;
  end;

  with ADOQueryBaseTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select Filtration= SUBSTRING(CAST((select distinct '','' +A.BCIOID from BS_Cloth_IO A    ');
    sql.Add(' where A.IOFlag=''入库'' and TIMID=  ' + quotedstr(Trim(FTIMID)));
    sql.Add('  for xml path('''') ) AS VARCHAR(max)) , 2, 8000)');
    open;
  end;

  MFiltration := Trim(ADOQueryBaseTemp.FieldByName('Filtration').AsString);

  MPrintJson := '              {"LMType": "TradeFinishClothInPrt1"    ';
  MPrintJson := MPrintJson + ' ,"PreviewPrint": true      ';
  MPrintJson := MPrintJson + ' ,"PrtArgs": [ { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '"  } ] }    ';
  JSONObject := TJSONObject.ParseJSONValue(FromPrintFr3(Application, PChar(DConString), PChar(MPrintJson))) as TJSONObject;
  if JSONObject.B['succsee'] then
  begin
    try
      ADOCmd.Connection.BeginTrans;
      with ADOCmd do
      begin
        Close;
        Sql.Clear;

        sql.add('update Trade_ItemizedList_Sub set  PrtCount=PrtCount+1');
        sql.Add(' where  TIMID=  ' + quotedstr(Trim(FTIMID)));
        ExecSQL;
      end;

      ADOCmd.Connection.CommitTrans;

    except
      ADOCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

    end;
  end;
end;

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

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

end;

procedure TfrmItemizedListInPut.CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      if TIMType.Text = '客户退货' then
        FCoType := '客户'
      else
        FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.OopCoName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.OopCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmItemizedListInPut.N3Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True); //全选
end;

procedure TfrmItemizedListInPut.N4Click(Sender: TObject);
begin
  inherited;
  SelOKNo(CDS_1, False); //全选
end;

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

procedure TfrmItemizedListInPut.v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  mvalues: string;
begin
  mvalues := tv1.Columns[tv1.Controller.FocusedColumn.index].DataBinding.FieldName;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrderUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName(mvalues).Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmItemizedListInPut.v1OrderUnitPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName, YValue: string;
  FDDQty, FCPCDXS, FQty, FDKCQty: Double;
begin
  mvalue := TcxComboBox(Sender).EditingText;                                           //变化后单位
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin

    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    YValue := FieldByName('DDQtyUnit').AsString;                                        //原单位=订单单位
    FDDQty := FieldByName('DDQty').AsFloat;                                            //订单数量
    FCPCDXS := FieldByName('CPCDXS').AsFloat;
    FDKCQty := FieldByName('DKCQty').AsFloat;
    FQty := FieldByName('PRTOrderQty').AsFloat;
  end;
  if mvalue = 'Kg' then
  begin
    if YValue = 'Y' then
    begin
      FQty := FDDQty * 0.9144 / FCPCDXS;
    end;
    if YValue = 'M' then
    begin
      FQty := FDDQty / FCPCDXS;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty;
    end;
  end;

  if mvalue = 'M' then
  begin
    if YValue = 'Y' then
    begin
      FQty := FDDQty * 0.9144;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty * FCPCDXS;
    end;
    if YValue = 'M' then
    begin
      FQty := FDDQty;
    end;
  end;
  if mvalue = 'Y' then
  begin
    if YValue = 'M' then
    begin
      FQty := FDDQty / 0.9144;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty * FCPCDXS / 0.9144;
    end;
    if YValue = 'Y' then
    begin
      FQty := FDDQty;
    end;
  end;

  with CDS_1 do
  begin
    Edit;
    FieldByName('PRTOrderQty').Value := FQty - FDKCQty;
    Post;
  end;

  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmItemizedListInPut.v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PriceUnit';
      flagname := '价格单位';
      if Trim(DParameters1) <> '高权限' then
      begin
        TBAdd.Visible := False;
        TBEdit.Visible := False;
        TBDel.Visible := False;
      end;

      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName('PriceUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmItemizedListInPut.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  MTISId, MQtyUnit, maxno: string;
  AColumn: TcxGridColumn;
begin
  AColumn := ACellViewInfo.Item as TcxGridColumn;

  if (AColumn.Name <> 'Tv1TISPiece') and (AColumn.Name <> 'Tv1TISQty') then
    Exit;

  if Trim(CDS_1.FieldByName('TISUnit').AsString) = '' then
  begin
    Application.MessageBox('单位不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('C_Name', null, []) then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('C_colorNo', null, []) then
  begin
    Application.MessageBox('色号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('C_Color', null, []) then
  begin
    Application.MessageBox('颜色不能为空!', '提示', 0);
    Exit;
  end;
  MTISId := Trim(Self.CDS_1.FieldByName('TISId').AsString);
  MQtyUnit := Trim(Self.CDS_1.FieldByName('TISUnit').AsString);
  FName := CDS_1.FieldByName('C_Name').ASString;
  FColor := CDS_1.FieldByName('C_Color').ASString;
  FColorNo := CDS_1.FieldByName('C_colorNo').ASString;
//  if SavesubData() then
//  begin
  if Trim(FTIMID) = '' then
  begin
    if GetLSNo(ADOCmd, FTIMID, 'DD', 'Trade_ItemizedList_Main', 3, 1) = False then
      raise Exception.Create('取最大号失败!');
  end;
  try
    frmItemizedListQtyInPut := TfrmItemizedListQtyInPut.Create(Application);
    with frmItemizedListQtyInPut do
    begin
      FTIMId := self.FTIMID;
      FTISId := MTISId;
      FQtyUnit := MQtyUnit;
      FName := Self.FName;
      FColor := Self.FColor;
      FColorNo := Self.FColorNo;
      if ShowModal = 1 then
      begin
//          Self.initGrid1(Self.FTIMID);
        with ADOTemp do
        begin
          Close;
          SQL.Clear;
          sql.Add('select  ');
          sql.Add('SubOutQty = sum(isnull( A.Qty,0))   ');
          sql.Add(',SubOutPS = count(isnull( A.TISId,0))   ');
          sql.Add('from BS_Cloth_IO   A  ');
          SQL.Add('where A.TISID =' + QuotedStr(CDS_1.FieldByName('TISId').AsString));
//              ShowMessage(sql.Text);
          open;
        end;
        with self.CDS_1 do
        begin
          edit;
          FieldByName('TISPiece').Value := ADOTemp.FieldByName('SubOutPS').Value;
          FieldByName('TISQty').Value := ADOTemp.FieldByName('SubOutQty').Value;
          post;
        end;
      end;
    end;
  finally
    frmItemizedListQtyInPut.Free;
  end;
//  end;

end;

procedure TfrmItemizedListInPut.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin

      if ShowModal = 1 then
      begin
        with Self.CDS_1 do
        begin
          edit;
          Fieldbyname('C_Code').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
          Fieldbyname('C_Name').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
          Fieldbyname('C_Spec').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
          Fieldbyname('C_Composition').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
          Fieldbyname('C_Width').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
          Fieldbyname('C_GramWeight').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
          Fieldbyname('TISUnit').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('qtyunit').AsString);
          CDS_3.Locate('ssel', True, []);
          Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_ColorNo').AsString);
          Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_Color').AsString);
          Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_Pattern').AsString);
          post;
        end;

      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select * from bs_cloth_info a inner join BS_Cloth_Info_GYSCP b on a.bciid=b.bciid   ');
    sql.Add('where c_code=' + QuotedStr(CDS_1.FieldByName('C_Code').AsString));
    sql.Add('and factoryname=' + QuotedStr(OopCoName.Text));

    open;
  end;
  if ADOTemp.IsEmpty = false then
  begin
    with Self.CDS_1 do
    begin
      edit;
      Fieldbyname('C_Name').AsString := trim(ADOTemp.Fieldbyname('C_GYSNAME').AsString);
      Post;
    end;

  end;

end;

procedure TfrmItemizedListInPut.Tv1Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;

  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    ADO_YS.Locate('C_ColorNo', mvalue, []);
    Edit;
    FieldByName('C_Color').Value := ADO_YS.FieldByName('C_Color').Value;
    Post;
  end;
end;

procedure TfrmItemizedListInPut.Tv1Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;

  with CDS_1 do
  begin

    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    ADO_rcPM.Locate('C_gysName', mvalue, []);
    Edit;

    Post;
  end;

  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select * from bs_cloth_info a inner join BS_Cloth_Info_GYSCP b on a.bciid=b.bciid   ');
    sql.Add('where C_gysName=' + QuotedStr(CDS_1.FieldByName('C_gysName').AsString));
    sql.Add('and factoryname=' + QuotedStr(OopCoName.Text));

    open;
  end;
  if ADOTemp.IsEmpty = false then
  begin
    with Self.CDS_1 do
    begin
      edit;
      Fieldbyname('C_Name').AsString := trim(ADOTemp.Fieldbyname('C_Name').AsString);
      Post;
    end;

  end;
end;

procedure TfrmItemizedListInPut.Tv1Column7PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;

  with CDS_1 do
  begin

    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    ADO_PM.Locate('C_Name', mvalue, []);
    Edit;
    FieldByName('C_Code').Value := ADO_PM.FieldByName('C_Code').Value;
    FieldByName('TISUnit').Value := ADO_PM.FieldByName('qtyunit').Value;
    Post;
  end;

  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select * from bs_cloth_info a inner join BS_Cloth_Info_GYSCP b on a.bciid=b.bciid   ');
    sql.Add('where c_name=' + QuotedStr(CDS_1.FieldByName('C_name').AsString));
    sql.Add('and factoryname=' + QuotedStr(OopCoName.Text));

    open;
  end;
  if ADOTemp.IsEmpty = false then
  begin
    with Self.CDS_1 do
    begin
      edit;
      Fieldbyname('C_GYSNAME').AsString := trim(ADOTemp.Fieldbyname('C_GYSNAME').AsString);
      Post;
    end;

  end;

end;

procedure TfrmItemizedListInPut.Tv1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'qtyunit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        CDS_1.Edit;
        CDS_1.fieldbyname('TISUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        CDS_1.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmItemizedListInPut.Tv1DblClick(Sender: TObject);
var
  MTISId, MQtyUnit: string;
begin
  if Trim(CDS_1.FieldByName('TISUnit').AsString) = '' then
  begin
    Application.MessageBox('单位不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('C_Name', null, []) then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('C_colorNo', null, []) then
  begin
    Application.MessageBox('色号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('C_Color', null, []) then
  begin
    Application.MessageBox('颜色不能为空!', '提示', 0);
    Exit;
  end;
  MTISId := Trim(Self.CDS_1.FieldByName('TISId').AsString);
  MQtyUnit := Trim(Self.CDS_1.FieldByName('TISUnit').AsString);
  FName := CDS_1.FieldByName('C_Name').ASString;
  FColor := CDS_1.FieldByName('C_Color').ASString;
  FColorNo := CDS_1.FieldByName('C_colorNo').ASString;
  if SavesubData() then
  begin

    try
      frmItemizedListQtyInPut := TfrmItemizedListQtyInPut.Create(Application);
      with frmItemizedListQtyInPut do
      begin
        FTIMId := Self.FTIMId;
        FTISId := MTISId;
        FQtyUnit := MQtyUnit;
        FName := Self.FName;
        FColor := Self.FColor;
        FColorNo := Self.FColorNo;
        if ShowModal = 1 then
        begin
          Self.initGrid1(Self.FTIMID);
        end;
      end;
    finally
      frmItemizedListQtyInPut.Free;
    end;
  end;
end;

procedure TfrmItemizedListInPut.Tv1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin

      // 判断按下的是 方向下键 ↓
  if Key = VK_DOWN then
  begin
    // 触发按钮的点击事件（把 Button1 换成你的按钮名称）
    btnAdd.Click;

    // 可选：阻止 TreeView 本身的向下移动（不需要就删掉这行）
    Key := 0;
  end;
end;

procedure TfrmItemizedListInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if not ADO_YS.IsEmpty then
  begin
    if ADO_YS.FieldByName('C_Code').AsString <> CDS_1.FieldByName('C_Code').AsString then
    begin
      with ADO_YS do
      begin
        Close;
        sql.Clear;
        sql.Add(' SELECT A.C_Code,B.C_Color,B.C_ColorNo FROM  Bs_Cloth_Info A inner join BS_Cloth_Info_Sub B on A.BCIID=B.BCIID ');
        sql.Add(' where A.C_Code= ' + QuotedStr(CDS_1.FieldByName('C_Code').AsString));
        Open;
      end;
    end;
  end
  else
  begin
    with ADO_YS do
    begin
      Close;
      sql.Clear;
      sql.Add(' SELECT A.C_Code,B.C_Color,B.C_ColorNo FROM  Bs_Cloth_Info A inner join BS_Cloth_Info_Sub B on A.BCIID=B.BCIID ');
      sql.Add(' where A.C_Code= ' + QuotedStr(CDS_1.FieldByName('C_Code').AsString));
      Open;
    end;
  end;

end;

procedure TfrmItemizedListInPut.v1Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    FieldByName('MGTMMID').Value := '';
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

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

procedure TfrmItemizedListInPut.KYTTBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxTextEdit(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 Trim(flag) = 'OrdDefStr2' then
      begin
        V1Name.Caption := '中文名称';
        V1Note.Caption := '英文名称';
        fnote := True;
      end;
      if ShowModal = 1 then
      begin
        TcxTextEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

