1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\grid; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\finance\models\DomainServicePrice; |
6
|
|
|
use hipanel\modules\finance\widgets\PriceDifferenceWidget; |
7
|
|
|
use hipanel\modules\finance\widgets\ResourcePriceWidget; |
8
|
|
|
use yii\helpers\Html; |
9
|
|
|
|
10
|
|
|
class DomainServicePriceGridView extends PriceGridView |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var DomainServicePrice[] |
14
|
|
|
*/ |
15
|
|
|
public $parentPrices; |
16
|
|
|
|
17
|
|
|
public function columns() |
18
|
|
|
{ |
19
|
|
|
return array_merge(parent::columns(), [ |
20
|
|
|
'purchase' => $this->getPriceGrid('feature,premium_dns_purchase'), |
21
|
|
|
'renewal' => $this->getPriceGrid('feature,premium_dns_renew'), |
22
|
|
|
]); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $type |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
private function getPriceGrid(string $type): array |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
|
|
'label' => DomainServicePrice::getOperations()[$type], |
33
|
|
|
'contentOptions' => ['class' => 'text-center'], |
34
|
|
|
'format' => 'raw', |
35
|
|
|
'value' => function($prices) use ($type) { |
36
|
|
|
/** @var DomainServicePrice[] $prices */ |
37
|
|
|
if (!isset($prices[$type])) { |
38
|
|
|
return ''; |
39
|
|
|
} |
40
|
|
|
$price = $prices[$type]; |
41
|
|
|
$parent = $this->parentPrices[$type] ?? null; |
42
|
|
|
$parentValue = $parent ? PriceDifferenceWidget::widget([ |
43
|
|
|
'new' => $price->price, |
44
|
|
|
'old' => $parent->price, |
45
|
|
|
]) : ''; |
46
|
|
|
$priceValue = floatval($price->price) || |
47
|
|
|
(!floatval($price->price) && $parentValue) ? |
48
|
|
|
ResourcePriceWidget::widget([ |
49
|
|
|
'price' => $price->price, |
50
|
|
|
'currency' => $price->currency |
51
|
|
|
]) : ''; |
52
|
|
|
$options = ['class' => 'col-md-6']; |
53
|
|
|
return Html::tag('div', $priceValue, $options) . |
54
|
|
|
Html::tag('div', $parentValue, $options); |
55
|
|
|
} |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.