forked from hiqdev/hipanel-module-stock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelColumn.php
51 lines (45 loc) · 1.45 KB
/
ModelColumn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace hipanel\modules\stock\grid;
use DateTimeImmutable;
use hipanel\grid\MainColumn;
use hipanel\modules\stock\models\Model;
use hipanel\widgets\Label;
use yii\helpers\Html;
use Yii;
class ModelColumn extends MainColumn
{
public function init()
{
parent::init();
$this->attribute = 'model';
$this->prepareValue();
}
private function prepareValue(): void
{
$this->value = function ($model): string {
$modelLabel = Html::encode($model->model_label);
if (Yii::$app->user->can('model.read')) {
$modelLabel = Html::a($modelLabel, ['@model/view', 'id' => $model->model_id]);
}
if (isset($model->warranty_till)) {
$modelLabel .= $this->getWarrantyLabel($model);
}
return $modelLabel;
};
}
private function getWarrantyLabel($model): string
{
$diffTime = date_diff(new DateTimeImmutable(), new DateTimeImmutable($model->warranty_till));
$diff = (int)$diffTime->format('%y') * 12 + (int)$diffTime->format('%m');
$diff = ($diffTime->invert === 1) ? $diff * -1 : $diff;
$diff = ($diff < 0) ? 'X' : $diff;
$color = 'info';
if ($diff <= 6) {
$color = 'warning';
}
if (!is_numeric($diff)) {
$color = 'danger';
}
return Label::widget(['label' => $diff, 'tag' => 'sup', 'color' => $color]);
}
}