unit U_TradePlanInPut;

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,
  dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, cxPC,
  System.ImageList, Vcl.ImgList, U_BaseInput, cxImage, cxDBEdit, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
  IdFTP, ShellAPI, dxSkinWXI, dxScrollbarAnnotations, 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,
  dxSkinXmas2008Blue, cxCalc;

type
  TfrmTradePlanInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    OrderNo: TEdit;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    Label3: TLabel;
    ConDate: TDateTimePicker;
    Label25: TLabel;
    ConDLYDate: TDateTimePicker;
    GPM_1: TcxGridPopupMenu;
    ADOQueryLabel: TADOQuery;
    Label51: TLabel;
    Label7: TLabel;
    cxDBImage1: TcxDBImage;
    Qry_ImgHX: TADOQuery;
    DS_ImgHX: TDataSource;
    ToolButton3: TToolButton;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    BuyConNo: TEdit;
    Label5: TLabel;
    C_Name: TEdit;
    Label6: TLabel;
    C_Composition: TEdit;
    Label9: TLabel;
    Con_GramWeight: TEdit;
    Label10: TLabel;
    Con_Width: TEdit;
    Label11: TLabel;
    IdFTP1: TIdFTP;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column11: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    VC_PRTPs: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    btnConSel: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Label14: TLabel;
    Label20: TLabel;
    CustName: TBtnEditC;
    OurCoName: TBtnEditC;
    ToolButton5: TToolButton;
    C_Code: TBtnEditC;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Label12: TLabel;
    Label13: TLabel;
    Label15: TLabel;
    RuleBeforeTare: TEdit;
    Coefficient: TEdit;
    RuleLenUnit: TComboBox;
    Label16: TLabel;
    RuleLaterTare: TEdit;
    Label17: TLabel;
    FtyName: TBtnEditC;
    Label18: TLabel;
    Technics: TBtnEditC;
    Follower: TEdit;
    Label19: TLabel;
    Label21: TLabel;
    ProcessingType: TComboBox;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Label22: TLabel;
    ShippMent: TEdit;
    Label24: TLabel;
    Label26: TLabel;
    Label23: TLabel;
    Label27: TLabel;
    OrdUnit: TComboBox;
    Label8: TLabel;
    FOrdNo: TEdit;
    Label29: TLabel;
    Label30: TLabel;
    OrdDate: TDateTimePicker;
    DLYDate: TDateTimePicker;
    Label31: TLabel;
    Label32: TLabel;
    Label35: TLabel;
    Label36: TLabel;
    ggft: TMemo;
    Label37: TLabel;
    tj: TMemo;
    Label38: TLabel;
    Label39: TLabel;
    jy: TEdit;
    hd: TEdit;
    Label40: TLabel;
    cm: TMemo;
    Label41: TLabel;
    jm: TMemo;
    Label42: TLabel;
    Label43: TLabel;
    Saleser: TBtnEditC;
    Tallyer: TBtnEditC;
    Label44: TLabel;
    Label45: TLabel;
    QTNote: TMemo;
    Currency: TComboBox;
    IsTax: TComboBox;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    C_Width: TBtnEditC;
    C_GramWeight: TBtnEditC;
    CY: TBtnEditC;
    ToolButton4: TToolButton;
    MainId: TEdit;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    GY: TBtnEditC;
    Label33: TLabel;
    Label34: TLabel;
    Label46: TLabel;
    KZPZ: TEdit;
    CPMC: TEdit;
    HXYS: TEdit;
    PZNote: TBtnEditC;
    Label47: TLabel;
    KJNote: TBtnEditC;
    Label48: TLabel;
    DBNote: TBtnEditC;
    ConNo: TBtnEditC;
    ADOQuery1: TADOQuery;
    Label28: TLabel;
    DBFS: TBtnEditC;
    Label49: TLabel;
    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 ScrollBox1Click(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure HYTTBtnUpClick(Sender: TObject);
    procedure KYTTBtnUpClick(Sender: TObject);
    procedure btnConSelClick(Sender: TObject);
    procedure edtGYBtnUpClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v1OrderUnitPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxDBImage1DblClick(Sender: TObject);
    procedure SYRNameBtnUpClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CustNameBtnUpClick(Sender: TObject);
    procedure OurCoNameBtnDnClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure C_CodeBtnUpClick(Sender: TObject);
    procedure C_CodeBtnDnClick(Sender: TObject);
    procedure FtyNameBtnUpClick(Sender: TObject);
    procedure TechnicsBtnUpClick(Sender: TObject);
    procedure ButtonEdit(Sender: TObject; AButtonIndex: Integer);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ShippMentKeyPress(Sender: TObject; var Key: Char);
    procedure ShippMentChange(Sender: TObject);
    procedure TallyerBtnUpClick(Sender: TObject);
    procedure TallyerBtnDnClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure PZNoteBtnUpClick(Sender: TObject);
    procedure ggftDblClick(Sender: TObject);
    procedure ConNoBtnUpClick(Sender: TObject);
    procedure OurCoNameBtnUpClick(Sender: TObject);
  private
    FSCXDH: string;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function getOrderNo(fconNo: string): string;
    procedure InitImageHX();
    procedure LookImage(FileName: string);
    function CheckData(): Boolean;
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo: string;
    FXS: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmTradePlanInPut: TfrmTradePlanInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_TradeSalesContractSel, U_CompanySel,
  U_ClothInfoSel, U_AttachmentUpload, U_ZDYHelpSel, U_TradeConSel;


{$R *.dfm}

procedure TfrmTradePlanInPut.InitImageHX();
begin
  with Qry_ImgHX do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where isnull(WBID,'''')<>'''' ');
    sql.Add('and WBID=(select HXSID from  SalesContract_Sub X where X.SubId=' + quotedstr(Trim(self.CDS_1.fieldbyname('ConSubId').AsString)) + ')');
    open;
  end;
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.ggftDblClick(Sender: TObject);
var
  fsj, ZYSC: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  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
        while frmZDYHelpSel.ClientDataSet1.Locate('SSel', true, []) do
        begin

          if trim(ZYSC) = '' then
            ZYSC := trim(ClientDataSet1.fieldbyname('ZDYName').AsString)
          else
            ZYSC := ZYSC + sLineBreak + trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

          TEdit(Sender).Text := ZYSC;
          frmZDYHelpSel.ClientDataSet1.delete;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmTradePlanInPut.TallyerBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

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

procedure TfrmTradePlanInPut.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradePlanInPut.InitData();
var
  FDate: TDateTime;
begin
  FDate := SGetServerDateTime(ADOTemp);
  OrdDate.DateTime := FDate;
  DlyDate.DateTime := FDate;
  ConDate.DateTime := FDate;
  ConDlyDate.DateTime := FDate;

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from V_Trade_Plan_Single_1 where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;

  SCSHData(ADOTemp, ScrollBox1, 2);
  SCSHData(ADOTemp, ScrollBox1, 1);
  SCSHData(ADOTemp, ScrollBox1, 9);
  SCSHData(ADOTemp, ScrollBox1, 7);
  OurCoName.TxtCode := Trim(ADOTemp.FieldByName('OurCoNo').AsString);
  CustName.TxtCode := Trim(ADOTemp.FieldByName('CustNo').AsString);
  Saleser.TxtCode := Trim(ADOTemp.FieldByName('SalesId').AsString);
  Tallyer.TxtCode := Trim(ADOTemp.FieldByName('TallyId').AsString);
//  FtyName.TxtCode := Trim(ADOTemp.FieldByName('FtyNo').AsString);

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *  from Trade_Plan_Sub where 1=1 ');
    if PState = 1 then
    begin
      sql.Add(' and MainId=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1<>1');
    end;
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_1);
  SInitCDSData(ADOTemp, CDS_1);

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *  from FJ_File where 1=1 ');
    if PState = 1 then
    begin
      sql.Add(' and WBId=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1<>1');
    end;
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_2);
  SInitCDSData(ADOTemp, CDS_2);

  if CopyInt <> 99 then
  begin
    if PState = 1 then
    begin
      C_Code.ReadOnly := True;
      C_Name.ReadOnly := True;
      Con_Width.ReadOnly := True;
      Con_GramWeight.ReadOnly := True;
      C_Composition.ReadOnly := True;
      ConNo.Enabled := false;
      C_Code.Enabled := false;
      C_Name.Enabled := false;
    end;
  end;

  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := OrdDate.DateTime;
    OrderNo.Text := '自动生成';
    OrderNo.Enabled := True;
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrderNo.Text := '自动生成';
    OrderNo.Enabled := True;

    OrdDate.DateTime := FDate;

    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        Post;
        Next;
      end;
    end;

  end;
end;

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

procedure TfrmTradePlanInPut.FormShow(Sender: TObject);
var
  i: Integer;
begin
  inherited;
  readCxGrid(TRIM(SELF.Caption), Tv1, '生产指示单管理');
  OrderNo.Text := '自动生成';
  Saleser.Text := Trim(DName);
  Saleser.TxtCode := Trim(DCode);
  fFlileFlag := UserDataFlag + 'HX';
  InitData();
end;

procedure TfrmTradePlanInPut.FtyNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.FtyName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
        self.FtyName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        self.Follower.Text := Trim(CDS_1.fieldbyname('Follower').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

function TfrmTradePlanInPut.SaveData(): Boolean;
var
  maxno, Smaxno, maxorderno, MaxFNo, Fdate, FOrdeNo, FProcessingType: string;
begin

  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表

    if Trim(FMainId) = '' then
    begin
      Fdate := FormatDateTime('yy', SGetServerDate(ADOTemp));
      FProcessingType := ProcessingType.Text;
      if (FProcessingType <> '大货生产') and (FProcessingType <> '大货生产(成品)') then
      begin
//        if (FProcessingType <> '大货生产(成品)') then
//        begin
        if GetLSNo(ADOCmd, maxno, 'FDH', 'Trade_Plan_Main', 5, 0) = False then
          raise Exception.Create('取最大号失败!');
        with ADOQuery1 do
        begin
          Close;
          sql.Clear;
          SQL.Add('select Right(''' + Trim(maxno) + ''',Len(''' + Trim(maxno) + ''')-3) AS S');
          Open;
        end;
        FOrderNo := Trim(Fdate) + ADOQuery1.FieldByName('S').ASString;

        if FProcessingType = '公司备货' then
        begin
          OrderNo.text := 'BH' + Trim(FOrderNo);
        end
        else if FProcessingType = '自主开发' then
        begin
          OrderNo.text := 'KF' + Trim(FOrderNo);
        end
        else if FProcessingType = '客需开发' then
        begin
          OrderNo.text := 'KX' + Trim(FOrderNo);
        end;
//        end
//        else
//        begin
//          if GetLSNo(ADOCmd, maxno, Trim(ConNo.Text), 'Trade_Plan_Main', 2, 0) = False then
//            raise Exception.Create('取最大号失败!');
//          with ADOQuery1 do
//          begin
//            Close;
//            sql.Clear;
//            SQL.Add('select Cast(Right(''' + Trim(maxno) + ''',2) AS INT) AS S');
//            Open;
//          end;
//          ForderNo := 'C-' + Trim(ConNo.Text) + '-' + ADOQuery1.FieldByName('S').ASString;
//          OrderNo.text := Trim(FOrderNo);
//        end;
      end
      else
      begin
        if GetLSNo(ADOCmd, maxno, Trim(ConNo.Text), 'Trade_Plan_Main', 2, 0) = False then
          raise Exception.Create('取最大号失败!');
        with ADOQuery1 do
        begin
          Close;
          sql.Clear;
          SQL.Add('select Cast(Right(''' + Trim(maxno) + ''',2) AS INT) AS S');
          Open;
        end;
        if ProcessingType.Text = '大货生产(成品)' then
        begin
          ForderNo := 'C-' + Trim(ConNo.Text) + '-' + ADOQuery1.FieldByName('S').ASString;
        end
        else
        begin
          ForderNo := Trim(ConNo.Text) + '-' + ADOQuery1.FieldByName('S').ASString;
        end;
        OrderNo.text := Trim(FOrderNo);
      end;
    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 Trim(FMainId) = '' then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd, 'Trade_Plan_Main', ScrollBox1, 2);
      FieldByName('MainId').Value := Trim(maxno);
      FieldByName('OurCoNo').Value := Trim(OurCoName.TxtCode);
      FieldByName('CustNo').Value := Trim(CustName.TxtCode);
      FieldByName('ConNo').Value := Trim(ConNo.Text);
//      FieldByName('Saleser').Value := Trim(Saleser.Text);
      FieldByName('SalesId').Value := Trim(Saleser.TxtCode);
//      FieldByName('Tallyer').Value := Trim(Tallyer.Text);
      FieldByName('TallyId').Value := Trim(Tallyer.TxtCode);
//      FieldByName('ShippMent').Value := Trim(ShippMent.text);
      Post;
    end;

/////////////////////               保存子表            ///////////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin

        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;
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Trade_Plan_Sub', 0);

          RTSetsavedata(ADOCmd, 'Trade_Plan_Sub', ScrollBox1, 7);
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(Smaxno);
//          FieldByName('C_Code').Value := Trim(C_Code.Text);
//          FieldByName('C_Name').Value := Trim(C_Name.Text);
          FieldByName('ConSId').Value := Trim(CDS_1.fieldbyname('ConSId').AsString);
          FieldByName('ConMId').Value := Trim(CDS_1.fieldbyname('ConMId').AsString);
          Post;
          with CDS_1 do
          begin
            Edit;
            fieldbyname('SubId').Value := Trim(Smaxno);
            Post;
          end;

        end;
        Next;
      end;
    end;
/////////////////////               保存子表           ///////////////////////////
/////////////////////               保存加工厂            ///////////////////////////
//    with ADOCmd do
//    begin
//      Close;
//      sql.Clear;
//      SQL.Add('select * from Trade_Plan_Fty where TPFId=''' + Trim(FMainId) + '''');
//      Open;
//    end;
//    with ADOCmd do
//    begin
//      if ADOCmd.IsEmpty then
//      begin
//        Append;
//      end
//      else
//        Edit;
//      RTSetsavedata(ADOCmd, 'Trade_Plan_Fty', ScrollBox1, 5);
//      FieldByName('FtyNo').Value := Trim(FtyName.TxtCode);
//      FieldByName('MainId').Value := Trim(FMainId);
//      FieldByName('TPFId').Value := Trim(FMainId);
//      Post;
//    end;
/////////////////////               保存加工厂           ///////////////////////////
///
///////////////       保存打卷设置       ///////////////
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Trade_Cloth_Inspect_Rule where RuleID=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if ADOCmd.IsEmpty then
      begin
        Append;
      end
      else
        Edit;
      RTSetsavedata(ADOCmd, 'Trade_Cloth_Inspect_Rule', ScrollBox1, 9);
      FieldByName('RuleID').Value := Trim(FMainId);
      Post;
    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('select * from BS_Cloth_Info where C_Code=''' + Trim(C_Code.Text) + '''');
      Open;
    end;
    if ADOCmd.IsEmpty then
      raise Exception.Create('产品编号不存在，不能保存！');

    FMainId := Trim(maxno);

    ADOCmd.Connection.CommitTrans;
    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;

function TfrmTradePlanInPut.CheckData(): Boolean;
var
  inputNum: Float64;
begin
  Result := true;
  if CDS_1.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Result := false;
    exit;
  end;

  if ProcessingType.Text = '' then
  begin
    Application.MessageBox('加工类型不能为空!', '提示', 0);
    Result := false;
    exit;
  end;

  if OrdUnit.Text = '' then
  begin
    Application.MessageBox('单位不能为空!', '提示', 0);
    Result := false;
    exit;
  end;

  if ProcessingType.text = '大货生产' then
  begin
    if ConNo.Text = '' then
    begin
      Application.MessageBox('大货单合同号不能为空!', '提示', 0);
      Result := false;
      exit;
    end;
  end;

  if OurCoName.Text = '' then
  begin
    Application.MessageBox('经营单位不能为空!', '提示', 0);
    Result := false;
    exit;
  end;

  if CPMC.Text = '' then
  begin
    Application.MessageBox('成品米长不能为空!', '提示', 0);
    Result := false;
    exit;
  end;

  if OrderNo.Text = '' then
  begin
    Application.MessageBox('计划单号不能为空!', '提示', 0);
    Result := false;
    exit;
  end;

  if Currency.Text = '' then
  begin
    Application.MessageBox('币种不能为空!', '提示', 0);
    Result := false;
    exit;
  end;

  if IsTax.Text = '' then
  begin
    Application.MessageBox('是否含税不能为空!', '提示', 0);
    Result := false;
    exit;
  end;

  if CDS_1.Locate('OrdQty', null, []) then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Result := false;
    exit;
  end;

  if not TryStrToFloat(cpmc.Text, inputNum) then
  begin
    Application.MessageBox('成品米长请输入数字!', '提示', 0);
    Result := false;
    exit;
  end;

end;

procedure TfrmTradePlanInPut.TBSaveClick(Sender: TObject);
var
  MAXNO1: string;
begin
  if PState <> 1 then
  begin
    OrdDate.SetFocus;
  end;

  if CheckData() = false then
    exit;


//  if CDS_1.Locate('C_Color', null, []) then
//  begin
//    Application.MessageBox('颜色不能为空!', '提示', 0);
//    Exit;
//  end;

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

procedure TfrmTradePlanInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin

  CopyAddRow(Tv1, CDS_1);
  with CDS_1 do
  begin
    Edit;
    FieldByName('SSel').Value := false;
//    FieldByName('hxFile').Value := '';
//    FieldByName('TPPS').Value := 0;
    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_Cloth_Inspect where isnull(SubId,'''')<>'''' and SubId=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');
        Open;
      end;
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Pur_ClothPlan_Main A  left join Pur_ClothPlan_Sub B on A.PurMid=B.PurMid');
        sql.Add('where Status<>''-1'' and isnull(PurSid,'''')<>'''' and Subid=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');
        Open;
      end;

      if ADOTemp.IsEmpty = False then
      begin
        Application.MessageBox('已检验不能删除数据!', '提示', 0);
        Exit;
      end;

      if ADOQuery1.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;

end;

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

end;

procedure TfrmTradePlanInPut.ToolButton4Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      if MainId.text <> '' then
        FEditAuthority := True;
      fkeyNO := Trim(Self.MainId.text);
      FType := '指示单';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select *  from FJ_File where 1=1 ');
      if PState = 1 then
      begin
        sql.Add(' and WBId=''' + Trim(FMainId) + '''');
      end;
      if PState = 0 then
      begin
        sql.Add(' and 1<>1');
      end;
      Open;
    end;
    SCreateCDS(ADOTemp, CDS_2);
    SInitCDSData(ADOTemp, CDS_2);
  end;

end;

procedure TfrmTradePlanInPut.ToolButton5Click(Sender: TObject);
begin
  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('ConSubId', trim(CDS_1.Fieldbyname('SubId').AsString), []) then
          begin
            CDS_1.Edit;
            CDS_1.fieldbyname('SSel').value := false;
            continue;
          end;
          with self.CDS_1 do
          begin
            append;
            FieldByName('SSel').Value := false;
            C_Code.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
            C_Name.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
//            C_Spec.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
            C_Composition.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
            C_Width.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
            C_GramWeight.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
            OrdUnit.ItemIndex := OrdUnit.Items.IndexOf(trim(frmClothInfoSel.CDS_1.fieldbyname('ConUnit').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('OrdQty').value := frmClothInfoSel.CDS_1.Fieldbyname('ConQty').AsFloat;

            Fieldbyname('DDQtyUnit').value := frmClothInfoSel.CDS_1.Fieldbyname('SCSUnit').AsString;
            Fieldbyname('OrderUnit').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('SCSUnit').AsString);

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

end;

procedure TfrmTradePlanInPut.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 TfrmTradePlanInPut.TechnicsBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      fnote := True;
      flag := 'Technics';
      flagname := '工艺';
      if ShowModal = 1 then
      begin
        Technics.text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

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

procedure TfrmTradePlanInPut.CustNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if Self.canshu1 = '录入' then
        FAuthority := '理单业务';
      if ShowModal = 1 then
      begin
        self.CustName.TxtCode := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.CustName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
        self.Saleser.TxtCode := Trim(frmCompanySel.CDS_1.fieldbyname('SalesId').AsString);
        self.Saleser.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Saleser').AsString);
        self.Tallyer.TxtCode := Trim(frmCompanySel.CDS_1.fieldbyname('TallyId').AsString);
        self.Tallyer.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Tallyer').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradePlanInPut.CustomerNoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

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

procedure TfrmTradePlanInPut.C_CodeBtnDnClick(Sender: TObject);
begin
  Self.C_Code.Text := '';
  Self.C_Name.Text := '';
//  Self.C_Spec.Text := '';
  Self.C_Composition.Text := '';
  Self.C_Width.Text := '';
  Self.C_GramWeight.Text := '';
end;

procedure TfrmTradePlanInPut.C_CodeBtnUpClick(Sender: TObject);
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.Con_Width.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
        Self.Con_GramWeight.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;

end;

procedure TfrmTradePlanInPut.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 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.v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_Color';
      flagname := '颜色';
      if ShowModal = 1 then
      begin
        CDS_1.Edit;
        CDS_1.fieldbyname('C_Color').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        CDS_1.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

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

procedure TfrmTradePlanInPut.btnConSelClick(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
begin
  if CDS_1.IsEmpty = False then
  begin
    CDS_1.EmptyDataSet;
  end;

  try
    frmTradeSalesContractSel := TfrmTradeSalesContractSel.Create(Application);
    with frmTradeSalesContractSel do
    begin
//      fconNo := ConNo.Text;
      canshu2 := self.canshu2;
      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;

          with self.CDS_1 do
          begin

            Append;
            FieldByName('SSel').Value := false;
//            Fieldbyname('ConNo').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConNo').AsString);
            Fieldbyname('ConMId').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConMId').AsString);
            Fieldbyname('ConSId').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConSId').AsString);

            ConDate.DateTime := frmTradeSalesContractSel.CDS_1.Fieldbyname('ConDate').AsDatetime;
            ConDlyDate.DateTime := frmTradeSalesContractSel.CDS_1.Fieldbyname('DlyDate').AsDatetime;
            ShippMent.text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ShippMent').AsString);
            Saleser.text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('Saleser').AsString);
            Saleser.Txtcode := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('SalesId').AsString);
            Tallyer.text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('Tallyer').AsString);
            Tallyer.Txtcode := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('TallyId').AsString);

            Currency.ItemIndex := Currency.Items.IndexOf(trim(frmTradeSalesContractSel.CDS_1.fieldbyname('Currency').AsString));
            IsTax.ItemIndex := IsTax.Items.IndexOf(trim(frmTradeSalesContractSel.CDS_1.fieldbyname('IsTax').AsString));
            OurCoName.text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('SellName').AsString);
            OurCoName.Txtcode := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('SellNo').AsString);
            CustName.text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('BuyName').AsString);
            CustName.Txtcode := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('BuyNo').AsString);

            self.ConNo.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConNo').AsString);
            BuyConNo.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('BuyConNO').AsString);
            C_Code.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Code').AsString);
            Self.C_Name.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Name').AsString);
//            RuleLenUnit.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConQty').AsString);
            C_Composition.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Composition').AsString);
            Con_Width.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Width').AsString);
            Con_GramWeight.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
            OrdUnit.ItemIndex := OrdUnit.Items.IndexOf(trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConUnit').AsString));
            if frmTradeSalesContractSel.CDS_1.fieldbyname('ConUnit').AsString = 'Kg' then
            begin
              RuleLenUnit.ItemIndex := RuleLenUnit.Items.IndexOf('M');
            end
            else
            begin
              RuleLenUnit.ItemIndex := RuleLenUnit.Items.IndexOf(trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConUnit').AsString));
            end;

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

            Fieldbyname('OrdQty').value := frmTradeSalesContractSel.CDS_1.Fieldbyname('ConQty').AsFloat;

//            Fieldbyname('DDQtyUnit').value := frmTradeSalesContractSel.CDS_1.Fieldbyname('ConUnit').AsString;
            Fieldbyname('OrdUnit').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConUnit').AsString);

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

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

procedure TfrmTradePlanInPut.ScrollBox1Click(Sender: TObject);
begin
  OrdDate.SetFocus;
end;

procedure TfrmTradePlanInPut.ShippMentChange(Sender: TObject);
begin
  if ShippMent.text <> '' then
  begin
    Dlydate.DateTime := OrdDate.DateTime + StrToFloat(Shippment.text);
  end;

end;

procedure TfrmTradePlanInPut.ShippMentKeyPress(Sender: TObject; var Key: Char);
begin
  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
  begin
    Key := #0;
  end
  else if (pos('.', TEdit(Sender).text) <> 0) then
  begin
    if Key = '.' then
    begin
      Key := #0;
    end;
  end;

end;

procedure TfrmTradePlanInPut.OurCoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmTradePlanInPut.OurCoNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin
        self.OurCoName.TxtCode := 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.PZNoteBtnUpClick(Sender: TObject);
var
  fsj, ZYSC: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  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
        while frmZDYHelpSel.ClientDataSet1.Locate('SSel', true, []) do
        begin

          if trim(ZYSC) = '' then
            ZYSC := trim(ClientDataSet1.fieldbyname('ZDYName').AsString)
          else
            ZYSC := ZYSC + ',' + trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

          TEdit(Sender).Text := ZYSC;
          frmZDYHelpSel.ClientDataSet1.delete;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

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

procedure TfrmTradePlanInPut.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  frmClothInfoSel := TfrmClothInfoSel.Create(self);
//  with frmClothInfoSel do
//  begin
//    if showmodal = 1 then
//    begin
//      CDS_1.Edit;
//      CDS_1.fieldbyname('MPRTCode').Value := CDS_1.fieldbyname('CYNO').asstring;
//      CDS_1.fieldbyname('MPRTCodeName').Value := CDS_1.fieldbyname('CYName').asstring;
//      CDS_1.fieldbyname('MPRTSpec').Value := CDS_1.fieldbyname('CYSpec').asstring;
//      CDS_1.fieldbyname('MPRTCF').Value := CDS_1.fieldbyname('CYCF').asstring;
//      CDS_1.fieldbyname('PRTColor').Value := CDS_1.fieldbyname('CYColor').asstring;
//      CDS_1.fieldbyname('PRTHX').Value := CDS_1.fieldbyname('CYHX').asstring;
//      CDS_1.fieldbyname('MPRTMF').Value := CDS_1.fieldbyname('CYMF').asstring;
//      CDS_1.fieldbyname('MPRTKZ').Value := CDS_1.fieldbyname('CYKZ').asstring;
//      CDS_1.fieldbyname('OrderUnit').Value := CDS_1.fieldbyname('CYPUnit').asstring;
//      CDS_1.fieldbyname('SuoLv').Value := CDS_1.fieldbyname('SuoLv').asstring;
//      CDS_1.fieldbyname('GcHuaNo').Value := CDS_1.fieldbyname('GcHua').asstring;
//      CDS_1.fieldbyname('GsHuaNo').Value := CDS_1.fieldbyname('GsHua').asstring;
//      CDS_1.Post;
//    end;
//    free;
//  end;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'XH';
      flagname := '选号';
      if ShowModal = 1 then
      begin
        CDS_1.Edit;
        CDS_1.fieldbyname('XH').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        CDS_1.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

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

end;

procedure TfrmTradePlanInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
//  InitImageHX();
end;

procedure TfrmTradePlanInPut.ConNoBtnUpClick(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
begin
  if CDS_1.IsEmpty = False then
  begin
    CDS_1.EmptyDataSet;
  end;

  try
    frmTradeConSel := TfrmTradeConSel.Create(Application);
    with frmTradeConSel do
    begin
      canshu2 := self.canshu2;
      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;

          with self.CDS_1 do
          begin

            Append;
            FieldByName('SSel').Value := false;
            OrderNo.Text := trim(frmTradeConSel.CDS_1.Fieldbyname('ConNo').AsString);
            Fieldbyname('ConMId').value := trim(frmTradeConSel.CDS_1.Fieldbyname('ConMId').AsString);
            Fieldbyname('ConSId').value := trim(frmTradeConSel.CDS_1.Fieldbyname('ConSId').AsString);

            ConDate.DateTime := frmTradeConSel.CDS_1.Fieldbyname('ConDate').AsDatetime;
            ConDlyDate.DateTime := frmTradeConSel.CDS_1.Fieldbyname('DlyDate').AsDatetime;
            ShippMent.text := trim(frmTradeConSel.CDS_1.Fieldbyname('ShippMent').AsString);
            Saleser.text := trim(frmTradeConSel.CDS_1.Fieldbyname('Saleser').AsString);
            Saleser.Txtcode := trim(frmTradeConSel.CDS_1.Fieldbyname('SalesId').AsString);
            Tallyer.text := trim(frmTradeConSel.CDS_1.Fieldbyname('Tallyer').AsString);
            Tallyer.Txtcode := trim(frmTradeConSel.CDS_1.Fieldbyname('TallyId').AsString);

            Currency.ItemIndex := Currency.Items.IndexOf(trim(frmTradeConSel.CDS_1.fieldbyname('Currency').AsString));
            IsTax.ItemIndex := IsTax.Items.IndexOf(trim(frmTradeConSel.CDS_1.fieldbyname('IsTax').AsString));
            OurCoName.text := trim(frmTradeConSel.CDS_1.Fieldbyname('SellName').AsString);
            OurCoName.Txtcode := trim(frmTradeConSel.CDS_1.Fieldbyname('SellNo').AsString);
            CustName.text := trim(frmTradeConSel.CDS_1.Fieldbyname('BuyName').AsString);
            CustName.Txtcode := trim(frmTradeConSel.CDS_1.Fieldbyname('BuyNo').AsString);

            self.ConNo.Text := trim(frmTradeConSel.CDS_1.Fieldbyname('ConNo').AsString);
            BuyConNo.Text := trim(frmTradeConSel.CDS_1.Fieldbyname('BuyConNO').AsString);
            C_Code.Text := trim(frmTradeConSel.CDS_1.Fieldbyname('C_Code').AsString);
            Self.C_Name.Text := trim(frmTradeConSel.CDS_1.Fieldbyname('C_Name').AsString);
//            RuleLenUnit.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('ConQty').AsString);
            C_Composition.Text := trim(frmTradeConSel.CDS_1.Fieldbyname('C_Composition').AsString);
            Con_Width.Text := trim(frmTradeConSel.CDS_1.Fieldbyname('C_Width').AsString);
            Con_GramWeight.Text := trim(frmTradeConSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
            OrdUnit.ItemIndex := OrdUnit.Items.IndexOf(trim(frmTradeConSel.CDS_1.fieldbyname('ConUnit').AsString));
            if frmTradeConSel.CDS_1.fieldbyname('ConUnit').AsString = 'Kg' then
            begin
              RuleLenUnit.ItemIndex := RuleLenUnit.Items.IndexOf('M');
            end
            else
            begin
              RuleLenUnit.ItemIndex := RuleLenUnit.Items.IndexOf(trim(frmTradeConSel.CDS_1.fieldbyname('ConUnit').AsString));
            end;

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

            Fieldbyname('OrdQty').value := frmTradeConSel.CDS_1.Fieldbyname('ConQty').AsFloat;

//            Fieldbyname('DDQtyUnit').value := frmTradeSalesContractSel.CDS_1.Fieldbyname('ConUnit').AsString;
            Fieldbyname('OrdUnit').value := trim(frmTradeConSel.CDS_1.Fieldbyname('ConUnit').AsString);

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

      end;
    end;
  finally
    frmTradeConSel.Free;
  end;
end;

procedure TfrmTradePlanInPut.ButtonEdit(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_ColorNo';
      flagname := '色号';
      if ShowModal = 1 then
      begin
        CDS_1.Edit;
        CDS_1.fieldbyname('C_ColorNo').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        CDS_1.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmTradePlanInPut.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_Pattern';
      flagname := '花型';
      if ShowModal = 1 then
      begin
        CDS_1.Edit;
        CDS_1.fieldbyname('C_Pattern').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        CDS_1.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure 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(TEdit(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
        TEdit(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(TEdit(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
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

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

end.

