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, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, JPEG, U_SLT;

var
  Mach: array of TfrmSlt;

type
  TfrmCustInput = class(TForm)
    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: TEdit;
    CoNote: TMemo;
    Label1: TLabel;
    CoAbbrName: TEdit;
    CoCode: TEdit;
    Panel1: TPanel;
    Label4: TLabel;
    Label8: TLabel;
    CoAddress: TMemo;
    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;
    cocountry: TBtnEditC;
    Label3: TLabel;
    CoBusinessType: TBtnEditC;
    Label5: TLabel;
    ParentCoName: TBtnEditC;
    Label9: TLabel;
    Source: TBtnEditC;
    ToolButton1: TToolButton;
    COID: TEdit;
    CoLevel: TBtnEditC;
    Label11: TLabel;
    marke: TBtnEditC;
    Label12: TLabel;
    CoreProducts: TBtnEditC;
    Panel2: TPanel;
    GroupBox1: TGroupBox;
    ScrollBox2: TScrollBox;
    adoqueryPicture: TADOQuery;
    Panel4: TPanel;
    ToolBar2: TToolBar;
    ToolButton5: TToolButton;
    ToolButton8: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Label13: TLabel;
    Label14: TLabel;
    CoAccount: TEdit;
    CoOpenBank: TEdit;
    Label15: TLabel;
    CoDPNo: TEdit;
    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 ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cocountryBtnDnClick(Sender: TObject);
    procedure cocountryBtnUpClick(Sender: TObject);
    procedure ParentCoNameBtnUpClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ButtonEdit(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure cxGridDBColumn9PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    procedure InitGrid();
    procedure InitImg();
    function SaveData(): Boolean;
  public
    fkhType: string;
    { Public declarations }
    FCOID: string;
  end;

var
  frmCustInput: TfrmCustInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ZDYHelpSelX, U_YGSel, U_CompanySel,
  U_CustFileUp;

{$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.TxtCode := ADOQueryMain.FieldByName('ParentCoCode').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_JR where COID=' + quotedstr(Trim(FCOID)));
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);

    InitImg();    //容易有图片缓存需要再FromClose中释放图片缓存

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCustInput.InitImg();
var
  i, j: integer;
  jpg: TJpegImage;
  myStream: TADOBlobStream;
begin

  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  if trim(FCOID) = '' then
    exit;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,A.FileName from TP_File A  ');
      sql.add('where  A.WBID=' + quotedstr(trim(FCoID)));
//      if Trim(cbbHX.Text) <> '' then
//        sql.add(' and A.HXName=' + quotedstr(trim(cbbHX.Text)));
//        ShowMessage(SQL.text) ;
      open;
    end;
    j := adoqueryPicture.RecordCount;
    if j < 1 then
      exit;
    adoqueryPicture.DisableControls;
    adoqueryPicture.First;
    SetLength(Mach, j);
    jpg := TJpegImage.Create();
    for i := 0 to j - 1 do
    begin
      if triM(adoqueryPicture.fieldbyname('FilesOther').AsString) <> '' then
      begin
        myStream := tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')), bmread);
        jpg.LoadFromStream(myStream);
        Mach[i] := TfrmSlt.Create(Self);
        Mach[i].Name := trim(adoqueryPicture.fieldbyname('TFID').AsString);
        Mach[i].Parent := ScrollBox2;
        Mach[i].Left := 0 + i * 165;
        Mach[i].Init(adoqueryPicture.fieldbyname('TFID').AsString, adoqueryPicture.fieldbyname('FileName').AsString, jpg);
      end;
      adoqueryPicture.Next;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

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

procedure TfrmCustInput.FormDestroy(Sender: TObject);
begin

  frmCustInput := nil;
end;

procedure TfrmCustInput.cocountryBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmCustInput.cocountryBtnUpClick(Sender: TObject);
var
  fsj, zysc: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try

    frmZDYHelpSelX := TfrmZDYHelpSelX.Create(Application);
    with frmZDYHelpSelX do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
//        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        while frmZDYHelpSelX.ClientDataSet1.Locate('SSel', true, []) do
        begin

          if trim(zysc) = '' then
            zysc := trim(ClientDataSet1.fieldbyname('ZDYName').AsString)
          else
            zysc := zysc + ',' + trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

          TEdit(Sender).Text := zysc;
          frmZDYHelpSelX.ClientDataSet1.delete;
        end;
      end;
    end;
  finally
    frmZDYHelpSelX.Free;
  end;
end;

procedure TfrmCustInput.cxGridDBColumn9PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JRType';
      flagname := '类型';
      if ShowModal = 1 then
      begin
        CDS_1.Edit;
        CDS_1.fieldbyname('JRType').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        CDS_1.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCustInput.ButtonEdit(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'position';
      flagname := '职位';
      if ShowModal = 1 then
      begin
        CDS_LXR.Edit;
        CDS_LXR.fieldbyname('position').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        CDS_LXR.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
//  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCustInput.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i, j: integer;
begin
  //释放图片缓存
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  Action := caFree;
end;

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

procedure TfrmCustInput.FormShow(Sender: TObject);
var
  i: integer;
  a: string;
begin
  ReadCxGrid(trim(self.Caption) + 'TV2', Tv2, '供应商管理');
  ReadCxGrid(trim(self.Caption) + 'TV1', Tv1, '供应商管理');
  InitGrid();
//  InitImg();
  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;
    CoBusinessType.Enabled := False;
    Source.Enabled := False;
    CoName.Enabled := False;
    cocountry.Enabled := False;
    CoAbbrName.Enabled := False;
    ParentCoName.Enabled := False;
  end;
end;

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

function TfrmCustInput.SaveData(): Boolean;
var
  MaxId, MaxSubId, MaxJRId, FCoCode, FCCID, FJRID: 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;

      COID.Text := trim(MaxId);

      if trim(CoCode.Text) = '' 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;
    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('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);
      RTSetsavedata(ADOQueryCmd, 'BS_Company', ScrollBox1, 2);
      FieldByName('CoType').Value := '客户';
      FieldByName('ParentCoCode').Value := ParentCoName.TxtCode;
      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_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 CDS_1.IsEmpty then
    begin
      with CDS_1 do
      begin
        CDS_1.First;
        while not Eof do
        begin
          FJRID := Trim(CDS_1.fieldbyname('JRID').AsString);
          if Trim(FJRID) = '' then
          begin
            if GetLSNo(ADOQueryCmd, MaxJRId, 'JR', 'BS_Company_JR', 4, 1) = False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取号失败!', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            MaxJRId := trim(FJRID);
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from BS_Company_JR where JRID=' + quotedstr(Trim(MaxJRId)));
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FJRID) = '' then
            begin
              Append;
            end
            else
            begin
              Edit;
            end;
            FieldByName('JRID').Value := Trim(MaxJRId);
            FieldByName('COID').Value := Trim(MaxId);
            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'BS_Company_JR', 0);
            Post;
          end;
          with CDS_1 do
          begin
            Edit;
            FieldByName('JRID').Value := Trim(MaxJRId);
            Post;
          end;
          CDS_1.Next;
        end;
      end;
    end;
//////////////            节日提醒              ///////////////////
////////////////         更新名称          ////////////////////////
//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('exec P_Com_Up_Code @COID=' + quotedstr(Trim(MaxId)));
//      ExecSQL;
//    end;
////////////////         更新名称          ////////////////////////
    FCOID := MaxId;
    ADOQueryCmd.Connection.CommitTrans;
    FCCID := Trim(MaxSubId);
    Result := True;
  except
    Result := false;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmCustInput.ToolButton3Click(Sender: TObject);
begin

  if Trim(CoAbbrName.Text) = '' then
  begin
    Application.MessageBox('简称不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(CoBusinessType.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 TfrmCustInput.ToolButton5Click(Sender: TObject);
var
  maxno: string;
begin
  if GetLSNo(ADOQueryCmd, maxno, 'JR', 'BS_Company_JR', 4, 1) = False then
  begin
    Application.MessageBox('取号失败!', '提示', 0);
    Exit;
  end;

  with CDS_1 do
  begin
    Append;
    FieldByName('JRType').Value := '节日提醒';
    fieldbyname('JRID').Value := trim(maxno);
    FieldByName('FillId').Value := trim(DCode);
    fieldbyname('FIller').Value := trim(DName);
    FieldByName('Date').Value := SGetServerDate(ADOQueryTemp);
//      fieldbyname('IsDefault').Value := true;
    Post;
  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 CDS_1.IsEmpty then
    Exit;
  if Trim(CDS_1.fieldbyname('JRID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from BS_Company_JR where JRID=''' + Trim(CDS_1.fieldbyname('JRID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_1.Delete;
//  if CDS_1.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.ToolButton1Click(Sender: TObject);
begin
  if self.FCOID = '' then
  begin
    if Trim(CoAbbrName.Text) = '' then
    begin
      Application.MessageBox('简称不能为空!', '提示', 0);
      Exit;
    end;
    if Trim(CoBusinessType.Text) = '' then
    begin
      Application.MessageBox('客户类型不能为空!', '提示', 0);
      Exit;
    end;
    if Trim(CoName.Text) = '' then
    begin
      Application.MessageBox('名称不能为空!', '提示', 0);
      Exit;
    end;
    if SaveData() then
    begin

    end;
  end;
  try
    frmCustFileUp := TfrmCustFileUp.Create(Application);
    with frmCustFileUp do
    begin
      frmCustFileUp.Code.Text := self.CoCode.Text;
      frmCustFileUp.FBCIID := self.FCOID;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
//        Self.CDS_1.Locate('BCIID', BCIID, []);
      end;
    end;
  finally
    frmCustFileUp.Free;
  end;
//  InitImage();
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.

