|
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\bill; |
|
12
|
|
|
|
|
13
|
|
|
use DateTime; |
|
14
|
|
|
use hiqdev\php\billing\bill\BillInterface; |
|
15
|
|
|
use hiqdev\php\billing\customer\Customer; |
|
16
|
|
|
use hiqdev\php\billing\target\Target; |
|
17
|
|
|
use hiqdev\php\billing\type\Type; |
|
18
|
|
|
use hiqdev\php\units\Quantity; |
|
19
|
|
|
use hiqdev\yii\DataMapper\expressions\CallExpression; |
|
20
|
|
|
use hiqdev\yii\DataMapper\expressions\HstoreExpression; |
|
21
|
|
|
use Money\Currency; |
|
22
|
|
|
use Money\Money; |
|
23
|
|
|
use yii\db\Query; |
|
24
|
|
|
|
|
25
|
|
|
class BillRepository extends \hiqdev\yii\DataMapper\repositories\BaseRepository |
|
26
|
|
|
{ |
|
27
|
|
|
public function create(array $row) |
|
28
|
|
|
{ |
|
29
|
|
|
$row['type'] = $this->createEntity(Type::class, $row['type']); |
|
|
|
|
|
|
30
|
|
|
$row['time'] = new DateTime($row['time']); |
|
31
|
|
|
$row['quantity'] = Quantity::create('megabyte', $row['quantity']['quantity']); |
|
32
|
|
|
$currency = new Currency(strtoupper($row['sum']['currency'])); |
|
33
|
|
|
$row['sum'] = new Money($row['sum']['amount'], $currency); |
|
34
|
|
|
$row['customer'] = $this->createEntity(Customer::class, $row['customer']); |
|
|
|
|
|
|
35
|
|
|
$row['target'] = $this->createEntity(Target::class, $row['target']); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
return parent::create($row); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* XXX TO BE REMOVED |
|
42
|
|
|
*/ |
|
43
|
|
|
public function saveReal(BillInterface $bill) |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->save($bill, true); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param BillInterface $bill |
|
50
|
|
|
* @param bool $isReal XXX TO BE REMOVED |
|
51
|
|
|
*/ |
|
52
|
|
|
public function save(BillInterface $bill, $isReal = false) |
|
53
|
|
|
{ |
|
54
|
|
|
$hstore = new HstoreExpression([ |
|
55
|
|
|
'id' => $bill->getId(), |
|
56
|
|
|
'object_id' => $bill->getTarget()->getId(), |
|
57
|
|
|
'tariff_id' => $bill->getPlan() ? $bill->getPlan()->getId() : null, |
|
58
|
|
|
'type_id' => $bill->getType()->getId(), |
|
59
|
|
|
'type' => $bill->getType()->getName(), |
|
60
|
|
|
'buyer_id' => $bill->getCustomer()->getId(), |
|
61
|
|
|
'buyer' => $bill->getCustomer()->getLogin(), |
|
62
|
|
|
'currency' => $bill->getSum()->getCurrency()->getCode(), |
|
63
|
|
|
'sum' => $bill->getSum()->getAmount() * -1, |
|
64
|
|
|
'quantity' => $bill->getQuantity()->getQuantity(), |
|
65
|
|
|
'time' => $bill->getTime()->format('c'), |
|
66
|
|
|
'label' => $bill->getComment() ?: null, |
|
|
|
|
|
|
67
|
|
|
'is_finished' => $bill->getIsFinished(), |
|
68
|
|
|
'increment' => true, |
|
69
|
|
|
]); |
|
70
|
|
|
$call = new CallExpression('set_bill' . ($isReal ? '' : '2'), [$hstore]); |
|
71
|
|
|
$command = (new Query())->select($call); |
|
72
|
|
|
$bill->setId($command->scalar($this->db)); |
|
|
|
|
|
|
73
|
|
|
foreach ($bill->getCharges() as $charge) { |
|
74
|
|
|
$charge->setBill($bill); |
|
75
|
|
|
$this->em->save($charge); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: