Test Failed
Push — master ( 249f62...0357c9 )
by Alexey
05:01
created

Price::name()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Item offer price
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ecommerce\Item\Offer;
13
14
use Users\User;
15
16
/**
17
 * Class Price
18
 *
19
 * @property int $id
20
 * @property int $item_offer_id
21
 * @property int $item_offer_price_type_id
22
 * @property string $name
23
 * @property number $price
24
 * @property int $currency_id
25
 * @property int $weight
26
 * @property string $date_create
27
 *
28
 * @property-read \Ecommerce\Item\Offer $offer
29
 * @property-read \Ecommerce\Item\Offer\Price\Type $type
30
 * @property-read \Money\Currency $currency
31
 *
32
 * @method \Ecommerce\Item\Offer\Price\Type type($options)
33
 * @method \Ecommerce\Item\Offer offer($options)
34
 * @method \Money\Currency currency($options)
35
 */
36
class Price extends \Model {
37
38
    public static $objectName = 'Цена';
39
    public static $cols = [
40
        //Основные параметры
41
        'item_offer_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'offer'],
42
        'item_offer_price_type_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'type'],
43
        'name' => ['type' => 'text'],
44
        'price' => ['type' => 'decimal'],
45
        'currency_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'currency'],
46
        //Системные
47
        'weight' => ['type' => 'text'],
48
        'date_create' => ['type' => 'dateTime'],
49
    ];
50
    public static $labels = [
51
        'price' => 'Цена',
52
        'item_offer_price_type_id' => 'Тип цены',
53
        'item_offer_id' => 'Товар',
54
        'currency_id' => 'Валюта',
55
    ];
56
    public static $dataManagers = [
57
        'manager' => [
58
            'name' => 'Цены',
59
            'cols' => [
60
                'item_offer_price_type_id',
61
                'price',
62
                'currency_id',
63
            ]
64
        ],
65
    ];
66
    public static $forms = [
67
        'manager' => [
68
            'map' => [
69
                ['price', 'currency_id'],
70
                ['item_offer_price_type_id']
71
            ]
72
        ]
73
    ];
74
75
    /**
76
     * @param bool|\Users\User $user
77
     * @return bool
78
     */
79
    public function checkUserAccess($user = false) {
80
        if (!$user) {
81
            $user = User::$cur;
82
        }
83
        return (!$this->type->roles) ||
84
            ($this->type->roles && strpos($this->type->roles, "|" . $user->role_id . "|") !== false);
85
    }
86
87
    public function name() {
88
        $name = $this->offer->name();
89
        if ($this->type) {
90
            $name .= ' - ' . $this->type->name();
91
        }
92
        $name .= ': ' . $this->price;
93
        if ($this->currency) {
94
            $name .= '  ' . $this->currency->name();
95
        }
96
        return $name;
97
    }
98
99
    public static function relations() {
100
        return [
101
            'offer' => [
102
                'model' => 'Ecommerce\Item\Offer',
103
                'col' => 'item_offer_id'
104
            ],
105
            'type' => [
106
                'model' => 'Ecommerce\Item\Offer\Price\Type',
107
                'col' => 'item_offer_price_type_id'
108
            ],
109
            'currency' => [
110
                'model' => 'Money\Currency',
111
                'col' => 'currency_id'
112
            ],
113
        ];
114
    }
115
116
}
117