Receivables.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 回款
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\controller;
  8. use app\admin\controller\ApiCommon;
  9. use app\admin\model\Message;
  10. use app\admin\model\User;
  11. use app\crm\model\NumberSequence;
  12. use app\crm\traits\AutoNumberTrait;
  13. use think\Hook;
  14. use think\Request;
  15. use think\Db;
  16. class Receivables extends ApiCommon
  17. {
  18. use AutoNumberTrait;
  19. /**
  20. * 用于判断权限
  21. * @permission 无限制
  22. * @allow 登录用户可访问
  23. * @other 其他根据系统设置
  24. **/
  25. public function _initialize()
  26. {
  27. $action = [
  28. 'permission' => [''],
  29. 'allow' => ['check', 'revokecheck', 'system', 'count']
  30. ];
  31. Hook::listen('check_auth', $action);
  32. $request = Request::instance();
  33. $a = strtolower($request->action());
  34. if (!in_array($a, $action['permission'])) {
  35. parent::_initialize();
  36. }
  37. }
  38. /**
  39. * 回款列表
  40. * @return
  41. * @author Michael_xu
  42. */
  43. public function index()
  44. {
  45. $receivablesModel = model('Receivables');
  46. $param = $this->param;
  47. $userInfo = $this->userInfo;
  48. $param['user_id'] = $userInfo['id'];
  49. $data = $receivablesModel->getDataList($param);
  50. return resultArray(['data' => $data]);
  51. }
  52. /**
  53. * 导出
  54. * @param
  55. * @return
  56. * @author guogaobo
  57. */
  58. public function excelExport()
  59. {
  60. $param = $this->param;
  61. $userInfo = $this->userInfo;
  62. $param['user_id'] = $userInfo['id'];
  63. $action_name = '导出全部';
  64. if ($param['receivables_id']) {
  65. $param['receivables_id'] = ['condition' => 'in', 'value' => $param['receivables_id'], 'form_type' => 'text', 'name' => ''];
  66. $action_name='导出选中';
  67. }
  68. $excelModel = new \app\admin\model\Excel();
  69. // 导出的字段列表
  70. $fieldModel = new \app\admin\model\Field();
  71. $field_list = $fieldModel->getIndexFieldConfig('crm_receivables', $userInfo['id']);
  72. // 文件名
  73. $file_name = '5kcrm_receivables_' . date('Ymd');
  74. $model = model('Receivables');
  75. $temp_file = $param['temp_file'];
  76. unset($param['temp_file']);
  77. $page = $param['page'] ?: 1;
  78. unset($param['page']);
  79. unset($param['export_queue_index']);
  80. RecordActionLog($userInfo['id'],'crm_receivables','excelexport',$action_name,'','','导出回款');
  81. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
  82. $param['page'] = $page;
  83. $param['limit'] = $limit;
  84. $data = $model->getDataList($param);
  85. $data['list'] = $model->exportHandle($data['list'], $field_list, 'Receivables');
  86. return $data;
  87. });
  88. }
  89. /**
  90. * 添加回款
  91. *
  92. */
  93. public function save()
  94. {
  95. $receivablesModel = model('Receivables');
  96. $param = $this->param;
  97. $userInfo = $this->userInfo;
  98. $examineStepModel = new \app\admin\model\ExamineStep();
  99. $param['create_user_id'] = $userInfo['id'];
  100. $param['owner_user_id'] = $userInfo['id'];
  101. $examineStatus = $param['examineStatus']; // 审批是否停用
  102. unset($param['examineStatus']);
  103. # 自动设置回款编号
  104. $numberInfo = [];
  105. if (empty($param['number'])) {
  106. $numberInfo = $this->getAutoNumbers(2);
  107. if (empty($numberInfo['number'])) return resultArray(['error' => '请填写回款编号!']);
  108. $param['number'] = $numberInfo['number'];
  109. }
  110. if ($param['is_draft'] || (!empty($param['check_status']) && $param['check_status'] == 5)) {
  111. //保存为草稿
  112. $param['check_status'] = 5; //草稿(未提交)
  113. $param['check_user_id'] = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
  114. }
  115. if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
  116. //审核判断(是否有符合条件的审批流)
  117. $examineFlowModel = new \app\admin\model\ExamineFlow();
  118. if (!$examineFlowModel->checkExamine($param['owner_user_id'], 'crm_receivables')) {
  119. return resultArray(['error' => '暂无审批人,无法创建']);
  120. }
  121. //添加审批相关信息
  122. $examineFlowData = $examineFlowModel->getFlowByTypes($param['owner_user_id'], 'crm_receivables');
  123. if (!$examineFlowData) {
  124. return resultArray(['error' => '无可用审批流,请联系管理员']);
  125. }
  126. $param['flow_id'] = $examineFlowData['flow_id'];
  127. //获取审批人信息
  128. if ($examineFlowData['config'] == 1) {
  129. //固定审批流
  130. $nextStepData = $examineStepModel->nextStepUser($userInfo['id'], $examineFlowData['flow_id'], 'crm_receivables', 0, 0, 0);
  131. $next_user_ids = arrayToString($nextStepData['next_user_ids']) ?: '';
  132. $check_user_id = $next_user_ids ?: [];
  133. $param['order_id'] = 1;
  134. } else {
  135. $check_user_id = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
  136. }
  137. if (!$check_user_id) {
  138. return resultArray(['error' => '无可用审批人,请联系管理员']);
  139. }
  140. $param['check_user_id'] = is_array($check_user_id) ? ',' . implode(',', $check_user_id) . ',' : $check_user_id;
  141. } else {
  142. # 审批流停用,将状态改为审核通过
  143. $param['check_status'] = 2;
  144. }
  145. $res = $receivablesModel->createData($param);
  146. if ($res) {
  147. //回款计划关联
  148. if ($param['plan_id']) {
  149. db('crm_receivables_plan')->where(['plan_id' => $param['plan_id']])->update(['receivables_id' => $res['receivables_id']]);
  150. }
  151. # 更新crm_number_sequence表中的last_date、create_time字段
  152. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  153. return resultArray(['data' => '添加成功']);
  154. } else {
  155. return resultArray(['error' => $receivablesModel->getError()]);
  156. }
  157. }
  158. /**
  159. * 回款详情
  160. * @param
  161. * @return
  162. * @author Michael_xu
  163. */
  164. public function read()
  165. {
  166. $receivablesModel = model('Receivables');
  167. $userModel = new \app\admin\model\User();
  168. $param = $this->param;
  169. $userInfo = $this->userInfo;
  170. $data = $receivablesModel->getDataById($param['id'], $userInfo['id']);
  171. //判断权限
  172. $auth_user_ids = $userModel->getUserByPer('crm', 'receivables', 'read');
  173. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  174. $authData['dataAuth'] = (int)0;
  175. return resultArray(['data' => $authData]);
  176. }
  177. if (!$data) {
  178. return resultArray(['error' => $receivablesModel->getError()]);
  179. }
  180. return resultArray(['data' => $data]);
  181. }
  182. /**
  183. * 编辑回款
  184. * @param
  185. * @return
  186. * @author Michael_xu
  187. */
  188. public function update()
  189. {
  190. $receivablesModel = model('Receivables');
  191. $userModel = new \app\admin\model\User();
  192. $param = $this->param;
  193. $userInfo = $this->userInfo;
  194. $param['user_id'] = $userInfo['id'];
  195. $examineStatus = $param['examineStatus']; // 审批流是否停用
  196. unset($param['examineStatus']);
  197. //判断权限
  198. $dataInfo = $receivablesModel->getDataById($param['id']);
  199. $auth_user_ids = $userModel->getUserByPer('crm', 'receivables', 'update');
  200. if (!in_array($dataInfo['owner_user_id'], $auth_user_ids)) {
  201. header('Content-Type:application/json; charset=utf-8');
  202. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  203. }
  204. # 自动设置回款编号
  205. $numberInfo = [];
  206. if (empty($param['number'])) {
  207. $numberInfo = $this->getAutoNumbers(2);
  208. if (empty($numberInfo['number'])) return resultArray(['error' => '请填写回款编号!']);
  209. $param['number'] = $numberInfo['number'];
  210. }
  211. //已进行审批,不能编辑
  212. if (!in_array($dataInfo['check_status'], ['3', '4', '5', '6'])) {
  213. return resultArray(['error' => '当前状态为审批中或已审批通过,不可编辑']);
  214. }
  215. if ($param['is_draft'] || (!empty($param['check_status']) && $param['check_status'] == 5)) {
  216. //保存为草稿
  217. $param['check_status'] = 5; //草稿(未提交)
  218. $param['check_user_id'] = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
  219. } else {
  220. if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
  221. if ($param['is_draft']) {
  222. //保存为草稿
  223. $param['check_status'] = 5;
  224. $param['check_user_id'] = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
  225. } else {
  226. //将回款审批状态至为待审核,提交后重新进行审批
  227. //审核判断(是否有符合条件的审批流)
  228. $examineFlowModel = new \app\admin\model\ExamineFlow();
  229. $examineStepModel = new \app\admin\model\ExamineStep();
  230. if (!$examineFlowModel->checkExamine($param['user_id'], 'crm_receivables')) {
  231. return resultArray(['error' => '暂无审批人,无法创建']);
  232. }
  233. //添加审批相关信息
  234. $examineFlowData = $examineFlowModel->getFlowByTypes($param['user_id'], 'crm_receivables');
  235. if (!$examineFlowData) {
  236. return resultArray(['error' => '无可用审批流,请联系管理员']);
  237. }
  238. $param['flow_id'] = $examineFlowData['flow_id'];
  239. //获取审批人信息
  240. if ($examineFlowData['config'] == 1) {
  241. //固定审批流
  242. $nextStepData = $examineStepModel->nextStepUser($dataInfo['owner_user_id'], $examineFlowData['flow_id'], 'crm_receivables', 0, 0, 0);
  243. $next_user_ids = arrayToString($nextStepData['next_user_ids']) ?: '';
  244. $check_user_id = $next_user_ids ?: [];
  245. $param['order_id'] = 1;
  246. } else {
  247. $check_user_id = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
  248. }
  249. if (!$check_user_id) {
  250. return resultArray(['error' => '无可用审批人,请联系管理员']);
  251. }
  252. $param['check_user_id'] = is_array($check_user_id) ? ',' . implode(',', $check_user_id) . ',' : $check_user_id;
  253. $param['check_status'] = 0;
  254. $param['flow_user_id'] = '';
  255. }
  256. }
  257. }
  258. $res = $receivablesModel->updateDataById($param, $param['id']);
  259. if ($res) {
  260. //将审批记录至为无效
  261. $examineRecordModel = new \app\admin\model\ExamineRecord();
  262. $examineRecordModel->setEnd(['types' => 'crm_receivables', 'types_id' => $param['id']]);
  263. # 更新crm_number_sequence表中的last_date、create_time字段
  264. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  265. return resultArray(['data' => '编辑成功']);
  266. } else {
  267. return resultArray(['error' => $receivablesModel->getError()]);
  268. }
  269. }
  270. /**
  271. * 删除回款
  272. * @param
  273. * @return
  274. * @author Michael_xu
  275. */
  276. public function delete()
  277. {
  278. $actionRecordModel = new \app\admin\model\ActionRecord();
  279. $fileModel = new \app\admin\model\File();
  280. $recordModel = new \app\admin\model\Record();
  281. $receivablesModel = model('Receivables');
  282. $param = $this->param;
  283. $userInfo = $this->userInfo;
  284. if (!is_array($param['id'])) {
  285. $receivables_id = [$param['id']];
  286. } else {
  287. $receivables_id = $param['id'];
  288. }
  289. $delIds = [];
  290. $errorMessage = [];
  291. //数据权限判断
  292. $userModel = new \app\admin\model\User();
  293. $auth_user_ids = $userModel->getUserByPer('crm', 'receivables', 'delete');
  294. $adminTypes = adminGroupTypes($userInfo['id']);
  295. foreach ($receivables_id as $k => $v) {
  296. $isDel = true;
  297. //数据详情
  298. $data = $receivablesModel->getDataById($v);
  299. if (!$data) {
  300. $isDel = false;
  301. $errorMessage[] = 'id为' . $v . '的回款删除失败,错误原因:' . $receivablesModel->getError();
  302. continue;
  303. }
  304. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  305. $isDel = false;
  306. $errorMessage[] = '名称为' . $data['number'] . '的回款删除失败,错误原因:无权操作';
  307. continue;
  308. }
  309. if (!in_array($data['check_status'], [4, 5]) && !in_array(1, $adminTypes)) {
  310. $isDel = false;
  311. $errorMessage[] = '名称为' . $data['number'] . '的回款删除失败,错误原因:请先撤销审核';
  312. continue;
  313. }
  314. if ($isDel) {
  315. if (db('crm_receivables_plan')->where('receivables_id', $v)->value('plan_id')) {
  316. $isDel = false;
  317. $errorMessage[] = '名称为' . $data['number'] . '的回款删除失败,错误原因:回款已关联回款计划,不能删除!';
  318. continue;
  319. }
  320. }
  321. if ($isDel) {
  322. $delIds[] = $v;
  323. }
  324. }
  325. $dataInfo = $receivablesModel->where('receivables_id',['in',$delIds])->select();
  326. if ($delIds) {
  327. $data = $receivablesModel->delDatas($delIds);
  328. if (!$data) {
  329. return resultArray(['error' => $receivablesModel->getError()]);
  330. }
  331. // 删除回款扩展数据
  332. db('crm_receivables_data')->whereIn('receivables_id', $delIds)->delete();
  333. //删除跟进记录
  334. $recordModel->delDataByTypes(7,$delIds);
  335. # 删除附件
  336. $fileModel->delRFileByModule('crm_receivables', $delIds);
  337. //删除关联操作记录
  338. $actionRecordModel->delDataById(['types' => 'crm_receivables', 'action_id' => $delIds]);
  339. $userInfo = $this->userInfo;
  340. foreach ($dataInfo as $k => $v) {
  341. RecordActionLog($userInfo['id'], 'crm_contacts', 'delete', $v['number'], '', '', '删除了回款:' . $v['number']);
  342. }
  343. }
  344. if ($errorMessage) {
  345. return resultArray(['error' => $errorMessage]);
  346. } else {
  347. return resultArray(['data' => '删除成功']);
  348. }
  349. }
  350. /**
  351. * 回款审核
  352. * @param
  353. * @return
  354. * @author Michael_xu
  355. */
  356. public function check()
  357. {
  358. $param = $this->param;
  359. $userInfo = $this->userInfo;
  360. $user_id = $userInfo['id'];
  361. $receivablesModel = model('Receivables');
  362. $examineStepModel = new \app\admin\model\ExamineStep();
  363. $examineRecordModel = new \app\admin\model\ExamineRecord();
  364. $examineFlowModel = new \app\admin\model\ExamineFlow();
  365. $receivablesData = [];
  366. $receivablesData['update_time'] = time();
  367. $receivablesData['check_status'] = 1; //0待审核,1审核通中,2审核通过,3审核未通过
  368. //权限判断
  369. if (!$examineStepModel->checkExamine($user_id, 'crm_receivables', $param['id'])) {
  370. return resultArray(['error' => $examineStepModel->getError()]);
  371. };
  372. //审批主体详情
  373. $dataInfo = $receivablesModel->getDataById($param['id']);
  374. $flowInfo = $examineFlowModel->getDataById($dataInfo['flow_id']);
  375. $is_end = 0; // 1审批结束
  376. $status = $param['status'] ? 1 : 0; //1通过,0驳回
  377. $checkData = [];
  378. $checkData['check_user_id'] = $user_id;
  379. $checkData['types'] = 'crm_receivables';
  380. $checkData['types_id'] = $param['id'];
  381. $checkData['check_time'] = time();
  382. $checkData['content'] = $param['content'];
  383. $checkData['flow_id'] = $dataInfo['flow_id'];
  384. $checkData['order_id'] = $dataInfo['order_id'] ?: 1;
  385. $checkData['status'] = $status;
  386. if ($status == 1) {
  387. if ($flowInfo['config'] == 1) {
  388. //固定流程
  389. //获取下一审批信息
  390. $nextStepData = $examineStepModel->nextStepUser($dataInfo['owner_user_id'], $dataInfo['flow_id'], 'crm_receivables', $param['id'], $dataInfo['order_id'], $user_id);
  391. $next_user_ids = $nextStepData['next_user_ids'] ?: [];
  392. $receivablesData['order_id'] = $nextStepData['order_id'] ?: '';
  393. if (!$next_user_ids) {
  394. $is_end = 1;
  395. //审批结束
  396. $checkData['check_status'] = !empty($status) ? 2 : 3;
  397. $receivablesData['check_user_id'] = '';
  398. } else {
  399. //修改主体相关审批信息
  400. $receivablesData['check_user_id'] = arrayToString($next_user_ids);
  401. }
  402. } else {
  403. //自选流程
  404. $is_end = $param['is_end'] ? 1 : '';
  405. $check_user_id = $param['check_user_id'] ?: '';
  406. if ($is_end !== 1 && empty($check_user_id)) {
  407. return resultArray(['error' => '请选择下一审批人']);
  408. }
  409. $receivablesData['check_user_id'] = arrayToString($param['check_user_id']);
  410. }
  411. if ($is_end == 1) {
  412. $checkData['check_status'] = !empty($status) ? 2 : 3;
  413. $receivablesData['check_user_id'] = '';
  414. $receivablesData['check_status'] = 2;
  415. }
  416. } else {
  417. //审批驳回
  418. $is_end = 1;
  419. $receivablesData['check_status'] = 3;
  420. //将审批记录至为无效
  421. // $examineRecordModel->setEnd(['types' => 'crm_receivables','types_id' => $param['id']]);
  422. }
  423. //已审批人ID
  424. $receivablesData['flow_user_id'] = stringToArray($dataInfo['flow_user_id']) ? arrayToString(array_merge(stringToArray($dataInfo['flow_user_id']), [$user_id])) : arrayToString([$user_id]);
  425. $resReceivables = db('crm_receivables')->where(['receivables_id' => $param['id']])->update($receivablesData);
  426. if ($resReceivables) {
  427. if ($status) {
  428. // 审批通过,通知下一审批人
  429. (new Message())->send(
  430. Message::RECEIVABLES_TO_DO,
  431. [
  432. 'from_user' => User::where(['id' => $dataInfo['owner_user_id']])->value('realname'),
  433. 'title' => $dataInfo['number'],
  434. 'action_id' => $param['id']
  435. ],
  436. stringToArray($receivablesData['check_user_id'])
  437. );
  438. } else {
  439. // 驳回通知负责人
  440. (new Message())->send(
  441. Message::RECEIVABLES_REJECT,
  442. [
  443. 'title' => $dataInfo['number'],
  444. 'action_id' => $param['id']
  445. ],
  446. $dataInfo['owner_user_id']
  447. );
  448. }
  449. //审批记录
  450. $resRecord = $examineRecordModel->createData($checkData);
  451. if ($is_end == 1 && !empty($status)) {
  452. //发送站内信 通过
  453. (new Message())->send(
  454. Message::RECEIVABLES_PASS,
  455. [
  456. 'title' => $dataInfo['number'],
  457. 'action_id' => $param['id']
  458. ],
  459. $dataInfo['owner_user_id']
  460. );
  461. }
  462. return resultArray(['data' => '审批成功']);
  463. } else {
  464. return resultArray(['error' => '审批失败,请重试!']);
  465. }
  466. }
  467. /**
  468. * 回款撤销审核
  469. * @param
  470. * @return
  471. * @author Michael_xu
  472. */
  473. public function revokeCheck()
  474. {
  475. $param = $this->param;
  476. $userInfo = $this->userInfo;
  477. $user_id = $userInfo['id'];
  478. $receivablesModel = model('Receivables');
  479. $examineStepModel = new \app\admin\model\ExamineStep();
  480. $examineRecordModel = new \app\admin\model\ExamineRecord();
  481. $customerModel = model('Customer');
  482. $userModel = new \app\admin\model\User();
  483. $receivablesData = [];
  484. $receivablesData['update_time'] = time();
  485. $receivablesData['check_status'] = 0; //0待审核,1审核通中,2审核通过,3审核未通过
  486. //审批主体详情
  487. $dataInfo = $receivablesModel->getDataById($param['id']);
  488. //权限判断(创建人或负责人或管理员)
  489. if ($dataInfo['check_status'] == 2) {
  490. return resultArray(['error' => '已审批结束,不能撤销']);
  491. }
  492. if ($dataInfo['check_status'] == 4) {
  493. return resultArray(['error' => '无需撤销']);
  494. }
  495. $admin_user_ids = $userModel->getAdminId();
  496. if ($dataInfo['owner_user_id'] !== $user_id && !in_array($user_id, $admin_user_ids)) {
  497. return resultArray(['error' => '没有权限']);
  498. }
  499. $is_end = 0; // 1审批结束
  500. $status = 2; //1通过,0驳回, 2撤销
  501. $checkData = [];
  502. $checkData['check_user_id'] = $user_id;
  503. $checkData['types'] = 'crm_receivables';
  504. $checkData['types_id'] = $param['id'];
  505. $checkData['check_time'] = time();
  506. $checkData['content'] = $param['content'];
  507. $checkData['flow_id'] = $dataInfo['flow_id'];
  508. $checkData['order_id'] = $dataInfo['order_id'];
  509. $checkData['status'] = $status;
  510. $receivablesData['check_status'] = 4;
  511. $receivablesData['check_user_id'] = '';
  512. $receivablesData['flow_user_id'] = '';
  513. $resReceivables = db('crm_receivables')->where(['receivables_id' => $param['id']])->update($receivablesData);
  514. if ($resReceivables) {
  515. //将审批记录至为无效
  516. // $examineRecordModel->setEnd(['types' => 'crm_receivables','types_id' => $param['id']]);
  517. //审批记录
  518. $resRecord = $examineRecordModel->createData($checkData);
  519. return resultArray(['data' => '撤销成功']);
  520. } else {
  521. return resultArray(['error' => '撤销失败,请重试!']);
  522. }
  523. }
  524. /**
  525. * 转移
  526. *
  527. * @return \think\response\Json
  528. */
  529. public function transfer()
  530. {
  531. $param = $this->param;
  532. $userInfo = $this->userInfo;
  533. $receivablesModel = model('Receivables');
  534. $userModel = new \app\admin\model\User();
  535. $authIds = $userModel->getUserByPer();
  536. if (empty($param['owner_user_id'])) return resultArray(['error' => '变更负责人不能为空']);
  537. if (empty($param['receivables_id']) || !is_array($param['receivables_id'])) return resultArray(['error' => '请选择需要转移的回款']);
  538. $owner_user_info = $userModel->getUserById($param['owner_user_id']);
  539. $data = [
  540. 'owner_user_id' => $param['owner_user_id'],
  541. 'update_time' => time(),
  542. ];
  543. $errorMessage = [];
  544. foreach ($param['receivables_id'] as $receivables_id) {
  545. $receivables_info = $receivablesModel->getDataById($receivables_id);
  546. if (!$receivables_info) {
  547. $errorMessage[] = 'id:为《' . $receivables_id . '》的回款转移失败,错误原因:数据不存在;';
  548. continue;
  549. }
  550. # 转移至当前负责人的直接跳过
  551. if ($param['owner_user_id'] == $receivables_info['owner_user_id']) continue;
  552. if (!in_array($receivables_info['owner_user_id'], $authIds)) {
  553. $errorMessage[] = $receivables_info['number'] . '"转移失败,错误原因:无权限;';
  554. continue;
  555. }
  556. if (in_array($receivables_info['check_status'], ['0', '1'])) {
  557. $errorMessage[] = $receivables_info['number'] . '转移失败,错误原因:待审或审批中,无法转移;';
  558. continue;
  559. }
  560. $res = $receivablesModel->where(['receivables_id' => $receivables_id])->update($data);
  561. if (!$res) {
  562. $errorMessage[] = $receivables_info['number'] . '"转移失败,错误原因:数据出错;';
  563. continue;
  564. }
  565. updateActionLog($userInfo['id'], 'crm_receivables', $receivables_id, '', '', '将回款转移给:' . $owner_user_info['realname']);
  566. RecordActionLog($userInfo['id'], 'crm_receivables', 'transfer',$receivables_info['number'], '','','将回款:'.$receivables_info['number'].'转移给:' . $owner_user_info['realname']);
  567. }
  568. if (!$errorMessage) {
  569. return resultArray(['data' => '转移成功']);
  570. } else {
  571. return resultArray(['error' => $errorMessage]);
  572. }
  573. }
  574. /**
  575. * 系统信息
  576. *
  577. * @return \think\response\Json
  578. * @throws \think\db\exception\DataNotFoundException
  579. * @throws \think\db\exception\ModelNotFoundException
  580. * @throws \think\exception\DbException
  581. */
  582. public function system()
  583. {
  584. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  585. $receivablesModel = new \app\crm\model\Receivables();
  586. $data = $receivablesModel->getSystemInfo($this->param['id']);
  587. return resultArray(['data' => $data]);
  588. }
  589. /**
  590. * table标签栏数量
  591. *
  592. * @return \think\response\Json
  593. * @throws \think\db\exception\DataNotFoundException
  594. * @throws \think\db\exception\ModelNotFoundException
  595. * @throws \think\exception\DbException
  596. */
  597. public function count()
  598. {
  599. if (empty($this->param['receivables_id'])) return resultArray(['error' => '参数错误!']);
  600. $receivablesId = $this->param['receivables_id'];
  601. # 团队成员
  602. $receivables = Db::name('crm_receivables')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('receivables_id', $receivablesId)->find();
  603. $receivables['ro_user_id'] = stringToArray($receivables['ro_user_id']);
  604. $receivables['rw_user_id'] = stringToArray($receivables['rw_user_id']);
  605. $receivables['owner_user_id'] = [$receivables['owner_user_id']];
  606. $teamCount = array_filter(array_unique(array_merge($receivables['ro_user_id'], $receivables['rw_user_id'], $receivables['owner_user_id'])));
  607. # 附件
  608. $fileCount = Db::name('crm_receivables_file')->alias('receivables')->join('__ADMIN_FILE__ file', 'file.file_id = receivables.file_id', 'LEFT')->where('receivables_id', $this->param['receivables_id'])->count();
  609. return resultArray(['data' => ['fileCount' => $fileCount,'memberCount' => count($teamCount)]]);
  610. }
  611. }