unit U_CompanySel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, U_BaseHelp, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, System.ImageList, U_BaseInput, dxSkinWXI,
  dxScrollbarAnnotations;

type
  TfrmCompanySel = class(TfrmBaseHelp)
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    ADOConnection1: TADOConnection;
    Panel1: TPanel;
    Label1: TLabel;
    CoName: TEdit;
    GPM_1: TcxGridPopupMenu;
    ImageList1: TImageList;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    SHuangSeCu: TcxStyle;
    TV1Column4: TcxGridDBColumn;
    Label2: TLabel;
    CoBusinessType: TEdit;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    Label3: TLabel;
    CoAbbrName: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure CoNameChange(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FCoType, FAuthority: string;
    { Public declarations }
  end;

var
  frmCompanySel: TfrmCompanySel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmCompanySel.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;

end;

procedure TfrmCompanySel.CoNameChange(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 TfrmCompanySel.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  CoName.SetFocus;

  Action := cahide;
end;

procedure TfrmCompanySel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select C.Currency,A.*,B.Contacts,C.* from BS_Company A left join Bs_Company_contact B on A.CoId=B.CoID ');
      sql.Add('left join Bs_Company_bank C on A.COID=C.CoID where   Valid =''Y''');
      if Trim(FCoType) <> '我司' then
      begin
        sql.Add(' and isnull(B.IsDefault,1)=1');
      end;
      if Trim(FCoType) <> '' then
      begin
        sql.Add(' and A.CoType=''' + Trim(FCoType) + '''');
      end;
      if Trim(FAuthority) = '理单业务' then
      begin
        sql.Add(' and ( SalesId=' + quotedstr(trim(DCode)) + ' or exists (select * from [dbo].[F_Tool_SplitString](TallyId,'','') X where X.RTValue =' + quotedstr(trim(DCode)) + '))');
      end;
//      ShowMessage(sql.text);
      Open;
    end;

    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCompanySel.FormShow(Sender: TObject);
begin
  inherited;
  if FCoType = '我司' then
  begin
    TV1Column5.visible := True;
    TV1Column6.visible := True;
    TV1Column7.visible := True;
    TV1Column8.visible := False;
    TV1Column9.visible := False;
    TV1Column10.visible := False;
  end
  else
  begin
    TV1Column5.visible := False;
    TV1Column6.visible := False;
    TV1Column7.visible := False;
    TV1Column8.visible := True;
    TV1Column9.visible := True;
    TV1Column10.visible := True;
  end;
  if FCoType = '供应商' then
  begin
     TV1Column11.visible := True;
  end;
    InitGrid();

  ReadCxGrid('单位名称1' + Trim(FCoType), TV1, '自定义数据');

end;

procedure TfrmCompanySel.TBCloseClick(Sender: TObject);
begin
  inherited;
  Close;
end;

procedure TfrmCompanySel.TBSaveClick(Sender: TObject);
begin
  WriteCxGrid('单位名称1' + Trim(FCoType), TV1, '自定义数据');

end;

procedure TfrmCompanySel.ToolButton1Click(Sender: TObject);
begin
  CoName.SetFocus;
  ModalResult := 1;
end;

procedure TfrmCompanySel.TV1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmCompanySel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmCompanySel := nil;
end;

end.

