Cnki.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace app\admin\controller\qingdongams\customer;
  3. use addons\qingdongams\model\ContactsFile;
  4. use addons\qingdongams\model\ReceivablesPlan;
  5. use app\admin\controller\qingdongams\Base;
  6. use addons\qingdongams\model\Staff;
  7. use think\Db;
  8. use think\Exception;
  9. use addons\qingdongams\model\Customer;
  10. use addons\qingdongams\model\Contacts;
  11. use addons\qingdongams\model\Form;
  12. use addons\qingdongams\model\CustomerOther;
  13. use addons\qingdongams\model\Receivables;
  14. use addons\qingdongams\model\Record;
  15. use addons\qingdongams\model\Contract;
  16. use addons\qingdongams\model\Comment;
  17. use addons\qingdongams\model\Consume;
  18. /**
  19. * 客户查重管理
  20. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  21. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  22. * @desc 售后微信:qingdong_crm
  23. */
  24. class Cnki extends Base
  25. {
  26. protected $relationSearch = true;
  27. /**
  28. * @var \addons\qingdongams\model\Customer
  29. */
  30. protected $model = null;
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = new \addons\qingdongams\model\Customer;
  35. }
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. if ($this->request->isAjax()) {
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $customer_ids_name=Customer::where('name in '.Customer::where([])->having('count(*) > 1')->group('name')->field('name')->buildSql(true))->column('id');
  49. $customer_ids_mobile= Contacts::where('mobile in '. Contacts::where([])->having('count(*) > 1')->group('mobile')->where(['mobile'=>['neq','']])->field('mobile')->buildSql(true))->column('customer_id');
  50. $customer_ids_email = Contacts::where('email in '.Contacts::where([])->having('count(*) > 1')->group('email,customer_id')->where(['email'=>['neq','']])->field('email')->buildSql(true))->column('customer_id');
  51. $customer_ids=array_merge($customer_ids_name?:[],$customer_ids_mobile?:[],$customer_ids_email?:[]);
  52. $customer_ids=array_values(array_unique($customer_ids));
  53. $list = $this->model->where($where)
  54. ->where(['id'=>['in',$customer_ids]])
  55. ->order($sort, $order)->paginate($limit);
  56. $rows = $list->items();
  57. foreach($rows as $k=>$v){
  58. $contacts = Contacts::where(array('customer_id'=>$v['id']))->select();
  59. foreach($contacts as $ks=>$vs){
  60. $emailinfo='';
  61. $mobileinfo ='';
  62. if($vs['mobile']){
  63. $mobileinfo = Contacts::where(array('id'=>['neq',$vs['id']],'customer_id'=>$v['id'],'mobile'=>$vs['mobile']))->value('id');
  64. }
  65. if($vs['email']){
  66. $emailinfo = Contacts::where(array('id'=>['neq',$vs['id']],'customer_id'=>$v['id'],'email'=>$vs['email']))->value('id');
  67. }
  68. if($mobileinfo || $emailinfo){
  69. unset($rows[$k]);
  70. }
  71. }
  72. }
  73. $result = array("total" => $list->total(), "rows" => array_values($rows));
  74. return json($result);
  75. }
  76. return $this->view->fetch();
  77. }
  78. /**
  79. * 合并客户
  80. */
  81. public function edit($ids=null)
  82. {
  83. if ($this->request->isPost()) {
  84. $params = $this->request->post("row/a");
  85. if ($params) {
  86. $check = isset($params['check']) ? $params['check'] : '';
  87. $ids = $params['id'];
  88. $customer_ids = $params['customer_ids'];
  89. unset($params['check']);
  90. unset($params['customer_ids']);
  91. $params = $this->preExcludeFields($params);
  92. $result = false;
  93. Db::startTrans();
  94. try {
  95. $params['id'] = $ids;
  96. $result = $this->model::updateCustomer($params);
  97. //合并后删除被合并客户
  98. $rescustomer = true;
  99. $rescontacts = true;
  100. $resrecord = true;
  101. $rescontract = true;
  102. $resconsume = true;
  103. $resreceivables = true;
  104. $resreceivables_plan = true;
  105. $rescomment = true;
  106. if($check == 1){
  107. //客户
  108. $hecustomer = $this->model->where(array('id'=>$customer_ids))->find();
  109. if($hecustomer){
  110. $rescustomer = $this->model->where(array('id'=>$customer_ids))->update(array('deletetime'=>time()));
  111. }
  112. //联系人
  113. $hecontacts =Contacts::where(array('customer_id'=>$customer_ids))->find();
  114. if($hecontacts){
  115. $rescontacts = Contacts::where(array('customer_id'=>$customer_ids))->update(array('customer_id'=>$ids,'updatetime'=>time()));
  116. }
  117. //跟进记录
  118. $herecord = Record::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->find();
  119. if($herecord){
  120. $resrecord = Record::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->update(array('relation_id'=>$ids,'updatetime'=>time()));
  121. }
  122. //合同
  123. $hecontract=Contract::where(array('customer_id'=>$customer_ids))->find();
  124. if($hecontract){
  125. $rescontract=Contract::where(array('customer_id'=>$customer_ids))->update(array('customer_id'=>$ids,'updatetime'=>time()));
  126. }
  127. //费用
  128. $heconsume = Consume::where(array('customer_id'=>$customer_ids))->find();
  129. if($heconsume){
  130. $resconsume = Consume::where(array('customer_id'=>$customer_ids))->update(array('customer_id'=>$ids,'updatetime'=>time()));
  131. }
  132. //回款
  133. $hereceivables = Receivables::where(array('customer_id'=>$customer_ids))->find();
  134. if($hereceivables){
  135. $resreceivables = Receivables::where(array('customer_id'=>$customer_ids))->update(array('customer_id'=>$ids,'updatetime'=>time()));
  136. }
  137. //回款计划
  138. $hereceivables_plan = ReceivablesPlan::where(array('customer_id'=>$customer_ids))->find();
  139. if($hereceivables_plan){
  140. $resreceivables_plan = ReceivablesPlan::where(array('customer_id'=>$customer_ids))->update(array('customer_id'=>$ids,'updatetime'=>time()));
  141. }
  142. //评论
  143. $hecomment = Comment::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->find();
  144. if($hecomment){
  145. $rescomment = Comment::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->update(array('relation_id'=>$ids,'updatetime'=>time()));
  146. }
  147. if(!$rescustomer || !$rescontacts || !$resrecord || !$rescontract || !$resconsume || !$resreceivables || !$resreceivables_plan || !$rescomment){
  148. $result = false;
  149. throw new Exception('合并失败');
  150. }
  151. }else{
  152. //联系人
  153. $hecontacts = collection(Contacts::where(array('customer_id'=>$customer_ids))->select())->toArray();
  154. foreach($hecontacts as $k=>$v){
  155. unset($v['id']);
  156. $mobileinfo = Contacts::where(array('customer_id'=>$customer_ids,'mobile'=>$v['mobile']))->value('id');
  157. $emailinfo = Contacts::where(array('customer_id'=>$customer_ids,'email'=>$v['email']))->value('id');
  158. if($emailinfo){
  159. $v['email'] = '';
  160. }
  161. if(($mobileinfo && $emailinfo) || $mobileinfo){
  162. continue;
  163. }
  164. $v['customer_id'] = $ids;
  165. $rescontacts = Contacts::create($v);
  166. }
  167. //跟进记录
  168. $herecords = collection(Record::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->select())->toArray();
  169. foreach($herecords as $k=>$vs){
  170. unset($vs['id']);
  171. $vs['relation_id'] = $ids;
  172. $resrecord = Record::create($vs);
  173. }
  174. //合同
  175. $hecontract=collection(Contract::where(array('customer_id'=>$customer_ids))->select())->toArray();
  176. foreach($hecontract as $k=>$v){
  177. unset($v['id']);
  178. $v['customer_id'] = $ids;
  179. $rescontract = Contract::create($v);
  180. }
  181. //费用
  182. $heconsume = collection(Consume::where(array('customer_id'=>$customer_ids))->select())->toArray();
  183. foreach($heconsume as $k=>$v){
  184. unset($v['id']);
  185. $v['customer_id'] = $ids;
  186. $resconsume = Consume::create($v);
  187. }
  188. //回款
  189. $hereceivables = collection(Receivables::where(array('customer_id'=>$customer_ids))->select())->toArray();
  190. foreach($hereceivables as $k=>$v){
  191. unset($v['id']);
  192. $v['customer_id'] = $ids;
  193. $resreceivables = Receivables::create($v);
  194. }
  195. //回款计划
  196. $hereceivables_plan = collection(ReceivablesPlan::where(array('customer_id'=>$customer_ids))->select())->toArray();
  197. foreach($hereceivables_plan as $k=>$v){
  198. unset($v['id']);
  199. $v['customer_id'] = $ids;
  200. $resreceivables_plan = ReceivablesPlan::create($v);
  201. }
  202. //评论
  203. $hecomment = collection(Comment::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->select())->toArray();
  204. foreach($hecomment as $k=>$v){
  205. unset($v['id']);
  206. $v['relation_id'] = $ids;
  207. $rescomment = Comment::create($v);
  208. }
  209. if(!$rescontacts || !$resrecord || !$rescontract || !$resconsume || !$resreceivables || !$resreceivables_plan || !$rescomment){
  210. $result = false;
  211. throw new Exception('合并失败');
  212. }
  213. }
  214. Db::commit();
  215. } catch (Exception $e) {
  216. Db::rollback();
  217. $this->error($e->getMessage());
  218. }
  219. if ($result !== false) {
  220. $this->success();
  221. } else {
  222. $this->error('合并失败');
  223. }
  224. }
  225. $this->error('参数不能为空');
  226. }
  227. //客户
  228. $row = Customer::where(array('id'=>$ids))->find();
  229. if(!$row){
  230. $this->error('客户不存在');
  231. }
  232. $row=$row->toArray();
  233. $row = CustomerOther::getOther($row);
  234. $fieldlist = Form::getDataValue('customer');
  235. //被合并客户信息
  236. $customer_ids = Customer::where(array('name'=>$row['name'],'id'=>['neq',$ids]))->value('id');
  237. if(!$customer_ids){
  238. //手机号
  239. $wheres['customer_id'] = array('neq',$ids);
  240. $customer_ids_mobile = Contacts::where(array('customer_id'=>$ids))->field('mobile')->group('mobile')->column('mobile');
  241. if($customer_ids_mobile){
  242. $customer_ids_mobile = Contacts::where($wheres)->where('mobile', 'in', $customer_ids_mobile)->value('customer_id');
  243. if($customer_ids_mobile){
  244. $customer_ids = $customer_ids_mobile;
  245. }
  246. }
  247. $customer_ids_email = Contacts::where(array('customer_id'=>$ids))->field('email')->group('email')->column('email');
  248. if($customer_ids_email){
  249. $customer_ids_email = Contacts::where($wheres)->where('email', 'in', $customer_ids_mobile)->value('customer_id');
  250. if($customer_ids_email){
  251. $customer_ids = $customer_ids_email;
  252. }
  253. }
  254. }
  255. if(!$customer_ids){
  256. $this->error('被合并客户不存在');
  257. }
  258. $rowinfo = Customer::where(array('id'=>$customer_ids))->find();
  259. $rowinfo=$rowinfo->toArray();
  260. $rowinfo = CustomerOther::getOther($rowinfo);
  261. $hecustomer = $fieldlist;
  262. $hecustomerInfo = [];
  263. foreach($hecustomer as $k=>$v){
  264. $hecustomerInfo[$v['id']] = $v;
  265. $hecustomerInfo[$v['id']]['value']= $rowinfo[$v['id']]??'';
  266. }
  267. $zucustomer = $fieldlist;
  268. //主客户信息
  269. foreach($zucustomer as $k=>$v){
  270. $zucustomer[$k]['value']= $row[$v['id']]??'';
  271. if(key_exists($v['id'],$hecustomerInfo)){
  272. $zucustomer[$k]['chilend'] = $hecustomerInfo[$v['id']];;
  273. }
  274. }
  275. $check = array('1'=>'合并后删除被合并客户');
  276. $this->assign('check',$check);
  277. $this->assign('form_data', $zucustomer);
  278. $this->assign('ids',$ids);
  279. $this->assign('customer_ids',$customer_ids);
  280. return $this->view->fetch();
  281. }
  282. }