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,
  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, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator;

type
  TfrmCustomer = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    Name: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    Label1: TLabel;
    Code: TEdit;
    v2Column10: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Label2: TLabel;
    ywyname: TEdit;
    v2DEFStr7: TcxGridDBColumn;
    area: TEdit;
    Label4: TLabel;
    v2Column15: TcxGridDBColumn;
    CDS_LXR: TClientDataSet;
    DSLXR: TDataSource;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v3Column3: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolButton1: TToolButton;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    TJNAME: 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 ToolButton2Click(Sender: TObject);
    procedure NameChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TBuserClick(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private

    { Private declarations }
    procedure InitGrid();
    procedure SetStatus();
  public
    canshu1: string;
  end;

//var
//  frmCustomer: TfrmCustomer;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_CustomerInput, U_YWYSel;

{$R *.dfm}
procedure TfrmCustomer.SetStatus();
begin
  ToolButton1.Visible := false;
  if Trim(canshu1) = '管理员' then
  begin
    ToolButton1.Visible := true;
  end
  else
  begin

  end;
end;

procedure TfrmCustomer.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from BS_Company_Main where CoType=''客户'' and Valid=''Y'' ');
      if Trim(canshu1) = '业务员' then
        sql.Add(' and YwyName=' + quotedstr(trim(DName)));
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmCustomer.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.caption), Tv2, '客户管理');
  Close;
end;

procedure TfrmCustomer.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(CDS_HZ.fieldbyname('COID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update BS_Company_Main set Valid=''N'' where COID=''' + Trim(CDS_HZ.fieldbyname('COID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmCustomer.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(self.Caption), Tv2, '客户管理');
  SetStatus();
  InitGrid();
end;

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

procedure TfrmCustomer.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmCustomer.NameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

procedure TfrmCustomer.ToolButton3Click(Sender: TObject);
begin
  try
    frmCustomerInput := TfrmCustomerInput.Create(Application);
    with frmCustomerInput do
    begin
      YwyName.Text := trim(Dname);
      ywycode.Text := trim(DCode);
      FCOID := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmCustomerInput.Free;
  end;
end;

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

end;

procedure TfrmCustomer.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    SQL.Clear;
    sql.Add(' select * from BS_Company_Sub where COID=' + quotedstr(Trim(CDS_HZ.FieldByName('COID').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_LXR);
  SInitCDSData20(ADOQueryTemp, CDS_LXR);
end;

procedure TfrmCustomer.ToolButton1Click(Sender: TObject);
var
  MSalesman, MSalesCode: string;
begin
  try
    frmYWYSel := TfrmYWYSel.Create(Application);
    with frmYWYSel do
    begin
      if ShowModal = 1 then
      begin
        MSalesman := Trim(ClientDataSet1.fieldbyname('UserName').AsString);
        MSalesCode := Trim(ClientDataSet1.fieldbyname('UserID').AsString);
      end;
    end;
  finally
    frmYWYSel.Free;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update BS_Company_Main set Ywyname=' + quotedstr(trim(MSalesman)) + ',Ywycode=' + quotedstr(trim(MSalesCode)) + ' where COID=''' + Trim(CDS_HZ.fieldbyname('COID').AsString) + '''');
    ExecSQL;
  end;
  initgrid();
end;

procedure TfrmCustomer.FormCreate(Sender: TObject);
begin
//  canshu1 := trim(DParameters1);
end;

end.

