ReceivablesPlan.php 32KB

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