unit U_PlanOrderPL;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ImgList, ComCtrls, ToolWin, ExtCtrls, BtnEdit, Grids,
  DBGrids, DB, ADODB, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, cxDBData, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxPC, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxTextEdit, cxCalendar, cxMemo, Buttons, DBClient, cxButtonEdit,
  cxSplitter;

type
  TfrmPlanOrderPL = class(TForm)
    ToolBar3: TToolBar;
    Tclose: TToolButton;
    ImageList24: TImageList;
    GroupBox1: TGroupBox;
    ComboBox1: TComboBox;
    begDate: TDateTimePicker;
    endDate: TDateTimePicker;
    Label2: TLabel;
    edtPlan: TEdit;
    Label3: TLabel;
    ADOQueryPlan: TADOQuery;
    DataSourcePlan: TDataSource;
    Tfind: TToolButton;
    ADOQueryCmd: TADOQuery;
    Label4: TLabel;
    P_chnName: TEdit;
    Panel1: TPanel;
    Tv1: TcxGridDBTableView;
    dbGrid2Level1: TcxGridLevel;
    dbGrid2: TcxGrid;
    Tv1PlanNo: TcxGridDBColumn;
    Tv1Deliver: TcxGridDBColumn;
    Tv1exigence: TcxGridDBColumn;
    Tv1p_num: TcxGridDBColumn;
    Tv1p_chnname: TcxGridDBColumn;
    Tv1customName: TcxGridDBColumn;
    Tv1BusineesName: TcxGridDBColumn;
    Tv1Busntime: TcxGridDBColumn;
    Tv1statusName: TcxGridDBColumn;
    Tv1P_unitName: TcxGridDBColumn;
    v1P_Other: TcxGridDBColumn;
    Edt_cust: TEdit;
    v1P_tnum: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v1P_FQNo: TcxGridDBColumn;
    v1P_YhNo: TcxGridDBColumn;
    v1P_BaseChnName: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1P_Color: TcxGridDBColumn;
    v1sbtr2: TcxGridDBColumn;
    v1LbStr6: TcxGridDBColumn;
    v1P_Csid: TcxGridDBColumn;
    v1sid: TcxGridDBColumn;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1P_FactoryName: TcxGridDBColumn;
    v1DhChk: TcxGridDBColumn;
    v1PCSeqNo: TcxGridDBColumn;
    TBPrint: TToolButton;
    v1Vuserdef1: TcxGridDBColumn;
    v1Crafts: TcxGridDBColumn;
    TBExport: TToolButton;
    v1CheckName: TcxGridDBColumn;
    v1PCDate: TcxGridDBColumn;
    v1BChkTime: TcxGridDBColumn;
    TBView: TToolButton;
    Label5: TLabel;
    p_num1: TEdit;
    Label6: TLabel;
    p_num2: TEdit;
    Label1: TLabel;
    P_FQNo: TEdit;
    Label7: TLabel;
    P_YhNo: TEdit;
    Label8: TLabel;
    P_Color: TEdit;
    Label9: TLabel;
    P_BaseChnName: TEdit;
    Label10: TLabel;
    LbStr6: TEdit;
    Label12: TLabel;
    P_Csid: TEdit;
    cxSplitter1: TcxSplitter;
    ToolButton5: TToolButton;
    v1PLTime: TcxGridDBColumn;
    v1PLPerson: TcxGridDBColumn;
    v1PCPerson: TcxGridDBColumn;
    Panel2: TPanel;
    ToolBar1: TToolBar;
    btnAddDye: TToolButton;
    btnDelDye: TToolButton;
    btnSaveDye: TToolButton;
    grdDye: TcxGrid;
    grdDyeTV1: TcxGridDBTableView;
    grdDyeTV1OrderId: TcxGridDBColumn;
    grdDyeTV1chnName: TcxGridDBColumn;
    grdDyeTV1DyeStyle: TcxGridDBColumn;
    grdDyeTV1DyeUnitName: TcxGridDBColumn;
    grdDyeTV1UnitdyeNum: TcxGridDBColumn;
    grdDyeTV1stepId: TcxGridDBColumn;
    grdDyeTV1beizhu: TcxGridDBColumn;
    grdDyeL1: TcxGridLevel;
    CDS_Sub: TClientDataSet;
    AdoQuerySub: TADOQuery;
    ds1: TDataSource;
    grdDyeTV1Crafts: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    procedure FormCreate(Sender: TObject);
    procedure TcloseClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TfindClick(Sender: TObject);
    procedure rgSystypeClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v1P_FQNoCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure TBPrintClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBViewClick(Sender: TObject);
    procedure p_num1KeyPress(Sender: TObject; var Key: Char);
    procedure edtPlanChange(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure btnAddDyeClick(Sender: TObject);
    procedure btnDelDyeClick(Sender: TObject);
    procedure grdDyeTV1chnNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure grdDyeTV1CraftsPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure btnSaveDyeClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure grdDyeTV1UnitdyeNumPropertiesChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure grdDyeTV1beizhuPropertiesEditValueChanged(Sender: TObject);
  private
    procedure DoQuery();
    procedure InitSubGrid();
    function  saveDye(): Boolean;
  public
    FPlanNo:string;
    { Public declarations }
  end;

var
  frmPlanOrderPL: TfrmPlanOrderPL;

implementation
uses
  U_adodbmd,U_global, U_status,U_SelfForm,U_CxGridSet,
  U_FormPas,U_plan,U_frameHelp10,U_XcCodeHelp,U_ItemManageNew,U_RhlHelp,
  U_RhlHelpOne,U_GetXYHelp,U_GetPlanXYHelp;
{$R *.dfm}

procedure TfrmPlanOrderPL.FormCreate(Sender: TObject);
begin
  ReadCxGrid('计划单配料信息',Tv1,'计划单');
  dbgrid2.Align :=alClient;
  begDate.DateTime :=SGetServerDate(ADOQueryTemp);
  endDate.DateTime :=SGetServerDate(ADOQueryTemp);
  DoQuery();
  InitSubGrid();
end;
//////////////////////////////////////////////////
  //函数：提交数据
//////////////////////////////////////////////////
procedure TfrmPlanOrderPL.DoQuery();
var
  mwhere:string;
begin
  if comboBox1.ItemIndex=0 then
  begin
    mwhere:=' and BusnTime>='''+dateTostr(begdate.Date)+''' and BusnTime<'''+dateTostr(endDate.Date+1)+'''';
  end
  else if comboBox1.ItemIndex=1 then
  begin
    mwhere:=' and deliver>='''+dateTostr(begdate.Date)+''' and deliver<'''+dateTostr(endDate.Date+1)+'''';
  end
  else if comboBox1.ItemIndex=2 then
  begin
    mwhere:=' and PCSCDate>='''+dateTostr(begdate.Date)+''' and PCSCDate<'''+dateTostr(endDate.Date+1)+'''';
  end;

  //计划单
  if trim(edtPlan.Text)<>'' then
  begin
    mwhere:=mwhere+' and A.planNo like ''%'+trim(edtPlan.Text)+'%''';
  end;

  //客户
  if trim(Edt_cust.Text)<>'' then
  begin
    mwhere:=mwhere+' and CustomNo like ''%'+trim(Edt_cust.Text)+'%''';
  end;
  //品种
  if trim(P_chnName.Text)<>'' then
  begin
    mwhere:=mwhere+' and P_chnName like ''%'+trim(P_chnName.Text)+'%''';
  end;
  //产品编号
  if trim(P_FQNo.Text)<>'' then
  begin
    mwhere:=mwhere+' and P_FQNo like ''%'+trim(P_FQNo.Text)+'%''';
  end;
  //纹路
  if trim(P_YhNo.Text)<>'' then
  begin
    mwhere:=mwhere+' and P_YhNo like ''%'+trim(P_YhNo.Text)+'%''';
  end;
  //色号
  if trim(P_Color.Text)<>'' then
  begin
    mwhere:=mwhere+' and P_Color like ''%'+trim(P_Color.Text)+'%''';
  end;
  //底布
  if trim(P_BaseChnName.Text)<>'' then
  begin
    mwhere:=mwhere+' and P_BaseChnName like ''%'+trim(P_BaseChnName.Text)+'%''';
  end;
  //贝斯
  if trim(LbStr6.Text)<>'' then
  begin
    mwhere:=mwhere+' and LbStr6 like ''%'+trim(LbStr6.Text)+'%''';
  end;
  //下达部门
  if trim(gDef1)<>'' then
  begin
    mwhere:=mwhere+' and M.Dept ='''+trim(gDef1)+'''';
  end;
  //业务员
  {if trim(BusineesName.Text)<>'' then
  begin
    mwhere:=mwhere+' and BusineesName like ''%'+trim(BusineesName.Text)+'%''';
  end;}
  //打样号
  if trim(P_Csid.Text)<>'' then
  begin
    mwhere:=mwhere+' and P_Csid like ''%'+trim(P_Csid.Text)+'%''';
  end;
  if Trim(p_num1.Text)<>'' then
  begin
    mwhere:=mwhere+' and P_num >='+trim(p_num1.Text);
  end;
  if Trim(p_num2.Text)<>'' then
  begin
    mwhere:=mwhere+' and P_num <='+trim(p_num2.Text);
  end;

  try
    panel1.Visible:=true;
    panel1.Refresh;
    ADOQueryPlan.DisableControls;
    with ADOQueryPlan do
    begin
      close;
      sql.Clear ;
      sql.Add('EXEC P_Get_PlanListPL ');
      sql.Add(' '+intTostr(cxTabControl1.TabIndex));
      sql.Add(','+QuotedStr(formatDatetime('yyyy-MM-dd',begdate.Date)));
      sql.Add(','+QuotedStr(formatDatetime('yyyy-MM-dd',(enddate.Date+1))));
      sql.Add(','+quotedStr(mwhere));
      Open;
    end;
  finally
    panel1.Visible :=false;
    ADOQueryPlan.EnableControls;
  end;
end;
procedure TfrmPlanOrderPL.InitSubGrid();
begin
  try
    AdoQuerySub.DisableControls;
    with AdoQuerySub do
    begin
      close;
      sql.Clear ;
      SQL.Add('select A.*,B.MName ChnName from MD_ProductPL  A ');
      SQL.Add(' JOIN Gy_Material B ON B.MNumber = A.Dyecode ');
      SQL.Add('where PlanNo='''+Trim(ADOQueryPlan.fieldbyname('PlanNo').AsString)+'''');
      sql.Add(' and P_SeqNo='''+Trim(ADOQueryPlan.fieldbyname('P_SeqNo').AsString)+'''');
      sql.Add(' and Dept='''+Trim(ADOQueryPlan.fieldbyname('Dept').AsString)+'''');
      Open;
      CreateCDS20(AdoQuerySub,CDS_Sub);
      InitCDSData20(AdoQuerySub,CDS_Sub);
    end;
  finally
    AdoQuerySub.EnableControls;
  end;
end;

procedure TfrmPlanOrderPL.TcloseClick(Sender: TObject);
begin
  close;
  WriteCxGrid('计划单配料信息',Tv1,'计划单');
end;

procedure TfrmPlanOrderPL.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmPlanOrderPL.FormDestroy(Sender: TObject);
begin
  frmPlanOrderPL:=NIL;
end;

procedure TfrmPlanOrderPL.TfindClick(Sender: TObject);
begin
  DoQuery();
end;

procedure TfrmPlanOrderPL.rgSystypeClick(Sender: TObject);
begin
  DoQuery();
end;

procedure TfrmPlanOrderPL.cxTabControl1Change(Sender: TObject);
begin
 { if cxTabControl1.TabIndex=0 then
    v1PCSeqNo.Visible:=False
  else
    v1PCSeqNo.Visible:=True;
  if (cxTabControl1.TabIndex=1) or (cxTabControl1.TabIndex=2)  then
  begin
    v1PCSeqNo.Options.Focusing:=True ;
    Tv1exigence.Options.Focusing:=True;
  end
  else
  begin
    v1PCSeqNo.Options.Focusing:=False;
    Tv1exigence.Options.Focusing:=False;
  end;  }
  Tfind.Click ;
  //ADOQueryPlan.Locate('PlanNo',FPlanNo,[]);
end;

procedure TfrmPlanOrderPL.v1P_FQNoCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id:Integer;
begin
    begin
      Id:=TV1.GetColumnByFieldName('SBtr3').Index;//;-TV1.GroupedItemCount;
      if Id<0 then Exit;
      if AViewInfo.GridRecord.Values[Id]='Red' then
      begin
        ACanvas.Brush.Color:=clRed;
      end else
      if AViewInfo.GridRecord.Values[Id]='Purple' then
      begin
        ACanvas.Brush.Color:=clPurple;
      end else
      if AViewInfo.GridRecord.Values[Id]='Olive' then
      begin
        ACanvas.Brush.Color:=clOlive;
      end else
      if AViewInfo.GridRecord.Values[Id]='Teal' then
      begin
        ACanvas.Brush.Color:=clTeal;
      end else
      if AViewInfo.GridRecord.Values[Id]='Background' then
      begin
        ACanvas.Brush.Color:=clBackground;
      end;
    end
end;

procedure TfrmPlanOrderPL.TBPrintClick(Sender: TObject);
begin
  SelPrintDataNew(Tv1,ADOQueryPlan,'生 产 顺 序 表','日期：'+(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOQueryTemp))),'');
end;

procedure TfrmPlanOrderPL.FormShow(Sender: TObject);
begin
  ReadCxGrid('计划单排产信息',Tv1,'计划单');
  v1PCSeqNo.Visible:=True;
end;

procedure TfrmPlanOrderPL.TBExportClick(Sender: TObject);
begin
  if ADOQueryPlan.IsEmpty then Exit;
  TcxGridToExcel('生产顺序表',dbGrid2);
end;

procedure TfrmPlanOrderPL.TBViewClick(Sender: TObject);
begin
  try
    frmPlan:=TfrmPlan.Create(Application);
    with frmPlan do
    begin
      fWinStatus:=5;
      FplanNo:=Trim(Self.ADOQueryPlan.fieldbyname('PlanNo').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmPlan.Free;
  end;

end;

procedure TfrmPlanOrderPL.p_num1KeyPress(Sender: TObject; var Key: Char);
begin
  if not(Key in['0'..'9',#8,#27]) then
  begin
    key:=#0;
  end;
end;

procedure TfrmPlanOrderPL.edtPlanChange(Sender: TObject);
begin
  DoQuery();
end;

procedure TfrmPlanOrderPL.ToolButton5Click(Sender: TObject);
var
  splanNo:string;
begin
  frmPlan:=TfrmPlan.create(self);
  with frmPlan do
  begin
    fWinStatus:=1;
    FplanNo:=trim(ADOQueryPlan.fieldByName('PlanNo').asString);
    planNo.Enabled:=False;
    showModal;
    free;
  end;
  splanno:=trim(ADOQueryPlan.fieldByName('PlanNo').asString);
  DoQuery();
  ADOQueryPlan.Locate('planno',splanno,[]);
end;

procedure TfrmPlanOrderPL.btnAddDyeClick(Sender: TObject);
var
  mStepId: Integer;
begin
  if ADOQueryPlan.IsEmpty then Exit;
  if CDS_Sub.IsEmpty then
  begin
    mStepId := 1;
  end
  else
  begin
    CDS_Sub.Last ;
    mStepId := CDS_Sub.FieldByName('stepId').AsInteger;
  end;

  frmRhlHelp := TfrmRhlHelp.Create(Application);
  with frmRhlHelp do
  begin
    if ShowModal = 1 then
    begin
      with CDS_sel do
      begin
        try
          DisableControls;
          First ;
          while not Eof do
          begin
            CDS_Sub.Append;
            CDS_Sub.FieldByName('stepId').Value:=mStepId;
            CDS_Sub.FieldByName('OrderId').Value:= CDS_Sub.RecordCount+1;
            CDS_Sub.FieldByName('DyeCode').Value :=trim(FieldByName('DyeCode').AsString);
            CDS_Sub.FieldByName('chnName').Value :=trim(FieldByName('chnName').AsString);
            CDS_Sub.FieldByName('DyeStyle').Value :=FieldByName('DyepropertyName').AsString;
            CDS_Sub.FieldByName('Unit').Value :=FieldByName('DyeUnitName').AsString;
            CDS_Sub.Post;
            Next;
          end;
        finally
          EnableControls;
        end;
      end;
    end;
    Release;
  end;
  btnSaveDye.Enabled:=True;
end;

procedure TfrmPlanOrderPL.btnDelDyeClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('PLID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete MD_ProductPL where PLID='''+Trim(CDS_Sub.fieldbyname('PLID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_Sub.Delete;
end;

procedure TfrmPlanOrderPL.grdDyeTV1chnNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  if ADOQueryPlan.IsEmpty then Exit;
  frmRhlHelpOne := TfrmRhlHelpOne.Create(Application);
  with frmRhlHelpOne do
  begin
    if ShowModal = 1 then
    begin
      CDS_Sub.Edit;
      CDS_Sub.FieldByName('DyeCode').Value :=trim(ADOQueryFrom.FieldByName('MNumber').AsString);
      CDS_Sub.FieldByName('chnName').Value :=trim(ADOQueryFrom.FieldByName('MName').AsString);
      CDS_Sub.FieldByName('DyeStyle').Value :=ADOQueryFrom.FieldByName('kfsortname').AsString;
      CDS_Sub.FieldByName('Unit').Value :=ADOQueryFrom.FieldByName('PrimaryUnitName').AsString;
    end;
    Release;
  end;
  btnSaveDye.Enabled:=True;
end;

procedure TfrmPlanOrderPL.grdDyeTV1CraftsPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      flag:='JGXMTYPE';
      flagname:='工序';
      if ShowModal=1 then
      begin
        CDS_Sub.Edit;
        CDS_Sub.FieldByName('Crafts').Value:=Trim(ClientDataSet1.fieldbyname('name').AsString);
        //CDSDye.Post;
      end;
    end;
  finally
    frmItemManageNew.Free;
  end;
  btnSaveDye.Enabled:=True;
end;
function TfrmPlanOrderPL.saveDye(): Boolean;
var
  k:integer;
  MaxNo:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.First;
    k := 1;
    while not CDS_Sub.Eof do
    begin
      if Trim(CDS_Sub.fieldbyname('PLId').AsString)<>'' then
      begin
        MaxNo:=Trim(CDS_Sub.fieldbyname('PLId').AsString);
      end else
      begin
        if GetMaxNo20(ADOQueryTemp,MaxNo,'MD_ProductPL','PL',1,3)=False  then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('SELECT * FROM MD_ProductPL');
        SQL.Add('WHERE PlID='''+Trim(CDS_Sub.fieldbyname('PlID').AsString)+'''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        if Trim(CDS_Sub.FieldByName('PlID').AsString)='' then
          Append
        else
          Edit;
        FieldByName('PLID').Value := Trim(MaxNo);
        FieldByName('PlanNo').Value := Trim(ADOQueryPlan.FieldByName('PlanNo').AsString);
        FieldByName('P_SeqNo').Value :=Trim(ADOQueryPlan.FieldByName('P_SeqNo').AsString);
        FieldByName('Dept').Value :=Trim(ADOQueryPlan.FieldByName('Dept').AsString);
        FieldByName('dyeCode').Value :=CDS_Sub.FieldByName('DyeCode').AsString;
        FieldByName('DyeStyle').Value :=CDS_Sub.FieldByName('DyeStyle').AsString;
        FieldByName('stepId').Value :=CDS_Sub.FieldByName('stepId').AsInteger;
        FieldByName('OrderID').Value :=k;
        FieldByName('Unit').Value :=CDS_Sub.FieldByName('Unit').AsString ;
        FieldByName('UnitDyeNum').Value :=CDS_Sub.FieldByName('UnitDyeNum').AsFloat ;
        FieldByName('Crafts').Value :=trim(CDS_Sub.FieldByName('Crafts').asString);
        FieldByName('beizhu').Value :=trim(CDS_Sub.FieldByName('BeiZhu').asString);
        if Trim(CDS_Sub.FieldByName('PlanNo').AsString)='' then
        begin
          FieldByName('Filler').Value:=Trim(gUserName);
        end else
        begin
          FieldByName('Editer').Value:=Trim(gUserName);
          FieldByName('EditTime').Value:=GetServerTime10(ADOQueryTemp);
        end;
        Post;
      end;
      Inc(k);
      CDS_Sub.Next;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update MD_ProductPC Set PLFlag=1,PLPerson='''+Trim(gUserName)+'''');
      sql.Add(' ,PLTime=getdate() ');
      sql.Add(' where  PCID='''+Trim(ADOQueryPlan.fieldbyname('PCID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', MB_ICONSTOP);
    Result := False;
  end;
end;

procedure TfrmPlanOrderPL.btnSaveDyeClick(Sender: TObject);
begin
  if CDS_Sub.Locate('chnName',null,[]) then
  begin
    Application.MessageBox('所选材料不能为空！','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('UnitdyeNum',null,[]) then
  begin
    Application.MessageBox('配方数不能为空！','提示',0);
    Exit;
  end;
  if saveDye() then
  begin
    Application.MessageBox('数据保存成功！','提示',0);
    btnSaveDye.Enabled:=false;
    Tfind.Click;
  end;
end;

procedure TfrmPlanOrderPL.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitSubGrid();
end;

procedure TfrmPlanOrderPL.grdDyeTV1UnitdyeNumPropertiesChange(
  Sender: TObject);
begin
  btnSaveDye.Enabled:=True;
end;

procedure TfrmPlanOrderPL.ToolButton1Click(Sender: TObject);
var
  mStepId: Integer;
begin
  if ADOQueryPlan.IsEmpty then Exit;
  if CDS_Sub.IsEmpty then
  begin
    mStepId := 1;
  end
  else
  begin
    CDS_Sub.Last ;
    mStepId := CDS_Sub.FieldByName('stepId').AsInteger;
  end;
  try
    frmGetXYHelp:=TfrmGetXYHelp.Create(Application);
    with frmGetXYHelp do
    begin
      DYCode.Text:=Trim(ADOQueryPlan.fieldbyname('P_Csid').AsString);
      if ShowModal=1 then
      begin
        with ADOQueryDY do
        begin
          try
            DisableControls;
            First ;
            while not Eof do
            begin
              CDS_Sub.Append;
              CDS_Sub.FieldByName('stepId').Value:=mStepId;
              CDS_Sub.FieldByName('OrderId').Value:= CDS_Sub.RecordCount+1;
              CDS_Sub.FieldByName('DyeCode').Value :=trim(FieldByName('DyeCode').AsString);
              CDS_Sub.FieldByName('chnName').Value :=trim(FieldByName('chnName').AsString);
              CDS_Sub.FieldByName('DyeStyle').Value :=FieldByName('DyeStyle').AsString;
              CDS_Sub.FieldByName('Unit').Value :=FieldByName('DyeUnit').AsString;
              CDS_Sub.FieldByName('Crafts').Value:=FieldByName('GXMC').AsString;
              CDS_Sub.FieldByName('Beizhu').Value:=FieldByName('Beizhu').AsString;
              CDS_Sub.FieldByName('UnitdyeNum').Value:=FieldByName('UnitdyeNum').AsString;
              CDS_Sub.Post;
              Next;
            end;
          finally
            EnableControls;
          end;
        end;
      end;
    end;
  finally
    frmGetXYHelp.Free;
  end;
end;

procedure TfrmPlanOrderPL.ToolButton2Click(Sender: TObject);
var
  mStepId: Integer;
begin
  if ADOQueryPlan.IsEmpty then Exit;
  if CDS_Sub.IsEmpty then
  begin
    mStepId := 1;
  end
  else
  begin
    CDS_Sub.Last ;
    mStepId := CDS_Sub.FieldByName('stepId').AsInteger;
  end;
  try
    frmGetPlanXYHelp:=TfrmGetPlanXYHelp.Create(Application);
    with frmGetPlanXYHelp do
    begin
      PlanNO.Text:=Trim(Self.ADOQueryPlan.fieldbyname('sid').AsString);
      P_Color.Text:=Trim(Self.ADOQueryPlan.fieldbyname('P_Color').AsString);
      if ShowModal=1 then
      begin
        with ADOQueryDY do
        begin
          try
            DisableControls;
            First ;
            while not Eof do
            begin
              CDS_Sub.Append;
              CDS_Sub.FieldByName('stepId').Value:=mStepId;
              CDS_Sub.FieldByName('OrderId').Value:= CDS_Sub.RecordCount+1;
              CDS_Sub.FieldByName('DyeCode').Value :=trim(FieldByName('DyeCode').AsString);
              CDS_Sub.FieldByName('chnName').Value :=trim(FieldByName('chnName').AsString);
              CDS_Sub.FieldByName('DyeStyle').Value :=FieldByName('DyeStyle').AsString;
              CDS_Sub.FieldByName('Unit').Value :=FieldByName('Unit').AsString;
              CDS_Sub.FieldByName('Crafts').Value:=FieldByName('Crafts').AsString;
              CDS_Sub.FieldByName('Beizhu').Value:=FieldByName('Beizhu').AsString;
              CDS_Sub.FieldByName('UnitdyeNum').Value:=FieldByName('UnitdyeNum').AsString;
              CDS_Sub.Post;
              Next;
            end;
          finally
            EnableControls;
          end;
        end;
      end;
    end;
  finally
    frmGetPlanXYHelp.Free;
  end;
  btnSaveDye.Enabled:=True;
end;

procedure TfrmPlanOrderPL.grdDyeTV1beizhuPropertiesEditValueChanged(
  Sender: TObject);
begin
  btnSaveDye.Enabled:=True;
end;

end.
