|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* API for Billing |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/billing-hiapi |
|
6
|
|
|
* @package billing-hiapi |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\billing\hiapi\charge; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\billing\hiapi\type\TypeSemantics; |
|
14
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
|
15
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
|
16
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Generalizer extends \hiqdev\php\billing\charge\Generalizer |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var TypeSemantics |
|
25
|
|
|
*/ |
|
26
|
|
|
private $typeSemantics; |
|
27
|
|
|
|
|
28
|
1 |
|
public function __construct(TypeSemantics $typeSemantics) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$this->typeSemantics = $typeSemantics; |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
public function generalizeType(ChargeInterface $charge): TypeInterface |
|
34
|
|
|
{ |
|
35
|
1 |
|
if ($charge->getParent() !== null) { |
|
36
|
|
|
$chargeType = $charge->getParent()->getPrice()->getType(); |
|
37
|
|
|
} else { |
|
38
|
1 |
|
$chargeType = $charge->getPrice()->getType(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
if ($this->typeSemantics->isMonthly($chargeType)) { |
|
42
|
|
|
return $this->typeSemantics->createMonthlyType(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
return $chargeType; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public function generalizeTarget(ChargeInterface $charge): TargetInterface |
|
49
|
|
|
{ |
|
50
|
1 |
|
return $this->moreGeneral($charge->getAction()->getTarget(), $charge->getPrice()->getTarget()); |
|
51
|
|
|
|
|
52
|
|
|
/* Sorry, to be removed later, older variants |
|
53
|
|
|
* 1: |
|
54
|
|
|
if (in_array($charge->getTarget()->getType(), ['certificate', 'domain'], TRUE)) { |
|
55
|
|
|
$priceTarget = $charge->getPrice()->getTarget(); |
|
56
|
|
|
if ($priceTarget->getId()) { |
|
57
|
|
|
return $priceTarget; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
return parent::generalizeTarget($charge); |
|
61
|
|
|
* 2: |
|
62
|
|
|
return $priceTarget->getId() ? $priceTarget : new Target($charge->getSale()->getPlan()->getId(), 'plan'); |
|
63
|
|
|
*/ |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function moreGeneral(TargetInterface $first, TargetInterface $other) |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->isMoreGeneral($first, $other) || !$other->hasId() ? $first : $other; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function lessGeneral(TargetInterface $first, TargetInterface $other) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->isMoreGeneral($first, $other) || !$first->hasId() ? $other : $first; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function isMoreGeneral(TargetInterface $first, TargetInterface $other) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$i = 0; |
|
79
|
|
|
$order = [ |
|
80
|
1 |
|
'domain' => ++$i, |
|
81
|
1 |
|
'zone' => ++$i, |
|
82
|
1 |
|
'certificate' => ++$i, |
|
83
|
1 |
|
'type' => ++$i, |
|
84
|
1 |
|
'part' => ++$i, |
|
85
|
1 |
|
'server' => ++$i, |
|
86
|
1 |
|
'device' => $i, |
|
87
|
1 |
|
'tariff' => ++$i, |
|
88
|
1 |
|
'ref' => ++$i, |
|
89
|
1 |
|
'' => ++$i, |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
1 |
|
$lhs = $order[(string) $first->getType()] ?? 0; |
|
93
|
1 |
|
$rhs = $order[(string) $other->getType()] ?? 0; |
|
94
|
|
|
|
|
95
|
1 |
|
return $lhs > $rhs; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|