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\sale; |
12
|
|
|
|
13
|
|
|
use hiqdev\billing\hiapi\models\relations\Bucket; |
14
|
|
|
use hiqdev\yii\DataMapper\expressions\CallExpression; |
15
|
|
|
use hiqdev\yii\DataMapper\expressions\HstoreExpression; |
16
|
|
|
use hiqdev\yii\DataMapper\components\EntityManagerInterface; |
17
|
|
|
use hiqdev\yii\DataMapper\query\Specification; |
18
|
|
|
use hiqdev\yii\DataMapper\repositories\BaseRepository; |
19
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
20
|
|
|
use hiqdev\php\billing\order\OrderInterface; |
21
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
22
|
|
|
use hiqdev\php\billing\sale\SaleInterface; |
23
|
|
|
use hiqdev\php\billing\sale\SaleFactoryInterface; |
24
|
|
|
use hiqdev\php\billing\sale\SaleRepositoryInterface; |
25
|
|
|
use hiqdev\php\billing\sale\Sale; |
26
|
|
|
use Yii; |
27
|
|
|
|
28
|
|
|
class SaleRepository extends BaseRepository implements SaleRepositoryInterface |
29
|
|
|
{ |
30
|
|
|
/** {@inheritdoc} */ |
31
|
|
|
public $queryClass = SaleQuery::class; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var SaleFactory |
35
|
|
|
*/ |
36
|
|
|
protected $factory; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
EntityManagerInterface $em, |
40
|
|
|
SaleFactoryInterface $factory, |
41
|
|
|
array $config = [] |
42
|
|
|
) { |
43
|
|
|
parent::__construct($config); |
44
|
|
|
|
45
|
|
|
$this->em = $em; |
46
|
|
|
$this->factory = $factory; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function findId(SaleInterface $sale) |
50
|
|
|
{ |
51
|
|
|
$hstore = new HstoreExpression(array_filter([ |
52
|
|
|
'buyer' => $sale->getCustomer()->getLogin(), |
53
|
|
|
'buyer_id' => $sale->getCustomer()->getId(), |
54
|
|
|
'object_id' => $sale->getTarget()->getId(), |
55
|
|
|
'tariff_id' => $sale->getPlan()->getId(), |
56
|
|
|
])); |
57
|
|
|
$call = new CallExpression('sale_id', [$hstore]); |
58
|
|
|
$command = $this->em->getConnection()->createSelect($call); |
59
|
|
|
|
60
|
|
|
return $command->scalar(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param OrderInterface $order |
65
|
|
|
* @return Sale[]|SaleInterface[] |
66
|
|
|
*/ |
67
|
|
|
public function findByOrder(OrderInterface $order) |
68
|
|
|
{ |
69
|
|
|
return array_map([$this, 'findByAction'], $order->getActions()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param ActionInterface $action |
74
|
|
|
* @return SaleInterface |
75
|
|
|
*/ |
76
|
|
|
public function findByAction(ActionInterface $action) |
77
|
|
|
{ |
78
|
|
|
$client_id = $action->getCustomer()->getId(); |
79
|
|
|
//$seller_id = $action->getCustomer()->getSeller()->getId(); |
|
|
|
|
80
|
|
|
$type = $action->getTarget()->getType(); |
81
|
|
|
|
82
|
|
|
if ($type === 'certificate') { |
83
|
|
|
//// XXX tmp crutch |
84
|
|
|
$class_id = $this->em->db->createCommand("SELECT class_id('certificate')")->queryScalar(); |
|
|
|
|
85
|
|
|
$cond = [ |
86
|
|
|
'target-id' => $class_id, |
87
|
|
|
'customer-id' => $client_id, |
88
|
|
|
]; |
89
|
|
|
} else if ($type === 'server') { |
90
|
|
|
$cond = [ |
91
|
|
|
'target-id' => $action->getTarget()->getId(), |
92
|
|
|
'customer-id' => $client_id, |
93
|
|
|
]; |
94
|
|
|
} else { |
95
|
|
|
throw new \Exception('not implemented for: ' . $type); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$spec = Yii::createObject(Specification::class) |
99
|
|
|
/// XXX how to pass with prices into joinPlans? |
100
|
|
|
->with('plans') |
101
|
|
|
->where($cond); |
102
|
|
|
|
103
|
|
|
return $this->findOne($spec); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function joinPlans(&$rows) |
107
|
|
|
{ |
108
|
|
|
$bucket = Bucket::fromRows($rows, 'plan-id'); |
109
|
|
|
$plans = $this->getRepository(PlanInterface::class)->findByIds($bucket->getKeys()); |
110
|
|
|
$bucket->fill($plans, 'plan.id', 'id'); |
111
|
|
|
$bucket->pour($rows, 'plans'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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..