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;

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;
    CoName: 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;
    CoCode: TEdit;
    v2Column10: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Label2: TLabel;
    Salesman: TEdit;
    v2DEFStr7: TcxGridDBColumn;
    Coarea: 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;
    ToolButton5: TToolButton;
    Panel2: TPanel;
    Button1: TButton;
    Button2: TButton;
    BegDate: TDateTimePicker;
    Label5: TLabel;
    EndDate: TDateTimePicker;
    Label6: TLabel;
    LabTS: TLabel;
    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 CoNameChange(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);
    procedure ToolButton5Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(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;
  ToolButton5.Visible:=false;
  if Trim(canshu1)='管理员' then
  begin
    ToolButton1.Visible:=true;
    ToolButton5.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 Company where CoType=''客户'' and Valid=''Y'' ');
      if Trim(canshu1)='业务员' then
        sql.Add(' and Salesman='+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 Company 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.CoNameChange(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
      Salesman.Text:=trim(Dname);
      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 Company_contact 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:string;
begin
  try
    frmYWYSel:=TfrmYWYSel.Create(Application);
    with frmYWYSel do
    begin
      if ShowModal=1 then
      begin
        MSalesman:=Trim(ClientDataSet1.fieldbyname('UserName').AsString);
      end;
    end;
  finally
    frmYWYSel.Free;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update Company set Salesman='+quotedstr(trim(MSalesman))+' where COID='''+Trim(CDS_HZ.fieldbyname('COID').AsString)+'''');
    ExecSQL;
  end;
  initgrid();
end;

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

procedure TfrmCustomer.ToolButton5Click(Sender: TObject);
begin
  if Application.MessageBox('此操作会更新大批量数据，确定要继续操作吗？','提示',32+4)<>IDYES then Exit;
  Panel2.Visible:=True;
  BegDate.Date:=Now;
  EndDate.Date:=Now;
end;

procedure TfrmCustomer.Button1Click(Sender: TObject);
var
  FKHName,FYWY,FBeg,FEnd:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  FKHName:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
  FYWY:=Trim(CDS_HZ.fieldbyname('Salesman').AsString);
  FBeg:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date));
  FEnd:=Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date));
  if Trim(FKHName)='' then
  begin
    Application.MessageBox('客户为空，不能操作!','提示',0);
    Exit;
  end;
  if Trim(FYWY)='' then
  begin
    Application.MessageBox('业务员为空，不能操作!','提示',0);
    Exit;
  end;
  if Application.MessageBox(PChar('确定要把 客户：'+Trim(FKHName)+' '+FBeg+'至'+FEnd+' 的发货数据指定到 业务员：'+FYWY+' 名下吗？'),'提示',32+4)<>IDYES then  Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    LabTS.Visible:=True;
    LabTS.Refresh;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate CK_BanCP_CR Set XSYWY='''+Trim(FYWY)+'''');
      sql.Add('where CRTime>='''+Trim(FBeg)+''' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      sql.Add(' and isnull(XSKHName,'''')='''+Trim(FKHName)+'''');
      sql.Add(' and CRFlag=''出库'' and  isnull(XSType,'''')=''销售出库'' ');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    LabTS.Visible:=False;
    Application.MessageBox('操作成功!','提示',0);
    Panel2.Visible:=False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('更新失败!','提示',0);
  end;
end;

procedure TfrmCustomer.Button2Click(Sender: TObject);
begin
  Panel2.Visible:=False;
end;

end.


