unit U_CustomerInput;

interface

uses
  Windows, Messages, StrUtils, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB,
  ADODB, cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, BtnEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, cxContainer, dxCore, cxDateUtils, cxImage,
  cxDBEdit, IdExplicitTLSClientServerBase, cxMemo, U_BaseInput, System.ImageList,
  Vcl.ImgList;

type
  TfrmCustomerInput = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ToolButton3: TToolButton;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label6: TLabel;
    Label39: TLabel;
    CoName: TcxTextEdit;
    CoCode: TcxTextEdit;
    CoAddress: TcxTextEdit;
    Label7: TLabel;
    CoAbbrName: TcxTextEdit;
    Label3: TLabel;
    CoNote: TcxMemo;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CoareaBtnDnClick(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    fFlileFlag: string;
    fkhType: string;
    { Public declarations }
    FCOID: string;
  end;

var
  frmCustomerInput: TfrmCustomerInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, getpic, U_YWYSel;

{$R *.dfm}

procedure TfrmCustomerInput.InitGrid();
begin
  try

    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from BS_Company where  CoType=''我司'' and COID=''' + Trim(FCOID) + '''');
      Open;
    end;
    SCSHData(ADOQueryMain, ScrollBox1, 2);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCustomerInput.FormDestroy(Sender: TObject);
begin
  inherited;
  frmCustomerInput := nil;
end;

procedure TfrmCustomerInput.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmCustomerInput.TBCloseClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmCustomerInput.FormShow(Sender: TObject);
begin
  inherited;
  InitGrid();

  if CoCode.Text <> '' then
    exit;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' exec P_BS_Com_Get_No @Str=''W'' ');
    Open;
  end;
  CoCode.Text := trim(ADOQueryTemp.FieldByName('NewCoCode').asstring);

end;

procedure TfrmCustomerInput.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

function TfrmCustomerInput.SaveData(): Boolean;
var
  MaxId, MaxSubId, FCoCode, FCCID, MaxCFID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCOID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, MaxId, 'CO', 'BS_Company', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

      if trim(CoCode.Text) = '' then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add(' exec P_BS_Com_Get_No @Str=''W'' ');
          Open;
        end;
        CoCode.Text := trim(ADOQueryTemp.FieldByName('NewCoCode').asstring);
      end;
    end
    else
    begin
      MaxId := Trim(FCOID);
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BS_Company where COID=''' + Trim(FCOID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCOID) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('COID').Value := Trim(MaxId);
      RTSetsavedata(ADOQueryCmd, 'BS_Company', ScrollBox1, 2);
      FieldByName('CoType').Value := '我司';
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Company where CoCode=' + quotedstr(trim(CoCode.Text)));

      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('编号重复!', '提示', 0);
      Exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Company where CoName=' + quotedstr(trim(CoName.Text)));
      sql.Add(' and CoType=''我司''    ');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('名称重复!', '提示', 0);
      Exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Company where CoAbbrName=' + quotedstr(trim(CoAbbrName.Text)));
      sql.Add(' and CoType=''我司''    ');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('简称重复!', '提示', 0);
      Exit;
    end;
////////////////         更新名称          ////////////////////////
//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('exec P_Com_Up_Code @COID=' + quotedstr(Trim(MaxId)));
//      ExecSQL;
//    end;
////////////////         更新名称          ////////////////////////
    ADOQueryCmd.Connection.CommitTrans;
    FCCID := Trim(MaxSubId);
    Result := True;
  except
    Result := false;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmCustomerInput.ToolButton3Click(Sender: TObject);
begin
  if Trim(CoAbbrName.Text) = '' then
  begin
    Application.MessageBox('简称不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(CoName.Text) = '' then
  begin
    Application.MessageBox('名称不能为空!', '提示', 0);
    Exit;
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
  end;
end;

procedure TfrmCustomerInput.CoareaBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

end.

