unit U_TradeMarketInPut_XY;

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,
  cxDBLookupComboBox, cxMRUEdit, dxSkinBasic, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkroom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinOffice2016Colorful, dxSkinOffice2016Dark,
  dxSkinOffice2019Black, dxSkinOffice2019Colorful, dxSkinOffice2019DarkGray,
  dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringtime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinTheBezier, dxSkinValentine,
  dxSkinVisualStudio2013Blue, dxSkinVisualStudio2013Dark,
  dxSkinVisualStudio2013Light, dxSkinVS2010, dxSkinWhiteprint, dxSkinWXI,
  dxSkinXmas2008Blue, dxCoreGraphics;

type
  TfrmTradeMarketInPut_XY = 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;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Panel2: TPanel;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column11: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Panel4: TPanel;
    Panel5: TPanel;
    Label33: TLabel;
    Label38: TLabel;
    QtyMX: TcxTextEdit;
    GroupBox2: TGroupBox;
    cxGrid2: TcxGrid;
    TvMX: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DS_MX: TDataSource;
    CDS_MX: TClientDataSet;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    Label25: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label14: TLabel;
    Label20: TLabel;
    Label37: TLabel;
    Label18: TLabel;
    TMMNo: TcxTextEdit;
    TMMDate: TDateTimePicker;
    DLYDate: TDateTimePicker;
    OrdNote: TMemo;
    OrdType: TComboBox;
    OurCoName: TcxButtonEdit;
    ShippingAddress: TcxButtonEdit;
    ADO_YS: TADOQuery;
    DS_YS: TDataSource;
    DS_RCPM: TDataSource;
    ADO_PM: TADOQuery;
    ADO_RCPM: TADOQuery;
    DS_PM: TDataSource;
    Tv1Column12: TcxGridDBColumn;
    ADO_1: TADOQuery;
    Label4: TLabel;
    BuyOrdNo: TcxTextEdit;
    CustName: TcxMRUEdit;
    CDS_KH: TClientDataSet;
    ADO_KH: TADOQuery;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    GPM_2: TcxGridPopupMenu;
    DS_DanWei: TDataSource;
    ADO_DanWei: TADOQuery;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    Label19: TLabel;
    CONTACT: TcxButtonEdit;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    TBAdd: TToolButton;
    ToolButton6: TToolButton;
    TBEdit: TToolButton;
    Tv1Column23: TcxGridDBColumn;
    ADOConnection1: TADOConnection;
    Label5: TLabel;
    AddressArea: TcxTextEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ScrollBox1Click(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure HYTTBtnUpClick(Sender: TObject);
    procedure KYTTBtnUpClick(Sender: TObject);
    procedure edtGYBtnUpClick(Sender: TObject);
    procedure v1OrderUnitPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxDBImage1DblClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TechnicsBtnUpClick(Sender: TObject);
    procedure TechnicsBtnDnClick(Sender: TObject);
    procedure PackNoteDblClick(Sender: TObject);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure QtyMXKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TvMXCellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure OurCoNameDblClick(Sender: TObject);
    procedure OurCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ShippingAddressPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column7PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column12PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure StkPositionPropertiesButtonClick(Sender: TObject);
    procedure CustNamePropertiesChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure CustNameMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure CustNamePropertiesCloseUp(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure Tv1Column12PropertiesChange(Sender: TObject);
  private
    FSCXDH: string;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function getTMMNo(fconNo: string): string;
    procedure InitImage1();
    procedure InitImage2();
    procedure LookImage(FileName: string);
    procedure InitMXGrid(MTMSId: string);
    procedure AddLine();
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FTMMID, FTMMNo, MTMSId: string;
    FXS: Integer;
    btnFlag: Integer;
    canshu1, canshu4: string;
    fFlileFlag: string;
    fordtype: string;
    FName, FColor, FColorNo: string;
    FTIMID: string;
    FIncludeTax: Double;
    { Public declarations }
  end;

var
  frmTradeMarketInPut_XY: TfrmTradeMarketInPut_XY;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_ZDYHelpSel, U_RTFun, U_CompanySel, U_PictureUpload,
  U_ProgressUpdate, U_ClothInfoSel, U_ComContactSel, U_TradeMarketList,
  U_LabelPrint, U_TradeMarketQtyInPut;


{$R *.dfm}

procedure TfrmTradeMarketInPut_XY.AddLine();
var
  i: Integer;
  maxno: string;
begin

  i := cds_1.RecordCount;
  i := i + 1;

  if GetLSNo(ADOTemp, maxno, 'JS', 'Trade_Market_Sub', 4, 1) = False then
  begin
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  with cds_1 do
  begin
    Append;
    FieldByName('SerialNo').Value := IntToStr(i);
    FieldByName('TMSId').Value := Trim(maxno);
    FieldByName('TMSPiece').Value := 0;
    FieldByName('TMSQty').Value := 0;
    Post;
  end;
end;

procedure TfrmTradeMarketInPut_XY.InitImage1();
begin

end;

procedure TfrmTradeMarketInPut_XY.InitImage2();
begin

end;

function TfrmTradeMarketInPut_XY.getTMMNo(fconNo: string): string;
var
  str1: string;
  i: integer;
begin
  str1 := trim(fconNo) + '01';
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select TMMID from Trade_Plan_Main where TMMNo=' + 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 TMMID from Trade_Plan_Main where TMMNo=' + quotedstr(trim(str1)));
        open;
      end;
      if ADOCmd.IsEmpty then
        break;
    end;
  end;
  result := str1;
end;

procedure TfrmTradeMarketInPut_XY.TBAddClick(Sender: TObject);
begin
 { CDS_1.Close;
  InitData();
  SClearData(ScrollBox1, 2); //清空容器中控件的内容
  CustName.Text := '';
  if CDS_1.IsEmpty then
  begin
    AddLine();
  end;    }
  PState := 0;
  FTMMId := '';
  FormShow(Self);
end;

procedure TfrmTradeMarketInPut_XY.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradeMarketInPut_XY.TBEditClick(Sender: TObject);
begin
//  if TBSave.Visible = False then
//  begin
  PState := 1;
  FTMMId := self.FTMMId;

  TBSave.Enabled := true;
  ScrollBox1.Enabled := True;
  ToolBar2.Enabled := true;
  ToolButton2.Enabled := True;
  ToolButton1.Enabled := True;
  N2.Visible := true;
//    FormShow(Self);
//  end;

end;

procedure TfrmTradeMarketInPut_XY.InitData();
var
  FDate: TDateTime;
begin
  FDate := SGetServerDateTime(ADOTemp);
  TMMDate.DateTime := FDate;
  DlyDate.DateTime := FDate;

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Trade_Market_Main where TMMID=''' + Trim(FTMMID) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 2);
  SCSHData(ADOTemp, ScrollBox1, 5);
  SCSHData(ADOTemp, ScrollBox1, 9);
  SCSHData(ADOTemp, ScrollBox1, 7);
  OurCoName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('OurCoNo').AsString);
  CustName.Hint := Trim(ADOTemp.FieldByName('CustNo').AsString);
  CustName.Text := Trim(ADOTemp.FieldByName('CustName').AsString);

  with ADO_1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,StkQty =(select sum(isnull(X.StkQty,0)) from Bs_Cloth_IO X where X.C_Code=A.C_Code and X.C_Color=A.C_Color AND STKNAME=''新余'')   ');
    SQL.Add(' -ISNULL((select  sum(isnull(X.Qty,0)) from Trade_Market_Sub_Qty X INNER JOIN  Trade_Market_Sub F ON X.TMSID=F.TMSID  INNER JOIN Trade_Market_Main E ON E.TMMID=F.TMMID     ');
    sql.Add('where Store=''新余'' and  E.Status =''10''  AND F.C_Code=A.C_Code and F.C_Color=A.C_Color),0),SubOutQty =(select sum(isnull(X.Qty,0)) from Trade_Market_Sub_Qty X where X.TMSID=A.TMSID  )   ');
    sql.Add(',SubOutPS =(select count(isnull(X.TMSQID,0)) from Trade_Market_Sub_Qty X where X.TMSID=A.TMSID  )   ');
    sql.Add('from Trade_Market_Sub   A  ');
    SQL.adD('where 1=1 ');
    if PState = 1 then
    begin
      sql.Add(' and A.TMMID=''' + Trim(FTMMID) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1<>1');
    end;
//    ShowMessage(SQL.TEXT);
    Open;

  end;
  SCreateCDS(ADO_1, CDS_1);
  SInitCDSData(ADO_1, CDS_1);

  if PState = 0 then
  begin
    TMMDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := TMMDate.DateTime;
    TMMNo.Text := '';
    TMMNo.Enabled := True;
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FTMMID := '';
    TMMNo.Text := '';
    TMMNo.Enabled := True;

    TMMDate.DateTime := FDate;

    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('TMMID').Value := '';
        FieldByName('TMSId').Value := '';
        FieldByName('TMSPiece').Value := 0;
        FieldByName('TMSQty').Value := 0;
//        FieldByName('GangFee').Value := 0;
//        FieldByName('Transfee').Value := 0;
//        FieldByName('Otherfee').Value := 0;
        Post;
        Next;
      end;
    end;

  end;
end;

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

procedure TfrmTradeMarketInPut_XY.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := CAFREE;
end;

procedure TfrmTradeMarketInPut_XY.FormCreate(Sender: TObject);
begin
  inherited;
  OurCoName.Properties.LookupItems.Text := 'W0001';
  OurCoName.Text := '格美';
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
end;

procedure TfrmTradeMarketInPut_XY.FormShow(Sender: TObject);
var
  i: Integer;
begin
  inherited;

  with ADO_KH do
  begin
    close;
    sql.Clear;
    sql.Add(' select CoCode,CoAbbrName from BS_Company A left join BS_Company_contact B on A.COID = B.COID where CoType=''客户'' ');

    if Trim(canshu4) = '新余' then
//      sql.Add(' and Stores=''新余'' ');
      sql.Add(' and stores=''新余'' ');

    Open;

    CustName.Properties.LookupItems.Clear;
    while not Eof do
    begin
      CustName.Properties.LookupItems.Add(Trim(ADO_KH.fieldByName('CoAbbrName').AsString));
      Next;
    end;
    CustName.ItemIndex := -1;

    SCreateCDS(ADO_KH, CDS_KH);
    SInitCDSData(ADO_KH, CDS_KH);
  end;

  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' exec P_BS_CloInfo_Hot ');
    ExecSQL;
  end;
  readCxGrid(TRIM(SELF.Caption), Tv1, '生产指示单管理');
  TMMNo.Text := '';
  fFlileFlag := UserDataFlag + 'HX';
  InitData();

  if CDS_1.IsEmpty then
  begin
    AddLine();
//    AddLine();
//    AddLine();
//    AddLine();
//    AddLine();
  end;

  with ADO_PM do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT distinct A.C_Code,A.C_Name,A.qtyunit,A.HotQty,StkQty = SUM(B.StkQty) ');
    SQL.ADD(' FROM Bs_Cloth_Info A  LEFT JOIN Bs_Cloth_Io B on A.C_Code=B.C_Code');
    sql.add(' where B.ioflag=''入库'' ');
    if Trim(canshu4) = '新余' then
      sql.Add(' and isnull(STKNAME,'''')=''新余'' ');
    SQL.add('group by A.C_Code,A.C_Name,A.qtyunit,A.HotQty');
    SQL.ADD('order by  A.C_Code ');

    Open;
  end;
  with ADO_YS do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT distinct A.C_Code,B.C_Color,B.C_ColorNo,B.referencePrice AS Price  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 distinct C_GYSNAME FROM BS_Cloth_Info_GYSCP ');
    Open;
  end;
  with ADO_DanWei do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT distinct A.C_Code,B.CICUnit  FROM  Bs_Cloth_Info A inner join Bs_Cloth_Info_Coefficient B on A.BCIID=B.BCIID ');
    sql.Add(' where 2=1  ');
    Open;
  end;
//  frmTradeMarketlist := TfrmTradeMarketlist.Create(Application);
end;

function TfrmTradeMarketInPut_XY.SaveData(): Boolean;
var
  maxno, Smaxno, maxTMMNo, MaxFNo, MQZ, MTMMNo: string;
  RetryCount: Integer;
begin
  try
    ADOCmd.Connection.BeginTrans;

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

    if Trim(FTMMID) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'JM', 'Trade_Market_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败!');

//  大货单编号G50313001<-- G50313 001
//(“G”+ 25年的“5”(每过五年辉清一下以前的数据 所以0-9循环)+0313(月日)+“001”(单号,不会超过999单))

//      RetryCount := 0;
//
//  // 循环取号，直到不重复为止
//      while True do
//      begin
//    // 1. 取流水号
//        MQZ := 'G' + RightStr(Trim(FormatDateTime('yyMMdd', SGetServerDate(ADOTemp))), 6);
//        if not GetLSNo(ADOCmd, MTMMNo, MQZ, 'Trade_Market_Main', 3, 0) then
//          raise Exception.Create('取最大号失败!');
//
//    // 2. 查询是否已存在
//        with ADOCmd do
//        begin
//          Close;
//          SQL.Clear;
//          SQL.Add('select count(*) from Trade_Market_Main where TMMNo=:TMMNo');
//          Parameters.ParamByName('TMMNo').Value := Trim(MTMMNo);
//          Open;
//
//      // 如果数量 > 0 表示已存在
//          if Fields[0].AsInteger > 0 then
//          begin
//            Close;
//
//        // 防死循环：最多重试10次
//            Inc(RetryCount);
//            if RetryCount > 10 then
//              raise Exception.Create('自动重试取号10次均重复，请检查流水号规则！');
//
//            Continue; // 重复了 → 重新回到循环开头再取一次
//          end;
//
//          Close;
//        end;
//
//    // 3. 不重复 → 退出循环
//        Break;
//      end;
      if not GetLSNo(ADOCmd, MTMMNo, 'G', 'Trade_Market_Main', 3, 1) then
        raise Exception.Create('取最大号失败!');
      TMMNo.Text := MTMMNo;

    end
    else
    begin
      maxno := Trim(FTMMID);
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Trade_Market_Main where TMMID=''' + Trim(FTMMID) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FTMMID) = '' then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
        if btnFlag = 1 then //保存后立即完成
          FieldByName('status').Value := Trim('10');
        if btnFlag = 5 then //保存后立即提交
          FieldByName('status').Value := Trim('1');
      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);

      end;
      RTSetsavedata(ADOCmd, 'Trade_Market_Main', ScrollBox1, 2);
      FieldByName('TMMID').Value := Trim(maxno);
      FieldByName('OurCoNo').Value := Trim(OurCoName.Properties.LookupItems.Text);
      FieldByName('CustNo').Value := Trim(CustName.Hint);
      FieldByName('CustName').Value := Trim(CustName.Text);
      FieldByName('Store').Value := Trim(canshu4);
      Post;
    end;

/////////////////////               保存主表            ///////////////////////////
/////////////////////               保存子表            ///////////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('TMSId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, Smaxno, 'JS', 'Trade_Market_Sub', 4, 1) = False then
            raise Exception.Create('取子流水号失败!');
        end
        else
        begin
          Smaxno := Trim(CDS_1.fieldbyname('TMSId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Trade_Market_Sub where TMMID=''' + Trim(maxno) + '''');
          sql.Add(' and TMSId=''' + Trim(Smaxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_1.fieldbyname('TMSId').AsString) = '' then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Trade_Market_Sub', 0);
          //RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Trade_Market_Sub_Qty', 2);
          //RTSetsavedata(ADOCmd, 'Trade_Market_Sub', ScrollBox1, 7);
          FieldByName('TMMID').Value := Trim(maxno);
          FieldByName('TMSId').Value := Trim(Smaxno);
          Post;
        end;
        Next;
      end;
    end;
/////////////////////               保存子表           ///////////////////////////


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

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select C_Code,C_Color,C_ColorNo,CF=count(TMSID) from Trade_Market_sub where TMMID=''' + Trim(maxno) + '''');
      sql.Add('group by C_Code,C_Color,C_ColorNo HAVING count(TMSID)>1  ');
      Open;
    end;
    if not ADOCmd.IsEmpty then
      raise Exception.Create('颜色重复!');

    FTMMID := Trim(maxno);
    ADOCmd.Connection.CommitTrans;

    TradeMarketProgressUpdateByTMMID(ADOCmd, FTMMID);

    with ADO_1 do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select *  from Trade_Market_Sub where  TMMID=''' + Trim(FTMMID) + '''');
      Open;
    end;
    SCreateCDS(ADO_1, CDS_1);
    SInitCDSData(ADO_1, CDS_1);
    Result := True; //保存后立即打印
    if (btnFlag = 2) or (btnFlag = 4) then
    begin
      try
        frmLabelPrint := TfrmLabelPrint.Create(Application);
        with frmLabelPrint do
        begin
          if canshu4 = '新余' then
            FLMType := 'TradePlanPrintxy'
          else
            FLMType := 'TradePlanPrint';
          FFiltration1 := Trim(maxno);
          if ShowModal = 1 then
          begin
    //        Self.InitGrid();
          end;
        end;
      finally
        frmLabelPrint.Free;
      end;
    end;
    if btnFlag = 3 then //保存后立即新增
    begin
      PState := 0;
      FTMMId := '';
      FormShow(Self);
//      CDS_1.Close;
//      InitData();
//      SClearData(ScrollBox1, 2); //清空容器中控件的内容
//      CustName.Text := '';
//      if CDS_1.IsEmpty then
//      begin
//        AddLine();
//      end;
    end;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTradeMarketInPut_XY.TBSaveClick(Sender: TObject);
var
  MAXNO1: string;
begin

  ToolBar1.SetFocus;
  with cds_1 do
  begin
    while cds_1.Locate('C_Code', null, []) do
    begin
      cds_1.Delete
    end;
  end;

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

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

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

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

  if CDS_1.Locate('C_Color', null, []) then
  begin
    Application.MessageBox('颜色不能为空!', '提示', 0);
    Exit;
  end;
//  if CDS_1.Locate('OrderQty', null, []) then
//  begin
//    Application.MessageBox('报单数量不能为空!', '提示', 0);
//    Exit;
//  end;
//  if CDS_1.Locate('OrderUnit', null, []) then
//  begin
//    Application.MessageBox('报单单位不能为空!', '提示', 0);
//    Exit;
//  end;
  if CDS_1.Locate('Price', null, []) then
  begin
    Application.MessageBox('单价不能为空!', '提示', 0);
    Exit;
  end;
  if not CDS_KH.Locate('CoAbbrName', self.CustName.Text, []) then
  begin
    Application.MessageBox('客户不存在!', '提示', 0);
    Exit;
  end
  else
  begin
    self.CustName.Properties.LookupItems.Text := Trim(CDS_KH.fieldbyname('CoCode').AsString);
  end;

  if SaveData() then
  begin

    Application.MessageBox('保存成功！', '提示', 0);
    if btnFlag = 4 then  //保存后打印并退出
      ModalResult := 1;
  end;
end;

procedure TfrmTradeMarketInPut_XY.ToolButton1Click(Sender: TObject);
begin

  AddLine();
end;

procedure TfrmTradeMarketInPut_XY.ToolButton2Click(Sender: TObject);
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;
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while Locate('SSel', True, []) do
    begin
      if Trim(CDS_1.fieldbyname('TMSId').AsString) <> '' then
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete Trade_Market_Sub where TMSId=''' + Trim(CDS_1.fieldbyname('TMSId').AsString) + '''');
          sql.Add('delete Trade_Market_Sub_Qty where TMSId=''' + Trim(CDS_1.fieldbyname('TMSId').AsString) + '''');
          ExecSQL;
        end;
      end;
      Delete;
    end;
  end;
  CDS_1.EnableControls;

end;

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

end;

procedure TfrmTradeMarketInPut_XY.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_Market_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('TMSId').Value := Trim(maxno);
                  FieldByName('TMSUnit').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('qtyunit').AsString);
                  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('TMSId').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 TfrmTradeMarketInPut_XY.ToolButton5Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  OneKeyPost(Tv1, CDS_1);
end;

procedure TfrmTradeMarketInPut_XY.edtGYBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
  i: integer;
begin
//  fsj := Trim(TBtnEditC(Sender).Hint);
//  FWZ := Pos('/', fsj);
//  i := 0;
//  try
//    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
//    with frmZDYHelpSel do
//    begin
//      flag := Copy(fsj, 1, FWZ - 1);
//      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
//      if ShowModal = 1 then
//      begin
//        TBtnEditC(Sender).text := '';
//        with ClientDataSet1 do
//        begin
//          First;
//          while not Eof do
//          begin
//            if FieldByName('SSel').AsBoolean = True then
//            begin
//              i := i + 1;
//              if TBtnEditC(Sender).text = '' then
//                TBtnEditC(Sender).text := FieldByName('ZDYName').AsString
//              else
//                TBtnEditC(Sender).text := TBtnEditC(Sender).text + ',' + FieldByName('ZDYName').AsString;   //inttostr(i)+'.'+
//            end;
//            Next;
//          end;
//        end;
//      end;
//    end;
//  finally
//    frmZDYHelpSel.Free;
//  end;
end;

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

procedure TfrmTradeMarketInPut_XY.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 TfrmTradeMarketInPut_XY.CustNameMouseDown(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 DISTINCT A.C_Code,B.C_Color,B.C_ColorNo,B.referencePrice AS Price 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));
        SQL.ADD(' order by B.C_ColorNo');
         //ShowMessage(sql.text);
        Open;
      end;
    end;
  end
  else
  begin
    with ADO_YS do
    begin
      Close;
      sql.Clear;
      sql.Add(' SELECT DISTINCT A.C_Code,B.C_Color,B.C_ColorNo,B.referencePrice AS Price 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));
      SQL.ADD(' order by B.C_ColorNo');
      Open;
    end;
  end;
end;

procedure TfrmTradeMarketInPut_XY.CustNamePropertiesChange(Sender: TObject);
begin
  inherited;
  if CDS_KH.Locate('CoAbbrName', Trim(self.CustName.Text), []) then
  begin
    self.CustName.Hint := Trim(CDS_KH.fieldbyname('CoCode').AsString);
  end;
end;

procedure TfrmTradeMarketInPut_XY.CustNamePropertiesCloseUp(Sender: TObject);
begin
  inherited;
  with ADOtemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*    ');
    sql.Add(',BankName=(select top 1 BankName from Bs_Company_Bank X where X.CoID=A.CoID )   ');
    sql.Add(',ContactsId=(select top 1 UserID from SY_User X where X.UserName=B.Contacts )   ');
    sql.Add('from BS_Company A  ');
    sql.Add('left join  BS_Company_contact B ON A.COID=B.COID and B.IsDefault=1  ');
    sql.Add('where 1=1 ');
    sql.Add(' and A.CoType=''' + Trim('客户') + '''');
//    if Trim(FAuthority) = '理单业务' then
//    begin
//      sql.Add(' and ( SalesId=' + quotedstr(trim(DCode)) + ' or exists (select * from [dbo].[F_Tool_SplitString](TallyId,'','') X where X.RTValue =' + quotedstr(trim(DCode)) + '))');
//    end;
    if canshu4 = '新余' then
      sql.Add(' and A.Stores=''' + Trim('新余') + '''');
    sql.Add(' and A.CoAbbrName=''' + Trim(CustName.Text) + '''');
    Open;
  end;
  self.ShippingAddress.Text := Trim(adotemp.fieldbyname('mxaddress').AsString);
  self.CONTACT.Text := Trim(adotemp.fieldbyname('Telephone').AsString);
  self.AddressArea.Text := Trim(adotemp.fieldbyname('MXAREA').AsString);

end;

procedure TfrmTradeMarketInPut_XY.cxDBImage1DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;
  LookImage(ADO_I1.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

procedure TfrmTradeMarketInPut_XY.LookImage(FileName: string);
var
  sFieldName: string;
begin

  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);

  sFieldName := sFieldName + '\' + trim(FileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin
    try
      IdFTP1.Get(fFlileFlag + '\' + Trim(FileName), sFieldName, true, false);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmTradeMarketInPut_XY.N1Click(Sender: TObject);
begin
  btnFlag := 1;
  TBSaveClick(Sender);
end;

procedure TfrmTradeMarketInPut_XY.N2Click(Sender: TObject);
begin
  btnFlag := 2;
  TBSaveClick(Sender);
end;

procedure TfrmTradeMarketInPut_XY.N3Click(Sender: TObject);
begin
  btnFlag := 3;
  TBSaveClick(Sender);
end;

procedure TfrmTradeMarketInPut_XY.N4Click(Sender: TObject);
begin
  btnFlag := 4;
  TBSaveClick(Sender);
end;

procedure TfrmTradeMarketInPut_XY.N5Click(Sender: TObject);
begin
  btnFlag := 5;
  TBSaveClick(Sender);
end;

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

procedure TfrmTradeMarketInPut_XY.OurCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin
        self.OurCoName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.OurCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeMarketInPut_XY.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 TfrmTradeMarketInPut_XY.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 TfrmTradeMarketInPut_XY.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 TfrmTradeMarketInPut_XY.ScrollBox1Click(Sender: TObject);
begin
  TMMDate.SetFocus;
end;

procedure TfrmTradeMarketInPut_XY.ShippingAddressPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmComContactSel := TfrmComContactSel.Create(Application);
    with frmComContactSel do
    begin
      FCoCode := Trim(self.CustName.Hint);
      if ShowModal = 1 then
      begin
        self.ShippingAddress.Text := Trim(frmComContactSel.CDS_1.fieldbyname('mxaddress').AsString);
        self.CONTACT.Text := Trim(frmComContactSel.CDS_1.fieldbyname('PhoneNumber').AsString);
        self.AddressArea.Text := Trim(frmComContactSel.CDS_1.fieldbyname('MXAREA').AsString);

      end;
    end;
  finally
    frmComContactSel.Free;
  end;
end;

procedure TfrmTradeMarketInPut_XY.StkPositionPropertiesButtonClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      FStores := Trim(canshu4);
      FsqlStr := ' and stores = ' + QuotedStr(Trim(canshu4));
      if ShowModal = 1 then
      begin
        self.CustName.Hint := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.CustName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
        self.ShippingAddress.Text := Trim(frmCompanySel.CDS_1.fieldbyname('mxaddress').AsString);
        self.CONTACT.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Telephone').AsString);
        self.AddressArea.Text := Trim(frmCompanySel.CDS_1.fieldbyname('MXAREA').AsString);

      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeMarketInPut_XY.PackNoteDblClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
  i: integer;
begin
  fsj := Trim(TMemo(Sender).Hint);
  FWZ := Pos('/', fsj);
  i := 0;
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TMemo(Sender).Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean = True then
            begin
              i := i + 1;
              TMemo(Sender).Lines.Add(FieldByName('ZDYName').AsString)      //inttostr(i)+'.'+
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmTradeMarketInPut_XY.QtyMXKeyPress(Sender: TObject; var Key: Char);
var
  MaxNo: string;
begin
  if Key = #13 then
  begin
    if Label38.Caption = '序号' then
    begin
      Application.MessageBox('请选择数据', '提示', 0);
      Exit;
    end;
    if StrTofloatDef(QtyMX.Text, 0) = 0 then
      Exit;
    if GetLSNo(ADOTemp, MaxNo, 'MX', 'Trade_Market_Sub_Qty', 4, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    try
      ADOCmd.Connection.BeginTrans;
      with ADOCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('insert into  Trade_Market_Sub_Qty (TMSQID,TMSId,TMMID,Qty)');
        sql.Add('values(' + quotedstr(Trim(MaxNo)));
        sql.Add(',' + quotedstr(Trim(MTMSId)));
        sql.Add(',' + quotedstr(Trim(FTMMID)));
        sql.Add(',' + QtyMX.Text);
        sql.Add(')');
        ExecSQL;
      end;
      ADOCmd.Connection.CommitTrans;

      with CDS_MX do
      begin
        Append;
        FieldByName('TMSQID').Value := MaxNo;
        FieldByName('TMSId').Value := MTMSId;
        FieldByName('TMMId').Value := FTMMId;
        FieldByName('Qty').Value := QtyMX.Text;
        post;
      end;
    except
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('添加失败!', '提示', 0);
    end;
    QtyMX.Text := '';
    TvMX.Controller.EditingController.ShowEdit();
  end;
  with cds_1 do
  begin
    edit;
    if CDS_MX.IsEmpty then
    begin
      Fieldbyname('TMSPiece').Value := 0;
      Fieldbyname('TMSQty').Value := 0;
    end
    else
    begin
      Fieldbyname('TMSPiece').Value := TvMX.DataController.Summary.FooterSummaryValues[1];
      Fieldbyname('TMSQty').Value := TvMX.DataController.Summary.FooterSummaryValues[0];
    end;

    post;
  end;
end;

procedure TfrmTradeMarketInPut_XY.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  AColumn: TcxGridColumn;
begin
  // 获取被双击的列
  AColumn := ACellViewInfo.Item as TcxGridColumn;

  // 检查是否双击了 Qty 列（通过列名判断）
  if AColumn.Name = 'Tv1Column19' then
  begin
    if not CDS_1.IsEmpty then
    begin
      // 获取当前行的相关ID
      Self.MTMSId := CDS_1.FieldByName('TMSId').AsString;
      Self.FTMMID := CDS_1.FieldByName('TMMId').AsString;

      try
        frmTradeMarketQtyInPut := TfrmTradeMarketQtyInPut.Create(Application);
        with frmTradeMarketQtyInPut do
        begin
          PMTMSId := Self.MTMSId;
          PFTMMID := Self.FTMMID;
          canshu4 := self.canshu4;
          if ShowModal = 1 then
          begin
//            Self.InitMXGrid(Self.MTMSID);
            with ADOTemp do
            begin
              Close;
              SQL.Clear;
              sql.Add('select  ');
              sql.Add('SubOutQty = sum(isnull( A.Qty,0))   ');
              sql.Add(',SubOutPS = count(isnull( A.TMSQID,0))   ');
              sql.Add('from Trade_Market_Sub_Qty   A  ');
              SQL.Add('where A.TMSID =' + QuotedStr(CDS_1.FieldByName('TMSId').AsString));
//              ShowMessage(sql.Text);
              open;
            end;
            with self.CDS_1 do
            begin
              edit;
              FieldByName('SubOutPS').Value := ADOTemp.FieldByName('SubOutPS').Value;
              FieldByName('SubOutQty').Value := ADOTemp.FieldByName('SubOutQty').Value;
              post;
            end;
          end;
        end;
      finally
        FreeAndNil(frmTradeMarketQtyInPut);
      end;

      AHandled := True;
    end;
  end;
end;

procedure TfrmTradeMarketInPut_XY.Tv1Column12PropertiesChange(Sender: TObject);
var
  sInput: string;
  ACombo: TcxLookupComboBox;
begin
  inherited;
  ACombo := TcxLookupComboBox(Sender);
  sInput := Trim(ACombo.Text); // 获取你输入的文字
  if CDS_1.FieldByName('C_CODE').AsString = '' then
    EXIT;

  with ADO_YS do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT * ');
    sql.Add(' FROM V_COLOR_VIEW A WHERE 1=1 and C_CODE= ' + QuotedStr(CDS_1.FieldByName('C_CODE').AsString));

    if sInput <> '' then
    begin
      sql.Add(Format(' AND  A.C_ColorNo LIKE ''%%%s%%'' ', [sInput]));
    // ✅ 关键：DISTINCT 下绝对不报错的排序（只排查询里出现的字段）
      sql.Add(Format(' ORDER BY CASE WHEN A.C_ColorNo = ''%s'' THEN 0 ELSE 1 END, A.C_ColorNo ', [sInput]));

    end
    else
    begin
//      sql.Add(' AND  C_CODE= ' + QuotedStr(CDS_1.FieldByName('C_CODE').AsString));
    end;
//    ShowMessage(SQL.Text);
    Open;
  end;
end;

procedure TfrmTradeMarketInPut_XY.Tv1Column12PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select isnull(IncludeTax,0)IncludeTax from Bs_Company a    ');
    sql.Add('where CoCode=' + QuotedStr(CustName.hint));
    open;
  end;

  FIncludeTax := ADOTemp.Fieldbyname('IncludeTax').asfloat;

  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;
    FieldByName('price').Value := ADO_YS.FieldByName('price').Value * (1 + (FIncludeTax / 100));
    FieldByName('referencePrice').Value := ADO_YS.FieldByName('Price').Value;

    Post;

    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add(' SELECT');
      SQL.Add('   (ISNULL((SELECT SUM(ISNULL(StkQty, 0))');
      SQL.Add('      FROM Bs_Cloth_IO');
      SQL.Add('      WHERE C_Code = ' + QuotedStr(cds_1.FieldByName('C_Code').AsString));
      SQL.Add('        AND C_ColorNO = ' + QuotedStr(cds_1.FieldByName('C_ColorNO').AsString));
      SQL.Add('        AND STKNAME = ''新余''), 0)');
      SQL.Add('    - ISNULL((SELECT SUM(ISNULL(Qty, 0))');
      SQL.Add('      FROM Trade_Market_Sub_Qty X');
      SQL.Add('      INNER JOIN Trade_Market_Sub F ON X.TMSID = F.TMSID');
      SQL.Add('      INNER JOIN Trade_Market_Main E ON E.TMMID = F.TMMID');
      SQL.Add('      WHERE Store = ''新余''');
      SQL.Add('        AND E.Status = ''10''');
      SQL.Add('        AND F.C_Code = ' + QuotedStr(cds_1.FieldByName('C_Code').AsString));
      SQL.Add('        AND F.C_ColorNo = ' + QuotedStr(cds_1.FieldByName('C_ColorNO').AsString));
      SQL.Add('   ), 0)) AS StkQty');

      Open;

    // 获取计算结果

    end;

  // 更新当前记录
    CDS_1.Edit;
    CDS_1.FieldByName('StkQty').Value := ADOTemp.FieldByName('StkQty').AsFloat;
    CDS_1.Post;
  end;

  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('  SELECT top 1 isnull(Price,0) as Lastprice FROM Trade_Market_Main A inner join  Trade_Market_Sub B on A.TMMId=B.TMMId ');
    sql.Add('where A.CustName=' + QuotedStr(CustName.Text));
    sql.Add('and B.C_ColorNo=' + QuotedStr(CDS_1.FieldByName('C_ColorNo').AsString));
    sql.add('order by A.filltime desc');
    open;
  end;
  if ADOTemp.IsEmpty = false then
  begin
    with Self.CDS_1 do
    begin
      edit;
      Fieldbyname('Lastprice').AsString := trim(ADOTemp.Fieldbyname('Lastprice').AsString);
      Post;
    end;
  end;
end;

procedure TfrmTradeMarketInPut_XY.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
//      xzflag := 0;
//      kcflag := 1;
      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('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);
          post;
        end;

      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;

end;

procedure TfrmTradeMarketInPut_XY.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmPictureUpload := TfrmPictureUpload.create(self);
    with frmPictureUpload do
    begin
      fFlileFlag := self.fFlileFlag;
      FDataId := Trim(self.CDS_1.fieldbyname('AttName').AsString);
      FPictureName := Trim(self.CDS_1.fieldbyname('AttName').AsString);
      FWidth := 450;
      FHeight := 600;
      FTFType := 'HX';
      if showmodal = 1 then
      begin
        with self.CDS_1 do
        begin
          Edit;
          Fieldbyname('AttName').Value := frmPictureUpload.FPictureName;
          Post;
        end;
      end;
    end;
  finally
    frmPictureUpload.free;
  end;

end;

procedure TfrmTradeMarketInPut_XY.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('TMSUnit').Value := ADO_PM.FieldByName('qtyunit').Value;

//    FieldByName('StkQty').Value := ADO_PM.FieldByName('StkQty').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(CustName.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 TfrmTradeMarketInPut_XY.Tv1DblClick(Sender: TObject);
var
  MTMSQID: string;
begin

//  try
//    frmTradeMarketQtyInPut := TfrmTradeMarketQtyInPut.Create(Application);
//    with frmTradeMarketQtyInPut do
//    begin
//      MTMSId := self.MTMSId;
//      FTMMID := Self.FTMMID;
//      if ShowModal = 1 then
//      begin
////         Self.InitMXGrid(Self.MTMSQID);
//      end;
//    end;
//  finally
//    frmTradeMarketQtyInPut.Free;
//  end;

end;

procedure TfrmTradeMarketInPut_XY.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  inherited;
  Label38.Caption := '序号：' + Trim(cds_1.fieldbyname('SerialNo').AsString);
  MTMSId := Trim(cds_1.fieldbyname('TMSId').AsString);
  InitMXGrid(MTMSId);
end;

procedure TfrmTradeMarketInPut_XY.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 DISTINCT A.C_Code,B.C_Color,B.C_ColorNo,B.referencePrice AS Price 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));
        SQL.ADD(' order by B.C_ColorNo');
        Open;
      end;
    end;
  end
  else
  begin
    with ADO_YS do
    begin
      Close;
      sql.Clear;
      sql.Add(' SELECT DISTINCT A.C_Code,B.C_Color,B.C_ColorNo,B.referencePrice AS Price 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));
      SQL.ADD(' order by B.C_ColorNo');
      Open;
    end;
  end;

  if not ADO_DanWei.IsEmpty then
  begin
    if ADO_DanWei.FieldByName('C_Code').AsString <> CDS_1.FieldByName('C_Code').AsString then
    begin
      with ADO_DanWei do
      begin
        Close;
        sql.Clear;
        sql.Add(' EXEC P_Cloth_GetCICUnit @C_Code= ' + QuotedStr(CDS_1.FieldByName('C_Code').AsString));
        Open;
      end;
    end;
  end
  else
  begin
    with ADO_DanWei do
    begin
      Close;
      sql.Clear;
      sql.Add(' EXEC P_Cloth_GetCICUnit @C_Code= ' + QuotedStr(CDS_1.FieldByName('C_Code').AsString));
      Open;
    end;
  end;


//  Bs_Cloth_Info_Coefficient
end;

procedure TfrmTradeMarketInPut_XY.TvMXCellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  if ToolBar2.Enabled = False then
    Exit;
  with ADOCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete Trade_Market_Sub_Qty   where TMSQID=' + quotedstr(Trim(CDS_MX.FieldByName('TMSQID').AsString)));
    ExecSQL;
  end;

//  with ADOCmd do
//  begin
//    close;
//    sql.Clear;
//    sql.Add(' exec P_KCSD_AllNew ');
//    sql.Add(' @Operation=''大货单明细删除'' ');
//    sql.Add(' ,@TMMNo=' + quotedstr(trim(TMMNo.Text)));
//    ExecSQL;
//  end;
  CDS_MX.Delete;
  with cds_1 do
  begin
    edit;
    if CDS_MX.IsEmpty then
    begin
      Fieldbyname('TMSPiece').Value := 0;
      Fieldbyname('TMSQty').Value := 0;
    end
    else
    begin
      Fieldbyname('TMSPiece').Value := TvMX.DataController.Summary.FooterSummaryValues[1];
      Fieldbyname('TMSQty').Value := TvMX.DataController.Summary.FooterSummaryValues[0];
    end;

    post;
  end;
end;

procedure TfrmTradeMarketInPut_XY.InitMXGrid(MTMSId: string);
begin
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.* from Trade_Market_Sub_Qty A ');
    sql.Add('where TMSId=''' + Trim(MTMSId) + '''');
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_MX);
  SInitCDSData(ADOTemp, CDS_MX);
end;

procedure TfrmTradeMarketInPut_XY.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 TfrmTradeMarketInPut_XY.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 TfrmTradeMarketInPut_XY.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.

