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.

StoreOrderOrderInvoiceServices.php 6.7KB

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