unit U_SKBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ToolWin, ComCtrls,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, ADODB, ImgList,
  StdCtrls, ExtCtrls, cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxButtonEdit, BtnEdit, cxTL, cxMaskEdit, cxInplaceContainer, cxDBTL,
  cxTLData, cxContainer, cxCurrencyEdit, cxCheckBox, cxSplitter;

type
  TfrmSKBase = class(TForm)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBClose: TToolButton;
    ADOConnection1: TADOConnection;
    ThreeImgList: TImageList;
    DataSource2: TDataSource;
    ML_GYS: TClientDataSet;
    DataSource3: TDataSource;
    ML_GX: TClientDataSet;
    ADOQuery6: TADOQuery;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1XTNo: TcxGridDBColumn;
    v1CYName: TcxGridDBColumn;
    v1CYNo: TcxGridDBColumn;
    v1GYMXNote: TcxGridDBColumn;
    v1CYCF: TcxGridDBColumn;
    v1CYMF: TcxGridDBColumn;
    v1CYKZ: TcxGridDBColumn;
    v1CYSpec: TcxGridDBColumn;
    v1DefStr6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel3: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    CYID: TEdit;
    CYName: TEdit;
    CYNo: TEdit;
    Panel4: TPanel;
    Panel5: TPanel;
    Label6: TLabel;
    Label7: TLabel;
    MXName: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    BPMiDu: TEdit;
    BPJS: TEdit;
    Button1: TButton;
    DataSource4: TDataSource;
    ADOQueryTree: TADOQuery;
    Order_Tree: TClientDataSet;
    Panel6: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1BPCode: TcxGridDBColumn;
    V1BPName: TcxGridDBColumn;
    V1BPMF1: TcxGridDBColumn;
    V1BPKZ: TcxGridDBColumn;
    V1BPJS: TcxGridDBColumn;
    v1BPMiDu: TcxGridDBColumn;
    V1BPID: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    ToolButton1: TToolButton;
    Panel7: TPanel;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    ToolBar3: TToolBar;
    ToolPBLE: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    V1Column1: TcxGridDBColumn;
    Label12: TLabel;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    BPCF: TEdit;
    BPMF: TEdit;
    BPKZ: TEdit;
    MXCode: TEdit;
    Label13: TLabel;
    CPMiDu: TEdit;
    V1Column2: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    BPNameM: TEdit;
    BPCodeM: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure BPNamehange(Sender: TObject);
    procedure V1NotePropertiesEditValueChanged(Sender: TObject);
    procedure V1HelpTypePropertiesEditValueChanged(Sender: TObject);
    procedure v1note1PropertiesEditValueChanged(Sender: TObject);
    procedure V1BPCodePropertiesEditValueChanged(Sender: TObject);
    procedure V1BPNamePropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure BPCodeMChange(Sender: TObject);
    procedure Tv3CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure CYNoChange(Sender: TObject);
    procedure CYIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure TPBLBClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button1KeyPress(Sender: TObject; var Key: Char);
    procedure cxDBTreeList1Click(Sender: TObject);
    procedure MXCodeKeyPress(Sender: TObject; var Key: Char);
    procedure MXNameKeyPress(Sender: TObject; var Key: Char);
    procedure BPJSKeyPress(Sender: TObject; var Key: Char);
    procedure BPMFKeyPress(Sender: TObject; var Key: Char);
    procedure BPKZKeyPress(Sender: TObject; var Key: Char);
    procedure BPMiDuKeyPress(Sender: TObject; var Key: Char);
    procedure CYNameKeyPress(Sender: TObject; var Key: Char);
    procedure CYNoKeyPress(Sender: TObject; var Key: Char);
    procedure BPCFKeyPress(Sender: TObject; var Key: Char);
    procedure CPMiDuKeyPress(Sender: TObject; var Key: Char);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
  private
    FBPID,FCPID,FCPNo,FCPName:string;
    procedure InitGrid();
    procedure SaveData();
    procedure initTree();
    { Private declarations }
  public
    flag,flagname,snote,MainType,FGYName,Fint:string;
    fnote,fnote1,forderno,fZdyFlag,ViewFlag,fHelpType:Boolean;
    PPSTE:integer;
    { Public declarations }
  end;
var
  frmSKBase: TfrmSKBase;

implementation
uses
  U_DataLink,U_Fun10,U_BPZdy_SH, U_BPZdy_HXK,U_CPTypePB;

{$R *.dfm}
procedure TfrmSKBase.initTree();
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select *');
    sql.Add(', Case when isnull(CPNo,'''')<>'''' then RTrim(CPNo)+''/''+CPName else CPName end as CP ');
    SQL.Add('from CP_TypePB order by CPlevel,CPOrder,CPName');
    Open;
  end;
  SCreateCDS20(ADOQueryTree,Order_Tree);
  SInitCDSData20(ADOQueryTree,Order_Tree);
  cxDBTreeList1.Items[0].Expand(true);
end;

procedure TfrmSKBase.SaveData();
var maxno:string;
begin
  if FGYName<>'' then exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not eof do
      begin
        if Trim(ClientDataSet1.FieldByName('BPID').AsString)='' then
        begin
          if GetLSNo(ADOQueryTemp,maxno,'BP','BP_ZDY',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet1.EnableControls;
            Application.MessageBox('取最大编号失败！','提示',0);
            Exit;
          end;
        end
        else
        begin
          maxno:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BP_ZDY where BPID='''+Trim(maxno)+'''');
          Open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.Append;
        end
        else
        begin
          ADOQueryCmd.Edit;
        end;
        ADOQueryCmd.FieldByName('BPID').Value:=Trim(maxno);
        SSetSaveDataCDSNew(ADOQueryCmd,Tv1,ClientDataSet1,'BP_ZDY',3);
        ADOQueryCmd.FieldByName('BPType').Value:=flagName;
        ADOQueryCmd.FieldByName('BPFlag').Value:=flag;
        ADOQueryCmd.FieldByName('valid').Value:='Y';
        ADOQueryCmd.Post;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.add('select * from BP_ZDY where BPCode='''+Trim(ClientDataSet1.fieldbyname('BPCode').AsString)+'''');
          sql.Add(' and BPFlag='''+trim(flag)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          if ADOQueryTemp.RecordCount>1 then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('delete BP_ZDY where BPID='''+Trim(ClientDataSet1.fieldbyname('BPID').AsString)+'''');
              ExecSQL;
            end;
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet1.EnableControls;
            Application.MessageBox('编号重复！','提示',0);
            Exit;
          end;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.add('select * from BP_ZDY where BPName='''+Trim(ClientDataSet1.fieldbyname('BPName').AsString)+'''');
          sql.add(' and isnull(BPMF,'''')='''+Trim(ClientDataSet1.fieldbyname('BPMF').AsString)+'''');
          sql.add(' and isnull(BPKZ,'''')='''+Trim(ClientDataSet1.fieldbyname('BPKZ').AsString)+'''');
          sql.add(' and isnull(BPJS,'''')='''+Trim(ClientDataSet1.fieldbyname('BPJS').AsString)+'''');
          sql.add(' and isnull(BPWS,'''')='''+Trim(ClientDataSet1.fieldbyname('BPWS').AsString)+'''');
          sql.add(' and isnull(BPMiDu,'''')='''+Trim(ClientDataSet1.fieldbyname('BPMiDu').AsString)+'''');
          sql.add(' and isnull(BPUnit,'''')='''+Trim(ClientDataSet1.fieldbyname('BPUnit').AsString)+'''');
          sql.Add(' and BPFlag='''+trim(flag)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          if ADOQueryTemp.RecordCount>1 then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('delete BP_ZDY where BPID='''+Trim(ClientDataSet1.fieldbyname('BPID').AsString)+'''');
              ExecSQL;
            end;
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet1.EnableControls;
            Application.MessageBox('名称门幅克重经纱纬纱重复！','提示',0);
            Exit;
          end;
        end;
        ClientDataSet1.Edit;
        ClientDataSet1.FieldByName('BPID').Value:=Trim(maxno);
        //ClientDataSet1.Post;
        Next;
      end;
      ClientDataSet1.EnableControls;
    end;
    TV1.OptionsData.Editing:=false;
    TV1.OptionsSelection.CellSelect:=false;
    ADOQueryCmd.Connection.CommitTrans;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmSKBase.FormCreate(Sender: TObject);
begin
  try
    //cxGrid1.Align:=alClient;
    with ADOConnection1 do
    begin
      Connected:=false;
      ConnectionString:=DConString;
      //ConnectionString:='';
      Connected:=true;
    end;
  except;
    frmSKBase.Free;
  end;
end;

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

procedure TfrmSKBase.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Select_BP :Code,:PState,:CYType,:Stats ');
      Parameters.ParamByName('Code').Value:='';
      Parameters.ParamByName('PState').Value:=0;
      Parameters.ParamByName('CYType').Value:=Trim(Order_Tree.fieldbyname('CPID').AsString);
      Parameters.ParamByName('Stats').Value:='';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSKBase.TBAddClick(Sender: TObject);
var
  i:Integer;
begin

  if Order_Tree.FieldByName('CPLevel').AsInteger<3 then
  begin
    Panel5.Visible:=False;
    Application.MessageBox('没有选择类型，不能新增!','提示',0);
    Exit;
  end else
  begin
    Panel5.Visible:=True;
    SClearData(Panel5,2);
    BPMF.Text:='160';
    BPCF.Text:='100%P';
    FBPID:='';
    MXCode.SetFocus;
    Button1.Caption:='新增保存';
  end;

end;

procedure TfrmSKBase.TBSaveClick(Sender: TObject);
var
  maxno:string;
begin
  if ClientDataSet1.IsEmpty then Exit;
  Toolbar1.SetFocus;
  if ClientDataSet1.Locate('BPCode',null,[]) then
  begin
    Application.MessageBox('编号不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('BPCode','',[]) then
  begin
    Application.MessageBox('编号不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('BPName',null,[]) then
  begin
    Application.MessageBox('名称不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('BPName','',[]) then
  begin
    Application.MessageBox('名称不能为空！','提示',0);
    Exit;
  end;
  SaveData();
  Application.MessageBox('保存成功！','提示',0);
end;

procedure TfrmSKBase.TBDelClick(Sender: TObject);
begin
  MXCode.SetFocus;
  if ClientDataSet1.IsEmpty then Exit;
  if Trim(ClientDataSet1.FieldByName('BPID').AsString)<>'' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ML_Manage_PB where isnull(BPID,'''')='''+Trim(FBPID)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('已生成面料信息，不能删除数据!','提示',0);
      Exit;
    end;
    if application.MessageBox('确定要删除吗?','提示信息',1)=2 then exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete BP_ZDY where BPID='''+Trim(ClientDataSet1.fieldbyname('BPID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet1.Delete;
  SClearData(Panel5,2);
  BPMF.Text:='160';
  BPCF.Text:='100%P';
  FBPID:='';
end;

procedure TfrmSKBase.TBCloseClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  WriteCxGrid('自定义'+Trim(flag)+'1YX1',TV1,'自定义数据');
  WriteCxGrid('自定义'+Trim(flag)+'2YX1',TV2,'自定义数据');
  Close;
end;

procedure TfrmSKBase.FormShow(Sender: TObject);
begin
  initTree();
  InitGrid();
  ReadCxGrid('自定义'+Trim(flag)+'1YX1',TV1,'自定义数据');
  ReadCxGrid('自定义'+Trim(flag)+'2YX1',TV2,'自定义数据');
  frmSKBase.Caption:=Trim(flagname);
  if ViewFlag=True then
  begin
    TBAdd.Visible:=False;
    TBDel.Visible:=False;
    ToolButton1.Visible:=true;
  end;
end;

procedure TfrmSKBase.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var i:integer;
begin
  if ToolButton1.Visible=true then
  begin
    Fint:='1';
    ModalResult:=1;
  end else
  begin
    ToolBar1.SetFocus;
    if ClientDataSet1.IsEmpty then exit;
    BPMF.Text:=Trim(ClientDataSet1.fieldbyname('BPMF').AsString);
    BPKZ.Text:=Trim(ClientDataSet1.fieldbyname('BPKZ').AsString);
    BPCF.Text:=Trim(ClientDataSet1.fieldbyname('BPCF').AsString);
    BPJS.Text:=Trim(ClientDataSet1.fieldbyname('BPJS').AsString);
    BPMiDu.Text:=Trim(ClientDataSet1.fieldbyname('BPMiDu').AsString);
    CPMiDu.Text:=Trim(ClientDataSet1.fieldbyname('CPMiDu').AsString);
    MXCode.Text:=Trim(ClientDataSet1.FieldByName('MXCode').AsString);
    MXName.Text:=Trim(ClientDataSet1.FieldByName('MXName').AsString);
    FBPID:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
    FCPID:=Trim(ClientDataSet1.fieldbyname('CPID').AsString);
    FCPNo:=Trim(ClientDataSet1.fieldbyname('CPNo').AsString);
    FCPName:=Trim(ClientDataSet1.fieldbyname('CPName').AsString);
    Panel5.Visible:=True;
    Button1.Caption:='修改保存';
    TBDel.Visible:=True;
  end;
end;

procedure TfrmSKBase.BPNamehange(Sender: TObject);
var
  fsj:String;
begin
  if Trim(BPNameM.Text)<>'' then
  begin
    fsj:=' BPName like '''+'%'+Trim(BPNameM.Text)+'%'+'''';
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmSKBase.V1NotePropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPKZ').Value:=mvalue;
    //Post;
  end;
end;

procedure TfrmSKBase.V1HelpTypePropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPWS').Value:=mvalue;
    Post;
  end;
  {with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set HelpType='''+Trim(mvalue)+'''');
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;}
end;

procedure TfrmSKBase.v1note1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPMiDu').Value:=mvalue;
    Post;
  end;
end;

procedure TfrmSKBase.V1BPCodePropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPCode').Value:=mvalue;
  end;
  {with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update BP_ZDY Set BPCode='''+Trim(mvalue)+'''');
    sql.Add(' where BPID='''+Trim(ClientDataSet1.fieldbyname('BPID').AsString)+'''');
    ExecSQL;
  end;}
end;

procedure TfrmSKBase.V1BPNamePropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPName').Value:=mvalue;
  end;
end;

procedure TfrmSKBase.ToolButton4Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if ClientDataSet1.IsEmpty then exit;
  if ClientDataSet1.FieldByName('BPID').AsString='' then
  begin
    application.MessageBox('请保存主信息','提示');
    exit;
  end;
  with ML_GYS do
  begin
    Append;
    post;
  end;
end;

procedure TfrmSKBase.ToolButton5Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if ML_GYS.IsEmpty then Exit;
  if Trim(ML_GYS.FieldByName('BPSubID').AsString)<>'' then
  begin
    if application.MessageBox('确定要删除吗?','提示信息',1)=2 then exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete BP_ZDY_Sub where BPSubID='''+Trim(ML_GYS.fieldbyname('BPSubID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ML_GYS.Delete;
end;

procedure TfrmSKBase.ToolButton3Click(Sender: TObject);
  var maxno:string;
begin
  ToolBar1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ML_GYS.DisableControls;
    with ML_GYS do
    begin
      First;
      while not eof do
      begin
        if Trim(ML_GYS.FieldByName('BPSubID').AsString)='' then
        begin
          if GetLSNo(ADOQueryTemp,maxno,'BS','BP_ZDY_Sub',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            ML_GYS.EnableControls;
            Application.MessageBox('取最大编号失败！','提示',0);
            Exit;
          end;
        end
        else
        begin
          maxno:=Trim(ML_GYS.fieldbyname('BPSubID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BP_ZDY_Sub where BPSubID='''+Trim(maxno)+'''');
          Open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.Append;
        end
        else
        begin
          ADOQueryCmd.edit;
        end;
        ADOQueryCmd.FieldByName('BPSubID').Value:=Trim(maxno);
        ADOQueryCmd.FieldByName('BPCode').Value:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
        ADOQueryCmd.FieldByName('BPID').Value:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
        SSetSaveDataCDSNew(ADOQueryCmd,Tv2,ML_GYS,'BP_ZDY_Sub',1);
        ADOQueryCmd.FieldByName('BPPrice').Value:=ML_GYS.fieldbyname('BPPrice').AsString;
        ADOQueryCmd.FieldByName('BPType').Value:=flagName;
        ADOQueryCmd.FieldByName('BPFlag').Value:=flag;
        ADOQueryCmd.FieldByName('valid').Value:='Y';
        ADOQueryCmd.Post;
        ML_GYS.Edit;
        ML_GYS.FieldByName('BPSubID').Value:=Trim(maxno);
        Next;
      end;
      ML_GYS.EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功！','提示',0);
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmSKBase.BPCodeMChange(Sender: TObject);
var
  fsj:String;
begin
  if Trim(BPCodeM.Text)<>'' then
  begin
    fsj:=' BPCode like '''+'%'+Trim(BPCodeM.Text)+'%'+'''';
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmSKBase.Tv3CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var i:Integer;
begin
  if ML_GX.IsEmpty then exit;
  ML_GX.Delete;
  i:=1;
  ML_GX.DisableControls;
  with ML_GX do
  begin
    first;
    while not eof do
    begin
      Edit;
      FieldByName('BPMiDu').Value:=i;
      i:=i+1;
      next;
    end;
  end;
  ML_GX.EnableControls;
end;

procedure TfrmSKBase.CYNoChange(Sender: TObject);
var
  fsj:String;
begin
  if Trim(CYNo.Text)<>'' then
  begin
    fsj:=' CYNo like '''+'%'+Trim(CYNo.Text)+'%'+'''';
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,ML_GX);
    SInitCDSData20(ADOQueryMain,ML_GX);
  end;
end;

procedure TfrmSKBase.CYIDKeyPress(Sender: TObject; var Key: Char);
var FMXCYID,FColNo,FCYType,CPID:String;
begin
  if Key=#13 then
  begin
    if Trim(CYID.Text)='' then Exit;
    with ADOQuery6 do
    begin
      close;
      sql.Clear;
      sql.Add('select A.CYID MXCYType,A.ColorNo,CYType=isnull((select Top 1 C.CYType from CP_YDang B inner join CP_YDang C on C.CYID=B.CYType where B.CYID=A.CYID)');
      SQL.Add(',(select Top 1 B.CYType from CP_YDang B where B.CYID=A.CYID))');
      sql.Add(',CYID=isnull((select Top 1 C.CYID from CP_YDang B inner join CP_YDang C on C.CYID=B.CYType where B.CYID=A.CYID),');
      sql.Add('(select Top 1 B.CYID from CP_YDang B where B.CYID=A.CYID))');
      sql.Add(' from CP_YDang_Color A where A.CLRID='''+trim(CYID.Text)+'''');
      //ShowMessage(sql.Text);
      Open;
    end;
    with ADOQuery2 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYID='''+Trim(CYID.Text)+'''');
      Open;
    end;
    with ADOQuery3 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYID='''+Trim(ADOQuery2.fieldbyname('CYType').AsString)+'''');
      Open;
    end;
    if ADOQuery6.IsEmpty=false then
    begin
      CPID:=Trim(ADOQuery6.fieldbyname('CYType').AsString);
      FMXCYID:=Trim(ADOQuery6.fieldbyname('CYID').AsString);
      FColNo:=Trim(ADOQuery6.fieldbyname('ColorNo').AsString);
      FCYType:=Trim(ADOQuery6.fieldbyname('MXCYType').AsString);
    end;
    if ADOQuery2.IsEmpty=false then
    begin
      CPID:=Trim(ADOQuery2.fieldbyname('CYType').AsString);
      FMXCYID:=Trim(ADOQuery2.fieldbyname('CYID').AsString);
    end;
    if ADOQuery3.IsEmpty=false then
    begin
      CPID:=Trim(ADOQuery3.fieldbyname('CYType').AsString);
      FMXCYID:=Trim(ADOQuery3.fieldbyname('CYID').AsString);
      FCYType:=Trim(CYID.Text);
    end;
    with ADOQuery2 do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Select_CP_YDang1 :CYType,:CP,:RON,:WSql,:Stats ');
      Parameters.ParamByName('CYType').Value:=CPID;
      Parameters.ParamByName('CP').Value:=1;
      Parameters.ParamByName('RON').Value:=100;
      Parameters.ParamByName('WSql').Value:=' CYID='''+Trim(FMXCYID)+''' ';
      Parameters.ParamByName('Stats').Value:=2;
      Open;
    end;
    SCreateCDS20(ADOQuery2,ML_GX);
    SInitCDSData20(ADOQuery2,ML_GX);
    CYID.Text:='';
  end;
end;

procedure TfrmSKBase.Button1Click(Sender: TObject);
var
  maxno,FCPMiDu:string;
  FInt:Integer;
begin
  if Button1.Caption='修改保存' then
  begin
    if Application.MessageBox('确定要修改保存吗','提示',1)=2 then exit;
  end;
  if MXCode.Text='' then
  begin
    application.MessageBox('代码不能为空','提示');
    exit;
  end;
  if Length(MXCode.Text)>2 then
  begin
    application.MessageBox('代码不能超过2位数','提示');
    exit;
  end;
  if TryStrToInt(MXCode.Text,FInt)=False then
  begin
    Application.MessageBox('代码非法数字!','提示',0);
    Exit;
  end;
  if Trim(Button1.Caption)<>'修改保存' then
  begin
    if Order_Tree.FieldByName('CPLevel').AsInteger=1 then
    begin
      application.MessageBox('不能在主类里新增','提示');
      exit;
    end;
  end;
  if Trim(BPMF.Text)<>'' then
  begin
    if TryStrToInt(BPMF.Text,FInt)=False then
    begin
      Application.MessageBox('门幅非法数字!','提示',0);
      Exit;
    end;
  end;
  if Trim(BPKZ.Text)<>'' then
  begin
    if TryStrToInt(BPKZ.Text,FInt)=False then
    begin
      Application.MessageBox('克重非法数字!','提示',0);
      Exit;
    end;
  end;
  FCPMiDu:=Trim(CPMiDu.Text);
  if Trim(FCPMiDu)<>'' then
  begin
    if Pos('T',FCPMiDu)<>Length(FCPMiDu) then
    begin
      CPMiDu.Text:=CPMiDu.Text+'T';
    end;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if FBPID='' then
    begin
      if GetLSNo(ADOQueryTemp,FBPID,'BP','BP_ZDY',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大编号失败！','提示',0);
        Exit;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BP_ZDY where BPID='''+Trim(FBPID)+'''');
      Open;
    end;
    if ADOQueryCmd.IsEmpty then
    begin
      ADOQueryCmd.Append;
    end
    else
    begin
      ADOQueryCmd.Edit;
    end;
    ADOQueryCmd.FieldByName('BPID').Value:=Trim(FBPID);
    //SSetsaveSqlNew(ADOQueryCmd,'BP_ZDY',panel5,2);
    RTSetsavedata(ADOQueryCmd,'BP_ZDY',panel5,2);
    ADOQueryCmd.FieldByName('BPType').Value:=flagName;
    ADOQueryCmd.FieldByName('BPFlag').Value:=flag;
    ADOQueryCmd.FieldByName('valid').Value:='Y';
    ADOQueryCmd.FieldByName('CPID').Value:=trim(FCPID);
    ADOQueryCmd.FieldByName('CPNo').Value:=trim(FCPNo);
    ADOQueryCmd.FieldByName('CPName').Value:=trim(FCPName);
    ADOQueryCmd.FieldByName('BPCode').Value:=trim(FCPNo)+trim(MXCode.Text);
    ADOQueryCmd.FieldByName('BPName').Value:=trim(MXName.Text);
    ADOQueryCmd.FieldByName('BPCF').Value:=trim(BPCF.Text);
    ADOQueryCmd.Post;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select * from BP_ZDY where BPCode='''+Trim(trim(FCPNo)+trim(MXCode.Text))+'''');
      sql.Add(' and BPFlag='''+trim(flag)+'''');
      Open;
    end;
    if ADOQueryTemp.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('编号重复！','提示',0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select * from BP_ZDY where BPName='''+Trim(trim(FCPName)+trim(MXName.Text))+'''');
      sql.add(' and isnull(BPMF,'''')='''+Trim(BPMF.Text)+'''');
      sql.add(' and isnull(BPKZ,'''')='''+Trim(BPKZ.Text)+'''');
      sql.add(' and isnull(BPJS,'''')='''+Trim(BPJS.Text)+'''');
      sql.add(' and isnull(BPMiDu,'''')='''+Trim(BPMiDu.Text)+'''');
      sql.Add(' and BPFlag='''+trim(flag)+'''');
      Open;
    end;
    if ADOQueryTemp.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('信息重复！','提示',0);
      Exit;
    end;
    with ClientDataSet1 do
    begin
      if Button1.Caption<>'修改保存' then
        Append
      else
        edit;
      FieldByName('BPID').Value:=Trim(FBPID);
      FieldByName('BPCode').Value:=trim(FCPNo)+Trim(MXCode.Text);
      FieldByName('BPName').Value:=Trim(MXName.Text);
      FieldByName('BPMF').Value:=Trim(BPMF.Text);
      FieldByName('BPCF').Value:=Trim(BPCF.Text);
      FieldByName('BPKZ').Value:=Trim(BPKZ.Text);
      FieldByName('BPJS').Value:=Trim(BPJS.Text);
      FieldByName('BPMiDu').Value:=Trim(BPMiDu.Text);
      FieldByName('CPMiDu').Value:=Trim(CPMiDu.Text);
      FieldByName('MXCode').Value:=Trim(MXCode.Text);
      FieldByName('MXName').Value:=Trim(MXName.Text);
      post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate ML_Manage Set MLCF='''+Trim(BPCF.Text)+''',MLJWMD='''+Trim(CPMiDu.Text)+'''');
      SQL.Add(' where SUBSTRING(MLNO,2,LEN(MLNO)-1)='''+Trim(ClientDataSet1.fieldbyname('BPCode').AsString)+'''');
      sql.Add(' and isnull(GYType,'''')=''染色'' ');
      sql.Add(' and isnull(MLType,'''')=''单面料''');
      ExecSQL;
    end;
    //cxGrid1.Enabled:=true;
    SClearData(Panel5,2);
    BPMF.Text:='160';
    BPCF.Text:='100%P';
    FBPID:='';
    Button1.Caption:='新增保存';
    Panel5.Visible:=false;
    TBDel.Visible:=False;
    ADOQueryCmd.Connection.CommitTrans;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
  Application.MessageBox('保存成功','提示');
end;

procedure TfrmSKBase.TPBLBClick(Sender: TObject);
begin
  frmCPTypePB:=TfrmCPTypePB.Create(self);
  with frmCPTypePB do
  begin
    if ShowModal=1 then
    begin

    end;
    free;
  end;
  initTree();
end;

procedure TfrmSKBase.ToolButton1Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  Fint:='1';
  ModalResult:=1;
end;

procedure TfrmSKBase.Button1KeyPress(Sender: TObject; var Key: Char);
var maxno:string;
begin
  if key=#13 then
  begin
    if MXCode.Text='' then
    begin
      application.MessageBox('明细编号不能为空','提示');
      exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      if FBPID='' then
      begin
        if GetLSNo(ADOQueryTemp,FBPID,'BP','BP_ZDY',3,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大编号失败！','提示',0);
          Exit;
        end;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from BP_ZDY where BPID='''+Trim(FBPID)+'''');
        Open;
      end;
      if ADOQueryCmd.IsEmpty then
      begin
        ADOQueryCmd.Append;
      end
      else
      begin
        ADOQueryCmd.Edit;
      end;
      ADOQueryCmd.FieldByName('BPID').Value:=Trim(FBPID);
      SSetsaveSqlNew(ADOQueryCmd,'BP_ZDY',panel5,2);
      ADOQueryCmd.FieldByName('BPType').Value:=flagName;
      ADOQueryCmd.FieldByName('BPFlag').Value:=flag;
      ADOQueryCmd.FieldByName('valid').Value:='Y';
      ADOQueryCmd.FieldByName('CPID').Value:=trim(FCPID);
      ADOQueryCmd.FieldByName('CPNo').Value:=trim(FCPNo);
      ADOQueryCmd.FieldByName('CPName').Value:=trim(FCPName);
      ADOQueryCmd.FieldByName('BPCode').Value:=trim(FCPNo)+trim(MXCode.Text);
      ADOQueryCmd.FieldByName('BPName').Value:=trim(MXName.Text);
      ADOQueryCmd.Post;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.add('select * from BP_ZDY where BPCode='''+Trim(trim(FCPNo)+trim(MXCode.Text))+'''');
        sql.Add(' and BPFlag='''+trim(flag)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        if ADOQueryTemp.RecordCount>1 then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('delete BP_ZDY where BPID='''+Trim(FBPID)+'''');
            ExecSQL;
          end;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('编号重复！','提示',0);
          Exit;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.add('select * from BP_ZDY where BPName='''+Trim(trim(FCPName)+trim(MXName.Text))+'''');
        sql.add(' and isnull(BPMF,'''')='''+Trim(BPMF.Text)+'''');
        sql.add(' and isnull(BPKZ,'''')='''+Trim(BPKZ.Text)+'''');
        sql.add(' and isnull(BPJS,'''')='''+Trim(BPJS.Text)+'''');
        sql.add(' and isnull(BPMiDu,'''')='''+Trim(BPMiDu.Text)+'''');
        sql.Add(' and BPFlag='''+trim(flag)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        if ADOQueryTemp.RecordCount>1 then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('delete BP_ZDY where BPID='''+Trim(FBPID)+'''');
            ExecSQL;
          end;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('名称门幅克重经纱纬纱重复！','提示',0);
          Exit;
        end;
      end;
      with ClientDataSet1 do
      begin
        if Button1.Caption<>'确定修改' then
        Append
        else
        edit;
        FieldByName('BPID').Value:=Trim(FBPID);
        FieldByName('BPCode').Value:=Trim(MXCode.Text);
        FieldByName('BPName').Value:=Trim(MXName.Text);
        FieldByName('BPMF').Value:=Trim(BPMF.Text);
        FieldByName('BPKZ').Value:=Trim(BPKZ.Text);
        FieldByName('BPJS').Value:=Trim(BPJS.Text);
        FieldByName('BPMiDu').Value:=Trim(BPMiDu.Text);
        FieldByName('MXCode').Value:=Trim(MXCode.Text);
        FieldByName('MXName').Value:=Trim(MXName.Text);
        post;
      end;
      MXCode.Text:='';
      MXName.Text:='';
      BPJS.Text:='';
      BPMF.Text:='';
      BPKZ.Text:='';
      BPMiDu.Text:='';
      FBPID:='';
      MXCode.SetFocus;
      ADOQueryCmd.Connection.CommitTrans;
    except;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('保存失败！','提示',0);
    end;
    Application.MessageBox('保存成功','提示');
  end;
end;

procedure TfrmSKBase.cxDBTreeList1Click(Sender: TObject);
begin
  InitGrid();
  TBDel.Visible:=False;
  Panel5.Visible:=False;
  if Order_Tree.FieldByName('CPLevel').AsInteger<3 then
  begin
   // Panel5.Visible:=False;
    TBAdd.Visible:=False;
  end else
  begin
    //Panel5.Visible:=True;
    //SClearData(Panel5,2);
   // BPMF.Text:='160';
    //BPCF.Text:='100%P';
   // Button1.Caption:='新增保存';
    FBPID:='';
    FCPID:=Trim(Order_Tree.fieldbyname('CPID').AsString);
    FCPNo:=Trim(Order_Tree.fieldbyname('CPNo').AsString);
    FCPName:=Trim(Order_Tree.fieldbyname('CPName').AsString);
    TBAdd.Visible:=True;
    //MXCode.SetFocus;
  end;

end;

procedure TfrmSKBase.MXCodeKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    MXName.SetFocus;
  end;
end;

procedure TfrmSKBase.MXNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    BPCF.SetFocus;
  end;
end;

procedure TfrmSKBase.BPJSKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    BPMF.SetFocus;
  end;
end;

procedure TfrmSKBase.BPMFKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    CPMiDu.SetFocus;
  end;
end;

procedure TfrmSKBase.BPKZKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    BPJS.SetFocus;
  end;
end;

procedure TfrmSKBase.BPMiDuKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    BPKZ.SetFocus;
  end;
end;

procedure TfrmSKBase.CYNameKeyPress(Sender: TObject; var Key: Char);
var CPID:string;
begin
  if Key=#13 then
  begin
    if Trim(CYName.Text)='' then Exit;
    with ADOQuery2 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYName like '''+'%'+Trim(CYName.Text)+'%'+'''');
      Open;
    end;
    SCreateCDS20(ADOQuery2,ML_GX);
    SInitCDSData20(ADOQuery2,ML_GX);
  end;
end;

procedure TfrmSKBase.CYNoKeyPress(Sender: TObject; var Key: Char);
var CPID:string;
begin
  if Key=#13 then
  begin
    if Trim(CYNo.Text)='' then Exit;
    with ADOQuery2 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYNo like '''+'%'+Trim(CYNo.Text)+'%'+'''');
      Open;
    end;
    SCreateCDS20(ADOQuery2,ML_GX);
    SInitCDSData20(ADOQuery2,ML_GX);
  end;
end;

procedure TfrmSKBase.BPCFKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    BPMiDu.SetFocus;
  end;
end;

procedure TfrmSKBase.CPMiDuKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    Button1.SetFocus;
  end;
end;

procedure TfrmSKBase.TV1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  Panel5.Visible:=False;
  TBAdd.Visible:=False;
  TBDel.Visible:=False;
end;

end.
