| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shenyang
- * Date: 2017/7/25
- * Time: 下午7:10
- */
- namespace app\frontend\modules\deduction\orderGoods;
- use app\common\models\VirtualCoin;
- use app\frontend\models\order\PreOrderDeduction;
- use app\frontend\modules\deduction\InvalidOrderDeduction;
- use app\frontend\modules\deduction\orderGoods\amount\FixedAmount;
- use app\frontend\modules\deduction\orderGoods\amount\GoodsPriceProportion;
- use app\frontend\modules\deduction\orderGoods\amount\Invalid;
- use app\frontend\modules\deduction\orderGoods\amount\OrderGoodsDeductionAmount;
- use app\common\modules\orderGoods\models\PreOrderGoods;
- use \app\common\models\orderGoods\OrderGoodsDeduction;
- /**
- * 订单商品抵扣
- * Class PreOrderGoodsDeduction
- * @package app\frontend\models\orderGoods
- * @property string code
- * @property string name
- * @property float usable_amount
- * @property float usable_coin
- * @property float used_amount
- * @property float used_coin
- * @property int uid
- */
- class PreOrderGoodsDeduction extends OrderGoodsDeduction
- {
- /**
- * @var PreOrderGoods
- */
- public $orderGoods;
- /**
- * @var \app\frontend\modules\deduction\GoodsDeduction
- */
- public $goodsDeduction;
- /**
- * 可用的虚拟币
- * @var VirtualCoin
- */
- private $usablePoint;
- /**
- * 最少购买限制
- * @var VirtualCoin
- */
- private $minLimitBuyCoin;
- /**
- * 已用的虚拟币
- * @var VirtualCoin
- */
- private $usedPoint;
- /**
- * 订单抵扣模型
- * @var PreOrderDeduction
- */
- private $orderDeduction;
- /**
- * 订单商品最高抵扣金额类
- * @var OrderGoodsDeductionAmount
- */
- private $orderGoodsDeductionMaxAmount;
- /**
- * 订单商品最低抵扣金额类
- * @var OrderGoodsDeductionAmount
- */
- private $orderGoodsDeductionMinAmount;
- protected $appends = ['usable_amount', 'usable_coin'];
- public function setOrderDeduction(PreOrderDeduction $orderDeduction)
- {
- $this->orderDeduction = $orderDeduction;
- }
- public function setOrderGoods(PreOrderGoods $orderGoods)
- {
- $this->orderGoods = $orderGoods;
- $this->uid = $orderGoods->uid;
- }
- /**
- * @return float
- */
- public function getUsableAmountAttribute()
- {
- return $this->getUsableCoin()->getMoney();
- }
- /**
- * @return float|int
- */
- public function getUsableCoinAttribute()
- {
- return $this->getUsableCoin()->getCoin();
- }
- /**
- * @return mixed
- * @throws \Exception
- */
- public function getUsedAmountAttribute()
- {
- return $this->getUsedCoin()->getMoney();
- }
- /**
- * @return float|int
- * @throws \Exception
- */
- public function getUsedCoinAttribute()
- {
- return $this->getUsedCoin()->getCoin();
- }
- /**
- * @return VirtualCoin
- */
- private function newCoin()
- {
- return app('CoinManager')->make($this->code);
- }
- /**
- * 订单抵扣模型
- * @return PreOrderDeduction
- * @throws \Exception
- */
- private function getOrderDeduction()
- {
- if (!isset($this->orderDeduction)) {
- $this->orderDeduction = $this->orderGoods->getOrder()->getOrderDeductions()->where('code', $this->code)->first();
- if (!$this->orderDeduction) {
- return new InvalidOrderDeduction();
- }
- }
- return $this->orderDeduction;
- }
- /**
- * 最多可用金额
- * @return OrderGoodsDeductionAmount
- */
- private function getOrderGoodsMaxDeductionAmount()
- {
- if (!isset($this->orderGoodsDeductionMaxAmount)) {
- // 从商品抵扣中获取到类型
- switch ($this->getGoodsDeduction()->getMaxDeductionAmountCalculationType()) {
- case 'FixedAmount':
- $this->orderGoodsDeductionMaxAmount = new FixedAmount($this->orderGoods, $this->getGoodsDeduction());
- trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最大限额使用固定金额");
- break;
- case 'GoodsPriceProportion':
- $this->orderGoodsDeductionMaxAmount = new GoodsPriceProportion($this->orderGoods, $this->getGoodsDeduction());
- trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最大限额使用固定比例");
- break;
- default:
- $this->orderGoodsDeductionMaxAmount = new Invalid($this->orderGoods, $this->getGoodsDeduction());
- trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最大限额设置无效");
- break;
- }
- }
- return $this->orderGoodsDeductionMaxAmount;
- }
- /**
- * 最小限额
- * @return OrderGoodsDeductionAmount
- */
- private function getOrderGoodsMinDeductionAmount()
- {
- if (!isset($this->orderGoodsDeductionMinAmount)) {
- // 从商品抵扣中获取到类型
- switch ($this->getGoodsDeduction()->getMinDeductionAmountCalculationType()) {
- case 'FixedAmount':
- $this->orderGoodsDeductionMinAmount = new FixedAmount($this->orderGoods, $this->getGoodsDeduction());
- trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最小限额使用固定金额");
- break;
- case 'GoodsPriceProportion':
- $this->orderGoodsDeductionMinAmount = new GoodsPriceProportion($this->orderGoods, $this->getGoodsDeduction());
- trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最小限额使用固定比例");
- break;
- default:
- $this->orderGoodsDeductionMinAmount = new Invalid($this->orderGoods, $this->getGoodsDeduction());
- trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}未设置最小限额类型");
- break;
- }
- }
- return $this->orderGoodsDeductionMinAmount;
- }
- private function getGoodsDeduction()
- {
- if (!isset($this->goodsDeduction)) {
- $this->goodsDeduction = app('DeductionManager')->make('GoodsDeductionManager')->make($this->code, [$this->orderGoods->goods]);
- }
- return $this->goodsDeduction;
- }
- private $hasMinLimitBuyCoin;
- public function hasMinLimitBuyCoin()
- {
- if (isset($this->hasMinLimitBuyCoin)) {
- return $this->hasMinLimitBuyCoin;
- }
- return $this->hasMinLimitBuyCoin = $this->_hasMinLimitBuyCoin();
- }
- protected function _hasMinLimitBuyCoin()
- {
- if (!$this->getGoodsDeduction() || !$this->getGoodsDeduction()->deductible($this->orderGoods->goods)) {
- //todo 如果抵扣未开启,获取抵扣为空,返回了抵扣类,判断为true了,所以为必选中
- return false;
- // 购买商品不存在抵扣记录
- // return $this->newCoin();
- }
- return $this->getOrderGoodsMinDeductionAmount()->hasMinAmount();
- }
- /**
- * 最低使用虚拟币
- * @return mixed
- */
- public function getMinLimitBuyCoin()
- {
- if (isset($this->minLimitBuyCoin)) {
- return $this->minLimitBuyCoin;
- }
- return $this->minLimitBuyCoin = $this->_getMinLimitBuyCoin();
- }
- /**
- * 最低使用虚拟币
- * @return mixed
- */
- protected function _getMinLimitBuyCoin()
- {
- if (!$this->getGoodsDeduction() || !$this->getGoodsDeduction()->deductible($this->orderGoods->goods)) {
- // 购买商品不存在抵扣记录
- return $this->newCoin();
- }
- $amount = $this->getOrderGoodsMinDeductionAmount()->getMinAmount();
- $handleType = $this->getOrderDeduction()->getDeduction()->getAffectDeductionAmount();
- if ($handleType == 'integer') {
- $amount = intval($amount);
- }
- $coin = $this->newCoin()->setMoney($amount);
- trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最少需要抵扣{$coin->getMoney()}元");
- return $coin;
- }
- /**
- * 获取订单商品可用的虚拟币
- * @return VirtualCoin
- */
- public function getUsableCoin()
- {
- if (isset($this->usablePoint)) {
- return $this->usablePoint;
- }
- return $this->usablePoint = $this->_getUsableCoin();
- }
- /**
- * 获取订单商品可用的虚拟币
- * @return $this|VirtualCoin
- */
- private function _getUsableCoin()
- {
- if (!$this->getGoodsDeduction() || !$this->getGoodsDeduction()->deductible($this->orderGoods->goods)) {
- trace_log()->deduction('订单商品抵扣', "{$this->name} 购买商品不存在抵扣记录");
- // 购买商品不存在抵扣记录
- return $this->newCoin();
- }
- $amount = $this->getOrderGoodsMaxDeductionAmount()->getMaxAmount();
- $coin = $this->newCoin()->setMoney($amount);
- trace_log()->deduction("订单商品抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}可抵扣{$coin->getMoney()}元");
- return $coin;
- }
- /**
- * @return $this|VirtualCoin
- * @throws \Exception
- */
- public function _getUsedCoin()
- {
- // 未选中
- if (!$this->getOrderDeduction()->isChecked()) {
- return $this->newCoin();
- }
- // 没有可用抵扣金额
- if ($this->getUsableCoin()->getMoney() <= 0) {
- return $this->newCoin();
- }
- // 订单商品最低抵扣
- $amount = $this->getMinLimitBuyCoin()->getMoney();
- // 订单所有同类型的剩余抵扣
- $restAllDeductionAmount = $this->getOrderDeduction()->getMaxOrderGoodsDeduction()->getMoney()
- - $this->getOrderDeduction()->getMinDeduction()->getMoney();
- // dump($this->getOrderDeduction()->getCode().'--'.$this->orderGoods->goods_id);
- $restDeductionAmount = $this->getOrderDeduction()->getOrderGoodsDeductionAmount()
- - $this->getOrderDeduction()->getMinDeduction()->getMoney()
- - $this->getOrderDeduction()->getUsableFreightDeduction()->getMoney();
- // dump($restAllDeductionAmount, $restDeductionAmount);
- if ($restDeductionAmount) {
- // dump($this->getUsableCoin()->getMoney(), $restAllDeductionAmount, $restDeductionAmount);
- // 订单商品的剩余抵扣/ 订单所有同类型的剩余抵扣 获取订单商品占用的抵扣金额-d
- $amount += ($this->getUsableCoin()->getMoney() - $this->getMinLimitBuyCoin()->getMoney()) / $restAllDeductionAmount * $restDeductionAmount;
- // dump($amount, '------');
- }
- return $this->newCoin()->setMoney($amount);
- }
- /**
- * @return VirtualCoin|PreOrderGoodsDeduction
- * @throws \Exception
- */
- public function getUsedCoin()
- {
- trace_log()->deduction('订单抵扣', "{$this->name} 订单商品计算已抵扣的虚拟币");
- if (isset($this->usedPoint)) {
- return $this->usedPoint;
- }
- return $this->usedPoint = $this->_getUsedCoin();
- }
- /**
- * @return bool
- * @throws \Exception
- */
- public function used()
- {
- return $this->getOrderDeduction()->isChecked() && $this->getUsedCoin()->getCoin() > 0;
- }
- public function toArray()
- {
- $this->code = (string)$this->code;
- $this->name = (string)$this->name;
- $this->usable_amount = (float)$this->usable_amount;
- $this->usable_coin = (float)$this->usable_coin;
- $this->used_amount = (float)$this->used_amount;
- $this->used_coin = (float)$this->used_coin;
- return parent::toArray();
- }
- /**
- * @param array $options
- * @return bool
- * @throws \Exception
- */
- public function save(array $options = [])
- {
- if (!$this->used()) {
- return true;
- }
- // 确保魔术属性最少执行一次
- $this->code = (string)$this->code;
- $this->name = (string)$this->name;
- $this->usable_amount = (float)$this->usable_amount;
- $this->usable_coin = (float)$this->usable_coin;
- $this->used_amount = (float)$this->used_amount;
- $this->used_coin = (float)$this->used_coin;
- return parent::save($options);
- }
- }
|