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, dxSkinWXI, dxScrollbarAnnotations;

type
  TfrmCustomerInput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ToolButton3: TToolButton;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label6: TLabel;
    Label39: TLabel;
    CoName: TEdit;
    CoCode: TEdit;
    CoAddress: TEdit;
    Label7: TLabel;
    CoNote: TMemo;
    CoAbbrName: TEdit;
    Label3: TLabel;
    Label1: TLabel;
    Currency: TComboBox;
    Label4: TLabel;
    CoEName: TEdit;
    Panel1: TPanel;
    ToolBar3: TToolBar;
    ToolButton12: TToolButton;
    ToolButton13: TToolButton;
    ToolButton14: TToolButton;
    cxGrid4: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn10: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    CDS_Bank: TClientDataSet;
    DS_Bank: TDataSource;
    GPM_2: TcxGridPopupMenu;
    ADOQuery1: TADOQuery;
    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);
    procedure ToolButton13Click(Sender: TObject);
    procedure ToolButton14Click(Sender: TObject);
    procedure ToolButton12Click(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, U_CoBankSel;

{$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);
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from BS_Company_Bank where COID=' + quotedstr(Trim(FCOID)));
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_Bank);
    SInitCDSData(ADOQueryMain, CDS_Bank);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCustomerInput.FormDestroy(Sender: TObject);
begin

  frmCustomerInput := nil;
end;

procedure TfrmCustomerInput.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Action := caFree;
end;

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

procedure TfrmCustomerInput.FormShow(Sender: TObject);
begin

  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, MaxSubId1, FCoCode, FCCID, MaxCFID, FCBID: 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;
    /////////////            银行账户              ///////////////////
    if not CDS_Bank.IsEmpty then
    begin
      with CDS_Bank do
      begin
        CDS_Bank.First;
        while not Eof do
        begin
          FCBID := Trim(CDS_Bank.fieldbyname('CBID').AsString);
          if Trim(FCBID) = '' then
          begin
            if GetLSNo(ADOQuery1, MaxSubId1, 'CBS', 'BS_Company_Bank', 4, 1) = False then
            begin
              ADOQuery1.Connection.RollbackTrans;
              Application.MessageBox('银行账户最大ID失败!', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            MaxSubId1 := trim(FCBID);
          end;

          with ADOQuery1 do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from BS_Company_Bank where CBID=' + quotedstr(Trim(MaxSubId1)));
            Open;
          end;
          with ADOQuery1 do
          begin
            if Trim(FCBID) = '' then
            begin
              Append;
            end
            else
            begin
              Edit;
            end;
            FieldByName('CBID').Value := Trim(MaxSubId1);
            FieldByName('COID').Value := Trim(MaxId);
            RTSetSaveDataCDS(ADOQuery1, Tv2, CDS_Bank, 'BS_Company_Bank', 0);
            FieldByName('BKID').Value := CDS_Bank.FieldByName('BKID').ASString;
            Post;
          end;
          with CDS_Bank do
          begin
            Edit;
            FieldByName('CBID').Value := Trim(MaxSubId1);
            Post;
          end;
          CDS_Bank.Next;
        end;
      end;
    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.ToolButton12Click(Sender: TObject);
var
  BankFlag: string;
begin
  BankFlag := trim(CDS_Bank.FieldByName('CBID').AsString);
  if CDS_Bank.Locate('IsDefault', true, []) then
  begin
    CDS_Bank.edit;
    CDS_Bank.FieldByName('IsDefault').value := false;
  end;
  if CDS_Bank.Locate('CBID', BankFlag, []) then
  begin
    CDS_Bank.edit;
    CDS_Bank.FieldByName('IsDefault').value := true;
  end;

end;

procedure TfrmCustomerInput.ToolButton13Click(Sender: TObject);
var
  Maxbno: string;
begin
  if GetLSNo(ADOQueryCmd, Maxbno, 'CB', 'BS_Company_Bank', 4, 1) = False then
  begin
    Application.MessageBox('银行账户最大ID失败!', '提示', 0);
    Exit;
  end;

  try
    frmCoBankSel := TfrmCoBankSel.Create(Application);
    with frmCoBankSel do
    begin
      if ShowModal = 1 then
      begin
        with CDS_Bank do
        begin
//        Currency.Text := Trim(CDS_1.fieldbyname('Currency').AsString);
          Append;
          fieldbyname('CBID').Value := trim(Maxbno);
          fieldbyname('BankName').Value := Trim(CDS_1.fieldbyname('BankName').AsString);
          fieldbyname('BankCardNo').Value := Trim(CDS_1.fieldbyname('BankCardNo').AsString);
          fieldbyname('BankDeposit').Value := Trim(CDS_1.fieldbyname('BankDeposit').AsString);
          fieldbyname('BkID').Value := Trim(CDS_1.fieldbyname('BkID').AsString);
          post;
        end;
      end;
    end;
  finally
    frmCoBankSel.Free;
  end;
//  if CDS_Bank.IsEmpty then
//  begin
//    with CDS_Bank do
//    begin
//      Append;
//      fieldbyname('CBID').Value := trim(Maxbno);
//      fieldbyname('IsDefault').Value := true;
//      Post;
//    end;
//  end
//  else
//  begin
//    with CDS_Bank do
//    begin
//      Append;
//      fieldbyname('CBID').Value := trim(Maxbno);
//      fieldbyname('IsDefault').Value := false;
//      Post;
//    end;
//  end;

end;

procedure TfrmCustomerInput.ToolButton14Click(Sender: TObject);
begin
  if CDS_Bank.IsEmpty then
    Exit;
  if Trim(CDS_Bank.fieldbyname('CBID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from BS_Company_Bank where CBID=''' + Trim(CDS_Bank.fieldbyname('CBID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_Bank.Delete;
  if CDS_Bank.IsEmpty = false then
  begin
    if CDS_Bank.Locate('IsDefault', true, []) = false then
    begin
      CDS_Bank.first;
      CDS_Bank.edit;
      CDS_Bank.FieldByName('IsDefault').value := true;
    end;
  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);
    ModalResult := 1;
  end;
end;

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

end.

