unit U_SFProduceCaseInputNew;

interface

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

type
  TfrmSFProduceCaseInputNew = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Label8: TLabel;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    DataSource1: TDataSource;
    CDS_Sub: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOQueryTemp10: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_SeqNo: TcxGridDBColumn;
    v1ProduceBegTime: TcxGridDBColumn;
    v1ProduceEndTime: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    DeviceNo: TBtnEditA;
    GroupNo: TBtnEditA;
    P_BCode: TBtnEditA;
    P_JCode: TBtnEditA;
    PlanNO: TEdit;
    P_Color: TComboBox;
    Model: TEdit;
    Label9: TLabel;
    Label7: TLabel;
    P_JColor: TComboBox;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Tv2Column1: TcxGridDBColumn;
    CDS_JB: TClientDataSet;
    DataSource2: TDataSource;
    ADOQueryJB: TADOQuery;
    Panel2: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    procedure TBAddClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1DeviceNoPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1GroupNoPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1SupplierNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1P_BaseChnNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1LbStr6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure DeviceNoBtnClick(Sender: TObject);
    procedure GroupNoBtnClick(Sender: TObject);
    procedure P_BCodeBtnClick(Sender: TObject);
    procedure P_JCodeBtnClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    procedure  InitGrid();
    function   SaveData():Boolean;
    function   SaveDataSF():Boolean;
    procedure  InitJBGrid();
    { Private declarations }
  public
    PState:Integer;
    FPlanNo,FP_SeqNo,FADID,FADType:String;

    { Public declarations }
  end;

var
  frmSFProduceCaseInputNew: TfrmSFProduceCaseInputNew;

implementation
uses
  U_adodbmd,U_SelfForm,U_global,U_FormPas
  ,U_CxGridSet,U_ItemManageNew,U_SupplyHelp,U_PbProductHelp10;

{$R *.dfm}

procedure TfrmSFProduceCaseInputNew.TBAddClick(Sender: TObject);
begin
  with CDS_Sub do
  begin
    Append;
    CDS_Sub.FieldByName('SFlag').Value:='2';
    Post;
  end;
end;

procedure TfrmSFProduceCaseInputNew.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.FieldByName('SFlag').AsString)='2' then
    CDS_Sub.Delete
  else begin
    Application.MessageBox('此条数据不能在此删除！','提示',0);
  end;
end;

procedure TfrmSFProduceCaseInputNew.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Qry_SFCase :begdate,:enddate,:Pstate,:ADID');
      Parameters.ParamByName('Pstate').Value:=2;
      Parameters.ParamByName('ADID').Value:=Trim(FADID);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSFProduceCaseInputNew.FormShow(Sender: TObject);
var
  fsj:String;
begin
  ReadCxGrid('湿法涂布机录入1',Tv1,'生产管理');
  ReadCxGrid('湿法涂布机基布卷号录入1',Tv2,'生产管理');
  fsj:='select distinct(defStr3) name, '''' Code from WC_ClothInout_Info where DepotNo like ''B%''';
  //SInitCxGridComboBoxBySql(ADOQueryTemp,v1P_Clolor,fsj,0,True,'未找到颜色！');
  InitComBoxBySql(ADOQueryTemp,P_Color,fsj,0,'未找到颜色！',True);
  fsj:='select distinct(P_Color) name, '''' Code from WC_ClothInout_Info where DepotNo like ''P%''';
  //SInitCxGridComboBoxBySql(ADOQueryTemp,v1P_Clolor,fsj,0,True,'未找到颜色！');
  InitComBoxBySql(ADOQueryTemp,P_JColor,fsj,0,'未找到颜色！',True);
  InitGrid();
  SSetWinData30(ADOQueryMain,Panel1,1);
  InitJBGrid();
end;

procedure TfrmSFProduceCaseInputNew.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('湿法涂布机录入1',Tv1,'生产管理');
  WriteCxGrid('湿法涂布机基布卷号录入1',Tv2,'生产管理');
end;

function TfrmSFProduceCaseInputNew.SaveData():Boolean;
Var
  MaxNo:string;
begin
  try
    Result:=False;
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Sub do
    begin
      CDS_Sub.First;
      while not CDS_Sub.Eof do
      begin
        if Trim(CDS_Sub.fieldbyname('PlanNo').AsString)='' then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('批号不能为空!','提示',0);
          Exit;
        end;
        if Trim(CDS_Sub.fieldbyname('GroupNo').AsString)='' then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('班别不能为空!','提示',0);
          Exit;
        end;
        if Trim(CDS_Sub.fieldbyname('Quantity1').AsString)='' then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('数量不能为空!','提示',0);
          Exit;
        end;
        if Trim(CDS_Sub.fieldbyname('JBNo').AsString)='' then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('基布卷号不能为空!','提示',0);
          Exit;
        end;
        if Trim(CDS_Sub.fieldbyname('SubId').AsString)='' then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('序号不能为空!','提示',0);
          Exit;
        end;
        if Trim(CDS_Sub.fieldbyname('SFlag').AsString)='2' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Select * from AD_SFProduce_Sub where PlanNo='''+Trim(CDS_Sub.fieldbyname('PlanNo').AsString)+'''');
            sql.Add('and SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
            Open;
          end;
          if ADOQueryCmd.RecordCount>0 then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('不能重复登记！','提示',0);
            Exit;
          end;
        end;

        if Trim(CDS_Sub.fieldbyname('SFlag').AsString)<>'2' then
        begin
          MaxNo:=Trim(FADID);
        end else
        begin
          if GetMaxNo20(ADOQueryCmd,MaxNo,'AD_PlanProduce_Case','SF',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 AD_PlanProduce_Case where ADID='''+Trim(FADID)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_Sub.fieldbyname('SFlag').AsString)<>'2' then
            Edit
          else
            Append;
          FieldByName('ADID').Value:=Trim(MaxNo);
          SSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'AD_PlanProduce_Case',1);
          FieldByName('P_SeqNo').Value:=Trim(CDS_Sub.fieldbyname('P_SeqNo').AsString);
          FieldByName('P_JCode').Value:=Trim(CDS_Sub.fieldbyname('P_JCode').AsString);
          FieldByName('P_Factory').Value:=Trim(CDS_Sub.fieldbyname('P_Factory').AsString);
          FieldByName('AdType').Value:='湿法';
          if Trim(CDS_Sub.fieldbyname('ProduceEndTime').AsString)='' then
          begin
            FieldByName('ProduceEndTime').Value:=GetServerTime10(ADOQueryTemp);
          end;
          if Trim(CDS_Sub.fieldbyname('SFlag').AsString)='2' then
          begin
            FieldByName('Filler').Value:=Trim(gUserName);
            FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end else
          if Trim(CDS_Sub.fieldbyname('SFlag').AsString)<>'2' then
          begin
            FieldByName('Editer').Value:=Trim(gUserName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from AD_EndProduct_Case where ADID='''+Trim(FADID)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_Sub.fieldbyname('SFlag').AsString)<>'2' then
            Edit
          else
            Append;
          FieldByName('ADID').Value:=Trim(MaxNo);
          SSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'AD_EndProduct_Case',2);
          FieldByName('PlanNo').Value:=Trim(CDS_Sub.fieldbyname('PlanNo').AsString);
          FieldByName('P_SeqNo').Value:=Trim(CDS_Sub.fieldbyname('P_SeqNo').AsString);
          if Trim(CDS_Sub.fieldbyname('SFlag').AsString)='2' then
          begin
            FieldByName('Filler').Value:=Trim(gUserName);
            FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end else
          if Trim(CDS_Sub.fieldbyname('SFlag').AsString)<>'2' then
          begin
            FieldByName('Editer').Value:=Trim(gUserName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          Post;
        end;
        CDS_Sub.Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('保存异常！','提示',0);
  end;
end;

function TfrmSFProduceCaseInputNew.SaveDataSF():Boolean;
Var
  MaxNo:string;
begin
  try
    Result:=False;
    ADOQueryCmd.Connection.BeginTrans;
    if PState=0 then
    begin
      with ADOQueryTemp10 do
      begin
        Close;
        sql.Clear;
        SQL.Add(' select * from AD_SFProduce_Main where PlanNo='''+Trim(PlanNO.Text)+'''');
        sql.Add(' and GroupNo='''+Trim(GroupNo.Text)+'''');
        Open;
        if not IsEmpty then
        begin
          //MaxNo:=Trim(fieldbyname('ADID').AsString);
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('此班组此批号的记录已经存在请追加记录！','提示',0);
          Exit;
        end else
        begin
          if GetMaxNo20(ADOQueryTemp,MaxNo,'AD_SFProduce_Main','SF',1,4)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!','提示',0);
            Exit;
          end;
          //保存主表信息
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('select * from AD_SFProduce_Main where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('ADID').Value:=Trim(MaxNo);
            SSetsavedataNew(ADOQueryCmd,'AD_SFProduce_Main',Panel1,1);
            //SSetSaveDataCDSNew(ADOQueryCmd,Tv1,CDS_Sub,'AD_SFProduce_Main',1);
            FieldByName('ProduceDate').Value:=FormatDateTime('yyyy-MM-dd',CDS_Sub.fieldbyname('BegTime').AsDateTime);
            FieldByName('Filler').Value:=Trim(gUserName);
            Post;
          end;

        end;
      end;
      //保存子表信息
      with CDS_Sub do
      begin
        First;
        while not Eof do
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            SQL.Add('select * from AD_SFProduce_Sub where ADID='''+Trim(MaxNo)+'''');
            sql.Add(' and SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
            Open;
            if not IsEmpty then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('不能重复登记！','提示',0);
              Exit;
            end;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from AD_SFProduce_Sub where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('ADID').Value:=Trim(MaxNo);
            SSetSaveDataCDSNew(ADOQueryCmd,Tv1,CDS_Sub,'AD_SFProduce_Sub',2);
            FieldByName('Filler').Value:=Trim(gUserName);
            Post;
          end;
          CDS_Sub.Next;
        end;
      end;
      FADID:=Trim(MaxNo);
    end;
    if PState=1 then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete  AD_SFProduce_Sub where ADID='''+Trim(FADID)+'''');
        ExecSQL;
      end;
      with CDS_Sub do
      begin
        First;
        while not eof do
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            SQL.Add('select * from AD_SFProduce_Sub where ADID='''+Trim(FADID)+'''');
            sql.Add(' and SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
            Open;
            if not IsEmpty then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('序号重复！','提示',0);
              Exit;
            end;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from AD_SFProduce_Sub where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('ADID').Value:=Trim(FADID);
            SSetSaveDataCDSNew(ADOQueryCmd,Tv1,CDS_Sub,'AD_SFProduce_Sub',2);
            FieldByName('Filler').Value:=Trim(gUserName);
            Post;
          end;
          CDS_Sub.Next;
        end;
      end;
      CDS_Sub.First;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from AD_SFProduce_Main where ADID='''+Trim(FADID)+'''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Edit;
        SSetsavedataNew(ADOQueryCmd,'AD_SFProduce_Main',Panel1,1);
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add(' Update AD_SFProduce_Sub Set RunHours=datediff(mi,BegTime,EndTime)*1.00/60');
      sql.Add(' where ADID='''+Trim(FADID)+'''');
      SQL.Add(' and Quantity1>0');
      SQL.Add(' Update AD_SFProduce_Sub Set WaitHours=datediff(mi,BegTime,EndTime)*1.00/60');
      sql.Add(' where ADID='''+Trim(FADID)+'''');
      SQL.Add(' and Quantity1=0');
      ExecSQL;
    end;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('保存异常！','提示',0);
  end;
end;

procedure TfrmSFProduceCaseInputNew.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(PlanNO.Text)='' then
  begin
    Application.MessageBox('批号不能为空!','提示',0);
    Exit;
  end;
  if Trim(GroupNo.Text)='' then
  begin
    Application.MessageBox('班别不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('Quantity1',Null,[])=True then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('SubId',Null,[])=True then
  begin
    Application.MessageBox('序号不能为空!','提示',0);
    Exit;
  end;
  if SaveDataSF() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    Close;
    ModalResult:=1;
  end;
end;

procedure TfrmSFProduceCaseInputNew.v1DeviceNoPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      flag:='DeviceNo';
      flagname:='机台';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('DeviceNo').Value:=ClientDataSet1.fieldbyname('Name').AsString;
      end;
    end;
  finally
    frmItemManageNew.Free;
  end;
end;

procedure TfrmSFProduceCaseInputNew.v1GroupNoPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      flag:='GroupNo';
      flagname:='班别';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('GroupNo').Value:=ClientDataSet1.fieldbyname('Name').AsString;
      end;
    end;
  finally
    frmItemManageNew.Free;
  end;
end;

procedure TfrmSFProduceCaseInputNew.v1SupplierNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmSupplyHelp:=TfrmSupplyHelp.Create(Application);
    with frmSupplyHelp do
    begin
      FLType:='坯布';
      if ShowModal=1 then
      begin
        CDS_Sub.Edit;
        CDS_Sub.FieldByName('P_Factory').Value:=Trim(ADOQueryHelp.fieldbyname('CustomNo').AsString);
        CDS_Sub.FieldByName('ShortName').Value:=Trim(ADOQueryHelp.fieldbyname('ShortName').AsString);
      end;
    end;
  finally
    frmSupplyHelp.Free;
  end;
end;

procedure TfrmSFProduceCaseInputNew.v1P_BaseChnNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmPbProductHelp10:=TfrmPbProductHelp10.Create(Application);
    with frmPbProductHelp10 do
    begin
      if ShowModal=1 then
      begin
        CDS_Sub.Edit;
        CDS_Sub.FieldByName('P_JCode').Value:=Trim(ADOQueryHelp.fieldbyname('P_Code').AsString);
        CDS_Sub.FieldByName('P_JName').Value:=Trim(ADOQueryHelp.fieldbyname('P_ChnName').AsString);
      end;
    end;
  finally
    frmPbProductHelp10.Free;
  end;
end;

procedure TfrmSFProduceCaseInputNew.v1LbStr6PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmPbProductHelp10:=TfrmPbProductHelp10.Create(Application);
    with frmPbProductHelp10 do
    begin
      if ShowModal=1 then
      begin
        CDS_Sub.Edit;
        CDS_Sub.FieldByName('P_BCode').Value:=Trim(ADOQueryHelp.fieldbyname('P_Code').AsString);
        CDS_Sub.FieldByName('P_BName').Value:=Trim(ADOQueryHelp.fieldbyname('P_ChnName').AsString);
      end;
    end;
  finally
    frmPbProductHelp10.Free;
  end;
end;

procedure TfrmSFProduceCaseInputNew.ToolButton2Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  OneKeyPost(Tv1,CDS_Sub);
end;

procedure TfrmSFProduceCaseInputNew.ToolButton1Click(Sender: TObject);
begin
  CopyAddRowCDS(CDS_Sub);
  CDS_Sub.Edit;
  CDS_Sub.FieldByName('SFlag').Value:='2';
  CDS_Sub.Post;
end;

procedure TfrmSFProduceCaseInputNew.DeviceNoBtnClick(Sender: TObject);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      flag:='DeviceNo';
      flagname:='机台';
      if ShowModal=1 then
      begin
        Self.DeviceNo.Text:=ClientDataSet1.fieldbyname('Name').AsString;
        Self.DeviceNo.TxtCode:=ClientDataSet1.fieldbyname('Name').AsString;
      end;
    end;
  finally
    frmItemManageNew.Free;
  end;
end;

procedure TfrmSFProduceCaseInputNew.GroupNoBtnClick(Sender: TObject);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      flag:='GroupNo';
      flagname:='班别';
      if ShowModal=1 then
      begin
        Self.GroupNo.Text:=ClientDataSet1.fieldbyname('Name').AsString;
        Self.GroupNo.TxtCode:=ClientDataSet1.fieldbyname('Name').AsString;
      end;
    end;
  finally
    frmItemManageNew.Free;
  end;
end;

procedure TfrmSFProduceCaseInputNew.P_BCodeBtnClick(Sender: TObject);
begin
  try
    frmPbProductHelp10:=TfrmPbProductHelp10.Create(Application);
    with frmPbProductHelp10 do
    begin
      if ShowModal=1 then
      begin
        Self.P_BCode.TxtCode:=Trim(ADOQueryHelp.fieldbyname('P_Code').AsString);
        Self.P_BCode.Text:=Trim(ADOQueryHelp.fieldbyname('P_ChnName').AsString);
      end;
    end;
  finally
    frmPbProductHelp10.Free;
  end;
end;

procedure TfrmSFProduceCaseInputNew.P_JCodeBtnClick(Sender: TObject);
begin
  try
    frmPbProductHelp10:=TfrmPbProductHelp10.Create(Application);
    with frmPbProductHelp10 do
    begin
      if ShowModal=1 then
      begin
        Self.P_JCode.TxtCode:=Trim(ADOQueryHelp.fieldbyname('P_Code').AsString);
        Self.P_JCode.Text:=Trim(ADOQueryHelp.fieldbyname('P_ChnName').AsString);
      end;
    end;
  finally
    frmPbProductHelp10.Free;
  end;
end;

procedure TfrmSFProduceCaseInputNew.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  try
    ADOQueryJB.DisableControls;
    with ADOQueryJB do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from AD_SFProduce_JBNO where PlanNO='''+Trim(PlanNO.Text)+'''');
      SQL.Add(' and subId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
      Open;
    end;
    CreateCDS20(ADOQueryJB,CDS_JB);
    InitCDSData20(ADOQueryJB,CDS_JB);
  finally
    ADOQueryJB.EnableControls;
  end;
end;

procedure TfrmSFProduceCaseInputNew.Button1Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  with CDS_JB do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmSFProduceCaseInputNew.Button2Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if CDS_JB.IsEmpty then Exit;
  if Trim(CDS_JB.fieldbyname('PlanNo').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete AD_SFProduce_JBNO where PlanNo='''+Trim(PlanNO.Text)+'''');
      sql.Add(' and SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
      sql.Add(' and JBNO='''+Trim(CDS_JB.fieldbyname('JBNO').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_JB.Delete;
  

end;

procedure TfrmSFProduceCaseInputNew.Button3Click(Sender: TObject);
var
  MaxNo:String;
begin
  if CDS_JB.IsEmpty then Exit;
  if Trim(PlanNO.Text)='' then
  begin
    Application.MessageBox('生产批号不能为空！','提示',0);
    Exit;
  end;
  if Trim(CDS_Sub.fieldbyname('SubId').AsString)='' then
  begin
    Application.MessageBox('序号不能为空！','提示',0);
    Exit;
  end;
  if CDS_JB.Locate('JBNO',null,[])=True then
  begin
    Application.MessageBox('基布卷号不能为空！','提示',0);
    Exit;
  end;
  if CDS_JB.Locate('JBQuantity',null,[])=True then
  begin
    Application.MessageBox('基布米数量不能为空！','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete AD_SFProduce_JBNO where PlanNo='''+Trim(PlanNO.Text)+'''');
      sql.Add(' and SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with CDS_JB do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from AD_SFProduce_JBNO where PlanNo='''+Trim(PlanNO.Text)+'''');
          sql.Add(' and SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
          sql.Add(' and JBNO='''+Trim(CDS_JB.fieldbyname('JBNO').AsString)+'''');
          Open;
          if not IsEmpty then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('基布卷号重复！','提示',0);
            Exit;
          end;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from AD_SFProduce_JBNO where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('PlanNo').Value:=Trim(PlanNO.Text);
          FieldByName('SubId').Value:=Trim(CDS_Sub.fieldbyname('SubId').AsString);
          SSetSaveDataCDSNew(ADOQueryCmd,Tv2,CDS_JB,'AD_SFProduce_JBNO',2);
          Post;
        end;
        CDS_JB.Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;

end;

procedure TfrmSFProduceCaseInputNew.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  InitJBGrid();
end;

procedure TfrmSFProduceCaseInputNew.InitJBGrid();
begin
  try
    ADOQueryJB.DisableControls;
    with ADOQueryJB do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from AD_SFProduce_JBNO where PlanNO='''+Trim(PlanNO.Text)+'''');
      SQL.Add(' and subId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
      Open;
    end;
    CreateCDS20(ADOQueryJB,CDS_JB);
    InitCDSData20(ADOQueryJB,CDS_JB);
  finally
    ADOQueryJB.EnableControls;
  end;
end;

end.
