123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <?php
  2. /**
  3. * 发票控制器
  4. *
  5. * @author qifan
  6. * @date 2020-12-07
  7. */
  8. namespace app\crm\controller;
  9. use app\admin\controller\ApiCommon;
  10. use app\admin\model\Message;
  11. use app\admin\model\User;
  12. use app\crm\logic\InvoiceLogic;
  13. use app\crm\model\NumberSequence;
  14. use app\crm\traits\AutoNumberTrait;
  15. use think\Db;
  16. use think\Hook;
  17. use think\Request;
  18. class Invoice extends ApiCommon
  19. {
  20. use AutoNumberTrait;
  21. /**
  22. * 用于判断权限
  23. * @permission 无限制
  24. * @allow 登录用户可访问
  25. * @other 其他根据系统设置
  26. **/
  27. public function _initialize()
  28. {
  29. $action = [
  30. 'permission' => [],
  31. 'allow' => ['check', 'revokecheck', 'count', 'read']
  32. ];
  33. Hook::listen('check_auth',$action);
  34. $request = Request::instance();
  35. $a = strtolower($request->action());
  36. if (!in_array($a, $action['permission'])) {
  37. parent::_initialize();
  38. }
  39. }
  40. /**
  41. * 列表
  42. *
  43. * @param InvoiceLogic $invoiceLogic
  44. * @return \think\response\Json
  45. * @throws \think\exception\DbException
  46. */
  47. public function index(InvoiceLogic $invoiceLogic)
  48. {
  49. $param = $this->param;
  50. $param['user_id'] = $this->userInfo['id'];
  51. $data = $invoiceLogic->index($param, true);
  52. return resultArray(['data' => $data]);
  53. }
  54. /**
  55. * 创建
  56. *
  57. * @param InvoiceLogic $invoiceLogic
  58. * @return \think\response\Json
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. * @throws \think\exception\DbException
  62. */
  63. public function save(InvoiceLogic $invoiceLogic)
  64. {
  65. if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择客户!']);
  66. if (empty($this->param['contract_id'])) return resultArray(['error' => '请选择合同!']);
  67. if (empty($this->param['invoice_money'])) return resultArray(['error' => '请填写开票金额!']);
  68. if (empty($this->param['invoice_type'])) return resultArray(['error' => '请选择开票类型!']);
  69. if (empty($this->param['title_type'])) return resultArray(['error' => '请选择抬头类型!']);
  70. $param = $this->param;
  71. $userId = $this->userInfo['id'];
  72. # 审批是否停用
  73. $examineStatus = $param['examineStatus'];
  74. # 删除无用参数
  75. unset($param['examineStatus']);
  76. unset($param['customer_name']);
  77. unset($param['contract_money']);
  78. unset($param['contract_number']);
  79. # 设置创建人负责人ID
  80. $param['create_user_id'] = $userId;
  81. $param['owner_user_id'] = $userId;
  82. # 创建更新日期
  83. $param['create_time'] = time();
  84. $param['update_time'] = time();
  85. # 自动设置发票编号
  86. $numberInfo = [];
  87. if (empty($param['invoice_apple_number'])) {
  88. $numberInfo = $this->getAutoNumbers(4);
  89. if (empty($numberInfo['number'])) return resultArray(['error' => '请填写发票编号!']);
  90. $param['invoice_apple_number'] = $numberInfo['number'];
  91. }
  92. # 检查发票编号是否重复
  93. if ($invoiceLogic->getInvoiceId(['invoice_apple_number' => $param['invoice_apple_number']])) {
  94. return resultArray(['error' => '发票编号重复!']);
  95. }
  96. if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
  97. $examineStepModel = new \app\admin\model\ExamineStep();
  98. # 审核判断(是否有符合条件的审批流)
  99. $examineFlowModel = new \app\admin\model\ExamineFlow();
  100. if (!$examineFlowModel->checkExamine($userId, 'crm_invoice')) {
  101. return resultArray(['error' => '暂无审批人,无法创建']);
  102. }
  103. # 添加审批相关信息
  104. $examineFlowData = $examineFlowModel->getFlowByTypes($userId, 'crm_invoice');
  105. if (!$examineFlowData) {
  106. return resultArray(['error' => '无可用审批流,请联系管理员']);
  107. }
  108. $param['flow_id'] = $examineFlowData['flow_id'];
  109. # 获取审批人信息
  110. if ($examineFlowData['config'] == 1) {
  111. # 固定审批流
  112. $nextStepData = $examineStepModel->nextStepUser($userId, $examineFlowData['flow_id'], 'crm_invoice', 0, 0, 0);
  113. $next_user_ids = arrayToString($nextStepData['next_user_ids']) ? : '';
  114. $check_user_id = $next_user_ids ? : [];
  115. $param['order_id'] = 1;
  116. } else {
  117. # 授权审批流
  118. $check_user_id = $param['check_user_id'] ? ','.$param['check_user_id'].',' : '';
  119. }
  120. if (!$check_user_id) {
  121. return resultArray(['error' => '无可用审批人,请联系管理员']);
  122. }
  123. $param['check_user_id'] = is_array($check_user_id) ? ','.implode(',',$check_user_id).',' : $check_user_id;
  124. } else {
  125. # 审批流停用,将状态改为审核通过
  126. $param['check_status'] = 2;
  127. }
  128. if (!$invoice_id = $invoiceLogic->save($param)) {
  129. return resultArray(['error' => '创建失败!']);
  130. }
  131. $send_user_id = stringToArray($param['check_user_id']);
  132. (new Message())->send(
  133. Message::INVOICE_TO_DO,
  134. [
  135. 'title' => $param['invoice_apple_number'],
  136. 'action_id' => $invoice_id
  137. ],
  138. $send_user_id
  139. );
  140. # 更新crm_number_sequence表中的last_date、create_time字段
  141. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  142. updateActionLog($param['create_user_id'], 'crm_invoice', $invoice_id, '', '', '创建了发票');
  143. RecordActionLog($param['create_user_id'],'crm_invoice','save',$param['invoice_apple_number'],'','','新增了发票'.$param['invoice_apple_number']);
  144. # 创建待办事项的关联数据
  145. $checkUserIds = db('crm_invoice')->where('invoice_id', $invoice_id)->value('check_user_id');
  146. $checkUserIdArray = stringToArray($checkUserIds);
  147. $dealtData = [];
  148. foreach ($checkUserIdArray AS $kk => $vv) {
  149. $dealtData[] = [
  150. 'types' => 'crm_invoice',
  151. 'types_id' => $invoice_id,
  152. 'user_id' => $vv
  153. ];
  154. }
  155. if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
  156. return resultArray(['data' => '创建成功!']);
  157. }
  158. /**
  159. * 详情
  160. *
  161. * @param InvoiceLogic $invoiceLogic
  162. * @return \think\response\Json
  163. * @throws \think\Exception
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. * @throws \think\exception\DbException
  167. */
  168. public function read(InvoiceLogic $invoiceLogic)
  169. {
  170. $invoiceId = $this->param['id'];
  171. $isUpdate = !empty($this->param['is_update']) ? $this->param['is_update'] : 0;
  172. $userInfo = $this->userInfo;
  173. $data = $invoiceLogic->read($invoiceId, $isUpdate);
  174. $readStatus = false;
  175. # 角色权限
  176. $authArray = db('admin_access')->alias('access')
  177. ->join('__ADMIN_GROUP__ group', 'group.id = access.group_id', 'left')
  178. ->where('access.user_id', $userInfo['id'])->column('group.rules');
  179. # 详情权限ID
  180. $invoiceAuthId = db('admin_rule')->where(['types' => 2, 'name' => 'invoice', 'level' => 2])->value('id');
  181. $invoiceReadAuthId = db('admin_rule')->where(['types' => 2, 'name' => 'read', 'level' => 3, 'pid' => $invoiceAuthId])->value('id');
  182. foreach ($authArray AS $key => $value) {
  183. if (!empty($value) && in_array($invoiceReadAuthId, stringToArray($value))) $readStatus = true;
  184. }
  185. if (!isSuperAdministrators($userInfo['id']) && $readStatus === false) {
  186. $authData['dataAuth'] = (int)0;
  187. return resultArray(['data' => $authData]);
  188. }
  189. return resultArray(['data' => $data]);
  190. }
  191. /**
  192. * 编辑
  193. *
  194. * @param InvoiceLogic $invoiceLogic
  195. * @return \think\response\Json
  196. * @throws \think\db\exception\DataNotFoundException
  197. * @throws \think\db\exception\ModelNotFoundException
  198. * @throws \think\exception\DbException
  199. */
  200. public function update(InvoiceLogic $invoiceLogic)
  201. {
  202. $param = $this->param;
  203. if (empty($param['invoice_id'])) return resultArray(['error' => '缺少发票ID!']);
  204. if (empty($param['invoice_type'])) return resultArray(['error' => '请选择开票类型!']);
  205. if (empty($param['title_type'])) return resultArray(['error' => '请选择抬头类型!']);
  206. $userId = $this->userInfo['id'];
  207. $dataInfo = $this->get($param['invoice_id']);
  208. # 审批是否停用
  209. $examineStatus = $param['examineStatus'];
  210. # 删除无用参数
  211. unset($param['examineStatus']);
  212. unset($param['customer_name']);
  213. unset($param['contract_money']);
  214. unset($param['contract_number']);
  215. # 设置负责人ID
  216. $param['update_time'] = time();
  217. # 已进行审批,不能编辑
  218. // $dataInfo = $invoiceLogic->read($param['invoice_id'], '');
  219. // if (!$dataInfo) {
  220. // $this->error = '数据不存在或已删除';
  221. // return false;
  222. // }
  223. $checkStatus = $invoiceLogic->getExamineStatus($param['invoice_id']);
  224. if (!in_array($checkStatus, ['3', '4', '5', '6'])) return resultArray(['error' => '当前状态为审批中或已审批通过,不可编辑']);
  225. # 自动设置发票编号
  226. $numberInfo = [];
  227. if (empty($param['invoice_apple_number'])) {
  228. $numberInfo = $this->getAutoNumbers(4);
  229. if (empty($numberInfo['number'])) return resultArray(['error' => '请填写发票编号!']);
  230. $param['invoice_apple_number'] = $numberInfo['number'];
  231. }
  232. # 检查发票编号是否重复
  233. $invoiceWhere['invoice_apple_number'] = $param['invoice_apple_number'];
  234. $invoiceWhere['invoice_id'] = ['neq', $this->param['invoice_id']];
  235. if ($invoiceLogic->getInvoiceId($invoiceWhere)) return resultArray(['error' => '发票编号重复!']);
  236. if ($param['is_draft'] || (!empty($param['check_status']) && $param['check_status'] == 5)) {
  237. //保存为草稿
  238. $param['check_status'] = 5; //草稿(未提交)
  239. $param['check_user_id'] = $param['check_user_id'] ? ','.$param['check_user_id'].',' : '';
  240. } else {
  241. # 将合同审批状态至为待审核,提交后重新进行审批
  242. if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
  243. # 审核判断(是否有符合条件的审批流)
  244. $examineFlowModel = new \app\admin\model\ExamineFlow();
  245. $examineStepModel = new \app\admin\model\ExamineStep();
  246. if (!$examineFlowModel->checkExamine($userId, 'crm_invoice')) {
  247. return resultArray(['error' => '暂无审批人,无法创建']);
  248. }
  249. # 添加审批相关信息
  250. $examineFlowData = $examineFlowModel->getFlowByTypes($userId, 'crm_invoice');
  251. if (!$examineFlowData) {
  252. return resultArray(['error' => '无可用审批流,请联系管理员']);
  253. }
  254. $param['flow_id'] = $examineFlowData['flow_id'];
  255. # 获取审批人信息
  256. if ($examineFlowData['config'] == 1) {
  257. # 固定审批流
  258. $nextStepData = $examineStepModel->nextStepUser($userId, $examineFlowData['flow_id'], 'crm_invoice', 0, 0, 0);
  259. $next_user_ids = arrayToString($nextStepData['next_user_ids']) ? : '';
  260. $check_user_id = $next_user_ids ? : [];
  261. $param['order_id'] = 1;
  262. } else {
  263. # 授权审批流
  264. $check_user_id = $param['check_user_id'] ? ','.$param['check_user_id'].',' : '';
  265. }
  266. if ($param['is_draft']) {
  267. //保存为草稿
  268. $param['check_status'] = 5;
  269. $param['check_user_id'] = $param['check_user_id'] ? ','.$param['check_user_id'].',' : '';
  270. } else {
  271. if (!$check_user_id) {
  272. return resultArray(['error' => '无可用审批人,请联系管理员']);
  273. }
  274. $param['check_user_id'] = is_array($check_user_id) ? ','.implode(',',$check_user_id).',' : $check_user_id;
  275. $param['check_status'] = 0;
  276. }
  277. $param['flow_user_id'] = '';
  278. }
  279. }
  280. if (!$invoiceLogic->update($param)) {
  281. return resultArray(['error' => '编辑失败!']);
  282. }
  283. //将审批记录至为无效
  284. $examineRecordModel = new \app\admin\model\ExamineRecord();
  285. $examineRecordModel->setEnd(['types' => 'crm_invoice','types_id' => $param['invoice_id']]);
  286. # 更新crm_number_sequence表中的last_date、create_time字段
  287. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  288. //修改记录
  289. updateActionLog($param['user_id'], 'crm_invoice', $param['invoice_id'], $dataInfo, $param);
  290. RecordActionLog($param['user_id'], 'crm_invoice', 'update',$dataInfo['invoice_apple_number'], $dataInfo, $param);
  291. # 删除待办事项的关联数据
  292. db('crm_dealt_relation')->where(['types' => ['eq', 'crm_invoice'], 'types_id' => ['eq', $param['invoice_id']]])->delete();
  293. # 创建待办事项的关联数据
  294. $checkUserIds = db('crm_invoice')->where('invoice_id', $param['invoice_id'])->value('check_user_id');
  295. $checkUserIdArray = stringToArray($checkUserIds);
  296. $dealtData = [];
  297. foreach ($checkUserIdArray AS $kk => $vv) {
  298. $dealtData[] = [
  299. 'types' => 'crm_invoice',
  300. 'types_id' => $param['invoice_id'],
  301. 'user_id' => $vv
  302. ];
  303. }
  304. if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
  305. return resultArray(['data' => '编辑成功!']);
  306. }
  307. /**
  308. * 删除
  309. *
  310. * @param InvoiceLogic $invoiceLogic
  311. * @return \think\response\Json
  312. * @throws \think\db\exception\DataNotFoundException
  313. * @throws \think\db\exception\ModelNotFoundException
  314. * @throws \think\exception\DbException
  315. */
  316. public function delete(InvoiceLogic $invoiceLogic)
  317. {
  318. $actionRecordModel = new \app\admin\model\ActionRecord();
  319. $fileModel = new \app\admin\model\File();
  320. $idArray = $this->param['id'];
  321. $userinfo = $this->userInfo['id'];
  322. if (!is_array($idArray)) return resultArray(['error' => '发票ID类型错误!']);
  323. $idString = implode(',', $idArray);
  324. $status = true;
  325. if (!isSuperAdministrators($userinfo['id'])) {
  326. $list = $invoiceLogic->getExamineStatus($idString, true);
  327. foreach ($list AS $key => $value) {
  328. if (!in_array($value['check_status'], [4, 5])) {
  329. $status = false;
  330. break;
  331. }
  332. }
  333. }
  334. if (!$status) return resultArray(['error' => '不能删除审批中或审批结束的发票信息!']);
  335. $dataInfo=db('crm_invoice')->where('invoice_id',['in',$idArray])->select();
  336. if (!$invoiceLogic->delete($idArray)) return resultArray(['error' => '删除失败!']);
  337. # 删除附件
  338. $fileModel->delRFileByModule('crm_invoice', $idArray);
  339. //删除关联操作记录
  340. $actionRecordModel->delDataById(['types'=>'crm_invoice','action_id'=>$idArray]);
  341. $userInfo = $this->userInfo;
  342. foreach ($dataInfo as $k => $v) {
  343. RecordActionLog($userInfo['id'], 'crm_contacts', 'delete', $v['invoice_apple_number'], '', '', '删除了回款:' . $v['invoice_apple_number']);
  344. }
  345. return resultArray(['data' => '删除成功!']);
  346. }
  347. /**
  348. * 转移(变更负责人)
  349. *
  350. * @param InvoiceLogic $invoiceLogic
  351. * @return \think\response\Json
  352. */
  353. public function transfer(InvoiceLogic $invoiceLogic)
  354. {
  355. $ownerUserId = $this->param['owner_user_id'];
  356. $invoiceIds = $this->param['invoice_id'];
  357. $userModel = new \app\admin\model\User();
  358. $userInfo=$this->userInfo;
  359. if (empty($ownerUserId)) return resultArray(['error' => '请选择负责人!']);
  360. if (empty($invoiceIds)) return resultArray(['error' => '请选择发票!']);
  361. if (!is_array($invoiceIds)) return resultArray(['error' => '发票ID类型错误!']);
  362. if ($invoiceLogic->transfer($invoiceIds, $ownerUserId) === false) return resultArray(['error' => '操作失败!']);
  363. $owner_user_info = $userModel->getUserById($ownerUserId);
  364. foreach ($invoiceIds as $v){
  365. $invoice_info=db('crm_invoice')->where('invoice_id',$v)->find();
  366. updateActionLog($userInfo['id'], 'crm_invoice', $v, '', '', '将发票转移给:' . $owner_user_info['realname']);
  367. RecordActionLog($userInfo['id'], 'crm_invoice', 'transfer',$invoice_info['invoice_apple_number'], '','','将发票:'.$invoice_info['invoice_apple_number'].'转移给:' . $owner_user_info['realname']);
  368. }
  369. return resultArray(['data' => '操作成功!']);
  370. }
  371. /**
  372. * 设置开票
  373. *
  374. * @param InvoiceLogic $invoiceLogic
  375. * @return \think\response\Json
  376. */
  377. public function setInvoice(InvoiceLogic $invoiceLogic)
  378. {
  379. if (empty($this->param['invoice_id'])) return resultArray(['error' => '参数错误!']);
  380. // if (empty($this->param['invoice_number'])) return resultArray(['error' => '请填写发票号码!']);
  381. // if (empty($this->param['logistics_number'])) return resultArray(['error' => '请填写物流单号!']);
  382. // if (empty($this->param['real_invoice_date'])) return resultArray(['error' => '请选择开票日期!']);
  383. $this->param['real_invoice_date'] = !empty($this->param['real_invoice_date']) ? $this->param['real_invoice_date'] : date('Y-m-d');
  384. # 开票状态
  385. $this->param['invoice_status'] = 1;
  386. # 设置开票信息
  387. if (!$invoiceLogic->setInvoice($this->param)) return resultArray(['error' => '操作失败!']);
  388. return resultArray(['data' => '操作成功!']);
  389. }
  390. /**
  391. * 审核
  392. *
  393. * @param InvoiceLogic $invoiceLogic
  394. * @return \think\response\Json
  395. * @throws \think\db\exception\DataNotFoundException
  396. * @throws \think\db\exception\ModelNotFoundException
  397. * @throws \think\exception\DbException
  398. */
  399. public function check(InvoiceLogic $invoiceLogic)
  400. {
  401. $param = $this->param;
  402. $user_id = $this->userInfo['id'];
  403. $examineStepModel = new \app\admin\model\ExamineStep();
  404. $examineRecordModel = new \app\admin\model\ExamineRecord();
  405. $examineFlowModel = new \app\admin\model\ExamineFlow();
  406. $invoiceData = [];
  407. $invoiceData['invoice_id'] = $param['id'];
  408. $invoiceData['update_time'] = time();
  409. $invoiceData['check_status'] = 1;
  410. # 权限判断
  411. if (!$examineStepModel->checkExamine($user_id, 'crm_invoice', $param['id'])) {
  412. return resultArray(['error' => $examineStepModel->getError()]);
  413. }
  414. # 审批主体详情
  415. $dataInfo = $invoiceLogic->getExamineInfo($param['id']);
  416. $flowInfo = $examineFlowModel->getDataById($dataInfo['flow_id']);
  417. # 1审批结束
  418. $is_end = 0;
  419. # 1通过,0驳回
  420. $status = !empty($param['status']) && $param['status'] == 1 ? 1 : 0;
  421. # 审批记录
  422. $checkData = [];
  423. $checkData['check_user_id'] = $user_id;
  424. $checkData['types'] = 'crm_invoice';
  425. $checkData['types_id'] = $param['id'];
  426. $checkData['check_time'] = time();
  427. $checkData['content'] = $param['content'];
  428. $checkData['flow_id'] = $dataInfo['flow_id'];
  429. $checkData['order_id'] = $dataInfo['order_id'] ? : 1;
  430. $checkData['status'] = $status;
  431. if ($status == 1) {
  432. if ($flowInfo['config'] == 1) {
  433. # 固定流程
  434. # 获取下一审批信息
  435. $nextStepData = $examineStepModel->nextStepUser($dataInfo['owner_user_id'], $dataInfo['flow_id'], 'crm_invoice', $param['id'], $dataInfo['order_id'], $user_id);
  436. $next_user_ids = $nextStepData['next_user_ids'] ? : [];
  437. $invoiceData['order_id'] = $nextStepData['order_id'] ? : '';
  438. if (!$next_user_ids) {
  439. $is_end = 1;
  440. # 审批结束
  441. $checkData['check_status'] = !empty($status) ? 2 : 3;
  442. $invoiceData['check_user_id'] = '';
  443. } else {
  444. # 修改主体相关审批信息
  445. $invoiceData['check_user_id'] = arrayToString($next_user_ids);
  446. }
  447. } else {
  448. # 自选流程
  449. $is_end = $param['is_end'] ? 1 : '';
  450. $check_user_id = $param['check_user_id'] ? : '';
  451. if ($is_end !== 1 && empty($check_user_id)) {
  452. return resultArray(['error' => '请选择下一审批人']);
  453. }
  454. $invoiceData['check_user_id'] = arrayToString($param['check_user_id']);
  455. }
  456. if ($is_end == 1) {
  457. $checkData['check_status'] = !empty($status) ? 2 : 3;
  458. $invoiceData['check_user_id'] = '';
  459. $invoiceData['check_status'] = 2;
  460. }
  461. } else {
  462. # 审批驳回
  463. $is_end = 1;
  464. $invoiceData['check_status'] = 3;
  465. }
  466. # 已审批人ID
  467. $invoiceData['flow_user_id'] = stringToArray($dataInfo['flow_user_id']) ? arrayToString(array_merge(stringToArray($dataInfo['flow_user_id']),[$user_id])) : arrayToString([$user_id]);
  468. $resContract = $invoiceLogic->setExamineInfo($invoiceData);
  469. if ($resContract) {
  470. # 审批记录
  471. $examineRecordModel->createData($checkData);
  472. # 发送站内信
  473. if ($is_end == 1 && !empty($status)) {
  474. # 审批流程结束,将审批通过消息告知负责人
  475. (new Message())->send(
  476. Message::INVOICE_PASS,
  477. [
  478. 'title' => $dataInfo['invoice_apple_number'],
  479. 'action_id' => $param['id']
  480. ],
  481. stringToArray($dataInfo['owner_user_id'])
  482. );
  483. } else {
  484. if (!empty($status)) {
  485. # 审批流程未结束,将待审批提醒发送给下一级负责人
  486. (new Message())->send(
  487. Message::INVOICE_TO_DO,
  488. [
  489. 'from_user' => User::where(['id' => $dataInfo['owner_user_id']])->value('realname'),
  490. 'title' => $dataInfo['invoice_apple_number'],
  491. 'action_id' => $param['id']
  492. ],
  493. stringToArray($invoiceData['check_user_id'])
  494. );
  495. } else {
  496. # 将审批被驳回的消息告知负责人
  497. (new Message())->send(
  498. Message::INVOICE_REJECT,
  499. [
  500. 'title' => $dataInfo['invoice_apple_number'],
  501. 'action_id' => $param['id']
  502. ],
  503. stringToArray($dataInfo['owner_user_id'])
  504. );
  505. }
  506. }
  507. return resultArray(['data' => '审批成功']);
  508. } else {
  509. return resultArray(['error' => '审批失败,请重试!']);
  510. }
  511. }
  512. /**
  513. * 撤销审核
  514. *
  515. * @param InvoiceLogic $invoiceLogic
  516. * @return \think\response\Json
  517. * @throws \think\db\exception\DataNotFoundException
  518. * @throws \think\db\exception\ModelNotFoundException
  519. * @throws \think\exception\DbException
  520. */
  521. public function revokeCheck(InvoiceLogic $invoiceLogic)
  522. {
  523. $invoiceId = $this->param['id'];
  524. $content = $this->param['content'];
  525. $realname = $this->userInfo['realname'];
  526. $userInfo = $this->userInfo;
  527. $user_id = $userInfo['id'];
  528. if (empty($invoiceId)) return resultArray(['error' => '请选择要撤回审核的发票!']);
  529. $examineInfo = $invoiceLogic->getExamineInfo($invoiceId);
  530. if ($examineInfo['check_status'] == 2) {
  531. return resultArray(['error' => '已审批结束,不能撤销']);
  532. }
  533. if ($examineInfo['check_status'] == 4) {
  534. return resultArray(['error' => '无需撤销']);
  535. }
  536. $userModel = new \app\admin\model\User();
  537. $admin_user_ids = $userModel->getAdminId();
  538. if ($examineInfo['owner_user_id'] !== $user_id && !in_array($user_id, $admin_user_ids)) {
  539. return resultArray(['error' => '没有权限']);
  540. }
  541. # 修改发票审核状态
  542. if (!$invoiceLogic->update(['invoice_id' => $invoiceId, 'check_status' => 4, 'check_user_id' => '', 'flow_user_id' => ''])) {
  543. return resultArray(['error' => '操作失败!']);
  544. }
  545. # 添加撤销审核的记录
  546. $invoiceLogic->createExamineRecord($invoiceId, $examineInfo, $realname, $content, $user_id);
  547. return resultArray(['data' => '操作成功!']);
  548. }
  549. /**
  550. * table栏数量统计
  551. *
  552. * @return \think\response\Json
  553. * @throws \think\Exception
  554. */
  555. public function count()
  556. {
  557. if (empty($this->param['invoice_id'])) return resultArray(['error' => '参数错误!']);
  558. # 附件
  559. $fileCount = Db::name('crm_invoice_file')->alias('invoice')->join('__ADMIN_FILE__ file', 'file.file_id = invoice.file_id', 'LEFT')->where('invoice_id', $this->param['invoice_id'])->count();
  560. return resultArray(['data' => ['fileCount' => $fileCount]]);
  561. }
  562. /**
  563. * 重置开票信息
  564. *
  565. * @param InvoiceLogic $invoiceLogic
  566. * @return \think\response\Json
  567. */
  568. public function resetInvoiceStatus(InvoiceLogic $invoiceLogic)
  569. {
  570. if (empty($this->param['invoice_id'])) resultArray(['error' => '参数错误!']);
  571. $userInfo = $this->userInfo;
  572. $this->param['real_invoice_date'] = !empty($this->param['real_invoice_date']) ? $this->param['real_invoice_date'] : date('Y-m-d');
  573. # 开票状态
  574. $this->param['invoice_status'] = 1;
  575. $invoice_info=db('crm_invoice')->where('invoice_id',$this->param['invoice_id'])->find();
  576. if (!$invoiceLogic->setInvoice($this->param)) return resultArray(['error' => '操作失败!']);
  577. RecordActionLog($userInfo['id'], 'crm_invoice', 'update',$invoice_info['invoice_apple_number'], '','','将发票:'.$invoice_info['invoice_apple_number'].'重置开票状态');
  578. return resultArray(['data' => '操作成功!']);
  579. }
  580. }