Invoice.php 28KB

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