StoreOrderOrderInvoiceServices.php 7.5 KB

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