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\repositories; |
12
|
|
|
|
13
|
|
|
use DateTime; |
14
|
|
|
use hiqdev\yii\DataMapper\expressions\CallExpression; |
15
|
|
|
use hiqdev\yii\DataMapper\expressions\HstoreExpression; |
16
|
|
|
use hiqdev\php\billing\bill\BillInterface; |
17
|
|
|
use hiqdev\php\billing\bill\BillFactoryInterface; |
18
|
|
|
use hiqdev\php\billing\customer\Customer; |
19
|
|
|
use hiqdev\php\billing\target\Target; |
20
|
|
|
use hiqdev\php\billing\type\Type; |
21
|
|
|
use hiqdev\php\units\Quantity; |
22
|
|
|
use Money\Currency; |
23
|
|
|
use Money\Money; |
24
|
|
|
use yii\db\Query; |
25
|
|
|
|
26
|
|
|
class BillRepository extends \hiqdev\yii\DataMapper\repositories\BaseRepository |
27
|
|
|
{ |
28
|
|
|
public function create(array $row) |
29
|
|
|
{ |
30
|
|
|
$row['type'] = $this->createEntity(Type::class, $row['type']); |
|
|
|
|
31
|
|
|
$row['time'] = new DateTime($row['time']); |
32
|
|
|
$row['quantity'] = Quantity::create('megabyte', $row['quantity']['quantity']); |
33
|
|
|
$currency = new Currency(strtoupper($row['sum']['currency'])); |
34
|
|
|
$row['sum'] = new Money($row['sum']['amount'], $currency); |
35
|
|
|
$row['customer'] = $this->createEntity(Customer::class, $row['customer']); |
|
|
|
|
36
|
|
|
$row['target'] = $this->createEntity(Target::class, $row['target']); |
|
|
|
|
37
|
|
|
|
38
|
|
|
return parent::create($row); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function save(BillInterface $bill) |
42
|
|
|
{ |
43
|
|
|
$hstore = new HstoreExpression([ |
44
|
|
|
'id' => $bill->getId(), |
45
|
|
|
'object_id' => $bill->getTarget()->getId(), |
46
|
|
|
'tariff_id' => $bill->getPlan()->getId(), |
47
|
|
|
'type_id' => $bill->getType()->getId(), |
48
|
|
|
'type' => $bill->getType()->getName(), |
49
|
|
|
'buyer_id' => $bill->getCustomer()->getId(), |
50
|
|
|
'buyer' => $bill->getCustomer()->getLogin(), |
51
|
|
|
'currency' => $bill->getSum()->getCurrency()->getCode(), |
52
|
|
|
'sum' => $bill->getSum()->getAmount() * -1, |
53
|
|
|
'quantity' => $bill->getQuantity()->getQuantity(), |
54
|
|
|
'time' => $bill->getTime()->format('c'), |
55
|
|
|
'is_finished' => $bill->getIsFinished(), |
56
|
|
|
'increment' => true, |
57
|
|
|
]); |
58
|
|
|
$call = new CallExpression('set_bill2', [$hstore]); |
59
|
|
|
$command = (new Query())->select($call); |
60
|
|
|
$bill->setId($command->scalar($this->db)); |
|
|
|
|
61
|
|
|
foreach ($bill->getCharges() as $charge) { |
62
|
|
|
$charge->setBill($bill); |
63
|
|
|
$this->em->save($charge); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: