unit U_TradeDeliveryRequestInPut;

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,
  cxDropDownEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu, U_BaseList,
  dxScrollbarAnnotations, dxCore, cxDateUtils, Vcl.Menus, cxButtons, cxCheckBox,
  dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmTradeDeliveryRequestInPut = class(Tform)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label1: TLabel;
    Label12: TLabel;
    Label11: TLabel;
    v1Column1: TcxGridDBColumn;
    cv1Column3: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    dazi1: TcxStyle;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    Label2: TLabel;
    Tv1Column2: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv1Column6: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    ToCoName: TcxButtonEdit;
    DlyDate: TcxDateEdit;
    OutType: TcxComboBox;
    Note: TcxMemo;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    BuyRepresentative: TcxTextEdit;
    BuyAddress: TcxTextEdit;
    Label5: TLabel;
    BuyTel: TcxTextEdit;
    Label6: TLabel;
    InvoiceNo: TcxTextEdit;
    cxButton1: TcxButton;
    Label7: TLabel;
    MainConNo: TcxTextEdit;
    Tv1Column13: TcxGridDBColumn;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    ToolButton6: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    Label8: TLabel;
    OrderHolder: TcxTextEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure JSQtyUnitChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToCoNameDblClick(Sender: TObject);
    procedure ToCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxButton1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
  private
    fuserName: string;
    procedure InitData();
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, CopyInt: Integer;
    FDRMID: string;
    { Public declarations }
  end;

var
  frmTradeDeliveryRequestInPut: TfrmTradeDeliveryRequestInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_CustomFun, U_TradePlanSel,
  U_TradeSalesContractSel, U_CompanySel, U_TradeTotallClothStkSel;

{$R *.dfm}

procedure TfrmTradeDeliveryRequestInPut.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmTradeDeliveryRequestInPut.InitData();
var
  fsj: string;
begin
  if PState = 0 then
  begin
    DlyDate.Date := strtodatetime(Trim(FormatDateTime('yyyy-MM-dd', Now)));
  end;
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*  ');
    sql.Add(' from Trade_Cloth_DR_Sub A');
    if PState = 1 then
    begin
      sql.Add('where DRMID=''' + Trim(FDRMID) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' where 1<>1');
    end;
    Open;
  end;
  SCreateCDS(ADOTemp, Order_Sub);
  SInitCDSData(ADOTemp, Order_Sub);
  SCSHData(ADOTemp, ScrollBox1, 2);
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from Trade_Cloth_DR_Main A');
    sql.Add(' where DRMID=''' + Trim(FDRMID) + ''' ');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 1);
  ToCoName.Properties.LookupItems.Text := ADOTemp.FieldByName('ToCoNo').AsString;
  if CopyInt = 1 then
  begin
    DlyDate.Date := strtodatetime(Trim(FormatDateTime('yyyy-MM-dd', Now)));
    InvoiceNo.Text := '';
    MainConNo.Text := '';
    FDRMID := '';
  end;
end;

procedure TfrmTradeDeliveryRequestInPut.JSQtyUnitChange(Sender: TObject);
begin
  if not Order_Sub.IsEmpty then
  begin

    exit;
  end;
end;

procedure TfrmTradeDeliveryRequestInPut.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Sub, true);
end;

procedure TfrmTradeDeliveryRequestInPut.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Sub, False);
end;

procedure TfrmTradeDeliveryRequestInPut.cxButton1Click(Sender: TObject);
var
  fDate, MaxNo: string;               //InvoiceNo
begin
  fDate := 'UX' + LeftStr(FormatDateTime('yyyy', SGetServerDate(ADOTemp)), 2);
  if GetLSNo(ADOCmd, MaxNo, fDate, 'Trade_Cloth_DR_Main', 2, 1) = False then
    raise Exception.Create('取最大号失败！')
  else
    InvoiceNo.Text := trim(MaxNo);

end;

procedure TfrmTradeDeliveryRequestInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin

  readCxGrid(trim(self.Caption), Tv1, '发货管理');

  InitData();
end;

function TfrmTradeDeliveryRequestInPut.SaveData(): Boolean;
var
  MaxMid, MaxSid: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    /////////////////              保存主表                /////////////////
    if Trim(FDRMID) = '' then
    begin
      if GetLSNo(ADOCmd, MaxMid, 'DM', 'Trade_Cloth_DR_Main', 4, 1) = False then
        raise Exception.Create('取最大号失败！');
    end
    else
    begin
      MaxMid := Trim(FDRMID);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Trade_Cloth_DR_Main where DRMID=''' + Trim(MaxMid) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FDRMID) = '' 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_Cloth_DR_Main', ScrollBox1, 1);

      FieldByName('ToCoNo').Value := Trim(ToCoName.Properties.LookupItems.Text);
      FieldByName('DRMID').Value := Trim(MaxMid);
      FieldByName('Status').Value := '0';
      Post;
    end;
  /////////////////              保存主表                /////////////////
  ///
  /////////////////              保存子表                /////////////////
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('DRSID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, MaxSid, 'DS', 'Trade_Cloth_DR_Sub', 4, 1) = False then
            raise Exception.Create('取子流水号失败！');
        end
        else
        begin
          MaxSid := Trim(Order_Sub.fieldbyname('DRSID').AsString);
        end;

        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Trade_Cloth_DR_Sub ');
          sql.Add(' where DRMID=''' + Trim(MaxMid) + '''');
          sql.Add(' and DRSID=''' + Trim(MaxSid) + '''');
          Open;
          if ADOCmd.IsEmpty then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'Trade_Cloth_DR_Sub', 0);
          FieldByName('DRMID').Value := Trim(MaxMid);
          FieldByName('DRSID').Value := Trim(MaxSid);
//          FieldByName('QtyUnit').Value := QtyUnit.Text;
          FieldByName('MainId').Value := Order_Sub.fieldbyname('MainId').Value;
          FieldByName('SubId').Value := Order_Sub.fieldbyname('SubId').Value;
          FieldByName('ConMId').Value := Order_Sub.fieldbyname('ConMId').Value;
          FieldByName('ConSId').Value := Order_Sub.fieldbyname('ConSId').Value;

          FieldByName('ToConNo').Value := Order_Sub.fieldbyname('ToConNo').Value;
          FieldByName('ToConMId').Value := Order_Sub.fieldbyname('ToConMId').Value;
          FieldByName('ToConSId').Value := Order_Sub.fieldbyname('ToConSId').Value;

          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('DRSID').Value := Trim(MaxSid);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
   /////////////////              保存子表                /////////////////
    FDRMID := Trim(MaxMid);
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    Result := False;
  end;
end;

procedure TfrmTradeDeliveryRequestInPut.TBSaveClick(Sender: TObject);
var
  Freal: Double;
begin
  ToolBar1.SetFocus;

  if Trim(OutType.Text) = '' then
  begin
    Application.MessageBox('出库类型不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(ToCoName.Text) = '' then
  begin
    Application.MessageBox('收获单位不能为空！', '提示', 0);
    Exit;
  end;
  if (Order_Sub.Locate('Piece', null, [])) and (Order_Sub.Locate('Qty', null, [])) = True then
  begin
    Application.MessageBox('细码申请匹数和数量不能全为空！', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

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

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

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeDeliveryRequestInPut.ToolButton1Click(Sender: TObject);
var
  FQtyUnit, FFSubid: string;
begin

  try
    frmTradePlanSel := TfrmTradePlanSel.Create(Application);
    with frmTradePlanSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('OrderNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('Orderno').AsString);
              FieldByName('ConMId').Value := frmTradePlanSel.CDS_1.fieldbyname('ConMId').Value;
              FieldByName('ConSId').Value := frmTradePlanSel.CDS_1.fieldbyname('ConSId').Value;
              FieldByName('MainId').Value := frmTradePlanSel.CDS_1.fieldbyname('MainId').Value;
              FieldByName('SubId').Value := frmTradePlanSel.CDS_1.fieldbyname('SubId').Value;
              FieldByName('C_Code').Value := frmTradePlanSel.CDS_1.fieldbyname('C_Code').Value;
              FieldByName('C_Name').Value := frmTradePlanSel.CDS_1.fieldbyname('C_Name').Value;
              FieldByName('C_Composition').Value := frmTradePlanSel.CDS_1.fieldbyname('C_Composition').Value;
              FieldByName('C_Spec').Value := frmTradePlanSel.CDS_1.fieldbyname('C_Spec').Value;
              FieldByName('C_Width').Value := frmTradePlanSel.CDS_1.fieldbyname('C_Width').Value;
              FieldByName('C_GramWeight').Value := frmTradePlanSel.CDS_1.fieldbyname('C_GramWeight').Value;
              FieldByName('C_Color').Value := frmTradePlanSel.CDS_1.fieldbyname('C_Color').Value;
              FieldByName('C_ColorNo').Value := frmTradePlanSel.CDS_1.fieldbyname('C_ColorNo').Value;
              FieldByName('C_Pattern').Value := frmTradePlanSel.CDS_1.fieldbyname('C_Pattern').Value;
              FieldByName('C_StyleNo').Value := frmTradePlanSel.CDS_1.fieldbyname('C_StyleNo').Value;
            end;
          end;
          CDS_1.Delete;
        end;
      end;
    end;
  finally
    frmTradePlanSel.Free;
  end;
end;

procedure TfrmTradeDeliveryRequestInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  while Order_Sub.Locate('SSel', True, []) do
  begin
    if Trim(Order_Sub.fieldbyname('DRSID').AsString) <> '' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Trade_Cloth_DR_Sub where DRSID=''' + Trim(Order_Sub.fieldbyname('DRSID').AsString) + '''');
        sql.add(' and isnull(DlyPiece,0)>0');
        Open;
      end;
      if ADOTemp.IsEmpty = False then
      begin
        Application.MessageBox('已发货不能删除数据!', '提示', 0);
        exit;
      end;

      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Trade_Cloth_DR_Sub where DRSID=''' + Trim(Order_Sub.fieldbyname('DRSID').AsString) + '''');
        ExecSQL;
      end;
    end;
    Order_Sub.Delete;
  end;
end;

procedure TfrmTradeDeliveryRequestInPut.ToolButton3Click(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
begin
  try
    frmTradeSalesContractSel := TfrmTradeSalesContractSel.Create(Application);
    with frmTradeSalesContractSel do
    begin
      BuyName.Text := Self.ToCoName.Text;
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if not self.Order_Sub.locate('ConSId', trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConSId').AsString), []) then
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('ConNo').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConNo').AsString);
              FieldByName('ConMId').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConMId').AsString);
              FieldByName('ConSId').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('ConSId').Value;
              FieldByName('C_Code').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_Code').Value;
              FieldByName('C_Name').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_Name').Value;
              FieldByName('C_Composition').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_Composition').Value;
              FieldByName('C_Spec').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_Spec').Value;
              FieldByName('C_Width').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_Width').Value;
              FieldByName('C_GramWeight').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_GramWeight').Value;
              FieldByName('C_Color').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_Color').Value;
              FieldByName('C_ColorNo').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_ColorNo').Value;
              FieldByName('C_EColor').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_EColor').Value;
              FieldByName('C_Pattern').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_Pattern').Value;
              FieldByName('C_StyleNo').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_StyleNo').Value;
//              FieldByName('C_ColorDepth').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('C_ColorDepth').Value;
            end;
          end;

          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

      end;
    end;
  finally
    frmTradeSalesContractSel.Free;
  end;
end;

procedure TfrmTradeDeliveryRequestInPut.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '发货管理')
end;

procedure TfrmTradeDeliveryRequestInPut.ToolButton5Click(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
begin
  try
    frmTradeTotallClothStkSel := TfrmTradeTotallClothStkSel.Create(Application);
    with frmTradeTotallClothStkSel do
    begin
      FStkIdS := CustomSelCDSKey(SELF.Order_Sub, ['StkId'])[0];
//      FromConNo.Text := Self.ToCoName.Text;
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if not self.Order_Sub.locate('StkId', trim(frmTradeTotallClothStkSel.CDS_1.Fieldbyname('StkId').AsString), []) then
          begin
//            BuyRepresentative.text := frmTradeTotallClothStkSel.CDS_1.fieldbyname('BuyRepresentative').AsString;
//            BuyAddress.text := frmTradeTotallClothStkSel.CDS_1.fieldbyname('BuyAddress').AsString;
//            BuyTel.text := frmTradeTotallClothStkSel.CDS_1.fieldbyname('BuyTel').AsString;
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('ConNo').Value := Trim(frmTradeTotallClothStkSel.CDS_1.fieldbyname('FromConNo').AsString);
              FieldByName('ConMId').Value := Trim(frmTradeTotallClothStkSel.CDS_1.fieldbyname('FromConMId').AsString);
              FieldByName('ConSId').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('FromConSId').Value;
              FieldByName('ToConNo').Value := Trim(frmTradeTotallClothStkSel.CDS_1.fieldbyname('FromConNo').AsString);
              FieldByName('ToConMId').Value := Trim(frmTradeTotallClothStkSel.CDS_1.fieldbyname('FromConMId').AsString);
              FieldByName('ToConSId').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('FromConSId').Value;
              FieldByName('C_Code').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_Code').Value;
              FieldByName('C_Name').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_Name').Value;
              FieldByName('C_Composition').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_Composition').Value;
              FieldByName('C_Spec').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_Spec').Value;
              FieldByName('C_Width').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_Width').Value;
              FieldByName('C_GramWeight').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_GramWeight').Value;
              FieldByName('C_Color').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_Color').Value;
              FieldByName('C_ColorNo').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_ColorNo').Value;
              FieldByName('C_EColor').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_EColor').Value;
              FieldByName('C_Pattern').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_Pattern').Value;
              FieldByName('C_StyleNo').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('C_StyleNo').Value;
              FieldByName('StkId').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('StkId').Value;
              FieldByName('PieceNo').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('PieceNo').Value;

              FieldByName('CraftCode').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('CraftCode').Value;
              FieldByName('GC_Name').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('GC_Name').Value;
              FieldByName('CraftName').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('CraftName').Value;

              FieldByName('StkPosition').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('StkPosition').Value;
              FieldByName('PieceNo').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('PieceNo').Value;
              FieldByName('FtyPCId').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('FtyPCId').Value;
              FieldByName('BatchNO').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('BatchNO').Value;
              FieldByName('Piece').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('stkPiece').Value;
              FieldByName('Qty').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('stkQty').Value;
              FieldByName('QtyUnit').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('QtyUnit').Value;
              FieldByName('QtyKgGross').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('QtyKgGross').Value;
              FieldByName('QtyKgNet').Value := frmTradeTotallClothStkSel.CDS_1.fieldbyname('QtyKgNet').Value;
              post;
            end;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

      end;
    end;
  finally
    frmTradeTotallClothStkSel.Free;
  end;
end;

procedure TfrmTradeDeliveryRequestInPut.ToolButton6Click(Sender: TObject);
begin
  try
    frmTradeSalesContractSel := TfrmTradeSalesContractSel.Create(Application);
    with frmTradeSalesContractSel do
    begin
      BuyName.Text := Self.ToCoName.Text;
      if ShowModal = 1 then
      begin
        Self.Order_Sub.DisableControls;
        while Self.Order_Sub.locate('SSel', true, []) do
        begin

          with Self.Order_Sub do
          begin
            Edit;
            Fieldbyname('SSel').value := false;
            FieldByName('ToConNo').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConNo').AsString);
            FieldByName('ToConMId').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConMId').AsString);
            FieldByName('ToConSId').Value := frmTradeSalesContractSel.CDS_1.fieldbyname('ConSId').Value;
            post;
          end;

        end;
        Self.Order_Sub.EnableControls;
      end;
    end;
  finally
    frmTradeSalesContractSel.Free;
  end;
end;

procedure TfrmTradeDeliveryRequestInPut.ToolButton7Click(Sender: TObject);
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    Sql.Add(' select A.*  ');
    sql.Add(' from Bs_Contract_Main A   ');
    sql.Add(' where isnull(A.ConNo,'''')=''' + Trim(Order_Sub.FieldByName('ToConNo').AsString) + '''');
    Open;
  end;

  MainConNo.Text := ADOTemp.FieldByName('ConNo').AsString;
  ToCoName.Text := ADOTemp.FieldByName('BuyName').AsString;
  ToCoName.Properties.LookupItems.Text := ADOTemp.FieldByName('BuyNo').AsString;
//  BuyRepresentative.Text := ADOTemp.FieldByName('BuyRepresentative').AsString;
//  BuyTel.Text := ADOTemp.FieldByName('BuyTel').AsString;
//  BuyAddress.Text := ADOTemp.FieldByName('BuyAddress').AsString;
end;

procedure TfrmTradeDeliveryRequestInPut.ToolButton8Click(Sender: TObject);
begin
  CopyAddRow(Tv1, Order_Sub);
end;

end.

