Receivables.php 26KB

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