PreOrderTrait.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2018/8/13
  6. * Time: 下午4:14
  7. */
  8. namespace app\frontend\modules\order\models;
  9. use app\common\events\order\BeforeOrderCreateEvent;
  10. use app\common\exceptions\AppException;
  11. use app\frontend\modules\orderGoods\models\PreOrderGoodsCollection;
  12. /**
  13. * Trait PreOrderTrait
  14. * @package app\frontend\modules\order\models
  15. * @property PreOrderGoodsCollection orderGoods
  16. */
  17. trait PreOrderTrait
  18. {
  19. /**
  20. * 订单插入数据库,触发订单生成事件
  21. * @return mixed
  22. * @throws AppException
  23. */
  24. public function generate()
  25. {
  26. event(new BeforeOrderCreateEvent($this));
  27. $this->beforeSaving();
  28. $this->save();
  29. $this->afterSaving();
  30. $result = $this->push();
  31. if ($result === false) {
  32. throw new AppException('订单相关信息保存失败');
  33. }
  34. return $this->id;
  35. }
  36. /**
  37. * 统计商品总数
  38. * @return int
  39. */
  40. protected function getGoodsTotal()
  41. {
  42. //累加所有商品数量
  43. $result = $this->orderGoods->sum(function ($aOrderGoods) {
  44. return $aOrderGoods->total;
  45. });
  46. return $result;
  47. }
  48. /**
  49. * 统计订单商品成交金额
  50. * @return int
  51. */
  52. public function getOrderGoodsPrice()
  53. {
  54. return $this->goods_price = $this->orderGoods->getPrice();
  55. }
  56. /**
  57. * 统计订单商品会员价金额
  58. * @return int
  59. */
  60. public function getVipOrderGoodsPrice()
  61. {
  62. //订单禁用优惠返回,商品现价
  63. if ($this->isDiscountDisable()) {
  64. return $this->orderGoods->getPrice();
  65. }
  66. return $this->orderGoods->getVipPrice();
  67. }
  68. /**
  69. * 统计订单商品原价
  70. * @return int
  71. */
  72. public function getGoodsPrice()
  73. {
  74. return $this->orderGoods->getGoodsPrice();
  75. }
  76. public function getPriceAttribute()
  77. {
  78. return $this->getPrice();
  79. }
  80. public function getDispatchPriceAttribute()
  81. {
  82. return $this->getDispatchAmount();
  83. }
  84. }