Skip to content

HP-2313: extend trash drop down with disposal locations #184

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

Merged
merged 1 commit into from
Jan 24, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
use yii\base\Application;
use yii\caching\CacheInterface;

readonly class DisposalRepository
readonly class LocationRepository
{
public function __construct(public CacheInterface $cache, public Application $app)
{
}

public function findForLocation(?string $deviceLocation): array
public function findForLocation(?string $deviceLocation, string $locationLike = 'disposal_'): array
{
$devices = $this->getDevices();
$devices = $this->getDevices($locationLike);
$deviceId2Locations = array_filter(ArrayHelper::map($devices, 'id', 'bindings.location.switch'));
$deviceLocationIsEmptyOrDevicesAreNotBindedWithLocation = $deviceLocation === null || $deviceId2Locations === [];
if ($deviceLocationIsEmptyOrDevicesAreNotBindedWithLocation) {
Expand All @@ -25,11 +25,11 @@ public function findForLocation(?string $deviceLocation): array
return $this->sortBySimilarity($deviceId2Locations, $deviceLocation);
}

private function getDevices(): array
private function getDevices(string $dcLike): array
{
return $this->cache->getOrSet(
['disposal_id', $this->app->user->identity->id],
static fn() => Server::find()->where(['dc_like' => 'disposal_'])->withBindings()->limit(-1)->all(),
['disposal_id', $dcLike, $this->app->user->identity->id],
static fn() => Server::find()->where(['dc_like' => $dcLike])->withBindings()->limit(-1)->all(),
3600
);
}
Expand Down
8 changes: 2 additions & 6 deletions src/views/part/trash.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use hipanel\modules\stock\widgets\combo\PartnoCombo;
use hipanel\modules\stock\widgets\combo\SourceCombo;
use hipanel\modules\stock\widgets\combo\TrashDestinationCombo;
use hipanel\modules\stock\widgets\combo\TrashDestinationDropDownList;
use hipanel\widgets\Box;
use hipanel\widgets\DynamicFormWidget;
use yii\bootstrap\ActiveForm;
Expand Down Expand Up @@ -61,11 +61,7 @@
'readonly' => true,
],
]) ?>
<?= $form->field($model, "[$i]dst_id")->widget(TrashDestinationCombo::class, [
'pluginOptions' => [
'select2Options' => ['tags' => false],
],
]) ?>
<?= $form->field($model, "[$i]dst_id")->widget(TrashDestinationDropDownList::class) ?>
</div>
<div class="col-md-6">
<?= $form->field($model, "[$i]serial")->textInput(['readonly' => true]) ?>
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/DisposalField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace hipanel\modules\stock\widgets;

use hipanel\modules\stock\models\Part;
use hipanel\modules\stock\repositories\DisposalRepository;
use hipanel\modules\stock\repositories\LocationRepository;
use Yii;
use yii\base\Widget;
use yii\helpers\Html;
Expand All @@ -17,7 +17,7 @@ class DisposalField extends Widget
public string $attribute;
private array $disposalList;

public function __construct(DisposalRepository $disposalRepository, $config = [])
public function __construct(LocationRepository $disposalRepository, $config = [])
{
parent::__construct($config);
$this->disposalList = $disposalRepository->findForLocation($this->model?->device_location);
Expand Down
15 changes: 0 additions & 15 deletions src/widgets/combo/TrashDestinationCombo.php

This file was deleted.

23 changes: 23 additions & 0 deletions src/widgets/combo/TrashDestinationDropDownList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace hipanel\modules\stock\widgets\combo;

use hipanel\modules\stock\repositories\LocationRepository;
use yii\helpers\Html;
use yii\widgets\InputWidget;

class TrashDestinationDropDownList extends InputWidget
{
private array $items;

public function __construct(LocationRepository $disposalRepository, $config = [])
{
parent::__construct($config);
$this->items = $disposalRepository->findForLocation(null, 'trash') + $disposalRepository->findForLocation(null);
}

public function run()
{
return Html::activeDropDownList($this->model, $this->attribute, $this->items, array_merge($this->options, ['prompt' => '']));
}
}