PreOrderDeduction.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2017/7/25
  6. * Time: 下午7:33
  7. */
  8. namespace app\frontend\models\order;
  9. use app\backend\modules\order\services\models\ExcelModel;
  10. use app\common\exceptions\MinOrderDeductionNotEnough;
  11. use app\common\models\order\OrderDeduction;
  12. use app\common\models\VirtualCoin;
  13. use app\frontend\models\MemberCoin;
  14. use app\frontend\modules\deduction\models\Deduction;
  15. use app\frontend\modules\deduction\OrderGoodsDeductionCollection;
  16. use app\frontend\modules\deduction\orderGoods\PreOrderGoodsDeduction;
  17. use app\frontend\modules\order\models\PreOrder;
  18. /**
  19. * 订单抵扣类
  20. * Class PreOrderDeduction
  21. * @package app\frontend\models\order
  22. * @property int uid
  23. * @property int coin
  24. * @property int amount
  25. * @property int name
  26. * @property int code
  27. */
  28. class PreOrderDeduction extends OrderDeduction
  29. {
  30. protected $appends = ['checked'];
  31. /**
  32. * @var PreOrder
  33. */
  34. public $order;
  35. /**
  36. * @var Deduction
  37. */
  38. private $deduction;
  39. /**
  40. * @var MemberCoin
  41. */
  42. private $memberCoin;
  43. /**
  44. * @var OrderGoodsDeductionCollection
  45. */
  46. private $orderGoodsDeductionCollection;
  47. /**
  48. * 订单实付使用商品抵扣
  49. * @var VirtualCoin
  50. */
  51. private $usablePoint;
  52. /**
  53. * 实际使用运费抵扣
  54. * @var VirtualCoin
  55. */
  56. protected $usableFreightDeduction;
  57. /**
  58. * @param Deduction $deduction
  59. * @param PreOrder $order
  60. * @param OrderGoodsDeductionCollection $orderGoodsDeductionCollection
  61. */
  62. public function init(
  63. Deduction $deduction,
  64. PreOrder $order,
  65. OrderGoodsDeductionCollection $orderGoodsDeductionCollection)
  66. {
  67. $this->deduction = $deduction;
  68. $this->setOrder($order);
  69. $this->setOrderGoodsDeductionCollection($orderGoodsDeductionCollection);
  70. $this->orderGoodsDeductionCollection->each(function (PreOrderGoodsDeduction $orderGoodsDeduction) {
  71. $orderGoodsDeduction->setOrderDeduction($this);
  72. });
  73. }
  74. public function getUidAttribute()
  75. {
  76. return $this->order->uid;
  77. }
  78. public function getCodeAttribute()
  79. {
  80. return $this->getCode();
  81. }
  82. public function getNameAttribute()
  83. {
  84. return $this->getName();
  85. }
  86. /**
  87. * 最终抵扣值 = (最低抵扣+最高商品抵扣+运费抵扣)
  88. * @return float|int
  89. * @throws \app\common\exceptions\AppException
  90. */
  91. public function getCoinAttribute()
  92. {
  93. //todo 为了保证显示不超过两位小数,这里使用了四舍五入
  94. $coin = $this->getMinDeduction()->getCoin() + $this->getUsablePoint()->getCoin() + $this->getUsableFreightDeduction()->getCoin();
  95. //return $coin;
  96. return round($coin,2,PHP_ROUND_HALF_EVEN);
  97. }
  98. /**
  99. * @return mixed
  100. * @throws \app\common\exceptions\AppException
  101. */
  102. public function getAmountAttribute()
  103. {
  104. //return $this->getAmount();
  105. return round($this->getAmount(),2,PHP_ROUND_HALF_EVEN);
  106. }
  107. /**
  108. * 最终抵扣金额 = (最低抵扣+最高商品抵扣+运费抵扣)
  109. * @return mixed
  110. * @throws \app\common\exceptions\AppException
  111. */
  112. public function getAmount()
  113. {
  114. // dump($this->getMinDeduction()->getMoney(),$this->getUsablePoint()->getMoney(),$this->getUsableFreightDeduction()->getMoney());
  115. return $this->getMinDeduction()->getMoney() + $this->getUsablePoint()->getMoney() + $this->getUsableFreightDeduction()->getMoney();
  116. }
  117. public function getOrder()
  118. {
  119. return $this->order;
  120. }
  121. /**
  122. * @return Deduction
  123. */
  124. public function getDeduction()
  125. {
  126. return $this->deduction;
  127. }
  128. /**
  129. * @param PreOrder $order
  130. */
  131. private function setOrder(PreOrder $order)
  132. {
  133. $this->order = $order;
  134. }
  135. /**
  136. * 下单时此抵扣可选
  137. * @return bool
  138. */
  139. public function deductible()
  140. {
  141. return $this->amount > 0;
  142. }
  143. /**
  144. * 实例化并绑定所有的订单商品抵扣实例,集合 并将集合绑定在订单抵扣上
  145. * @param OrderGoodsDeductionCollection $orderGoodsDeductionCollection
  146. */
  147. private function setOrderGoodsDeductionCollection(OrderGoodsDeductionCollection $orderGoodsDeductionCollection)
  148. {
  149. $this->orderGoodsDeductionCollection = $orderGoodsDeductionCollection;
  150. }
  151. /**
  152. * 下单用户此抵扣对应虚拟币的余额
  153. * @return MemberCoin
  154. */
  155. public function getMemberCoin()
  156. {
  157. if (isset($this->memberCoin)) {
  158. return $this->memberCoin;
  159. }
  160. $code = $this->getCode();
  161. return \app\frontend\modules\deduction\EnableDeductionService::getInstance()->getMemberCoin($code);
  162. //return app('CoinManager')->make('MemberCoinManager')->make($code, [$this->order->belongsToMember]);
  163. }
  164. /**
  165. * 此抵扣对应的虚拟币
  166. * @return VirtualCoin
  167. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  168. */
  169. public function newCoin()
  170. {
  171. return app('CoinManager')->make($this->getCode());
  172. }
  173. public function openFreightDeduction()
  174. {
  175. return $this->getDeduction()->isEnableDeductDispatchPrice();
  176. }
  177. /**
  178. * 订单剩余运费,最高抵扣金额
  179. * @return VirtualCoin
  180. */
  181. public function getMaxFreightPriceDeduction()
  182. {
  183. $result = $this->newCoin();
  184. trace_log()->freight("监听抵扣", $this->getDeduction()->getName() . '运费抵扣开启状态' . $this->openFreightDeduction());
  185. //开关
  186. if ($this->openFreightDeduction()) {
  187. $amount = $this->order->getFreightManager()->getFinalFreightAmount();
  188. $result->setMoney($amount);
  189. trace_log()->freight("监听抵扣运费", $this->getDeduction()->getName() . '运费可抵扣金额');
  190. }
  191. return $result;
  192. }
  193. /**
  194. * 订单实际可用的此抵扣运费
  195. * @return VirtualCoin
  196. */
  197. public function getUsableFreightDeduction()
  198. {
  199. if (!isset($this->usableFreightDeduction)) {
  200. trace_log()->deduction('开始订单抵扣运费', "{$this->getName()} 计算可用运费抵扣金额");
  201. $this->usableFreightDeduction = $this->newCoin();
  202. // 购买者不存在虚拟币记录
  203. if (!$this->getMemberCoin()) {
  204. trace_log()->deduction('订单抵扣运费', "{$this->getName()} 用户没有对应虚拟币");
  205. return $this->usableFreightDeduction;
  206. }
  207. if (!$this->openFreightDeduction()) {
  208. trace_log()->deduction('订单抵扣运费', "{$this->getName()} 未开启运费抵扣");
  209. return $this->usableFreightDeduction;
  210. }
  211. //订单运费抵扣金额 不能超过订单运费当前抵扣项之前的金额
  212. $deductionAmount = min(
  213. $this->order->getDispatchAmount(),
  214. $this->getMaxFreightPriceDeduction()->getMoney()
  215. );
  216. //如果资产设置的转化比例小数点过低会,造成资产小数位数超过两位这样会造成 抵扣值和抵扣金额不对应
  217. $memberMaxUsableCoin = floor($this->getMemberCoin()->getMaxUsableCoin()->getMoney() * 100) / 100;
  218. // 运费抵扣金额(用户可用虚拟币 - 最低抵扣金额) 与运费抵扣金额虚拟币的最小值
  219. $amount = min(
  220. $memberMaxUsableCoin - $this->getMinDeduction()->getMoney(),
  221. $deductionAmount
  222. );
  223. //判断是否开启运费抵扣,开启运费抵扣需要把抵扣添加到运费的抵扣节点
  224. $this->order->getFreightManager()->pushDeductionPricePipe($this);
  225. $this->usableFreightDeduction = $this->newCoin()->setMoney($amount);
  226. trace_log()->deduction("订单抵扣运费", "{$this->name} 可抵扣{$this->usableFreightDeduction->getMoney()}元");
  227. }
  228. return $this->usableFreightDeduction;
  229. }
  230. /**
  231. * 订单中实际可用的此抵扣
  232. * @return $this|VirtualCoin
  233. * @throws \app\common\exceptions\AppException
  234. */
  235. public function getUsablePoint()
  236. {
  237. if (!isset($this->usablePoint)) {
  238. trace_log()->deduction('开始订单抵扣', "{$this->getName()} 计算可用金额");
  239. $this->usablePoint = $this->newCoin();
  240. // 购买者不存在虚拟币记录
  241. if (!$this->getMemberCoin()) {
  242. trace_log()->deduction('订单抵扣', "{$this->getName()} 用户没有对应虚拟币");
  243. return $this->usablePoint;
  244. }
  245. // 商品金额抵扣 不能超过订单当前抵扣项之前(去除运费)的金额
  246. //todo 这个有问题,当没有设置最高抵扣时只设置了最低抵扣 这里(最高-最低)减了会变为负数,为什么之前为负数也能显示出抵扣呢???
  247. $deductionAmount = min(
  248. $this->order->getPriceBefore($this->getCode() . 'RestDeduction') - $this->order->getDispatchAmount(),
  249. $this->getMaxDeduction()->getMoney() - $this->getMinDeduction()->getMoney()
  250. );
  251. //todo 修复抵扣金额为负数,这样就一定要保证最高抵扣的设置要大于最低抵扣,如小于可能会有问题
  252. //只会显示最低抵扣的金额
  253. $deductionAmount = max($deductionAmount, 0);
  254. //如果资产设置的转化比例小数点过低会,造成资产小数位数超过两位这样会造成 抵扣值和抵扣金额不对应
  255. $memberMaxUsableCoin = floor($this->getMemberCoin()->getMaxUsableCoin()->getMoney() * 100) / 100;
  256. // 用户可用虚拟币-最低抵扣-运费抵扣 与订单抵扣虚拟币的最小值 todo 如果以后需要一种抵扣币抵扣两次时,会产生bug
  257. $amount = min(
  258. $memberMaxUsableCoin - $this->getMinDeduction()->getMoney() - $this->getUsableFreightDeduction()->getMoney(),
  259. $deductionAmount
  260. );
  261. //整数抵扣
  262. $handleType = $this->getDeduction()->getAffectDeductionAmount();
  263. if ($handleType == 'integer') {
  264. $amount = intval($amount);
  265. }
  266. $this->usablePoint = $this->newCoin()->setMoney($amount);
  267. trace_log()->deduction("订单抵扣", "{$this->name} 可抵扣{$this->usablePoint->getMoney()}元");
  268. }
  269. return $this->usablePoint;
  270. }
  271. /**
  272. * 获取订单商品占用的抵扣金额
  273. * @return float|int
  274. */
  275. public function getOrderGoodsDeductionAmount()
  276. {
  277. //blank 这里不能加运费,运费不分摊到商品金额里
  278. $amount = ($this->getMaxOrderGoodsDeduction()->getMoney() / $this->getMaxDeduction()->getMoney()) * $this->amount;
  279. //dump($this->getMaxOrderGoodsDeduction()->getMoney(), $this->getMaxDeduction()->getMoney(), $this->getUsableFreightDeduction()->getMoney());
  280. //dump($this->amount, $amount, '---');
  281. // $amount = ($this->getMaxOrderGoodsDeduction()->getMoney() / ($this->getMaxDeduction()->getMoney()+$this->getMaxDispatchPriceDeduction()->getMoney())) * $this->amount;
  282. return $amount;
  283. }
  284. /**
  285. * @var VirtualCoin
  286. */
  287. private $maxDeduction;
  288. /**
  289. * 订单中此抵扣可用最大值
  290. * @return VirtualCoin
  291. */
  292. private function getMaxDeduction()
  293. {
  294. if (!isset($this->maxDeduction)) {
  295. $this->maxDeduction = $this->getMaxOrderGoodsDeduction();
  296. trace_log()->deduction('订单抵扣', "{$this->getName()} 计算最大抵扣{$this->maxDeduction}");
  297. }
  298. return $this->maxDeduction;
  299. }
  300. /**
  301. * @var VirtualCoin
  302. */
  303. private $minDeduction;
  304. /**
  305. * 订单中此抵扣可用最小值
  306. * @return VirtualCoin
  307. */
  308. public function getMinDeduction()
  309. {
  310. if (!isset($this->minDeduction)) {
  311. $this->minDeduction = $this->getMinOrderGoodsDeduction();
  312. trace_log()->deduction('订单抵扣', "{$this->getName()} 计算最小抵扣{$this->minDeduction->getMoney()}元");
  313. }
  314. return $this->minDeduction;
  315. }
  316. /**
  317. * 最多可抵扣商品金额的虚拟币
  318. * 累加所有订单商品的可用虚拟币
  319. * @return VirtualCoin
  320. */
  321. public function getMaxOrderGoodsDeduction()
  322. {
  323. return $this->getOrderGoodsDeductionCollection()->getUsablePoint();
  324. }
  325. /**
  326. * 最低抵扣商品金额的虚拟币
  327. * 累加所有订单商品的可用虚拟币
  328. * @return VirtualCoin
  329. */
  330. public function getMinOrderGoodsDeduction()
  331. {
  332. return $this->getOrderGoodsDeductionCollection()->getMinPoint();
  333. }
  334. /**
  335. * @return OrderGoodsDeductionCollection
  336. */
  337. public function getOrderGoodsDeductionCollection()
  338. {
  339. return $this->orderGoodsDeductionCollection;
  340. }
  341. /**
  342. * @return string
  343. */
  344. public function getCode()
  345. {
  346. return $this->getDeduction()->getCode();
  347. }
  348. /**
  349. * @return string
  350. */
  351. public function getName()
  352. {
  353. return $this->getDeduction()->getName();
  354. }
  355. /**
  356. * @return bool
  357. */
  358. public function getCheckedAttribute()
  359. {
  360. return $this->isChecked();
  361. }
  362. private $isChecked;
  363. public function setChecked()
  364. {
  365. $this->isChecked = true;
  366. }
  367. private $mustBeChecked;
  368. /**
  369. * 必须选中
  370. * @return bool
  371. */
  372. public function mustBeChecked()
  373. {
  374. if(!$this->mustBeChecked){
  375. // 设置了最低抵扣必须选中
  376. return $this->getOrderGoodsDeductionCollection()->hasMinDeduction() > 0;
  377. }
  378. return $this->mustBeChecked;
  379. }
  380. /**
  381. * 选择了此抵扣
  382. * @return bool
  383. */
  384. public function isChecked()
  385. {
  386. if (!isset($this->isChecked)) {
  387. if (!$this->order->uid) {
  388. return $this->isChecked = false;
  389. }
  390. if ($this->mustBeChecked()) {
  391. // 必须选中
  392. $this->isChecked = true;
  393. } elseif (!$this->order->getRequest()->no_deduction_ids &&
  394. \Setting::get('point.set')['default_deduction'] &&
  395. ($this->order->plugin_id == 0 || $this->order->plugin_id == 92)){//no_deduction_ids 新添参数,状态为空和设置为1 则开启默认抵扣按钮
  396. $this->isChecked = $this->getCode() == 'point';//添加默认开启积分抵扣 ,暂时只做积分的
  397. }else {
  398. // 用户选中
  399. $deduction_codes = $this->order->getParams('deduction_ids');
  400. if (!is_array($deduction_codes)) {
  401. $deduction_codes = json_decode($deduction_codes, true);
  402. if (!is_array($deduction_codes)) {
  403. $deduction_codes = explode(',', $deduction_codes);
  404. }
  405. }
  406. $this->isChecked = in_array($this->getCode(), $deduction_codes);
  407. }
  408. }
  409. return $this->isChecked;
  410. }
  411. /**
  412. * @return array
  413. */
  414. public function toArray()
  415. {
  416. $this->code = (string)$this->code;
  417. $this->name = (string)$this->name;
  418. $this->amount = sprintf('%.2f', $this->amount);
  419. $this->coin = sprintf('%.2f', $this->coin);
  420. return parent::toArray();
  421. }
  422. /**
  423. * @throws \app\common\exceptions\AppException
  424. */
  425. public function lock()
  426. {
  427. // 抵扣被选中后,锁定要使用的虚拟币额度
  428. $this->getMemberCoin()->lockCoin($this->coin);
  429. }
  430. /**
  431. * @return bool
  432. */
  433. public function beforeSaving()
  434. {
  435. if (!$this->isChecked() || ($this->getOrderGoodsDeductionCollection()->getUsablePoint() <= 0 && $this->getUsableFreightDeduction()->getMoney() <= 0)) {
  436. return false;
  437. }
  438. //抵扣金额小于0不保存
  439. if (bccomp($this->amount,0,2) !== 1) {
  440. return false;
  441. }
  442. $this->getMemberCoin()->consume($this->newCoin()->setMoney($this->amount), ['order_sn' => $this->order->order_sn]);
  443. $this->code = (string)$this->code;
  444. $this->name = (string)$this->name;
  445. $this->amount = sprintf('%.2f', $this->amount);
  446. $this->coin = sprintf('%.2f', $this->coin);
  447. return parent::beforeSaving();
  448. }
  449. /**
  450. * @throws MinOrderDeductionNotEnough
  451. */
  452. public function validateCoin()
  453. {
  454. // 验证最低抵扣大于可用抵扣
  455. if (bccomp($this->getMemberCoin()->getMaxUsableCoin()->getMoney(), $this->getMinDeduction()->getMoney(),2) === -1) {
  456. throw new MinOrderDeductionNotEnough("会员[{$this->getName()}]抵扣余额可抵扣金额{$this->getMemberCoin()->getMaxUsableCoin()->getMoney()}元,不满足最低抵扣金额{$this->getMinDeduction()->getMoney()}元");
  457. }
  458. //
  459. // if ($this->getMemberCoin()->getMaxUsableCoin()->getMoney() < $this->getMinDeduction()->getMoney()) {
  460. // throw new MinOrderDeductionNotEnough("会员[{$this->getName()}]抵扣余额可抵扣金额{$this->getMemberCoin()->getMaxUsableCoin()->getMoney()}元,不满足最低抵扣金额{$this->getMinDeduction()->getMoney()}元");
  461. // }
  462. //最低抵扣 + 运费抵扣 必须大于可用抵扣
  463. // $mustDeductionMoney = $this->getMinDeduction()->getMoney() + $this->getUsableFreightDeduction()->getMoney();
  464. // if (bccomp($this->getMemberCoin()->getMaxUsableCoin()->getMoney(), $mustDeductionMoney,2) === -1) {
  465. // throw new MinOrderDeductionNotEnough("会员[{$this->getName()}]抵扣余额可抵扣金额{$this->getMemberCoin()->getMaxUsableCoin()->getMoney()}元,不满足{$mustDeductionMoney}元(最低抵扣[{$this->getMinDeduction()->getMoney()}] + 运费抵扣[{$this->getUsableFreightDeduction()->getMoney()}])");
  466. // }
  467. }
  468. }