unit U_CPAddPN;

interface

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

type
  TfrmCPAddPN = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label7: TLabel;
    CYNo: TEdit;
    CYNote: TMemo;
    Label5: TLabel;
    CYName: TEdit;
    Label11: TLabel;
    CYCF: TEdit;
    Label15: TLabel;
    CYMF: TEdit;
    Label22: TLabel;
    CYKZ: TEdit;
    Label23: TLabel;
    CYSpec: TEdit;
    Label2: TLabel;
    CYColor: TEdit;
    Label4: TLabel;
    CYHX: TEdit;
    Label10: TLabel;
    CYPrice: TEdit;
    ToolButton2: TToolButton;
    CYID: TEdit;
    Label3: TLabel;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CYColorKeyPress(Sender: TObject; var Key: Char);
  private
    canshu1:string;
    Fint:Integer;
    procedure InitSubGrid();
    function  SaveData():Boolean;
    { Private declarations }
  public
    FCYID,FCYCode,FCPID,FCPNO,FCPName:String;
    CopyInt:Integer;

    { Public declarations }
  end;

var
  frmCPAddPN: TfrmCPAddPN;

implementation
uses
  U_DataLink,U_Fun,U_iniParam,U_ZDYHelp, U_ZdyAttachGYS,U_FileUp;

{$R *.dfm}

procedure TfrmCPAddPN.InitSubGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from CP_YDang where CYID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCSHDataNew(ADOQueryMain,ScrollBox1,2);
    SCSHDataNew(ADOQueryMain,ScrollBox1,0);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPAddPN.FormShow(Sender: TObject);
begin
  InitSubGrid();
  if CopyInt=1 then
  begin
    FCYID:='';
    CYID.text:='';
  end;
end;

procedure TfrmCPAddPN.TBCloseClick(Sender: TObject);
begin
  Close;
end;
function TfrmCPAddPN.SaveData():Boolean;
var
  maxId,FCYNo:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCYID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxId,'Y','CP_YDang',4,1)=False then
      begin
        Result:=False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('区最大号失败！','提示',0);
        Exit;
      end;
      CYID.Text:=trim(maxId);
    end
    else
    begin
      maxId:=Trim(FCYID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from CP_YDang where CYID='''+Trim(FCYID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCYID)='' then
      begin
        Append;
        FieldByName('CYType').Value:=Trim(FCPID);
      end
      else
      begin
        Edit;
      end;
      FieldByName('CYID').Value:=Trim(maxId);
      SSetsaveSqlNew(ADOQueryCmd,'CP_YDang',ScrollBox1,2);
      Post;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from CP_YDang where CYNo='''+Trim(CYNo.Text)+'''');
      Open;
      if FieldByName('AA').AsInteger>1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result:=False;
        Application.MessageBox('编号重复！','提示',0);
        Exit;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    FCYID:=maxId;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmCPAddPN.ToolButton1Click(Sender: TObject);
begin
  if CYNo.Text='' then
  begin
    Application.MessageBox('产品编号不能为空！','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

procedure TfrmCPAddPN.ToolButton2Click(Sender: TObject);
var
  maxId:string;
begin
//  if CYNo.Text='' then
//  begin
//    Application.MessageBox('产品编号不能为空！','提示',0);
//    Exit;
//  end
//  else
//  begin
//    CYNo.Enabled:=false;
//  end;
  if trim(CYID.Text)='' then
  begin
    if GetLSNo(ADOQueryCmd,maxId,'Y','CP_YDang',4,1)=False then
    begin
      Application.MessageBox('获取样品ID失败！','提示',0);
      Exit;
    end;
    CYID.Text:=maxId;
  end;
  try
    frmFileUp:=TfrmFileUp.Create(Application);
    with frmFileUp do
    begin
      Code.Text:=Trim(self.CYNo.Text);
      CYID:=trim(self.CYID.Text);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmFileUp.Free;
  end;
end;

procedure TfrmCPAddPN.CYColorKeyPress(Sender: TObject; var Key: Char);
var c: Integer;
begin
//  if key='.' then
//  begin
//    for c:=1 to length(TEdit(Sender).text) do
//    begin
//      if Tedit(Sender).text[c]='.' then key:=#0;
//    end;
//  end
//  else
//    if key=#13 then PerForm(WM_NEXTDLGCTL,0,0)
//    else
//        if key=#8 then key:=#8
//        else
//          if (key<'0') or (key>'9') then
//            key:=#0;
end;

end.
