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\yii\DataMapper\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\components\EntityManagerInterface; |
20
|
|
|
use hiqdev\yii\DataMapper\query\Specification; |
21
|
|
|
use hiqdev\yii\DataMapper\repositories\BaseRepository; |
22
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
23
|
|
|
use hiqdev\php\billing\customer\Customer; |
24
|
|
|
use hiqdev\php\billing\order\OrderInterface; |
25
|
|
|
use hiqdev\php\billing\plan\PlanFactoryInterface; |
26
|
|
|
use hiqdev\php\billing\plan\PlanRepositoryInterface; |
27
|
|
|
use Yii; |
28
|
|
|
|
29
|
|
|
class PlanRepository extends BaseRepository implements PlanRepositoryInterface |
30
|
|
|
{ |
31
|
|
|
/** {@inheritdoc} */ |
32
|
|
|
public $queryClass = PlanQuery::class; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ActionInterface $action |
36
|
|
|
* @return PlanInterface |
37
|
|
|
*/ |
38
|
|
|
public function findByAction(ActionInterface $action) |
39
|
|
|
{ |
40
|
|
|
$client_id = $action->getCustomer()->getId(); |
41
|
|
|
$seller = $action->getCustomer()->getSeller()->getLogin(); |
42
|
|
|
$type = $action->getTarget()->getType(); |
43
|
|
|
|
44
|
|
|
$spec = Yii::createObject(Specification::class) |
45
|
|
|
->with('prices') |
46
|
|
|
->where([ |
47
|
|
|
'type-name' => $type, |
48
|
|
|
'available_for' => [ |
49
|
|
|
'client_id' => $client_id, |
50
|
|
|
'seller' => $seller, |
51
|
|
|
], |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
return $this->findOne($spec); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param OrderInterface $order |
59
|
|
|
* @return Plan[]|PlanInterface[] |
60
|
|
|
*/ |
61
|
|
|
public function findByOrder(OrderInterface $order) |
62
|
|
|
{ |
63
|
|
|
return array_map([$this, 'findByAction'], $order->getActions()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function findByIds(array $ids) |
67
|
|
|
{ |
68
|
|
|
$spec = Yii::createObject(Specification::class) |
69
|
|
|
->with('prices') |
70
|
|
|
->where(['id' => $ids]); |
71
|
|
|
|
72
|
|
|
return $this->findAll($spec); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function joinPrices(&$rows) |
76
|
|
|
{ |
77
|
|
|
$bucket = Bucket::fromRows($rows, 'id'); |
78
|
|
|
$spec = (new Specification())->where(['plan-id' => $bucket->getKeys()]); |
79
|
|
|
$prices = $this->getRepository(PriceInterface::class)->queryAll($spec); |
80
|
|
|
$bucket->fill($prices, 'plan.id', 'id'); |
81
|
|
|
$bucket->pour($rows, 'prices'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|