UserInvoiceController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. namespace app\controller\api\v2\user;
  12. use app\services\user\UserInvoiceServices;
  13. use think\Request;
  14. /**
  15. * Class UserInvoiceController
  16. * @package app\controller\api\v2\user
  17. */
  18. class UserInvoiceController
  19. {
  20. /**
  21. * @var UserInvoiceServices
  22. */
  23. protected $services;
  24. /**
  25. * UserInvoiceController constructor.
  26. * @param UserInvoiceServices $services
  27. */
  28. public function __construct(UserInvoiceServices $services)
  29. {
  30. $this->services = $services;
  31. }
  32. /**
  33. * 获取单个发票信息
  34. * @param $id
  35. * @return mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function invoice($id)
  41. {
  42. if (!$id) {
  43. app('json')->fail('缺少参数');
  44. }
  45. return app('json')->successful($this->services->getInvoice((int)$id));
  46. }
  47. /**
  48. * 发票列表
  49. * @param Request $request
  50. * @return mixed
  51. */
  52. public function invoiceList(Request $request)
  53. {
  54. $data = $request->postMore([
  55. ['header_type', ''],
  56. ['type', '']
  57. ]);
  58. $uid = (int)$request->uid();
  59. return app('json')->successful($this->services->getUserList($uid, $data));
  60. }
  61. /**
  62. * 设置默认发票
  63. * @param Request $request
  64. * @return mixed
  65. */
  66. public function setDefaultInvoice(Request $request)
  67. {
  68. [$id] = $request->getMore([
  69. ['id', 0]
  70. ], true);
  71. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  72. $uid = (int)$request->uid();
  73. $this->services->setDefaultInvoice($uid, (int)$id);
  74. return app('json')->successful('设置成功');
  75. }
  76. /**
  77. * 获取默认发票
  78. * @param Request $request
  79. * @return mixed
  80. */
  81. public function getDefaultInvoice(Request $request)
  82. {
  83. [$type] = $request->postMore(['type', 1], true);
  84. $uid = (int)$request->uid();
  85. $defaultInvoice = $this->services->getUserDefaultInvoice($uid, (int)$type);
  86. if ($defaultInvoice) {
  87. $defaultInvoice = $defaultInvoice->toArray();
  88. return app('json')->successful('ok', $defaultInvoice);
  89. }
  90. return app('json')->successful('empty', []);
  91. }
  92. /**
  93. * 修改 添加地址
  94. * @param Request $request
  95. * @return mixed
  96. */
  97. public function saveInvoice(Request $request)
  98. {
  99. $data = $request->postMore([
  100. [['id', 'd'], 0],
  101. [['header_type', 'd'], 1],
  102. [['type', 'd'], 1],
  103. ['drawer_phone', ''],
  104. ['email', ''],
  105. ['name', ''],
  106. ['duty_number', ''],
  107. ['tell', ''],
  108. ['address', ''],
  109. ['bank', ''],
  110. ['card_number', ''],
  111. ['is_default', 0]
  112. ]);
  113. if (!$data['drawer_phone']) return app('json')->fail('请填写开票手机号');
  114. if (!check_phone($data['drawer_phone'])) return app('json')->fail('手机号码格式不正确');
  115. if (!$data['name']) return app('json')->fail('请填写发票抬头(开具发票企业名称)');
  116. if (!in_array($data['header_type'], [1, 2])) {
  117. $data['header_type'] = empty($data['duty_number']) ? 1 : 2;
  118. }
  119. if ($data['header_type'] == 1 && !preg_match('/^[\x80-\xff]{2,60}$/', $data['name'])) {
  120. return app('json')->fail('请填写正确的发票抬头(开具发票企业名称)');
  121. }
  122. if ($data['header_type'] == 2 && !preg_match('/^[0-9a-zA-Z&\(\)\(\)\x80-\xff]{2,150}$/', $data['name'])) {
  123. return app('json')->fail('请填写正确的发票抬头(开具发票企业名称)');
  124. }
  125. if ($data['header_type'] == 2 && !$data['duty_number']) {
  126. return app('json')->fail('请填写发票税号');
  127. }
  128. if ($data['header_type'] == 2 && !preg_match('/^[A-Z0-9]{15}$|^[A-Z0-9]{17}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/', $data['duty_number'])) {
  129. return app('json')->fail('请填写正确的发票税号');
  130. }
  131. if ($data['header_type'] == 1) {//个人清空企业相关字段
  132. $data['duty_number'] = $data['tell'] = $data['address'] = $data['bank'] = $data['card_number'] = '';
  133. }
  134. $uid = (int)$request->uid();
  135. $re = $this->services->saveInvoice($uid, $data);
  136. if ($re) {
  137. return app('json')->success($re['type'] == 'edit' ? $re['msg'] : $re['data']);
  138. } else {
  139. return app('json')->fail('处理失败');
  140. }
  141. }
  142. /**
  143. * 删除发票
  144. * @param Request $request
  145. * @return mixed
  146. */
  147. public function delInvoice(Request $request)
  148. {
  149. [$id] = $request->postMore([['id', 0]], true);
  150. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  151. $uid = (int)$request->uid();
  152. $re = $this->services->delInvoice($uid, (int)$id);
  153. if ($re)
  154. return app('json')->successful();
  155. else
  156. return app('json')->fail('删除地址失败!');
  157. }
  158. }