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