You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StoreOrderInvoiceServices.php 8.5KB

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