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