|
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 hiqdev\php\billing\bill\BillInterface; |
|
14
|
|
|
use hiqdev\yii\DataMapper\expressions\CallExpression; |
|
15
|
|
|
use hiqdev\yii\DataMapper\expressions\HstoreExpression; |
|
16
|
|
|
use yii\db\Query; |
|
17
|
|
|
|
|
18
|
|
|
class BillRepository extends \hiqdev\yii\DataMapper\repositories\BaseRepository |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param BillInterface $bill |
|
22
|
|
|
*/ |
|
23
|
|
|
public function save(BillInterface $bill) |
|
24
|
|
|
{ |
|
25
|
|
|
$hstore = new HstoreExpression([ |
|
26
|
|
|
'id' => $bill->getId(), |
|
27
|
|
|
'object_id' => $bill->getTarget()->getId(), |
|
28
|
|
|
'tariff_id' => $bill->getPlan() ? $bill->getPlan()->getId() : null, |
|
29
|
|
|
'type_id' => $bill->getType()->getId(), |
|
30
|
|
|
'type' => $bill->getType()->getName(), |
|
31
|
|
|
'buyer_id' => $bill->getCustomer()->getId(), |
|
32
|
|
|
'buyer' => $bill->getCustomer()->getLogin(), |
|
33
|
|
|
'currency' => $bill->getSum()->getCurrency()->getCode(), |
|
34
|
|
|
'sum' => $bill->getSum()->getAmount() * -1, |
|
35
|
|
|
'quantity' => $bill->getQuantity()->getQuantity(), |
|
36
|
|
|
'time' => $bill->getTime()->format('c'), |
|
37
|
|
|
'label' => $bill->getComment() ?: null, |
|
38
|
|
|
'is_finished' => $bill->isFinished(), |
|
39
|
|
|
'increment' => true, |
|
40
|
|
|
]); |
|
41
|
|
|
$this->db->transaction(function() use ($bill, $hstore) { |
|
42
|
|
|
$call = new CallExpression('set_bill', [$hstore]); |
|
43
|
|
|
$command = (new Query())->select($call); |
|
44
|
|
|
$bill->setId($command->scalar($this->db)); |
|
45
|
|
|
foreach ($bill->getCharges() as $charge) { |
|
46
|
|
|
$charge->setBill($bill); |
|
47
|
|
|
$this->em->save($charge); |
|
48
|
|
|
} |
|
49
|
|
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|