StoreOrderWriteOffServices.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. namespace app\services\order;
  12. use app\dao\order\StoreOrderDao;
  13. use app\dao\order\StoreOrderWriteoffDao;
  14. use app\services\activity\integral\StoreIntegralOrderServices;
  15. use app\services\activity\integral\StoreIntegralOrderStatusServices;
  16. use app\services\activity\combination\StorePinkServices;
  17. use app\services\BaseServices;
  18. use app\services\pay\PayServices;
  19. use app\services\store\SystemStoreServices;
  20. use app\services\store\SystemStoreStaffServices;
  21. use app\services\store\DeliveryServiceServices;
  22. use app\services\supplier\SystemSupplierServices;
  23. use app\services\user\UserServices;
  24. use think\exception\ValidateException;
  25. /**
  26. * 核销订单
  27. * Class StoreOrderWriteOffServices
  28. * @package app\sservices\order
  29. * @mixin StoreOrderDao
  30. */
  31. class StoreOrderWriteOffServices extends BaseServices
  32. {
  33. protected $orderDao;
  34. /**
  35. * 构造方法
  36. * @param StoreOrderWriteoffDao $dao
  37. * @param StoreOrderDao $orderDao
  38. */
  39. public function __construct(StoreOrderWriteoffDao $dao, StoreOrderDao $orderDao)
  40. {
  41. $this->dao = $dao;
  42. $this->orderDao = $orderDao;
  43. }
  44. /**
  45. * 获取核销列表
  46. * @param $where
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function writeOffList($where)
  53. {
  54. [$page, $limit] = $this->getPageValue();
  55. $list = $this->dao->getList($where, '*', $page, $limit, [
  56. 'userInfo',
  57. 'staffInfo',
  58. 'orderInfo' => function ($query) {
  59. $query->field('id,order_id,pay_type');
  60. },
  61. 'cartInfo' => function ($query) {
  62. $query->field('id,cart_info');
  63. },
  64. ]);
  65. $count = $this->dao->count($where);
  66. if ($list) {
  67. $supplierIds = $storeIds = [];
  68. foreach ($list as $value) {
  69. switch ($value['type']) {
  70. case 0:
  71. break;
  72. case 1://门店
  73. $storeIds[] = $value['relation_id'];
  74. break;
  75. case 2://供应商
  76. $supplierIds[] = $value['relation_id'];
  77. break;
  78. }
  79. }
  80. $supplierIds = array_unique($supplierIds);
  81. $storeIds = array_unique($storeIds);
  82. $supplierList = $storeList = [];
  83. if ($supplierIds) {
  84. /** @var SystemSupplierServices $supplierServices */
  85. $supplierServices = app()->make(SystemSupplierServices::class);
  86. $supplierList = $supplierServices->getColumn([['id', 'in', $supplierIds], ['is_del', '=', 0]], 'id,supplier_name', 'id');
  87. }
  88. if ($storeIds) {
  89. /** @var SystemStoreServices $storeServices */
  90. $storeServices = app()->make(SystemStoreServices::class);
  91. $storeList = $storeServices->getColumn([['id', 'in', $storeIds], ['is_del', '=', 0]], 'id,name', 'id');
  92. }
  93. foreach ($list as &$item) {
  94. $cartInfo = $item['cartInfo'] ?? [];
  95. $cartInfo = is_string($cartInfo['cart_info']) ? json_decode($cartInfo['cart_info'], true) : $cartInfo['cart_info'];
  96. $item['productInfo'] = $cartInfo['productInfo'] ?? [];
  97. $orderInfo = $item['orderInfo'] ?? [];
  98. $item['order_id'] = $orderInfo['order_id'] ?? '';
  99. $item['pay_type'] = $orderInfo['pay_type'] ?? '';
  100. switch ($item['pay_type']) {
  101. case PayServices::WEIXIN_PAY:
  102. $item['pay_type_name'] = '微信支付';
  103. break;
  104. case PayServices::YUE_PAY:
  105. $item['pay_type_name'] = '余额支付';
  106. break;
  107. case PayServices::OFFLINE_PAY:
  108. $item['pay_type_name'] = '线下支付';
  109. break;
  110. case PayServices::ALIAPY_PAY:
  111. $item['pay_type_name'] = '支付宝支付';
  112. break;
  113. case PayServices::CASH_PAY:
  114. $item['pay_type_name'] = '现金支付';
  115. break;
  116. default:
  117. $item['pay_type_name'] = '其他支付';
  118. break;
  119. }
  120. $item['plate_name'] = '平台';
  121. switch ($item['type']) {
  122. case 0:
  123. $item['plate_name'] = '平台';
  124. break;
  125. case 1://门店
  126. $item['plate_name'] = '门店:' . ($storeList[$item['relation_id']]['name'] ?? '');
  127. break;
  128. case 2://供应商
  129. $item['plate_name'] = '供应商:' . ($supplierList[$item['relation_id']]['supplier_name'] ?? '');
  130. break;
  131. }
  132. }
  133. }
  134. return compact('list', 'count');
  135. }
  136. /**
  137. * 保存核销记录
  138. * @param int $oid
  139. * @param array $cartIds
  140. * @param array $data
  141. * @param array $orderInfo
  142. * @param array $cartInfo
  143. * @return bool
  144. */
  145. public function saveWriteOff(int $oid, array $cartIds = [], array $data = [], array $orderInfo = [], array $cartInfo = [])
  146. {
  147. if (!$oid) {
  148. throw new ValidateException('缺少核销订单信息');
  149. }
  150. if (!$orderInfo) {
  151. /** @var StoreOrderServices $storeOrderServices */
  152. $storeOrderServices = app()->make(StoreOrderServices::class);
  153. $orderInfo = $storeOrderServices->get($oid);
  154. }
  155. if (!$orderInfo) {
  156. throw new ValidateException('核销订单不存在');
  157. }
  158. $orderInfo = is_object($orderInfo) ? $orderInfo->toArray() : $orderInfo;
  159. if (!$cartInfo) {
  160. /** @var StoreOrderCartInfoServices $cartInfoServices */
  161. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  162. if ($cartIds) {//商城存在部分核销
  163. $ids = array_unique(array_column($cartIds, 'cart_id'));
  164. $cartIds = array_combine($ids, $cartIds);
  165. //订单下原商品信息
  166. $cartInfo = $cartInfoServices->getCartColunm(['oid' => $orderInfo['id'], 'cart_id' => $ids], '*', 'cart_id');
  167. } else {//整单核销
  168. $cartInfo = $cartInfoServices->getCartColunm(['oid' => $orderInfo['id']], '*', 'cart_id');
  169. }
  170. }
  171. $writeOffDataAll = [];
  172. $writeOffData = ['uid' => $orderInfo['uid'], 'oid' => $oid, 'writeoff_code' => $orderInfo['verify_code'], 'add_time' => time()];
  173. foreach ($cartInfo as $cart) {
  174. $write = $cartIds[$cart['cart_id']] ?? [];
  175. $info = is_string($cart['cart_info']) ? json_decode($cart['cart_info'], true) : $cart['cart_info'];
  176. if (!$cartIds || $write) {
  177. $writeOffData['order_cart_id'] = $cart['id'];
  178. $writeOffData['writeoff_num'] = $write['cart_num'] ?? $cart['cart_num'];
  179. $writeOffData['type'] = $cart['type'];
  180. $writeOffData['relation_id'] = $cart['relation_id'];
  181. $writeOffData['product_id'] = $cart['product_id'];
  182. $writeOffData['product_type'] = $cart['product_type'];
  183. $writeOffData['writeoff_price'] = (float)bcmul((string)$info['truePrice'], (string)$writeOffData['writeoff_num'], 2);
  184. $writeOffData['staff_id'] = $data['staff_id'] ?? 0;
  185. $writeOffDataAll[] = $writeOffData;
  186. }
  187. }
  188. if ($writeOffDataAll) {
  189. $this->dao->saveAll($writeOffDataAll);
  190. }
  191. return true;
  192. }
  193. /**核销记录
  194. * @param array $where
  195. * @return array
  196. * @throws \think\db\exception\DataNotFoundException
  197. * @throws \think\db\exception\DbException
  198. * @throws \think\db\exception\ModelNotFoundException
  199. */
  200. public function userOrderWriteOffRecords(array $where = [])
  201. {
  202. [$page, $limit] = $this->getPageValue();
  203. $list = $this->dao->getList($where, '*', $page, $limit, ['cartInfo']);
  204. $count = $this->dao->count($where);
  205. $times = [];
  206. if ($list) {
  207. foreach ($list as &$item) {
  208. $item['time_key'] = $item['time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  209. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  210. $value = is_string($item['cartInfo']['cart_info']) ? json_decode($item['cartInfo']['cart_info'], true) : $item['cartInfo']['cart_info'];
  211. $value['productInfo']['store_name'] = $value['productInfo']['store_name'] ?? "";
  212. $value['productInfo']['store_name'] = substrUTf8($value['productInfo']['store_name'], 10, 'UTF-8', '');
  213. $item['cartInfo'] = $value;
  214. }
  215. $times = array_merge(array_unique(array_column($list, 'time_key')));
  216. }
  217. return ['count' => $count, 'list' => $list, 'time' => $times];
  218. }
  219. }