|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Treestoneit\ShoppingCart\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Support\Facades\Config; |
|
7
|
|
|
use Treestoneit\ShoppingCart\Models\CartItem; |
|
8
|
|
|
use Treestoneit\ShoppingCart\Models\CartItemCollection; |
|
9
|
|
|
use Treestoneit\ShoppingCart\Taxable; |
|
10
|
|
|
|
|
11
|
|
|
trait CalculatesTotals |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var float |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $subtotal = 0.0; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var float |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $tax = 0.0; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get the cart contents. |
|
25
|
|
|
* |
|
26
|
|
|
* @return \Treestoneit\ShoppingCart\Models\CartItemCollection |
|
27
|
|
|
*/ |
|
28
|
|
|
public function content(): CartItemCollection |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->items(); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get the number of items in the cart. |
|
35
|
|
|
* |
|
36
|
|
|
* @return int |
|
37
|
|
|
*/ |
|
38
|
|
|
public function count(): int |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->items()->sum('quantity'); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the subtotal of items in the cart. |
|
45
|
|
|
* |
|
46
|
|
|
* @return int|float |
|
47
|
|
|
*/ |
|
48
|
|
|
public function subtotal(): float |
|
49
|
|
|
{ |
|
50
|
|
|
if (! $this->subtotal) { |
|
51
|
|
|
$this->subtotal = $this->items()->sumRounded(function (CartItem $item) { |
|
|
|
|
|
|
52
|
|
|
return $item->subtotal; |
|
53
|
|
|
}); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->subtotal; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the tax for items in the cart. |
|
61
|
|
|
* |
|
62
|
|
|
* @param int|float|null $rate |
|
63
|
|
|
* @return float |
|
64
|
|
|
*/ |
|
65
|
|
|
public function tax($rate = null): float |
|
66
|
|
|
{ |
|
67
|
|
|
if (! $this->tax) { |
|
68
|
|
|
$this->tax = $this->items()->sumRounded($this->getTaxAmountForItem($rate)); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->tax; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Clear the cached totals. |
|
76
|
|
|
* |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
|
|
public function clearCached(): void |
|
80
|
|
|
{ |
|
81
|
|
|
$this->subtotal = 0.0; |
|
82
|
|
|
$this->tax = 0.0; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Figure out how to calculate tax for the cart items. |
|
87
|
|
|
* |
|
88
|
|
|
* @param int|float|null $rate |
|
89
|
|
|
* @return \Closure |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getTaxAmountForItem($rate = null): Closure |
|
92
|
|
|
{ |
|
93
|
|
|
if (! $rate && Config::get('shopping-cart.tax.mode') == 'flat') { |
|
94
|
|
|
$rate = Config::get('shopping-cart.tax.rate'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return function (CartItem $item) use ($rate) { |
|
98
|
|
|
if (! $rate) { |
|
99
|
|
|
$rate = $item->buyable instanceof Taxable |
|
|
|
|
|
|
100
|
|
|
? $item->buyable->getTaxRate() |
|
101
|
|
|
: 0; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return round($item->price * $item->quantity * ($rate / 100), 2); |
|
105
|
|
|
}; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.