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;
    Label2: TLabel;
    CYColor: TEdit;
    Label4: TLabel;
    CYHX: TEdit;
    Label10: TLabel;
    CYPrice: TEdit;
    CYID: TEdit;
    Label3: TLabel;
    Label6: TLabel;
    ZhenXing: TEdit;
    Label8: TLabel;
    ShaChang: TEdit;
    Label9: TLabel;
    CarType: TEdit;
    CYSpec: TMemo;
    Label13: TLabel;
    CYCL: TEdit;
    lbl1: TLabel;
    lbl2: TLabel;
    CYConNo: TEdit;
    lbl3: TLabel;
    BaoHuDate: TDateTimePicker;
    CYCNHS: TMemo;
    Label12: TLabel;
    CPType: TComboBox;
    Label14: TLabel;
    YWY: TBtnEditA;
    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 YWYBtnClick(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;
    if ADOQueryMain.IsEmpty then
    begin
      BaoHuDate.DateTime := SGetServerDateTime(ADOQueryTemp);
      BaoHuDate.Checked := False;
    end
    else
    begin
      if Trim(ADOQueryMain.FieldByName('BaoHuDate').AsString) = '' then
      begin
        BaoHuDate.DateTime := SGetServerDateTime(ADOQueryTemp);
        BaoHuDate.Checked := False;
      end;
    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 := '';
    BaoHuDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  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);
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
      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 Trim(CPType.Text)='' then
  begin
    Application.MessageBox('类别不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(YWY.Text)='' then
  begin
    Application.MessageBox('业务员不能为空！', '提示', 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('GSM', trim(CYKZ.Text)) = 0 then
    CYKZ.Text := trim(CYKZ.Text) + 'GSM';
end;

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

procedure TfrmCPAddPN.YWYBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YWY';
      flagname:='业务员';
      if ShowModal=1 then
      begin
        Self.YWY.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('zdyname').AsString)
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

