Order.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018-06-27
  6. * Time: 12:35
  7. */
  8. namespace iBrand\Component\Discount\Test\Models;
  9. use iBrand\Component\Discount\Contracts\DiscountSubjectContract;
  10. use Illuminate\Database\Eloquent\Model;
  11. class Order extends Model implements DiscountSubjectContract
  12. {
  13. protected $guarded=['id'];
  14. public function __construct(array $attributes = [])
  15. {
  16. $this->setTable(config('ibrand.app.database.prefix', 'ibrand_') . 'order');
  17. parent::__construct($attributes);
  18. $this->status = 0;
  19. }
  20. public function save(array $options = [])
  21. {
  22. $order = parent::save($options); // TODO: Change the autogenerated stub
  23. $this->items()->saveMany($this->getItems());
  24. $this->adjustments()->saveMany($this->getAdjustments());
  25. return $order;
  26. }
  27. public function items()
  28. {
  29. return $this->hasMany(OrderItem::class);
  30. }
  31. public function adjustments()
  32. {
  33. return $this->hasMany(Adjustment::class);
  34. }
  35. /**
  36. * get subject total amount.
  37. *
  38. * @return int
  39. */
  40. public function getSubjectTotal()
  41. {
  42. return $this->items_total;
  43. }
  44. /**
  45. * get subject count item.
  46. *
  47. * @return int
  48. */
  49. public function getSubjectCount()
  50. {
  51. return $this->count;
  52. }
  53. /**
  54. * get subject items.
  55. *
  56. * @return mixed
  57. */
  58. public function getItems()
  59. {
  60. return $this->items;
  61. }
  62. /**
  63. * get subject count.
  64. *
  65. * @return mixed
  66. */
  67. public function countItems()
  68. {
  69. return $this->items->count();
  70. }
  71. public function getAdjustments()
  72. {
  73. return $this->adjustments;
  74. }
  75. /**
  76. * @param $adjustment
  77. *
  78. * @return mixed
  79. */
  80. public function addAdjustment($adjustment)
  81. {
  82. if (!$this->hasAdjustment($adjustment)) {
  83. $this->adjustments->add($adjustment);
  84. $this->addToAdjustmentsTotal($adjustment);
  85. }
  86. }
  87. public function hasAdjustment(Adjustment $adjustment)
  88. {
  89. return $this->adjustments->contains(function ($value, $key) use ($adjustment) {
  90. if ($adjustment->order_item_id) {
  91. return $adjustment->origin_type == $value->origin_type
  92. AND $adjustment->origin_id == $value->origin_id
  93. AND $adjustment->order_item_id == $value->order_item_id;
  94. }
  95. return $adjustment->origin_type == $value->origin_type
  96. AND $adjustment->origin_id == $value->origin_id;
  97. });
  98. }
  99. protected function addToAdjustmentsTotal(Adjustment $adjustment)
  100. {
  101. $this->adjustments_total += $adjustment->amount;
  102. $this->recalculateTotal();
  103. }
  104. public function recalculateTotal()
  105. {
  106. $this->total = $this->items_total + $this->adjustments_total;
  107. if ($this->total < 0) {
  108. $this->total = 0;
  109. }
  110. }
  111. /**
  112. * get subject user.
  113. *
  114. * @return mixed
  115. */
  116. public function getSubjectUser()
  117. {
  118. // TODO: Implement getSubjectUser() method.
  119. }
  120. /**
  121. * get current total.
  122. *
  123. * @return mixed
  124. */
  125. public function getCurrentTotal()
  126. {
  127. return $this->total;
  128. }
  129. /**
  130. * get subject is paid.
  131. *
  132. * @return mixed
  133. */
  134. public function isPaid()
  135. {
  136. return $this->pay_status;
  137. }
  138. public function addItem(OrderItem $item)
  139. {
  140. if ($this->hasItem($item) AND !isset($item->item_meta['dynamic_sku'])) {
  141. return;
  142. }
  143. $this->items_total += $item->getTotal();
  144. $this->items->add($item);
  145. $this->recalculateTotal();
  146. $this->recalculateCount();
  147. }
  148. public function hasItem(OrderItem $item)
  149. {
  150. return $this->items->contains(function ($value, $key) use ($item) {
  151. return $item->item_id == $value->item_id AND $item->type = $value->type;
  152. });
  153. }
  154. protected function recalculateCount()
  155. {
  156. $this->count = $this->getTotalQuantity();
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getTotalQuantity()
  162. {
  163. $quantity = 0;
  164. foreach ($this->items as $item) {
  165. $quantity += $item->quantity;
  166. }
  167. return $quantity;
  168. }
  169. public function recalculateItemsTotal()
  170. {
  171. $items_total = 0;
  172. foreach ($this->items as $item) {
  173. $items_total += $item->total;
  174. }
  175. $this->items_total = $items_total;
  176. $this->recalculateTotal();
  177. }
  178. }