Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HP-1596/Add_warranty_period_for_models_and_parts #165

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/grid/ModelColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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]);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getWarrantyLabel method calculates the difference between the current date and the warranty expiration date to determine the warranty status. It uses a color-coded label to indicate the warranty status, which enhances the user interface by providing visual cues. However, consider adding comments to explain the logic behind the color coding for future maintainability.

+ // Color coding logic: 'info' for normal status, 'warning' for warranties expiring within 6 months, and 'danger' for expired warranties.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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]);
}
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';
}
// Color coding logic: 'info' for normal status, 'warning' for warranties expiring within 6 months, and 'danger' for expired warranties.
return Label::widget(['label' => $diff, 'tag' => 'sup', 'color' => $color]);
}

}
14 changes: 5 additions & 9 deletions src/grid/PartGridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use hipanel\grid\CurrencyColumn;
use hipanel\grid\RefColumn;
use hipanel\modules\client\grid\ClientColumn;
use hipanel\modules\stock\grid\ModelColumn;
use hipanel\modules\stock\helpers\ProfitColumns;
use hipanel\modules\stock\models\Move;
use hipanel\modules\stock\models\Part;
Expand Down Expand Up @@ -94,17 +95,9 @@ public function columns()
],
],
'model' => [
'attribute' => 'model',
'class' => ModelColumn::class,
'format' => 'raw',
'label' => Yii::t('hipanel:stock', 'Model'),
'value' => function ($model) {
$modelLabel = Html::encode($model->model_label);
if (Yii::$app->user->can('model.read')) {
return Html::a($modelLabel, ['@model/view', 'id' => $model->model_id]);
}

return $modelLabel;
},
],
'model_type' => [
'class' => RefColumn::class,
Expand All @@ -115,6 +108,9 @@ public function columns()
'value' => function ($model) {
return $model->model_type_label;
},
],
'warranty_till' => [

],
'model_type_label' => [
'class' => RefColumn::class,
Expand Down
2 changes: 1 addition & 1 deletion src/grid/PartRepresentations.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function fillRepresentations()
'columns' => array_filter([
'checkbox',
'model_type', 'model_brand', 'model', 'partno', 'serial',
'last_move', 'move_type_and_date', 'device_location',
'last_move', 'move_type_and_date', 'device_location', 'warranty_till',
$user->can('move.read') ? 'move_descr' : null,
$user->can('order.read') ? 'order_name' : null,
$user->can('order.read') ? 'company_id' : null,
Expand Down
5 changes: 3 additions & 2 deletions src/models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public function rules()
],
'safe',
],
[['is_favorite', 'show_deleted'], 'boolean'],
[['id', 'type_id', 'tariff_id', 'group_id'], 'integer'],
[['is_favorite', 'show_deleted', 'warranty_in_all_part'], 'boolean'],
[['id', 'type_id', 'tariff_id', 'group_id', 'warranty_months'], 'integer'],

// Delete
[['id'], 'integer', 'on' => ['delete']],
Expand Down Expand Up @@ -143,6 +143,7 @@ public function attributeLabels()
'group' => Yii::t('hipanel:stock', 'Group'),
'group_id' => Yii::t('hipanel:stock', 'Group'),
'is_favorite' => Yii::t('hipanel:stock', 'Is favorite'),
'warranty_months' => Yii::t('hipanel:stock', 'Warranty months'),
// Chassis
'UNITS_QTY' => Yii::t('hipanel:stock', 'Units'),
'35_HDD_QTY' => Yii::t('hipanel:stock', '3.5" HDD'),
Expand Down
1 change: 1 addition & 0 deletions src/models/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function rules()
'order_name',
'company',
'device_location',
'warranty_till',
],
'safe',
],
Expand Down
13 changes: 12 additions & 1 deletion src/views/model/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function getAdditionl(elem) {
'short',
'descr',
'url',
'warranty_months',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warranty_in_all_part field is probably missing?

],
]) ?>
<div class="container-items"><!-- widgetContainer -->
Expand Down Expand Up @@ -105,8 +106,18 @@ class="glyphicon glyphicon-minus"></i></button>
(!$model->isNewRecord && $model->scenario != Model::SCENARIO_COPY) ? ['disabled' => 'disabled'] : ['prompt' => '--']) ?>
</div>
</div>
<?= $form->field($model, "[$i]group_id")->widget(ModelGroupCombo::class) ?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, "[$i]group_id")->widget(ModelGroupCombo::class) ?>
</div>
<div class="col-md-6">
<?= $form->field($model, "[$i]warranty_months") ?>
</div>
</div>
<?= $form->field($model, "[$i]short")->textarea() ?>
<?php if (!$model->isNewRecord) : ?>
<?= $form->field($model, "[$i]warranty_in_all_part")->checkbox() ?>
<?php endif; ?>
</div>
<div class="col-md-4">
<div class="row">
Expand Down
61 changes: 42 additions & 19 deletions src/views/part/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use hipanel\modules\stock\widgets\combo\SourceCombo;
use hipanel\widgets\AmountWithCurrency;
use hipanel\widgets\Box;
use hipanel\widgets\DateTimePicker;
use hipanel\widgets\DynamicFormCopyButton;
use hipanel\widgets\DynamicFormWidget;
use yii\bootstrap\ActiveForm;
Expand Down Expand Up @@ -48,13 +49,15 @@
'price',
'currency',
'company_id',
'warranty_till',
] : [
'model_id',
'dst_name',
'serial',
'company_id',
'price',
'currency',
'warranty_till',
],
]) ?>

Expand Down Expand Up @@ -94,6 +97,13 @@ class="glyphicon glyphicon-minus"></i></button>
'multiple' => true,
]) ?>
<?php endif; ?>
<?= $form->field($model, "[$i]warranty_till")->widget(DateTimePicker::class, [
'clientOptions' => [
'format' => 'yyyy-mm-dd',
'minView' => 2,
'todayHighlight' => true,
],
]) ?>
</div>
<div class="col-md-6">
<div class="row">
Expand All @@ -118,26 +128,39 @@ class="glyphicon glyphicon-minus"></i></button>
</div>
</div>
<?php else : ?>
<div class="col-md-3">
<?= $form->field($model, "[$i]model_id")->widget(ModelCombo::class) ?>
</div>
<div class="col-md-2">
<?= $form->field($model, "[$i]dst_name")->textInput(['disabled' => true])->label(Yii::t('hipanel:stock', 'Location')) ?>
</div>
<div class="col-md-2">
<?= $form->field($model, "[$i]serial") ?>
</div>
<div class="col-md-2">
<?= $form->field($model, "[$i]company_id")->widget(CompanyCombo::class) ?>
<div class="col-md-12">
<div class="col-md-6">
<?= $form->field($model, "[$i]model_id")->widget(ModelCombo::class) ?>
</div>
<div class="col-md-3">
<?= $form->field($model, "[$i]dst_name")->textInput(['disabled' => true])->label(Yii::t('hipanel:stock', 'Location')) ?>
</div>
<div class="col-md-3">
<?= $form->field($model, "[$i]serial") ?>
</div>
</div>
<div class="col-md-3 <?= AmountWithCurrency::$widgetClass ?>">
<?= $form->field($model, "[$i]price")->widget(AmountWithCurrency::class, [
'currencyAttributeName' => "[$i]currency",
'currencyAttributeOptions' => [
'items' => $currencyTypes,
],
]) ?>
<?= $form->field($model, "[$i]currency", ['template' => '{input}{error}'])->hiddenInput() ?>
<div class="col-md-12">
<div class="col-md-3">
<?= $form->field($model, "[$i]warranty_till")->widget(DateTimePicker::class, [
'clientOptions' => [
'format' => 'yyyy-mm-dd',
'minView' => 2,
'todayHighlight' => true,
],
]) ?>
</div>
<div class="col-md-3">
<?= $form->field($model, "[$i]company_id")->widget(CompanyCombo::class) ?>
</div>
<div class="col-md-6 <?= AmountWithCurrency::$widgetClass ?>">
<?= $form->field($model, "[$i]price")->widget(AmountWithCurrency::class, [
'currencyAttributeName' => "[$i]currency",
'currencyAttributeOptions' => [
'items' => $currencyTypes,
],
]) ?>
<?= $form->field($model, "[$i]currency", ['template' => '{input}{error}'])->hiddenInput() ?>
</div>
</div>
<div class="col-md-12">
<?= $form->field($model, "[$i]move_descr") ?>
Expand Down