unit U_Company;

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

type
  TfrmCompany = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    btnAdd: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    btnEdit: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Panel1: TPanel;
    Label3: TLabel;
    Label1: TLabel;
    CoName: TEdit;
    CoCode: TEdit;
    btnFind: TToolButton;
    ToolButton2: TToolButton;
    ToolButton5: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    TV3Column1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    ADOQuery1: TADOQuery;
    Tv1Column3: TcxGridDBColumn;
    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 btnEditClick(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure btnFindClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
  private
    fFlileFlag: string;
    { Private declarations }
    procedure InitGrid();
  public
    FAuthority: string;
  end;

var
  frmCompany: TfrmCompany;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CustomerInput, U_AttachmentUpload;

{$R *.dfm}

procedure TfrmCompany.InitGrid();
var
  FCoCode: string;
begin
  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 * from BS_Company where CoType=''我司'' and Valid=''Y'' ');
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
    CDS_1.Locate('CoCode', FCoCode, []);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCompany.FormDestroy(Sender: TObject);
begin
  inherited;
//  frmCustomer:=nil;
end;

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

procedure TfrmCompany.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmCompany.TBDelClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  if Trim(CDS_1.fieldbyname('COID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update BS_Company set Valid=''N'' where COID=''' + Trim(CDS_1.fieldbyname('COID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_1.Delete;

end;

procedure TfrmCompany.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption), Tv1, '客户管理');
  InitGrid();
end;

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

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

procedure TfrmCompany.ToolButton5Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
//      FEditAuthority := True;
      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 TfrmCompany.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
  APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from BS_Company_Bank where COID=' + quotedstr(Trim(CDS_1.FieldByName('COID').AsString)));
    Open;
  end;
  SCreateCDS(ADOQuery1, CDS_3);
  SInitCDSData(ADOQuery1, CDS_3);

end;

procedure TfrmCompany.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 TfrmCompany.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.caption), Tv1, '客户管理');

end;

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

end.

