Deduction.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2017/10/6
  6. * Time: 上午10:14
  7. */
  8. namespace app\frontend\modules\deduction\models;
  9. use app\common\models\BaseModel;
  10. use app\common\models\VirtualCoin;
  11. use app\common\modules\shop\ShopConfig;
  12. use app\frontend\models\Goods;
  13. use app\frontend\modules\deduction\DeductionSettingCollection;
  14. /**
  15. * Class Deduction
  16. * @package app\frontend\modules\deduction\models
  17. * @property int id
  18. * @property string code
  19. */
  20. class Deduction extends BaseModel
  21. {
  22. protected $table = 'yz_deduction';
  23. private $setting;
  24. /**
  25. * @var VirtualCoin
  26. */
  27. private $coin;
  28. protected $guarded = [''];
  29. public function __construct(array $attributes = [])
  30. {
  31. parent::__construct($attributes);
  32. }
  33. public function valid()
  34. {
  35. return app('DeductionManager')->make('GoodsDeductionManager')->bound($this->getCode());
  36. }
  37. //订单抵扣新增判断,抵扣虚拟币必须注册会员类
  38. public function memberCoin($member)
  39. {
  40. if (app('CoinManager')->make('MemberCoinManager')->bound($this->getCode())) {
  41. return app('CoinManager')->make('MemberCoinManager')->make($this->getCode(), [$member]);
  42. }
  43. return false;
  44. }
  45. public function getId()
  46. {
  47. return $this->id;
  48. }
  49. public function getName()
  50. {
  51. return $this->getCoin()->getName();
  52. }
  53. public function getCode()
  54. {
  55. return $this->code;
  56. }
  57. public function getCoin()
  58. {
  59. if (isset($this->coin)) {
  60. return $this->coin;
  61. }
  62. return $this->coin = app('CoinManager')->make($this->getCode());
  63. }
  64. /**
  65. * @return bool | DeductionSettingCollection
  66. */
  67. public function getSettingCollection()
  68. {
  69. if (isset($this->setting)) {
  70. return $this->setting;
  71. }
  72. if (!app('DeductionManager')->make('DeductionSettingManager')->bound($this->getCode())) {
  73. return false;
  74. }
  75. // todo 这里设计有错误, 没有区分订单范围内的抵扣设置项和全局范围内的抵扣设置项, 为了快速解决这个问题,在getDeductionSettingCollection方法中过滤掉 商品的抵扣设置
  76. return $this->setting = app('DeductionManager')->make('DeductionSettingManager')->make($this->getCode())->getDeductionSettingCollection(new Goods());
  77. }
  78. public function isEnableDeductDispatchPrice()
  79. {
  80. if (!$this->getSettingCollection()) {
  81. return false;
  82. }
  83. return $this->getSettingCollection()->isEnableDeductDispatchPrice();
  84. }
  85. /**
  86. * todo 这里应该是全局范围内的抵扣设置项
  87. * @return bool|mixed|string
  88. */
  89. public function getAffectDeductionAmount()
  90. {
  91. if (!$this->getSettingCollection()) {
  92. return false;
  93. }
  94. return $this->getSettingCollection()->getAffectDeductionAmount();
  95. }
  96. public function getEnable(){
  97. if(ShopConfig::current()->get('shop-foundation.deduction.enable') == false){
  98. return collect();
  99. }
  100. return Deduction::where('enable', 1)->get();
  101. }
  102. }