UserInvoiceServices.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\dao\user\UserInvoiceDao;
  14. use app\services\BaseServices;
  15. use crmeb\exceptions\ApiException;
  16. /**
  17. * Class UserInvoiceServices
  18. * @package app\services\user
  19. */
  20. class UserInvoiceServices extends BaseServices
  21. {
  22. /**
  23. * LiveAnchorServices constructor.
  24. * @param UserInvoiceDao $dao
  25. */
  26. public function __construct(UserInvoiceDao $dao)
  27. {
  28. $this->dao = $dao;
  29. }
  30. /**
  31. * 检测系统设置发票功能
  32. * @param bool $is_speclial
  33. * @return bool|array
  34. */
  35. public function invoiceFuncStatus(bool $is_speclial = true)
  36. {
  37. $invoice = (bool)sys_config('invoice_func_status', 0);
  38. if ($is_speclial) {
  39. $specialInvoice = sys_config('special_invoice_status', 0);
  40. return ['invoice_func' => $invoice, 'special_invoice' => $invoice && $specialInvoice];
  41. }
  42. return $invoice;
  43. }
  44. /**
  45. * 获取单个发票信息
  46. * @param int $id
  47. * @param int $uid
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function getInvoice(int $id, int $uid = 0)
  54. {
  55. $invoice = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  56. if (!$invoice || ($uid && $invoice['uid'] != $uid)) {
  57. return [];
  58. }
  59. return $invoice->toArray();
  60. }
  61. /**
  62. * 检测该发票是否可用
  63. * @param int $id
  64. * @param int $uid
  65. * @return bool
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function checkInvoice(int $id, int $uid)
  71. {
  72. $invoice = $this->getInvoice($id, $uid);
  73. if (!$invoice) {
  74. throw new ApiException(100026);
  75. }
  76. $invoice_func = $this->invoiceFuncStatus();
  77. if (!$invoice_func['invoice_func']) {
  78. throw new ApiException(410280);
  79. }
  80. //专用发票
  81. if ($invoice['type'] == 2) {
  82. if (!$invoice_func['special_invoice']) {
  83. throw new ApiException(410281);
  84. }
  85. }
  86. return $invoice;
  87. }
  88. /**
  89. * 获取某个用户发票列表
  90. * @param int $uid
  91. * @return array
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function getUserList(int $uid, $where)
  97. {
  98. [$page, $limit] = $this->getPageValue();
  99. $where['is_del'] = 0;
  100. $where['uid'] = $uid;
  101. return $this->dao->getList($where, '*', $page, $limit);
  102. }
  103. /**
  104. * 获取某个用户默认发票
  105. * @param int $uid
  106. * @param string $field
  107. * @return array|\think\Model|null
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\DbException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. */
  112. public function getUserDefaultInvoice(int $uid, int $type, string $field = '*')
  113. {
  114. return $this->dao->getOne(['uid' => $uid, 'is_default' => 1, 'is_del' => 0, 'type' => $type], $field);
  115. }
  116. /**
  117. * 添加|修改
  118. * @param int $uid
  119. * @param array $data
  120. * @return array
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function saveInvoice(int $uid, array $data)
  126. {
  127. $id = (int)$data['id'];
  128. $data['uid'] = $uid;
  129. unset($data['id']);
  130. $invoice = $this->dao->get(['uid' => $uid, 'name' => $data['name'], 'drawer_phone' => $data['drawer_phone'], 'is_del' => 0]);
  131. if ($id) {
  132. if ($invoice && $id != $invoice['id']) {
  133. throw new ApiException(410282);
  134. }
  135. if ($this->dao->update($id, $data, 'id')) {
  136. if ($data['is_default']) {
  137. $this->setDefaultInvoice($uid, $id);
  138. }
  139. return ['type' => 'edit', 'msg' => '修改发票成功', 'data' => []];
  140. } else {
  141. throw new ApiException(100007);
  142. }
  143. } else {
  144. if ($invoice) {
  145. throw new ApiException(410282);
  146. }
  147. if ($add_invoice = $this->dao->save($data)) {
  148. $id = (int)$add_invoice['id'];
  149. if ($data['is_default']) {
  150. $this->setDefaultInvoice($uid, $id);
  151. }
  152. return ['type' => 'add', 'msg' => '添加发票成功', 'data' => ['id' => $id]];
  153. } else {
  154. throw new ApiException(100022);
  155. }
  156. }
  157. }
  158. /**
  159. * 设置默认发票
  160. * @param int $id
  161. * @return bool
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. */
  166. public function setDefaultInvoice(int $uid, int $id)
  167. {
  168. if (!$invoice = $this->getInvoice($id)) {
  169. throw new ApiException(100026);
  170. }
  171. if ($invoice['uid'] != $uid) {
  172. throw new ApiException(100101);
  173. }
  174. if (!$this->dao->setDefault($uid, $id, $invoice['header_type'], $invoice['type'])) {
  175. throw new ApiException(410283);
  176. }
  177. return true;
  178. }
  179. /**
  180. * 删除
  181. * @param $id
  182. * @throws \Exception
  183. */
  184. public function delInvoice(int $uid, int $id)
  185. {
  186. if ($invoice = $this->getInvoice($id)) {
  187. if ($invoice['uid'] != $uid) {
  188. throw new ApiException(100101);
  189. }
  190. if (!$this->dao->update($id, ['is_del' => 1])) {
  191. throw new ApiException(100008);
  192. }
  193. }
  194. return true;
  195. }
  196. }