StoreOrderInvoiceServices.php 8.3 KB

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