Invoice.php 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. if (empty($this->param['examineStatus'])) return resultArray(['error' => '缺少审批状态!']);
  71. $param = $this->param;
  72. $userId = $this->userInfo['id'];
  73. # 审批是否停用
  74. $examineStatus = $param['examineStatus'];
  75. # 删除无用参数
  76. unset($param['examineStatus']);
  77. unset($param['customer_name']);
  78. unset($param['contract_money']);
  79. unset($param['contract_number']);
  80. # 设置创建人负责人ID
  81. $param['create_user_id'] = $userId;
  82. $param['owner_user_id'] = $userId;
  83. # 创建更新日期
  84. $param['create_time'] = time();
  85. $param['update_time'] = time();
  86. # 自动设置发票编号
  87. $numberInfo = [];
  88. if (empty($param['invoice_apple_number'])) {
  89. $numberInfo = $this->getAutoNumbers(4);
  90. if (empty($numberInfo['number'])) return resultArray(['error' => '请填写发票编号!']);
  91. $param['invoice_apple_number'] = $numberInfo['number'];
  92. }
  93. # 检查发票编号是否重复
  94. if ($invoiceLogic->getInvoiceId(['invoice_apple_number' => $param['invoice_apple_number']])) {
  95. return resultArray(['error' => '发票编号重复!']);
  96. }
  97. if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
  98. $examineStepModel = new \app\admin\model\ExamineStep();
  99. # 审核判断(是否有符合条件的审批流)
  100. $examineFlowModel = new \app\admin\model\ExamineFlow();
  101. if (!$examineFlowModel->checkExamine($userId, 'crm_invoice')) {
  102. return resultArray(['error' => '暂无审批人,无法创建']);
  103. }
  104. # 添加审批相关信息
  105. $examineFlowData = $examineFlowModel->getFlowByTypes($userId, 'crm_invoice');
  106. if (!$examineFlowData) {
  107. return resultArray(['error' => '无可用审批流,请联系管理员']);
  108. }
  109. $param['flow_id'] = $examineFlowData['flow_id'];
  110. # 获取审批人信息
  111. if ($examineFlowData['config'] == 1) {
  112. # 固定审批流
  113. $nextStepData = $examineStepModel->nextStepUser($userId, $examineFlowData['flow_id'], 'crm_invoice', 0, 0, 0);
  114. $next_user_ids = arrayToString($nextStepData['next_user_ids']) ? : '';
  115. $check_user_id = $next_user_ids ? : [];
  116. $param['order_id'] = 1;
  117. } else {
  118. # 授权审批流
  119. $check_user_id = $param['check_user_id'] ? ','.$param['check_user_id'].',' : '';
  120. }
  121. if (!$check_user_id) {
  122. return resultArray(['error' => '无可用审批人,请联系管理员']);
  123. }
  124. $param['check_user_id'] = is_array($check_user_id) ? ','.implode(',',$check_user_id).',' : $check_user_id;
  125. } else {
  126. # 审批流停用,将状态改为审核通过
  127. $param['check_status'] = 2;
  128. }
  129. if (!$invoice_id = $invoiceLogic->save($param)) {
  130. return resultArray(['error' => '创建失败!']);
  131. }
  132. $send_user_id = stringToArray($param['check_user_id']);
  133. (new Message())->send(
  134. Message::INVOICE_TO_DO,
  135. [
  136. 'title' => $param['invoice_apple_number'],
  137. 'action_id' => $invoice_id
  138. ],
  139. $send_user_id
  140. );
  141. # 更新crm_number_sequence表中的last_date、create_time字段
  142. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  143. updateActionLog($param['create_user_id'], 'crm_invoice', $invoice_id, '', '', '创建了发票');
  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['customer_id'])) return resultArray(['error' => '请选择客户!']);
  205. // if (empty($param['contract_id'])) return resultArray(['error' => '请选择合同!']);
  206. // if (empty($param['invoice_money'])) return resultArray(['error' => '请填写开票金额!']);
  207. if (empty($param['invoice_type'])) return resultArray(['error' => '请选择开票类型!']);
  208. if (empty($param['title_type'])) return resultArray(['error' => '请选择抬头类型!']);
  209. if (empty($param['examineStatus'])) return resultArray(['error' => '缺少审批状态!']);
  210. $userId = $this->userInfo['id'];
  211. # 审批是否停用
  212. $examineStatus = $param['examineStatus'];
  213. # 删除无用参数
  214. unset($param['examineStatus']);
  215. unset($param['customer_name']);
  216. unset($param['contract_money']);
  217. unset($param['contract_number']);
  218. # 设置负责人ID
  219. $param['update_time'] = time();
  220. # 已进行审批,不能编辑
  221. // $dataInfo = $invoiceLogic->read($param['invoice_id'], '');
  222. // if (!$dataInfo) {
  223. // $this->error = '数据不存在或已删除';
  224. // return false;
  225. // }
  226. $checkStatus = $invoiceLogic->getExamineStatus($param['invoice_id']);
  227. if (!in_array($checkStatus, ['3', '4', '5', '6'])) return resultArray(['error' => '当前状态为审批中或已审批通过,不可编辑']);
  228. # 自动设置发票编号
  229. $numberInfo = [];
  230. if (empty($param['invoice_apple_number'])) {
  231. $numberInfo = $this->getAutoNumbers(4);
  232. if (empty($numberInfo['number'])) return resultArray(['error' => '请填写发票编号!']);
  233. $param['invoice_apple_number'] = $numberInfo['number'];
  234. }
  235. # 检查发票编号是否重复
  236. $invoiceWhere['invoice_apple_number'] = $param['invoice_apple_number'];
  237. $invoiceWhere['invoice_id'] = ['neq', $this->param['invoice_id']];
  238. if ($invoiceLogic->getInvoiceId($invoiceWhere)) return resultArray(['error' => '发票编号重复!']);
  239. if ($param['is_draft'] || (!empty($param['check_status']) && $param['check_status'] == 5)) {
  240. //保存为草稿
  241. $param['check_status'] = 5; //草稿(未提交)
  242. $param['check_user_id'] = $param['check_user_id'] ? ','.$param['check_user_id'].',' : '';
  243. } else {
  244. # 将合同审批状态至为待审核,提交后重新进行审批
  245. if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
  246. # 审核判断(是否有符合条件的审批流)
  247. $examineFlowModel = new \app\admin\model\ExamineFlow();
  248. $examineStepModel = new \app\admin\model\ExamineStep();
  249. if (!$examineFlowModel->checkExamine($userId, 'crm_invoice')) {
  250. return resultArray(['error' => '暂无审批人,无法创建']);
  251. }
  252. # 添加审批相关信息
  253. $examineFlowData = $examineFlowModel->getFlowByTypes($userId, 'crm_invoice');
  254. if (!$examineFlowData) {
  255. return resultArray(['error' => '无可用审批流,请联系管理员']);
  256. }
  257. $param['flow_id'] = $examineFlowData['flow_id'];
  258. # 获取审批人信息
  259. if ($examineFlowData['config'] == 1) {
  260. # 固定审批流
  261. $nextStepData = $examineStepModel->nextStepUser($userId, $examineFlowData['flow_id'], 'crm_invoice', 0, 0, 0);
  262. $next_user_ids = arrayToString($nextStepData['next_user_ids']) ? : '';
  263. $check_user_id = $next_user_ids ? : [];
  264. $param['order_id'] = 1;
  265. } else {
  266. # 授权审批流
  267. $check_user_id = $param['check_user_id'] ? ','.$param['check_user_id'].',' : '';
  268. }
  269. if ($param['is_draft']) {
  270. //保存为草稿
  271. $param['check_status'] = 5;
  272. $param['check_user_id'] = $param['check_user_id'] ? ','.$param['check_user_id'].',' : '';
  273. } else {
  274. if (!$check_user_id) {
  275. return resultArray(['error' => '无可用审批人,请联系管理员']);
  276. }
  277. $param['check_user_id'] = is_array($check_user_id) ? ','.implode(',',$check_user_id).',' : $check_user_id;
  278. $param['check_status'] = 0;
  279. }
  280. $param['flow_user_id'] = '';
  281. }
  282. }
  283. if (!$invoiceLogic->update($param)) {
  284. return resultArray(['error' => '编辑失败!']);
  285. }
  286. //将审批记录至为无效
  287. $examineRecordModel = new \app\admin\model\ExamineRecord();
  288. $examineRecordModel->setEnd(['types' => 'crm_invoice','types_id' => $param['invoice_id']]);
  289. # 更新crm_number_sequence表中的last_date、create_time字段
  290. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  291. //修改记录
  292. // updateActionLog($param['user_id'], 'crm_invoice', $param['invoice_id'], $dataInfo, $param);
  293. # 删除待办事项的关联数据
  294. db('crm_dealt_relation')->where(['types' => ['eq', 'crm_invoice'], 'types_id' => ['eq', $param['invoice_id']]])->delete();
  295. # 创建待办事项的关联数据
  296. $checkUserIds = db('crm_invoice')->where('invoice_id', $param['invoice_id'])->value('check_user_id');
  297. $checkUserIdArray = stringToArray($checkUserIds);
  298. $dealtData = [];
  299. foreach ($checkUserIdArray AS $kk => $vv) {
  300. $dealtData[] = [
  301. 'types' => 'crm_invoice',
  302. 'types_id' => $param['invoice_id'],
  303. 'user_id' => $vv
  304. ];
  305. }
  306. if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
  307. return resultArray(['data' => '编辑成功!']);
  308. }
  309. /**
  310. * 删除
  311. *
  312. * @param InvoiceLogic $invoiceLogic
  313. * @return \think\response\Json
  314. * @throws \think\db\exception\DataNotFoundException
  315. * @throws \think\db\exception\ModelNotFoundException
  316. * @throws \think\exception\DbException
  317. */
  318. public function delete(InvoiceLogic $invoiceLogic)
  319. {
  320. $actionRecordModel = new \app\admin\model\ActionRecord();
  321. $fileModel = new \app\admin\model\File();
  322. $idArray = $this->param['id'];
  323. $userinfo = $this->userInfo['id'];
  324. if (!is_array($idArray)) return resultArray(['error' => '发票ID类型错误!']);
  325. $idString = implode(',', $idArray);
  326. $status = true;
  327. if (!isSuperAdministrators($userinfo['id'])) {
  328. $list = $invoiceLogic->getExamineStatus($idString, true);
  329. foreach ($list AS $key => $value) {
  330. if (!in_array($value['check_status'], [4, 5])) {
  331. $status = false;
  332. break;
  333. }
  334. }
  335. }
  336. if (!$status) return resultArray(['error' => '不能删除审批中或审批结束的发票信息!']);
  337. if (!$invoiceLogic->delete($idArray)) return resultArray(['error' => '删除失败!']);
  338. # 删除附件
  339. $fileModel->delRFileByModule('crm_invoice', $idArray);
  340. //删除关联操作记录
  341. $actionRecordModel->delDataById(['types'=>'crm_invoice','action_id'=>$idArray]);
  342. return resultArray(['data' => '删除成功!']);
  343. }
  344. /**
  345. * 转移(变更负责人)
  346. *
  347. * @param InvoiceLogic $invoiceLogic
  348. * @return \think\response\Json
  349. */
  350. public function transfer(InvoiceLogic $invoiceLogic)
  351. {
  352. $ownerUserId = $this->param['owner_user_id'];
  353. $invoiceIds = $this->param['invoice_id'];
  354. if (empty($ownerUserId)) return resultArray(['error' => '请选择负责人!']);
  355. if (empty($invoiceIds)) return resultArray(['error' => '请选择发票!']);
  356. if (!is_array($invoiceIds)) return resultArray(['error' => '发票ID类型错误!']);
  357. if ($invoiceLogic->transfer($invoiceIds, $ownerUserId) === false) return resultArray(['error' => '操作失败!']);
  358. return resultArray(['data' => '操作成功!']);
  359. }
  360. /**
  361. * 设置开票
  362. *
  363. * @param InvoiceLogic $invoiceLogic
  364. * @return \think\response\Json
  365. */
  366. public function setInvoice(InvoiceLogic $invoiceLogic)
  367. {
  368. if (empty($this->param['invoice_id'])) return resultArray(['error' => '参数错误!']);
  369. // if (empty($this->param['invoice_number'])) return resultArray(['error' => '请填写发票号码!']);
  370. // if (empty($this->param['logistics_number'])) return resultArray(['error' => '请填写物流单号!']);
  371. // if (empty($this->param['real_invoice_date'])) return resultArray(['error' => '请选择开票日期!']);
  372. $this->param['real_invoice_date'] = !empty($this->param['real_invoice_date']) ? $this->param['real_invoice_date'] : date('Y-m-d');
  373. # 开票状态
  374. $this->param['invoice_status'] = 1;
  375. # 设置开票信息
  376. if (!$invoiceLogic->setInvoice($this->param)) return resultArray(['error' => '操作失败!']);
  377. return resultArray(['data' => '操作成功!']);
  378. }
  379. /**
  380. * 审核
  381. *
  382. * @param InvoiceLogic $invoiceLogic
  383. * @return \think\response\Json
  384. * @throws \think\db\exception\DataNotFoundException
  385. * @throws \think\db\exception\ModelNotFoundException
  386. * @throws \think\exception\DbException
  387. */
  388. public function check(InvoiceLogic $invoiceLogic)
  389. {
  390. $param = $this->param;
  391. $user_id = $this->userInfo['id'];
  392. $examineStepModel = new \app\admin\model\ExamineStep();
  393. $examineRecordModel = new \app\admin\model\ExamineRecord();
  394. $examineFlowModel = new \app\admin\model\ExamineFlow();
  395. $invoiceData = [];
  396. $invoiceData['invoice_id'] = $param['id'];
  397. $invoiceData['update_time'] = time();
  398. $invoiceData['check_status'] = 1;
  399. # 权限判断
  400. if (!$examineStepModel->checkExamine($user_id, 'crm_invoice', $param['id'])) {
  401. return resultArray(['error' => $examineStepModel->getError()]);
  402. }
  403. # 审批主体详情
  404. $dataInfo = $invoiceLogic->getExamineInfo($param['id']);
  405. $flowInfo = $examineFlowModel->getDataById($dataInfo['flow_id']);
  406. # 1审批结束
  407. $is_end = 0;
  408. # 1通过,0驳回
  409. $status = !empty($param['status']) && $param['status'] == 1 ? 1 : 0;
  410. # 审批记录
  411. $checkData = [];
  412. $checkData['check_user_id'] = $user_id;
  413. $checkData['types'] = 'crm_invoice';
  414. $checkData['types_id'] = $param['id'];
  415. $checkData['check_time'] = time();
  416. $checkData['content'] = $param['content'];
  417. $checkData['flow_id'] = $dataInfo['flow_id'];
  418. $checkData['order_id'] = $dataInfo['order_id'] ? : 1;
  419. $checkData['status'] = $status;
  420. if ($status == 1) {
  421. if ($flowInfo['config'] == 1) {
  422. # 固定流程
  423. # 获取下一审批信息
  424. $nextStepData = $examineStepModel->nextStepUser($dataInfo['owner_user_id'], $dataInfo['flow_id'], 'crm_invoice', $param['id'], $dataInfo['order_id'], $user_id);
  425. $next_user_ids = $nextStepData['next_user_ids'] ? : [];
  426. $invoiceData['order_id'] = $nextStepData['order_id'] ? : '';
  427. if (!$next_user_ids) {
  428. $is_end = 1;
  429. # 审批结束
  430. $checkData['check_status'] = !empty($status) ? 2 : 3;
  431. $invoiceData['check_user_id'] = '';
  432. } else {
  433. # 修改主体相关审批信息
  434. $invoiceData['check_user_id'] = arrayToString($next_user_ids);
  435. }
  436. } else {
  437. # 自选流程
  438. $is_end = $param['is_end'] ? 1 : '';
  439. $check_user_id = $param['check_user_id'] ? : '';
  440. if ($is_end !== 1 && empty($check_user_id)) {
  441. return resultArray(['error' => '请选择下一审批人']);
  442. }
  443. $invoiceData['check_user_id'] = arrayToString($param['check_user_id']);
  444. }
  445. if ($is_end == 1) {
  446. $checkData['check_status'] = !empty($status) ? 2 : 3;
  447. $invoiceData['check_user_id'] = '';
  448. $invoiceData['check_status'] = 2;
  449. }
  450. } else {
  451. # 审批驳回
  452. $is_end = 1;
  453. $invoiceData['check_status'] = 3;
  454. }
  455. # 已审批人ID
  456. $invoiceData['flow_user_id'] = stringToArray($dataInfo['flow_user_id']) ? arrayToString(array_merge(stringToArray($dataInfo['flow_user_id']),[$user_id])) : arrayToString([$user_id]);
  457. $resContract = $invoiceLogic->setExamineInfo($invoiceData);
  458. if ($resContract) {
  459. # 审批记录
  460. $examineRecordModel->createData($checkData);
  461. # 发送站内信
  462. if ($is_end == 1 && !empty($status)) {
  463. # 审批流程结束,将审批通过消息告知负责人
  464. (new Message())->send(
  465. Message::INVOICE_PASS,
  466. [
  467. 'title' => $dataInfo['invoice_apple_number'],
  468. 'action_id' => $param['id']
  469. ],
  470. stringToArray($dataInfo['owner_user_id'])
  471. );
  472. } else {
  473. if (!empty($status)) {
  474. # 审批流程未结束,将待审批提醒发送给下一级负责人
  475. (new Message())->send(
  476. Message::INVOICE_TO_DO,
  477. [
  478. 'from_user' => User::where(['id' => $dataInfo['owner_user_id']])->value('realname'),
  479. 'title' => $dataInfo['invoice_apple_number'],
  480. 'action_id' => $param['id']
  481. ],
  482. stringToArray($invoiceData['check_user_id'])
  483. );
  484. } else {
  485. # 将审批被驳回的消息告知负责人
  486. (new Message())->send(
  487. Message::INVOICE_REJECT,
  488. [
  489. 'title' => $dataInfo['invoice_apple_number'],
  490. 'action_id' => $param['id']
  491. ],
  492. stringToArray($dataInfo['owner_user_id'])
  493. );
  494. }
  495. }
  496. return resultArray(['data' => '审批成功']);
  497. } else {
  498. return resultArray(['error' => '审批失败,请重试!']);
  499. }
  500. }
  501. /**
  502. * 撤销审核
  503. *
  504. * @param InvoiceLogic $invoiceLogic
  505. * @return \think\response\Json
  506. * @throws \think\db\exception\DataNotFoundException
  507. * @throws \think\db\exception\ModelNotFoundException
  508. * @throws \think\exception\DbException
  509. */
  510. public function revokeCheck(InvoiceLogic $invoiceLogic)
  511. {
  512. $invoiceId = $this->param['id'];
  513. $content = $this->param['content'];
  514. $realname = $this->userInfo['realname'];
  515. $userInfo = $this->userInfo;
  516. $user_id = $userInfo['id'];
  517. if (empty($invoiceId)) return resultArray(['error' => '请选择要撤回审核的发票!']);
  518. $examineInfo = $invoiceLogic->getExamineInfo($invoiceId);
  519. if ($examineInfo['check_status'] == 2) {
  520. return resultArray(['error' => '已审批结束,不能撤销']);
  521. }
  522. if ($examineInfo['check_status'] == 4) {
  523. return resultArray(['error' => '无需撤销']);
  524. }
  525. $userModel = new \app\admin\model\User();
  526. $admin_user_ids = $userModel->getAdminId();
  527. if ($examineInfo['owner_user_id'] !== $user_id && !in_array($user_id, $admin_user_ids)) {
  528. return resultArray(['error' => '没有权限']);
  529. }
  530. # 修改发票审核状态
  531. if (!$invoiceLogic->update(['invoice_id' => $invoiceId, 'check_status' => 4, 'check_user_id' => '', 'flow_user_id' => ''])) {
  532. return resultArray(['error' => '操作失败!']);
  533. }
  534. # 添加撤销审核的记录
  535. $invoiceLogic->createExamineRecord($invoiceId, $examineInfo, $realname, $content, $user_id);
  536. return resultArray(['data' => '操作成功!']);
  537. }
  538. /**
  539. * table栏数量统计
  540. *
  541. * @return \think\response\Json
  542. * @throws \think\Exception
  543. */
  544. public function count()
  545. {
  546. if (empty($this->param['invoice_id'])) return resultArray(['error' => '参数错误!']);
  547. # 附件
  548. $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();
  549. return resultArray(['data' => ['fileCount' => $fileCount]]);
  550. }
  551. /**
  552. * 重置开票信息
  553. *
  554. * @param InvoiceLogic $invoiceLogic
  555. * @return \think\response\Json
  556. */
  557. public function resetInvoiceStatus(InvoiceLogic $invoiceLogic)
  558. {
  559. if (empty($this->param['invoice_id'])) resultArray(['error' => '参数错误!']);
  560. $this->param['real_invoice_date'] = !empty($this->param['real_invoice_date']) ? $this->param['real_invoice_date'] : date('Y-m-d');
  561. # 开票状态
  562. $this->param['invoice_status'] = 1;
  563. if (!$invoiceLogic->setInvoice($this->param)) return resultArray(['error' => '操作失败!']);
  564. return resultArray(['data' => '操作成功!']);
  565. }
  566. }