PreOrderGoodsDeduction.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2017/7/25
  6. * Time: 下午7:10
  7. */
  8. namespace app\frontend\modules\deduction\orderGoods;
  9. use app\common\models\VirtualCoin;
  10. use app\frontend\models\order\PreOrderDeduction;
  11. use app\frontend\modules\deduction\InvalidOrderDeduction;
  12. use app\frontend\modules\deduction\orderGoods\amount\FixedAmount;
  13. use app\frontend\modules\deduction\orderGoods\amount\GoodsPriceProportion;
  14. use app\frontend\modules\deduction\orderGoods\amount\Invalid;
  15. use app\frontend\modules\deduction\orderGoods\amount\OrderGoodsDeductionAmount;
  16. use app\common\modules\orderGoods\models\PreOrderGoods;
  17. use \app\common\models\orderGoods\OrderGoodsDeduction;
  18. /**
  19. * 订单商品抵扣
  20. * Class PreOrderGoodsDeduction
  21. * @package app\frontend\models\orderGoods
  22. * @property string code
  23. * @property string name
  24. * @property float usable_amount
  25. * @property float usable_coin
  26. * @property float used_amount
  27. * @property float used_coin
  28. * @property int uid
  29. */
  30. class PreOrderGoodsDeduction extends OrderGoodsDeduction
  31. {
  32. /**
  33. * @var PreOrderGoods
  34. */
  35. public $orderGoods;
  36. /**
  37. * @var \app\frontend\modules\deduction\GoodsDeduction
  38. */
  39. public $goodsDeduction;
  40. /**
  41. * 可用的虚拟币
  42. * @var VirtualCoin
  43. */
  44. private $usablePoint;
  45. /**
  46. * 最少购买限制
  47. * @var VirtualCoin
  48. */
  49. private $minLimitBuyCoin;
  50. /**
  51. * 已用的虚拟币
  52. * @var VirtualCoin
  53. */
  54. private $usedPoint;
  55. /**
  56. * 订单抵扣模型
  57. * @var PreOrderDeduction
  58. */
  59. private $orderDeduction;
  60. /**
  61. * 订单商品最高抵扣金额类
  62. * @var OrderGoodsDeductionAmount
  63. */
  64. private $orderGoodsDeductionMaxAmount;
  65. /**
  66. * 订单商品最低抵扣金额类
  67. * @var OrderGoodsDeductionAmount
  68. */
  69. private $orderGoodsDeductionMinAmount;
  70. protected $appends = ['usable_amount', 'usable_coin'];
  71. public function setOrderDeduction(PreOrderDeduction $orderDeduction)
  72. {
  73. $this->orderDeduction = $orderDeduction;
  74. }
  75. public function setOrderGoods(PreOrderGoods $orderGoods)
  76. {
  77. $this->orderGoods = $orderGoods;
  78. $this->uid = $orderGoods->uid;
  79. }
  80. /**
  81. * @return float
  82. */
  83. public function getUsableAmountAttribute()
  84. {
  85. return $this->getUsableCoin()->getMoney();
  86. }
  87. /**
  88. * @return float|int
  89. */
  90. public function getUsableCoinAttribute()
  91. {
  92. return $this->getUsableCoin()->getCoin();
  93. }
  94. /**
  95. * @return mixed
  96. * @throws \Exception
  97. */
  98. public function getUsedAmountAttribute()
  99. {
  100. return $this->getUsedCoin()->getMoney();
  101. }
  102. /**
  103. * @return float|int
  104. * @throws \Exception
  105. */
  106. public function getUsedCoinAttribute()
  107. {
  108. return $this->getUsedCoin()->getCoin();
  109. }
  110. /**
  111. * @return VirtualCoin
  112. */
  113. private function newCoin()
  114. {
  115. return app('CoinManager')->make($this->code);
  116. }
  117. /**
  118. * 订单抵扣模型
  119. * @return PreOrderDeduction
  120. * @throws \Exception
  121. */
  122. private function getOrderDeduction()
  123. {
  124. if (!isset($this->orderDeduction)) {
  125. $this->orderDeduction = $this->orderGoods->getOrder()->getOrderDeductions()->where('code', $this->code)->first();
  126. if (!$this->orderDeduction) {
  127. return new InvalidOrderDeduction();
  128. }
  129. }
  130. return $this->orderDeduction;
  131. }
  132. /**
  133. * 最多可用金额
  134. * @return OrderGoodsDeductionAmount
  135. */
  136. private function getOrderGoodsMaxDeductionAmount()
  137. {
  138. if (!isset($this->orderGoodsDeductionMaxAmount)) {
  139. // 从商品抵扣中获取到类型
  140. switch ($this->getGoodsDeduction()->getMaxDeductionAmountCalculationType()) {
  141. case 'FixedAmount':
  142. $this->orderGoodsDeductionMaxAmount = new FixedAmount($this->orderGoods, $this->getGoodsDeduction());
  143. trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最大限额使用固定金额");
  144. break;
  145. case 'GoodsPriceProportion':
  146. $this->orderGoodsDeductionMaxAmount = new GoodsPriceProportion($this->orderGoods, $this->getGoodsDeduction());
  147. trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最大限额使用固定比例");
  148. break;
  149. default:
  150. $this->orderGoodsDeductionMaxAmount = new Invalid($this->orderGoods, $this->getGoodsDeduction());
  151. trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最大限额设置无效");
  152. break;
  153. }
  154. }
  155. return $this->orderGoodsDeductionMaxAmount;
  156. }
  157. /**
  158. * 最小限额
  159. * @return OrderGoodsDeductionAmount
  160. */
  161. private function getOrderGoodsMinDeductionAmount()
  162. {
  163. if (!isset($this->orderGoodsDeductionMinAmount)) {
  164. // 从商品抵扣中获取到类型
  165. switch ($this->getGoodsDeduction()->getMinDeductionAmountCalculationType()) {
  166. case 'FixedAmount':
  167. $this->orderGoodsDeductionMinAmount = new FixedAmount($this->orderGoods, $this->getGoodsDeduction());
  168. trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最小限额使用固定金额");
  169. break;
  170. case 'GoodsPriceProportion':
  171. $this->orderGoodsDeductionMinAmount = new GoodsPriceProportion($this->orderGoods, $this->getGoodsDeduction());
  172. trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最小限额使用固定比例");
  173. break;
  174. default:
  175. $this->orderGoodsDeductionMinAmount = new Invalid($this->orderGoods, $this->getGoodsDeduction());
  176. trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}未设置最小限额类型");
  177. break;
  178. }
  179. }
  180. return $this->orderGoodsDeductionMinAmount;
  181. }
  182. private function getGoodsDeduction()
  183. {
  184. if (!isset($this->goodsDeduction)) {
  185. $this->goodsDeduction = app('DeductionManager')->make('GoodsDeductionManager')->make($this->code, [$this->orderGoods->goods]);
  186. }
  187. return $this->goodsDeduction;
  188. }
  189. private $hasMinLimitBuyCoin;
  190. public function hasMinLimitBuyCoin()
  191. {
  192. if (isset($this->hasMinLimitBuyCoin)) {
  193. return $this->hasMinLimitBuyCoin;
  194. }
  195. return $this->hasMinLimitBuyCoin = $this->_hasMinLimitBuyCoin();
  196. }
  197. protected function _hasMinLimitBuyCoin()
  198. {
  199. if (!$this->getGoodsDeduction() || !$this->getGoodsDeduction()->deductible($this->orderGoods->goods)) {
  200. //todo 如果抵扣未开启,获取抵扣为空,返回了抵扣类,判断为true了,所以为必选中
  201. return false;
  202. // 购买商品不存在抵扣记录
  203. // return $this->newCoin();
  204. }
  205. return $this->getOrderGoodsMinDeductionAmount()->hasMinAmount();
  206. }
  207. /**
  208. * 最低使用虚拟币
  209. * @return mixed
  210. */
  211. public function getMinLimitBuyCoin()
  212. {
  213. if (isset($this->minLimitBuyCoin)) {
  214. return $this->minLimitBuyCoin;
  215. }
  216. return $this->minLimitBuyCoin = $this->_getMinLimitBuyCoin();
  217. }
  218. /**
  219. * 最低使用虚拟币
  220. * @return mixed
  221. */
  222. protected function _getMinLimitBuyCoin()
  223. {
  224. if (!$this->getGoodsDeduction() || !$this->getGoodsDeduction()->deductible($this->orderGoods->goods)) {
  225. // 购买商品不存在抵扣记录
  226. return $this->newCoin();
  227. }
  228. $amount = $this->getOrderGoodsMinDeductionAmount()->getMinAmount();
  229. $handleType = $this->getOrderDeduction()->getDeduction()->getAffectDeductionAmount();
  230. if ($handleType == 'integer') {
  231. $amount = intval($amount);
  232. }
  233. $coin = $this->newCoin()->setMoney($amount);
  234. trace_log()->deduction("订单抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}最少需要抵扣{$coin->getMoney()}元");
  235. return $coin;
  236. }
  237. /**
  238. * 获取订单商品可用的虚拟币
  239. * @return VirtualCoin
  240. */
  241. public function getUsableCoin()
  242. {
  243. if (isset($this->usablePoint)) {
  244. return $this->usablePoint;
  245. }
  246. return $this->usablePoint = $this->_getUsableCoin();
  247. }
  248. /**
  249. * 获取订单商品可用的虚拟币
  250. * @return $this|VirtualCoin
  251. */
  252. private function _getUsableCoin()
  253. {
  254. if (!$this->getGoodsDeduction() || !$this->getGoodsDeduction()->deductible($this->orderGoods->goods)) {
  255. trace_log()->deduction('订单商品抵扣', "{$this->name} 购买商品不存在抵扣记录");
  256. // 购买商品不存在抵扣记录
  257. return $this->newCoin();
  258. }
  259. $amount = $this->getOrderGoodsMaxDeductionAmount()->getMaxAmount();
  260. $coin = $this->newCoin()->setMoney($amount);
  261. trace_log()->deduction("订单商品抵扣", "{$this->name} 商品{$this->orderGoods->goods_id}可抵扣{$coin->getMoney()}元");
  262. return $coin;
  263. }
  264. /**
  265. * @return $this|VirtualCoin
  266. * @throws \Exception
  267. */
  268. public function _getUsedCoin()
  269. {
  270. // 未选中
  271. if (!$this->getOrderDeduction()->isChecked()) {
  272. return $this->newCoin();
  273. }
  274. // 没有可用抵扣金额
  275. if ($this->getUsableCoin()->getMoney() <= 0) {
  276. return $this->newCoin();
  277. }
  278. // 订单商品最低抵扣
  279. $amount = $this->getMinLimitBuyCoin()->getMoney();
  280. // 订单所有同类型的剩余抵扣
  281. $restAllDeductionAmount = $this->getOrderDeduction()->getMaxOrderGoodsDeduction()->getMoney()
  282. - $this->getOrderDeduction()->getMinDeduction()->getMoney();
  283. // dump($this->getOrderDeduction()->getCode().'--'.$this->orderGoods->goods_id);
  284. $restDeductionAmount = $this->getOrderDeduction()->getOrderGoodsDeductionAmount()
  285. - $this->getOrderDeduction()->getMinDeduction()->getMoney()
  286. - $this->getOrderDeduction()->getUsableFreightDeduction()->getMoney();
  287. // dump($restAllDeductionAmount, $restDeductionAmount);
  288. if ($restDeductionAmount) {
  289. // dump($this->getUsableCoin()->getMoney(), $restAllDeductionAmount, $restDeductionAmount);
  290. // 订单商品的剩余抵扣/ 订单所有同类型的剩余抵扣 获取订单商品占用的抵扣金额-d
  291. $amount += ($this->getUsableCoin()->getMoney() - $this->getMinLimitBuyCoin()->getMoney()) / $restAllDeductionAmount * $restDeductionAmount;
  292. // dump($amount, '------');
  293. }
  294. return $this->newCoin()->setMoney($amount);
  295. }
  296. /**
  297. * @return VirtualCoin|PreOrderGoodsDeduction
  298. * @throws \Exception
  299. */
  300. public function getUsedCoin()
  301. {
  302. trace_log()->deduction('订单抵扣', "{$this->name} 订单商品计算已抵扣的虚拟币");
  303. if (isset($this->usedPoint)) {
  304. return $this->usedPoint;
  305. }
  306. return $this->usedPoint = $this->_getUsedCoin();
  307. }
  308. /**
  309. * @return bool
  310. * @throws \Exception
  311. */
  312. public function used()
  313. {
  314. return $this->getOrderDeduction()->isChecked() && $this->getUsedCoin()->getCoin() > 0;
  315. }
  316. public function toArray()
  317. {
  318. $this->code = (string)$this->code;
  319. $this->name = (string)$this->name;
  320. $this->usable_amount = (float)$this->usable_amount;
  321. $this->usable_coin = (float)$this->usable_coin;
  322. $this->used_amount = (float)$this->used_amount;
  323. $this->used_coin = (float)$this->used_coin;
  324. return parent::toArray();
  325. }
  326. /**
  327. * @param array $options
  328. * @return bool
  329. * @throws \Exception
  330. */
  331. public function save(array $options = [])
  332. {
  333. if (!$this->used()) {
  334. return true;
  335. }
  336. // 确保魔术属性最少执行一次
  337. $this->code = (string)$this->code;
  338. $this->name = (string)$this->name;
  339. $this->usable_amount = (float)$this->usable_amount;
  340. $this->usable_coin = (float)$this->usable_coin;
  341. $this->used_amount = (float)$this->used_amount;
  342. $this->used_coin = (float)$this->used_coin;
  343. return parent::save($options);
  344. }
  345. }