使用PHP和MySQL开发客户关系管理(CRM)系统教程

746 阅读4分钟

在本教程中,我们将用PHP和MySQL实现CRM系统

客户关系管理(CRM) 是一个自动化管理客户和公司之间关系的系统。该系统涵盖了客户与销售和营销部门的关系,同时也改善了业绩并提高了生产力。

客户关系管理系统总是被需要的,因为这些系统是有用的,并且广泛用于跟踪客户和销售记录。因此,如果你是开发者,并且正在考虑开发一个CRM系统,那么你就来对地方了。在本教程中,你将学习如何用PHP和MySQL开发CRM系统。

在本教程中,我们将为销售人员实现CRM系统来跟踪客户。我们将开发销售经理部分和销售人员部分来建立该系统。我们将在本教程中涵盖以下内容。

销售经理将能够做到以下几点:

  • 管理客户
  • 管理销售团队
  • 查看销售活动

销售人员将能够做以下事情:

  • 访问任务
  • 查看线索
  • 为每个销售线索创建新的任务
  • 创建新的机会
  • 完成销售

因此,让我们开始用PHP和MySQL开发CRM系统。主要的文件有

  • index.php
  • sales_people.php
  • tasks.php
  • contact.php
  • leads.php
  • opportunity.php
  • User.php:一个包含用户方法的类。
  • Leads.php:一个包含线索方法的类。
  • Tasks. php:一个包含任务方法的类。
  • Opportunity.php:一个包含销售机会方法的类。
  • Customer.php:一个包含客户方法的类。

步骤1:创建MySQL数据库表

首先,我们将为我们的系统创建MySQL数据库表。主要的表有以下几个。

我们将创建crm_users 表来存储用户的详细信息。

CREATE TABLE `crm_users` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `roles` enum('manager','sales') NOT NULL,
  `status` int(11) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

我们将创建crm_contact 表来存储协议的细节。

CREATE TABLE `crm_contact` (
  `id` int(11) NOT NULL,
  `contact_title` varchar(255) NOT NULL,
  `contact_first` varchar(255) NOT NULL,
  `contact_middle` varchar(255) NOT NULL,
  `contact_last` varchar(255) NOT NULL,
  `initial_contact_date` datetime NOT NULL DEFAULT current_timestamp(),
  `title` varchar(255) NOT NULL,
  `company` varchar(255) NOT NULL,
  `industry` varchar(255) NOT NULL,
  `address` text NOT NULL,
  `city` varchar(255) NOT NULL,
  `state` varchar(255) NOT NULL,
  `country` varchar(255) NOT NULL,
  `zip` int(11) NOT NULL,
  `phone` int(11) NOT NULL,
  `email` varchar(50) NOT NULL,
  `status` enum('Lead','Proposal','Customer / won','Archive') NOT NULL,
  `website` varchar(255) NOT NULL,
  `sales_rep` int(11) NOT NULL,
  `project_type` varchar(255) NOT NULL,
  `project_description` text NOT NULL,
  `proposal_due_date` varchar(255) NOT NULL,
  `budget` int(11) NOT NULL,
  `deliverables` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

我们将创建crm_tasks 表来存储任务细节。

CREATE TABLE `crm_tasks` (
  `id` int(11) NOT NULL,
  `created` datetime NOT NULL DEFAULT current_timestamp(),
  `task_type` varchar(255) NOT NULL,
  `task_description` text NOT NULL,
  `task_due_date` varchar(255) NOT NULL,
  `task_status` enum('Pending','Completed') NOT NULL,
  `task_update` varchar(255) NOT NULL,
  `contact` int(11) NOT NULL,
  `sales_rep` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

第二步:实现用户登录和访问

我们将为销售经理和销售人员实现用户登录,以访问相关部分来管理系统。因此,我们将在index.php 文件中创建登录表格。

<form id="loginform" class="form-horizontal" role="form" method="POST" action="">                                    
	<div style="margin-bottom: 25px" class="input-group">
		<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
		<input type="text" class="form-control" id="email" name="email" value="<?php if(!empty($_POST["email"])) { echo $_POST["email"]; } ?>" placeholder="email" style="background:white;" required>                                        
	</div>                                
	<div style="margin-bottom: 25px" class="input-group">
		<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
		<input type="password" class="form-control" id="password" name="password" value="<?php if(!empty($_POST["password"])) { echo $_POST["password"]; } ?>" placeholder="password" required>
	</div>	

	<label class="radio-inline">
	  <input type="radio" name="loginType" value="manager">Sales Manager
	</label>
	<label class="radio-inline">
	  <input type="radio" name="loginType" value="sales">Sales People
	</label>					
	
	<div style="margin-top:10px" class="form-group">                               
		<div class="col-sm-12 controls">
		  <input type="submit" name="login" value="Login" class="btn btn-info">						  
		</div>						
	</div>						
</form>

然后我们将通过调用类User.php 中的方法login() 来实现登录功能。

public function login(){
	if($this->email && $this->password) {			
		$sqlQuery = "
			SELECT * FROM ".$this->userTable."
			WHERE status = 1 
			AND roles = ? AND email = ? AND password = ?";			
		$stmt = $this->conn->prepare($sqlQuery);
		$password = md5($this->password);
		$stmt->bind_param("sss", $this->loginType, $this->email, $password);	
		$stmt->execute();
		$result = $stmt->get_result();
		if($result->num_rows > 0){
			$user = $result->fetch_assoc();
			$_SESSION["userid"] = $user['id'];
			$_SESSION["role"] = $this->loginType;
			$_SESSION["name"] = $user['email'];					
			return 1;		
		} else {
			return 0;		
		}			
	} else {
		return 0;
	}
}

第三步:管理联系人

联系人是关于我们认识和工作的人的信息。通常情况下,一个销售代表有很多联系人。所以在这里我们将管理联系人。在contact.php 文件中,我们将显示联系人的列表。

<table id="contactListing" class="table table-bordered table-striped">
	<thead>
		<tr>						
			<th>Id</th>					
			<th>Name</th>					
			<th>Company</th>
			<th>Industry</th>
			<th>Budget</th>
			<th>Sales Rep</th>
			<th></th>	
			<th></th>	
			<th></th>						
		</tr>
	</thead>
</table>

我们将通过动作listContact 到动作contact_action.php 进行ajax请求来加载联系人列表。

var contactRecords = $('#contactListing').DataTable({
	"lengthChange": false,
	"processing":true,
	"serverSide":true,		
	"bFilter": false,
	'serverMethod': 'post',		
	"order":[],
	"ajax":{
		url:"contact_action.php",
		type:"POST",
		data:{action:'listContact'},
		dataType:"json"
	},
	"columnDefs":[
		{
			"targets":[0, 6, 7, 8],
			"orderable":false,
		},
	],
	"pageLength": 10
});	

contact_action.php 中的动作listContact ,我们将从类Contact.php 中调用方法listContact()

$contact = new Contact($db);

if(!empty($_POST['action']) && $_POST['action'] == 'listContact') {
	$contact->listContact();
}

现在我们将在类中实现方法listContact()Contact.php

public function listContact(){
		
	$sqlWhere = '';
	if($_SESSION["role"] == 'sales') { 
		$sqlWhere = " WHERE c.sales_rep = '".$_SESSION["userid"]."'";
	}	
	
	$sqlQuery = "SELECT c.id, c.contact_first, c.company, c.industry, c.budget, u.name 
	FROM ".$this->contactTable." c
	LEFT JOIN ".$this->userTable." u ON c.sales_rep = u.id $sqlWhere";
	
	if(!empty($_POST["order"])){
		$sqlQuery .= ' ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
	} else {
		$sqlQuery .= ' ORDER BY id ASC ';
	}
	
	if($_POST["length"] != -1){
		$sqlQuery .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
	}
	
	$stmt = $this->conn->prepare($sqlQuery);
	$stmt->execute();
	$result = $stmt->get_result();	
	
	$stmtTotal = $this->conn->prepare("SELECT * FROM ".$this->contactTable);
	$stmtTotal->execute();
	$allResult = $stmtTotal->get_result();
	$allRecords = $allResult->num_rows;
	
	$displayRecords = $result->num_rows;
	$records = array();		
	while ($contact = $result->fetch_assoc()) { 				
		$rows = array();			
		$rows[] = $contact['id'];
		$rows[] = ucfirst($contact['contact_first']);			
		$rows[] = $contact['company'];
		$rows[] = $contact['industry'];	
		$rows[] = $contact['budget'];		
		$rows[] = $contact['name'];				
		$rows[] = '<button  type="button" name="view" id="'.$contact["id"].'" class="btn btn-info btn-xs view"><span title="View Tasks">View Tasks</span></button>';			
		$rows[] = '<button type="button" name="update" id="'.$contact["id"].'" class="btn btn-warning btn-xs update"><span class="glyphicon glyphicon-edit" title="Edit"></span></button>';			
		$rows[] = '<button type="button" name="delete" id="'.$contact["id"].'" class="btn btn-danger btn-xs delete" ><span class="glyphicon glyphicon-remove" title="Delete"></span></button>';			
		$records[] = $rows;
	}
	
	$output = array(
		"draw"	=>	intval($_POST["draw"]),			
		"iTotalRecords"	=> 	$displayRecords,
		"iTotalDisplayRecords"	=>  $allRecords,
		"data"	=> 	$records
	);
	
	echo json_encode($output);
}

我们还实现了联系人的添加、编辑和删除功能。

第四步:管理任务

现在我们将管理活动,如会议、电话、电子邮件和任何其他允许我们与客户互动的活动。我们将实现列出任务的细节。

所以在tasks.php ,我们将显示任务列表。

<table id="tasksListing" class="table table-bordered table-striped">
	<thead>
		<tr>						
			<th>Id</th>					
			<th>Description</th>					
			<th>Due Date</th>
			<th>Contact</th>
			<th>Sales Rep</th>
			<th>Status</th>					
			<th></th>	
			<th></th>						
		</tr>
	</thead>
</table>

我们将在类中实现方法listTasks()Tasks.php

public function listTasks(){
	
	$sqlWhere = '';
	if($_SESSION["role"] == 'sales') { 
		$sqlWhere = " WHERE t.sales_rep = '".$_SESSION["userid"]."'";
	}	
		
	$sqlQuery = "SELECT t.id, t.created, t.task_type, t.task_description, t.task_due_date, t.task_status, t.task_update, c.contact_first, u.name
		FROM ".$this->tasksTable." t 
		LEFT JOIN ".$this->contactTable." c ON t.contact = c.id
		LEFT JOIN ".$this->userTable." u ON t.sales_rep = u.id $sqlWhere";	
	
	if(!empty($_POST["order"])){
		$sqlQuery .= ' ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
	} else {
		$sqlQuery .= ' ORDER BY id ASC ';
	}
	
	if($_POST["length"] != -1){
		$sqlQuery .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
	}
	
	$stmt = $this->conn->prepare($sqlQuery);
	$stmt->execute();
	$result = $stmt->get_result();	
	
	$stmtTotal = $this->conn->prepare("SELECT * FROM ".$this->contactTable);
	$stmtTotal->execute();
	$allResult = $stmtTotal->get_result();
	$allRecords = $allResult->num_rows;
	
	$displayRecords = $result->num_rows;
	$records = array();		
	while ($tasks = $result->fetch_assoc()) { 				
		$rows = array();			
		$rows[] = $tasks['id'];
		$rows[] = $tasks['task_description'];			
		$rows[] = $tasks['task_due_date'];
		$rows[] = $tasks['contact_first'];	
		$rows[] = $tasks['name'];
		$rows[] = $tasks['task_status'];					
		$rows[] = '<button type="button" name="update" id="'.$tasks["id"].'" class="btn btn-warning btn-xs update"><span class="glyphicon glyphicon-edit" title="Edit"></span></button>';			
		$rows[] = '<button type="button" name="delete" id="'.$tasks["id"].'" class="btn btn-danger btn-xs delete" ><span class="glyphicon glyphicon-remove" title="Delete"></span></button>';			
		$records[] = $rows;
	}
	
	$output = array(
		"draw"	=>	intval($_POST["draw"]),			
		"iTotalRecords"	=> 	$displayRecords,
		"iTotalDisplayRecords"	=>  $allRecords,
		"data"	=> 	$records
	);
	
	echo json_encode($output);
}

我们还处理了任务的添加、编辑和删除功能。