UserReceipt.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\user;
  12. use app\common\repositories\store\order\StoreOrderReceiptRepository;
  13. use think\App;
  14. use crmeb\basic\BaseController;
  15. use app\common\repositories\user\UserReceiptRepository;
  16. use app\validate\api\UserReceiptValidate;
  17. class UserReceipt extends BaseController
  18. {
  19. /**
  20. * @var UserReceiptRepository
  21. */
  22. protected $repository;
  23. /**
  24. * UserReceipt constructor.
  25. * @param App $app
  26. * @param UserReceiptRepository $repository
  27. */
  28. public function __construct(App $app, UserReceiptRepository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. }
  33. /**
  34. * 创建发票
  35. * @param UserReceiptValidate $validate
  36. * @return \think\response\Json
  37. * @author wuhaotian
  38. * @email 442384644@qq.com
  39. * @date 2024/7/10
  40. */
  41. public function create(UserReceiptValidate $validate)
  42. {
  43. $data = $this->checkParams($validate);
  44. $data['uid'] = $this->request->uid();
  45. if($data['is_default'] == 1) $this->repository->clearDefault($this->request->uid());
  46. $this->repository->create($data);
  47. return app('json')->success('添加成功');
  48. }
  49. /**
  50. * 发票列表
  51. * @return \think\response\Json
  52. * @author wuhaotian
  53. * @email 442384644@qq.com
  54. * @date 2024/7/10
  55. */
  56. public function lst()
  57. {
  58. $where = $this->request->params(['receipt_title_type','receipt_type','is_default']);
  59. $where['uid'] = $this->request->uid();
  60. return app('json')->success($this->repository->getList($where));
  61. }
  62. /**
  63. * 订单发票列表
  64. * @param StoreOrderReceiptRepository $repository
  65. * @return \think\response\Json
  66. * @author wuhaotian
  67. * @email 442384644@qq.com
  68. * @date 2024/7/10
  69. */
  70. public function order(StoreOrderReceiptRepository $repository)
  71. {
  72. [$page, $limit] = $this->getPage();
  73. $where['status'] = $this->request->param('status');
  74. $where['uid'] = $this->request->uid();
  75. $where['order_type'] = 8;
  76. $data = $repository->getList($where, $page, $limit);
  77. $data['list']->append(['storeOrder.orderProduct']);
  78. return app('json')->success($data);
  79. }
  80. /**
  81. * 订单发票信息
  82. * @param $id
  83. * @param StoreOrderReceiptRepository $repository
  84. * @return \think\response\Json
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @author wuhaotian
  89. * @email 442384644@qq.com
  90. * @date 2024/7/10
  91. */
  92. public function orderDetail($id, StoreOrderReceiptRepository $repository)
  93. {
  94. $receipt = $repository->getWhere(['order_receipt_id' => $id, 'uid' => $this->request->uid()],'*',[
  95. 'storeOrder.orderProduct',
  96. 'merchant' => function($query) {
  97. $query->field('mer_id,service_phone')->append(['services_type']);
  98. }
  99. ]);
  100. if (!$receipt) return app('json')->fail('发票信息不存在');
  101. return app('json')->success($receipt);
  102. }
  103. /**
  104. * 设置默认发票
  105. * @param $id
  106. * @return \think\response\Json
  107. * @author wuhaotian
  108. * @email 442384644@qq.com
  109. * @date 2024/7/10
  110. */
  111. public function isDefault($id)
  112. {
  113. $res = $this->repository->uidExists($id,$this->request->uid());
  114. if(!$res) return app('json')->fail('信息丢失');
  115. $this->repository->isDefault($id,$this->request->uid());
  116. return app('json')->success('修改成功');
  117. }
  118. /**
  119. * 修改发票
  120. * @param $id
  121. * @param UserReceiptValidate $validate
  122. * @return \think\response\Json
  123. * @author wuhaotian
  124. * @email 442384644@qq.com
  125. * @date 2024/7/10
  126. */
  127. public function update($id,UserReceiptValidate $validate)
  128. {
  129. $data = $this->checkParams($validate);
  130. if(!$this->repository->uidExists($id,$this->request->uid())) return app('json')->fail('信息丢失');
  131. if($data['is_default'] == 1) $this->repository->clearDefault($this->request->uid());
  132. $this->repository->update($id,$data);
  133. return app('json')->success('编辑成功');
  134. }
  135. /**
  136. * 发票详情
  137. * @param $id
  138. * @return \think\response\Json
  139. * @author wuhaotian
  140. * @email 442384644@qq.com
  141. * @date 2024/7/10
  142. */
  143. public function detail($id)
  144. {
  145. $where = [
  146. 'uid' => $this->request->uid(),
  147. 'user_receipt_id' => $id
  148. ];
  149. return app('json')->success($this->repository->detail($where));
  150. }
  151. /**
  152. * 发票删除
  153. * @param $id
  154. * @return \think\response\Json
  155. * @author wuhaotian
  156. * @email 442384644@qq.com
  157. * @date 2024/7/10
  158. */
  159. public function delete($id)
  160. {
  161. if(!$this->repository->uidExists($id,$this->request->uid()))
  162. return app('json')->fail('信息丢失');
  163. $res = $this->repository->getIsDefault($this->request->uid());
  164. if($res && $res['user_receipt_id'] == $id)
  165. return app('json')->fail('默认项不可删除');
  166. $this->repository->delete($id);
  167. return app('json')->success('删除成功');
  168. }
  169. /**
  170. * 验证参数
  171. * @param UserReceiptValidate $validate
  172. * @return array|mixed|string|string[]
  173. * @author wuhaotian
  174. * @email 442384644@qq.com
  175. * @date 2024/7/10
  176. */
  177. public function checkParams(UserReceiptValidate $validate)
  178. {
  179. $data = $this->request->params(['receipt_type','receipt_title','receipt_title_type','duty_paragraph','email','bank_name','bank_code','address','tel','is_default']);
  180. $validate->check($data);
  181. return $data;
  182. }
  183. }