unit U_TradePlanInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  U_FormLayOutDesign, Winapi.ShellAPI, ComObj, 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, dxCore,
  cxDateUtils, dxScrollbarAnnotations, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdFTP, cxImage,
  cxDBEdit, cxGroupBox, cxCheckComboBox, cxImageList, 
   dxSkinWXI, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmTradePlanInPut = class(TfrmBaseInput)
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ADOQueryLabel: TADOQuery;
    Label51: TLabel;
    Label7: TLabel;
    ADO_I1: TADOQuery;
    DS_I1: TDataSource;
    IdFTP1: TIdFTP;
    ADO_I2: TADOQuery;
    DS_I2: TDataSource;
    CDS_LM: TClientDataSet;
    OpenDialog1: TOpenDialog;
    PM_1: TPopupMenu;
    N5: TMenuItem;
    N6: TMenuItem;
    GPM_2: TcxGridPopupMenu;
    cxPageControl3: TcxPageControl;
    cxTabSheet2: TcxTabSheet;
    cxTabSheet3: TcxTabSheet;
    RMDB_2: TRMDBDataSet;
    ADO_1: TADOQuery;
    ADO_2: TADOQuery;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    ScrollBox2: TScrollBox;
    Label5: TLabel;
    Label54: TLabel;
    Label55: TLabel;
    Label56: TLabel;
    Label57: TLabel;
    Label58: TLabel;
    Label59: TLabel;
    Label60: TLabel;
    Label61: TLabel;
    Label62: TLabel;
    Label63: TLabel;
    Label64: TLabel;
    Label65: TLabel;
    Label66: TLabel;
    Label67: TLabel;
    Label68: TLabel;
    Label69: TLabel;
    Label70: TLabel;
    OrdNote: TcxMemo;
    LightNote: TcxButtonEdit;
    OtherProcess1: TcxComboBox;
    Resin: TcxButtonEdit;
    WaterPressure: TcxButtonEdit;
    LightType: TcxButtonEdit;
    DyeingMach: TcxButtonEdit;
    plane: TcxButtonEdit;
    ColourFastness: TcxButtonEdit;
    Feel: TcxButtonEdit;
    OtherProcess8: TcxButtonEdit;
    ReferenceNumber: TcxButtonEdit;
    shrink: TcxButtonEdit;
    soft: TcxButtonEdit;
    OtherProcess6: TcxButtonEdit;
    EnvironmentalProtection: TcxButtonEdit;
    waterProof: TcxButtonEdit;
    coating: TcxButtonEdit;
    ADOQuery1: TADOQuery;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    TV1Column8: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    VC_PRTPs: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    TV1Column15: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    tlb2: TToolBar;
    btnConSel: TToolButton;
    ToolButton5: TToolButton;
    ToolButton7: TToolButton;
    ToolButton4: TToolButton;
    ToolButton1: TToolButton;
    ToolButton6: TToolButton;
    ToolButton2: TToolButton;
    ToolButton8: TToolButton;
    f: TToolButton;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    ToolBar2: TToolBar;
    ToolButton12: TToolButton;
    ToolButton14: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column7: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    ToolButton3: TToolButton;
    TBClose: TToolButton;
    Panel5: TPanel;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    Label25: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label27: TLabel;
    Label14: TLabel;
    Label20: TLabel;
    lbl17: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label15: TLabel;
    Label37: TLabel;
    lbl24: TLabel;
    Label21: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    lbl23: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label19: TLabel;
    OrderNo: TcxTextEdit;
    BuyConNo: TcxTextEdit;
    C_Name: TcxTextEdit;
    C_Spec: TcxTextEdit;
    C_Composition: TcxTextEdit;
    C_GramWeight: TcxTextEdit;
    C_Width: TcxTextEdit;
    ConNo: TcxTextEdit;
    OrdDate: TcxDateEdit;
    DlyDate: TcxDateEdit;
    CustName: TcxButtonEdit;
    OurCoName: TcxButtonEdit;
    OrdUnit: TcxComboBox;
    C_Degree: TcxComboBox;
    GC_Name: TcxTextEdit;
    CraftCode: TcxTextEdit;
    OrdType: TcxComboBox;
    Saleser: TcxTextEdit;
    SalesId: TcxTextEdit;
    TallyId: TcxTextEdit;
    DlyNote: TcxTextEdit;
    ConDate: TcxTextEdit;
    ISTax: TcxComboBox;
    PayMent: TcxButtonEdit;
    OrdReason: TcxComboBox;
    C_EComposition: TcxTextEdit;
    C_EName: TcxTextEdit;
    SerNum: TcxTextEdit;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    TV3Column3: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    TV3Column1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolButton11: TToolButton;
    ToolButton13: TToolButton;
    TV1Column17: TcxGridDBColumn;
    Label18: TLabel;
    torque: TcxButtonEdit;
    Label22: TLabel;
    Label23: TLabel;
    ToolButton9: TToolButton;
    CDS_MX: TClientDataSet;
    DS_MX: TDataSource;
    Label24: TLabel;
    C_code: TcxTextEdit;
    TV1Column10: TcxGridDBColumn;
    TV1Column19: TcxGridDBColumn;
    Label26: TLabel;
    PieceWeight: TcxCurrencyEdit;
    ShrinkageRate: TcxCurrencyEdit;
    Tallyer: TcxButtonEdit;
    Label28: TLabel;
    JGType: TcxButtonEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure HYTTBtnUpClick(Sender: TObject);
    procedure KYTTBtnUpClick(Sender: TObject);
    procedure btnConSelClick(Sender: TObject);
    procedure v1OrderUnitPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SYRNameBtnUpClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure TechnicsBtnDnClick(Sender: TObject);
    procedure Tv1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CustNameDblClick(Sender: TObject);
    procedure C_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OrdNoteDblClick(Sender: TObject);
    procedure QtyNotePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OurCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure LabPackagePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure LabVolumeDblClick(Sender: TObject);
    procedure LabPackageDblClick(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton14Click(Sender: TObject);
    procedure TV2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure EnvironmentalProtectionPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure fClick(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
    procedure ToolButton13Click(Sender: TObject);
    procedure torquePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton9Click(Sender: TObject);
    procedure TV1Column18PropertiesEditValueChanged(Sender: TObject);
    procedure ShrinkageRatePropertiesEditValueChanged(Sender: TObject);
    procedure TallyerPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure JGTypeDblClick(Sender: TObject);
    procedure JGTypePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    FSCXDH: string;
    procedure InitData();
    procedure upOrdRawQty();
    function SaveData(): Boolean;
    function getOrderNo(fconNo: string): string;
    procedure SetXH(CDS_PX: TClientDataSet);
    procedure INITMX();

    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo: string;
    FXS: Integer;
    canshu1: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmTradePlanInPut: TfrmTradePlanInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_ZDYHelpSel, U_RTFun, U_TradeSalesContractSel,
  U_CompanySel, U_PictureUpload, U_ClothInfoSel, U_PatternColorHelp,
  U_TradeLabelSel, U_TradeGreyClothStkSel, U_dynamicsDll, U_printPdf,
  U_ZDYHelpSelXR, U_TotalClothStk, U_ContractSel, U_TradePlanFtyInPut,
  U_TradeInspSet, U_KUANNOInfoImport, U_UserSel;


{$R *.dfm}
procedure TfrmTradePlanInPut.InitMX();
var
  i: Integer;
  maxno: string;
begin
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select sum(OrdRawQty)OrdRawQty,SCUNIT,isnull(C_Pattern,'''')C_Pattern,isnull(C_ColorNo,'''')C_ColorNo,isnull(C_Color,'''')C_Color  from Trade_Plan_Sub_mx  ');
    sql.Add(' where MainId=''' + Trim(FMainId) + '''');
    sql.Add('group by isnull(C_Pattern,''''),isnull(C_ColorNo,''''),isnull(C_Color,''''),SCUNIT ');
//    ShowMessage(sql.Text);
    Open;
  end;

  SCreateCDS(ADOTemp, CDS_mx);
  SInitCDSData(ADOTemp, CDS_mx);
  with CDS_mx do
  begin
    CDS_mx.First; // 移动到第一条记录
    while not CDS_mx.Eof do
    begin
      if CDS_1.Locate('C_Pattern;C_ColorNo;C_EColor', VarArrayOf([CDS_mx.FieldByName('C_Pattern').AsString, CDS_mx.FieldByName('C_ColorNo').AsString, CDS_mx.FieldByName('C_Color').AsString]), []) then
      begin
        if CDS_1.FieldByName('OrdQty').asfloat <> CDS_mx.FieldByName('OrdRawQty').asfloat then
        begin
          if Application.MessageBox('该颜色色号订单数量已发生变动,是否同步修改', '提示', 32 + 4) <> IDYES then
          begin

          end
          else
          begin
            with CDS_1 do
            begin
              edit;

              CDS_1.FieldByName('OrdQty').Value := CDS_mx.FieldByName('OrdRawQty').asfloat;
              post;
            end;
          end;

        end;

      end
      else
      begin
        with CDS_1 do
        begin
          append;

          i := CDS_1.RecordCount;
          if GetLSNo(ADOCmd, maxno, 'JS', 'Trade_Plan_Sub', 4, 1) = False then
          begin

            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
          FieldByName('subid').Value := trim(maxno);
          FieldByName('SerialNo').Value := i + 1;
          FieldByName('AttName').Value := '';
          CDS_1.FieldByName('C_Pattern').Value := CDS_mx.FieldByName('C_Pattern').asstring;
          CDS_1.FieldByName('C_ColorNo').Value := CDS_mx.FieldByName('C_ColorNo').asstring;
          CDS_1.FieldByName('C_EColor').Value := CDS_mx.FieldByName('C_Color').asstring;
          CDS_1.FieldByName('OrdQty').Value := CDS_mx.FieldByName('OrdRawQty').asfloat;

          post;
        end;
      end;
      OrdUnit.ItemIndex := OrdUnit.Properties.Items.IndexOf(trim(CDS_mx.fieldbyname('SCUNIT').AsString));
      CDS_mx.Next;

    end
  end

end;

procedure TfrmTradePlanInPut.JGTypeDblClick(Sender: TObject);
begin
  JGType.Text:='';
end;

procedure TfrmTradePlanInPut.JGTypePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JGType';
      flagname := '加工类型';
      if ShowModal = 1 then
      begin
        JGType.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTradePlanInPut.upOrdRawQty();
var
  FOrgQty, FWeight, FShrinkageRate, FQty: Double;        //个数，单价，金额
begin

  with CDS_1 do
  begin
    FOrgQty := Fieldbyname('OrdQty').AsFloat;
    FWeight := Fieldbyname('Weight').AsFloat;
    FShrinkageRate := ShrinkageRate.Value;
  end;
  if Trim(CDS_1.Fieldbyname('Weight').AsString)='' then
  begin
    FQty := RoundFloat(FOrgQty / ((100 - FShrinkageRate) / 100), 0);
  end
  else
  begin
    FQty := RoundFloat(FWeight / ((100 - FShrinkageRate) / 100), 0);
  end;

  with CDS_1 do
  begin
    Edit;
    FieldByName('OrdRawQty').Value := FQty;
    if PieceWeight.Value > 0 then
      FieldByName('OrdPiece').Value := RoundFloat(FieldByName('OrdRawQty').Value / PieceWeight.Value, 0);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

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

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

end;

procedure TfrmTradePlanInPut.ShrinkageRatePropertiesEditValueChanged(
  Sender: TObject);
begin
  upOrdRawQty();
end;

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

procedure TfrmTradePlanInPut.TallyerPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(CDS_1.fieldbyname('UserName').AsString);
//        TcxButtonEdit(Sender).Properties.LookupItems.Text := Trim(CDS_1.fieldbyname('UserID').AsString);
      end;
    end;
  finally
    frmUserSel.Free;
  end;
end;

procedure TfrmTradePlanInPut.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradePlanInPut.InitData();
var
  FDate: TDateTime;
  maxno: string;
begin
  FDate := SGetServerDateTime(ADOTemp);
  OrdDate.Date := FDate;
  DlyDate.Date := FDate;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Trade_Plan_Main where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 1);
  SCSHData(ADOTemp, ScrollBox2, 1);
  OurCoName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('OurCoNo').AsString);
  CustName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('CustNo').AsString);

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *  from Trade_Plan_Sub  ');
    sql.Add(' where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
//  SCSHData(ADOTemp, ScrollBox1, 2);
  SCreateCDS(ADOTemp, CDS_1);
  SInitCDSData(ADOTemp, CDS_1);
  SCSHData(ADOTemp, ScrollBox1, 2);

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *  from Trade_Plan_Technical   ');
    sql.Add(' where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_2);
  SInitCDSData(ADOTemp, CDS_2);

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select SerialNo,FtyName,Technics,FtyQty=sum(FtyQty),mainid from Trade_Plan_Fty   ');
    sql.Add(' where MainId=''' + Trim(FMainId) + '''');
    sql.Add(' group by SerialNo,Technics,FtyName,mainid order by SerialNo ');
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_3);
  SInitCDSData(ADOTemp, CDS_3);

  if PState = 0 then
  begin
    OrdDate.Date := SGetServerDate(ADOTemp);
    DlyDate.Date := OrdDate.Date;

    OrderNo.Text := '';
    OrderNo.Enabled := True;
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrderNo.Text := '';
    OrderNo.Enabled := True;

    OrdDate.Date := FDate;

    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        Post;
        Next;
      end;
    end;
    with CDS_2 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SUBID').Value := '';
        Post;
        Next;
      end;
    end;
    with CDS_3 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
//        FieldByName('SubID').Value := '';
//        FieldByName('TPFId').Value := '';
        Post;
        Next;
      end;
    end;
  end;
  if FMainId = '' then
  begin
    if GetLSNo(ADOCmd, maxno, 'JM', 'Trade_Plan_Main', 3, 1) = False then
      raise Exception.Create('取最大号失败!');

    FMainId := trim(maxno);
  end;
end;

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

procedure TfrmTradePlanInPut.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradePlanInPut := nil;
end;

procedure TfrmTradePlanInPut.FormResize(Sender: TObject);
begin
  inherited;
  if self.WindowState = wsMinimized then
    SetShowModalHide(application.MainForm.Handle, self);
end;

procedure TfrmTradePlanInPut.FormShow(Sender: TObject);
var
  i: Integer;
begin
  inherited;
  readCxGrid(TRIM(SELF.Caption) + 'Tv88', Tv1, '生产指示单管理');
  with CDS_LM do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;
  with ADO_1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_BS_Label_Get_Test');
    Open;
  end;

  readCxGrid(TRIM(SELF.Caption) + 'Tv2', Tv2, '生产指示单管理');
  OrderNo.Text := '';
  fFlileFlag := UserDataFlag + 'HX';
  if canshu1<>'管理' then
    Tallyer.Enabled := False;
  InitData();

end;

function TfrmTradePlanInPut.SaveData(): Boolean;
var
  maxno, Smaxno, maxorderno, MaxFNo, MaxTFTNo, MaxTPFTNo: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'JM', 'Trade_Plan_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败!');
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Trade_Plan_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if ADOCmd.IsEmpty then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('MasterOrderNo').Value := Trim(OrderNo.Text);
      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd, 'Trade_Plan_Main', ScrollBox1, 1);
      RTSetsavedata(ADOCmd, 'Trade_Plan_Main', ScrollBox2, 1);
      FieldByName('MainId').Value := Trim(maxno);
      FieldByName('OrdType').Value := '大货单';
      FieldByName('OurCoNo').Value := Trim(OurCoName.Properties.LookupItems.Text);
      FieldByName('CustNo').Value := Trim(CustName.Properties.LookupItems.Text);
      Post;
    end;

/////////////////////               保存子表            ///////////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('ConSid').AsString) <> '' then
        begin
          with ADOQuery1 do
          begin
            Close;
            sql.Clear;
            SQL.Add('select ConQty - ' + Trim(CDS_1.fieldbyname('OrdQty').AsString) + '');
            SQL.Add('-ISNULL((select SUM(OrdQty) From Trade_Plan_Sub X where X.ConSid=A.ConSid ');
            sql.Add('and X.SubID<>''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''),0) AS SS');
            sql.Add('from Bs_Contract_Sub A where A.ConSid=''' + Trim(CDS_1.fieldbyname('ConSid').AsString) + '''');
//          showmessage(sql.text);
            Open;
          end;
          if ADOQuery1.FieldByName('SS').value < 0 then
          begin
            Application.MessageBox('指示单数量不能超过合同数量！', '提示', 0);
          end;
        end;

        if Trim(CDS_1.fieldbyname('SubId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, Smaxno, 'JS', 'Trade_Plan_Sub', 4, 1) = False then
            raise Exception.Create('取子流水号失败!');
        end
        else
        begin
          Smaxno := Trim(CDS_1.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Trade_Plan_Sub where MainId=''' + Trim(maxno) + '''');
          sql.Add(' and SubId=''' + Trim(Smaxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_1.fieldbyname('SubId').AsString) = '' then
            Append
          else
            Edit;
          RTSetsavedata(ADOCmd, 'Trade_Plan_Sub', ScrollBox1, 2);
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Trade_Plan_Sub', 0);
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(Smaxno);
          FieldByName('ConSId').Value := Trim(CDS_1.fieldbyname('ConSId').AsString);
          FieldByName('ConMId').Value := Trim(CDS_1.fieldbyname('ConMId').AsString);
          Post;

        end;
        Next;
      end;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('update Trade_Plan_Sub set AttName=null where MainId=''' + Trim(maxno) + '''');
      SQL.Add(' and not exists( select * from TP_File X where X.WBID=Trade_Plan_Sub.AttName )');
      ExecSQL;
    end;
/////////////////////               保存子表           ///////////////////////////
/////////////////////               保存加工条款            ///////////////////////////
    with CDS_2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_2.fieldbyname('TPTID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, MaxTFTNo, 'TF', 'Trade_Plan_Technical', 4, 1) = False then
            raise Exception.Create('取子流水号失败!');
        end
        else
        begin
          MaxTFTNo := Trim(CDS_2.fieldbyname('TPTID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Trade_Plan_Technical where MainId=''' + Trim(maxno) + '''');
          sql.Add(' and TPTID=''' + Trim(MaxTFTNo) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_2.fieldbyname('TPTID').AsString) = '' then
            Append
          else
            Edit;

          if Trim(CDS_2.fieldbyname('TPTID').AsString) <> '' then
          begin
            with ADOTemp do
            begin
              Close;
              SQL.Clear;
              sql.Add('select * from Trade_Plan_Technical where MainId=''' + Trim(maxno) + '''');

              Open;
            end;

          end;
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('TPTID').Value := Trim(MaxTFTNo);
          RTSetSaveDataCDS(ADOCmd, Tv2, CDS_2, 'Trade_Plan_Technical', 0);
          Post;

        end;
        Next;
      end;
    end;
/////////////////////               保存加工条款           ///////////////////////////


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

//    with ADOCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('update Trade_Plan_Main set SalesId=X.SalesId,Saleser=X.Saleser,TallyId=X.TallyId,Tallyer=X.Tallyer from Bs_Company X where X.CoCode=CustNo');
//      sql.Add('and orderno=''' + Trim(OrderNo.Text) + '''');
//      execsql;
//    end;
    ADOCmd.Connection.CommitTrans;
    FMainId := Trim(maxno);

    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select *  from Trade_Plan_Sub  ');
      sql.Add(' where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    SCreateCDS(ADOTemp, CDS_1);
    SInitCDSData(ADOTemp, CDS_1);

    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select *  from Trade_Plan_Technical   ');
      sql.Add(' where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    SCreateCDS(ADOTemp, CDS_2);
    SInitCDSData(ADOTemp, CDS_2);

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

    if pos('KEY 约束', Exception(ExceptObject).Message) > 0 then
    begin
      if GetLSNo(ADOCmd, Smaxno, 'JS', 'Trade_Plan_Sub', 4, 1) = False then
      begin
        Application.MessageBox('重新取子流水号失败！', '提示', 0);
        Exit;
      end;
    end;
  end;
end;

procedure TfrmTradePlanInPut.TBSaveClick(Sender: TObject);
var
  maxno, CurrentYear: string;
begin
  if PState <> 1 then
  begin
    ToolBar1.SetFocus;
  end;

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

  if OrdUnit.Text = '' then
  begin
    Application.MessageBox('单位不能为空!', '提示', 0);
    Exit;
  end;
  if SerNum.Text = '' then
  begin
    Application.MessageBox('序次号不能为空!', '提示', 0);
    Exit;
  end;
//  if OrdTYpe.Text = '' then
//  begin
//    Application.MessageBox('类型不能为空!', '提示', 0);
//    Exit;
//  end;
  if CDS_1.Locate('OrdQty', null, []) then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  CurrentYear := Copy(Trim(FormatDateTime('yyyy', Now)), Length(Trim(FormatDateTime('yyyy', Now))) - 1, 2);

  if Trim(OrderNo.Text) = '' then
  begin
    if GetLSNo(ADOCmd, maxno, 'OK-' + CurrentYear, 'Trade_Plan_Main', 4, 0) = False then
    begin
      Application.MessageBox('订单号生成失败!', '提示', 0);
      Exit;
    end;

//    with ADOTemp do
//    begin
//      Close;
//      sql.Clear;
//      SQL.Add('exec P_Trade_Plan_Get_OrdNo @ConNo=''' + Trim(ConNo.Text) + '''');
//      SQL.Add(',@OrdType=''' + Trim(OrdType.Text) + '''');
//      Open;
//    end;
    OrderNo.Text := TRIM(maxno);
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
//    ModalResult := 1;
  end;
end;

procedure TfrmTradePlanInPut.ToolButton11Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if SaveData() then
  begin
    try
      frmTradePlanFtyInPut := TfrmTradePlanFtyInPut.Create(Application);
      with frmTradePlanFtyInPut do
      begin
        FMainId := self.FMainId;
        if ShowModal = 1 then
        begin
          with Self.ADOTemp do
          begin
            Close;
            SQL.Clear;
            sql.Add(' select SerialNo,FtyName,Technics,FtyQty=sum(FtyQty) from Trade_Plan_Fty   ');
            sql.Add(' where MainId=''' + Trim(FMainId) + '''');
            sql.Add(' group by SerialNo,Technics,FtyName order by SerialNo ');
            Open;
          end;
          SCreateCDS(Self.ADOTemp, Self.CDS_2);
          SInitCDSData(Self.ADOTemp, Self.CDS_2);
        end;

      end;
    finally
      frmTradePlanFtyInPut.Free;
    end;
  end;

end;

procedure TfrmTradePlanInPut.ToolButton12Click(Sender: TObject);
var
  i: Integer;
begin
  i := CDS_2.RecordCount;

  try
    frmZDYHelpSelXR := TfrmZDYHelpSelXR.Create(Application);
    with frmZDYHelpSelXR do
    begin

      flag := 'TPTName';
      flagname := '测试项目';
      if ShowModal = 1 then
      begin
        while ClientDataSet2.Locate('SSel', True, []) do
        begin
          with Self.CDS_2 do
          begin
            append;
            FieldByName('SSel').Value := false;
            FieldByName('SerialNo').Value := i + 1;
            FieldByName('TPTName').Value := frmZDYHelpSelXR.ClientDataSet2.FieldByName('ZDYName').AsString;
            Post;
          end;
          ClientDataSet2.Delete;
          i := i + 1
        end;
      end;
    end;
  finally
    frmZDYHelpSelXR.Free;
  end;

end;

procedure TfrmTradePlanInPut.ToolButton13Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if SaveData() then
  begin
    frmTradeInspSet := TfrmTradeInspSet.Create(SELF);
    with frmTradeInspSet do
    begin
      FDataID := trim(SELF.FMainId);
//    Tsave.Tag := 2;
      if ShowModal = 1 then
      begin

      end;
      Free;
    end;
  end;

end;

procedure TfrmTradePlanInPut.ToolButton14Click(Sender: TObject);
begin
  if CDS_2.IsEmpty then
    Exit;
  if CDS_2.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除选择的数据吗？', '提示', 32 + 4) <> IDYES then
    exit;
  CDS_2.DisableControls;
  with CDS_2 do
  begin
    First;
    while Locate('SSel', True, []) do
    begin
      if Trim(CDS_2.fieldbyname('TPTID').AsString) <> '' then
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete Trade_Plan_Technical where TPTID=''' + Trim(CDS_2.fieldbyname('TPTID').AsString) + '''');
          ExecSQL;
        end;
      end;
      Delete;
    end;
  end;
  CDS_2.EnableControls;
  SetXH(CDS_2);
end;

procedure TfrmTradePlanInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
begin
  if GetLSNo(ADOCmd, maxno, 'JS', 'Trade_Plan_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  i := CDS_1.RecordCount;
  CopyAddRow(Tv1, CDS_1);
  with CDS_1 do
  begin
    Edit;
    FieldByName('SSel').Value := false;
    FieldByName('SerialNo').Value := i + 1;
    FieldByName('AttName').Value := '';
    FieldByName('SubId').Value := maxno;
    Post;
  end;

end;

procedure TfrmTradePlanInPut.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
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Trade_Plan_Sub where isnull(ORDSID,'''')<>'''' and ORDSID=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');
        Open;
      end;
      if ADOTemp.IsEmpty = False then
      begin
        Application.MessageBox('已做包装指示单不能删除数据!', '提示', 0);
        Exit;
      end;
      if Trim(CDS_1.fieldbyname('SubId').AsString) <> '' then
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;

//          sql.Add('insert into Trade_Plan_Sub_Del select * from Trade_Plan_Sub where SubId=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');

          sql.Add('delete Trade_Plan_Sub where SubId=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');

          ExecSQL;
        end;
      end;
      Delete;
    end;
  end;
  CDS_1.EnableControls;
  SetXH(CDS_1);
end;

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

procedure TfrmTradePlanInPut.ToolButton4Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3, FFID: string;
begin
  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;
    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  CDS_LM.EmptyDataSet;

  for j := 0 to Tv1.ColumnCount - 1 do
  begin
    with CDS_LM do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
  end;

  try
    for i := 1 to 50 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[1, i].value) = '' then
        continue;
      if CDS_LM.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[1, i].value), []) then
      begin
        with CDS_LM do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    for i := 2 to ExcelRowCount do
    begin
      with CDS_1 do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if CDS_LM.FieldByName('LXH').AsInteger > 0 then
            CDS_1.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
          CDS_LM.Next;
        end;
        CDS_1.FieldByName('SSel').Value := false;
        Post;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    exit;
  end;
  SetXH(CDS_1);
end;

procedure TfrmTradePlanInPut.ToolButton5Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if GetLSNo(ADOCmd, maxno, 'JS', 'Trade_Plan_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('SubId').Value := maxno;
//            Fieldbyname('C_Code').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
            self.C_Name.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
            self.C_EName.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_EName').AsString);
            self.C_code.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_code').AsString);
            self.C_Spec.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
            self.C_Composition.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
            self.C_EComposition.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_EComposition').AsString);

            self.C_Width.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
            self.C_GramWeight.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
            self.GC_Name.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('GC_Name').AsString);
            self.CraftCode.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('CraftCode').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('ShrinkageRate').value := frmClothInfoSel.CDS_1.Fieldbyname('ShrinkageRate').ASfloat;
            Fieldbyname('SerialNo').value := 999;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;
  SetXH(CDS_1);
end;

procedure TfrmTradePlanInPut.ToolButton6Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
begin
  if GetLSNo(ADOCmd, maxno, 'JS', 'Trade_Plan_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  i := CDS_1.RecordCount;
  with CDS_1 do
  begin
    Append;
    FieldByName('SSel').Value := false;
    FieldByName('SerialNo').Value := i + 1;
    FieldByName('AttName').Value := '';
    FieldByName('SubId').Value := maxno;
    Post;
  end;
end;

procedure TfrmTradePlanInPut.ToolButton7Click(Sender: TObject);
var
  I, J, K: Integer;
  maxno: string;
begin

  K := CDS_1.RecordCount + 1;

  try
    frmPatternColorHelp := TfrmPatternColorHelp.Create(Application);
    with frmPatternColorHelp do
    begin
      if ShowModal = 1 then
      begin
        for I := STRTOINTDEF(QSHX.Text, 1) to STRTOINTDEF(JSHX.Text, 1) do
        begin
          for J := 1 to STRTOINTDEF(YSSL.Text, 1) do
          begin
            with CDS_1 do
            begin
              if GetLSNo(ADOCmd, maxno, 'JS', 'Trade_Plan_Sub', 4, 1) = False then
              begin
                ADOCmd.Connection.RollbackTrans;
                Application.MessageBox('取子流水号失败！', '提示', 0);
                Exit;
              end;
              Append;
              FieldByName('SSel').Value := false;
              FieldByName('SerialNo').Value := K;
              FieldByName('SubId').Value := maxno;
              if STRTOINTDEF(QSHX.Text, 0) <> 0 then
                FieldByName('C_Pattern').Value := 'D#' + inttostr(I);
              if STRTOINTDEF(YSSL.Text, 0) <> 0 then
                FieldByName('C_ColorNo').Value := 'C#' + inttostr(J);
              FieldByName('OrdPiece').Value := STRTOINTDEF(ps.Text, 0);
              FieldByName('OrdQty').Value := STRTOINTDEF(sl.Text, 0);

              FieldByName('C_GS').Value := STRTOINTDEF(GS.Text, 0);
              FieldByName('C_GL').Value := STRTOINTDEF(GL.Text, 0);
              FieldByName('OrdPiece').Value := STRTOINTDEF(GS.Text, 0) * STRTOINTDEF(GL.Text, 0);
              Post;
            end;
            K := K + 1;
          end;
        end;
        SetXH(CDS_1);
      end;
    end;
  finally
    frmPatternColorHelp.Free;
  end;
end;

procedure TfrmTradePlanInPut.ToolButton8Click(Sender: TObject);
begin
  with CDS_1 do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      upOrdRawQty();
      Next;
    end;
    EnableControls;
  end;
end;

procedure TfrmTradePlanInPut.ToolButton9Click(Sender: TObject);
begin
  try
    frmkuannoImport := TfrmkuannoImport.Create(Application);
    with frmkuannoImport do
    begin
      FCTID := Trim(FMainId);
      if ShowModal = 1 then
      begin
//        Self.InitGrid();

      end;
    end;
  finally
    frmkuannoImport.Free;
  end;
  INITMX();
end;

procedure TfrmTradePlanInPut.fClick(Sender: TObject);
begin
  try
    frmTotalClothStk := TfrmTotalClothStk.Create(Application);
    with frmTotalClothStk do
    begin
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTotalClothStk.Free;
  end;

end;

procedure TfrmTradePlanInPut.torquePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'torque';
      flagname := '扭力';
      if ShowModal = 1 then
      begin
        torque.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

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

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

  finally
    frmCompanySel.Free;
  end;
end;

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

procedure TfrmTradePlanInPut.C_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
//        Self.C_Code.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
        Self.C_Name.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
        Self.C_Spec.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
        Self.C_Composition.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
        Self.C_Width.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
        Self.C_GramWeight.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;
end;

procedure TfrmTradePlanInPut.EnvironmentalProtectionPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'EnvironmentalProtection';
      flagname := '环保要求';
      if ShowModal = 1 then
      begin
        EnvironmentalProtection.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTradePlanInPut.OrdNoteDblClick(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(inttostr(i) + '.' + FieldByName('ZDYName').AsString)      //inttostr(i)+'.'+
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmTradePlanInPut.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 TfrmTradePlanInPut.LabPackageDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';

end;

procedure TfrmTradePlanInPut.LabPackagePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmTradeLabelSel := TfrmTradeLabelSel.Create(Application);
    with frmTradeLabelSel do
    begin
      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(frmTradeLabelSel.CDS_Label.fieldbyname('labelCaption').AsString);

      end;
    end;
  finally
    frmTradeLabelSel.Free;
  end;
end;

procedure TfrmTradePlanInPut.LabVolumeDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';

end;

procedure TfrmTradePlanInPut.N5Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);
end;

procedure TfrmTradePlanInPut.N6Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);
end;

procedure TfrmTradePlanInPut.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 TfrmTradePlanInPut.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 TfrmTradePlanInPut.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 TfrmTradePlanInPut.v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FOrgQty, FShrinkageRate, FQty: Double;        //个数，单价，金额
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

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

  end;
  upOrdRawQty();
end;

procedure TfrmTradePlanInPut.YCLFactoryBtnUpClick(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 TfrmTradePlanInPut.btnConSelClick(Sender: TObject);
var
  FConMainId, FMPRTCode, maxno: string;
begin
  try
    frmContractSel := TfrmContractSel.Create(Application);
    with frmContractSel do
    begin
      fconNo := self.ConNo.Text;
//      fC_Code := self.C_Code.Text;
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if self.CDS_1.locate('ConSId', trim(CDS_1.Fieldbyname('ConSId').AsString), []) then
          begin
            CDS_1.Edit;
            CDS_1.fieldbyname('SSel').value := false;
            continue;
          end;

          if GetLSNo(ADOCmd, maxno, 'JS', 'Trade_Plan_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;

          self.SalesId.Text := Trim(frmContractSel.CDS_1.fieldbyname('SalesId').AsString);
          self.Saleser.Text := Trim(frmContractSel.CDS_1.fieldbyname('Saleser').AsString);
          self.TallyId.Text := Trim(frmContractSel.CDS_1.fieldbyname('TallyId').AsString);
          self.Tallyer.Text := Trim(frmContractSel.CDS_1.fieldbyname('Tallyer').AsString);

          self.BuyConNo.Text := Trim(frmContractSel.CDS_1.fieldbyname('BuyConNo').AsString);
          self.CustName.Properties.LookupItems.Text := Trim(frmContractSel.CDS_1.fieldbyname('BuyNo').AsString);
          self.CustName.Text := Trim(frmContractSel.CDS_1.fieldbyname('BuyName').AsString);

//          self.PackNote.Text := Trim(frmContractSel.CDS_1.fieldbyname('PackNote').AsString);
          self.OurCoName.Properties.LookupItems.Text := Trim(frmContractSel.CDS_1.fieldbyname('Sellno').AsString);
          self.OurCoName.Text := Trim(frmContractSel.CDS_1.fieldbyname('Sellname').AsString);
//          self.OrderNo.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConNo').AsString);
          self.ConNo.Text := Trim(frmContractSel.CDS_1.fieldbyname('ConNo').AsString);
          self.C_Name.Text := trim(frmContractSel.CDS_1.Fieldbyname('C_Name').AsString);

          self.C_code.Text := trim(frmContractSel.CDS_1.Fieldbyname('C_code').AsString);

          self.C_EName.Text := trim(frmContractSel.CDS_1.Fieldbyname('C_EName').AsString);
          self.C_Spec.Text := trim(frmContractSel.CDS_1.Fieldbyname('C_Spec').AsString);
          self.C_Composition.Text := trim(frmContractSel.CDS_1.Fieldbyname('C_Composition').AsString);
          self.C_EComposition.Text := trim(frmContractSel.CDS_1.Fieldbyname('C_EComposition').AsString);
          self.C_Width.Text := trim(frmContractSel.CDS_1.Fieldbyname('C_Width').AsString);
          self.C_GramWeight.Text := trim(frmContractSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
          self.GC_Name.Text := trim(frmContractSel.CDS_1.Fieldbyname('GC_Name').AsString);
          self.CraftCode.Text := trim(frmContractSel.CDS_1.Fieldbyname('CraftCode').AsString);
          self.ConDate.Text := trim(frmContractSel.CDS_1.Fieldbyname('ConDate').AsString);
          self.DlyNote.Text := trim(frmContractSel.CDS_1.Fieldbyname('DlyNote').AsString);

          self.OrdUnit.ItemIndex := OrdUnit.Properties.Items.IndexOf(trim(frmContractSel.CDS_1.fieldbyname('ConUnit').AsString));

          with self.CDS_1 do
          begin
            Append;
            FieldByName('SSel').Value := false;
            FieldByName('SubId').Value := maxno;
//            Fieldbyname('C_Code').value := trim(frmContractSel.CDS_1.Fieldbyname('C_Code').AsString);
            Fieldbyname('SerialNo').value := trim(frmContractSel.CDS_1.Fieldbyname('SerialNo').AsString);
            Fieldbyname('ConNo').value := trim(frmContractSel.CDS_1.Fieldbyname('ConNo').AsString);
            Fieldbyname('ConMId').value := trim(frmContractSel.CDS_1.Fieldbyname('ConMId').AsString);
            Fieldbyname('ConSId').value := trim(frmContractSel.CDS_1.Fieldbyname('ConSId').AsString);
            Fieldbyname('C_ColorNo').value := trim(frmContractSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
            Fieldbyname('C_Color').value := trim(frmContractSel.CDS_1.Fieldbyname('C_Color').AsString);
            Fieldbyname('C_EColor').value := trim(frmContractSel.CDS_1.Fieldbyname('C_EColor').AsString);
            Fieldbyname('C_Pattern').value := trim(frmContractSel.CDS_1.Fieldbyname('C_Pattern').AsString);
            Fieldbyname('C_ColorConfirm').value := trim(frmContractSel.CDS_1.Fieldbyname('C_ColorConfirm').AsString);
            Fieldbyname('OrdQty').value := frmContractSel.CDS_1.Fieldbyname('ConQty').AsFloat;
            Fieldbyname('LessQty').value := frmContractSel.CDS_1.Fieldbyname('LessQty').value;
            Fieldbyname('MoreQty').value := frmContractSel.CDS_1.Fieldbyname('MoreQty').value;
            Fieldbyname('MoreLessUnit').value := frmContractSel.CDS_1.Fieldbyname('MoreLessUnit').value;
            Fieldbyname('C_StyleNo').value := frmContractSel.CDS_1.Fieldbyname('C_StyleNo').value;
            Fieldbyname('ShrinkageRate').value := frmContractSel.CDS_1.Fieldbyname('ShrinkageRate').value;
            Fieldbyname('Weight').value := frmContractSel.CDS_1.Fieldbyname('ConWeight').value;
            if OrdType.text <> '大货单' then
            begin
              Fieldbyname('ProcessingCost').value := frmContractSel.CDS_1.Fieldbyname('ConCost').value;
            end;
            Post;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

      end;
    end;
  finally
    frmContractSel.Free;
  end;
  SetXH(CDS_1);
end;

procedure TfrmTradePlanInPut.QtyNotePropertiesButtonClick(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 TfrmTradePlanInPut.SYRNameBtnUpClick(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
      fnote := True;
      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 TfrmTradePlanInPut.TV1Column18PropertiesEditValueChanged(
  Sender: TObject);
begin
  CDS_1.FieldByName(Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName)).value:=TcxCurrencyEdit(Sender).Value;
  upOrdRawQty();
end;

procedure TfrmTradePlanInPut.Tv1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FGS, FGL, FOrdPiece: Double;
  i: Integer;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  FOrdPiece := 0;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;

  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    FGL := CDS_1.fieldbyname('C_Gl').AsFloat;
    FGS := CDS_1.fieldbyname('C_GS').AsFloat;
    FOrdPiece := CDS_1.fieldbyname('OrdPiece').AsFloat;
  end;

  if Trim(FFieldName) <> 'OrdPiece' then
  begin
    FOrdPiece := (FGS * FGL);
  end;

  with CDS_1 do
  begin
    Edit;
    FieldByName('OrdPiece').Value := FOrdPiece;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmTradePlanInPut.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 TfrmTradePlanInPut.TV2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'T1' + Trim(Self.CDS_2.fieldbyname('TPTName').AsString);
      flagname := '检测方法';
      if ShowModal = 1 then
      begin
        Self.CDS_2.Edit;
        Self.CDS_2.FieldByName('TPTType').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradePlanInPut.TV2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      if DCode <> 'ADMIN' then
        ViewFlag := True;

      flag := 'T2' + Trim(Self.CDS_2.fieldbyname('TPTName').AsString);
      flagname := '检测方法';
      if ShowModal = 1 then
      begin
        Self.CDS_2.Edit;
        Self.CDS_2.FieldByName('TPTUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradePlanInPut.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('MGMainId').Value := '';
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

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

