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\charge; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\charge\Charge; |
14
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
15
|
|
|
use hiqdev\yii\DataMapper\components\ConnectionInterface; |
16
|
|
|
use hiqdev\yii\DataMapper\components\EntityManagerInterface; |
17
|
|
|
use hiqdev\yii\DataMapper\expressions\CallExpression; |
18
|
|
|
use hiqdev\yii\DataMapper\expressions\HstoreExpression; |
19
|
|
|
use hiqdev\yii\DataMapper\models\relations\Bucket; |
20
|
|
|
use hiqdev\yii\DataMapper\query\Specification; |
21
|
|
|
use hiqdev\yii\DataMapper\repositories\BaseRepository; |
22
|
|
|
use League\Event\EmitterInterface; |
23
|
|
|
use yii\db\Query; |
24
|
|
|
|
25
|
|
|
class ChargeRepository extends BaseRepository |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var EmitterInterface |
29
|
|
|
*/ |
30
|
|
|
private $emitter; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
ConnectionInterface $db, |
34
|
|
|
EntityManagerInterface $em, |
35
|
|
|
EmitterInterface $emitter, |
36
|
|
|
array $config = [] |
37
|
|
|
) { |
38
|
|
|
parent::__construct($db, $em, $config); |
39
|
|
|
|
40
|
|
|
$this->emitter = $emitter; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** {@inheritdoc} */ |
44
|
|
|
public $queryClass = ChargeQuery::class; |
45
|
|
|
|
46
|
|
|
public function save(Charge $charge) |
47
|
|
|
{ |
48
|
|
|
$action = $charge->getAction(); |
49
|
|
|
$tariff_id = null; |
50
|
|
|
if ($action->hasSale($action)) { |
51
|
|
|
$tariff_id = $action->getSale()->getPlan()->getId(); |
52
|
|
|
$this->em->save($action); |
53
|
|
|
} |
54
|
|
|
$hstore = new HstoreExpression(array_filter([ |
55
|
|
|
'id' => $charge->getId(), |
56
|
|
|
'object_id' => $charge->getTarget()->getId(), |
57
|
|
|
'tariff_id' => $tariff_id, |
58
|
|
|
'action_id' => $action->getId(), |
59
|
|
|
'buyer_id' => $action->getCustomer()->getId(), |
60
|
|
|
'buyer' => $action->getCustomer()->getLogin(), |
61
|
|
|
'type_id' => $charge->getType()->getId(), |
62
|
|
|
'type' => $charge->getType()->getName(), |
63
|
|
|
'currency' => $charge->getSum()->getCurrency()->getCode(), |
64
|
|
|
'sum' => $charge->getSum()->getAmount(), |
65
|
|
|
'unit' => $charge->getUsage()->getUnit()->getName(), |
66
|
|
|
'quantity' => $charge->getUsage()->getQuantity(), |
67
|
|
|
'bill_id' => $charge->getBill()->getId(), |
68
|
|
|
'parent_id' => $charge->getParent() !== null ? $charge->getParent()->getId() : null, |
69
|
|
|
'time' => $charge->getAction()->getTime()->format('c'), |
70
|
|
|
'is_finished' => $charge->isFinished(), |
71
|
|
|
'label' => $charge->getComment(), |
72
|
|
|
])); |
73
|
|
|
$call = new CallExpression('set_charge', [$hstore]); |
74
|
|
|
$command = (new Query())->select($call); |
75
|
|
|
$charge->setId($command->scalar($this->db)); |
76
|
|
|
$events = $charge->releaseEvents(); |
77
|
|
|
if (!empty($events)) { |
78
|
|
|
$this->emitter->emitBatch($events); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function joinParent(&$rows) |
83
|
|
|
{ |
84
|
|
|
$bucket = Bucket::fromRows($rows, 'parent-id'); |
85
|
|
|
$spec = (new Specification())->where(['id' => $bucket->getKeys()]); |
86
|
|
|
$charges = $this->getRepository(ChargeInterface::class)->queryAll($spec); |
87
|
|
|
$bucket->fill($charges, 'id'); |
88
|
|
|
$bucket->pourOneToOne($rows, 'parent'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|