ReceivablesPlan.php 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 回款计划计划
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\model;
  8. use app\admin\traits\FieldVerificationTrait;
  9. use think\composer\LibraryInstaller;
  10. use think\Db;
  11. use app\admin\model\Common;
  12. use app\crm\model\Contract as ContractModel;
  13. use think\Request;
  14. use think\Validate;
  15. class ReceivablesPlan extends Common
  16. {
  17. /**
  18. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  19. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  20. */
  21. protected $name = 'crm_receivables_plan';
  22. protected $createTime = 'create_time';
  23. protected $updateTime = 'update_time';
  24. protected $autoWriteTimestamp = true;
  25. protected $statusArr = [0 =>'待回款', 1=>'完成', 2=>'部分回款', 3=>'已作废', 4=>'已逾期', 5=>'待生效'];
  26. /**
  27. * [getDataList 回款计划list]
  28. * @param [string] $map [查询条件]
  29. * @param [number] $page [当前页数]
  30. * @param [number] $limit [每页数量]
  31. * @param [string] $types 1 未使用的回款计划
  32. * @return [array] [description]
  33. * @author Michael_xu
  34. */
  35. public function getDataList($request)
  36. {
  37. $userModel = new \app\admin\model\User();
  38. $fieldModel = new \app\admin\model\Field();
  39. $search = $request['search'];
  40. $user_id = $request['user_id'];
  41. $scene_id = (int)$request['scene_id'];
  42. $check_status = $request['check_status'];
  43. $types = $request['types'];
  44. $getCount = $request['getCount'];
  45. $status = isset($request['status']) ? $request['status'] : 1;
  46. $dealt = $request['dealt']; # 待办事项
  47. $order_field = $request['order_field'];
  48. $order_type = $request['order_type'];
  49. $is_excel = $request['is_excel'];
  50. unset($request['scene_id']);
  51. unset($request['search']);
  52. unset($request['user_id']);
  53. unset($request['check_status']);
  54. unset($request['types']);
  55. unset($request['getCount']);
  56. unset($request['status']);
  57. unset($request['dealt']);
  58. unset($request['order_field']);
  59. unset($request['order_type']);
  60. unset($request['is_excel']);
  61. $request = $this->fmtRequest($request);
  62. $map = $request['map'] ?: [];
  63. $sceneModel = new \app\admin\model\Scene();
  64. $sceneMap = [];
  65. if (empty($getCount)) {
  66. if ($scene_id) {
  67. //自定义场景
  68. $sceneMap = $sceneModel->getDataById($scene_id, $user_id, 'receivables_plan') ?: [];
  69. } else {
  70. //默认场景
  71. $sceneMap = $sceneModel->getDefaultData('crm_receivables_plan', $user_id) ?: [];
  72. }
  73. }
  74. if (isset($map['search'])) {
  75. //普通筛选
  76. $map['name'] = ['like', '%' . $map['search'] . '%'];
  77. unset($map['search']);
  78. } else {
  79. // 高级筛选
  80. $map = advancedQuery($map, 'crm', 'receivables_plan', 'index');
  81. }
  82. if ($map['receivables_plan.owner_user_id']) {
  83. $map['contract.owner_user_id'] = $map['receivables_plan.owner_user_id'];
  84. unset($map['receivables_plan.owner_user_id']);
  85. }
  86. $whereData = [];
  87. if ($check_status) {
  88. unset($map['receivables_plan.check_status']);
  89. if ($check_status == 2) {
  90. $map['receivables.check_status'] = $check_status;
  91. } else {
  92. unset($map['receivables_plan.receivables_id']);
  93. $data = [];
  94. $data['check_status'] = $check_status;
  95. $whereData = function ($query) use ($data) {
  96. $query->where(['receivables_plan.receivables_id' => ['eq', 0]])
  97. ->whereOr(['receivables.check_status' => $data['check_status']]);
  98. };
  99. }
  100. }
  101. // @ymob 2019-12-11 17:51:54
  102. // 修改回款时,回款计划选项列表应该包含该回款对应的回款计划 不能过滤
  103. // 将types改为status,status:可用的回款计划 fanqi
  104. if (empty($dealt)) { # 不是待办事项
  105. if ($request['map']['receivables_id']) {
  106. if (!empty($request['map']['contract_id'])) {
  107. $map = "
  108. (`receivables_plan`.`contract_id` = {$request['map']['contract_id']} AND `receivables_plan`.`receivables_id` = {$request['map']['receivables_id']})
  109. OR
  110. (`receivables_plan`.`contract_id` = {$request['map']['contract_id']} AND `receivables_plan`.`receivables_id` = 0)
  111. ";
  112. } else {
  113. $map = " (`receivables_plan`.`receivables_id` = 0 )";
  114. }
  115. } elseif ($status == 0) {
  116. $map['receivables_plan.receivables_id'] = 0;
  117. }
  118. }
  119. $dataCount = db('crm_receivables_plan')
  120. ->alias('receivables_plan')
  121. ->join('__CRM_CONTRACT__ contract', 'receivables_plan.contract_id = contract.contract_id', 'LEFT')
  122. ->join('__CRM_CUSTOMER__ customer', 'receivables_plan.customer_id = customer.customer_id', 'LEFT')
  123. ->join('__CRM_RECEIVABLES__ receivables', 'receivables_plan.plan_id = receivables.plan_id', 'LEFT')
  124. ->where($map)
  125. ->where($sceneMap)
  126. ->where($whereData)
  127. ->count('receivables_plan.plan_id');
  128. $indexField = $fieldModel->getIndexField('crm_receivables_plan', $user_id, 1) ?: array('name'); // 列表展示字段
  129. $userField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'user'); // 人员类型
  130. $structureField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'structure'); // 部门类型
  131. $datetimeField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'datetime'); // 日期时间类型
  132. $booleanField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'boolean_value'); // 布尔值类型字段
  133. $dateIntervalField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'date_interval'); // 日期区间类型字段
  134. $positionField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'position'); // 地址类型字段
  135. $handwritingField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'handwriting_sign'); // 手写签名类型字段
  136. if (!empty($getCount) && $getCount == 1) {
  137. $data['dataCount'] = !empty($dataCount) ? $dataCount : 0;
  138. return $data;
  139. }
  140. # 处理人员和部门类型的排序报错问题(前端传来的是包含_name的别名字段)
  141. $temporaryField = str_replace('_name', '', $order_field);
  142. if (in_array($temporaryField, $userField) || in_array($temporaryField, $structureField)) {
  143. $order_field = $temporaryField;
  144. }
  145. # 排序
  146. if ($order_type && $order_field) {
  147. $order = $fieldModel->getOrderByFormtype('crm_receivables_plan', 'receivables_plan', $order_field, $order_type);
  148. } else {
  149. $order = 'receivables_plan.num asc';
  150. }
  151. $list = db('crm_receivables_plan')
  152. ->alias('receivables_plan')
  153. ->join('__CRM_CONTRACT__ contract', 'receivables_plan.contract_id = contract.contract_id', 'LEFT')
  154. ->join('__CRM_CUSTOMER__ customer', 'receivables_plan.customer_id = customer.customer_id', 'LEFT')
  155. ->join('__CRM_RECEIVABLES__ receivables', 'receivables_plan.plan_id = receivables.plan_id', 'LEFT')
  156. ->limit($request['offset'], $request['length'])
  157. ->field(array_merge($indexField, [
  158. 'customer.name' => 'customer_name',
  159. 'receivables.receivables_id' => 'receivables_id',
  160. 'receivables.check_status' => 'check_status',
  161. 'contract.num ' => 'contract_name',
  162. 'ifnull(SUM(receivables_plan.money), 0)' => 'done_money',//计划回款总金额
  163. '(ifnull(SUM(receivables.money), 0)-SUM( real_money))' => 'un_money',//未回款总金额
  164. 'SUM(real_money) AS real_money'//实际回款总金额
  165. ]))
  166. ->where($map)
  167. ->where($sceneMap)
  168. ->where($whereData)
  169. ->group('receivables_plan.contract_id')
  170. ->orderRaw($order)
  171. ->select();
  172. $grantData = getFieldGrantData($user_id);
  173. foreach ($grantData['crm_visit_'] as $key => $value) {
  174. foreach ($value as $ke => $va) {
  175. if($va['maskType']!=0){
  176. $fieldGrant[$ke]['maskType'] = $va['maskType'];
  177. $fieldGrant[$ke]['form_type'] = $va['form_type'];
  178. $fieldGrant[$ke]['field'] = $va['field'];
  179. }
  180. }
  181. }
  182. $readAuthIds = $userModel->getUserByPer('crm', 'receivables_plan', 'read');
  183. $updateAuthIds = $userModel->getUserByPer('crm', 'receivables_plan', 'update');
  184. $deleteAuthIds = $userModel->getUserByPer('crm', 'receivables_plan', 'delete');
  185. $real_money=0.00;
  186. $receivedMoney=0.00;
  187. $unReceivedMoney=0.00;
  188. foreach ($list as $k => $v) {
  189. $list[$k]['create_user_id_info'] = isset($v['create_user_id']) ? $userModel->getUserById($v['create_user_id']) : [];
  190. $list[$k]['owner_user_id_info'] = isset($v['owner_user_id']) ? $userModel->getUserById($v['owner_user_id']) : [];
  191. $list[$k]['create_user_name'] = !empty($list[$k]['create_user_id_info']['realname']) ? $list[$k]['create_user_id_info']['realname'] : '';
  192. $list[$k]['owner_user_name'] = !empty($list[$k]['owner_user_id_info']['realname']) ? $list[$k]['owner_user_id_info']['realname'] : '';
  193. foreach ($userField as $key => $val) {
  194. if (in_array($val, $indexField)) {
  195. $usernameField = !empty($v[$val]) ? db('admin_user')->whereIn('id', stringToArray($v[$val]))->column('realname') : [];
  196. $list[$k][$val] = implode($usernameField, ',');
  197. }
  198. }
  199. # 部门类型字段
  200. foreach ($structureField as $key => $val) {
  201. if (in_array($val, $indexField)) {
  202. $structureNameField = !empty($v[$val]) ? db('admin_structure')->whereIn('id', stringToArray($v[$val]))->column('name') : [];
  203. $list[$k][$val] = implode($structureNameField, ',');
  204. }
  205. }
  206. # 日期时间类型字段
  207. foreach ($datetimeField as $key => $val) {
  208. $list[$k][$val] = !empty($v[$val]) ? date('Y-m-d H:i:s', $v[$val]) : null;
  209. }
  210. // 布尔值类型字段
  211. foreach ($booleanField as $key => $val) {
  212. $list[$k][$val] = !empty($v[$val]) ? (string)$v[$val] : '0';
  213. }
  214. // 处理日期区间类型字段的格式
  215. foreach ($dateIntervalField as $key => $val) {
  216. $list[$k][$val] = !empty($extraData[$v['customer_id']][$val]) ? json_decode($extraData[$v['customer_id']][$val], true) : null;
  217. }
  218. // 处理地址类型字段的格式
  219. foreach ($positionField as $key => $val) {
  220. $list[$k][$val] = !empty($extraData[$v['customer_id']][$val]) ? json_decode($extraData[$v['customer_id']][$val], true) : null;
  221. }
  222. // 手写签名类型字段
  223. foreach ($handwritingField as $key => $val) {
  224. $handwritingData = !empty($v[$val]) ? db('admin_file')->where('file_id', $v[$val])->value('file_path') : null;
  225. $list[$k][$val] = ['url' => !empty($handwritingData) ? getFullPath($handwritingData) : null
  226. ];
  227. }
  228. foreach ($fieldGrant AS $key => $val){
  229. //掩码相关类型字段
  230. if ($val['maskType']!=0 && $val['form_type'] == 'mobile') {
  231. $pattern = "/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i";
  232. $rs = preg_replace($pattern, "$1****$2", $v[$val['field']]);
  233. $list[$k][$val['field']] = !empty($v[$val['field']]) ? (string)$rs : null;
  234. } elseif ($val['maskType']!=0 && $val['form_type'] == 'email') {
  235. $email_array = explode("@", $v[$val['field']]);
  236. $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($v[$val['field']], 0, 2); //邮箱前缀
  237. $str = preg_replace('/([\d\w+_-]{0,100})@/', "***@", $v[$val['field']], -1, $count);
  238. $rs = $prevfix . $str;
  239. $list[$k][$val['field']] = !empty($v[$val['field']]) ?$rs: null;
  240. } elseif ($val['maskType']!=0 && in_array($val['form_type'],['position','floatnumber'])) {
  241. $list[$k][$val['field']] = !empty($v[$val['field']]) ? (string)substr_replace($v[$val['field']], '*****',0,strlen($v[$val['field']])) : null;
  242. }
  243. }
  244. // 状态
  245. if(strtotime($v['return_date'])>strtotime(date("Y-m-d",strtotime("+1 day")))){
  246. $list[$k]['status']=4;
  247. }
  248. # 时间格式
  249. $list[$k]['create_time']=!empty($v['create_time'])?date('Y-m-d H:i:s',$v['create_time']):null;
  250. $list[$k]['update_time']=!empty($v['update_time'])?date('Y-m-d H:i:s',$v['update_time']):null;
  251. # 权限
  252. $roPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'read');
  253. $rwPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'update');
  254. $permission = [];
  255. $is_read = 0;
  256. $is_update = 0;
  257. $is_delete = 0;
  258. if (in_array($v['owner_user_id'], $readAuthIds) || $roPre || $rwPre) $is_read = 1;
  259. if (in_array($v['owner_user_id'], $updateAuthIds) || $rwPre) $is_update = 1;
  260. if (in_array($v['owner_user_id'], $deleteAuthIds)) $is_delete = 1;
  261. $permission['is_read'] = $is_read;
  262. $permission['is_update'] = $is_update;
  263. $permission['is_delete'] = $is_delete;
  264. $list[$k]['permission'] = $permission;
  265. $real_money += $v['real_money']; //实际回款总金额
  266. $receivedMoney += $v['done_money'];// 回款总金额
  267. $unReceivedMoney += $v['un_money']; // 未回款
  268. }
  269. $data = [];
  270. $data['list'] = $list;
  271. $data['dataCount'] = $dataCount ?: 0;
  272. $data['extraData']['money'] = [
  273. 'real_money' => $real_money, # 实际回款总金额
  274. 'receivedMoney' => $receivedMoney, # 回款总金额
  275. 'unReceivedMoney' => $unReceivedMoney # 未回款
  276. ];
  277. return $data ?: [];
  278. }
  279. /**
  280. * 创建回款计划信息
  281. * @author Michael_xu
  282. * @param
  283. * @return
  284. */
  285. public function createData($param)
  286. {
  287. $userId = $param['user_id'];
  288. unset($param['user_id']);
  289. if (!$param['contract_id']) {
  290. $this->error = '请先选择合同';
  291. return false;
  292. } else {
  293. $res = ContractModel::where(['contract_id' => $param['contract_id']])->value('check_status');
  294. if (6 == $res) {
  295. $this->error = '合同已作废';
  296. return false;
  297. }
  298. if (!in_array($res,['2'])) {
  299. $this->error = '当前合同未审核通过,不能添加回款';
  300. return false;
  301. }
  302. }
  303. if ($param['remind'] > 90) {
  304. $this->error = '提前提醒最大时间为 90 天';
  305. return false;
  306. }
  307. // // 自动验证
  308. // $validate = validate($this->name);
  309. // if (!$validate->check($param)) {
  310. // $this->error = $validate->getError();
  311. // return false;
  312. // }
  313. // 数据验证
  314. $validateResult = $this->fieldDataValidate($param, 'crm_receivables_plan', $userId);
  315. if (!empty($validateResult)) {
  316. $this->error = $validateResult;
  317. return false;
  318. }
  319. if ($param['file_ids']) $param['file'] = arrayToString($param['file_ids']); //附件
  320. //期数规则(1,2,3..)
  321. $maxNum = db('crm_receivables_plan')->where(['contract_id' => $param['contract_id']])->max('num');
  322. $param['num'] = $maxNum ? $maxNum+1 : 1;
  323. //提醒日期
  324. $param['remind_date'] = $param['remind'] ? date('Y-m-d',strtotime($param['return_date'])-86400*$param['remind']) : $param['return_date'];
  325. $fieldModel = new \app\admin\model\Field();
  326. // 处理部门、员工、附件、多选类型字段
  327. $arrFieldAtt = $fieldModel->getArrayField('crm_receivables_plan');
  328. foreach ($arrFieldAtt AS $key => $value) {
  329. $param[$value] = !empty($param[$value]) ? arrayToString($param[$value]) : '';
  330. }
  331. // 处理日期(date)类型
  332. $dateField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'date');
  333. foreach ($dateField AS $key => $value) {
  334. $param[$value] = !empty($param[$value]) ? $param[$value] : null;
  335. }
  336. // 处理手写签名类型
  337. $handwritingField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'handwriting_sign');
  338. foreach ($handwritingField AS $key => $value) {
  339. $param[$value] = !empty($param[$value]['file_id']) ? $param[$value]['file_id'] : '';
  340. }
  341. // 处理地址、定位、日期区间、明细表格类型字段
  342. $receivablesPlanData = [];
  343. $positionField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'position');
  344. $locationField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'location');
  345. $dateIntervalField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'date_interval');
  346. $detailTableField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'detail_table');
  347. foreach ($param AS $key => $value) {
  348. // 处理地址类型字段数据
  349. if (in_array($key, $positionField)) {
  350. if (!empty($value)) {
  351. $receivablesPlanData[] = [
  352. 'field' => $key,
  353. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  354. 'create_time' => time()
  355. ];
  356. $positionNames = array_column($value, 'name');
  357. $param[$key] = implode(',', $positionNames);
  358. } else {
  359. $param[$key] = '';
  360. }
  361. }
  362. // 处理定位类型字段数据
  363. if (in_array($key, $locationField)) {
  364. if (!empty($value)) {
  365. $receivablesPlanData[] = [
  366. 'field' => $key,
  367. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  368. 'create_time' => time()
  369. ];
  370. $param[$key] = $value['address'];
  371. } else {
  372. $param[$key] = '';
  373. }
  374. }
  375. // 处理日期区间类型字段数据
  376. if (in_array($key, $dateIntervalField)) {
  377. if (!empty($value)) {
  378. $receivablesPlanData[] = [
  379. 'field' => $key,
  380. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  381. 'create_time' => time()
  382. ];
  383. $param[$key] = implode('_', $value);
  384. } else {
  385. $param[$key] = '';
  386. }
  387. }
  388. // 处理明细表格类型字段数据
  389. if (in_array($key, $detailTableField)) {
  390. if (!empty($value)) {
  391. $receivablesPlanData[] = [
  392. 'field' => $key,
  393. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  394. 'create_time' => time()
  395. ];
  396. $param[$key] = $key;
  397. } else {
  398. $param[$key] = '';
  399. }
  400. }
  401. }
  402. switch ($res){
  403. case 1:
  404. $param['status']=5;
  405. break;
  406. case 2:
  407. $param['status']=0;
  408. break;
  409. case 3:
  410. $param['status']=3;
  411. break;
  412. case 6:
  413. $param['status']=0;
  414. break;
  415. }
  416. if ($this->data($param)->allowField(true)->save()) {
  417. $data = [];
  418. $data['plan_id'] = $this->plan_id;
  419. // 添加回款计划扩展数据
  420. array_walk($receivablesPlanData, function (&$val) use ($data) {
  421. $val['plan_id'] = $data['plan_id'];
  422. });
  423. db('crm_receivables_plan_data')->insertAll($receivablesPlanData);
  424. return $data;
  425. } else {
  426. $this->error = '添加失败';
  427. return false;
  428. }
  429. }
  430. /**
  431. * 编辑回款计划
  432. * @author Michael_xu
  433. * @param
  434. * @return
  435. */
  436. public function updateDataById($param, $plan_id = '')
  437. {
  438. $userId = $param['user_id'];
  439. unset($param['user_id']);
  440. $dataInfo = $this->getDataById($plan_id);
  441. if (!$dataInfo) {
  442. $this->error = '数据不存在或已删除';
  443. return false;
  444. }
  445. $param['plan_id'] = $plan_id;
  446. //过滤不能修改的字段
  447. $unUpdateField = ['num','create_user_id','is_deleted','delete_time','delete_user_id'];
  448. foreach ($unUpdateField as $v) {
  449. unset($param[$v]);
  450. }
  451. // // 自动验证
  452. // $validate = validate($this->name);
  453. // if (!$validate->check($param)) {
  454. // $this->error = $validate->getError();
  455. // return false;
  456. // }
  457. // 数据验证
  458. $validateResult = $this->fieldDataValidate($param, 'crm_receivables_plan', $userId, $plan_id);
  459. if (!empty($validateResult)) {
  460. $this->error = $validateResult;
  461. return false;
  462. }
  463. if ($param['file_ids']) $param['file'] = arrayToString($param['file_ids']); //附件
  464. //提醒日期
  465. $param['remind_date'] = $param['remind'] ? date('Y-m-d',strtotime($param['return_date'])-86400*$param['remind']) : $param['return_date'];
  466. $fieldModel = new \app\admin\model\Field();
  467. // 处理部门、员工、附件、多选类型字段
  468. $arrFieldAtt = $fieldModel->getArrayField('crm_receivables_plan');
  469. foreach ($arrFieldAtt AS $key => $value) {
  470. $param[$value] = !empty($param[$value]) ? arrayToString($param[$value]) : '';
  471. }
  472. // 处理日期(date)类型
  473. $dateField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'date');
  474. foreach ($dateField AS $key => $value) {
  475. $param[$value] = !empty($param[$value]) ? $param[$value] : null;
  476. }
  477. // 处理手写签名类型
  478. $handwritingField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'handwriting_sign');
  479. foreach ($handwritingField AS $key => $value) {
  480. $param[$value] = !empty($param[$value]['file_id']) ? $param[$value]['file_id'] : '';
  481. }
  482. // 处理地址、定位、日期区间、明细表格类型字段
  483. $receivablesPlanData = [];
  484. $positionField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'position');
  485. $locationField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'location');
  486. $dateIntervalField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'date_interval');
  487. $detailTableField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'detail_table');
  488. foreach ($param AS $key => $value) {
  489. // 处理地址类型字段数据
  490. if (in_array($key, $positionField)) {
  491. if (!empty($value)) {
  492. $receivablesPlanData[] = [
  493. 'field' => $key,
  494. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  495. 'create_time' => time()
  496. ];
  497. $positionNames = array_column($value, 'name');
  498. $param[$key] = implode(',', $positionNames);
  499. } else {
  500. $param[$key] = '';
  501. }
  502. }
  503. // 处理定位类型字段数据
  504. if (in_array($key, $locationField)) {
  505. if (!empty($value)) {
  506. $receivablesPlanData[] = [
  507. 'field' => $key,
  508. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  509. 'create_time' => time()
  510. ];
  511. $param[$key] = $value['address'];
  512. } else {
  513. $param[$key] = '';
  514. }
  515. }
  516. // 处理日期区间类型字段数据
  517. if (in_array($key, $dateIntervalField)) {
  518. if (!empty($value)) {
  519. $receivablesPlanData[] = [
  520. 'field' => $key,
  521. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  522. 'create_time' => time()
  523. ];
  524. $param[$key] = implode('_', $value);
  525. } else {
  526. $param[$key] = '';
  527. }
  528. }
  529. // 处理明细表格类型字段数据
  530. if (in_array($key, $detailTableField)) {
  531. if (!empty($value)) {
  532. $receivablesPlanData[] = [
  533. 'field' => $key,
  534. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  535. 'create_time' => time()
  536. ];
  537. $param[$key] = $key;
  538. } else {
  539. $param[$key] = '';
  540. }
  541. }
  542. }
  543. if ($this->allowField(true)->save($param, ['plan_id' => $plan_id])) {
  544. $data = [];
  545. $data['plan_id'] = $plan_id;
  546. // 添加回款计划扩展数据
  547. db('crm_receivables_plan_data')->where('plan_id', $data['plan_id'])->delete();
  548. array_walk($receivablesPlanData, function (&$val) use ($data) {
  549. $val['plan_id'] = $data['plan_id'];
  550. });
  551. db('crm_receivables_plan_data')->insertAll($receivablesPlanData);
  552. return $data;
  553. } else {
  554. $this->error = '编辑失败';
  555. return false;
  556. }
  557. }
  558. /**
  559. * 回款计划数据
  560. * @param $id 回款计划ID
  561. * @return
  562. */
  563. public function getDataById($id = '', $userId = 0, $model='')
  564. {
  565. $map['plan_id'] = $id;
  566. $dataInfo = $this->where($map)->find();
  567. if (!$dataInfo) {
  568. $this->error = '暂无此数据';
  569. return false;
  570. }
  571. if(empty($model) && $model!='update'){
  572. $grantData = getFieldGrantData($userId);
  573. foreach ($grantData['crm_receivables_plan'] as $key => $value) {
  574. foreach ($value as $ke => $va) {
  575. if($va['maskType']!=0){
  576. $fieldGrant[$ke]['maskType'] = $va['maskType'];
  577. $fieldGrant[$ke]['form_type'] = $va['form_type'];
  578. $fieldGrant[$ke]['field'] = $va['field'];
  579. }
  580. }
  581. }
  582. foreach ($fieldGrant AS $key => $val){
  583. //掩码相关类型字段
  584. if ($val['maskType']!=0 && $val['form_type'] == 'mobile') {
  585. $pattern = "/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i";
  586. $rs = preg_replace($pattern, "$1****$2", $dataInfo[$val['field']]);
  587. $dataInfo[$val['field']] = !empty($dataInfo[$val['field']]) ? (string)$rs : null;
  588. } elseif ($val['maskType']!=0 && $val['form_type'] == 'email') {
  589. $email_array = explode("@", $dataInfo[$val['field']]);
  590. $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($dataInfo[$val['field']], 0, 2); //邮箱前缀
  591. $str = preg_replace('/([\d\w+_-]{0,100})@/', "***@", $dataInfo[$val['field']], -1, $count);
  592. $rs = $prevfix . $str;
  593. $dataInfo[$val['field']] = !empty($dataInfo[$val['field']]) ?$rs: null;
  594. } elseif ($val['maskType']!=0 && in_array($val['form_type'],['position','floatnumber'])) {
  595. $dataInfo[$val['field']] = !empty($dataInfo[$val['field']]) ? (string)substr_replace($dataInfo[$val['field']], '*****',0,strlen($dataInfo[$val['field']])) : null;
  596. }
  597. }
  598. }
  599. // $userModel = new \app\admin\model\User();
  600. // $dataInfo['create_user_id_info'] = isset($dataInfo['create_user_id']) ? $userModel->getUserById($dataInfo['create_user_id']) : [];
  601. // $dataInfo['owner_user_id_info'] = isset($dataInfo['owner_user_id']) ? $userModel->getUserById($dataInfo['owner_user_id']) : [];
  602. // $dataInfo['create_user_name'] = !empty($dataInfo['create_user_id_info']['realname']) ? $dataInfo['create_user_id_info']['realname'] : '';
  603. // $dataInfo['owner_user_name'] = !empty($dataInfo['owner_user_id_info']['realname']) ? $dataInfo['owner_user_id_info']['realname'] : '';
  604. // $dataInfo['customer_id_info'] = $dataInfo['customer_id'] ? db('crm_customer')->where(['customer_id' => $dataInfo['customer_id']])->field('customer_id,name')->find() : [];
  605. // $dataInfo['contract_id_info'] = $dataInfo['contract_id'] ? db('crm_contract')->where(['contract_id' => $dataInfo['contract_id']])->field('contract_id,name,money')->find() : [];
  606. // $dataInfo['receivables_id'] = $id;
  607. // $userModel = new \app\admin\model\User();
  608. // $dataInfo['create_user_info'] = $userModel->getUserById($dataInfo['create_user_id']);
  609. // $dataInfo['plan_id'] = $id;
  610. // # 处理时间格式
  611. // $fieldModel = new \app\admin\model\Field();
  612. // $datetimeField = $fieldModel->getFieldByFormType('crm_receivables', 'datetime'); //日期时间类型
  613. // foreach ($datetimeField as $key => $val) {
  614. // $dataInfo[$val] = !empty($dataInfo[$val]) ? date('Y-m-d H:i:s', $dataInfo[$val]) : null;
  615. // }
  616. // $dataInfo['create_time'] = !empty($dataInfo['create_time']) ? date('Y-m-d H:i:s', $dataInfo['create_time']) : null;
  617. // $dataInfo['update_time'] = !empty($dataInfo['update_time']) ? date('Y-m-d H:i:s', $dataInfo['update_time']) : null;
  618. // // 字段授权
  619. // if (!empty($userId)) {
  620. // $grantData = getFieldGrantData($userId);
  621. // $userLevel = isSuperAdministrators($userId);
  622. // foreach ($dataInfo as $key => $value) {
  623. // if (!$userLevel && !empty($grantData['crm_receivables'])) {
  624. // $status = getFieldGrantStatus($key, $grantData['crm_receivables']);
  625. //
  626. // # 查看权限
  627. // if ($status['read'] == 0) unset($dataInfo[$key]);
  628. // }
  629. // }
  630. // if (!$userLevel && !empty($grantData['crm_receivables'])) {
  631. // # 客户名称
  632. // $customerStatus = getFieldGrantStatus('customer_id', $grantData['crm_receivables']);
  633. // if ($customerStatus['read'] == 0) {
  634. // $dataInfo['customer_name'] = '';
  635. // $dataInfo['customer_id_info'] = [];
  636. // }
  637. // # 合同金额
  638. // $contractMoneyStatus = getFieldGrantStatus('contract_money', $grantData['crm_receivables']);
  639. // if ($contractMoneyStatus['read'] == 0) $dataInfo['contract_id_info']['money'] = '';
  640. // # 合同名称
  641. // $contractMoneyStatus = getFieldGrantStatus('contract_money', $grantData['crm_receivables']);
  642. // if ($contractMoneyStatus['read'] == 0) $dataInfo['contract_id_info']['money'] = '';
  643. // }
  644. // }
  645. return $dataInfo;
  646. }
  647. }