StoreOrderInvoiceServices.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\order;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\ApiException;
  15. use app\dao\order\StoreOrderInvoiceDao;
  16. use app\services\user\UserInvoiceServices;
  17. /**
  18. * Class StoreOrderInvoiceServices
  19. * @package app\services\order
  20. */
  21. class StoreOrderInvoiceServices extends BaseServices
  22. {
  23. /**
  24. * LiveAnchorServices constructor.
  25. * @param StoreOrderInvoiceDao $dao
  26. */
  27. public function __construct(StoreOrderInvoiceDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. public function chart(array $where)
  32. {
  33. $where['is_pay'] = 1;
  34. //全部
  35. $data['all'] = (string)$this->dao->count($where);
  36. //待开
  37. $where['type'] = 1;
  38. $data['noOpened'] = (string)$this->dao->count($where);
  39. //已开
  40. $where['type'] = 2;
  41. $data['opened'] = (string)$this->dao->count($where);
  42. //退款
  43. $where['type'] = 3;
  44. $data['refund'] = (string)$this->dao->count($where);
  45. return $data;
  46. }
  47. /**
  48. * 后台获取开票列表
  49. * @param array $where
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function getList(array $where)
  56. {
  57. [$page, $list] = $this->getPageValue();
  58. $field = 'id as invoice_id,order_id,header_type,type,name,duty_number,drawer_phone,email,tell,address,bank,card_number,is_invoice,invoice_number,remark as invoice_reamrk,invoice_time,add_time as invoice_add_time';
  59. $where['is_pay'] = 1;
  60. $list = $this->dao->getList($where, $field, ['order' => function ($query) {
  61. $query->field('id,order_id,pay_price,add_time,real_name,user_phone,status,refund_status');
  62. }], 'add_time desc', $page, $list);
  63. foreach ($list as &$item) {
  64. $item['id'] = $item['order']['id'] ?? 0;
  65. $item['order_id'] = $item['order']['order_id'] ?? '';
  66. $item['pay_price'] = $item['order']['pay_price'] ?? 0.00;
  67. $item['real_name'] = $item['order']['real_name'] ?? '';
  68. $item['user_phone'] = $item['order']['user_phone'] ?? '';
  69. $item['status'] = $item['order']['status'] ?? '';
  70. $item['refund_status'] = $item['order']['refund_status'] ?? 0;
  71. $item['add_time'] = date('Y-m-d H:i:s', $item['order']['add_time'] ?? $item['invoice_add_time'] ?? time());
  72. $item['invoice_add_time'] = date('Y-m-d H:i:s', $item['invoice_add_time']);
  73. }
  74. $count = $this->dao->count($where);
  75. return compact('list', 'count');
  76. }
  77. /**
  78. * 前端获取开票列表(带商品信息)
  79. * @param $where
  80. * @return array
  81. */
  82. public function getOrderInvoiceList(array $where)
  83. {
  84. [$page, $list] = $this->getPageValue();
  85. $where['is_pay'] = 1;
  86. $list = $this->dao->getList($where, '*', ['order'], 'add_time desc', $page, $list);
  87. /** @var StoreOrderServices $storeOrderServices */
  88. $storeOrderServices = app()->make(StoreOrderServices::class);
  89. foreach ($list as &$item) {
  90. if (isset($item['order']) && $item['order']) {
  91. $item['order'] = $storeOrderServices->tidyOrder($item['order'], true);
  92. if (isset($item['order']['_status']['_type']) && $item['order']['_status']['_type'] == 3) {
  93. foreach ($item['order']['cartInfo'] ?: [] as $key => $product) {
  94. $item['order']['cartInfo'][$key]['add_time'] = isset($product['add_time']) ? date('Y-m-d H:i', (int)$product['add_time']) : '时间错误';
  95. }
  96. }
  97. }
  98. }
  99. return $list;
  100. }
  101. /**
  102. * 订单申请开票
  103. * @param int $uid
  104. * @param $order_id
  105. * @param int $invoice_id
  106. * @return mixed
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function makeUp(int $uid, $order_id, int $invoice_id)
  112. {
  113. if (!$order_id) throw new ApiException(100100);
  114. if (!$invoice_id) throw new ApiException(410325);
  115. /** @var StoreOrderServices $storeOrderServices */
  116. $storeOrderServices = app()->make(StoreOrderServices::class);
  117. /** @var UserInvoiceServices $userInvoiceServices */
  118. $userInvoiceServices = app()->make(UserInvoiceServices::class);
  119. $order = $storeOrderServices->getOne(['order_id|id' => $order_id, 'is_del' => 0]);
  120. if (!$order) {
  121. throw new ApiException(410173);
  122. }
  123. //检测再带查询
  124. $invoice = $userInvoiceServices->checkInvoice($invoice_id, $uid);
  125. if ($this->dao->getOne(['order_id' => $order['id'], 'uid' => $uid])) {
  126. throw new ApiException(410249);
  127. }
  128. if ($order['refund_status'] == 2) {
  129. throw new ApiException(410226);
  130. }
  131. if ($order['refund_status'] == 1) {
  132. throw new ApiException(410250);
  133. }
  134. unset($invoice['id'], $invoice['add_time']);
  135. $data = [];
  136. $data['category'] = 'order';
  137. $data['order_id'] = $order['id'];
  138. $data['invoice_id'] = $invoice_id;
  139. $data['add_time'] = time();
  140. $data['is_pay'] = $order['paid'] == 1 ? 1 : 0;
  141. $data = array_merge($data, $invoice);
  142. if (!$re = $this->dao->save($data)) {
  143. throw new ApiException(410251);
  144. }
  145. return ['id' => $re->id];
  146. }
  147. public function setInvoice(int $id, array $data)
  148. {
  149. $orderInvoice = $this->dao->get($id);
  150. if (!$orderInvoice) {
  151. throw new ApiException(100026);
  152. }
  153. if ($data['is_invoice'] == 1) {
  154. $data['invoice_time'] = time();
  155. }
  156. if (!$this->dao->update($id, $data, 'id')) {
  157. throw new ApiException(100015);
  158. }
  159. return true;
  160. }
  161. /**
  162. * 拆分订单同步拆分申请开票记录
  163. * @param int $oid
  164. * @return bool
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\DbException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. */
  169. public function splitOrderInvoice(int $oid)
  170. {
  171. /** @var StoreOrderServices $storeOrderServices */
  172. $storeOrderServices = app()->make(StoreOrderServices::class);
  173. $orderInfo = $storeOrderServices->getOne(['id' => $oid, 'is_del' => 0]);
  174. if (!$orderInfo) {
  175. throw new ApiException(410173);
  176. }
  177. $pid = $orderInfo['pid'] > 0 ? $orderInfo['pid'] : $orderInfo['id'];
  178. //查询开票记录
  179. $orderInvoice = $this->dao->get(['order_id' => $oid]);
  180. //查询子订单
  181. $spliteOrder = $storeOrderServices->getColumn(['pid' => $pid, 'is_system_del' => 0], 'id,order_id');
  182. if ($spliteOrder && $orderInvoice) {
  183. $data = $orderInvoice->toArray();
  184. unset($data['id']);
  185. $data['add_time'] = strtotime($data['add_time']);
  186. $data_all = [];
  187. foreach ($spliteOrder as $order) {
  188. if (!$this->dao->count(['order_id' => $order['id']])) {
  189. $data['order_id'] = $order['id'];
  190. $data_all[] = $data;
  191. }
  192. }
  193. if ($data_all) {
  194. $this->transaction(function () use ($data_all, $orderInvoice, $orderInfo) {
  195. $this->dao->saveAll($data_all);
  196. if ($orderInfo['pid'] <= 0) {
  197. $this->dao->delete(['id' => $orderInvoice['id']]);
  198. }
  199. });
  200. }
  201. }
  202. return true;
  203. }
  204. }