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