|
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, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\billing\hiapi\plan; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\billing\hiapi\models\relations\Bucket; |
|
14
|
|
|
use hiqdev\php\billing\plan\Plan; |
|
15
|
|
|
use hiqdev\php\billing\plan\PlanFactory; |
|
16
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
|
17
|
|
|
use hiqdev\php\billing\price\PriceInterface; |
|
18
|
|
|
use hiqdev\yii\DataMapper\components\ConnectionInterface; |
|
19
|
|
|
use hiqdev\yii\DataMapper\query\Specification; |
|
20
|
|
|
use hiqdev\yii\DataMapper\repositories\BaseRepository; |
|
21
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
|
22
|
|
|
use hiqdev\php\billing\customer\Customer; |
|
23
|
|
|
use hiqdev\php\billing\order\OrderInterface; |
|
24
|
|
|
use hiqdev\php\billing\plan\PlanFactoryInterface; |
|
25
|
|
|
use hiqdev\php\billing\plan\PlanRepositoryInterface; |
|
26
|
|
|
use Yii; |
|
27
|
|
|
|
|
28
|
|
|
class PlanRepository extends BaseRepository implements PlanRepositoryInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** {@inheritdoc} */ |
|
31
|
|
|
public $queryClass = PlanQuery::class; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var PlanFactory|PlanFactoryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $factory; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* PlanRepository constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param ConnectionInterface $db |
|
42
|
|
|
* @param PlanFactory|PlanFactoryInterface $factory |
|
43
|
|
|
* @param array $config |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( |
|
46
|
|
|
ConnectionInterface $db, |
|
47
|
|
|
PlanFactoryInterface $factory, |
|
48
|
|
|
array $config = [] |
|
49
|
|
|
) { |
|
50
|
|
|
parent::__construct($config); |
|
51
|
|
|
|
|
52
|
|
|
$this->db = $db; |
|
53
|
|
|
$this->factory = $factory; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array $row |
|
58
|
|
|
* @return Plan|PlanInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
public function create(array $row) |
|
61
|
|
|
{ |
|
62
|
|
|
$row['seller'] = $this->createEntity(Customer::class, $row['seller'] ?? []); |
|
63
|
|
|
$raw_prices = $row['prices']; |
|
64
|
|
|
unset($row['prices']); |
|
65
|
|
|
/** @var Plan $plan */ |
|
66
|
|
|
$plan = parent::create($row); |
|
67
|
|
|
if (is_array($raw_prices)) { |
|
68
|
|
|
$prices = []; |
|
69
|
|
|
foreach ($raw_prices as $key => $price) { |
|
70
|
|
|
$price['plan'] = $plan; |
|
71
|
|
|
$prices[$key] = $this->createEntity(PriceInterface::class, $price); |
|
72
|
|
|
} |
|
73
|
|
|
$plan->setPrices($prices); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $plan; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param ActionInterface $action |
|
81
|
|
|
* @return PlanInterface |
|
82
|
|
|
*/ |
|
83
|
|
|
public function findByAction(ActionInterface $action) |
|
84
|
|
|
{ |
|
85
|
|
|
$client_id = $action->getCustomer()->getId(); |
|
86
|
|
|
$seller = $action->getCustomer()->getSeller()->getLogin(); |
|
87
|
|
|
$type = $action->getTarget()->getType(); |
|
88
|
|
|
|
|
89
|
|
|
$spec = Yii::createObject(Specification::class) |
|
90
|
|
|
->with('prices') |
|
91
|
|
|
->where([ |
|
92
|
|
|
'type-name' => $type, |
|
93
|
|
|
'available_for' => [ |
|
94
|
|
|
'client_id' => $client_id, |
|
95
|
|
|
'seller' => $seller, |
|
96
|
|
|
], |
|
97
|
|
|
]); |
|
98
|
|
|
|
|
99
|
|
|
return $this->findOne($spec); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param OrderInterface $order |
|
104
|
|
|
* @return Plan[]|PlanInterface[] |
|
105
|
|
|
*/ |
|
106
|
|
|
public function findByOrder(OrderInterface $order) |
|
107
|
|
|
{ |
|
108
|
|
|
return array_map([$this, 'findByAction'], $order->getActions()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function findByIds(array $ids) |
|
112
|
|
|
{ |
|
113
|
|
|
$spec = Yii::createObject(Specification::class) |
|
114
|
|
|
->with('prices') |
|
115
|
|
|
->where(['id' => $ids]); |
|
116
|
|
|
|
|
117
|
|
|
return $this->findAll($spec); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected function joinPrices(&$rows) |
|
121
|
|
|
{ |
|
122
|
|
|
$bucket = Bucket::fromRows($rows, 'id'); |
|
123
|
|
|
$spec = (new Specification())->where(['plan-id' => $bucket->getKeys()]); |
|
124
|
|
|
$prices = $this->getRepository(PriceInterface::class)->queryAll($spec); |
|
125
|
|
|
$bucket->fill($prices, 'plan.id', 'id'); |
|
126
|
|
|
$bucket->pour($rows, 'prices'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|