unit U_CustInput;

interface

uses
  Windows, Messages, 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, dxDateRanges, dxBarBuiltInMenu, U_BaseInput, System.ImageList,
  Vcl.ImgList, cxContainer, cxMemo, dxScrollbarAnnotations, dxSkinsCore,
  dxSkinsDefaultPainters, cxImageList, cxMRUEdit, dxSkinOffice2013White,
  dxSkinSharpPlus, dxSkinWXI, dxSkinBasic, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkroom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2016Colorful, dxSkinOffice2016Dark, dxSkinOffice2019Black,
  dxSkinOffice2019Colorful, dxSkinOffice2019DarkGray, dxSkinOffice2019White,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSilver,
  dxSkinSpringtime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinTheBezier, dxSkinValentine, dxSkinVisualStudio2013Blue,
  dxSkinVisualStudio2013Dark, dxSkinVisualStudio2013Light, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue, dxCoreGraphics;

type
  TfrmCustInput = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    CDS_LXR: TClientDataSet;
    DSLXR: TDataSource;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    CoName: TcxTextEdit;
    Label1: TLabel;
    CoAbbrName: TcxTextEdit;
    CoCode: TcxTextEdit;
    Panel1: TPanel;
    Label4: TLabel;
    Label8: TLabel;
    Panel3: TPanel;
    ToolBar3: TToolBar;
    ToolButton2: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Label10: TLabel;
    TV2Column2: TcxGridDBColumn;
    Label3: TLabel;
    Label5: TLabel;
    CoBusinessType: TcxButtonEdit;
    cocountry: TcxButtonEdit;
    CoLevel: TcxComboBox;
    ParentCoName: TcxButtonEdit;
    ToolButton1: TToolButton;
    CoNote: TcxMemo;
    Label9: TLabel;
    Continent: TcxButtonEdit;
    Coarea: TcxButtonEdit;
    Label11: TLabel;
    Label12: TLabel;
    PayMentMonth: TcxComboBox;
    Saleser: TcxButtonEdit;
    lbl2: TLabel;
    SalesId: TcxTextEdit;
    lblSalesId: TLabel;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton4: TToolButton;
    ToolButton8: TToolButton;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    TV2Column1: TcxGridDBColumn;
    Label13: TLabel;
    AMOUNTOWED: TcxTextEdit;
    area: TcxMRUEdit;
    TV2Column3: TcxGridDBColumn;
    Label14: TLabel;
    Label15: TLabel;
    MoreFlag: TcxComboBox;
    SealFlag: TcxComboBox;
    Label16: TLabel;
    Stores: TcxComboBox;
    Label17: TLabel;
    IncludeTax: TcxTextEdit;
    Label18: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure BuyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CoBusinessTypeDblClick(Sender: TObject);
    procedure SaleserPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SaleserButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure CoareaKeyPress(Sender: TObject; var Key: Char);
    procedure SaleserDblClick(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure areaPropertiesButtonClick(Sender: TObject);
    procedure TV2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    fkhType: string;
    { Public declarations }
    FCOID: string;
  end;

var
  frmCustInput: TfrmCustInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_YGSel, U_CompanySel, U_UserSel;
{$R *.dfm}

procedure TfrmCustInput.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);
    ParentCoName.Properties.LookupItems.Text := ADOQueryMain.FieldByName('ParentCoCode').AsString;
    area.Text := ADOQueryMain.FieldByName('area').AsString;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from BS_Company_contact where COID=' + quotedstr(Trim(FCOID)));
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_LXR);
    SInitCDSData(ADOQueryMain, CDS_LXR);
    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, ClientDataSet1);
    SInitCDSData(ADOQueryMain, ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCustInput.FormDestroy(Sender: TObject);
begin
  inherited;
  frmCustInput := nil;
end;

procedure TfrmCustInput.areaPropertiesButtonClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := 'Cust';
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCustInput.BuyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := 'Cust';
      fnote := True;
      V1Note.caption := '区域代号';
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCustInput.CoareaKeyPress(Sender: TObject; var Key: Char);
begin
  if ((ord(Key) > 128) and (Key <> #8)) then
    Key := #0;
end;

procedure TfrmCustInput.CoBusinessTypeDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmCustInput.SaleserPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        self.ParentCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        self.ParentCoName.Properties.LookupItems.Text := Trim(CDS_1.fieldbyname('CoCode').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmCustInput.SaleserButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
      Fdept := '业务部';
      if ShowModal = 1 then
      begin

        self.SalesId.Text := Trim(CDS_1.fieldbyname('EECode').AsString);
        self.Saleser.Text := Trim(CDS_1.fieldbyname('EEName').AsString);

      end;
    end;
  finally
    frmUserSel.Free;
  end;
end;

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

procedure TfrmCustInput.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'TV2', Tv2, '供应商管理');
  close;
end;

procedure TfrmCustInput.FormShow(Sender: TObject);
var
  i: integer;
  a: string;
begin
  inherited;
  ReadCxGrid(trim(self.caption) + 'Tv2', Tv2, '客户管理');
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where type=''area''');

    Open;
    if isEmpty then
    begin
      exit;
    end;
    area.Properties.LookupItems.Clear;
    while not Eof do
    begin

      area.Properties.LookupItems.Add(Trim(fieldByName('zdyName').AsString));
      Next;
    end;

    area.ItemIndex := -1;
  end;
  InitGrid();
//  if trim(FCOID) = '' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add(' exec P_BS_Com_Get_No @Str=''K'' ');
//      Open;
//    end;
//    CoCode.Text := trim(ADOQueryTemp.FieldByName('NewCoCode').asstring);
//  end;
   //已审核的也要能修改保存，不能修改主表内容，只能改下面两部分


  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from BS_Company where CoId=' + quotedstr(FCoId));
    Open;
  end;

  if trim(ADOQueryTemp.FieldByName('Status').asstring) = '1' then
  begin
    ScrollBox1.Enabled := False;
  end;

end;

procedure TfrmCustInput.SaleserDblClick(Sender: TObject);
begin
  self.SalesId.Text := '';
  self.Saleser.Text := '';
end;

function TfrmCustInput.SaveData(): Boolean;
var
  MaxId, MaxSubId, FCoCode, FCCID: 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=''K'' ');
          sql.Add(' exec P_BS_Com_Get_CustNo @Str=' + Quotedstr(Coarea.Text));
          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('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('Saleser').Value := trim(DName);
        FieldByName('SalesId').Value := trim(DCode);
        FieldByName('Tallyer').Value := trim(DName);
        FieldByName('TallyId').Value := trim(DCode);
        FieldByName('status').Value := '0';
      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('COID').Value := Trim(MaxId);
      FieldByName('area').Value := Trim(area.Text);
      RTSetsavedata(ADOQueryCmd, 'BS_Company', ScrollBox1, 2);
      FieldByName('CoType').Value := '客户';
      FieldByName('ParentCoCode').Value := ParentCoName.Properties.LookupItems.Text;
      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;

//////////////            联系人              ///////////////////
    if not CDS_LXR.IsEmpty then
    begin
      with CDS_LXR do
      begin
        CDS_LXR.First;
        while not Eof do
        begin
          FCCID := Trim(CDS_LXR.fieldbyname('CCID').AsString);
          if Trim(FCCID) = '' then
          begin
            if GetLSNo(ADOQueryCmd, MaxSubId, 'CCS', 'BS_Company_contact', 4, 1) = False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取系人最大ID失败!', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            MaxSubId := trim(FCCID);
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from BS_Company_contact where CCID=' + quotedstr(Trim(MaxSubId)));
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FCCID) = '' then
            begin
              Append;
            end
            else
            begin
              Edit;
            end;
            FieldByName('CCID').Value := Trim(MaxSubId);
            FieldByName('COID').Value := Trim(MaxId);
            RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_LXR, 'BS_Company_contact', 0);
            Post;
          end;
          with CDS_LXR do
          begin
            Edit;
            FieldByName('CCID').Value := Trim(MaxSubId);
            Post;
          end;
          CDS_LXR.Next;
        end;
      end;
    end;
//////////////            联系人              ///////////////////
///
    if not ClientDataSet1.IsEmpty then
    begin
      with ClientDataSet1 do
      begin
        ClientDataSet1.First;
        while not Eof do
        begin
          FCCID := Trim(ClientDataSet1.fieldbyname('CoBID').AsString);
          if Trim(FCCID) = '' then
          begin
            if GetLSNo(ADOQueryCmd, MaxSubId, 'CoB', 'BS_Company_Bank', 4, 1) = False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取系人最大ID失败!', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            MaxSubId := trim(FCCID);
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from BS_Company_Bank where CoBID=' + quotedstr(Trim(MaxSubId)));
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FCCID) = '' then
            begin
              Append;
            end
            else
            begin
              Edit;
            end;
            FieldByName('CoBID').Value := Trim(MaxSubId);
            FieldByName('COID').Value := Trim(MaxId);
            RTSetSaveDataCDS(ADOQueryCmd, TV1, ClientDataSet1, 'BS_Company_Bank', 0);
            Post;
          end;
          with ClientDataSet1 do
          begin
            Edit;
            FieldByName('CoBID').Value := Trim(MaxSubId);
            Post;
          end;
          ClientDataSet1.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(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

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

  end;

  if Trim(Saleser.Text) = '' then
  begin
    Application.MessageBox('业务员不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(Stores.Text) = '' then
  begin
    Application.MessageBox('门店不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_LXR.IsEmpty then
  begin
    Application.MessageBox('明细数据不能为空!', '提示', 0);
    Exit;
  end;

  with CDS_LXR do
  begin
    DisableControls;
    while not Eof do
    begin
      if Trim(FieldByName('Contacts').AsString) = '' then
      begin
        Application.MessageBox('联系人不能为空!', '提示', 0);
        EnableControls;
        Exit;
      end;
      if Trim(FieldByName('PhoneNumber').AsString) = '' then
      begin
        Application.MessageBox('手机号不能为空!', '提示', 0);
        EnableControls;
        Exit;
      end;
//      if Trim(FieldByName('Telephone').AsString) = '' then
//      begin
//        Application.MessageBox('联系电话不能为空!', '提示', 0);
//        EnableControls;
//        Exit;
//      end;
      if Trim(FieldByName('mxaddress').AsString) = '' then
      begin
        Application.MessageBox('地址不能为空!', '提示', 0);
        EnableControls;
        Exit;
      end;
      if Trim(FieldByName('mxarea').AsString) = '' then
      begin
        Application.MessageBox('区域不能为空!', '提示', 0);
        EnableControls;
        Exit;
      end;
      Next;
    end;
    EnableControls;
  end;

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

procedure TfrmCustInput.ToolButton4Click(Sender: TObject);
var
  maxno: string;
begin
  if GetLSNo(ADOQueryCmd, maxno, 'CD', 'BS_Company_Bank', 4, 1) = False then
  begin
    Application.MessageBox('账号最大ID失败!', '提示', 0);
    Exit;
  end;
  if ClientDataSet1.IsEmpty then
  begin
    with ClientDataSet1 do
    begin
      Append;
      fieldbyname('CoBID').Value := trim(maxno);
      Post;
    end;
  end
  else
  begin
    with ClientDataSet1 do
    begin
      Append;
      fieldbyname('CoBID').Value := trim(maxno);
      Post;
    end;
  end;

end;

procedure TfrmCustInput.ToolButton6Click(Sender: TObject);
var
  maxno: string;
begin
  if GetLSNo(ADOQueryCmd, maxno, 'CC', 'BS_Company_contact', 4, 1) = False then
  begin
    Application.MessageBox('取系人最大ID失败!', '提示', 0);
    Exit;
  end;
  if CDS_LXR.IsEmpty then
  begin
    with CDS_LXR do
    begin
      Append;
      fieldbyname('CCID').Value := trim(maxno);
      fieldbyname('IsDefault').Value := true;
      Post;
    end;
  end
  else
  begin
    with CDS_LXR do
    begin
      Append;
      fieldbyname('CCID').Value := trim(maxno);
      fieldbyname('IsDefault').Value := false;
      Post;
    end;
  end;
end;

procedure TfrmCustInput.ToolButton7Click(Sender: TObject);
begin
  if CDS_LXR.IsEmpty then
    Exit;
  if Trim(CDS_LXR.fieldbyname('CCID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from BS_Company_contact where CCID=''' + Trim(CDS_LXR.fieldbyname('CCID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_LXR.Delete;
  if CDS_LXR.IsEmpty = false then
  begin
    if CDS_LXR.Locate('IsDefault', true, []) = false then
    begin
      CDS_LXR.first;
      CDS_LXR.edit;
      CDS_LXR.FieldByName('IsDefault').value := true;
    end;
  end;
end;

procedure TfrmCustInput.ToolButton8Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then
    Exit;
  if Trim(ClientDataSet1.fieldbyname('CoBID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from BS_Company_Bank where CoBID=''' + Trim(ClientDataSet1.fieldbyname('CoBID').AsString) + '''');
      ExecSQL;
    end;
  end;
  ClientDataSet1.Delete;
end;

procedure TfrmCustInput.TV2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := 'Cust';
      flag := 'AREA';
      flagname := '区域';
      fnote := true;
      V1Note.Caption := '区域代码';
      if ShowModal = 1 then
      begin
        with CDS_LXR do
        begin
          Edit;
          FieldByName('mxAREA').AsString := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCustInput.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.caption) + 'Tv2', Tv2, '客户管理');
end;

procedure TfrmCustInput.ToolButton2Click(Sender: TObject);
var
  DwFlag: string;
begin
  DwFlag := trim(CDS_LXR.FieldByName('CCID').AsString);
  if CDS_LXR.Locate('IsDefault', true, []) then
  begin
    CDS_LXR.edit;
    CDS_LXR.FieldByName('IsDefault').value := false;
  end;
  if CDS_LXR.Locate('CCID', DwFlag, []) then
  begin
    CDS_LXR.edit;
    CDS_LXR.FieldByName('IsDefault').value := true;
  end;
end;

end.

