Order.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use addons\shop\model\Freight;
  7. use addons\shop\model\Carts;
  8. use addons\shop\model\Address;
  9. use Yansongda\Pay\Exceptions\GatewayException;
  10. use addons\epay\library\Service;
  11. use addons\shop\model\OrderAction;
  12. use addons\shop\model\TemplateMsg;
  13. use traits\model\SoftDelete;
  14. /**
  15. * 模型
  16. */
  17. class Order extends Model
  18. {
  19. use SoftDelete;
  20. // 表名
  21. protected $name = 'shop_order';
  22. // 开启自动写入时间戳字段
  23. protected $autoWriteTimestamp = 'int';
  24. // 定义时间戳字段名
  25. protected $createTime = 'createtime';
  26. protected $updateTime = 'updatetime';
  27. protected $deleteTime = 'deletetime';
  28. // 追加属性
  29. protected $append = [
  30. 'url'
  31. ];
  32. protected static $config = [];
  33. protected static $tagCount = 0;
  34. protected static function init()
  35. {
  36. $config = get_addon_config('shop');
  37. self::$config = $config;
  38. }
  39. public function getUrlAttr($value, $data)
  40. {
  41. return url('shop.order/detail', ['orderid' => $data['order_sn']]);
  42. }
  43. public function getPayurlAttr($value, $data)
  44. {
  45. return addon_url('shop/payment/index') . '?orderid=' . $data['order_sn'];
  46. }
  47. public function getCommenturlAttr($value, $data)
  48. {
  49. return url('shop.comment/post') . '?orderid=' . $data['order_sn'];
  50. }
  51. /**
  52. * 获取快递查询URL
  53. */
  54. public function getLogisticsurlAttr($value, $data)
  55. {
  56. $url = self::$config['logisticstype'] == 'kdnapi' ? url('shop.order/logistics') . '?orderid=' . $data['order_sn'] : "https://www.kuaidi100.com/chaxun?com={$data['expressname']}&nu={$data['expressno']}";
  57. return $url;
  58. }
  59. public function getOrderstateList()
  60. {
  61. return ['0' => __('Orderstate 0'), '1' => __('Orderstate 1'), '2' => __('Orderstate 2'), '3' => __('Orderstate 3'), '4' => __('Orderstate 4'), '5' => __('Orderstate 5')];
  62. }
  63. public function getShippingstateList()
  64. {
  65. return ['0' => __('Shippingstate 0'), '1' => __('Shippingstate 1'), '2' => __('Shippingstate 2'), '3' => __('Shippingstate 3')];
  66. }
  67. public function getPaystateList()
  68. {
  69. return ['0' => __('Paystate 0'), '1' => __('Paystate 1')];
  70. }
  71. public function getOrderstateTextAttr($value, $data)
  72. {
  73. $value = $value ? $value : $data['orderstate'];
  74. $list = $this->getOrderstateList();
  75. return $list[$value] ?? '';
  76. }
  77. public function getShippingstateTextAttr($value, $data)
  78. {
  79. $value = $value ? $value : $data['shippingstate'];
  80. $list = $this->getShippingstateList();
  81. return $list[$value] ?? '';
  82. }
  83. public function getPaystateTextAttr($value, $data)
  84. {
  85. $value = $value ? $value : $data['paystate'];
  86. $list = $this->getPaystateList();
  87. return $list[$value] ?? '';
  88. }
  89. public function getStatusTextAttr($value, $data)
  90. {
  91. if ($data['orderstate'] == 0) {
  92. if ($data['paystate'] == 0) {
  93. return '待付款';
  94. }
  95. if ($data['shippingstate'] == 0) {
  96. return '待发货';
  97. } elseif ($data['shippingstate'] == 1) {
  98. return '待收货';
  99. } elseif ($data['shippingstate'] == 2) {
  100. return '待评价';
  101. }
  102. } elseif ($data['orderstate'] == 1) {
  103. return '已取消';
  104. } elseif ($data['orderstate'] == 2) {
  105. return '已失效';
  106. } elseif ($data['orderstate'] == 3) {
  107. return '已完成';
  108. } elseif ($data['orderstate'] == 4) {
  109. return '退货/退款中';
  110. }
  111. return '未知';
  112. }
  113. //获取订单剩余有效时长
  114. public function getRemainsecondsAttr($value, $data)
  115. {
  116. return max(0, $data['expiretime'] - time());
  117. }
  118. //计算购物车商品
  119. public static function computeCarts(&$orderInfo, $cart_ids, $user_id, $area_id, $user_coupon_id = '')
  120. {
  121. $config = get_addon_config('shop');
  122. $goodsList = Carts::getGoodsList($cart_ids, $user_id);
  123. if (empty($goodsList)) {
  124. throw new \Exception("未找到商品");
  125. }
  126. $orderInfo['amount'] = 0;
  127. $orderInfo['goodsprice'] = 0;
  128. $orderInfo['shippingfee'] = 0;
  129. $orderInfo['discount'] = 0;
  130. $orderItem = [];
  131. $shippingTemp = [];
  132. $userCoupon = null;
  133. //校验领取和是否可使用
  134. if ($user_coupon_id) {
  135. $userCouponModel = new UserCoupon();
  136. $userCoupon = $userCouponModel->checkUserOrUse($user_coupon_id, $user_id);
  137. $orderInfo['user_coupon_id'] = $user_coupon_id;
  138. }
  139. //判断商品库存和状态
  140. foreach ($goodsList as $item) {
  141. $goodsItem = [];
  142. if (empty($item->goods) && empty($item->sku)) {
  143. throw new \Exception("商品已下架");
  144. }
  145. //规格
  146. if ($item->goods_sku_id && empty($item->sku)) {
  147. throw new \Exception("商品规格不存在");
  148. }
  149. if (!empty($item->sku)) { //规格计算
  150. if ($item->sku->stocks < $item->nums) {
  151. throw new \Exception("有商品库存不足,请返回购物车重新修改");
  152. }
  153. $goodsItem['image'] = !empty($item->sku->image) ? $item->sku->image : $item->goods->image;
  154. $goodsItem['price'] = $item->sku->price;
  155. $goodsItem['marketprice'] = $item->sku->marketprice;
  156. $goodsItem['goods_sn'] = $item->sku->goods_sn;
  157. $amount = bcmul($item->sku->price, $item->nums, 2);
  158. } else { //商品默认计算
  159. if ($item->goods->stocks < $item->nums) {
  160. throw new \Exception("有商品库存不足,请返回购物车重新修改");
  161. }
  162. $goodsItem['image'] = !empty($item->sku->image) ? $item->sku->image : $item->goods->image;
  163. $goodsItem['price'] = $item->goods->price;
  164. $goodsItem['marketprice'] = $item->goods->marketprice;
  165. $goodsItem['goods_sn'] = $item->goods->goods_sn;
  166. $amount = bcmul($item->goods->price, $item->nums, 2);
  167. }
  168. $goodsItem['amount'] = $amount;
  169. //订单总价
  170. $orderInfo['amount'] = bcadd($orderInfo['amount'], $amount, 2);
  171. //商品总价
  172. $orderInfo['goodsprice'] = bcadd($orderInfo['goodsprice'], $amount, 2);
  173. $freight_id = $item->goods->freight_id;
  174. //计算邮费【合并运费模板】
  175. if (!isset($shippingTemp[$freight_id])) {
  176. $shippingTemp[$freight_id] = [
  177. 'nums' => $item->nums,
  178. 'weight' => $item->goods->weight,
  179. 'amount' => $amount
  180. ];
  181. } else {
  182. $shippingTemp[$freight_id] = [
  183. 'nums' => bcadd($shippingTemp[$freight_id]['nums'], $item->nums, 2),
  184. 'weight' => bcadd($shippingTemp[$freight_id]['weight'], $item->goods->weight, 2),
  185. 'amount' => bcadd($shippingTemp[$freight_id]['amount'], $amount, 2)
  186. ];
  187. }
  188. //创建订单商品数据
  189. $orderItem[] = array_merge($goodsItem, [
  190. 'order_sn' => $orderInfo['order_sn'],
  191. 'goods_id' => $item->goods_id,
  192. 'title' => $item->goods->title,
  193. 'url' => $item->goods->url,
  194. 'nums' => $item->nums,
  195. 'goods_sku_id' => $item->goods_sku_id,
  196. 'attrdata' => $item->sku_attr,
  197. 'weight' => $item->goods->weight,
  198. 'category_id' => $item->goods->category_id,
  199. 'brand_id' => $item->goods->brand_id,
  200. ]);
  201. }
  202. //按运费模板计算
  203. foreach ($shippingTemp as $key => $item) {
  204. $shippingfee = Freight::calculate($key, $area_id, $item['nums'], $item['weight'], $item['amount']);
  205. $orderInfo['shippingfee'] = bcadd($orderInfo['shippingfee'], $shippingfee, 2);
  206. }
  207. //订单总价(含邮费)
  208. $orderInfo['amount'] = bcadd($orderInfo['goodsprice'], $orderInfo['shippingfee'], 2);
  209. if (!empty($userCoupon)) {
  210. //校验优惠券
  211. $goods_ids = array_column($orderItem, 'goods_id');
  212. $category_ids = array_column($orderItem, 'category_id');
  213. $brand_ids = array_column($orderItem, 'brand_id');
  214. $couponModel = new Coupon();
  215. $coupon = $couponModel->getCoupon($userCoupon['coupon_id'])
  216. ->checkCoupon()
  217. ->checkOpen()
  218. ->checkUseTime($userCoupon['createtime'])
  219. ->checkConditionGoods($goods_ids, $user_id, $category_ids, $brand_ids);
  220. //计算折扣金额,判断是使用不含运费,还是含运费的金额
  221. $amount = !isset($config['shippingfeecoupon']) || $config['shippingfeecoupon'] == 0 ? $orderInfo['goodsprice'] : $orderInfo['amount'];
  222. list($new_money, $coupon_money) = $coupon->doBuy($amount);
  223. //判断优惠金额是否超出总价,超出则直接设定优惠金额为总价
  224. $orderInfo['discount'] = $coupon_money > $amount ? $amount : $coupon_money;
  225. }
  226. //计算订单的应付金额【减去折扣】
  227. $orderInfo['saleamount'] = max(0, bcsub($orderInfo['amount'], $orderInfo['discount'], 2));
  228. $orderInfo['discount'] = bcadd($orderInfo['discount'], 0, 2);
  229. return [
  230. $orderItem,
  231. $goodsList,
  232. $userCoupon
  233. ];
  234. }
  235. /**
  236. * @ DateTime 2021-05-28
  237. * @ 创建订单
  238. * @param int $address_id
  239. * @param int $user_id
  240. * @param mixed $cart_ids
  241. * @param string $memo
  242. * @return Order|null
  243. */
  244. public static function createOrder($address_id, $user_id, $cart_ids, $user_coupon_id, $memo)
  245. {
  246. $address = Address::get($address_id);
  247. if (!$address || $address['user_id'] != $user_id) {
  248. throw new \Exception("地址未找到");
  249. }
  250. $config = get_addon_config('shop');
  251. $order_sn = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
  252. //订单主表
  253. $orderInfo = [
  254. 'user_id' => $user_id,
  255. 'order_sn' => $order_sn,
  256. 'address_id' => $address->id,
  257. 'province_id' => $address->province_id,
  258. 'city_id' => $address->city_id,
  259. 'area_id' => $address->area_id,
  260. 'receiver' => $address->receiver,
  261. 'mobile' => $address->mobile,
  262. 'address' => $address->address,
  263. 'zipcode' => $address->zipcode,
  264. 'goodsprice' => 0, //商品金额 (不含运费)
  265. 'amount' => 0, //总金额 (含运费)
  266. 'shippingfee' => 0, //运费
  267. 'discount' => 0, //优惠金额
  268. 'saleamount' => 0,
  269. 'memo' => $memo,
  270. 'expiretime' => time() + $config['order_timeout'], //订单失效
  271. 'status' => 'normal'
  272. ];
  273. //订单详细表
  274. list($orderItem, $goodsList, $userCoupon) = self::computeCarts($orderInfo, $cart_ids, $user_id, $address->area_id, $user_coupon_id);
  275. $order = null;
  276. Db::startTrans();
  277. try {
  278. //创建订单
  279. $order = Order::create($orderInfo, true);
  280. //减库存
  281. foreach ($goodsList as $index => $item) {
  282. if ($item->sku) {
  283. $item->sku->setDec('stocks', $item->nums);
  284. }
  285. $item->goods->setDec("stocks", $item->nums);
  286. }
  287. //计算单个商品折扣后的价格
  288. $saleamount = bcsub($order['saleamount'], $order['shippingfee'], 2);
  289. $saleratio = $order['goodsprice'] > 0 ? bcdiv($saleamount, $order['goodsprice'], 10) : 1;
  290. $saleremains = $saleamount;
  291. foreach ($orderItem as $index => &$item) {
  292. if (!isset($orderItem[$index + 1])) {
  293. $saleprice = $saleremains;
  294. } else {
  295. $saleprice = $order['discount'] == 0 ? bcmul($item['price'], $item['nums'], 2) : bcmul(bcmul($item['price'], $item['nums'], 2), $saleratio, 2);
  296. }
  297. $saleremains = bcsub($saleremains, $saleprice, 2);
  298. $item['realprice'] = $saleprice;
  299. }
  300. unset($item);
  301. //创建订单商品数据
  302. foreach ($orderItem as $index => $item) {
  303. OrderGoods::create($item, true);
  304. }
  305. //修改地址使用次数
  306. $address->setInc('usednums');
  307. //优惠券已使用
  308. if (!empty($userCoupon)) {
  309. $userCoupon->save(['is_used' => 2]);
  310. }
  311. //提交事务
  312. Db::commit();
  313. } catch (Exception $e) {
  314. Db::rollback();
  315. throw new Exception($e->getMessage());
  316. }
  317. //清空购物车
  318. Carts::clear($cart_ids);
  319. //记录操作
  320. OrderAction::push($order_sn, '系统', '订单创建成功');
  321. //订单应付金额为0时直接结算
  322. if ($order['saleamount'] == 0) {
  323. self::settle($order->order_sn, 0);
  324. $order = Order::get($order->id);
  325. }
  326. return $order;
  327. }
  328. /**
  329. * @ DateTime 2021-05-31
  330. * @ 订单信息
  331. * @param $order_sn
  332. * @param $user_id
  333. * @return array|false|\PDOStatement|string|Model
  334. */
  335. public static function getDetail($order_sn, $user_id)
  336. {
  337. return self::with(['orderGoods'])->where('order_sn', $order_sn)->where('user_id', $user_id)->find();
  338. }
  339. /**
  340. * 判断订单是否失效
  341. * @param $order_sn
  342. * @return bool
  343. */
  344. public static function isExpired($order_sn)
  345. {
  346. $orderInfo = Order::getByOrderSn($order_sn);
  347. //订单过期
  348. if (!$orderInfo['orderstate'] && !$orderInfo['paystate'] && time() > $orderInfo['expiretime']) {
  349. // 启动事务
  350. Db::startTrans();
  351. try {
  352. $orderInfo->save(['orderstate' => 2]);
  353. //库存恢复
  354. OrderGoods::setGoodsStocksInc($orderInfo->order_sn);
  355. //恢复优惠券
  356. UserCoupon::resetUserCoupon($orderInfo->user_coupon_id, $orderInfo->order_sn);
  357. // 提交事务
  358. Db::commit();
  359. } catch (\Exception $e) {
  360. // 回滚事务
  361. Db::rollback();
  362. }
  363. return true;
  364. }
  365. return false;
  366. }
  367. /**
  368. * @ 支付
  369. * @param string $orderid
  370. * @param int $user_id
  371. * @param string $paytype
  372. * @param string $method
  373. * @param string $openid
  374. * @param string $notifyurl
  375. * @param string $returnurl
  376. * @return \addons\epay\library\Collection|\addons\epay\library\RedirectResponse|\addons\epay\library\Response|null
  377. * @throws \Exception
  378. */
  379. public static function pay($orderid, $user_id, $paytype, $method = 'web', $openid = '', $notifyurl = null, $returnurl = null)
  380. {
  381. $request = \think\Request::instance();
  382. $order = self::getDetail($orderid, $user_id);
  383. if (!$order) {
  384. throw new \Exception('订单不存在!');
  385. }
  386. if ($order->paystate) {
  387. throw new \Exception('订单已支付!');
  388. }
  389. if ($order->orderstate) {
  390. throw new \Exception('订单已失效!');
  391. }
  392. //支付金额为0,无需支付
  393. if ($order->saleamount == 0) {
  394. throw new \Exception('无需支付!');
  395. }
  396. $order_sn = $order->order_sn;
  397. // 启动事务
  398. Db::startTrans();
  399. try {
  400. //支付方式变更
  401. if (($order['paytype'] == $paytype && $order['method'] != $method)) {
  402. $order_sn = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
  403. //更新电子面单
  404. $electronics = $order->order_electronics;
  405. foreach ($electronics as $aftersales) {
  406. $aftersales->order_sn = $order_sn;
  407. $aftersales->save();
  408. }
  409. //更新操作日志
  410. $orderAction = $order->order_action;
  411. foreach ($orderAction as $action) {
  412. $action->order_sn = $order_sn;
  413. $action->save();
  414. }
  415. $order->save(['order_sn' => $order_sn]);
  416. //更新订单明细
  417. foreach ($order->order_goods as $item) {
  418. $item->order_sn = $order_sn;
  419. $item->save();
  420. }
  421. }
  422. //更新支付类型和方法
  423. $order->allowField(true)->save(['paytype' => $paytype, 'method' => $method, 'openid' => $openid]);
  424. //提交事务
  425. Db::commit();
  426. } catch (\Exception $e) {
  427. // 回滚事务
  428. Db::rollback();
  429. throw new \Exception($e->getMessage());
  430. }
  431. $response = null;
  432. $epay = get_addon_info('epay');
  433. if ($epay && $epay['state']) {
  434. $notifyurl = $notifyurl ? $notifyurl : $request->root(true) . '/addons/shop/order/epay/type/notify/paytype/' . $paytype;
  435. $returnurl = $returnurl ? $returnurl : $request->root(true) . '/addons/shop/order/epay/type/return/paytype/' . $paytype . '/order_sn/' . $order_sn;
  436. //保证取出的金额一致,不一致将导致订单重复错误
  437. $amount = sprintf("%.2f", $order->saleamount);
  438. $params = [
  439. 'amount' => $amount,
  440. 'orderid' => $order_sn,
  441. 'type' => $paytype,
  442. 'title' => "支付{$amount}元",
  443. 'notifyurl' => $notifyurl,
  444. 'returnurl' => $returnurl,
  445. 'method' => $method,
  446. 'openid' => $openid
  447. ];
  448. try {
  449. $response = Service::submitOrder($params);
  450. } catch (GatewayException $e) {
  451. throw new \Exception(config('app_debug') ? $e->getMessage() : "支付失败,请稍后重试");
  452. }
  453. } else {
  454. throw new \Exception("请在后台安装配置微信支付宝整合插件");
  455. }
  456. return $response;
  457. }
  458. /**
  459. * 订单列表
  460. *
  461. * @param $param
  462. * @return \think\Paginator
  463. */
  464. public static function tableList($param)
  465. {
  466. $pageNum = 15;
  467. if (!empty($param['num'])) {
  468. $pageNum = $param['num'];
  469. }
  470. return self::with(['orderGoods'])
  471. ->where(function ($query) use ($param) {
  472. $query->where('status', 'normal');
  473. if (!empty($param['user_id'])) {
  474. $query->where('user_id', $param['user_id']);
  475. }
  476. if (isset($param['orderstate']) && $param['orderstate'] != '') {
  477. $query->where('orderstate', $param['orderstate']);
  478. }
  479. if (isset($param['shippingstate']) && $param['shippingstate'] != '') {
  480. $query->where('shippingstate', $param['shippingstate']);
  481. }
  482. if (isset($param['paystate']) && $param['paystate'] != '') {
  483. $query->where('paystate', $param['paystate']);
  484. }
  485. if (isset($param['q']) && $param['q'] != '') {
  486. $query->where('order_sn', 'in', function ($query) use ($param) {
  487. return $query->name('shop_order_goods')->where('order_sn|title', 'like', '%' . $param['q'] . '%')->field('order_sn');
  488. });
  489. }
  490. })
  491. ->order('createtime desc')->paginate($pageNum, false, ['query' => request()->get()]);
  492. }
  493. /**
  494. * @ DateTime 2021-06-01
  495. * @ 订单结算
  496. * @param string $order_sn 订单号
  497. * @param float $payamount 支付金额
  498. * @param string $transactionid 流水号
  499. * @return bool
  500. */
  501. public static function settle($order_sn, $payamount, $transactionid = '')
  502. {
  503. $order = self::with(['orderGoods'])->where('order_sn', $order_sn)->find();
  504. if (!$order || $order->paystate == 1) {
  505. return false;
  506. }
  507. if ($payamount != $order->saleamount) {
  508. \think\Log::write("[shop][pay][{$order_sn}][订单支付金额不一致]");
  509. return false;
  510. }
  511. // 启动事务
  512. Db::startTrans();
  513. try {
  514. $order->paystate = 1;
  515. $order->transactionid = $transactionid;
  516. $order->payamount = $payamount;
  517. $order->paytime = time();
  518. $order->paytype = !$order->paytype ? 'system' : $order->paytype;
  519. $order->method = !$order->method ? 'system' : $order->method;
  520. $order->save();
  521. if ($order->payamount == $order->saleamount) {
  522. //支付完成后,商品销量+1
  523. foreach ($order->order_goods as $item) {
  524. $goods = $item->goods;
  525. $sku = $item->sku;
  526. if ($goods) {
  527. $goods->setInc('sales', $item->nums);
  528. }
  529. if ($sku) {
  530. $sku->setInc('sales', $item->nums);
  531. }
  532. }
  533. }
  534. // 提交事务
  535. Db::commit();
  536. } catch (\Exception $e) {
  537. // 回滚事务
  538. Db::rollback();
  539. return false;
  540. }
  541. //记录操作
  542. OrderAction::push($order_sn, '系统', '订单支付成功');
  543. //发送通知
  544. TemplateMsg::sendTempMsg(0, $order->order_sn);
  545. return true;
  546. }
  547. public function user()
  548. {
  549. return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  550. }
  551. public function address()
  552. {
  553. return $this->belongsTo('Address', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
  554. }
  555. public function orderGoods()
  556. {
  557. return $this->hasMany('OrderGoods', 'order_sn', 'order_sn');
  558. }
  559. public function orderElectronics()
  560. {
  561. return $this->hasMany('OrderElectronics', 'order_sn', 'order_sn');
  562. }
  563. public function orderAction()
  564. {
  565. return $this->hasMany('OrderAction', 'order_sn', 'order_sn');
  566. }
  567. }