unit U_CPAdd;

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;

type
  TfrmCPAdd = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column15: TcxGridDBColumn;
    v1CYNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column37: TcxGridDBColumn;
    v1Column38: TcxGridDBColumn;
    v1Column39: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1Column15PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    canshu1:string;
    Fint:Integer;
    procedure InitSubGrid();
    function  SaveData():Boolean;
    { Private declarations }
  public
    FCYID,FCYCode,FCPID,FCPNO:String;
    CopyInt:Integer;

    { Public declarations }
  end;

var
  frmCPAdd: TfrmCPAdd;

implementation
uses
  U_DataLink,U_Fun,U_iniParam,U_ZDYHelp;

{$R *.dfm}

procedure TfrmCPAdd.InitSubGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from CP_YDang where CYID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPAdd.FormShow(Sender: TObject);
begin
  ReadCxGrid('样品编辑YT1',Tv1,'样品管理');
  InitSubGrid();
  if CopyInt=1 then
  begin
    FCYID:='';
  end;
  begin
    if CDS_Sub.IsEmpty then
    begin
      CDS_Sub.Append;
      CDS_Sub.Post;
    end;
  end;
end;

procedure TfrmCPAdd.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('样品编辑YT1',Tv1,'样品管理');
  Close;
end;
function TfrmCPAdd.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;
    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;
      end else
      begin
        Edit;
      end;
      FieldByName('CYID').Value:=Trim(maxId);
      FieldByName('CYType').Value:=Trim(FCPID);
      SSetSaveDataCDSNew(ADOQueryCmd,Tv1,CDS_Sub,'CP_YDang',3);
      Post;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from CP_YDang where CYNo='''+Trim(CDS_Sub.fieldbyname('CYNo').AsString)+'''');
      Open;
      if FieldByName('AA').AsInteger>1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result:=False;
        Application.MessageBox('编号重复！','提示',0);
        Exit;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    with CDS_Sub do
    begin
      Edit;
      FieldByName('CYID').Value:=Trim(maxId);
      FieldByName('CYType').Value:=Trim(FCPID);
      Post;
    end;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmCPAdd.ToolButton1Click(Sender: TObject);
begin
  if CDS_Sub.Locate('CYNo',null,[])=True then
  begin
    Application.MessageBox('产品编号不能为空！','提示',0);
    Exit;
  end;
  ToolBar1.SetFocus;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

procedure TfrmCPAdd.v1Column15PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='DefStr2';
      flagname:='库位放置';
      if ShowModal=1 then
      begin
        with CDS_Sub do
        begin
          Edit;
          FieldByName('DefStr2').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
