Completed
Push — master ( 753652...d0e0e5 )
by Dmitry
04:54
created

src/models/Calculation.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\models;
12
13
use Yii;
14
use yii\base\InvalidParamException;
15
16
/**
17
 * Class Calculation.
18
 */
19
class Calculation extends \hipanel\base\Model
20
{
21
    use \hipanel\base\ModelTrait;
22
23
    /** {@inheritdoc} */
24
    public static function tableName()
25
    {
26
        return 'action';
27
    }
28
29
    /** {@inheritdoc} */
30
    public function init()
31
    {
32
        if (!isset($this->seller)) {
33
            if (Yii::$app->user->getIsGuest()) {
34
                $this->seller = Yii::$app->params['user.seller'];
35
            } else {
36
                $this->seller = Yii::$app->user->identity->seller;
37
            }
38
        }
39
40
        if (!isset($this->client) && !Yii::$app->user->getIsGuest()) {
41
            $this->client = Yii::$app->user->identity->username;
42
        }
43
44
        $this->synchronize();
45
    }
46
47
    public function getValue()
48
    {
49
        // ['tariff_id' => 'currency'] is a dumb relation condition and does not make any sense
50
        return $this->hasMany(Value::class, ['tariff_id' => 'currency'])->indexBy('currency');
51
    }
52
53
    /**
54
     * @param string $currency
55
     *
56
     * @throws InvalidParamException when the $currency is not calculated
57
     * @return Value
58
     */
59
    public function forCurrency($currency)
60
    {
61
        if (isset($this->value[$currency])) {
0 ignored issues
show
The property value does not exist on object<hipanel\modules\f...nce\models\Calculation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read 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.

Loading history...
62
            return $this->value[$currency];
0 ignored issues
show
The property value does not exist on object<hipanel\modules\f...nce\models\Calculation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read 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.

Loading history...
63
        }
64
        if (!empty($this->value)) {
0 ignored issues
show
The property value does not exist on object<hipanel\modules\f...nce\models\Calculation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read 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.

Loading history...
65
            return reset($this->value);
0 ignored issues
show
The property value does not exist on object<hipanel\modules\f...nce\models\Calculation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read 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.

Loading history...
66
        }
67
        Yii::warning('Value for currency $currency was not found. Using fake free value. Most probably, tariff is free', __METHOD__);
68
69
        return new Value(['value' => 0, 'price' => 0]);
70
71
    }
72
73
    /** {@inheritdoc} */
74
    public function rules()
75
    {
76
        return [
77
            [['object', 'seller', 'client', 'type', 'currency', 'item'], 'safe'],
78
            [['amount'], 'number'],
79
            [['tariff_id', 'calculation_id'], 'integer'],
80
        ];
81
    }
82
83
    /**
84
     * Synchronises the model to represent actual state of [[position]]
85
     * The method must update values, that affects the calculation and
86
     * can be changed in cart without position re-adding.
87
     * For example: quantity.
88
     */
89
    public function synchronize()
90
    {
91
        return true;
92
    }
93
}
94