<?php<hyzwd@outlook.com>namespace app\index\controller;
use think\Db;
use think\Loader;
use think\Request;
use think\Session;
use app\index\model\Customers AS CustomersModel;
use app\index\model\CustomersPremises;
use app\index\model\Purchase;
use app\index\model\Logistics;
use app\index\model\Finance AS FinanceModel;
use think\Url;
class Customers extends Common_base
{
public function _initialize()
{
IS_ROOT([1,2]) ? true : $this->error('没有权限');
return parent::_initialize();
}
public function index()
{
$Customers = new CustomersModel();
$request = Request::instance();
$query = $request->param(); $status = $request->param('status'); $q = $request->param('q');
if ($q !== '') {
$data = $Customers->where('cus_name|cus_duty', 'like', '%'.$q.'%')->where('status','>=',0)->paginate('', false, ['query' => $query ]); } else {
$data = $Customers->where('status','>=',0)->paginate(20); }
$page = $data->render();
$this->assign('page',$page);
$this->assign('data', $data);
$this->assign('empty', '<tr><td colspan="9" align="center">当前条件没有查到数据</td></tr>');
$this->assign('title', '客户信息');
return $this->fetch();
}
public function add()
{
$property=Db::name('customers_type')->order('ty_id', 'asc')->select();
$evaluate=Db::name('customers_evaluate')->order('eva_id', 'asc')->select();
$this->assign('title', '新增客户');
$this->assign('property', $property);
$this->assign('evaluate', $evaluate);
return $this->fetch();
}
public function add_do()
{
$request=Request::instance();
if ($request->isPost()) {
$duty = cutstr_html($request->param('duty'));
$Customers = new CustomersModel();
$validate = Loader::validate('Customers');
if (!$validate->scene("add")->check($request->param())) {
}
$city = $request->param('city');
$dist = $request->param('dist');
$cus_city = isset($city) ? $city : '';
$cus_dist = isset($dist) ? $dist : '';
$data = [
'cus_name' => $request->param('name'),
'cus_duty' => $duty,
'cus_phome' => $request->param('phome'),
'cus_fax' => $request->param('fax'),
'cus_moble' => $request->param('moble'),
'cus_email' => $request->param('email'),
'cus_http' => $request->param('http'),
'cus_code' => $request->param('code'),
'cus_prov' => $request->param('prov'),
'cus_city' => $cus_city,
'cus_dist' => $cus_dist,
'cus_log_id'=> $request->param('cus_log_id'), 'cus_street' => $request->param('street'),
'status' => 1
];
$result = $Customers->data($data)->save();
if ($result) {
Db::name('customers_message')->insert(['msg_cus_id'=>$Customers->cus_id, 'msg_content'=>cutstr_html($request->param('content'))]);
$this->success($duty.' 添加成功',Url::build('customers/index'));
} else {
$this->error('数据操作有错误');
}
}
}
public function edit() {
$Request = Request::instance();
$id = $Request->param('id');
if (!is_numeric($id) || empty($id)) {
$this->error('参数错误!');
}
$data = Db::name('customers')->where('cus_id', $id)->find();
if (empty($data)) {
$this->error('参数错误!');
}
$Logistics = new Logistics();
$dataLog = $Logistics::get($data['cus_log_id']);
$message = Db::name('customers_message')->where('msg_cus_id', $id)->find();
$assign = array(
'title' => '修改客户信息',
'data' => $data,
'dataLog' => $dataLog,
'msg' => $message,
);
$this->assign($assign);
return $this->fetch();
}
public function edit_do() {
$Request = Request::instance();
$id = $Request->param('cus_id');
if (!is_numeric($id) || empty($id)) {
$this->error('参数错误!');
}
$By = Db::name('customers')->where('cus_id', $id)->find();
if (empty($By)) {
$this->error('参数错误!');
}
$dist = $Request->param('dist');
$dist = isset($dist) ? $dist : '';
$data = [
'cus_name' => $Request->param('name'),
'cus_duty' => $Request->param('duty'),
'cus_phome' => $Request->param('phome'),
'cus_fax' => $Request->param('fax'),
'cus_moble' => $Request->param('moble'),
'cus_email' => $Request->param('email'),
'cus_http' => $Request->param('http'),
'cus_code' => $Request->param('code'),
'cus_prov' => $Request->param('prov'),
'cus_city' => $Request->param('city'),
'cus_dist' => $dist,
'cus_log_id'=> $Request->param('cus_log_id'), 'cus_street' => $Request->param('street'),
];
Db::name('customers')->where('cus_id', $id)->update($data);
Db::name('customers_message')->where('msg_cus_id', $id)->update(['msg_content'=>cutstr_html($Request->param('content'))]);
$this->success('修改成功', Url::build('customers/index'));
}
public function view() {
$Request = Request::instance();
$id = $Request->param('id');
if (!is_numeric($id) || empty($id)) {
$this->error('参数错误!');
}
$Customers = new CustomersModel();
$data = $Customers->where('cus_id', $id)->find();
$message = Db::name('customers_message')->where('msg_cus_id', $id)->find();
$contact = Db::name('customers_contact')->where('con_cus_id', $id)->where('status','>=','1')->select();
$purchase = new Purchase();
$lsdd = $purchase->where('pcus_id', $id)->order('pstart_date','deas')->paginate();
foreach ($lsdd AS $key=>$val) {
$amo_dj = FinanceModel::where('fpnumber',$val['pnumber'])->where('sort',1)->sum('amount');
$lsdd[$key]['amo_dj'] = '¥'.number_format($amo_dj,2);
$amo_yk = FinanceModel::where('fpnumber',$val['pnumber'])->where('sort',2)->sum('amount');
$lsdd[$key]['amo_yk'] = '¥'.number_format($amo_yk,2);
}
$page_l = $lsdd->render();
$assign = array(
'title' => '查看信息',
'data' => $data,
'msg' => $message,
'contact' => $contact,
'lsdd' => $lsdd,
'page_l' => $page_l,
'empty_con' => '<tr><td colspan="8" align="center">当前条件没有查到数据</td></tr>',
'empty_lsdd' => '<tr><td colspan="9" align="center">当前客户还没有订单</td></tr>',
);
$this->assign($assign);
return $this->fetch();
}
public function adduser() {
$Request = Request::instance();
if ($Request->isPost()) {
$conid = $Request->param('con');
$cusid = $Request->param('cus');
if (!is_numeric($conid) || empty($conid) && is_numeric($cusid) || empty($cusid)) {
$this->error('参数错误!');
}
$result = Db::name('customers')->where('cus_id', $cusid)->update(['cus_con_id'=>$conid]);
if ($result) {
$this->success('设定默认联系人成功');
} else {
$this->error('设定错误!');
}
}
}
public function delete() {
IS_ROOT([1]) ? true : $this->error('没有权限');
$Request = Request::instance();
if ($Request->isPost()) {
$uid = $Request->param("uid");
$name = $Request->param("name");
if (empty($uid)) {
$this->error('传入参数错误');
}
if ($name == 'delone') {
Db::name('customers')->where('cus_id', $uid)->update(['status'=>'-1']);
$this->success('删除成功', Url::build('customers/index'));
} elseif ($name == 'delallattr') {
$arrUid = explode(",",$uid);
if (!empty($arrUid)) {
$i=0;
foreach ($arrUid as $key=>$val) {
Db::name('customers')->where('cus_id', $val)->update(['status'=>'-1']);
$i++;
}
$this->success($i.' 条记录删除成功', Url::build('customers/index'));
}
} else {
$this->error('传入参数错误');
}
}
}
public function excel() {
$customers = Db::name('customers')->order('cus_prov asc')->select();
foreach ($customers as $kc=>$vc) {
$xiadan = Db::name('purchase')->where('pcus_id', $vc['cus_id'])->count();
$lianxi = Db::name('customers_contact')->where('con_cus_id', $vc['cus_id'])->find();
$dataResult[$kc]['cus_prov'] = $vc['cus_prov'];
$dataResult[$kc]['cus_city'] = $vc['cus_city'];
$dataResult[$kc]['cus_dist'] = $vc['cus_dist'];
$dataResult[$kc]['cus_street'] = $vc['cus_street'];
$dataResult[$kc]['cus_name'] = $vc['cus_name'];
if ($xiadan > 0) {
$dataResult[$kc]['status'] = '有下单';
} else {
$dataResult[$kc]['status'] = '未下单';
}
$dataResult[$kc]['cus_duty'] = $vc['cus_duty'];
$dataResult[$kc]['cus_moble'] = $vc['cus_moble'];
$dataResult[$kc]['cus_phome'] = $vc['cus_phome'];
$dataResult[$kc]['cus_fax'] = $vc['cus_fax'];
$dataResult[$kc]['qq'] = '';
$dataResult[$kc]['cus_email'] = $vc['cus_email'];
if (empty($lianxi)) {
$dataResult[$kc]['lxr2'] = '';
$dataResult[$kc]['lxdh2'] = '';
} else {
$dataResult[$kc]['lxr2'] = $lianxi['con_name'];
$dataResult[$kc]['lxdh2'] = $lianxi['con_mobile'];
}
$wuliu = Db::name('logistics')->where('log_id', $vc['cus_log_id'])->field('log_name,log_phone,log_address')->find();
if (empty($wuliu)) {
$dataResult[$kc]['log_name'] = '';
$dataResult[$kc]['log_phone'] = '';
$dataResult[$kc]['log_address'] = '';
} else {
$dataResult[$kc]['log_name'] = $wuliu['log_name'];
$dataResult[$kc]['log_phone'] = $wuliu['log_phone'];
$dataResult[$kc]['log_address'] = $wuliu['log_address'];
}
}
$headTitle = \think\Config::get('syc_webname')." - 客户信息";
$title = "客户信息-".date('Y-m-d');
$headtitle= '<thead><tr style="height:50px;border-style:none;"><th border="0" style="height:60px;width:270px;font-size:22px;" colspan="17">'.$headTitle.'</th></tr>';
$titlename = "<tr style='height:30px;'>
<th style='width:60px;'>省</th>
<th style='width:60px;'>市</th>
<th style='width:60px;'>区/县/镇</th>
<th style='width:150px;'>地址</th>
<th style='width:70px;'>单位</th>
<th style='width:50px;'>状态</th>
<th style='width:70px;'>联系人</th>
<th style='width:100px;'>手机</th>
<th style='width:70px;'>固话</th>
<th style='width:70px;'>传真</th>
<th style='width:90px;'>QQ</th>
<th style='width:90px;'>邮箱</th>
<th style='width:90px;'>联系人2</th>
<th style='width:90px;'>联系人2电话</th>
<th style='width:90px;'>物流名称</th>
<th style='width:90px;'>物流电话</th>
<th style='width:90px;'>物流地址</th>
</tr></thead>";
$filename = $title.".xls";
$this->excelData($dataResult,$titlename,$headtitle,$filename);
}
public function excelData($datas,$titlename,$title,$filename){
IS_ROOT([1,2,3,4]) ? true : $this->error('没有权限');
$str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";
$str .="<table border=1>".$title."";
$str .= $titlename;
$str .= "<tbody>";
foreach ($datas as $key=> $rt )
{
$str .= "<tr>";
foreach ( $rt as $k => $v )
{
$str .= "<td>{$v}</td>";
}
$str .= "</tr>\n";
}
$str .= "</tbody>";
$str .= "</table>\r\n</body>\r\n</html>";
header( "Content-Type: application/vnd.ms-excel; name='excel'" );
header( "Content-type: application/octet-stream" );
header( "Content-Disposition: attachment; filename=".$filename );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Pragma: no-cache" );
header( "Expires: 0" );
exit( $str );
}
}