|
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\yii\DataMapper\components\EntityManagerInterface; |
|
17
|
|
|
use hiqdev\php\billing\bill\BillInterface; |
|
18
|
|
|
use hiqdev\php\billing\bill\BillFactoryInterface; |
|
19
|
|
|
use hiqdev\php\billing\customer\Customer; |
|
20
|
|
|
use hiqdev\php\billing\target\Target; |
|
21
|
|
|
use hiqdev\php\billing\type\Type; |
|
22
|
|
|
use hiqdev\php\units\Quantity; |
|
23
|
|
|
use Money\Currency; |
|
24
|
|
|
use Money\Money; |
|
25
|
|
|
|
|
26
|
|
|
class BillRepository extends \hiapi\repositories\BaseRepository |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var BillFactoryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $factory; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( |
|
34
|
|
|
EntityManagerInterface $em, |
|
35
|
|
|
BillFactoryInterface $factory, |
|
36
|
|
|
array $config = [] |
|
37
|
|
|
) { |
|
38
|
|
|
parent::__construct($config); |
|
39
|
|
|
|
|
40
|
|
|
$this->em = $em; |
|
|
|
|
|
|
41
|
|
|
$this->factory = $factory; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function create(array $row) |
|
45
|
|
|
{ |
|
46
|
|
|
$row['type'] = $this->createEntity(Type::class, $row['type']); |
|
47
|
|
|
$row['time'] = new DateTime($row['time']); |
|
48
|
|
|
$row['quantity'] = Quantity::create('megabyte', $row['quantity']['quantity']); |
|
49
|
|
|
$currency = new Currency(strtoupper($row['sum']['currency'])); |
|
50
|
|
|
$row['sum'] = new Money($row['sum']['amount'], $currency); |
|
51
|
|
|
$row['customer'] = $this->createEntity(Customer::class, $row['customer']); |
|
52
|
|
|
$row['target'] = $this->createEntity(Target::class, $row['target']); |
|
53
|
|
|
|
|
54
|
|
|
return parent::create($row); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function save(BillInterface $bill) |
|
58
|
|
|
{ |
|
59
|
|
|
$chargeIds = []; |
|
60
|
|
|
foreach ($bill->getCharges() as $charge) { |
|
61
|
|
|
$this->em->save($charge); |
|
|
|
|
|
|
62
|
|
|
$chargeIds[] = $charge->getId(); |
|
63
|
|
|
} |
|
64
|
|
|
$hstore = new HstoreExpression([ |
|
65
|
|
|
'id' => $bill->getId(), |
|
66
|
|
|
'object_id' => $bill->getTarget()->getId(), |
|
67
|
|
|
'tariff_id' => $bill->getPlan()->getId(), |
|
68
|
|
|
'type_id' => $bill->getType()->getId(), |
|
69
|
|
|
'type' => $bill->getType()->getName(), |
|
70
|
|
|
'buyer_id' => $bill->getCustomer()->getId(), |
|
71
|
|
|
'buyer' => $bill->getCustomer()->getLogin(), |
|
72
|
|
|
'currency' => $bill->getSum()->getCurrency()->getCode(), |
|
73
|
|
|
'sum' => $bill->getSum()->getAmount(), |
|
74
|
|
|
'quantity' => $bill->getQuantity()->getQuantity(), |
|
75
|
|
|
'is_finished' => $bill->getIsFinished(), |
|
76
|
|
|
'charge_ids' => implode(',', $chargeIds), |
|
77
|
|
|
]); |
|
78
|
|
|
$call = new CallExpression('set_bill', [$hstore]); |
|
79
|
|
|
$command = $this->em->getConnection()->createSelect($call); |
|
|
|
|
|
|
80
|
|
|
$bill->setId($command->scalar()); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.