123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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\admin\traits\FieldVerificationTrait;
  13. use app\crm\logic\InvoiceLogic;
  14. use app\crm\model\NumberSequence;
  15. use app\crm\traits\AutoNumberTrait;
  16. use think\Db;
  17. use think\Hook;
  18. use think\Request;
  19. class Invoice extends ApiCommon
  20. {
  21. use AutoNumberTrait, FieldVerificationTrait;
  22. /**
  23. * 用于判断权限
  24. * @permission 无限制
  25. * @allow 登录用户可访问
  26. * @other 其他根据系统设置
  27. **/
  28. public function _initialize()
  29. {
  30. $action = [
  31. 'permission' => [],
  32. 'allow' => ['check', 'revokecheck', 'count', 'read']
  33. ];
  34. Hook::listen('check_auth', $action);
  35. $request = Request::instance();
  36. $a = strtolower($request->action());
  37. if (!in_array($a, $action['permission'])) {
  38. parent::_initialize();
  39. }
  40. }
  41. /**
  42. * 列表
  43. *
  44. * @param InvoiceLogic $invoiceLogic
  45. * @return \think\response\Json
  46. * @throws \think\exception\DbException
  47. */
  48. public function index(InvoiceLogic $invoiceLogic)
  49. {
  50. $param = $this->param;
  51. $param['user_id'] = $this->userInfo['id'];
  52. $data = $invoiceLogic->index($param);
  53. return resultArray(['data' => $data]);
  54. }
  55. /**
  56. * 创建
  57. *
  58. * @param InvoiceLogic $invoiceLogic
  59. * @return \think\response\Json
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. * @throws \think\exception\DbException
  63. */
  64. public function save(InvoiceLogic $invoiceLogic)
  65. {
  66. if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择客户!']);
  67. if (empty($this->param['contract_id'])) return resultArray(['error' => '请选择合同!']);
  68. if (empty($this->param['invoice_money'])) return resultArray(['error' => '请填写开票金额!']);
  69. if (empty($this->param['invoice_type'])) return resultArray(['error' => '请选择开票类型!']);
  70. if (empty($this->param['title_type'])) 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'] = 7;
  128. }
  129. // 数据验证
  130. // $validateResult = $this->fieldDataValidate($param, 'crm_invoice', $userId);
  131. // if (!empty($validateResult)) {
  132. // $this->error = $validateResult;
  133. // return false;
  134. // }
  135. // 发票扩展表数据
  136. $invoiceData = [];
  137. $fieldModel = new \app\admin\model\Field();
  138. // 处理部门、员工、附件、多选类型字段
  139. $arrFieldAtt = $fieldModel->getArrayField('crm_invoice');
  140. foreach ($arrFieldAtt as $k=>$v) {
  141. $param[$v] = arrayToString($param[$v]);
  142. }
  143. // 处理日期(date)类型
  144. $dateField = $fieldModel->getFieldByFormType('crm_invoice', 'date');
  145. if (!empty($dateField)) {
  146. foreach ($param AS $key => $value) {
  147. if (in_array($key, $dateField) && empty($value)) $param[$key] = null;
  148. }
  149. }
  150. // 处理手写签名类型
  151. $handwritingField = $fieldModel->getFieldByFormType('crm_invoice', 'handwriting_sign');
  152. if (!empty($handwritingField)) {
  153. foreach ($param AS $key => $value) {
  154. if (in_array($key, $handwritingField)) {
  155. $param[$key] = !empty($value['file_id']) ? $value['file_id'] : '';
  156. }
  157. }
  158. }
  159. // 处理地址、定位、日期区间、明细表格类型字段
  160. $positionField = $fieldModel->getFieldByFormType('crm_invoice', 'position');
  161. $locationField = $fieldModel->getFieldByFormType('crm_invoice', 'location');
  162. $dateIntervalField = $fieldModel->getFieldByFormType('crm_invoice', 'date_interval');
  163. $detailTableField = $fieldModel->getFieldByFormType('crm_invoice', 'detail_table');
  164. foreach ($param AS $key => $value) {
  165. // 处理地址类型字段数据
  166. if (in_array($key, $positionField)) {
  167. if (!empty($value)) {
  168. $invoiceData[] = [
  169. 'field' => $key,
  170. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  171. 'create_time' => time()
  172. ];
  173. $positionNames = array_column($value, 'name');
  174. $param[$key] = implode(',', $positionNames);
  175. } else {
  176. $param[$key] = '';
  177. }
  178. }
  179. // 处理定位类型字段数据
  180. if (in_array($key, $locationField)) {
  181. if (!empty($value)) {
  182. $invoiceData[] = [
  183. 'field' => $key,
  184. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  185. 'create_time' => time()
  186. ];
  187. $param[$key] = $value['address'];
  188. } else {
  189. $param[$key] = '';
  190. }
  191. }
  192. // 处理日期区间类型字段数据
  193. if (in_array($key, $dateIntervalField)) {
  194. if (!empty($value)) {
  195. $invoiceData[] = [
  196. 'field' => $key,
  197. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  198. 'create_time' => time()
  199. ];
  200. $param[$key] = implode('_', $value);
  201. } else {
  202. $param[$key] = '';
  203. }
  204. }
  205. // 处理明细表格类型字段数据
  206. if (in_array($key, $detailTableField)) {
  207. if (!empty($value)) {
  208. $invoiceData[] = [
  209. 'field' => $key,
  210. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  211. 'create_time' => time()
  212. ];
  213. $param[$key] = $key;
  214. } else {
  215. $param[$key] = '';
  216. }
  217. }
  218. }
  219. if (!$invoice_id = $invoiceLogic->save($param)) {
  220. return resultArray(['error' => '创建失败!']);
  221. }
  222. $send_user_id = stringToArray($param['check_user_id']);
  223. (new Message())->send(
  224. Message::INVOICE_TO_DO,
  225. [
  226. 'title' => $param['invoice_apple_number'],
  227. 'action_id' => $invoice_id
  228. ],
  229. $send_user_id
  230. );
  231. # 更新crm_number_sequence表中的last_date、create_time字段
  232. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  233. updateActionLog($param['create_user_id'], 'crm_invoice', $invoice_id, '', '', '创建了发票');
  234. RecordActionLog($param['create_user_id'], 'crm_invoice', 'save', $param['invoice_apple_number'], '', '', '新增了发票' . $param['invoice_apple_number']);
  235. # 创建待办事项的关联数据
  236. $checkUserIds = db('crm_invoice')->where('invoice_id', $invoice_id)->value('check_user_id');
  237. $checkUserIdArray = stringToArray($checkUserIds);
  238. $dealtData = [];
  239. foreach ($checkUserIdArray as $kk => $vv) {
  240. $dealtData[] = [
  241. 'types' => 'crm_invoice',
  242. 'types_id' => $invoice_id,
  243. 'user_id' => $vv
  244. ];
  245. }
  246. if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
  247. // 添加发票扩展数据
  248. array_walk($invoiceData, function (&$val) use ($invoice_id) {
  249. $val['invoice_id'] = $invoice_id;
  250. });
  251. db('crm_invoice_data')->insertAll($invoiceData);
  252. return resultArray(['data' => '创建成功!']);
  253. }
  254. /**
  255. * 详情
  256. *
  257. * @param InvoiceLogic $invoiceLogic
  258. * @return \think\response\Json
  259. * @throws \think\Exception
  260. * @throws \think\db\exception\DataNotFoundException
  261. * @throws \think\db\exception\ModelNotFoundException
  262. * @throws \think\exception\DbException
  263. */
  264. public function read(InvoiceLogic $invoiceLogic)
  265. {
  266. $invoiceId = $this->param['id'];
  267. $isUpdate = !empty($this->param['is_update']) ? $this->param['is_update'] : 0;
  268. $userInfo = $this->userInfo;
  269. $data = $invoiceLogic->read($invoiceId, $isUpdate);
  270. $readStatus = false;
  271. # 角色权限
  272. $authArray = db('admin_access')->alias('access')
  273. ->join('__ADMIN_GROUP__ group', 'group.id = access.group_id', 'left')
  274. ->where('access.user_id', $userInfo['id'])->column('group.rules');
  275. # 详情权限ID
  276. $invoiceAuthId = db('admin_rule')->where(['types' => 2, 'name' => 'invoice', 'level' => 2])->value('id');
  277. $invoiceReadAuthId = db('admin_rule')->where(['types' => 2, 'name' => 'read', 'level' => 3, 'pid' => $invoiceAuthId])->value('id');
  278. foreach ($authArray as $key => $value) {
  279. if (!empty($value) && in_array($invoiceReadAuthId, stringToArray($value))) $readStatus = true;
  280. }
  281. if (!isSuperAdministrators($userInfo['id']) && $readStatus === false) {
  282. $authData['dataAuth'] = (int)0;
  283. return resultArray(['data' => $authData]);
  284. }
  285. return resultArray(['data' => $data]);
  286. }
  287. /**
  288. * 编辑
  289. *
  290. * @param InvoiceLogic $invoiceLogic
  291. * @return \think\response\Json
  292. * @throws \think\db\exception\DataNotFoundException
  293. * @throws \think\db\exception\ModelNotFoundException
  294. * @throws \think\exception\DbException
  295. */
  296. public function update(InvoiceLogic $invoiceLogic)
  297. {
  298. $param = $this->param;
  299. if (empty($param['invoice_id'])) return resultArray(['error' => '缺少发票ID!']);
  300. if (empty($param['invoice_type'])) return resultArray(['error' => '请选择开票类型!']);
  301. if (empty($param['title_type'])) return resultArray(['error' => '请选择抬头类型!']);
  302. $userId = $this->userInfo['id'];
  303. $dataInfo = db('crm_invoice')->where('invoice_id',$param['invoice_id'])->find();
  304. # 审批是否停用
  305. $examineStatus = $param['examineStatus'];
  306. # 删除无用参数
  307. unset($param['examineStatus']);
  308. unset($param['customer_name']);
  309. unset($param['contract_money']);
  310. unset($param['contract_number']);
  311. # 设置负责人ID
  312. $param['update_time'] = time();
  313. # 已进行审批,不能编辑
  314. // $dataInfo = $invoiceLogic->read($param['invoice_id'], '');
  315. // if (!$dataInfo) {
  316. // $this->error = '数据不存在或已删除';
  317. // return false;
  318. // }
  319. $checkStatus = $invoiceLogic->getExamineStatus($param['invoice_id']);
  320. if (!in_array($checkStatus, ['3', '4', '5', '6', '7']) && $checkStatus!=0) return resultArray(['error' => '当前状态为审批中或已审批通过,不可编辑']);
  321. # 自动设置发票编号
  322. $numberInfo = [];
  323. if (empty($param['invoice_apple_number'])) {
  324. $numberInfo = $this->getAutoNumbers(4);
  325. if (empty($numberInfo['number'])) return resultArray(['error' => '请填写发票编号!']);
  326. $param['invoice_apple_number'] = $numberInfo['number'];
  327. }
  328. # 检查发票编号是否重复
  329. $invoiceWhere['invoice_apple_number'] = $param['invoice_apple_number'];
  330. $invoiceWhere['invoice_id'] = ['neq', $this->param['invoice_id']];
  331. if ($invoiceLogic->getInvoiceId($invoiceWhere)) return resultArray(['error' => '发票编号重复!']);
  332. if ($param['is_draft'] || (!empty($param['check_status']) && $param['check_status'] == 5)) {
  333. //保存为草稿
  334. $param['check_status'] = 5; //草稿(未提交)
  335. $param['check_user_id'] = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
  336. } else {
  337. # 将合同审批状态至为待审核,提交后重新进行审批
  338. if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
  339. # 审核判断(是否有符合条件的审批流)
  340. $examineFlowModel = new \app\admin\model\ExamineFlow();
  341. $examineStepModel = new \app\admin\model\ExamineStep();
  342. if (!$examineFlowModel->checkExamine($userId, 'crm_invoice')) {
  343. return resultArray(['error' => '暂无审批人,无法创建']);
  344. }
  345. # 添加审批相关信息
  346. $examineFlowData = $examineFlowModel->getFlowByTypes($userId, 'crm_invoice');
  347. if (!$examineFlowData) {
  348. return resultArray(['error' => '无可用审批流,请联系管理员']);
  349. }
  350. $param['flow_id'] = $examineFlowData['flow_id'];
  351. # 获取审批人信息
  352. if ($examineFlowData['config'] == 1) {
  353. # 固定审批流
  354. $nextStepData = $examineStepModel->nextStepUser($userId, $examineFlowData['flow_id'], 'crm_invoice', 0, 0, 0);
  355. $next_user_ids = arrayToString($nextStepData['next_user_ids']) ?: '';
  356. $check_user_id = $next_user_ids ?: [];
  357. $param['order_id'] = 1;
  358. } else {
  359. # 授权审批流
  360. $check_user_id = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
  361. }
  362. if ($param['is_draft']) {
  363. //保存为草稿
  364. $param['check_status'] = 5;
  365. $param['check_user_id'] = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
  366. } else {
  367. if (!$check_user_id) {
  368. return resultArray(['error' => '无可用审批人,请联系管理员']);
  369. }
  370. $param['check_user_id'] = is_array($check_user_id) ? ',' . implode(',', $check_user_id) . ',' : $check_user_id;
  371. $param['check_status'] = 0;
  372. }
  373. $param['flow_user_id'] = '';
  374. }
  375. }
  376. // 数据验证
  377. // $validateResult = $this->fieldDataValidate($param, 'crm_invoice', $userId);
  378. // if (!empty($validateResult)) {
  379. // $this->error = $validateResult;
  380. // return false;
  381. // }
  382. // 发票扩展表数据
  383. $invoiceData = [];
  384. $fieldModel = new \app\admin\model\Field();
  385. // 处理部门、员工、附件、多选类型字段
  386. $arrFieldAtt = $fieldModel->getArrayField('crm_invoice');
  387. foreach ($arrFieldAtt as $k=>$v) {
  388. $param[$v] = arrayToString($param[$v]);
  389. }
  390. // 处理日期(date)类型
  391. $dateField = $fieldModel->getFieldByFormType('crm_invoice', 'date');
  392. if (!empty($dateField)) {
  393. foreach ($param AS $key => $value) {
  394. if (in_array($key, $dateField) && empty($value)) $param[$key] = null;
  395. }
  396. }
  397. // 处理手写签名类型
  398. $handwritingField = $fieldModel->getFieldByFormType('crm_invoice', 'handwriting_sign');
  399. if (!empty($handwritingField)) {
  400. foreach ($param AS $key => $value) {
  401. if (in_array($key, $handwritingField)) {
  402. $param[$key] = !empty($value['file_id']) ? $value['file_id'] : '';
  403. }
  404. }
  405. }
  406. // 处理地址、定位、日期区间、明细表格类型字段
  407. $positionField = $fieldModel->getFieldByFormType('crm_invoice', 'position');
  408. $locationField = $fieldModel->getFieldByFormType('crm_invoice', 'location');
  409. $dateIntervalField = $fieldModel->getFieldByFormType('crm_invoice', 'date_interval');
  410. $detailTableField = $fieldModel->getFieldByFormType('crm_invoice', 'detail_table');
  411. foreach ($param AS $key => $value) {
  412. // 处理地址类型字段数据
  413. if (in_array($key, $positionField)) {
  414. if (!empty($value)) {
  415. $invoiceData[] = [
  416. 'field' => $key,
  417. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  418. 'create_time' => time()
  419. ];
  420. $positionNames = array_column($value, 'name');
  421. $param[$key] = implode(',', $positionNames);
  422. } else {
  423. $param[$key] = '';
  424. }
  425. }
  426. // 处理定位类型字段数据
  427. if (in_array($key, $locationField)) {
  428. if (!empty($value)) {
  429. $invoiceData[] = [
  430. 'field' => $key,
  431. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  432. 'create_time' => time()
  433. ];
  434. $param[$key] = $value['address'];
  435. } else {
  436. $param[$key] = '';
  437. }
  438. }
  439. // 处理日期区间类型字段数据
  440. if (in_array($key, $dateIntervalField)) {
  441. if (!empty($value)) {
  442. $invoiceData[] = [
  443. 'field' => $key,
  444. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  445. 'create_time' => time()
  446. ];
  447. $param[$key] = implode('_', $value);
  448. } else {
  449. $param[$key] = '';
  450. }
  451. }
  452. // 处理明细表格类型字段数据
  453. if (in_array($key, $detailTableField)) {
  454. if (!empty($value)) {
  455. $invoiceData[] = [
  456. 'field' => $key,
  457. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  458. 'create_time' => time()
  459. ];
  460. $param[$key] = $key;
  461. } else {
  462. $param[$key] = '';
  463. }
  464. }
  465. }
  466. if (!$invoiceLogic->update($param)) {
  467. return resultArray(['error' => '编辑失败!']);
  468. }
  469. //将审批记录至为无效
  470. $examineRecordModel = new \app\admin\model\ExamineRecord();
  471. $examineRecordModel->setEnd(['types' => 'crm_invoice', 'types_id' => $param['invoice_id']]);
  472. # 更新crm_number_sequence表中的last_date、create_time字段
  473. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  474. //修改记录
  475. updateActionLog($param['user_id'], 'crm_invoice', $param['invoice_id'], $dataInfo, $param);
  476. RecordActionLog($param['user_id'], 'crm_invoice', 'update', $dataInfo['invoice_apple_number'], $dataInfo, $param);
  477. # 删除待办事项的关联数据
  478. db('crm_dealt_relation')->where(['types' => ['eq', 'crm_invoice'], 'types_id' => ['eq', $param['invoice_id']]])->delete();
  479. # 创建待办事项的关联数据
  480. $checkUserIds = db('crm_invoice')->where('invoice_id', $param['invoice_id'])->value('check_user_id');
  481. $checkUserIdArray = stringToArray($checkUserIds);
  482. $dealtData = [];
  483. foreach ($checkUserIdArray as $kk => $vv) {
  484. $dealtData[] = [
  485. 'types' => 'crm_invoice',
  486. 'types_id' => $param['invoice_id'],
  487. 'user_id' => $vv
  488. ];
  489. }
  490. if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
  491. // 添加客户扩展数据
  492. $invoiceId = $param['invoice_id'];
  493. db('crm_invoice_data')->where('invoice_id', $invoiceId)->delete();
  494. array_walk($invoiceData, function (&$val) use ($invoiceId) {
  495. $val['invoice_id'] = $invoiceId;
  496. });
  497. db('crm_invoice_data')->insertAll($invoiceData);
  498. return resultArray(['data' => '编辑成功!']);
  499. }
  500. /**
  501. * 删除
  502. *
  503. * @param InvoiceLogic $invoiceLogic
  504. * @return \think\response\Json
  505. * @throws \think\db\exception\DataNotFoundException
  506. * @throws \think\db\exception\ModelNotFoundException
  507. * @throws \think\exception\DbException
  508. */
  509. public function delete(InvoiceLogic $invoiceLogic)
  510. {
  511. $actionRecordModel = new \app\admin\model\ActionRecord();
  512. $fileModel = new \app\admin\model\File();
  513. $idArray = $this->param['id'];
  514. $userinfo = $this->userInfo['id'];
  515. if (!is_array($idArray)) return resultArray(['error' => '发票ID类型错误!']);
  516. $idString = implode(',', $idArray);
  517. $status = true;
  518. if (!isSuperAdministrators($userinfo['id'])) {
  519. $list = $invoiceLogic->getExamineStatus($idString, true);
  520. foreach ($list as $key => $value) {
  521. if (!in_array($value['check_status'], [4, 5])) {
  522. $status = false;
  523. break;
  524. }
  525. }
  526. }
  527. if (!$status) return resultArray(['error' => '不能删除审批中或审批结束的发票信息!']);
  528. $dataInfo = db('crm_invoice')->where('invoice_id', ['in', $idArray])->select();
  529. if (!$invoiceLogic->delete($idArray)) return resultArray(['error' => '删除失败!']);
  530. # 删除附件
  531. $fileModel->delRFileByModule('crm_invoice', $idArray);
  532. //删除关联操作记录
  533. $actionRecordModel->delDataById(['types' => 'crm_invoice', 'action_id' => $idArray]);
  534. $userInfo = $this->userInfo;
  535. foreach ($dataInfo as $k => $v) {
  536. RecordActionLog($userInfo['id'], 'crm_contacts', 'delete', $v['invoice_apple_number'], '', '', '删除了回款:' . $v['invoice_apple_number']);
  537. }
  538. return resultArray(['data' => '删除成功!']);
  539. }
  540. /**
  541. * 转移(变更负责人)
  542. *
  543. * @param InvoiceLogic $invoiceLogic
  544. * @return \think\response\Json
  545. */
  546. public function transfer(InvoiceLogic $invoiceLogic)
  547. {
  548. $ownerUserId = $this->param['owner_user_id'];
  549. $invoiceIds = $this->param['invoice_id'];
  550. $userModel = new \app\admin\model\User();
  551. $userInfo = $this->userInfo;
  552. if (empty($ownerUserId)) return resultArray(['error' => '请选择负责人!']);
  553. if (empty($invoiceIds)) return resultArray(['error' => '请选择发票!']);
  554. if (!is_array($invoiceIds)) return resultArray(['error' => '发票ID类型错误!']);
  555. if ($invoiceLogic->transfer($invoiceIds, $ownerUserId) === false) return resultArray(['error' => '操作失败!']);
  556. $owner_user_info = $userModel->getUserById($ownerUserId);
  557. foreach ($invoiceIds as $v) {
  558. $invoice_info = db('crm_invoice')->where('invoice_id', $v)->find();
  559. updateActionLog($userInfo['id'], 'crm_invoice', $v, '', '', '将发票转移给:' . $owner_user_info['realname']);
  560. RecordActionLog($userInfo['id'], 'crm_invoice', 'transfer', $invoice_info['invoice_apple_number'], '', '', '将发票:' . $invoice_info['invoice_apple_number'] . '转移给:' . $owner_user_info['realname']);
  561. }
  562. return resultArray(['data' => '操作成功!']);
  563. }
  564. /**
  565. * 设置开票
  566. *
  567. * @param InvoiceLogic $invoiceLogic
  568. * @return \think\response\Json
  569. */
  570. public function setInvoice(InvoiceLogic $invoiceLogic)
  571. {
  572. if (empty($this->param['invoice_id'])) return resultArray(['error' => '参数错误!']);
  573. // if (empty($this->param['invoice_number'])) return resultArray(['error' => '请填写发票号码!']);
  574. // if (empty($this->param['logistics_number'])) return resultArray(['error' => '请填写物流单号!']);
  575. // if (empty($this->param['real_invoice_date'])) return resultArray(['error' => '请选择开票日期!']);
  576. $this->param['real_invoice_date'] = !empty($this->param['real_invoice_date']) ? $this->param['real_invoice_date'] : date('Y-m-d');
  577. # 开票状态
  578. $this->param['invoice_status'] = 1;
  579. # 设置开票信息
  580. if (!$invoiceLogic->setInvoice($this->param)) return resultArray(['error' => '操作失败!']);
  581. return resultArray(['data' => '操作成功!']);
  582. }
  583. /**
  584. * 审核
  585. *
  586. * @param InvoiceLogic $invoiceLogic
  587. * @return \think\response\Json
  588. * @throws \think\db\exception\DataNotFoundException
  589. * @throws \think\db\exception\ModelNotFoundException
  590. * @throws \think\exception\DbException
  591. */
  592. public function check(InvoiceLogic $invoiceLogic)
  593. {
  594. $param = $this->param;
  595. $user_id = $this->userInfo['id'];
  596. $examineStepModel = new \app\admin\model\ExamineStep();
  597. $examineRecordModel = new \app\admin\model\ExamineRecord();
  598. $examineFlowModel = new \app\admin\model\ExamineFlow();
  599. $invoiceData = [];
  600. $invoiceData['invoice_id'] = $param['id'];
  601. $invoiceData['update_time'] = time();
  602. $invoiceData['check_status'] = 1;
  603. # 权限判断
  604. if (!$examineStepModel->checkExamine($user_id, 'crm_invoice', $param['id'])) {
  605. return resultArray(['error' => $examineStepModel->getError()]);
  606. }
  607. # 审批主体详情
  608. $dataInfo = $invoiceLogic->getExamineInfo($param['id']);
  609. $flowInfo = $examineFlowModel->getDataById($dataInfo['flow_id']);
  610. # 1审批结束
  611. $is_end = 0;
  612. # 1通过,0驳回
  613. $status = !empty($param['status']) && $param['status'] == 1 ? 1 : 0;
  614. # 审批记录
  615. $checkData = [];
  616. $checkData['check_user_id'] = $user_id;
  617. $checkData['types'] = 'crm_invoice';
  618. $checkData['types_id'] = $param['id'];
  619. $checkData['check_time'] = time();
  620. $checkData['content'] = $param['content'];
  621. $checkData['flow_id'] = $dataInfo['flow_id'];
  622. $checkData['order_id'] = $dataInfo['order_id'] ?: 1;
  623. $checkData['status'] = $status;
  624. if ($status == 1) {
  625. if ($flowInfo['config'] == 1) {
  626. # 固定流程
  627. # 获取下一审批信息
  628. $nextStepData = $examineStepModel->nextStepUser($dataInfo['owner_user_id'], $dataInfo['flow_id'], 'crm_invoice', $param['id'], $dataInfo['order_id'], $user_id);
  629. $next_user_ids = $nextStepData['next_user_ids'] ?: [];
  630. $invoiceData['order_id'] = $nextStepData['order_id'] ?: '';
  631. if (!$next_user_ids) {
  632. $is_end = 1;
  633. # 审批结束
  634. $checkData['check_status'] = !empty($status) ? 2 : 3;
  635. $invoiceData['check_user_id'] = '';
  636. } else {
  637. # 修改主体相关审批信息
  638. $invoiceData['check_user_id'] = arrayToString($next_user_ids);
  639. }
  640. } else {
  641. # 自选流程
  642. $is_end = $param['is_end'] ? 1 : '';
  643. $check_user_id = $param['check_user_id'] ?: '';
  644. if ($is_end !== 1 && empty($check_user_id)) {
  645. return resultArray(['error' => '请选择下一审批人']);
  646. }
  647. $invoiceData['check_user_id'] = arrayToString($param['check_user_id']);
  648. }
  649. if ($is_end == 1) {
  650. $checkData['check_status'] = !empty($status) ? 2 : 3;
  651. $invoiceData['check_user_id'] = '';
  652. $invoiceData['check_status'] = 2;
  653. }
  654. } else {
  655. # 审批驳回
  656. $is_end = 1;
  657. $invoiceData['check_status'] = 3;
  658. }
  659. # 已审批人ID
  660. $invoiceData['flow_user_id'] = stringToArray($dataInfo['flow_user_id']) ? arrayToString(array_merge(stringToArray($dataInfo['flow_user_id']), [$user_id])) : arrayToString([$user_id]);
  661. $resContract = $invoiceLogic->setExamineInfo($invoiceData);
  662. if ($resContract) {
  663. # 审批记录
  664. $examineRecordModel->createData($checkData);
  665. # 发送站内信
  666. if ($is_end == 1 && !empty($status)) {
  667. # 审批流程结束,将审批通过消息告知负责人
  668. (new Message())->send(
  669. Message::INVOICE_PASS,
  670. [
  671. 'title' => $dataInfo['invoice_apple_number'],
  672. 'action_id' => $param['id']
  673. ],
  674. stringToArray($dataInfo['owner_user_id'])
  675. );
  676. } else {
  677. if (!empty($status)) {
  678. # 审批流程未结束,将待审批提醒发送给下一级负责人
  679. (new Message())->send(
  680. Message::INVOICE_TO_DO,
  681. [
  682. 'from_user' => User::where(['id' => $dataInfo['owner_user_id']])->value('realname'),
  683. 'title' => $dataInfo['invoice_apple_number'],
  684. 'action_id' => $param['id']
  685. ],
  686. stringToArray($invoiceData['check_user_id'])
  687. );
  688. } else {
  689. # 将审批被驳回的消息告知负责人
  690. (new Message())->send(
  691. Message::INVOICE_REJECT,
  692. [
  693. 'title' => $dataInfo['invoice_apple_number'],
  694. 'action_id' => $param['id']
  695. ],
  696. stringToArray($dataInfo['owner_user_id'])
  697. );
  698. }
  699. }
  700. return resultArray(['data' => '审批成功']);
  701. } else {
  702. return resultArray(['error' => '审批失败,请重试!']);
  703. }
  704. }
  705. /**
  706. * 撤销审核
  707. *
  708. * @param InvoiceLogic $invoiceLogic
  709. * @return \think\response\Json
  710. * @throws \think\db\exception\DataNotFoundException
  711. * @throws \think\db\exception\ModelNotFoundException
  712. * @throws \think\exception\DbException
  713. */
  714. public function revokeCheck(InvoiceLogic $invoiceLogic)
  715. {
  716. $invoiceId = $this->param['id'];
  717. $content = $this->param['content'];
  718. $realname = $this->userInfo['realname'];
  719. $userInfo = $this->userInfo;
  720. $user_id = $userInfo['id'];
  721. if (empty($invoiceId)) return resultArray(['error' => '请选择要撤回审核的发票!']);
  722. $examineInfo = $invoiceLogic->getExamineInfo($invoiceId);
  723. if ($examineInfo['check_status'] == 2) {
  724. return resultArray(['error' => '已审批结束,不能撤销']);
  725. }
  726. if ($examineInfo['check_status'] == 4) {
  727. return resultArray(['error' => '无需撤销']);
  728. }
  729. $userModel = new \app\admin\model\User();
  730. $admin_user_ids = $userModel->getAdminId();
  731. if ($examineInfo['owner_user_id'] !== $user_id && !in_array($user_id, $admin_user_ids)) {
  732. return resultArray(['error' => '没有权限']);
  733. }
  734. # 修改发票审核状态
  735. if (!$invoiceLogic->update(['invoice_id' => $invoiceId, 'check_status' => 4, 'check_user_id' => '', 'flow_user_id' => ''])) {
  736. return resultArray(['error' => '操作失败!']);
  737. }
  738. # 添加撤销审核的记录
  739. $invoiceLogic->createExamineRecord($invoiceId, $examineInfo, $realname, $content, $user_id);
  740. return resultArray(['data' => '操作成功!']);
  741. }
  742. /**
  743. * table栏数量统计
  744. *
  745. * @return \think\response\Json
  746. * @throws \think\Exception
  747. */
  748. public function count()
  749. {
  750. if (empty($this->param['invoice_id'])) return resultArray(['error' => '参数错误!']);
  751. # 附件
  752. $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();
  753. return resultArray(['data' => ['fileCount' => $fileCount]]);
  754. }
  755. /**
  756. * 重置开票信息
  757. *
  758. * @param InvoiceLogic $invoiceLogic
  759. * @return \think\response\Json
  760. */
  761. public function resetInvoiceStatus(InvoiceLogic $invoiceLogic)
  762. {
  763. if (empty($this->param['invoice_id'])) resultArray(['error' => '参数错误!']);
  764. $userInfo = $this->userInfo;
  765. $this->param['real_invoice_date'] = !empty($this->param['real_invoice_date']) ? $this->param['real_invoice_date'] : date('Y-m-d');
  766. # 开票状态
  767. $this->param['invoice_status'] = 1;
  768. $invoice_info = db('crm_invoice')->where('invoice_id', $this->param['invoice_id'])->find();
  769. if (!$invoiceLogic->setInvoice($this->param)) return resultArray(['error' => '操作失败!']);
  770. RecordActionLog($userInfo['id'], 'crm_invoice', 'update', $invoice_info['invoice_apple_number'], '', '', '将发票:' . $invoice_info['invoice_apple_number'] . '重置开票状态');
  771. return resultArray(['data' => '操作成功!']);
  772. }
  773. /**
  774. * 导出
  775. *
  776. * @author alvin guogaobo
  777. * @version 1.0 版本号
  778. * @since 2021/5/22 0022 14:34
  779. */
  780. public function excelExport()
  781. {
  782. $param = $this->param;
  783. $userInfo = $this->userInfo;
  784. $action_name = '导出全部';
  785. $param['is_excel'] = 1;
  786. $invoiceLogic=new InvoiceLogic();
  787. $excelModel = new \app\admin\model\Excel();
  788. // 导出的字段列表
  789. $fieldModel = new \app\admin\model\Field();
  790. $field_list = $fieldModel->getIndexFieldConfig('crm_invoice', $userInfo['id'],'','excel');
  791. if ($param['invoice_id']) {
  792. $param['invoice_id'] = ['condition' => 'in', 'value' => $param['invoice_id'], 'form_type' => 'text', 'name' => ''];
  793. $action_name = '导出选中';
  794. }
  795. $field = [
  796. 0 => [
  797. 'field' => 'check_status',
  798. 'name' => '审核状态',
  799. 'form_type' => 'text',
  800. 'is_hidden' => 0,
  801. ],
  802. 1 => [
  803. 'field' => 'invoice_status',
  804. 'name' => '开票状态',
  805. 'form_type' => 'text',
  806. 'is_hidden' => 0
  807. ],
  808. 2 => [
  809. 'field' => 'real_invoice_date',
  810. 'name' => '实际开票日期',
  811. 'form_type' => 'date',
  812. 'is_hidden' => 0
  813. ],
  814. 3 => [
  815. 'field' => 'invoice_number',
  816. 'name' => '发票号码',
  817. 'form_type' => 'text',
  818. 'is_hidden' => 0
  819. ],
  820. 4 => [
  821. 'field' => 'logistics_number',
  822. 'name' => '物流单号',
  823. 'form_type' => 'text',
  824. 'is_hidden' => 0,
  825. ]
  826. ];
  827. $field_list=array_merge($field_list,$field);
  828. // 文件名
  829. $file_name = '5kcrm_invoice_' . date('Ymd');
  830. $model = model('Invoice');
  831. $temp_file = $param['temp_file'];
  832. unset($param['temp_file']);
  833. $page = $param['page']?: 1;
  834. unset($param['page']);
  835. unset($param['export_queue_index']);
  836. // RecordActionLog($userInfo['id'],'crm_invoice','excelexport',$action_name,'','','导出客户');
  837. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list,$invoiceLogic) {
  838. $param['page'] = $page;
  839. $param['limit'] = $limit;
  840. $data = $invoiceLogic->index($param);
  841. $data['list'] = $model->exportHandle($data['list'], $field_list, 'invoice');
  842. return $data;
  843. });
  844. }
  845. }