1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiqdev\billing\hiapi\plan; |
4
|
|
|
|
5
|
|
|
use hiapi\components\ConnectionInterface; |
6
|
|
|
use hiapi\query\Specification; |
7
|
|
|
use hiapi\repositories\BaseRepository; |
8
|
|
|
use hiqdev\php\billing\customer\Customer; |
9
|
|
|
use hiqdev\php\billing\plan\PlanFactoryInterface; |
10
|
|
|
use hiqdev\php\billing\plan\PlanRepositoryInterface; |
11
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
12
|
|
|
use hiqdev\php\billing\order\OrderInterface; |
13
|
|
|
use Yii; |
14
|
|
|
use yii\db\Query; |
15
|
|
|
|
16
|
|
|
class PlanRepository extends BaseRepository implements PlanRepositoryInterface |
17
|
|
|
{ |
18
|
|
|
public $queryClass = PlanQuery::class; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var PlanFactory |
22
|
|
|
*/ |
23
|
|
|
protected $factory; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
ConnectionInterface $db, |
27
|
|
|
PlanFactoryInterface $factory, |
28
|
|
|
array $config = [] |
29
|
|
|
) { |
30
|
|
|
parent::__construct($config); |
31
|
|
|
|
32
|
|
|
$this->db = $db; |
33
|
|
|
$this->factory = $factory; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function create(array $row) |
37
|
|
|
{ |
38
|
|
|
$row['seller'] = $this->createEntity(Customer::class, $row['seller']); |
39
|
|
|
|
40
|
|
|
return parent::create($row); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function old___findAll(Specification $specification) |
44
|
|
|
{ |
45
|
|
|
$mutator = (new QueryMutator((new Query()) |
46
|
|
|
->select(['p.obj_id as id', 'p.name']) |
47
|
|
|
->from('tariff p') |
48
|
|
|
))->apply($specification); |
49
|
|
|
|
50
|
|
|
$rows = $mutator->getQuery()->createCommand($this->db)->queryAll(); |
51
|
|
|
$this->extendWithPrices($rows); |
52
|
|
|
|
53
|
|
|
$plans = $this->planHydrator->hydrateMultiple($this->planFactory->createPrototype(), $rows); |
|
|
|
|
54
|
|
|
|
55
|
|
|
return $plans; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function extendWithPrices(&$rows) |
59
|
|
|
{ |
60
|
|
|
$tariff_ids = array_column($rows, 'id'); |
61
|
|
|
|
62
|
|
|
$pricesRows = (new Query()) |
63
|
|
|
->select([ |
64
|
|
|
'tr.id', 'tr.tariff_id', |
65
|
|
|
'tr.object_id as target_id', 'ob.label as target_name', 'tt.obj_id as target_type_id', 'tt.name as target_type_name', |
66
|
|
|
'rt.obj_id as type_id', 'rt.name as type', |
67
|
|
|
'tr.quantity', 'tu.name as unit', |
68
|
|
|
'cu.name as currency', 'round(tr.price)' // todo: do not round |
69
|
|
|
]) |
70
|
|
|
->from('tariff_resource tr') |
71
|
|
|
->leftJoin('zref rt', 'rt.obj_id = tr.type_id') |
72
|
|
|
->leftJoin('obj ob', 'ob.obj_id = tr.object_id') |
73
|
|
|
->leftJoin('zref tt', 'tt.obj_id = ob.class_id') |
74
|
|
|
->leftJoin('zref tu', 'tu.obj_id = tr.unit_id') |
75
|
|
|
->leftJoin('zref cu', 'cu.obj_id = tr.currency_id') |
76
|
|
|
->where(['tr.tariff_id' => $tariff_ids]) |
77
|
|
|
->createCommand($this->db)->queryAll(); |
|
|
|
|
78
|
|
|
|
79
|
|
|
foreach ($rows as &$plan) { |
80
|
|
|
$plan['prices'] = $this->priceHydrator->hydrateMultiple(array_filter($pricesRows, function ($row) use ($plan) { |
|
|
|
|
81
|
|
|
return $row['tariff_id'] === $plan['id']; |
82
|
|
|
})); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function findByAction(ActionInterface $action) |
87
|
|
|
{ |
88
|
|
|
$client_id = $action->getCustomer()->getId(); |
89
|
|
|
$seller = $action->getCustomer()->getSeller()->getLogin(); |
90
|
|
|
$type = $action->getTarget()->getType(); |
91
|
|
|
|
92
|
|
|
$spec = Yii::createObject(Specification::class)->where([ |
93
|
|
|
'type-name' => $type, |
94
|
|
|
'available_for' => [ |
95
|
|
|
'client_id' => $client_id, |
96
|
|
|
'seller' => $seller, |
97
|
|
|
], |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
return $this->findOne($spec); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function findByOrder(OrderInterface $order) |
104
|
|
|
{ |
105
|
|
|
return array_map([$this, 'findByAction'], $order->getActions()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..