GoodsDiscount.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/27
  6. * Time: 下午1:58
  7. */
  8. namespace app\common\models;
  9. use app\frontend\modules\orderGoods\price\adapter\BaseGoodsPriceAdapter;
  10. /**
  11. * Class GoodsDiscount
  12. * @package app\common\models
  13. * @property int discount_method
  14. * @property int discount_value
  15. */
  16. class GoodsDiscount extends BaseModel
  17. {
  18. public $table = 'yz_goods_discount';
  19. public $guarded = [];
  20. const MONEY_OFF = 1;//折扣
  21. const DISCOUNT = 2;//固定金额
  22. const COST_RATE = 3;//成本比例
  23. public $amount;
  24. /**
  25. * 开启商品独立优惠
  26. * @return bool
  27. */
  28. public function enable()
  29. {
  30. //设置了折扣方式 并且 设置了折扣值
  31. return $this->discount_method != 0 && $this->discount_value !== '';
  32. }
  33. /**
  34. * @param $price
  35. * @return int|mixed
  36. * @throws \app\common\exceptions\AppException
  37. */
  38. public function getAmount($price,$member = null)
  39. {
  40. if(array_key_exists('amount',$this->attributes)){
  41. return $this->amount;
  42. }
  43. if ($this->enable()) {
  44. $this->amount = $this->getIndependentDiscountAmount($price);
  45. } else {
  46. $this->amount = $this->getGlobalDiscountAmount($price,$member);
  47. }
  48. return $this->amount;
  49. }
  50. /**
  51. * @param BaseGoodsPriceAdapter $price
  52. * @param \app\common\models\MemberLevel $nextLevel
  53. */
  54. public function getNextAmount($price,$nextLevel)
  55. {
  56. if(array_key_exists('amount',$this->attributes)){
  57. return $this->amount;
  58. }
  59. if ($this->enable()) {
  60. return $this->getIndependentDiscountAmount($price);
  61. } else {
  62. $price = $nextLevel->getDiscountCalculation($price);
  63. return round($price,2);
  64. // // 商品折扣 默认 10折
  65. // $discount = trim($nextLevel->discount);
  66. // $discount = ($discount == false) ? 10 : $discount;
  67. // // 折扣/10 得到折扣百分比
  68. // return round((1 - $discount / 10) * $price,2);
  69. }
  70. }
  71. /**
  72. * @param $price
  73. * @return int
  74. * @throws \app\common\exceptions\AppException
  75. */
  76. public function getGlobalDiscountAmount($price,$member = null)
  77. {
  78. //$member = \app\frontend\models\Member::current();
  79. if (!isset($member->yzMember->level)) {
  80. return 0;
  81. }
  82. return $member->yzMember->level->getMemberLevelGoodsDiscountAmount($price);
  83. }
  84. /**
  85. * 获取等级优惠金额
  86. * @param BaseGoodsPriceAdapter $price
  87. * @return int|mixed
  88. */
  89. public function getIndependentDiscountAmount($price)
  90. {
  91. //其次等级商品全局设置
  92. switch ($this->discount_method) {
  93. case self::DISCOUNT:
  94. $result = $this->getMoneyAmount();
  95. break;
  96. case self::MONEY_OFF:
  97. $result = $this->getMemberLevelGoodsPriceDiscountAmount($price->getDealPrice());
  98. break;
  99. case self::COST_RATE:
  100. $result = $this->getMemberLevelGoodsCostPriceDiscountAmount($price->getCostPrice());
  101. break;
  102. default:
  103. $result = $price;
  104. break;
  105. }
  106. return $result ? $result : 0;
  107. }
  108. /**
  109. * 商品独立等级立减后优惠金额
  110. * @return mixed
  111. */
  112. private function getMoneyAmount()
  113. {
  114. if ($this->discount_value == 0) {
  115. return 0;
  116. }
  117. return $this->discount_value;
  118. }
  119. /**
  120. * 商品独立等级折扣优惠金额
  121. * @param $price
  122. * @return mixed
  123. */
  124. private function getDiscountAmount($price)
  125. {
  126. if ($this->discount_value == 0) {
  127. return 0;
  128. }
  129. return $price * (1 - $this->discount_value / 10);
  130. }
  131. /**
  132. * @param BaseGoodsPriceAdapter $priceClass
  133. */
  134. public function getDiscountCalculation($priceClass)
  135. {
  136. //获取设置的计算方式
  137. $level_discount_calculation = \Setting::get('shop.member.level_discount_calculation');
  138. switch ($level_discount_calculation) {
  139. case 1:
  140. //取商品成本价
  141. $discountAmount = $this->getMemberLevelGoodsCostPriceDiscountAmount($priceClass->getCostPrice());
  142. break;
  143. default:
  144. //为空为0,取商品现价
  145. $discountAmount = $this->getMemberLevelGoodsPriceDiscountAmount($priceClass->getPrice());
  146. break;
  147. }
  148. return max($discountAmount, 0);
  149. }
  150. protected function getMemberLevelGoodsPriceDiscountAmount($goodsPrice)
  151. {
  152. if ($this->discount_value == 0) {
  153. return 0;
  154. }
  155. return $goodsPrice * (1 - $this->discount_value / 10);
  156. }
  157. protected function getMemberLevelGoodsCostPriceDiscountAmount($goodsCostPrice)
  158. {
  159. if ($this->discount_value == 0) {
  160. return 0;
  161. }
  162. return $goodsCostPrice * ($this->discount_value / 100);
  163. }
  164. public function goods()
  165. {
  166. return $this->belongsTo(Goods::class);
  167. }
  168. }