123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 自定义字段
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use app\admin\logic\FieldGrantLogic;
  9. use app\crm\logic\VisitLogic;
  10. use app\crm\model\Business;
  11. use app\crm\model\Contacts;
  12. use app\crm\model\Contract;
  13. use app\crm\model\Customer;
  14. use app\crm\model\InvoiceInfoLogic;
  15. use app\crm\model\Leads;
  16. use app\crm\model\Product;
  17. use app\crm\model\Receivables;
  18. use think\Hook;
  19. use think\Request;
  20. use think\Db;
  21. use app\admin\model\User as UserModel;
  22. class Field extends ApiCommon
  23. {
  24. /**
  25. * 用于判断权限
  26. * @permission 无限制
  27. * @allow 登录用户可访问
  28. * @other 其他根据系统设置
  29. **/
  30. public function _initialize()
  31. {
  32. $action = [
  33. 'permission'=>[''],
  34. 'allow'=>['index','getfield','update','read','config','validates','configindex','columnwidth','uniquefield']
  35. ];
  36. Hook::listen('check_auth',$action);
  37. $request = Request::instance();
  38. $a = strtolower($request->action());
  39. if (!in_array($a, $action['permission'])) {
  40. parent::_initialize();
  41. }
  42. }
  43. /**
  44. * 自定义字段列表
  45. */
  46. public function index()
  47. {
  48. //权限判断
  49. if (!checkPerByAction('admin', 'crm', 'field')) {
  50. header('Content-Type:application/json; charset=utf-8');
  51. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  52. }
  53. $param = $this->param;
  54. $types_arr = [
  55. ['types' => 'crm_leads', 'name' => '线索管理'],
  56. ['types' => 'crm_customer', 'name' => '客户管理'],
  57. ['types' => 'crm_contacts', 'name' => '联系人管理'],
  58. ['types' => 'crm_business', 'name' => '商机管理'],
  59. ['types' => 'crm_contract', 'name' => '合同管理'],
  60. ['types' => 'crm_receivables', 'name' => '回款管理'],
  61. ['types' => 'crm_receivables_plan', 'name' => '回款计划管理'],
  62. ['types' => 'crm_invoice', 'name' => '发票管理'],
  63. ['types' => 'crm_visit', 'name' => '回访管理'],
  64. ['types' => 'crm_product', 'name' => '产品管理'],
  65. ];
  66. $examine_types_arr = [];
  67. switch ($param['type']) {
  68. case 'crm' : $typesArr = $types_arr; break;
  69. case 'examine' : $typesArr = $examine_types_arr; break;
  70. default : $typesArr = $types_arr; break;
  71. }
  72. foreach ($typesArr as $k=>$v) {
  73. $updateTime = db('admin_field')->where(['types' => $v['types']])->max('update_time');
  74. $typesArr[$k]['update_time'] = !empty($updateTime) ? date('Y-m-d H:i:s', $updateTime) : '';
  75. }
  76. return resultArray(['data' => $typesArr]);
  77. }
  78. /**
  79. * 自定义字段数据
  80. */
  81. public function read()
  82. {
  83. $fieldModel = model('Field');
  84. $param = $this->param;
  85. $data = $fieldModel->getDataList($param);
  86. if ($data === false) {
  87. return resultArray(['error' => $fieldModel->getError()]);
  88. }
  89. return resultArray(['data' => $data]);
  90. }
  91. /**
  92. * 自定义字段创建
  93. */
  94. public function update()
  95. {
  96. # 系统审批类型暂不支持编辑
  97. if ($this->param['types'] == 'oa_examine' && $this->param['types_id'] < 7) {
  98. return resultArray(['error' => '系统审批类型暂不支持编辑']);
  99. }
  100. $userInfo=$this->userInfo;
  101. $fieldModel = model('Field');
  102. $param = $this->param;
  103. $types = $param['types'];
  104. $types_id = $param['types_id'] ? : 0;
  105. $data = $param['data'];
  106. $saveParam = []; # 新增数据
  107. $updateParam = []; # 编辑数据
  108. $delParam = []; # 删除数据
  109. $fieldIds = []; # 删除数据(兼容前端11.*.*版本)
  110. $errorMessage = []; # 错误数据
  111. $i = 0;
  112. foreach ($data AS $k => $v) {
  113. $i++;
  114. # 必填的字段不可以隐藏
  115. if (!empty($v['is_null']) && !empty($v['is_hidden'])) {
  116. $errorMessage = '必填的字段不可以隐藏!';
  117. break;
  118. }
  119. # 验证数字范围
  120. if (!empty($v['max_num_restrict']) && !empty($v['min_num_restrict']) && $v['min_num_restrict'] > $v['max_num_restrict']) {
  121. $errorMessage = '数字范围错误!';
  122. break;
  123. }
  124. # 验证百分数小数位
  125. if ($v['form_type'] == 'percent' && !empty($v['precisions']) && ($v['precisions'] < 1 || $v['precisions'] > 5)) {
  126. $errorMessage = '百分数字段类型的小数配置错误!';
  127. break;
  128. }
  129. # 验证数字小数位
  130. if ($v['form_type'] == 'number' && !empty($v['precisions']) && ($v['precisions'] < 1 || $v['precisions'] > 14)) {
  131. $errorMessage = '数字字段类型的小数配置错误!';
  132. break;
  133. }
  134. if ($v['field_id']) {
  135. if (isset($v['is_deleted']) && $v['is_deleted'] == '1') {
  136. # 删除
  137. $delParam[] = $v['field_id']; //删除
  138. } else {
  139. # 编辑
  140. $updateParam[$k] = $v;
  141. $updateParam[$k]['order_id'] = $i;
  142. # 用来删除自定义字段(兼容前端11.*.*版本):记录存在的自定义字段ID,取出差集,就是要删的数。
  143. $fieldIds[] = $v['field_id'];
  144. }
  145. } else {
  146. # 新增
  147. $saveParam[$k] = $v;
  148. $saveParam[$k]['order_id'] = $i;
  149. $saveParam[$k]['types_id'] = $types_id;
  150. }
  151. }
  152. # 必填的字段不可以隐藏
  153. if ($errorMessage) return resultArray(['error' => $errorMessage]);
  154. # 兼容前端11.*.*版本的删除条件处理,通过比较差异,来确定谁被前端给删除了 todo 这段代码需要写在新增上面,不然会把新增的给删除掉
  155. $oldFieldIds = Db::name('admin_field')->where('types', $types)->column('field_id');
  156. $deleteIds = array_diff($oldFieldIds, $fieldIds);
  157. foreach ($deleteIds AS $key => $value) {
  158. if (!in_array($value, $delParam)) $delParam[] = $value;
  159. }
  160. # 新增
  161. if (!empty($saveParam)) {
  162. if (!$data = $fieldModel->createData($types, $saveParam)) {
  163. $errorMessage[] = $fieldModel->getError();
  164. }
  165. }
  166. # 编辑
  167. if (!empty($updateParam)) {
  168. if (!$data = $fieldModel->updateDataById($updateParam, $types)) {
  169. $errorMessage[] = $fieldModel->getError();
  170. }
  171. }
  172. # 删除
  173. if (!empty($delParam)) {
  174. if (!$data = $fieldModel->delDataById($delParam, $types)) {
  175. $errorMessage[] = $fieldModel->getError();
  176. }
  177. }
  178. # 自定义字段变更后,同步更新字段授权表
  179. (new FieldGrantLogic())->fieldGrantDiyHandle($types);
  180. if ($errorMessage) {
  181. return resultArray(['error' => $errorMessage]);
  182. } else {
  183. # 系统操作记录
  184. $recordModules = [
  185. 'crm_leads' => '线索',
  186. 'crm_customer' => '客户',
  187. 'crm_pool' => '客户公海',
  188. 'crm_contacts' => '联系人',
  189. 'crm_product' => '产品',
  190. 'crm_business' => '商机',
  191. 'crm_contract' => '合同',
  192. 'crm_receivables' => '回款',
  193. 'crm_visit' => '回访',
  194. 'crm_invoice' => '回款',
  195. 'oa_log' => '办公日志',
  196. 'oa_examine' => '办公审批',
  197. ];
  198. if ($types !== 'oa_examine') {
  199. $systemModules = 'customer';
  200. } else {
  201. $systemModules = 'approval';
  202. }
  203. SystemActionLog($userInfo['id'], $types,$systemModules, 1, 'update', $recordModules[$types], '','','编辑了自定义字段:'.$recordModules[$types]);
  204. return resultArray(['data' => '修改成功']);
  205. }
  206. }
  207. /**
  208. * 自定义字段数据获取
  209. *
  210. * @return \think\response\Json
  211. * @throws \think\db\exception\DataNotFoundException
  212. * @throws \think\db\exception\ModelNotFoundException
  213. * @throws \think\exception\DbException
  214. */
  215. public function getField()
  216. {
  217. $fieldModel = model('Field');
  218. $userModel = model('User');
  219. $param = $this->param;
  220. $module = trim($param['module']);
  221. $controller = trim($param['controller']);
  222. $action = trim($param['action']);
  223. $system = !empty($param['system']) ? $param['system'] : 0;
  224. $format = !empty($param['format']) ? $param['format'] : 1; // 设置返回数据的格式类型:1 还是之前的二维数组格式,兼容移动端、 2 三维数组,新版自定义字段的分组排序。
  225. unset($param['system']);
  226. if (!$module || !$controller || !$action) {
  227. return resultArray(['error' => '参数错误']);
  228. }
  229. //判断权限
  230. $userInfo = $this->userInfo;
  231. $user_id = $userInfo['id'];
  232. $types = $param['types'];
  233. $types_id = $param['types_id'] ? : '';
  234. $dataInfo = [];
  235. if ($action == 'read' || $action == 'update') {
  236. //获取详情数据
  237. if (($param['action'] == 'update' || $param['action'] == 'read') && $param['action_id']) {
  238. switch ($param['types']) {
  239. case 'crm_customer' :
  240. $customerModel = new \app\crm\model\Customer();
  241. $dataInfo = $customerModel->getDataById(intval($param['action_id']));
  242. //判断权限
  243. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', $param['action']);
  244. //读写权限
  245. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  246. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  247. //判断是否客户池数据
  248. $wherePool = $customerModel->getWhereByPool();
  249. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $param['action_id']])->where($wherePool)->find();
  250. if (!$resPool && !in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  251. header('Content-Type:application/json; charset=utf-8');
  252. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  253. }
  254. break;
  255. case 'crm_leads' :
  256. $leadsModel = new \app\crm\model\Leads();
  257. $dataInfo = $leadsModel->getDataById(intval($param['action_id']));
  258. //判断权限
  259. $auth_user_ids = $userModel->getUserByPer('crm', 'leads', $param['action']);
  260. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids)) {
  261. header('Content-Type:application/json; charset=utf-8');
  262. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  263. }
  264. break;
  265. case 'crm_contacts' :
  266. $contactsModel = new \app\crm\model\Contacts();
  267. $dataInfo = $contactsModel->getDataById(intval($param['action_id']));
  268. //判断权限
  269. $auth_user_ids = $userModel->getUserByPer('crm', 'contacts', $param['action']);
  270. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids)) {
  271. header('Content-Type:application/json; charset=utf-8');
  272. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  273. }
  274. break;
  275. case 'crm_business' :
  276. $businessModel = new \app\crm\model\Business();
  277. $dataInfo = $businessModel->getDataById(intval($param['action_id']));
  278. //判断权限
  279. $auth_user_ids = $userModel->getUserByPer('crm', 'business', $param['action']);
  280. //读写权限
  281. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  282. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  283. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  284. header('Content-Type:application/json; charset=utf-8');
  285. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  286. }
  287. break;
  288. case 'crm_contract' :
  289. $contractModel = new \app\crm\model\Contract();
  290. $dataInfo = $contractModel->getDataById(intval($param['action_id']));
  291. //判断权限
  292. $auth_user_ids = $userModel->getUserByPer('crm', 'contract', $param['action']);
  293. //读写权限
  294. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  295. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  296. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  297. header('Content-Type:application/json; charset=utf-8');
  298. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  299. }
  300. break;
  301. case 'crm_product' :
  302. $productModel = new \app\crm\model\Product();
  303. $dataInfo = $productModel->getDataById(intval($param['action_id']));
  304. //判断权限
  305. $auth_user_ids = $userModel->getUserByPer('crm', 'product', $param['action']);
  306. if (!in_array($dataInfo['owner_user_id'], $auth_user_ids)) {
  307. header('Content-Type:application/json; charset=utf-8');
  308. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  309. }
  310. break;
  311. case 'crm_receivables' :
  312. $receivablesModel = new \app\crm\model\Receivables();
  313. $dataInfo = $receivablesModel->getDataById(intval($param['action_id']));
  314. //判断权限
  315. $auth_user_ids = $userModel->getUserByPer('crm', 'receivables', $param['action']);
  316. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids)) {
  317. header('Content-Type:application/json; charset=utf-8');
  318. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  319. }
  320. break;
  321. case 'crm_receivables_plan' :
  322. $receivablesPlanModel = new \app\crm\model\ReceivablesPlan();
  323. $dataInfo = $receivablesPlanModel->getDataById(intval($param['action_id']));
  324. break;
  325. case 'oa_examine' :
  326. $examineModel = new \app\oa\model\Examine();
  327. $examineFlowModel = new \app\admin\model\ExamineFlow();
  328. $dataInfo = $examineModel->getDataById(intval($param['action_id']));
  329. # 前端没有传types_id,这里需要指定一下types_id
  330. if (!empty($dataInfo['category_id'])) $param['types_id'] = $dataInfo['category_id'];
  331. $adminIds = $userModel->getAdminId(); //管理员
  332. $checkUserIds = $examineFlowModel->getUserByFlow($dataInfo['flow_id'], $dataInfo['create_user_id'], $dataInfo['check_user_id']);
  333. if (((int)$dataInfo['create_user_id'] != $user_id && !in_array($user_id,$adminIds) && !in_array($user_id,$checkUserIds))) {
  334. header('Content-Type:application/json; charset=utf-8');
  335. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  336. }
  337. break;
  338. case 'crm_visit' :
  339. $visit = new \app\crm\model\Visit();
  340. $dataInfo = $visit->getDataById(intval($param['action_id']));
  341. $fieldModel = new \app\admin\model\Field();
  342. $datetimeField = $fieldModel->getFieldByFormType('crm_visit', 'datetime'); //日期时间类型
  343. foreach ($datetimeField as $key => $val) {
  344. $dataInfo[$val] = !empty($dataInfo[$val]) ? date('Y-m-d H:i:s', $dataInfo[$val]) : null;
  345. }
  346. //判断权限
  347. $auth_user_ids = $userModel->getUserByPer('crm', 'visit', $param['action']);
  348. //读写权限
  349. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  350. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  351. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  352. header('Content-Type:application/json; charset=utf-8');
  353. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  354. }
  355. break;
  356. case 'crm_invoice' :
  357. $visit = new \app\crm\model\Invoice();
  358. $dataInfo = $visit->getDataById(intval($param['action_id']));
  359. $fieldModel = new \app\admin\model\Field();
  360. $datetimeField = $fieldModel->getFieldByFormType('crm_invoice', 'datetime'); //日期时间类型
  361. foreach ($datetimeField as $key => $val) {
  362. $dataInfo[$val] = !empty($dataInfo[$val]) ? date('Y-m-d H:i:s', $dataInfo[$val]) : null;
  363. }
  364. //判断权限
  365. $auth_user_ids = $userModel->getUserByPer('crm', 'invoice', $param['action']);
  366. if (!in_array($user_id, stringToArray($dataInfo['owner_user_id'])) && !in_array($user_id,$auth_user_ids) ) {
  367. header('Content-Type:application/json; charset=utf-8');
  368. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  369. }
  370. break;
  371. }
  372. }
  373. }
  374. $param['user_id'] = $user_id;
  375. $action_id = $param['action_id'] ? : '';
  376. $data = $fieldModel->field($param, $dataInfo) ? : [];
  377. # 多公海数据详情
  378. if (!empty($param['pool_id']) && $param['action'] == 'read') {
  379. $data = $this->setPoolDetailData($data, $param['pool_id'], $param['action_id']);
  380. }
  381. # 回访模块下,负责人名称变更为回访人
  382. if ($param['types'] == 'crm_visit') {
  383. foreach ($data AS $key => $value) {
  384. if ($value['field'] == 'owner_user_id') {
  385. $data[$key]['name'] = '回访人';
  386. break;
  387. }
  388. }
  389. }
  390. # 去掉客户模块下的成交信息
  391. if ($param['types'] == 'crm_customer' && $param['action'] == 'read') {
  392. foreach ($data AS $key => $value) {
  393. if ($value['field'] == 'deal_status') {
  394. unset($data[(int)$key]);
  395. break;
  396. }
  397. }
  398. }
  399. # 合同回款 基本信息审核状态
  400. if(in_array($param['types'], ['crm_receivables', 'crm_contract']) && $param['action'] == 'read'){
  401. $check=['0'=>'待审核','1'=>'审核中','2'=>'审核通过','3'=>'审核未通过','4'=>'撤销','5'=>'草稿(未提交)','6'=>'作废'];
  402. $data[] = [
  403. 'field' => 'check_status',
  404. 'name' => '审核状态',
  405. 'form_type' => 'text',
  406. 'writeStatus' => 0,
  407. 'fieldName' => 'check_status',
  408. 'value' => $check[$dataInfo['check_status']],
  409. ];
  410. }
  411. # 合同自动编号设置
  412. if ($param['types'] == 'crm_contract') {
  413. foreach ($data AS $key => $value) {
  414. if ($value['field'] == 'num') {
  415. if ($this->getAutoNumberStatus(1)) {
  416. $data[$key]['is_null'] = 0;
  417. $data[$key]['is_unique'] = 0;
  418. }
  419. $data[$key]['autoGeneNumber'] = $this->getAutoNumberStatus(1) ? 1 : 0;
  420. }
  421. }
  422. }
  423. # 回款自动编号设置
  424. if ($param['types'] == 'crm_receivables') {
  425. foreach ($data AS $key => $value) {
  426. if ($value['field'] == 'number') {
  427. if ($this->getAutoNumberStatus(2)) {
  428. $data[$key]['is_null'] = 0;
  429. $data[$key]['is_unique'] = 0;
  430. }
  431. $data[$key]['autoGeneNumber'] = $this->getAutoNumberStatus(2) ? 1 : 0;
  432. }
  433. }
  434. }
  435. # 回访自动编号设置
  436. if ($param['types'] == 'crm_visit') {
  437. foreach ($data AS $key => $value) {
  438. if ($value['field'] == 'number') {
  439. if ($this->getAutoNumberStatus(3)) {
  440. $data[$key]['is_null'] = 0;
  441. $data[$key]['is_unique'] = 0;
  442. }
  443. $data[$key]['autoGeneNumber'] = $this->getAutoNumberStatus(3) ? 1 : 0;
  444. }
  445. }
  446. }
  447. # 发票自动编号设置
  448. if ($param['types'] == 'crm_invoice') {
  449. foreach ($data AS $key => $value) {
  450. if ($value['field'] == 'invoice_apple_number') {
  451. if ($this->getAutoNumberStatus(4)) {
  452. $data[$key]['is_null'] = 0;
  453. $data[$key]['is_unique'] = 0;
  454. }
  455. $data[$key]['autoGeneNumber'] = $this->getAutoNumberStatus(4) ? 1 : 0;
  456. }
  457. }
  458. }
  459. # 隐藏回款计划中的附件
  460. if ($param['types'] == 'crm_receivables_plan') {
  461. foreach ($data AS $key => $value) {
  462. if ($value['field'] == 'file') {
  463. unset($data[(int)$key]);
  464. }
  465. }
  466. }
  467. if (!empty($system) && $system == 1) {
  468. # 商机和合同排除产品字段
  469. if (in_array($param['types'], ['crm_business', 'crm_contract'])) {
  470. foreach ($data AS $key => $value) {
  471. if ($value['field'] == 'product' && $value['name'] == '产品') {
  472. unset($data[(int)$key]);
  473. break;
  474. }
  475. }
  476. }
  477. $data = array_values($data);
  478. # 系统信息
  479. switch ($types) {
  480. case 'crm_leads' :
  481. $leadsModel = new Leads();
  482. $leadsData = $leadsModel->getSystemInfo($action_id);
  483. $leadsArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'last_time' => '最后跟进时间'];
  484. foreach ($leadsData AS $key => $value) {
  485. if (empty($leadsArray[$key])) continue;
  486. $data[] = [
  487. 'field' => $key,
  488. 'name' => $leadsArray[$key],
  489. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  490. 'value' => $value,
  491. 'system' => 1
  492. ];
  493. }
  494. break;
  495. case 'crm_customer' :
  496. $customerModel = new Customer();
  497. $customerData = $customerModel->getSystemInfo($action_id);
  498. $customerArray = ['obtain_time' => '负责人获取客户时间', 'owner_user_id' => '负责人', 'create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'last_time' => '最后跟进时间', 'last_record' => '最后跟进记录', 'deal_status' => '成交状态'];
  499. foreach ($customerData AS $key => $value) {
  500. if (empty($customerArray[$key]) || (!empty($param['pool_id']) && in_array($key, ['obtain_time', 'owner_user_id']))) continue;
  501. $data[] = [
  502. 'field' => $key,
  503. 'name' => $customerArray[$key],
  504. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  505. 'value' => $value,
  506. 'system' => 1
  507. ];
  508. }
  509. break;
  510. case 'crm_contacts' :
  511. $contactsModel = new Contacts();
  512. $contactsData = $contactsModel->getSystemInfo($action_id);
  513. $contactsArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'last_time' => '最后跟进时间'];
  514. foreach ($contactsData AS $key => $value) {
  515. if (empty($contactsArray[$key])) continue;
  516. $data[] = [
  517. 'field' => $key,
  518. 'name' => $contactsArray[$key],
  519. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  520. 'value' => $value,
  521. 'system' => 1
  522. ];
  523. }
  524. break;
  525. case 'crm_business' :
  526. $businessModel = new Business();
  527. $businessData = $businessModel->getSystemInfo($action_id);
  528. $businessArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'last_time' => '最后跟进时间'];
  529. foreach ($businessData AS $key => $value) {
  530. if (empty($businessArray[$key])) continue;
  531. $data[] = [
  532. 'field' => $key,
  533. 'name' => $businessArray[$key],
  534. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  535. 'value' => $value,
  536. 'system' => 1
  537. ];
  538. }
  539. break;
  540. case 'crm_contract' :
  541. $contractModel = new Contract();
  542. $contractData = $contractModel->getSystemInfo($action_id);
  543. $contractArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'last_time' => '最后跟进时间', 'done_money' => '已收款金额', 'un_money' => '未收款金额'];
  544. foreach ($contractData AS $key => $value) {
  545. if (empty($contractArray[$key])) continue;
  546. $data[] = [
  547. 'field' => $key,
  548. 'name' => $contractArray[$key],
  549. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  550. 'value' => $value,
  551. 'system' => 1
  552. ];
  553. }
  554. break;
  555. case 'crm_receivables' :
  556. $receivablesModel = new Receivables();
  557. $receivablesData = $receivablesModel->getSystemInfo($action_id);
  558. $receivablesArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间'];
  559. foreach ($receivablesData AS $key => $value) {
  560. if (empty($receivablesArray[$key])) continue;
  561. $data[] = [
  562. 'field' => $key,
  563. 'name' => $receivablesArray[$key],
  564. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  565. 'value' => $value,
  566. 'system' => 1
  567. ];
  568. }
  569. break;
  570. case 'crm_product' :
  571. $productModel = new Product();
  572. $productData = $productModel->getSystemInfo($action_id);
  573. $productArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间'];
  574. foreach ($productData AS $key => $value) {
  575. if (empty($productArray[$key])) continue;
  576. $data[] = [
  577. 'field' => $key,
  578. 'name' => $productArray[$key],
  579. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  580. 'value' => $value,
  581. 'system' => 1
  582. ];
  583. }
  584. break;
  585. case 'crm_visit' :
  586. $visitLogic = new VisitLogic();
  587. $visitData = $visitLogic->getSystemInfo($action_id);
  588. $visitArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间'];
  589. foreach ($visitData AS $key => $value) {
  590. if (empty($visitArray[$key])) continue;
  591. $data[] = [
  592. 'field' => $key,
  593. 'name' => $visitArray[$key],
  594. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  595. 'value' => $value,
  596. 'system' => 1
  597. ];
  598. }
  599. break;
  600. case 'crm_invoice' :
  601. $invoiceData = db('crm_invoice')->field(['create_user_id', 'owner_user_id', 'create_time', 'update_time'])->where('invoice_id', $action_id)->find();
  602. $createUserName = db('admin_user')->where('id', $invoiceData['create_user_id'])->value('realname');
  603. $ownerUserName = db('admin_user')->where('id', $invoiceData['owner_user_id'])->value('realname');
  604. $data[] = ['field' => 'create_user_id', 'name' => '创建人', 'form_type' => 'user', 'value' => $createUserName, 'system' => 1];
  605. $data[] = ['field' => 'create_user_id', 'name' => '负责人', 'form_type' => 'user', 'value' => $ownerUserName, 'system' => 1];
  606. $data[] = ['field' => 'create_time', 'name' => '创建时间', 'form_type' => 'datetime', 'value' => date('Y-m-d H:i:s', $invoiceData['create_time']), 'system' => 1];
  607. $data[] = ['field' => 'update_time', 'name' => '更新时间', 'form_type' => 'datetime', 'value' => date('Y-m-d H:i:s', $invoiceData['update_time']), 'system' => 1];
  608. }
  609. }
  610. # 处理自定义字段别名、权限
  611. $data = $fieldModel->resetField($user_id, $param['types'], $param['action'], $data);
  612. # 处理自定义字段分组排序
  613. if (in_array($param['action'], ['save', 'update', 'relative']) && $format == 2) $data = getFieldGroupOrderData($data);
  614. return resultArray(['data' => array_values($data)]);
  615. }
  616. /**
  617. * 自定义字段数据验重
  618. *
  619. * @return \think\response\Json
  620. */
  621. public function validates()
  622. {
  623. $param = $this->param;
  624. $fieldModel = model('Field');
  625. $res = $fieldModel->getValidate(trim($param['field']), $param['val'], intval($param['id']), trim($param['types']));
  626. if (!$res) {
  627. return resultArray(['error' => $fieldModel->getError()]);
  628. }
  629. return resultArray(['data' => '验证通过']);
  630. }
  631. /**
  632. * 自定义字段列表设置(排序、展示、列宽度)
  633. * @param types 分类
  634. * @param value 值
  635. */
  636. public function config()
  637. {
  638. $param = $this->param;
  639. $userInfo = $this->userInfo;
  640. $param['user_id'] = $userInfo['id'];
  641. $userFieldModel = model('UserField');
  642. $res = $userFieldModel->updateConfig($param['types'], $param);
  643. if (!$res) {
  644. return resultArray(['error' => $userFieldModel->getError()]);
  645. }
  646. return resultArray(['data' => '设置成功']);
  647. }
  648. /**
  649. * 自定义字段列宽度设置
  650. * @param types 分类
  651. * @param field 字段名
  652. * @param width 列宽度
  653. */
  654. public function columnWidth()
  655. {
  656. $param = $this->param;
  657. $userInfo = $this->userInfo;
  658. $userFieldModel = model('UserField');
  659. $width = $param['width'] > 10 ? $param['width'] : '';
  660. $unField = array('pool_day','owner_user_name','is_lock','create_user_name');
  661. switch ($param['field']) {
  662. case 'status_id_info' : $param['field'] = 'status_id';
  663. break;
  664. }
  665. if (!in_array($param['field'],$unField)) {
  666. $res = $userFieldModel->setColumnWidth($param['types'], $param['field'], $width, $userInfo['id']);
  667. if (!$res) {
  668. return resultArray(['error' => $userFieldModel->getError()]);
  669. }
  670. }
  671. return resultArray(['data' => '设置成功']);
  672. }
  673. /**
  674. * 自定义字段列表设置数据
  675. * @param types 分类
  676. * @param value 值
  677. */
  678. public function configIndex()
  679. {
  680. $param = $this->param;
  681. $userInfo = $this->userInfo;
  682. $userFieldModel = model('UserField');
  683. $res = $userFieldModel->getDataList($param['types'], $userInfo['id']);
  684. if (!$res) {
  685. return resultArray(['error' => $userFieldModel->getError()]);
  686. }
  687. return resultArray(['data' => $res]);
  688. }
  689. /**
  690. * 自定义验重字段
  691. * @param types 分类
  692. * @param
  693. */
  694. public function uniqueField()
  695. {
  696. $param = $this->param;
  697. if ($param['types'] == 'crm_user') {
  698. $list = array_filter(UserModel::$import_field_list, function ($val) {
  699. return $val['is_unique'] == 1;
  700. });
  701. $list = array_column($list, 'name');
  702. } else {
  703. $list = db('admin_field')->where(['types' => $param['types'],'is_unique' => 1])->column('name');
  704. }
  705. $list = $list ? implode(',',$list) : '无';
  706. return resultArray(['data' => $list]);
  707. }
  708. /**
  709. * 获取自动编号状态
  710. *
  711. * @param $type
  712. * @return int|mixed|string|null
  713. */
  714. private function getAutoNumberStatus($type)
  715. {
  716. return Db::name('crm_number_sequence')->where('number_type', $type)->where('status', 0)->value('number_sequence_id');
  717. }
  718. /**
  719. * 处理公海详情数据
  720. *
  721. * @param $data array 公海数据
  722. * @param $poolId int 公海ID
  723. * @param $actionId int 数据ID
  724. * @author fanqi
  725. * @since 2021-06-21
  726. * @return array
  727. */
  728. private function setPoolDetailData($data, $poolId, $actionId)
  729. {
  730. $poolData = [];
  731. $poolList = db('crm_customer_pool_field_setting')->field(['field_name', 'is_hidden'])->where('pool_id', $poolId)->select();
  732. // 组装字段数据
  733. foreach ($poolList AS $key => $value) {
  734. $poolData[$value['field_name']] = $value['is_hidden'];
  735. }
  736. // 处理公海数据
  737. foreach ($data AS $key => $value) {
  738. if (!empty($poolData[$value['field']]) || $value['field'] == 'owner_user_id') {
  739. unset($data[(int)$key]);
  740. continue;
  741. }
  742. }
  743. // 前负责人
  744. $beforeUser = db('crm_customer')->alias('customer')
  745. ->join('__ADMIN_USER__ user', 'user.id = customer.before_owner_user_id', 'left')
  746. ->field(['user.id', 'user.username', 'user.realname', 'user.thumb_img'])
  747. ->where('customer_id', $actionId)
  748. ->find();
  749. if (!empty($beforeUser['thumb_img'])) $beforeUser['thumb_img'] = getFullPath($beforeUser['thumb_img']);
  750. $data[] = [
  751. 'field' => 'before_owner_user_id',
  752. 'name' => '前负责人',
  753. 'form_type' => 'user',
  754. 'writeStatus' => 0,
  755. 'fieldName' => 'before_owner_user_id',
  756. 'value' => [$beforeUser],
  757. 'setting' => [],
  758. 'default_value' => [],
  759. 'options' => '',
  760. 'optionsData' => ''
  761. ];
  762. return array_values($data);
  763. }
  764. }