unit U_Customer;

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, U_SLT,
  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, cxDBLookupComboBox, ComObj, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, U_BaseList, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  dxBarBuiltInMenu, cxPC, jpeg, dxSkinWXI, dxScrollbarAnnotations;

var
  Mach: array of TfrmSlt;

type
  TfrmCustomer = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    btnFind: TToolButton;
    ADOQueryMain: TADOQuery;
    Label3: TLabel;
    CoName: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    btnAdd: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    Label1: TLabel;
    CoCode: TEdit;
    v2Column10: TcxGridDBColumn;
    btnEdit: TToolButton;
    Label2: TLabel;
    Saleser: TEdit;
    CoCountry: TEdit;
    Label4: TLabel;
    v2Column15: TcxGridDBColumn;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    btnSaleser: TToolButton;
    v2Column3: TcxGridDBColumn;
    OpenDialog1: TOpenDialog;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Panel2: TPanel;
    cxTabControl1: TcxTabControl;
    btnChk: TToolButton;
    btnReChk: TToolButton;
    btnTallyer: TToolButton;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    ToolButton1: TToolButton;
    GPM_2: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    ToolButton2: TToolButton;
    cxGrid3: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Tv1Column9: TcxGridDBColumn;
    adoqueryPicture: TADOQuery;
    ToolButton4: TToolButton;
    Tv1Column10: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    cxSplitter1: TcxSplitter;
    cxGrid2: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    GPM3: TcxGridPopupMenu;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    ADOQuery3: TADOQuery;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    TV3Column3: TcxGridDBColumn;
    TV3Column4: TcxGridDBColumn;
    TV3Column5: TcxGridDBColumn;
    TV3Column6: TcxGridDBColumn;
    TV3Column7: TcxGridDBColumn;
    TV3Column8: TcxGridDBColumn;
    TV3Column9: TcxGridDBColumn;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    TV3Column10: TcxGridDBColumn;
    TV3Column11: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    ADOQuery2: TADOQuery;
    Label5: TLabel;
    CoAbbrName: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure btnFindClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure TBuserClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure btnSaleserClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CoCodeKeyPress(Sender: TObject; var Key: Char);
    procedure btnChkClick(Sender: TObject);
    procedure btnReChkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure btnTallyerClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CoCodeChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
  private
    fFlileFlag: string;
    { Private declarations }
    procedure InitGrid();
    procedure InitImage();
    procedure SetStatus();
  public
    FAuthority: string;
  end;

var
  frmCustomer: TfrmCustomer;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CustomerInput, U_CustInput, U_UserSel,
  U_AttachmentUpload, U_CustFileUp, U_LabelPrint, U_CustInRC;

{$R *.dfm}
procedure TfrmCustomer.SetStatus();
begin
  btnSaleser.Enabled := false;     //指定
  btnTallyer.Enabled := false;     //指定
  btnChk.Enabled := false;     //审核
  btnReChk.Enabled := false;     //撤销

  btnAdd.Enabled := false;     //新增
  btnEdit.Enabled := false;     //修改
  TBDel.Enabled := false;           //删除

  if Trim(FAuthority) = '管理' then
  begin
    btnSaleser.Enabled := true;
    btnTallyer.Enabled := true;
    btnAdd.Enabled := true;

    case cxTabControl1.TabIndex of
      0:
        begin
          btnEdit.Enabled := true;
          TBDel.Enabled := true;
          btnChk.Enabled := true;
        end;
      1:
        begin
          btnEdit.Enabled := true;
          btnReChk.Enabled := true;
        end;
      2:
        begin

        end;
    end;
  end
  else
  begin
    btnAdd.Enabled := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          btnEdit.Enabled := true;
          TBDel.Enabled := true;
        end;
      1:
        begin

        end;
      2:
        begin

        end;
    end;
  end;
end;

procedure TfrmCustomer.InitGrid();
var
  WSql, FCoCode: string;
begin
  WSql := SGetFilters(Panel1, 1, 2);
  if trim(WSql) <> '' then
  begin
    WSql := ' and ' + trim(WSql);
  end;
  if not CDS_1.IsEmpty then
    FCoCode := CDS_1.FieldByName('CoCode').AsString;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('select IsFJ=cast((case when (select COUNT(FF.WBID) from FJ_File FF');
      sql.Add('where FF.TFType = ''客户'' and FF.WBID=A.CoID)=0 then 0 else 1 end) as bit )   ');
      sql.Add(',IsImg=cast((case when (select COUNT(TF.WBID) from TP_File TF');
      sql.Add('where TF.FileType = ''KH'' and TF.WBID=A.CoID)=0 then 0 else 1 end) as bit )   ');
      sql.Add(',Contacts=(select Contacts from Bs_Company_contact BC where BC.CoID=A.CoId and BC.IsDefault=1)');
      sql.Add(',* from BS_Company A where 1=1 ' + (WSql));
      sql.Add('and A.CoType=''客户'' and A.Valid=''Y'' ');

      if Trim(FAuthority) = '录入' then
      begin
        sql.Add(' and ( A.SalesId=' + quotedstr(trim(DCode)) + ' or exists (select * from [dbo].[F_Tool_SplitString](TallyId,'','') X where X.RTValue =' + quotedstr(trim(DCode)) + '))');
      end;

      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and  A.status=''0''');
          end;
        1:
          begin
            sql.Add(' and  A.status=''1''');
          end;
      end;
      SQL.Add(' ORDER BY A.CoCode ');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
    CDS_1.Locate('CoCode', FCoCode, []);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCustomer.InitImage();
//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 CDS_1.IsEmpty 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(CDS_1.fieldbyname('CoID').AsString)));
////      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 := ScrollBox1;
//        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 TfrmCustomer.FormDestroy(Sender: TObject);
begin
  inherited;
  frmCustomer := nil;
end;

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

procedure TfrmCustomer.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmCustomer.TBDelClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select Count(MainId) S from Trade_Plan_Main where CustNo=' + quotedstr(trim(CDS_1.fieldbyname('CoCode').AsString)));
    open;
  end;
  with ADOQuery2 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select COUNT(ConMid) S from Bs_Contract_Main where BuyNo=' + quotedstr(trim(CDS_1.fieldbyname('CoCode').AsString)));
    open;
  end;
  if ADOQuery1.FieldByName('S').AsInteger >0 then
  begin
     Application.MessageBox('有计划单使用该客户不能删除！', '提示', 0);
     exit
  end;
  if ADOQuery2.FieldByName('S').AsInteger >0 then
  begin
     Application.MessageBox('有销售合同使用该客户不能删除！', '提示', 0);
     exit
  end;
//  if CDS_1.Locate('ssel', true, []) = false then
//  begin
//    Application.MessageBox('没有选择数据!', '提示', 0);
//    Exit;
//  end;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

//  while CDS_1.Locate('SSel', true, []) do
//  begin
//    if Trim(CDS_1.fieldbyname('COID').AsString) <> '' then
//    begin
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_BS_Com_Del @COID=' + quotedstr(trim(CDS_1.fieldbyname('COID').AsString)));
    sql.Add(',@DCode=' + quotedstr(trim(DCode)));
    sql.Add(',@DName=' + quotedstr(trim(DName)));
    ExecSQL;
  end;
//  end;
  CDS_1.Delete;
//  end;

end;

procedure TfrmCustomer.FormShow(Sender: TObject);
begin
  inherited;
  fFlileFlag := UserDataFlag + 'HX';
  FAuthority := self.fParameters1;
  ReadCxGrid(trim(self.caption) + 'Tv1', Tv1, '客户管理');
  ReadCxGrid(trim(self.caption) + 'Tv2', Tv2, '客户管理');
  ReadCxGrid(trim(self.caption) + 'Tv3', Tv3, '客户管理');
  SetStatus();
  InitGrid();
end;

procedure TfrmCustomer.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
//  InitImage();
end;

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

end;

procedure TfrmCustomer.ToolButton2Click(Sender: TObject);
begin
  try
    frmCustFileUp := TfrmCustFileUp.Create(Application);
    with frmCustFileUp do
    begin
      Code.Text := Trim(Self.CDS_1.fieldbyname('CoCode').AsString);
      FBCIID := Trim(Self.CDS_1.fieldbyname('CoID').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
//        Self.CDS_1.Locate('BCIID', BCIID, []);
      end;
    end;
  finally
    frmCustFileUp.Free;
  end;
  InitImage();
end;

procedure TfrmCustomer.btnFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;
end;

procedure TfrmCustomer.ToolButton3Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      if (Trim(FAuthority) = '录入') or (Trim(FAuthority) = '管理') then
        FEditAuthority := True;
      fkeyNO := Trim(Self.CDS_1.fieldbyname('Coid').AsString);
      fType := '客户';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;

end;

procedure TfrmCustomer.ToolButton4Click(Sender: TObject);
begin
  try
    frmCustInput := TfrmCustInput.Create(Application);
    with frmCustInput do
    begin
      ToolButton3.Visible := False;
      fFlileFlag := self.fFlileFlag;
      FCOID := Trim(Self.CDS_1.fieldbyname('COID').AsString);
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmCustInput.Free;
  end;

end;

procedure TfrmCustomer.ToolButton5Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'CustomerPrint';
      FFiltration1 := QuotedStr(Trim(Self.CDS_1.fieldbyname('CoID').AsString));
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmCustomer.ToolButton6Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  try
    frmCustInput := TfrmCustInput.Create(Application);
    with frmCustInput do
    begin
      fFlileFlag := self.fFlileFlag;
      FCOID := Trim(Self.CDS_1.fieldbyname('COID').AsString);
//      Panel1.Visible := False;
//      Panel4.Visible := True;
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmCustInput.Free;
  end;

end;

procedure TfrmCustomer.ToolButton7Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  try
    frmCustInRC := TfrmCustInRC.Create(Application);
    with frmCustInRC do
    begin
      fFlileFlag := self.fFlileFlag;
      FCOID := Trim(Self.CDS_1.fieldbyname('COID').AsString);
      Panel1.Visible := False;
      Panel4.Visible := True;
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmCustInRC.Free;
  end;

end;

procedure TfrmCustomer.CoCodeChange(Sender: TObject);
begin

  btnFind.Click;
end;

procedure TfrmCustomer.CoCodeKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmCustomer.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  TBRafresh.Click;
end;

procedure TfrmCustomer.btnEditClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  try
    frmCustInput := TfrmCustInput.Create(Application);
    with frmCustInput do
    begin
      fFlileFlag := self.fFlileFlag;
      FCOID := Trim(Self.CDS_1.fieldbyname('COID').AsString);
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmCustInput.Free;
  end;
end;

procedure TfrmCustomer.btnTallyerClick(Sender: TObject);
var
  MTallyId, MTallyer: string;
begin
  MTallyId := '';
  MTallyer := '';
  try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
      Fdept := '理单';
      FMultiple := True;
      if ShowModal = 1 then
      begin
        while frmUserSel.CDS_1.Locate('SSel', True, []) do
        begin
          MTallyId := MTallyId + Trim(CDS_1.FieldByName('UserID').AsString) + ',';
          MTallyer := MTallyer + Trim(CDS_1.FieldByName('UserName').AsString) + ',';
          frmUserSel.CDS_1.Delete;
        end;
      end;
    end;
  finally
    frmUserSel.Free;
  end;
  if MTallyId = '' then
    Exit;
  MTallyId := copy(MTallyId, 1, Length(MTallyId) - 1);
  MTallyer := copy(MTallyer, 1, Length(MTallyer) - 1);
  while CDS_1.Locate('SSel', True, []) do
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update BS_Company set TallyId=' + quotedstr(trim(MTallyId)));
      sql.Add(' ,Tallyer=' + quotedstr(trim(MTallyer)));
      sql.Add(' where COID=''' + Trim(CDS_1.fieldbyname('COID').AsString) + '''');
      ExecSQL;
    end;
    CDS_1.Delete;
  end;
  InitGrid();
end;

procedure TfrmCustomer.btnChkClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if fieldbyname('SSel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  BS_Company SET status=''1'',Chktime=getdate(),Chker=' + quotedstr(trim(DName)));
            sql.Add('where CoID=' + quotedstr(trim(CDS_1.fieldbyname('CoID').AsString)));
//            showmessage(sql.text);
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    application.MessageBox('审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmCustomer.btnReChkClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if fieldbyname('SSel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  BS_Company SET status=''0'',Chktime=null,Chker=null ');
            sql.Add('where CoID=' + quotedstr(trim(CDS_1.fieldbyname('CoID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    application.MessageBox('撤销审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmCustomer.btnAddClick(Sender: TObject);
begin
  try
    frmCustInput := TfrmCustInput.Create(Application);
    with frmCustInput do
    begin
      FCOID := '';
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmCustInput.Free;
  end;
end;

procedure TfrmCustomer.TBuserClick(Sender: TObject);
var
  FuserName: string;
begin

end;

procedure TfrmCustomer.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  if cds_1.IsEmpty then
    exit;

  with ADOQuery3 do
  begin
    Close;
    SQL.Clear;
    sql.add('select * From V_BS_Company_GZ A ');
//    sql.add(',SUM(B.ConQty) ZQty,B.ConUnit,SUM(B.ConTotalAmount) ZAmount');
//    sql.add(',STUFF((select '';'' + S.C_Code from Bs_Contract_Sub S where S.ConMid=A.ConMid for xml path('''')),1,1,'''') as CodeList');
//    sql.add(',STUFF((select '';'' + S.C_Name from Bs_Contract_Sub S where S.ConMid=A.ConMid for xml path('''')),1,1,'''') as NameList');
//    sql.add('from Bs_Contract_Main A left join Bs_Contract_Sub B on B.ConMId=A.ConMId');
    sql.Add('where A.CustNo=' + quotedstr(Trim(CDS_1.FieldByName('COCode').AsString)));
    sql.add('	ORDER BY date desc');
//    ShowMessage(Sql.text);
    Open;
  end;
  SCreateCDS(ADOQuery3, CDS_3);
  SInitCDSData(ADOQuery3, CDS_3);

//  with ADOQuery4 do
//  begin
//    Close;
//    SQL.Clear;
//    sql.add('select ''生产指示单'' Type,A.Mainid,A.OrderNo,A.OrdDate,'''' AS Currency ');
//    sql.add(',SUM(B.OrdQty) ZQty,B.OrdUnit');
//    sql.add(',STUFF((select '';'' + S.C_Code from Trade_Plan_Sub S where S.MainID=A.MainId for xml path('''')),1,1,'''') as CodeList');
//    sql.add(',STUFF((select '';'' + S.C_Name from Trade_Plan_Sub S where S.MainID=A.MainId for xml path('''')),1,1,'''') as NameList');
//    sql.add('from Trade_Plan_Main A left join Trade_Plan_Sub B on B.Mainid=A.MainId');
//    sql.Add('where A.CustNo=' + quotedstr(Trim(CDS_1.FieldByName('COCode').AsString)));
//    sql.add('GROUP BY A.MainID,A.OrderNo,A.OrdDate,B.OrdUnit ORDER BY Orddate desc');
////    ShowMessage(sql.text);
//    Open;
//  end;
//  SCreateCDS(ADOQuery4, CDS_4);
//  SInitCDSData(ADOQuery4, CDS_4);
//
//  with ADOQuery5 do
//  begin
//    Close;
//    SQL.Clear;
//    sql.add('select ''报价'' Type,A.MVCMId,A.MVCNo,A.MVCDate,A.Currency ');
////    sql.add(',SUM(B.OrdQty) ZQty,B.OrdUnit');
//    sql.add(',STUFF((select '';'' + S.C_Code from MV_Cloth_Sub S where S.MVCMID=A.MVCMID for xml path('''')),1,1,'''') as CodeList');
//    sql.add(',STUFF((select '';'' + S.C_Name from MV_Cloth_Sub S where S.MVCMID=A.MVCMID for xml path('''')),1,1,'''') as NameList');
//    sql.add(' from MV_Cloth_Main A left join MV_Cloth_Sub B on B.MVCMID=A.MVCMID');
//    sql.Add('where A.BuyNo=' + quotedstr(Trim(CDS_1.FieldByName('COCode').AsString)));
//    sql.add('GROUP BY A.MVCMId,A.MVCNo,A.MVCDate,A.Currency ORDER BY A.MVCDate desc');
////    ShowMessage(sql.text);
//    Open;
//  end;
//  SCreateCDS(ADOQuery5, CDS_5);
//  SInitCDSData(ADOQuery5, CDS_5);
//
//  with ADOQuery6 do
//  begin
//    Close;
//    SQL.Clear;
//    sql.add('select * from BS_Company_RC ');
//    sql.Add('where CoID=' + quotedstr(Trim(CDS_1.FieldByName('CoID').AsString)));
//    sql.add('ORDER BY Date desc');
//    ShowMessage(SQL.text);
//    Open;
//  end;
//  SCreateCDS(ADOQuery6, CDS_6);
//  SInitCDSData(ADOQuery6, CDS_6);
//  InitImage();
end;

procedure TfrmCustomer.btnSaleserClick(Sender: TObject);
var
  MSaleser, MSalesId: string;
begin
  try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
      Fdept := '业务';
      if ShowModal = 1 then
      begin
        MSalesId := Trim(CDS_1.fieldbyname('UserID').AsString);
        MSaleser := Trim(CDS_1.fieldbyname('UserName').AsString);
      end;
    end;
  finally
    frmUserSel.Free;
  end;

  if MSalesId = '' then
    Exit;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update BS_Company set  Saleser=' + quotedstr(trim(MSaleser)));
    sql.Add(' ,SalesId=' + quotedstr(trim(MSalesId)));
    sql.Add(' where COID=''' + Trim(CDS_1.fieldbyname('COID').AsString) + '''');
    ExecSQL;
  end;

  initgrid();
end;

procedure TfrmCustomer.FormCreate(Sender: TObject);
begin
  inherited;
  FAuthority := trim(fParameters1);
end;

end.

