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\sale; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
14
|
|
|
use hiqdev\php\billing\order\OrderInterface; |
15
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
16
|
|
|
use hiqdev\php\billing\sale\Sale; |
17
|
|
|
use hiqdev\php\billing\sale\SaleInterface; |
18
|
|
|
use hiqdev\php\billing\sale\SaleRepositoryInterface; |
19
|
|
|
use hiqdev\yii\DataMapper\expressions\CallExpression; |
20
|
|
|
use hiqdev\yii\DataMapper\expressions\HstoreExpression; |
21
|
|
|
use hiqdev\yii\DataMapper\models\relations\Bucket; |
22
|
|
|
use hiqdev\yii\DataMapper\query\Specification; |
23
|
|
|
use hiqdev\yii\DataMapper\repositories\BaseRepository; |
24
|
|
|
use Yii; |
25
|
|
|
|
26
|
|
|
class SaleRepository extends BaseRepository implements SaleRepositoryInterface |
27
|
|
|
{ |
28
|
|
|
/** {@inheritdoc} */ |
29
|
|
|
public $queryClass = SaleQuery::class; |
30
|
|
|
|
31
|
|
|
public function findId(SaleInterface $sale) |
32
|
|
|
{ |
33
|
|
|
if ($sale->hasId()) { |
34
|
|
|
return $sale->getId(); |
35
|
|
|
} |
36
|
|
|
$hstore = new HstoreExpression(array_filter([ |
37
|
|
|
'buyer' => $sale->getCustomer()->getLogin(), |
38
|
|
|
'buyer_id' => $sale->getCustomer()->getId(), |
39
|
|
|
'object_id' => $sale->getTarget()->getId(), |
40
|
|
|
'tariff_id' => $sale->getPlan()->getId(), |
41
|
|
|
])); |
42
|
|
|
$call = new CallExpression('sale_id', [$hstore]); |
43
|
|
|
$command = $this->em->getConnection()->createSelect($call); |
|
|
|
|
44
|
|
|
|
45
|
|
|
return $command->scalar(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param OrderInterface $order |
50
|
|
|
* @return Sale[]|SaleInterface[] |
51
|
|
|
*/ |
52
|
|
|
public function findByOrder(OrderInterface $order) |
53
|
|
|
{ |
54
|
|
|
return array_map([$this, 'findByAction'], $order->getActions()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ActionInterface $action |
59
|
|
|
* @return SaleInterface |
60
|
|
|
*/ |
61
|
|
|
public function findByAction(ActionInterface $action) |
62
|
|
|
{ |
63
|
|
|
$client_id = $action->getCustomer()->getId(); |
64
|
|
|
$seller_id = $action->getCustomer()->getSeller()->getId(); |
65
|
|
|
$type = $action->getTarget()->getType(); |
66
|
|
|
|
67
|
|
|
if ($type === 'certificate') { |
68
|
|
|
//// XXX tmp crutch |
69
|
|
|
$class_id = new CallExpression('class_id', ['certificate']); |
70
|
|
|
$cond = [ |
71
|
|
|
'target-id' => $class_id, |
72
|
|
|
'customer-id' => $client_id, |
73
|
|
|
]; |
74
|
|
|
if (empty($client_id)) { |
75
|
|
|
$cond['customer-id'] = $seller_id; |
76
|
|
|
$cond['seller-id'] = $seller_id; |
77
|
|
|
} |
78
|
|
|
} elseif ($type === 'server') { |
79
|
|
|
$cond = [ |
80
|
|
|
'target-id' => $action->getTarget()->getId(), |
81
|
|
|
'customer-id' => $client_id, |
82
|
|
|
]; |
83
|
|
|
} else { |
84
|
|
|
throw new \Exception('not implemented for: ' . $type); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$spec = Yii::createObject(Specification::class) |
88
|
|
|
/// XXX how to pass if we want with prices into joinPlans? |
89
|
|
|
->with('plans') |
90
|
|
|
->where($cond); |
91
|
|
|
|
92
|
|
|
return $this->findOne($spec); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function joinPlans(&$rows) |
96
|
|
|
{ |
97
|
|
|
$bucket = Bucket::fromRows($rows, 'plan-id'); |
98
|
|
|
$spec = (new Specification()) |
99
|
|
|
->with('prices') |
100
|
|
|
->where(['id' => $bucket->getKeys()]); |
101
|
|
|
$raw_plans = $this->getRepository(PlanInterface::class)->queryAll($spec); |
102
|
|
|
/// TODO for SilverFire: try to do with bucket |
103
|
|
|
$plans = []; |
104
|
|
|
foreach ($raw_plans as $plan) { |
105
|
|
|
$plans[$plan['id']] = $plan; |
106
|
|
|
} |
107
|
|
|
foreach ($rows as &$sale) { |
108
|
|
|
$sale['plan'] = $plans[$sale['plan-id']]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.