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,
   dxDateRanges, dxBarBuiltInMenu, cxPC,
  System.ImageList, Vcl.ImgList, U_BaseInput, cxImage, cxDBEdit,
  IdBaseComponent, Winapi.UrlMon,
  IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
  IdFTP, ShellAPI,  dxScrollbarAnnotations, cxGeometry,
  dxFramedControl, dxLayoutContainer, dxLayoutControl, dxPanel, cxGroupBox,
  dxLayoutcxEditAdapters, dxCore, cxDateUtils, cxImageList;

type
  TfrmTradePlanInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ADOQueryLabel: TADOQuery;
    Label51: TLabel;
    CDS_Fact: TClientDataSet;
    DSFact: TDataSource;
    PM_Fact: TPopupMenu;
    N1: TMenuItem;
    cxgrdpmn2: TcxGridPopupMenu;
    Qry_ImgHX: TADOQuery;
    DS_ImgHX: TDataSource;
    N3: TMenuItem;
    Tbcgs: TToolButton;
    IdFTP1: TIdFTP;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton5: TToolButton;
    dxPanel1: TdxPanel;
    dxLayoutControl_bzplan: TdxLayoutControl;
    dxLayoutControl_bzplanGroup_Root: TdxLayoutGroup;
    dxLayoutGroup_1: TdxLayoutGroup;
    dxLayoutItem1: TdxLayoutItem;
    dxLayoutItem3: TdxLayoutItem;
    dxLayoutItem_htno: TdxLayoutItem;
    dxLayoutItem5: TdxLayoutItem;
    OrderNo: TcxTextEdit;
    CustName: TcxButtonEdit;
    ConNo: TcxTextEdit;
    OrdDate: TcxDateEdit;
    dxLayoutGroup_2: TdxLayoutGroup;
    dxLayoutItem2: TdxLayoutItem;
    dxLayoutItem6: TdxLayoutItem;
    dxLayoutItem7: TdxLayoutItem;
    DLYDate: TcxDateEdit;
    saleser: TcxButtonEdit;
    factoryName: TcxButtonEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1SerialNo: TcxGridDBColumn;
    v1BuyConNo: TcxGridDBColumn;
    v1C_Code: TcxGridDBColumn;
    v1C_StyleNo: TcxGridDBColumn;
    v1c_Name: TcxGridDBColumn;
    v1C_EName: TcxGridDBColumn;
    v1C_ColorNo: TcxGridDBColumn;
    v1C_Color: TcxGridDBColumn;
    v1C_EColor: TcxGridDBColumn;
    v1OrdQty: TcxGridDBColumn;
    v1OrdUnit: TcxGridDBColumn;
    v1OrdSNote: TcxGridDBColumn;
    v1C_Pattern: TcxGridDBColumn;
    v1C_Width: TcxGridDBColumn;
    v1C_GramWeight: TcxGridDBColumn;
    v1C_Spec: TcxGridDBColumn;
    v1AttName: TcxGridDBColumn;
    Tv1batchNo: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    dxLayoutGroup_3: TdxLayoutGroup;
    dxLayoutItem8: TdxLayoutItem;
    dxLayoutItem9: TdxLayoutItem;
    dxLayoutItem10: TdxLayoutItem;
    dxLayoutItem11: TdxLayoutItem;
    Gilding: TcxButtonEdit;
    PackSeal: TcxButtonEdit;
    ShippingSample: TcxButtonEdit;
    SizeNote: TcxButtonEdit;
    dxLayoutGroup_4: TdxLayoutGroup;
    dxLayoutItem12: TdxLayoutItem;
    dxLayoutItem13: TdxLayoutItem;
    OrdNote: TcxMemo;
    PackNote: TcxMemo;
    dxLayoutItem4: TdxLayoutItem;
    dxLayoutItem14: TdxLayoutItem;
    dxLayoutItem15: TdxLayoutItem;
    PackType: TcxButtonEdit;
    QtyNote: TcxButtonEdit;
    OtherProcess1: TcxButtonEdit;
    PackSealNote: TcxButtonEdit;
    dxLayoutItem16: TdxLayoutItem;
    Tsj: TToolButton;
    cxMemo1: TcxMemo;
    cxMemo2: TcxMemo;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    LabVolume: TcxButtonEdit;
    LabPackage: TcxButtonEdit;
    dxLayoutControl_otherGroup_Root: TdxLayoutGroup;
    dxLayoutControl_other: TdxLayoutControl;
    dxLayoutGroup1: TdxLayoutGroup;
    dxLayoutItem17: TdxLayoutItem;
    dxLayoutItem18: TdxLayoutItem;
    dxLayoutItem19: TdxLayoutItem;
    dxLayoutGroup_label: TdxLayoutGroup;
    dxLayoutItem20: TdxLayoutItem;
    dxLayoutItem21: TdxLayoutItem;
    RMPreview1: TRMPreview;
    RMPreview2: TRMPreview;
    RMGridReport1: TRMGridReport;
    OpenDialog1: TOpenDialog;
    ADOQuery_label: TADOQuery;
    RMGridReport2: TRMGridReport;
    dxLayoutItem22: TdxLayoutItem;
    dxLayoutItem23: TdxLayoutItem;
    dxLayoutItem24: TdxLayoutItem;
    dxLayoutItem25: TdxLayoutItem;
    dxLayoutItem26: TdxLayoutItem;
    CustDefStr1: TcxButtonEdit;
    CustDefStr2: TcxButtonEdit;
    CustDefStr3: TcxButtonEdit;
    CustDefFloat1: TcxTextEdit;
    CustDefFloat2: TcxTextEdit;
    Tv1SSel: TcxGridDBColumn;
    Tv1FtyPCId: TcxGridDBColumn;
    Tv1SLabVolume: TcxGridDBColumn;
    Tv1SLabPackage: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tupimage: TToolButton;
    ToolButton4: TToolButton;
    PM_1: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure ScrollBox1Click(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure HYTTBtnUpClick(Sender: TObject);
    procedure KYTTBtnUpClick(Sender: TObject);
    procedure N1Click(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 N3Click(Sender: TObject);
    procedure TV2Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TV2Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TbcgsClick(Sender: TObject);
    procedure OurCoNameBtnUpClick(Sender: TObject);
    procedure OurCoNameBtnDnClick(Sender: TObject);
    procedure V2Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure C_CodeBtnDnClick(Sender: TObject);
    procedure CustomerNoNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FormCreate(Sender: TObject);
    procedure v1PRTCodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1C_ColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure GildingPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FtyNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1OrdUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TsjClick(Sender: TObject);
    procedure LabVolumePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure LabVolumeEnter(Sender: TObject);
    procedure LabPackageEnter(Sender: TObject);
    procedure Tv1SLabVolumePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1SLabPackagePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
    procedure TupimageClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ToolButton4Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure v1AttNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ButtonEdit(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton8Click(Sender: TObject);
  private
    fIsLabelSave: Boolean;
    FSCXDH: string;
    fOrdeNOIsAuto:string; //订单编号是否自动
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function getOrderNo(fconNo: string): string;
    procedure LookImage(FileName: string);
    procedure InitDataSetDictionary();
    function saveCopyRuleInfo(mPlanId: string): Boolean;
    function getLabel(labelType:string=''):Boolean;
    procedure initGrid();
    function getOrderMakeType(): Boolean;
    procedure threadLoadform();
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo: string;
    fCopyMainId:string;
    FXS: Integer;
    canshu1: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmTradePlanInPut: TfrmTradePlanInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_TradeInspSet, U_ZDYHelp, U_RTFun, U_CompanySel,
  U_FormLayOutDesign,U_cxGridCustomSet, U_PictureUpload,
  U_ClothInfoSel, U_globalVar, U_UptPlanDataToRoll;
// getpic     ,

{$R *.dfm}

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.GildingPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);

      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text :=
          trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTradePlanInPut.TBCloseClick(Sender: TObject);
begin

  close;

end;

procedure TfrmTradePlanInPut.InitData();
var
  FDate: TDateTime;
begin
  FDate := SGetServerDateTime(ADOTemp);
  OrdDate.Date := FDate;
  DLYDate.Date := FDate;

  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,B.LabVolume,B.LabPackage from Trade_Plan_main A');
    sql.Add('left join Trade_Cloth_Inspect_Rule B on B.RuleID=A.mainId');
    sql.Add('where A.MainId=''' + trim(FMainId) + '''');
    open;
  end;

  LSCSHData(ADOTemp, dxLayoutControl_bzplan, 2);

  // OurCoName.TxtCode := Trim(ADOTemp.FieldByName('OurCoNo').AsString);
  CustName.Properties.LookupItems.Text :=
    trim(ADOTemp.fieldbyname('CustNo').AsString);
  LabVolume.Text := trim(ADOTemp.fieldbyname('LabVolume').AsString);
  LabPackage.Text := trim(ADOTemp.fieldbyname('LabPackage').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);
  LSCSHData(ADOTemp, dxLayoutControl_bzplan, 7);

 // Tv1.ApplyBestFit();

  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select * from Trade_Plan_Fty  ');
    sql.Add(' where MainId=''' + trim(FMainId) + '''');
    // ShowMessage(SQL.Text);
    open;
  end;
  SCreateCDS(ADOTemp, CDS_Fact);
  SInitCDSData(ADOTemp, CDS_Fact);

  if PState = 0 then
  begin
    OrdDate.Date := SGetServerDateTime(ADOTemp);
    DLYDate.Date := OrdDate.Date;
    if fOrdeNOIsAuto='1' then
    begin
      orderNo.Text:='自动';
      orderNo.Enabled:=false;
     end
     else
     begin
       OrderNo.Text := '';
       OrderNo.Enabled := True;
     end;
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    if fOrdeNOIsAuto='1' then
    begin
      orderNo.Text:='自动';
      orderNo.Enabled:=false;
     end
     else
     begin
       OrderNo.Text := '';
       OrderNo.Enabled := True;
     end;

    OrdDate.Date := FDate;

    try
      CDS_1.DisableControls ;
      with CDS_1 do
      begin
        First;
        while not Eof do
        begin
          Edit;
          fieldbyname('MainId').Value := '';
          fieldbyname('SubId').Value := '';
          Post;
          Next;
        end;
      end;
    finally
      CDS_1.EnableControls;
    end;
  end;
end;

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

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

procedure TfrmTradePlanInPut.FormCreate(Sender: TObject);
begin
   inherited;
   cxGrid1.Align := alClient;
   getOrderMakeType();
   if fOrdeNOIsAuto='1' then
    orderNo.Enabled:=false;
end;

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

procedure TfrmTradePlanInPut.FormShow(Sender: TObject);
var
  thread: TThread;
begin
  inherited;
  cxPageControl1.Align := alClient;
  cxPageControl1.ActivePageIndex:=0;
  if PState = 5 then
  begin
    Tbcgs.Visible := false;
    Tsj.Visible := false;
    Tv1.OptionsData.Editing := false;
    dxLayoutControl_bzplan.Enabled := false;
  end;
  OrderNo.Text := '';
  fFlileFlag := UserDataFlag + 'HX';

  thread:=TThread.CreateAnonymousThread(
    procedure
    begin
      TThread.Synchronize(nil,
        procedure
        begin

           readCxGrid(trim(SELF.Caption) + '-录入窗口', Tv1, gDllFileCaption);
           getLabel();
        end)
    end);

     thread.FreeOnTerminate := True;
     thread.Start;
    application.ProcessMessages;
  InitData();



end;

procedure TfrmTradePlanInPut.FtyNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
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);
      MainType := TEdit(Sender).Name;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := trim(ClientDataSet1.fieldbyname('ZDYName')
          .AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

function TfrmTradePlanInPut.SaveData(): Boolean;
var
  maxno, Smaxno, maxorderno, MaxFNo: string;
begin
  try
    result := false;
    ADOCmd.Connection.BeginTrans;
    /// 保存主表
    if trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'JM', 'Trade_Plan_Main', 3, 1) = false then
        raise Exception.Create('取最大号失败!');
      if (trim(OrderNo.Text) = '') or (trim(OrderNo.Text) = '自动') then
        OrderNo.Text := maxno;
    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 IsEmpty then
      begin
        Append;
        fieldbyname('FillId').Value := trim(DCode);
        fieldbyname('Filler').Value := trim(DName);
        fieldbyname('status').Value := '0';
      end
      else
      begin
        Edit;
        fieldbyname('EditId').Value := trim(DCode);
        fieldbyname('Editer').Value := trim(DName);
        fieldbyname('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      LRTSetsavedata(ADOCmd, 'Trade_Plan_Main', dxLayoutControl_bzplan, 2);
      fieldbyname('MainId').Value := trim(maxno);
      fieldbyname('CustNo').Value := trim(CustName.Properties.LookupItems.Text);
      Post;
    end;

    /// //////////////////               保存子表            ///////////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if trim(CDS_1.fieldbyname('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 IsEmpty then
          begin
            Append;
            fieldbyname('SubId').ASSTRING := trim(Smaxno);
          end
          else
            Edit;
//          ShowMessage(ADOCmd.fieldbyname('SubId').AsString);
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Trade_Plan_Sub', 0);
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Trade_Plan_Sub', 9);
//          ShowMessage(ADOCmd.fieldbyname('SubId').AsString);
          fieldbyname('SubId').ASSTRING := trim(Smaxno);
          LRTSetsavedata(ADOCmd, 'Trade_Plan_Sub', dxLayoutControl_bzplan, 7);
//          ShowMessage(ADOCmd.fieldbyname('SubId').AsString);
          fieldbyname('MainId').Value := trim(maxno);
          fieldbyname('ConSId').Value :=
            trim(CDS_1.fieldbyname('ConSId').AsString);
          fieldbyname('ConMId').Value :=
            trim(CDS_1.fieldbyname('ConMId').AsString);
//          ShowMessage(ADOCmd.fieldbyname('MainId').AsString);
//          ShowMessage(ADOCmd.fieldbyname('SubId').AsString);
//          ShowMessage(ADOCmd.fieldbyname('OrdQty').AsString);
          Post;
//          with CDS_1 do
//          begin
//            Edit;
//            fieldbyname('SubId').Value := trim(Smaxno);
//            Post;
//          end;

        end;
        Next;
      end;
    end;
    /// //////////////////               保存子表           ///////////////////////////
    /// //////////////////               保存加工厂            ///////////////////////////
    with CDS_Fact do
    begin
      First;
      while not Eof do
      begin

        with ADOCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from Trade_Plan_Fty where MainId=''' +
            trim(maxno) + '''');
          open;
        end;
        with ADOCmd do
        begin
          if IsEmpty then
          begin
            if GetLSNo(ADOQueryBaseCmd, MaxFNo, 'JF', 'Trade_Plan_Fty', 4, 1) = false
            then
              raise Exception.Create('取子流水号失败!');
            Append;
            fieldbyname('MainId').Value := trim(maxno);
            fieldbyname('TPFId').Value := trim(MaxFNo);
          end
          else
            Edit;

          fieldbyname('ftyName').Value := trim(factoryName.Text);
          fieldbyname('ftyNo').Value :=
            trim(factoryName.Properties.LookupItems.Text);
          // RTSetSaveDataCDS(ADOCmd, Tv2, CDS_Fact, 'Trade_Plan_Fty', 0);
          Post;
          with CDS_Fact do
          begin
            Edit;
            fieldbyname('TPFId').Value := trim(MaxFNo);
            Post;
          end;

        end;
        Next;
      end;
    end;
    /// //////////////////               保存加工厂           ///////////////////////////

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

    // 自动审核
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update Trade_Plan_Main SET status=''9'',chker=''' + trim(DName) +
        ''',Chktime=getdate() ');
      sql.Add('where mainId='+quotedstr(maxno));
      sql.Add('and status<''9''');
      execsql;
    end;

     if not saveCopyRuleInfo(maxno) then
     begin
      // raise Exception.Create('复制订单打卷规则失败!');
     end;

    FMainId := trim(maxno);
    ADOCmd.Connection.CommitTrans;
    result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTradePlanInPut.TBSaveClick(Sender: TObject);
var
  MAXNO1: string;
begin
  // if PState <> 1 then
  // begin
  OrdDate.SetFocus;
  // end;
  if not mustInCheck(application, dxLayoutControl_bzplan,PWideChar('*')) then
    exit;
 if (pos('*', dxLayoutItem1.Caption)>0) and (trim(OrderNo.Text) = '') then
 begin
    Application.MessageBox('订单号不能为空！', '提示', 0);
      exit;
 end;
//  if trim(OrderNo.Text) = '' then
//  begin
//    Application.MessageBox('订单号不能为空！', '提示', 0);
//    exit;
//  end;
  if CDS_1.IsEmpty then
  begin
    Application.MessageBox('产品明细不能为空!', '提示', 0);
    exit;
  end;

  if not checkColumnMustIn(application,tv1,9) then exit;


  if SaveData() then
  begin
    initGrid();
    if not fIsLabelSave then
      Application.MessageBox('保存成功！', '提示', 0);
    if PState=0 then PState:=1;
    
     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('SerialNo').Value := CDS_1.RecordCount;
    //FieldByName('c_Name').Value := '';
    if trim(CDS_1.FieldByName('batchno').AsString)='' then
    begin
      FieldByName('C_Color').Value := '';
    end
    else
    begin
      FieldByName('batchno').Value := '';
      FieldByName('FtyPCId').Value := '';
    end;

    // 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;
  try
  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('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;
  finally
    CDS_1.EnableControls;
  end;


end;

procedure TfrmTradePlanInPut.ToolButton3Click(Sender: TObject);
begin
  inherited;
  if CDS_1.IsEmpty then
    Exit;
  OneKeyPost(Tv1, CDS_1);
end;

procedure TfrmTradePlanInPut.ToolButton4Click(Sender: TObject);
var
  strResult:string;
  k:integer;
  col:integer;
  fieldName:string;
begin
  if CDS_1.IsEmpty then
  begin
    exit;
  end;
  col :=tv1.controller.focusedcolumnIndex;
  if col<=0 then
  begin
     application.MessageBox('请选择要操作的列(点击列单元格)！','警告信息',0);
     exit;
  end;

  fieldName:=tv1.Columns[col+1].DataBinding.FieldName;
  if fieldName='' then exit;

  if InputQuery('','前缀字符:',strResult) then
  begin
    CDS_1.First;
    k:=1;
    while not CDS_1.eof do
    begin
      CDS_1.edit;
      CDS_1.FieldByName(fieldName).value :=strResult+intTostr(k);
      CDS_1.Post;
      inc(k);
      CDS_1.Next;
    end;
  end;
end;

procedure TfrmTradePlanInPut.TbcgsClick(Sender: TObject);
begin
  WriteCxGrid(trim(SELF.Caption) + '-录入窗口', Tv1, gDllFileCaption);

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.ToolButton6Click(Sender: TObject);
var
  FValue, FFValue, FColumn, FFColumn: string;
begin
  inherited;
  if CDS_1.IsEmpty then
    Exit;
  // FColumn:=tv1.Columns[Tv1.Controller.FocusedColumnIndex].DataBinding.FieldName;
  // FFColumn:=Tv1.Columns[Tv1.Controller.FocusedColumnIndex].Summary.GroupFormat;
  FColumn := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FFColumn := Tv1.Controller.FocusedColumn.Summary.GroupFormat;
  FValue := Trim(CDS_1.fieldByName(FColumn).AsString);
  if Trim(FFColumn) <> '' then
  begin
    FFValue := Trim(CDS_1.fieldByName(FFColumn).AsString);
  end;
  with CDS_1 do
  begin
    DisableControls;
    while not Eof do
    begin
      edit;
      if FValue = '' then
      begin
        CDS_1.fieldByName(FColumn).Value := null;
      end
      else
      begin
        CDS_1.fieldByName(FColumn).Value := FValue;
      end;
      if Trim(FFColumn) <> '' then
      begin
        if FFValue = '' then
        begin
          CDS_1.fieldByName(FFColumn).Value := null;
        end
        else
        begin
          CDS_1.fieldByName(FFColumn).Value := FFValue;
        end;
      end;

      post;

      Next;
    end;
    EnableControls;
  end;
end;

procedure TfrmTradePlanInPut.ToolButton7Click(Sender: TObject);
var
  strResult:string;
  k:integer;
  col:integer;
  fieldName:string;
begin
  if CDS_1.IsEmpty then
  begin
    exit;
  end;
  col :=tv1.controller.focusedcolumnIndex;
  if col<=0 then
  begin
     application.MessageBox('请选择要操作的列(点击列单元格)！','警告信息',0);
     exit;
  end;

  fieldName:=tv1.Columns[col+1].DataBinding.FieldName;
  if fieldName='' then exit;

  if InputQuery('','前缀字符:',strResult) then
  begin
    k:=1;
    while not CDS_1.eof do
    begin
      CDS_1.edit;
      CDS_1.FieldByName(fieldName).value :=strResult+intTostr(k);
      CDS_1.Post;
      inc(k);
      CDS_1.Next;
    end;
  end;
end;

procedure TfrmTradePlanInPut.ToolButton8Click(Sender: TObject);
begin
  if (Trim(FMainId)='') then
  begin
    Application.MessageBox('保存后才能上传图片!','提示信息',0);
    Exit;
  end;
  try
    frmPictureUpload := TfrmPictureUpload.Create(Application);
    with frmPictureUpload do begin
      fFlileFlag := self.fFlileFlag;
      FWidth := 250;
      FHeight := 300;
      FTFType := 'HX';
      FDataId := Trim(FMainId);
      if ShowModal = 1 then begin
        InitData();
      end;
    end;
  finally
    frmPictureUpload.Free;
  end;

end;

procedure TfrmTradePlanInPut.TsjClick(Sender: TObject);
begin
  saveLayOut(application,dxLayoutControl_bzplan, ADOQueryBaseCmd,
   PWideChar( fDllFileName + '|' + SELF.Name + '|' + dxLayoutControl_bzplan.Name+ '.ini'));

end;

procedure TfrmTradePlanInPut.TupimageClick(Sender: TObject);
begin
  if CDS_1.IsEmpty  then
    Exit;
   if  (Trim(Self.CDS_1.fieldbyname('subId').AsString)='') then
   begin
      Application.MessageBox('保存后才能上传图片!','提示信息',0);
      Exit;
   end;
  try
    frmPictureUpload := TfrmPictureUpload.Create(Application);
    with frmPictureUpload do begin
      fFlileFlag := self.fFlileFlag;
      FWidth := 250;
      FHeight := 300;
      FTFType := 'HX';
      FDataId := Trim(Self.CDS_1.fieldbyname('subId').AsString);
      if ShowModal = 1 then begin
         InitData();
      end;
    end;
  finally
    frmPictureUpload.Free;
  end;

end;

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

end;

procedure TfrmTradePlanInPut.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.LabPackageEnter(Sender: TObject);
begin
  inherited;
  cxPageControl1.ActivePageIndex := 1;
end;

procedure TfrmTradePlanInPut.LabVolumeEnter(Sender: TObject);
begin
  inherited;
  cxPageControl1.ActivePageIndex := 0;
end;

procedure TfrmTradePlanInPut.LabVolumePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    fIsLabelSave := True;
    if trim(FMainId) = '' then
      TBSave.Click;
    if trim(FMainId) = '' then
      exit;
    frmTradeInspSet := TfrmTradeInspSet.Create(SELF);
    with frmTradeInspSet do
    begin
      FDataID := trim(SELF.FMainId);
      Tsave.Tag :=2;
      if ShowModal = 1 then
      begin
        // InitDataSetDictionary();
        SELF.LabVolume.Properties.LookupItems.Text := LabVolume.Text;
        SELF.LabVolume.Text := LabVolume.Text;
        SELF.LabPackage.Properties.LookupItems.Text := LabPackage.Text;
        SELF.LabPackage.Text := LabPackage.Text;
        getLabel();
        TcxButtonEdit( Sender ).SetFocus     ;

      end;
      Free;
    end;
  finally
    fIsLabelSave := false;
  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.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(CDS_1,True);
end;

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

procedure TfrmTradePlanInPut.v1OrderUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  mvalues: string;
begin
  mvalues := Tv1.Columns[Tv1.Controller.FocusedColumn.index]
    .DataBinding.FieldName;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrderUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        SELF.CDS_1.Edit;
        SELF.CDS_1.fieldbyname(mvalues).Value :=
          trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradePlanInPut.v1OrderUnitPropertiesEditValueChanged
  (Sender: TObject);
var
  mvalue, FFieldName, YValue: string;
  FDDQty, FCPCDXS, FQty, FDKCQty: Double;
begin
  mvalue := TcxComboBox(Sender).EditingText; // 变化后单位
  FFieldName := trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin

    Edit;
    fieldbyname(FFieldName).Value := mvalue;
    Post;
    YValue := fieldbyname('DDQtyUnit').AsString; // 原单位=订单单位
    FDDQty := fieldbyname('DDQty').AsFloat; // 订单数量
    FCPCDXS := fieldbyname('CPCDXS').AsFloat;
    FDKCQty := fieldbyname('DKCQty').AsFloat;
    FQty := fieldbyname('PRTOrderQty').AsFloat;
  end;
  if mvalue = 'Kg' then
  begin
    if YValue = 'Y' then
    begin
      FQty := FDDQty * 0.9144 / FCPCDXS;
    end;
    if YValue = 'M' then
    begin
      FQty := FDDQty / FCPCDXS;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty;
    end;
  end;

  if mvalue = 'M' then
  begin
    if YValue = 'Y' then
    begin
      FQty := FDDQty * 0.9144;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty * FCPCDXS;
    end;
    if YValue = 'M' then
    begin
      FQty := FDDQty;
    end;
  end;
  if mvalue = 'Y' then
  begin
    if YValue = 'M' then
    begin
      FQty := FDDQty / 0.9144;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty * FCPCDXS / 0.9144;
    end;
    if YValue = 'Y' then
    begin
      FQty := FDDQty;
    end;
  end;

  with CDS_1 do
  begin
    Edit;
    fieldbyname('PRTOrderQty').Value := FQty - FDKCQty;
    Post;
  end;

  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradePlanInPut.v1OrdUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  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('OrdUnit').Value :=
          trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

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.v1PRTCodeNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  inherited;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        SELF.CDS_1.Edit;
        SELF.CDS_1.fieldbyname('c_code').Value :=
          trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        SELF.CDS_1.fieldbyname('c_name').Value :=
          trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
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.N1Click(Sender: TObject);
begin
  if CDS_Fact.IsEmpty then
    exit;

  if trim(CDS_Fact.fieldbyname('TPFId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      exit;
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete Trade_Plan_Fty where TPFId=''' +
        trim(CDS_Fact.fieldbyname('TPFId').AsString) + '''');
      execsql;
    end;
  end;
  CDS_Fact.Delete;
end;

procedure TfrmTradePlanInPut.N3Click(Sender: TObject);
var
  i: Integer;
begin
  i := CDS_Fact.RecordCount + 1;
  with CDS_Fact do
  begin
    Append;
    fieldbyname('SerialNo').Value := i;
    Post;
  end;

end;

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

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

  // InitImageHX();
end;

procedure TfrmTradePlanInPut.Tv1SLabPackagePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  try
   // fIsLabelSave := true;
    frmTradeInspSet := TfrmTradeInspSet.Create(SELF);
    with frmTradeInspSet do
    begin
      FDataID := trim(SELF.FMainId);
      fSubUseFlag:=true;
      if ShowModal = 1 then
      begin
        SELF.CDS_1.Edit;
        SELF.CDS_1.fieldbyname('SLabPackage').Value :=
          trim(CDS_Label.fieldbyname('labelCaption').AsString);
       // getLabel();

      end;
      Free;
    end;
  finally
   // fIsLabelSave := false;
  end;
end;

procedure TfrmTradePlanInPut.Tv1SLabVolumePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  inherited;
  try

    frmTradeInspSet := TfrmTradeInspSet.Create(SELF);
    with frmTradeInspSet do
    begin
      FDataID := trim(SELF.FMainId);
      fSubUseFlag:=true;
      if ShowModal = 1 then
      begin
        SELF.CDS_1.Edit;
        SELF.CDS_1.fieldbyname('SLabVolume').Value :=
          trim(CDS_Label.fieldbyname('labelCaption').AsString);
       // getLabel();

      end;
      Free;
    end;
  finally
    //fIsLabelSave := false;
  end;
end;

procedure TfrmTradePlanInPut.TV2Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      fnote := True;
      flag := 'Technics';
      flagname := '工艺';
      if ShowModal = 1 then
      begin
        CDS_Fact.Edit;
        CDS_Fact.fieldbyname('Technics').Value :=
          trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTradePlanInPut.TV2Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      fnote := True;
      flag := 'GDY';
      flagname := '跟单员';
      if ShowModal = 1 then
      begin
        CDS_Fact.Edit;
        CDS_Fact.fieldbyname('SJGDY').Value :=
          trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

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

procedure TfrmTradePlanInPut.V2Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        CDS_Fact.Edit;

        CDS_Fact.fieldbyname('FtyNo').Value :=
          trim(CDS_1.fieldbyname('CoCode').AsString);
        CDS_Fact.fieldbyname('FtyName').Value :=
          trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmTradePlanInPut.v1AttNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  inherited;
  Tupimage.Click;
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.v1C_ColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdColor';
      flagname := '颜色';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
      fnote := True;
      if ShowModal = 1 then
      begin
        SELF.CDS_1.Edit;
        SELF.CDS_1.fieldbyname('c_color').Value :=
          trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        SELF.CDS_1.fieldbyname('c_ecolor').Value :=
          trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradePlanInPut.ButtonEdit(Sender: TObject;
  AButtonIndex: Integer);
begin
  inherited;
  Tupimage.Click;
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;

procedure TfrmTradePlanInPut.InitDataSetDictionary();
begin
  with ADOQuery_label do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Print_RollLabel');
    sql.Add('@MJID='''' ');
    sql.Add(',@Flag=''1'' ');
    open;
  end;
end;

/// //////////////////////////////////
///
function TfrmTradePlanInPut.saveCopyRuleInfo(mPlanId: string): Boolean;
var
  MaxId, MaxSubId, FCoCode, FCCID, MaxCFID: string;
  i:integer;
begin
  result := false;
  try
    if trim(mPlanId) = '' then
    begin
      exit;
    end
    else
    begin
      MaxId := trim(mPlanId);
    end;
    ///////////////////////////////////////////
    with ADOQueryBaseTemp do
    begin
      close;
      sql.clear;
      sql.Add('select * from Trade_Cloth_Inspect_Rule where RuleID=''' +trim(fCopyMainId) + '''');
      open;
      if IsEmpty then exit;
    end;
    with ADOQueryBaseCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from Trade_Cloth_Inspect_Rule where RuleID=''' +
        trim(mPlanId) + '''');
      open;
    end;
    with ADOQueryBaseCmd do
    begin
      if IsEmpty then
      begin
        Append;
      end
      else
      begin
        Edit;
        fieldbyname('Editer').Value := trim(DName);
        fieldbyname('Edittime').Value := now;
      end;
      //////////////
      for i := 0 to ADOQueryBaseTemp.Fields.Count -1 do
      begin
        if (lowercase(ADOQueryBaseTemp.Fields[i].FieldName)<>'ruleid') then
        begin
          fields[i].Value:= ADOQueryBaseTemp.Fields[i].Value;
        end;
      end;
      fieldbyname('RuleID').Value := trim(mPlanId);
      fieldbyname('Filler').Value := trim(DName);
      fieldbyname('Editer').Value := '';
      fieldbyname('Edittime').Value := null;
      Post;
    end;

    FCCID := trim(MaxSubId);
    result := True;
  except
    Application.MessageBox('保存异常!', '提示', 0);
  end;

end;

function TfrmTradePlanInPut.getLabel(labelType:string=''):Boolean;
var
  mLblFile:String;
begin
  if TRIM(LabVolume.Text)<>'' then
  begin
    ExportFtErpFile(TRIM(LabVolume.Text) + '.rmf', ADOQueryBaseTemp);
    mLblFile := ExtractFilePath(Application.ExeName) + 'Report\' + TRIM(LabVolume.Text)+ '.rmf';
    if not FileExists(mLblFile) then
      CopyFile(PChar(ExtractFilePath(Application.ExeName) + 'Report\模板标签.rmf'), PChar(mLblFile), False);
     with RMGridReport1 do
    begin
      LoadFromFile(mLblFile);
      //Preview := RMPreview1;
      ShowReport;
    end;
  end
  else
  begin
    RMGridReport1.Clear;
  end;
  if TRIM(LabPackage.Text)<>'' then
  begin
    ExportFtErpFile(TRIM(LabPackage.Text) + '.rmf', ADOQueryBaseTemp);
    mLblFile := ExtractFilePath(Application.ExeName) + 'Report\' + TRIM(LabPackage.Text)+ '.rmf';
    if not FileExists(mLblFile) then
      CopyFile(PChar(ExtractFilePath(Application.ExeName) + 'Report\模板标签.rmf'), PChar(mLblFile), False);
     with RMGridReport2 do
    begin
      LoadFromFile(mLblFile);
     // Preview := RMPreview2;
      ShowReport;
    end;
  end
  else
  begin
    RMGridReport2.Clear;
  end;


end;

//**
procedure TfrmTradePlanInPut.initGrid();
begin
  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;
  try
    dxLayoutControl_bzplan.BeginUpdate ;
    tv1.BeginUpdate() ;
    SCreateCDS(ADOTemp, CDS_1);
    SInitCDSData(ADOTemp, CDS_1);
    LSCSHData(ADOTemp, dxLayoutControl_bzplan, 7);
  finally
    dxLayoutControl_bzplan.EndUpdate();
    tv1.EndUpdate ;
  end;
end;


 ///////////////////////////////////////////////
function TfrmTradePlanInPut.getOrderMakeType(): Boolean;
begin
  result := false;
  fOrdeNOIsAuto:='';
  //////////////////////////////
  try
    with DataLink_TradePlan.AdoDataLink do
    begin
      close;
      sql.Clear;
      sql.Add('select * from sd_erp_dll_params');
      sql.Add('where dll_name='+quotedstr('djdPackPlan.dll'));
      sql.Add('and set_item='+quotedstr('order_auto'));
      sql.Add('and valid=1');
      open;
      if recordCount>0 then
        fOrdeNOIsAuto := trim(fieldByName('set_value').AsString);
    end;
  except

  end;
  result := true;
end;

////////////////////////////////////////////////
procedure TfrmTradePlanInPut.threadLoadform();

begin



end;

end.
