UserInvoiceServices.php 6.8 KB

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