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\plan; |
12
|
|
|
|
13
|
|
|
use hiqdev\yii\DataMapper\components\ConnectionInterface; |
14
|
|
|
use hiqdev\php\billing\price\PriceFactoryInterface; |
15
|
|
|
use hiqdev\php\billing\target\Target; |
16
|
|
|
use hiqdev\php\billing\type\Type; |
17
|
|
|
use hiqdev\php\units\Unit; |
18
|
|
|
use hiqdev\php\units\Quantity; |
19
|
|
|
use hiqdev\yii2\collection\Model; |
20
|
|
|
use Money\Currency; |
21
|
|
|
use Money\Money; |
22
|
|
|
use Money\Number; |
23
|
|
|
use yii\helpers\Json; |
24
|
|
|
|
25
|
|
|
class PriceRepository extends \hiqdev\yii\DataMapper\repositories\BaseRepository |
26
|
|
|
{ |
27
|
|
|
public $queryClass = PriceQuery::class; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var PriceFactoryInterface |
31
|
|
|
*/ |
32
|
|
|
protected $factory; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
ConnectionInterface $db, |
36
|
|
|
PriceFactoryInterface $factory, |
37
|
|
|
array $config = [] |
38
|
|
|
) { |
39
|
|
|
parent::__construct($config); |
40
|
|
|
|
41
|
|
|
$this->db = $db; |
42
|
|
|
$this->factory = $factory; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function create(array $row) |
46
|
|
|
{ |
47
|
|
|
$row['target'] = $this->createEntity(Target::class, $row['target']); |
48
|
|
|
$row['type'] = $this->createEntity(Type::class, $row['type']); |
49
|
|
|
$row['unit'] = Unit::create($row['prepaid']['unit']); |
50
|
|
|
$row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']); |
51
|
|
|
$row['currency'] = new Currency(strtoupper($row['price']['currency'])); |
52
|
|
|
$row['price'] = $this->negotiatePriceAndUnit($row['price']['amount'], $row['unit'], $row['currency']); |
53
|
|
|
$data = Json::decode($row['data']); |
54
|
|
|
$row['sums'] = empty($data['sums']) ? [] : $data['sums']; |
55
|
|
|
|
56
|
|
|
return parent::create($row); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $amount |
61
|
|
|
* @param Unit $unit |
62
|
|
|
* @param Currency $currency |
63
|
|
|
* @return Money |
64
|
|
|
*/ |
65
|
|
|
protected function negotiatePriceAndUnit($amount, Unit &$unit, Currency $currency) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
if (filter_var($amount, FILTER_VALIDATE_INT) === false) { |
68
|
|
|
$numberFromString = Number::fromString($amount); |
69
|
|
|
if (!$numberFromString->isInteger()) { |
70
|
|
|
// $smaller = $unit->getSmaller(); |
|
|
|
|
71
|
|
|
// $amount = $amount/$smaller->getFactor(); |
|
|
|
|
72
|
|
|
// TODO: !!! |
|
|
|
|
73
|
|
|
return $this->negotiatePriceAndUnit($amount, $smaller, $currency); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return new Money($amount, $currency); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.