UserTax.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\controller\mall\Orders;
  4. use app\common\controller\Api;
  5. use app\common\validate\TaxValidate;
  6. use think\db\Query;
  7. use Yansongda\Supports\Arr;
  8. /**
  9. * 发票接口
  10. */
  11. class UserTax extends Api
  12. {
  13. protected $noNeedRight='*';
  14. use TaxValidate;
  15. /**
  16. * 发票列表
  17. * @ApiParams (name=from,description=1自己创建的2订单生成的)
  18. * @ApiParams (name=id,description=查看单条传id)
  19. * @ApiParams (name=page,description=分页)
  20. * @ApiParams (name=limit,description=分页)
  21. * @ApiReturnParams (name=status,description=1开票中2可查看)
  22. */
  23. public function list(){
  24. $user=$this->auth->getUser();
  25. $data=input();
  26. $map=[];
  27. if(!empty($data['id'])){
  28. $map['id']=$data['id'];
  29. }
  30. $tax=$user->tax()
  31. ->where($map)
  32. ->where(function (Query $query)use ($data){
  33. if(!empty($data['from'])){
  34. if($data['from']==1){
  35. $query->whereNull('order_id');
  36. }elseif ($data['from']==2){
  37. $query->whereNotNull('order_id');
  38. }
  39. }
  40. })
  41. ->with(['orders','orders.info'])
  42. ->order('is_default','desc')
  43. ->order('id','desc')
  44. ->paginate(input('limit',15));
  45. $newList=[
  46. 'data'=>[]
  47. ];
  48. foreach ($tax->items() as $item){
  49. $order=[
  50. 'order_no'=>$item['orders']['order_no']??'',
  51. 'create_time'=>$item['create_time'],
  52. 'amount_pay'=>$item['orders']['amount_pay']??0,
  53. 'tax'=>$item['tax'],
  54. 'info'=>$item['orders']['info']??[],
  55. 'id'=>$item['id'],
  56. 'tax_link'=>$item['orders']['tax_link']??null,
  57. 'status'=>empty($item['orders']['tax_link'])?1:2,
  58. 'is_default'=>$item['is_default'],
  59. 'order_id'=>$item['order_id'],
  60. ];
  61. $newList['data'][]=$order;
  62. }
  63. $newList['current_page']=$tax->currentPage();
  64. $newList['last_page']=$tax->lastPage();
  65. $newList['per_page']=$tax->listRows();
  66. $newList['total']=$tax->total();
  67. $this->success('',$newList);
  68. }
  69. /**
  70. * 创建发票
  71. * @ApiMethod (POST)
  72. * @ApiParams (name=id,description=如果要修改传ID)
  73. * @ApiParams (name=tax,description="发票对象,见注释")
  74. * @ApiParams (name=is_default,description="是1否0默认")
  75. */
  76. public function store(){
  77. $data=$this->request->post();
  78. $this->validateTaxBody($data);
  79. $user=$this->auth->getUser();
  80. $needData=Arr::only($data,['tax','is_default']);
  81. if(!empty($data['id'])){
  82. $tax=$user->tax()->findOrFail($data['id']);
  83. $tax->save($needData);
  84. }else {
  85. $user->tax()->save($needData);
  86. }
  87. $this->success();
  88. }
  89. /**
  90. * 删除发票
  91. * @ApiParams (name=id,description=ID)
  92. */
  93. public function del(){
  94. $user=$this->auth->getUser();
  95. $data=$this->_validate([
  96. 'id'=>['require']
  97. ]);
  98. $tax=$user->tax()->where('id',$data['id'])->delete();
  99. $this->success();
  100. }
  101. }