StoreOrderInvoiceServices.php 9.1 KB

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