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;
    Label5: TLabel;
    Label11: TLabel;
    Label15: TLabel;
    Label22: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label3: TLabel;
    lbl1: TLabel;
    Label12: TLabel;
    Label14: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    CYNo: TEdit;
    CYNote: TMemo;
    CYName: TEdit;
    CYCF: TEdit;
    CYMF: TEdit;
    CYKZ: TEdit;
    CYColor: TEdit;
    CYHX: TEdit;
    CYID: TEdit;
    CYConNo: TEdit;
    CYECF: TEdit;
    CYEName: TEdit;
    CFYR: TEdit;
    MLType: TComboBox;
    LaYangDate: TDateTimePicker;
    ShaZhi: TEdit;
    Label6: TLabel;
    Label8: TLabel;
    XX1: TEdit;
    XXNote1: TEdit;
    Label9: TLabel;
    XX2: TEdit;
    XXNote2: TEdit;
    Label10: TLabel;
    ToolButton2: TToolButton;
    Label13: TLabel;
    CYKCMF: TEdit;
    Label20: TLabel;
    xxnote3: TEdit;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYColorKeyPress(Sender: TObject; var Key: Char);
    procedure CYKZExit(Sender: TObject);
    procedure CYMFExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  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();
  MLType.ItemIndex := MLType.Items.IndexOf(trim(ADOQueryMain.fieldbyname('MLType').AsString));
  if CopyInt = 1 then
  begin
    FCYID := '';
    CYID.text := '';
    LaYangDate.DateTime := SGetServerDateTime(ADOQueryTemp);
    CYNo.text := '';
    CFYR.text := '';
  end;
  if CYNo.text = '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_YP_Get_CPBH  ');
      sql.Add('@CPID=' + quotedstr(Trim(FCPID)));
//      ShowMessage(SQL.Text);
      Open;
    end;
    CYNo.text := Trim(Self.ADOQueryTemp.fieldbyname('NewCYNo').AsString);
  end;

end;

procedure TfrmCPAddPN.TBCloseClick(Sender: TObject);
begin
  Close;
end;

function TfrmCPAddPN.SaveData(): Boolean;
var
  maxId, FCYNo, MAXKHCODE: 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);
        FieldByName('FILLER').Value := Trim(DName);
        FieldByName('FILLTIME').Value := SGetServerDateTime(ADOQueryTemp);
        if ADOQueryCmd.FieldByName('khcpcode2').AsString = '' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            SQL.Clear;
            sql.Add('select maxno= max(dbo.getNum(isnull(KHCPCODE2,''0'')))+1 from  CP_YDANG');
            Open;
          end;
          FieldByName('KHCPCODE2').Value := 'WS' + ADOQueryTemp.fieldbyname('maxno').AsString;

        end;
      end
      else
      begin
        Edit;
        FieldByName('EDITER').Value := Trim(DName);
        FieldByName('EDITTIME').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('CYID').Value := Trim(maxId);
      if trim(CYNo.Text) = '' then
        CYNo.Text := 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 CYName.Text = '' then
  begin
    Application.MessageBox('产品名称不能为空！', '提示', 0);
    Exit;
  end;
  if CFYR.Text = '' then
  begin
    Application.MessageBox('采/放样人不能为空！', '提示', 0);
    Exit;
  end;

  if CYNote.Text = '' then
  begin
    Application.MessageBox('备注1不能为空！', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  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;

procedure TfrmCPAddPN.CYKZExit(Sender: TObject);
begin
  if pos('G/M2', trim(CYKZ.Text)) = 0 then
  begin
    if pos('G/M', trim(CYKZ.Text)) = 0 then
    begin
      CYKZ.Text := trim(CYKZ.Text) + 'G/M2';
    end;
  end;
end;

procedure TfrmCPAddPN.CYMFExit(Sender: TObject);
begin
  if pos('"', trim(CYMF.Text)) = 0 then
  begin
    if pos('CM', trim(CYMF.Text)) = 0 then
      CYMF.Text := trim(CYMF.Text) + 'CM';
  end;

end;

procedure TfrmCPAddPN.FormCreate(Sender: TObject);
begin
  LaYangDate.DateTime := SGetServerDateTime(ADOQueryTemp);
end;

procedure TfrmCPAddPN.ToolButton2Click(Sender: TObject);
begin
  if pos('CM', trim(CYMF.Text)) > 0 then
  begin
    CYMF.Text := StringReplace(trim(CYMF.Text), 'CM', '"', []);
  end
  else
  begin
    CYMF.Text := StringReplace(trim(CYMF.Text), '"', 'CM', []);
  end;
end;

end.

