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 filter by remaining warrenty months, fill the warranty i… #168

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 3 additions & 8 deletions src/grid/ModelColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@

class ModelColumn extends MainColumn
{
public function init()
public function init(): void
{
parent::init();

$this->attribute = 'model';
$this->prepareValue();
}

private function prepareValue(): void
{
$this->value = function (Part $model): string {
$modelLabel = Html::encode($model->model_label);
if (Yii::$app->user->can('model.read')) {
Expand All @@ -31,6 +24,7 @@ private function prepareValue(): void
if (isset($model->warranty_till)) {
$modelLabel .= $this->getWarrantyLabel($model);
}

return $modelLabel;
};
}
Expand All @@ -48,6 +42,7 @@ private function getWarrantyLabel(Part $model): string
if (!is_numeric($diff)) {
$color = 'danger';
}

return Label::widget(['label' => $diff, 'tag' => 'sup', 'color' => $color]);
}
}
10 changes: 6 additions & 4 deletions src/grid/PartGridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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 @@ -96,6 +95,7 @@ public function columns()
],
'model' => [
'class' => ModelColumn::class,
'attribute' => 'model',
'format' => 'raw',
'label' => Yii::t('hipanel:stock', 'Model'),
],
Expand All @@ -108,9 +108,6 @@ public function columns()
'value' => function ($model) {
return $model->model_type_label;
},
],
'warranty_till' => [

],
'model_type_label' => [
'class' => RefColumn::class,
Expand Down Expand Up @@ -343,6 +340,11 @@ public function columns()
return '';
}
],
'warranty_till' => [
'class' => WarrantyColumn::class,
'attribute' => 'warranty_till',
'format' => ['datetime', 'php:Y-m-d'],
],
]);
}

Expand Down
16 changes: 16 additions & 0 deletions src/grid/WarrantyColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace hipanel\modules\stock\grid;

use hipanel\grid\DataColumn;

class WarrantyColumn extends DataColumn
{
public function init(): void
{
parent::init();
$this->filter = '';
}
}
2 changes: 1 addition & 1 deletion src/models/PartSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class PartSearch extends Part
{
use SearchModelTrait {
searchAttributes as defaultSearchAttributes;
SearchModelTrait::searchAttributes as defaultSearchAttributes;
}

public function searchAttributes()
Expand Down
87 changes: 56 additions & 31 deletions src/views/part/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,27 @@
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\View;

/**
* @var Part $model
* @var array $currencyTypes
*/

$this->registerJs(/** @lang JavaScript */ <<<JS
(() => {
$(document).on("select2:select", "[id$='partno']", function (event) {
const datetimePlugin = $(event.target).parents(".item").find("[id$='warranty_till']").parent().data('datetimepicker');
const { warranty_months } = event.params.data;
if (warranty_months) {
datetimePlugin.setDate(moment().add(warranty_months, 'months').toDate());
}
});
})();
JS
,
View::POS_LOAD);

?>
<?php $form = ActiveForm::begin([
'id' => 'dynamic-form',
Expand Down Expand Up @@ -91,7 +106,9 @@ class="glyphicon glyphicon-minus"></i></button>
<?php if ($model->dst_id) : ?>
<?= $form->field($model, "[$i]dst_id")->widget(PartDestinationCombo::class, ['name' => 'dst_id']) ?>
<?php else : ?>
<?= $form->field($model, "[$i]dst_ids", ['options' => ['class' => 'required']])->widget(PartDestinationCombo::class, [
<?= $form->field($model,
"[$i]dst_ids",
['options' => ['class' => 'required']])->widget(PartDestinationCombo::class, [
'primaryFilter' => 'name_inilike',
'hasId' => true,
'multiple' => true,
Expand All @@ -108,7 +125,8 @@ class="glyphicon glyphicon-minus"></i></button>
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<?= $form->field($model, "[$i]serials")->hint(Yii::t('hipanel:stock', 'In order to use the automatic serials generation, the field should look like: <samp>[number of generated serials]_</samp>')) ?>
<?= $form->field($model, "[$i]serials")->hint(Yii::t('hipanel:stock',
'In order to use the automatic serials generation, the field should look like: <samp>[number of generated serials]_</samp>')) ?>
</div>
<div class="col-md-12">
<?= $form->field($model, "[$i]move_descr") ?>
Expand All @@ -120,7 +138,9 @@ class="glyphicon glyphicon-minus"></i></button>
'items' => $this->context->getCurrencyTypes(),
],
]) ?>
<?= $form->field($model, "[$i]currency", ['template' => '{input}{error}'])->hiddenInput(['data-amount-with-currency' => 'currency']) ?>
<?= $form->field($model,
"[$i]currency",
['template' => '{input}{error}'])->hiddenInput(['data-amount-with-currency' => 'currency']) ?>
</div>
<div class="col-md-6">
<?= $form->field($model, "[$i]company_id")->widget(CompanyCombo::class) ?>
Expand All @@ -129,37 +149,42 @@ class="glyphicon glyphicon-minus"></i></button>
</div>
<?php else : ?>
<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 class="row">
<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>
<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 class="row">
<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>
<div class="col-md-12">
Expand Down
5 changes: 5 additions & 0 deletions src/views/part/_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use hipanel\modules\stock\widgets\combo\PartCombo;
use hipanel\modules\stock\widgets\combo\PartnoCombo;
use hipanel\modules\stock\widgets\StockLocationsListTreeSelect;
use hipanel\modules\stock\widgets\WarrantyMonthsRangeInput;
use hipanel\widgets\AdvancedSearch;
use hiqdev\combo\StaticCombo;
use hipanel\widgets\RefCombo;
Expand Down Expand Up @@ -172,6 +173,10 @@
<div class="col-md-4 col-sm-6 col-xs-12">
<?= $search->field('order_name_ilike') ?>
</div>

<div class="col-md-4 col-sm-6 col-xs-12">
<?= $search->field('warranty_till')->widget(WarrantyMonthsRangeInput::class) ?>
</div>
<?php endif ?>

<?php if (Yii::$app->user->can('move.read')): ?>
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/StockLocationsListTreeSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function run()
:multiple="true"
value-consists-of="BRANCH_PRIORITY"
delimiter=","
auto-select-ancestors="true"
:auto-select-ancestors="true"
:clearable="false"
:allow-selecting-disabled-descendants="true"
search-nested
Expand Down
69 changes: 69 additions & 0 deletions src/widgets/WarrantyMonthsRangeInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace hipanel\modules\stock\widgets;

use Yii;
use yii\helpers\Html;
use yii\widgets\InputWidget;

class WarrantyMonthsRangeInput extends InputWidget
{
public ?string $hint = null;

public function run(): string
{
$widgetId = $this->getId();
$inputId = $this->options['id'];
if (str_contains((string)$this->model->{$this->attribute}, ',')) {
[$from, $till] = explode(',', $this->model->{$this->attribute});
} else {
$this->model->{$this->attribute} = null;
}
$hiddenInput = $this->hasModel()
? Html::activeHiddenInput($this->model, $this->attribute, $this->options)
: Html::hiddenInput($this->name, $this->value, $this->options);
$fromInput = $this->createInput('warranty_from', Yii::t('hipanel:stock', 'Warranty from'), $from ?? '');
$tillInput = $this->createInput('warranty_till', Yii::t('hipanel:stock', 'Warranty till'), $till ?? '');
$hint = Yii::t('hipanel:stock', "Enter a range of months in numeric format, where 0 is the current month, -1 the previous month and 1 the next month.");
$this->view->registerJs(/** @lang JavaScript */ <<<"JS"
;(() => {
const fromInput = \$("#$widgetId input[name$='from']");
const tillInput = \$("#$widgetId input[name$='till']");
\$("#$widgetId input[type!='hidden']").on("change", function (event) {
const from = fromInput.val();
const till = tillInput.val();
const value = [from, till];
\$("#$inputId").val(value.join(","));
});
})();

JS
);

return <<<HTML
<div id="$widgetId" style="display: flex;">
$fromInput
<span style="display: inline-block; width: 24px; line-height: 32px; text-align: center;">-</span>
$tillInput
</div>
<div style="color: rgba(0, 0, 0, 0.45)">$hint</div>
$hiddenInput
HTML;
}

private function createInput(string $attribute, string $label, string $value): string
{
return Html::input('number', $attribute, $value, [
'class' => 'form-control',
'placeholder' => $label,
'min' => -999,
'max' => 999,
'step' => 1,
'style' => [
'width' => 'calc(50% - 12px)',
],
]);
}
}
2 changes: 1 addition & 1 deletion src/widgets/combo/PartnoCombo.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PartnoCombo extends Combo
public $url = '/stock/model/index';

/** {@inheritdoc} */
public $_return = ['id'];
public $_return = ['id', 'warranty_months'];

/** {@inheritdoc} */
public $_rename = ['text' => 'partno'];
Expand Down