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

#178 - Equipment management #363

Merged
merged 15 commits into from
Oct 18, 2023
103 changes: 103 additions & 0 deletions app/Domain/EquipmentExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace Toby\Domain;

use Generator;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromGenerator;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class EquipmentExport implements WithHeadings, WithStyles, WithEvents, WithStrictNullComparison, WithColumnFormatting, ShouldAutoSize, FromGenerator
{
use RegistersEventListeners;

public function __construct(
protected Collection $equipmentItems,
) {}

public function generator(): Generator
{
foreach ($this->equipmentItems as $equipmentItem) {
$row = [
$equipmentItem->id_number,
$equipmentItem->name,
$equipmentItem->labels?->implode(", "),
$equipmentItem->is_mobile,
$equipmentItem->assignee->profile->full_name ?? "",
$equipmentItem->assigned_at ? Date::dateTimeToExcel($equipmentItem->assigned_at) : "",
];

yield $row;
}
}

public function headings(): array
{
return [
__("ID"),
__("Name"),
__("Labels"),
__("Is mobile"),
__("Assignee"),
__("Assigned at"),
];
}

public function columnFormats(): array
{
return [
"A" => NumberFormat::FORMAT_TEXT,
"B" => NumberFormat::FORMAT_TEXT,
"C" => NumberFormat::FORMAT_TEXT,
"D" => DataType::TYPE_BOOL,
"E" => NumberFormat::FORMAT_TEXT,
"F" => NumberFormat::FORMAT_DATE_DDMMYYYY,
];
}

public function styles(Worksheet $sheet): void
{
$lastRow = $sheet->getHighestRow();
$lastColumn = $sheet->getHighestColumn();

$sheet->getStyle("A1:{$lastColumn}1")
->getFont()
->setBold(true);

$sheet->getStyle("A1:{$lastColumn}1")
->getAlignment()
->setVertical(Alignment::VERTICAL_CENTER);

$sheet->getStyle("A1:{$lastColumn}1")
->getFill()
->setFillType(Fill::FILL_SOLID)
->getStartColor()
->setRGB("D9D9D9");

$sheet->getStyle("A2:A{$lastRow}")
->getFont()
->setBold(true);

$sheet->getStyle("A1:{$lastColumn}{$lastRow}")
->getBorders()
->getAllBorders()
->setBorderStyle(Border::BORDER_THIN)
->getColor()
->setRGB("B7B7B7");
}
}
94 changes: 94 additions & 0 deletions app/Eloquent/Models/EquipmentItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Toby\Eloquent\Models;

use Carbon\Carbon;
use Database\Factories\EquipmentItemFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

/**
* @property int $id
* @property string $id_number
* @property string $name
* @property bool $is_mobile
* @property ?int $assignee_id
* @property ?User $assignee
* @property Carbon $assigned_at
* @property Collection $labels
*/
class EquipmentItem extends Model
{
use HasFactory;

protected $casts = [
"assigned_at" => "date",
"labels" => AsCollection::class,
];
protected $fillable = [
"id_number",
"name",
"is_mobile",
"labels",
"assignee_id",
"assigned_at",
];

public function scopeSearch(Builder $query, ?string $text): Builder
{
if ($text === null) {
return $query;
}

return $query
->where("id_number", "ILIKE", "%{$text}%")
->orWhere("name", "ILIKE", "%{$text}%")
->orWhere("labels", "ILIKE", "%{$text}%")
->orWhereRelation(
"assignee",
fn(Builder $query): Builder => $query
->where("email", "ILIKE", "%{$text}%")
->orWhereRelation(
"profile",
fn(Builder $query): Builder => $query
->where("first_name", "ILIKE", "%{$text}%")
->orWhere("last_name", "ILIKE", "%{$text}%")
->orWhere(DB::raw("CONCAT(first_name, ' ', last_name)"), "ILIKE", "%{$text}%"),
),
);
}

public function scopeLabels(Builder $query, ?array $labels): Builder
{
if ($labels === null) {
return $query;
}

$query->where(function (Builder $query) use ($labels): Builder {
foreach ($labels as $label) {
$query->orWhereJsonContains("labels", $label);
}

return $query;
});

return $query;
}

public function assignee(): BelongsTo
{
return $this->belongsTo(User::class, "assignee_id");
}

protected static function newFactory(): EquipmentItemFactory
{
return EquipmentItemFactory::new();
}
}
35 changes: 35 additions & 0 deletions app/Eloquent/Models/EquipmentLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Toby\Eloquent\Models;

use Database\Factories\EquipmentLabelFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Collection;

/**
* @property int $id
* @property string $name
* @property Collection $items
*/
class EquipmentLabel extends Model
{
use HasFactory;

protected $fillable = [
"name",
];

public function items(): BelongsToMany
{
return $this->belongsToMany(EquipmentItem::class, "equipment_items_labels");
}

protected static function newFactory(): EquipmentLabelFactory
{
return EquipmentLabelFactory::new();
}
}
4 changes: 3 additions & 1 deletion app/Eloquent/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Traits\HasRoles;
use Toby\Domain\Enums\EmploymentForm;
use Toby\Domain\Enums\Role;
Expand Down Expand Up @@ -102,7 +103,8 @@ public function scopeSearch(Builder $query, ?string $text): Builder
"profile",
fn(Builder $query): Builder => $query
->where("first_name", "ILIKE", "%{$text}%")
->orWhere("last_name", "ILIKE", "%{$text}%"),
->orWhere("last_name", "ILIKE", "%{$text}%")
->orWhere(DB::raw("CONCAT(first_name, ' ', last_name)"), "ILIKE", "%{$text}%"),
);
}

Expand Down
Loading
Loading