|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of ibrand/discount. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) iBrand <https://www.ibrand.cc> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace iBrand\Component\Discount\Actions; |
|
13
|
|
|
|
|
14
|
|
|
use iBrand\Component\Discount\Contracts\DiscountActionContract; |
|
15
|
|
|
use iBrand\Component\Discount\Contracts\DiscountContract; |
|
16
|
|
|
use iBrand\Component\Discount\Contracts\DiscountSubjectContract; |
|
17
|
|
|
use iBrand\Component\Discount\Distributors\PercentageIntegerDistributor; |
|
18
|
|
|
use Illuminate\Support\Collection; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class OrderFixedDiscountAction. |
|
22
|
|
|
*/ |
|
23
|
|
View Code Duplication |
class OrderFixedDiscountAction extends DiscountAction implements DiscountActionContract |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
const TYPE = 'order_fixed_discount'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var IntegerDistributor |
|
29
|
|
|
*/ |
|
30
|
|
|
private $distributor; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* OrderFixedDiscountAction constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param IntegerDistributor $distributor |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function __construct(PercentageIntegerDistributor $distributor) |
|
38
|
|
|
{ |
|
39
|
2 |
|
$this->distributor = $distributor; |
|
|
|
|
|
|
40
|
2 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param DiscountSubjectContract $subject |
|
44
|
|
|
* @param array $configuration |
|
45
|
|
|
* @param DiscountContract $discount |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed|void |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function execute(DiscountSubjectContract $subject, array $configuration, DiscountContract $discount) |
|
50
|
|
|
{ |
|
51
|
1 |
|
$discountAmount = $this->calculateAdjustmentAmount($subject->getCurrentTotal(), $configuration['amount']); |
|
52
|
|
|
|
|
53
|
1 |
|
if (0 === $discountAmount) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
$adjustment = $this->createAdjustment($discount, $discountAmount); |
|
58
|
1 |
|
$subject->addAdjustment($adjustment); |
|
59
|
|
|
|
|
60
|
1 |
|
$splitDiscountAmount = $this->distributor->distribute($subject->getItems()->pluck('total')->toArray(), $discountAmount); |
|
61
|
|
|
|
|
62
|
1 |
|
$i = 0; |
|
63
|
1 |
|
foreach ($subject->getItems() as $item) { |
|
64
|
1 |
|
$item->divide_order_discount += $splitDiscountAmount[$i++]; |
|
65
|
1 |
|
$item->recalculateAdjustmentsTotal(); |
|
66
|
|
|
} |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $discountSubjectTotal |
|
71
|
|
|
* @param $targetDiscountAmount |
|
72
|
|
|
* |
|
73
|
|
|
* @return float|int |
|
74
|
|
|
*/ |
|
75
|
2 |
|
private function calculateAdjustmentAmount($discountSubjectTotal, $targetDiscountAmount) |
|
76
|
|
|
{ |
|
77
|
2 |
|
return -1 * min($discountSubjectTotal, $targetDiscountAmount); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param DiscountSubjectContract $subject |
|
82
|
|
|
* @param array $configuration |
|
83
|
|
|
* @param DiscountContract $discount |
|
84
|
|
|
* |
|
85
|
|
|
* @return float|int|mixed |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function calculate(DiscountSubjectContract $subject, array $configuration, DiscountContract $discount) |
|
88
|
|
|
{ |
|
89
|
1 |
|
$discount->adjustments = new Collection(); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
1 |
|
$discountAmount = $this->calculateAdjustmentAmount($subject->getCurrentTotal(), $configuration['amount']); |
|
92
|
|
|
|
|
93
|
1 |
|
$discount->adjustments->push(['order_id' => $subject->id, 'amount' => $discountAmount]); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
1 |
|
$discount->adjustmentTotal = $discountAmount; |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
1 |
|
return $discountAmount; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.