OrderDeductionCollection.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2018/11/15
  6. * Time: 4:04 PM
  7. */
  8. namespace app\frontend\modules\deduction;
  9. use app\common\exceptions\MinOrderDeductionNotEnough;
  10. use app\framework\Database\Eloquent\Collection;
  11. use app\frontend\models\order\PreOrderDeduction;
  12. use app\frontend\models\order\PreOrderDiscount;
  13. use app\frontend\modules\deduction\models\Deduction;
  14. use app\frontend\modules\deduction\orderGoods\PreOrderGoodsDeduction;
  15. use app\frontend\modules\order\models\PreOrder;
  16. class OrderDeductionCollection extends Collection
  17. {
  18. public function minAmount()
  19. {
  20. return $this->where('checked', 1)->sum(function (PreOrderDeduction $orderDeduction) {
  21. return $orderDeduction->getMinDeduction()->getMoney();
  22. });
  23. }
  24. public function usedAmount()
  25. {
  26. return $this->where('checked', 1)->sum('amount');
  27. }
  28. /**
  29. * 过滤掉不可抵扣的
  30. * @return $this
  31. */
  32. public function filterNotDeductible()
  33. {
  34. $this->items = $this->filter(function (PreOrderDeduction $orderDeduction) {
  35. return $orderDeduction->deductible();
  36. })->values()->all();
  37. return $this;
  38. }
  39. public function toArray()
  40. {
  41. $this->filterNotDeductible();
  42. return parent::toArray(); // TODO: Change the autogenerated stub
  43. }
  44. public function lock()
  45. {
  46. $this->each(function (PreOrderDeduction $orderDeduction) {
  47. return $orderDeduction->lock();
  48. });
  49. return $this;
  50. }
  51. /**
  52. * 按照必选排序
  53. * @return $this
  54. */
  55. public function sortOrderDeductionCollection()
  56. {
  57. // 按照选中状态排序
  58. $this->items = $this->sortByDesc(function (PreOrderDeduction $preOrderDeduction) {
  59. return $preOrderDeduction->mustBeChecked();
  60. })->all();
  61. return $this;
  62. }
  63. /**
  64. * @param mixed $value
  65. * @return $this
  66. */
  67. public function push($value)
  68. {
  69. parent::push($value);
  70. sort($this->items);
  71. return $this;
  72. }
  73. /**
  74. * @return $this
  75. */
  76. public function validate()
  77. {
  78. $this->each(function (PreOrderDeduction $orderDeduction) {
  79. $orderDeduction->validateCoin();
  80. });
  81. return $this;
  82. }
  83. }