unit U_TradePlanColorInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Winapi.ShellAPI, ComObj, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter,
  U_FormLayOutDesign, 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, cxImageList, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinBasic, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkroom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinOffice2016Colorful,
  dxSkinOffice2016Dark, dxSkinOffice2019Black, dxSkinOffice2019Colorful,
  dxSkinOffice2019DarkGray, dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringtime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinTheBezier, dxSkinValentine, dxSkinVisualStudio2013Blue,
  dxSkinVisualStudio2013Dark, dxSkinVisualStudio2013Light, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinWXI, dxSkinXmas2008Blue;

type
  TfrmTradePlanColorInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    OrderNo: TcxTextEdit;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    Label3: TLabel;
    Label25: TLabel;
    Label23: TLabel;
    GPM_1: TcxGridPopupMenu;
    ADOQueryLabel: TADOQuery;
    Label51: TLabel;
    Label7: TLabel;
    ADO_I1: TADOQuery;
    DS_I1: TDataSource;
    ToolButton3: TToolButton;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    BuyConNo: TcxTextEdit;
    Label5: TLabel;
    C_Name: TcxTextEdit;
    Label6: TLabel;
    Label8: TLabel;
    C_Spec: TcxTextEdit;
    C_Composition: TcxTextEdit;
    Label9: TLabel;
    C_GramWeight: TcxTextEdit;
    Label10: TLabel;
    C_Width: TcxTextEdit;
    Label11: TLabel;
    Label27: TLabel;
    IdFTP1: TIdFTP;
    Label14: TLabel;
    Label20: TLabel;
    ConNo: TcxTextEdit;
    ADO_I2: TADOQuery;
    DS_I2: TDataSource;
    CDS_LM: TClientDataSet;
    OpenDialog1: TOpenDialog;
    PM_1: TPopupMenu;
    N5: TMenuItem;
    N6: TMenuItem;
    CDS_Fact: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    DSFact: TDataSource;
    cxPageControl3: TcxPageControl;
    cxTabSheet2: TcxTabSheet;
    cxTabSheet3: TcxTabSheet;
    tlb2: TToolBar;
    btnConSel: TToolButton;
    ToolButton5: TToolButton;
    ToolButton7: TToolButton;
    ToolButton4: TToolButton;
    ToolButton1: TToolButton;
    ToolButton6: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    TV1Column8: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    VC_PRTPs: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel3: TPanel;
    OrdDate: TcxDateEdit;
    DlyDate: TcxDateEdit;
    CustName: TcxButtonEdit;
    OurCoName: TcxButtonEdit;
    C_Code: TcxButtonEdit;
    OrdUnit: TcxComboBox;
    OrdNote: TcxMemo;
    RM2: TRMGridReport;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    RMDB_2: TRMDBDataSet;
    ADO_1: TADOQuery;
    lbl8: TLabel;
    lbl9: TLabel;
    lbl15: TLabel;
    Label12: TLabel;
    lbl19: TLabel;
    lbl20: TLabel;
    lbl24: TLabel;
    lbl25: TLabel;
    lbl26: TLabel;
    lbl28: TLabel;
    lbl29: TLabel;
    lbl30: TLabel;
    PH: TcxTextEdit;
    LightNote: TcxButtonEdit;
    FtyName: TcxButtonEdit;
    Technics: TcxButtonEdit;
    Feel: TcxButtonEdit;
    OtherProcess4: TcxButtonEdit;
    OtherProcess5: TcxButtonEdit;
    OtherProcess6: TcxButtonEdit;
    OtherProcess8: TcxButtonEdit;
    OtherProcess9: TcxButtonEdit;
    OtherProcess10: TcxButtonEdit;
    Label16: TLabel;
    GC_Name: TcxTextEdit;
    Label17: TLabel;
    CraftCode: TcxTextEdit;
    Label18: TLabel;
    CraftName: TcxTextEdit;
    Label34: TLabel;
    ReferenceNumber: TcxButtonEdit;
    Label35: TLabel;
    LightType: TcxButtonEdit;
    OtherProcess1: TcxComboBox;
    cxTabSheet1: TcxTabSheet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column7: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton12: TToolButton;
    ToolButton14: TToolButton;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ADO_2: TADOQuery;
    Label37: TLabel;
    Label13: TLabel;
    GC_BatchNo: TcxButtonEdit;
    TV1Column1: TcxGridDBColumn;
    ToolButton8: TToolButton;
    TV1Column7: TcxGridDBColumn;
    Label15: TLabel;
    C_EComposition: TcxTextEdit;
    Saleser: TcxButtonEdit;
    SalesId: TcxTextEdit;
    Label19: TLabel;
    Tallyer: TcxButtonEdit;
    TallyId: TcxTextEdit;
    Label21: TLabel;
    CraftEName: TcxTextEdit;
    C_EName: TcxTextEdit;
    Label22: TLabel;
    pm2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    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 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 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_CodeDblClick(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 FtyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton14Click(Sender: TObject);
    procedure TV2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure GC_BatchNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton8Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure SaleserPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TallyerPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TallyerDblClick(Sender: TObject);
    procedure SaleserDblClick(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
  private
    FSCXDH: string;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function getOrderNo(fconNo: string): string;
    procedure InitImage2();
    procedure LookImage(FileName: string);
    procedure SetXH(CDS_PX: TClientDataSet);
    { Private declarations }
  public
    PState, CopyInt, PriceFlag, FQRS: Integer;
    FMainId, FFMainId, FOrderNo: string;
    FXS: Integer;
    canshu1: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmTradePlanColorInPut: TfrmTradePlanColorInPut;
  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_ZDYHelpSelXR, U_UserSel;


{$R *.dfm}

procedure TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.InitImage2();
begin
  with ADO_I2 do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where isnull(WBID,'''')<>'''' and FileName=' + quotedstr(Trim(CDS_1.fieldbyname('AttName').AsString)));
    open;
  end;
end;

function TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.TallyerDblClick(Sender: TObject);
begin
  Tallyer.Text := '';
  TallyId.Text := '';
end;

procedure TfrmTradePlanColorInPut.TallyerPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
      Fdept := '理单';
      if ShowModal = 1 then
      begin
        self.TallyId.Text := Trim(CDS_1.fieldbyname('UserID').AsString);
        self.Tallyer.Text := Trim(CDS_1.fieldbyname('UserName').AsString);
      end;
    end;
  finally
    frmUserSel.Free;
  end;
end;

procedure TfrmTradePlanColorInPut.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradePlanColorInPut.InitData();
var
  FDate: TDateTime;
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, Panel3, 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 where 1=1 ');
    sql.Add(' and MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 2);
  SCreateCDS(ADOTemp, CDS_1);
  SInitCDSData(ADOTemp, CDS_1);

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Trade_Plan_Fty  ');
    sql.Add(' where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHData(ADOTemp, Panel3, 4);
  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);
  if PState = 0 then
  begin
    OrdDate.Date := SGetServerDateTime(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('TPTID').Value := '';
        Post;
        Next;
      end;
    end;
  end;
end;

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

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

procedure TfrmTradePlanColorInPut.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradePlanColorInPut := nil;
end;

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

procedure TfrmTradePlanColorInPut.FormShow(Sender: TObject);
var
  i: Integer;
begin
  inherited;
  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) + 'Tv1', Tv1, '生产指示单管理');
  readCxGrid(TRIM(SELF.Caption) + 'Tv2', Tv2, '生产指示单管理');
  OrderNo.Text := '';
  fFlileFlag := UserDataFlag + 'HX';
  InitData();
  InitImage2();

  if FQRS = 1 then
  begin
    ScrollBox1.Enabled := False;
    ToolBar1.Visible := False;
    cxTabSheet3.Visible := False;
    cxTabSheet1.Visible := False;
    for i := 0 to TV1.ColumnCount - 1 do
    begin
      if uppercase(TV1.Columns[i].DataBinding.FieldName) <> 'SSEL' then
        TV1.Columns[i].Options.Editing := false;
    end;
    TV1Column1.Options.Editing := True;
    TV1Column6.Options.Editing := True;
  end;
end;

procedure TfrmTradePlanColorInPut.FtyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.FtyName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.FtyName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);

      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradePlanColorInPut.GC_BatchNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fRollNum: integer;
  MGC_BatchNo: string;
begin
  MGC_BatchNo := '';

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

  frmTradeGreyClothStkSel := TfrmTradeGreyClothStkSel.create(self);
  with frmTradeGreyClothStkSel do
  begin
    if showmodal = 1 then
    begin
      CDS_Main.DisableControls;
      CDS_Main.First;
      while not CDS_Main.eof do
      begin
        if CDS_Main.FieldByName('ssel').AsBoolean then
        begin
          MGC_BatchNo := MGC_BatchNo + trim(CDS_Main.FieldByName('BatchNo').AsString) + '+';

        end;
        CDS_Main.Next;
      end;
      CDS_Main.EnableControls;
    end;
    free;
  end;

  if length(MGC_BatchNo) > 0 then
  begin
    MGC_BatchNo := copy(MGC_BatchNo, 1, Length(MGC_BatchNo) - 1);

    GC_BatchNo.Text := MGC_BatchNo;
  end;

end;

procedure TfrmTradePlanColorInPut.SaleserDblClick(Sender: TObject);
begin
  Saleser.Text := '';
  SalesId.Text := '';
end;

procedure TfrmTradePlanColorInPut.SaleserPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
      Fdept := '业务';
      if ShowModal = 1 then
      begin

        self.SalesId.Text := Trim(CDS_1.fieldbyname('UserID').AsString);
        self.Saleser.Text := Trim(CDS_1.fieldbyname('UserName').AsString);
      end;
    end;
  finally
    frmUserSel.Free;
  end;
end;

function TfrmTradePlanColorInPut.SaveData(): Boolean;
var
  maxno, Smaxno, maxorderno, MaxFNo, MaxTFTNo: 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 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, 1);
      RTSetsavedata(ADOCmd, 'Trade_Plan_Main', Panel3, 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);
      if DlyDate.Text <> '' then
      begin
        FieldByName('DlyDate').Value := Trim(DlyDate.Text);
      end;
      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, 2);

          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 ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Trade_Plan_Fty where TPFId=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if ADOCmd.IsEmpty then
      begin
        Append;
      end
      else
        Edit;
      RTSetsavedata(ADOCmd, 'Trade_Plan_Fty', Panel3, 4);
      FieldByName('FtyNo').Value := Trim(FtyName.Properties.LookupItems.Text);
      FieldByName('MainId').Value := Trim(maxno);
      FieldByName('TPFId').Value := Trim(maxno);
      Post;
    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;

          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('EXEC  P_Authority_Controls   ');
      sql.Add(' @DataType =''贸易计划单'' ');
      sql.Add(',@DataCode=' + quotedstr(Trim(maxno)));
      execsql;
    end;
    ADOCmd.Connection.CommitTrans;
    FMainId := Trim(maxno);

    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select *  from Trade_Plan_Sub where 1=1 ');
      sql.Add(' and MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
//    SCSHData(ADOTemp, ScrollBox1, 2);
    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 TfrmTradePlanColorInPut.TBSaveClick(Sender: TObject);
var
  fDate, maxno: string;
begin
  if PState <> 1 then
  begin
    ToolBar1.SetFocus;
  end;

  if Trim(OrderNo.Text) = '' then
  begin
//    fDate := 'LD' + LeftStr(FormatDateTime('yyyy', SGetServerDate(ADOTemp)), 2);
    if GetLSNo(ADOCmd, maxno, 'LD', 'Trade_Plan_Main', 3, 1) = False then
      raise Exception.Create('取最大号失败!')
    else
      OrderNo.Text := trim(maxno);

//    with ADOTemp do
//    begin
//      Close;
//      sql.Clear;
//      SQL.Add('exec P_Trade_Plan_Get_OrdNo @ConNo=''' + Trim(ConNo.Text) + '''');
//      Open;
//    end;
//    OrderNo.Text := ADOTemp.FieldByName('OrderNo').AsString;
  end;

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

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

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

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

procedure TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
  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 := '';
    Post;
  end;

end;

procedure TfrmTradePlanColorInPut.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;
      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('update Trade_Plan_Sub set DelId=' + QuotedStr(Trim(DCode)) + ',Deler=' + QuotedStr(Trim(dname)) + ',Deltime=GETDATE()    ');
          sql.Add('where SubId=' + QuotedStr(Trim(CDS_1.fieldbyname('SubId').AsString)));

          sql.Add('insert into Trade_Plan_Sub_Del select * from Trade_Plan_Sub where SubId=' + QuotedStr(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 TfrmTradePlanColorInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(TRIM(SELF.Caption) + 'Tv1', Tv1, '生产指示单管理');
  WriteCxGrid(TRIM(SELF.Caption) + 'Tv2', Tv2, '生产指示单管理');
end;

procedure TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.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_EName.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_EName').AsString);
            C_Spec.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
            C_Composition.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
            C_EComposition.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_EComposition').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.Properties.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);
            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 TfrmTradePlanColorInPut.ToolButton6Click(Sender: TObject);
var
  i: Integer;
begin
  i := CDS_1.RecordCount;
  with CDS_1 do
  begin
    Append;
    FieldByName('SSel').Value := false;
    FieldByName('SerialNo').Value := i + 1;
    FieldByName('AttName').Value := '';
    Post;
  end;
end;

procedure TfrmTradePlanColorInPut.ToolButton7Click(Sender: TObject);
var
  I, J, K: Integer;
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
              Append;
              FieldByName('SSel').Value := false;
              FieldByName('SerialNo').Value := K;
              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 TfrmTradePlanColorInPut.ToolButton8Click(Sender: TObject);
begin
  with CDS_2 do
  begin

    append;
    FieldByName('SSel').Value := false;
    FieldByName('SerialNo').Value := 1;
    FieldByName('TPTName').Value := '水洗色牢度';
    FieldByName('TPTType').Value := 'GB/T3921-2008-A';
    FieldByName('TPTUnit').Value := '级';
    Post;

    append;
    FieldByName('SSel').Value := false;
    FieldByName('SerialNo').Value := 2;
    FieldByName('TPTName').Value := '耐日晒(光照)色牢度';
    FieldByName('TPTType').Value := 'GB/T8427-2008方法3';
    FieldByName('TPTUnit').Value := '级';
    Post;

    append;
    FieldByName('SSel').Value := false;
    FieldByName('SerialNo').Value := 3;
    FieldByName('TPTName').Value := '耐水渍色牢度';
    FieldByName('TPTType').Value := 'GB/T5713-2013';
    FieldByName('TPTUnit').Value := '级';
    Post;

    append;
    FieldByName('SSel').Value := false;
    FieldByName('SerialNo').Value := 4;
    FieldByName('TPTName').Value := '耐汗渍色牢度(碱)';
    FieldByName('TPTType').Value := 'GB/T3922-2013';
    FieldByName('TPTUnit').Value := '级';
    Post;

    append;
    FieldByName('SSel').Value := false;
    FieldByName('SerialNo').Value := 5;
    FieldByName('TPTName').Value := '耐汗渍色牢度(酸)';
    FieldByName('TPTType').Value := 'GB/T3922-2013';
    FieldByName('TPTUnit').Value := '级';
    Post;

    append;
    FieldByName('SSel').Value := false;
    FieldByName('SerialNo').Value := 6;
    FieldByName('TPTName').Value := '耐摩擦色牢度(干)';
    FieldByName('TPTType').Value := 'GB/T3920-2008';
    FieldByName('TPTUnit').Value := '级';
    Post;

    append;
    FieldByName('SSel').Value := false;
    FieldByName('SerialNo').Value := 7;
    FieldByName('TPTName').Value := '耐摩擦色牢度(湿)';
    FieldByName('TPTType').Value := 'GB/T3920-2008';
    FieldByName('TPTUnit').Value := '级';
    Post;

  end;

end;

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

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

procedure TfrmTradePlanColorInPut.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);
        self.Saleser.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Saleser').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

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

procedure TfrmTradePlanColorInPut.C_CodeDblClick(Sender: TObject);
begin
  Self.C_Code.Text := '';
  Self.C_Name.Text := '';
  Self.C_EName.Text := '';
  Self.C_Spec.Text := '';
  Self.C_Composition.Text := '';
  Self.C_Width.Text := '';
  Self.C_GramWeight.Text := '';
  Self.GC_Name.Text := '';
  Self.CraftCode.Text := '';
  Self.CraftName.Text := '';
  Self.CraftEName.Text := '';
end;

procedure TfrmTradePlanColorInPut.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_EName.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_EName').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);
        Self.CraftName.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('CraftName').AsString);
        Self.CraftEName.Text := trim(frmClothInfoSel.CDS_1.Fieldbyname('CraftEName').AsString);

      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;
end;

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

procedure TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(CDS_2, True);
end;

procedure TfrmTradePlanColorInPut.MenuItem2Click(Sender: TObject);
begin
  SelOKNo(CDS_2, False);
end;

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

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

procedure TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.btnConSelClick(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
begin
  try
    frmTradeSalesContractSel := TfrmTradeSalesContractSel.Create(Application);
    with frmTradeSalesContractSel 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;

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

          self.OurCoName.Properties.LookupItems.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('Sellno').AsString);
          self.OurCoName.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('Sellname').AsString);
          self.OrderNo.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConNo').AsString);
          self.ConNo.Text := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConNo').AsString);
          self.C_Code.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Code').AsString);
          self.C_Name.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Name').AsString);
          self.C_EName.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_EName').AsString);
          self.C_Spec.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Spec').AsString);
          self.C_Composition.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Composition').AsString);
          self.C_EComposition.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_EComposition').AsString);

          self.C_Width.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_Width').AsString);
          self.C_GramWeight.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
          self.GC_Name.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('GC_Name').AsString);
          self.CraftCode.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('CraftCode').AsString);
          self.CraftName.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('CraftName').AsString);
          self.CraftEName.Text := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('CraftEName').AsString);

          self.OrdUnit.ItemIndex := OrdUnit.Properties.Items.IndexOf(trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConUnit').AsString));
          with self.CDS_1 do
          begin
            Append;
            FieldByName('SSel').Value := false;
            Fieldbyname('SerialNo').value := trim(frmTradeSalesContractSel.CDS_1.Fieldbyname('SerialNo').AsString);
            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);
            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;
            Post;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

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

procedure TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.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 TfrmTradePlanColorInPut.TV1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitImage2();
end;

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

