StoreOrderInvoice.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\admin\v1\order;
  12. use app\controller\admin\AuthController;
  13. use app\services\order\StoreOrderInvoiceServices;
  14. use app\services\order\StoreOrderServices;
  15. use app\services\product\category\StoreProductCategoryServices;
  16. use app\services\product\product\StoreProductServices;
  17. use app\services\store\SystemStoreServices;
  18. use app\services\user\UserServices;
  19. use think\facade\App;
  20. /**
  21. * 发票管理
  22. * Class StoreOrderInvoice
  23. * @package app\controller\admin\v1\order
  24. */
  25. class StoreOrderInvoice extends AuthController
  26. {
  27. /**
  28. * StoreOrderInvoice constructor.
  29. * @param App $app
  30. * @param StoreOrderInvoiceServices $services
  31. */
  32. public function __construct(App $app, StoreOrderInvoiceServices $services)
  33. {
  34. parent::__construct($app);
  35. $this->services = $services;
  36. }
  37. /**
  38. * 获取订单类型数量
  39. * @return mixed
  40. */
  41. public function chart()
  42. {
  43. $where = $this->request->getMore([
  44. ['data', '', '', 'time'],
  45. [['type', 'd'], 0],
  46. ]);
  47. $data = $this->services->chart($where);
  48. return $this->success($data);
  49. }
  50. /**
  51. * 查询发票列表
  52. * @return mixed
  53. */
  54. public function list()
  55. {
  56. $where = $this->request->getMore([
  57. ['status', 0],
  58. ['real_name', ''],
  59. ['header_type', ''],
  60. ['type', ''],
  61. ['data', '', '', 'time'],
  62. ['field_key', ''],
  63. ]);
  64. return $this->success($this->services->getList($where));
  65. }
  66. /**
  67. * 设置发票状态
  68. * @param string $id
  69. * @return mixed
  70. */
  71. public function set_invoice($id = '')
  72. {
  73. if ($id == '') return $this->fail('缺少参数');
  74. $data = $this->request->postMore([
  75. ['is_invoice', 0],
  76. ['invoice_number', 0],
  77. ['invoice_amount', 0],
  78. ['remark', '']
  79. ]);
  80. if ($data['is_invoice'] == 1 && !$data['invoice_number']) {
  81. return $this->fail('请填写开票号');
  82. }
  83. if ($data['invoice_number'] && !preg_match('/^\d{8,10}$/', $data['invoice_number'])) {
  84. return $this->fail('请填写正确的开票号');
  85. }
  86. $this->services->setInvoice((int)$id, $data);
  87. return $this->success('设置成功');
  88. }
  89. /**
  90. * 订单详情
  91. * @param $id 订单id
  92. * @return mixed
  93. */
  94. public function orderInfo(StoreProductServices $productServices, StoreOrderServices $orderServices, $id)
  95. {
  96. if (!$id || !($orderInfo = $orderServices->get($id))) {
  97. return $this->fail('订单不存在');
  98. }
  99. /** @var UserServices $services */
  100. $services = app()->make(UserServices::class);
  101. $userInfo = $services->getUserWithTrashedInfo((int)$orderInfo['uid']);
  102. if (!$userInfo) {
  103. return app('json')->fail('用户信息不存在');
  104. }
  105. $userInfo = $userInfo->hidden(['pwd', 'add_ip', 'last_ip', 'login_type']);
  106. $userInfo['spread_name'] = '';
  107. if ($userInfo['spread_uid'])
  108. $userInfo['spread_name'] = $services->value(['uid' => $userInfo['spread_uid']], 'nickname');
  109. $orderInfo = $orderServices->tidyOrder($orderInfo->toArray(), true, true);
  110. //核算优惠金额
  111. $vipTruePrice = 0;
  112. foreach ($orderInfo['cartInfo'] ?? [] as $cart) {
  113. $vipTruePrice = bcadd((string)$vipTruePrice, (string)$cart['vip_sum_truePrice'], 2);
  114. }
  115. $orderInfo['vip_true_price'] = $vipTruePrice;
  116. $orderInfo['add_time'] = $orderInfo['_add_time'] ?? '';
  117. $cateIds = [];
  118. foreach ($orderInfo['cartInfo'] as &$item) {
  119. $cateIds = array_merge($cateIds, explode(',', $item['productInfo']['cate_id'] ?? ''));
  120. }
  121. $cateIds = implode(',', array_unique($cateIds));
  122. /** @var StoreProductCategoryServices $categoryService */
  123. $categoryService = app()->make(StoreProductCategoryServices::class);
  124. $cateList = $categoryService->getCateParentAndChildName($cateIds);
  125. foreach ($orderInfo['cartInfo'] as &$item) {
  126. $item['class_name'] = '';
  127. if (isset($item['productInfo']['cate_id']) && $item['productInfo']['cate_id']) {
  128. $cate_name = $categoryService->getCateName(explode(',', $item['productInfo']['cate_id']), $cateList);
  129. if ($cate_name) {
  130. $item['class_name'] = is_array($cate_name) ? implode(',', $cate_name) : '';
  131. }
  132. }
  133. }
  134. if ($orderInfo['store_id'] && $orderInfo['shipping_type'] == 2) {
  135. /** @var $storeServices */
  136. $storeServices = app()->make(SystemStoreServices::class);
  137. $orderInfo['_store_name'] = $storeServices->value(['id' => $orderInfo['store_id']], 'name');
  138. } else {
  139. $orderInfo['_store_name'] = '';
  140. }
  141. $userInfo = $userInfo->toArray();
  142. $invoice = $this->services->getOne(['order_id' => $id]);
  143. return app('json')->success(compact('orderInfo', 'userInfo', 'invoice'));
  144. }
  145. }