StoreOrderOrderInvoiceServices.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\dao\order\StoreOrderOrderInvoiceDao;
  14. use app\services\activity\StorePinkServices;
  15. use app\services\BaseServices;
  16. use think\exception\ValidateException;
  17. use app\services\user\UserInvoiceServices;
  18. /**
  19. * Class StoreOrderOrderInvoiceServices
  20. * @package app\services\order
  21. */
  22. class StoreOrderOrderInvoiceServices extends BaseServices
  23. {
  24. /**
  25. * StoreOrderOrderInvoiceServices constructor.
  26. * @param StoreOrderOrderInvoiceDao $dao
  27. */
  28. public function __construct(StoreOrderOrderInvoiceDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. public function orderCount(array $where)
  33. {
  34. //全部订单
  35. $data['all'] = (string)$this->dao->getCount(['time' => $where['time'], 'is_system_del' => 0]);
  36. //普通订单
  37. $data['general'] = (string)$this->dao->getCount(['type' => 1, 'is_system_del' => 0]);
  38. //拼团订单
  39. $data['pink'] = (string)$this->dao->getCount(['type' => 2, 'is_system_del' => 0]);
  40. //秒杀订单
  41. $data['seckill'] = (string)$this->dao->getCount(['type' => 3, 'is_system_del' => 0]);
  42. //砍价订单
  43. $data['bargain'] = (string)$this->dao->getCount(['type' => 4, 'is_system_del' => 0]);
  44. switch ($where['type']) {
  45. case 0:
  46. $data['statusAll'] = $data['all'];
  47. break;
  48. case 1:
  49. $data['statusAll'] = $data['general'];
  50. break;
  51. case 2:
  52. $data['statusAll'] = $data['pink'];
  53. break;
  54. case 3:
  55. $data['statusAll'] = $data['seckill'];
  56. break;
  57. case 4:
  58. $data['statusAll'] = $data['bargain'];
  59. break;
  60. }
  61. //未支付
  62. $data['unpaid'] = (string)$this->dao->getCount(['status' => 0, 'time' => $where['time'], 'is_system_del' => 0, 'type' => $where['type']]);
  63. //未发货
  64. $data['unshipped'] = (string)$this->dao->getCount(['status' => 1, 'time' => $where['time'], 'shipping_type' => 1, 'is_system_del' => 0, 'type' => $where['type']]);
  65. //待收货
  66. $data['untake'] = (string)$this->dao->getCount(['status' => 2, 'time' => $where['time'], 'shipping_type' => 1, 'is_system_del' => 0, 'type' => $where['type']]);
  67. //待核销
  68. $data['write_off'] = (string)$this->dao->getCount(['status' => 5, 'time' => $where['time'], 'shipping_type' => 1, 'is_system_del' => 0, 'type' => $where['type']]);
  69. //待评价
  70. $data['unevaluate'] = (string)$this->dao->getCount(['status' => 3, 'time' => $where['time'], 'is_system_del' => 0, 'type' => $where['type']]);
  71. //交易完成
  72. $data['complete'] = (string)$this->dao->getCount(['status' => 4, 'time' => $where['time'], 'is_system_del' => 0, 'type' => $where['type']]);
  73. //退款中
  74. $data['refunding'] = (string)$this->dao->getCount(['status' => -1, 'time' => $where['time'], 'is_system_del' => 0, 'type' => $where['type']]);
  75. //已退款
  76. $data['refund'] = (string)$this->dao->getCount(['status' => -2, 'time' => $where['time'], 'is_system_del' => 0, 'type' => $where['type']]);
  77. //删除订单
  78. $data['del'] = (string)$this->dao->getCount(['status' => -4, 'time' => $where['time'], 'is_system_del' => 0, 'type' => $where['type']]);
  79. return $data;
  80. }
  81. /**
  82. * 获取开票列表
  83. * @param $where
  84. * @return array
  85. */
  86. public function getList(array $where)
  87. {
  88. [$page, $list] = $this->getPageValue();
  89. $list = $this->dao->getList($where, 'o.*,i.id as invoice_id,i.header_type,i.type,i.name,i.duty_number,i.drawer_phone,i.email,i.tell,i.address,i.bank,i.card_number,i.is_invoice,invoice_number,i.remark as invoice_reamrk,i.invoice_time,i.add_time as invoice_add_time', 'add_time desc', $page, $list);
  90. /** @var StorePinkServices $pinkService */
  91. $pinkService = app()->make(StorePinkServices::class);
  92. foreach ($list as &$item){
  93. $item['add_time'] = strtotime($item['add_time']);
  94. $item['invoice_add_time'] = date('Y-m-d H:i:s', $item['invoice_add_time']);
  95. $pinkStatus = $pinkService->value(['order_id_key' => $item['id']], 'status');
  96. $item['pinkStatus'] = $pinkStatus;
  97. }
  98. /** @var StoreOrderServices $storeOrderServices */
  99. $storeOrderServices = app()->make(StoreOrderServices::class);
  100. $list = $storeOrderServices->tidyOrderList($list);
  101. $count = $this->dao->getCount($where);
  102. return compact('list', 'count');
  103. }
  104. /**
  105. * 订单申请开票
  106. * @param int $uid
  107. * @param $order_id
  108. * @param int $invoice_id
  109. * @return mixed
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\DbException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. */
  114. public function makeUp(int $uid, $order_id, int $invoice_id)
  115. {
  116. /** @var StoreOrderServices $storeOrderServices */
  117. $storeOrderServices = app()->make(StoreOrderServices::class);
  118. /** @var UserInvoiceServices $userInvoiceServices */
  119. $userInvoiceServices = app()->make(UserInvoiceServices::class);
  120. $order = $storeOrderServices->getOne(['order_id|id' => $order_id, 'is_del' => 0]);
  121. if (!$order) {
  122. throw new ValidateException('订单不存在');
  123. }
  124. //检测再带查询
  125. $invoice = $userInvoiceServices->checkInvoice($invoice_id, $uid);
  126. if ($this->dao->getOne(['order_id' => $order['id'], 'uid' => $uid])) {
  127. throw new ValidateException('发票已申请,正在审核打印中');
  128. }
  129. if ($order['refund_status'] == 2) {
  130. throw new ValidateException('订单已退款');
  131. }
  132. if ($order['refund_status'] == 1) {
  133. throw new ValidateException('正在申请退款中');
  134. }
  135. unset($invoice['id'], $invoice['add_time']);
  136. $data = [];
  137. $data['order_id'] = $order['id'];
  138. $data['invoice_id'] = $invoice_id;
  139. $data['add_time'] = time();
  140. $data = array_merge($data, $invoice);
  141. if (!$re = $this->dao->save($data)) {
  142. throw new ValidateException('申请失败,请稍后重试');
  143. }
  144. return ['id' => $re->id];
  145. }
  146. public function setInvoice(int $id)
  147. {
  148. $orderInvoice = $this->dao->get($id);
  149. if(!$orderInvoice){
  150. throw new ValidateException('数据不存在');
  151. }
  152. if($orderInvoice->is_invoice){
  153. return true;
  154. }
  155. $data = [];
  156. $data['is_invoice'] = 1;
  157. $data['invoice_time'] = time();
  158. if(!$this->dao->update($id,$data,'id')){
  159. throw new ValidateException('设置失败,请重试');
  160. }
  161. return true;
  162. }
  163. }