Receivables.php 26KB

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