1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Finance module for HiPanel |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
7
|
|
|
* @package hipanel-module-finance |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\modules\finance\models; |
13
|
|
|
|
14
|
|
|
use Guzzle\Plugin\ErrorResponse\Exception\ErrorResponseException; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\base\InvalidParamException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Calculation. |
20
|
|
|
*/ |
21
|
|
|
class Calculation extends \hipanel\base\Model |
22
|
|
|
{ |
23
|
|
|
use \hipanel\base\ModelTrait; |
24
|
|
|
|
25
|
|
|
/** {@inheritdoc} */ |
26
|
|
|
public static function index() |
27
|
|
|
{ |
28
|
|
|
return 'actions'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** {@inheritdoc} */ |
32
|
|
|
public static function type() |
33
|
|
|
{ |
34
|
|
|
return 'action'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** {@inheritdoc} */ |
38
|
|
|
public static function primaryKey() |
39
|
|
|
{ |
40
|
|
|
return ['tariff_id']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** {@inheritdoc} */ |
44
|
|
|
public function init() |
45
|
|
|
{ |
46
|
|
|
if (Yii::$app->user->isGuest) { |
47
|
|
|
$this->seller = Yii::$app->params['seller']; |
|
|
|
|
48
|
|
|
} else { |
49
|
|
|
$this->client = Yii::$app->user->identity->username; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->synchronize(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getValue() |
56
|
|
|
{ |
57
|
|
|
return $this->hasMany(Value::class, ['tariff_id' => 'currency'])->indexBy('currency'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function forCurrency($currency) |
61
|
|
|
{ |
62
|
|
|
if (!isset($this->value[$currency])) { |
|
|
|
|
63
|
|
|
throw new InvalidParamException("Calculation for currency \"$currency\" does not exist"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->value[$currency]; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** {@inheritdoc} */ |
70
|
|
View Code Duplication |
public function rules() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
[['tariff_id', 'object', 'seller', 'client', 'type', 'currency', 'item'], 'safe'], |
74
|
|
|
[['amount'], 'number'], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Synchronises the model to represent actual state of [[position]] |
80
|
|
|
* The method must update values, that affects the calculation and |
81
|
|
|
* can be changed in cart without position re-adding. |
82
|
|
|
* For example: quantity. |
83
|
|
|
*/ |
84
|
|
|
public function synchronize() |
85
|
|
|
{ |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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@property
annotation 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.