This commit is contained in:
Simon Pocrnjič 2025-10-20 19:39:26 +02:00
parent 90bbf1942c
commit 872b76b012
9 changed files with 268 additions and 43 deletions

View File

@ -118,6 +118,7 @@ public function contracts(Client $client, Request $request)
$from = $request->input('from');
$to = $request->input('to');
$search = $request->input('search');
$segmentId = $request->input('segment');
$contractsQuery = \App\Models\Contract::query()
->whereHas('clientCase', function ($q) use ($client) {
@ -149,8 +150,16 @@ public function contracts(Client $client, Request $request)
});
});
})
->when($segmentId, function ($q) use ($segmentId) {
$q->whereHas('segments', function ($s) use ($segmentId) {
$s->where('segments.id', $segmentId)
->where('contract_segment.active', true);
});
})
->orderByDesc('start_date');
$segments = \App\Models\Segment::orderBy('name')->get(['id', 'name']);
$types = [
'address_types' => \App\Models\Person\AddressType::all(),
'phone_types' => \App\Models\Person\PhoneType::all(),
@ -159,7 +168,8 @@ public function contracts(Client $client, Request $request)
return Inertia::render('Client/Contracts', [
'client' => $data,
'contracts' => $contractsQuery->paginate($request->integer('perPage', 20))->withQueryString(),
'filters' => $request->only(['from', 'to', 'search']),
'filters' => $request->only(['from', 'to', 'search', 'segment']),
'segments' => $segments,
'types' => $types,
]);
}

View File

@ -415,6 +415,15 @@ public function missingContracts(Import $import)
->where('client_cases.client_id', $import->client_id)
->where('contracts.active', 1)
->whereNull('contracts.deleted_at')
// Exclude contracts that have any ACTIVE segment marked as excluded
->whereNotExists(function ($sq) {
$sq->select(\DB::raw(1))
->from('contract_segment')
->join('segments', 'segments.id', '=', 'contract_segment.segment_id')
->whereColumn('contract_segment.contract_id', 'contracts.id')
->where('contract_segment.active', true)
->where('segments.exclude', true);
})
->when(count($present) > 0, function ($q) use ($present) {
$q->whereNotIn('contracts.reference', $present);
})

View File

@ -120,6 +120,7 @@ public function update(UpdateSegmentRequest $request, Segment $segment)
'name' => $data['name'],
'description' => $data['description'] ?? null,
'active' => $data['active'] ?? $segment->active,
'exclude' => $data['exclude'] ?? $segment->exclude
]);
return to_route('settings.segments')->with('success', 'Segment updated');

View File

@ -17,6 +17,7 @@ public function rules(): array
'name' => ['required', 'string', 'max:50'],
'description' => ['nullable', 'string', 'max:255'],
'active' => ['boolean'],
'exclude' => ['boolean']
];
}

View File

@ -15,12 +15,14 @@ class Segment extends Model
'name',
'description',
'active',
'exclude'
];
protected function casts(): array
{
return [
'active' => 'boolean',
'exclude' => 'boolean'
];
}

View File

@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('segments', function (Blueprint $table) {
if (! Schema::hasColumn('segments', 'exclude')) {
$table->boolean('exclude')->default(false)->after('active');
}
});
}
public function down(): void
{
Schema::table('segments', function (Blueprint $table) {
if (Schema::hasColumn('segments', 'exclude')) {
$table->dropColumn('exclude');
}
});
}
};

View File

@ -1,43 +1,128 @@
<script setup>
import DialogModal from '@/Components/DialogModal.vue'
import DialogModal from "@/Components/DialogModal.vue";
import { router } from "@inertiajs/vue3";
import { ref, computed } from "vue";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { library } from "@fortawesome/fontawesome-svg-core";
import { faTrash, faXmark, faCheck } from "@fortawesome/free-solid-svg-icons";
library.add(faTrash, faXmark, faCheck);
const props = defineProps({
show: { type: Boolean, default: false },
client_case: { type: Object, required: true },
contract: { type: Object, default: null },
})
});
const emit = defineEmits(['close'])
const close = () => emit('close')
const emit = defineEmits(["close"]);
const close = () => emit("close");
const items = () => Array.isArray(props.contract?.objects) ? props.contract.objects : []
// Local optimistic filtering of removed items so the list updates immediately
const removedIds = ref([]);
const confirmingId = ref(null);
const items = computed(() => {
const arr = Array.isArray(props.contract?.objects) ? props.contract.objects : [];
if (!removedIds.value.length) {
return arr;
}
return arr.filter((o) => !removedIds.value.includes(o.id));
});
// Delete handling (no confirmation)
const deleteObject = (o) => {
if (!o?.id) {
return;
}
const id = o.id;
router.delete(
route("clientCase.object.delete", {
client_case: props.client_case.uuid,
id,
}),
{
preserveScroll: true,
onSuccess: () => {
if (!removedIds.value.includes(id)) {
removedIds.value.push(id);
}
if (confirmingId.value === id) {
confirmingId.value = null;
}
},
}
);
};
</script>
<template>
<DialogModal :show="show" @close="close">
<template #title>
Predmeti
<span v-if="contract" class="ml-2 text-sm text-gray-500">(Pogodba: {{ contract.reference }})</span>
<span v-if="contract" class="ml-2 text-sm text-gray-500"
>(Pogodba: {{ contract.reference }})</span
>
</template>
<template #content>
<div class="mt-1 max-h-[60vh] overflow-y-auto">
<div v-if="items().length > 0" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
<div v-for="o in items()" :key="o.id" class="rounded-lg border border-gray-200 bg-white shadow-sm">
<div
v-if="items.length > 0"
class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4"
>
<div
v-for="o in items"
:key="o.id"
class="rounded-lg border border-gray-200 bg-white shadow-sm"
>
<div class="p-4">
<div class="flex items-start justify-between">
<div>
<div class="text-xs uppercase text-gray-500">Ref.</div>
<div class="font-semibold text-gray-900">{{ o.reference || '-' }}</div>
<div class="font-semibold text-gray-900">{{ o.reference || "-" }}</div>
</div>
<div class="ml-3 flex items-center gap-2">
<span
class="inline-flex items-center rounded-full bg-gray-100 px-2 py-0.5 text-xs text-gray-700"
>{{ o.type || "—" }}</span
>
<template v-if="confirmingId === o.id">
<button
type="button"
class="inline-flex h-8 w-8 items-center justify-center rounded-full hover:bg-gray-100 text-gray-700"
:title="'Prekliči'"
@click.stop="confirmingId = null"
>
<FontAwesomeIcon :icon="['fas', 'xmark']" class="text-[16px]" />
</button>
<button
type="button"
class="inline-flex h-8 w-8 items-center justify-center rounded-full hover:bg-green-50 text-green-700"
:title="'Potrdi brisanje'"
@click.stop="deleteObject(o)"
>
<FontAwesomeIcon :icon="['fas', 'check']" class="text-[16px]" />
</button>
</template>
<template v-else>
<button
type="button"
class="inline-flex h-8 w-8 items-center justify-center rounded-full hover:bg-red-50"
:title="'Izbriši'"
@click.stop="confirmingId = o.id"
>
<FontAwesomeIcon :icon="['fas', 'trash']" class="text-red-600" />
</button>
</template>
</div>
<span class="ml-3 inline-flex items-center rounded-full bg-gray-100 px-2 py-0.5 text-xs text-gray-700">{{ o.type || '—' }}</span>
</div>
<div class="mt-3">
<div class="text-xs uppercase text-gray-500">Naziv</div>
<div class="text-gray-900">{{ o.name || '-' }}</div>
<div class="text-gray-900">{{ o.name || "-" }}</div>
</div>
<div class="mt-3">
<div class="text-xs uppercase text-gray-500">Opis</div>
<div class="text-gray-700 whitespace-pre-wrap">{{ o.description || '' }}</div>
<div class="text-gray-700 whitespace-pre-wrap">
{{ o.description || "" }}
</div>
</div>
</div>
</div>
@ -45,7 +130,13 @@ const items = () => Array.isArray(props.contract?.objects) ? props.contract.obje
<div v-else class="text-center text-gray-500 py-3">Ni predmetov.</div>
</div>
<div class="mt-4 flex justify-end">
<button type="button" class="px-4 py-2 rounded-md border border-gray-300 text-gray-700 hover:bg-gray-50" @click="close">Zapri</button>
<button
type="button"
class="px-4 py-2 rounded-md border border-gray-300 text-gray-700 hover:bg-gray-50"
@click="close"
>
Zapri
</button>
</div>
</template>
</DialogModal>

View File

@ -10,12 +10,14 @@ const props = defineProps({
client: Object,
contracts: Object,
filters: Object,
segments: Object,
types: Object,
});
const fromDate = ref(props.filters?.from || "");
const toDate = ref(props.filters?.to || "");
const search = ref(props.filters?.search || "");
const selectedSegment = ref(props.filters?.segment || "");
function applyDateFilter() {
const params = Object.fromEntries(
new URLSearchParams(window.location.search).entries()
@ -35,6 +37,11 @@ function applyDateFilter() {
} else {
delete params.search;
}
if (selectedSegment.value) {
params.segment = selectedSegment.value;
} else {
delete params.segment;
}
delete params.page;
router.get(route("client.contracts", { uuid: props.client.uuid }), params, {
preserveState: true,
@ -46,6 +53,7 @@ function applyDateFilter() {
function clearDateFilter() {
fromDate.value = "";
toDate.value = "";
selectedSegment.value = "";
applyDateFilter();
}
@ -150,7 +158,7 @@ function formatDate(value) {
class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between"
>
<div class="flex items-center gap-3 flex-wrap">
<label class="font-medium mr-2">Filter po datumu:</label>
<label class="font-medium mr-2">Filtri:</label>
<div class="flex items-center gap-2">
<span class="text-sm text-gray-600">Od</span>
<input
@ -169,12 +177,25 @@ function formatDate(value) {
class="rounded border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 px-3 py-2 text-sm"
/>
</div>
<div class="flex items-center gap-2">
<span class="text-sm text-gray-600">Segment</span>
<select
v-model="selectedSegment"
@change="applyDateFilter"
class="rounded border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 px-3 py-2 text-sm"
>
<option value="">Vsi segmenti</option>
<option v-for="segment in segments" :key="segment.id" :value="segment.id">
{{ segment.name }}
</option>
</select>
</div>
<button
type="button"
class="inline-flex items-center px-3 py-2 text-sm font-medium rounded border border-gray-300 text-gray-700 hover:bg-gray-50 disabled:opacity-50"
:disabled="!fromDate && !toDate"
:disabled="!fromDate && !toDate && !selectedSegment"
@click="clearDateFilter"
title="Počisti datum"
title="Počisti filtre"
>
Počisti
</button>
@ -194,7 +215,7 @@ function formatDate(value) {
:meta="{ current_page: contracts.current_page, per_page: contracts.per_page, total: contracts.total, last_page: contracts.last_page }"
route-name="client.contracts"
:route-params="{ uuid: client.uuid }"
:query="{ from: fromDate || undefined, to: toDate || undefined }"
:query="{ from: fromDate || undefined, to: toDate || undefined, segment: selectedSegment || undefined }"
:search="search"
row-key="uuid"
:only-props="['contracts']"

View File

@ -1,12 +1,12 @@
<script setup>
import AppLayout from '@/Layouts/AppLayout.vue';
import { useForm, router } from '@inertiajs/vue3';
import { ref } from 'vue';
import DialogModal from '@/Components/DialogModal.vue';
import PrimaryButton from '@/Components/PrimaryButton.vue';
import InputLabel from '@/Components/InputLabel.vue';
import InputError from '@/Components/InputError.vue';
import TextInput from '@/Components/TextInput.vue';
import AppLayout from "@/Layouts/AppLayout.vue";
import { useForm, router } from "@inertiajs/vue3";
import { ref } from "vue";
import DialogModal from "@/Components/DialogModal.vue";
import PrimaryButton from "@/Components/PrimaryButton.vue";
import InputLabel from "@/Components/InputLabel.vue";
import InputError from "@/Components/InputError.vue";
import TextInput from "@/Components/TextInput.vue";
const props = defineProps({
segments: Array,
@ -17,16 +17,18 @@ const showEdit = ref(false);
const editing = ref(null);
const createForm = useForm({
name: '',
description: '',
name: "",
description: "",
active: true,
exclude: false,
});
const editForm = useForm({
id: null,
name: '',
description: '',
name: "",
description: "",
active: true,
exclude: false,
});
const openCreate = () => {
@ -41,7 +43,7 @@ const closeCreate = () => {
};
const store = () => {
createForm.post(route('settings.segments.store'), {
createForm.post(route("settings.segments.store"), {
preserveScroll: true,
onSuccess: () => closeCreate(),
});
@ -51,8 +53,9 @@ const openEdit = (segment) => {
editing.value = segment;
editForm.id = segment.id;
editForm.name = segment.name;
editForm.description = segment.description ?? '';
editForm.description = segment.description ?? "";
editForm.active = !!segment.active;
editForm.exclude = !!segment.exclude;
showEdit.value = true;
};
@ -63,7 +66,7 @@ const closeEdit = () => {
};
const update = () => {
editForm.put(route('settings.segments.update', { segment: editForm.id }), {
editForm.put(route("settings.segments.update", { segment: editForm.id }), {
preserveScroll: true,
onSuccess: () => closeEdit(),
});
@ -87,21 +90,44 @@ const update = () => {
<form @submit.prevent="store" class="space-y-4">
<div>
<InputLabel for="nameCreate" value="Name" />
<TextInput id="nameCreate" v-model="createForm.name" type="text" class="mt-1 block w-full" />
<TextInput
id="nameCreate"
v-model="createForm.name"
type="text"
class="mt-1 block w-full"
/>
<InputError :message="createForm.errors.name" class="mt-1" />
</div>
<div>
<InputLabel for="descCreate" value="Description" />
<TextInput id="descCreate" v-model="createForm.description" type="text" class="mt-1 block w-full" />
<TextInput
id="descCreate"
v-model="createForm.description"
type="text"
class="mt-1 block w-full"
/>
<InputError :message="createForm.errors.description" class="mt-1" />
</div>
<div class="flex items-center gap-2">
<input id="activeCreate" type="checkbox" v-model="createForm.active" />
<label for="activeCreate">Active</label>
</div>
<div class="flex items-center gap-2">
<input
id="excludeCreate"
type="checkbox"
v-model="createForm.exclude"
/>
<label for="excludeCreate">Exclude</label>
</div>
<div class="flex justify-end gap-2 mt-4">
<button type="button" @click="closeCreate" class="px-4 py-2 rounded bg-gray-200 hover:bg-gray-300">Cancel</button>
<button
type="button"
@click="closeCreate"
class="px-4 py-2 rounded bg-gray-200 hover:bg-gray-300"
>
Cancel
</button>
<PrimaryButton :disabled="createForm.processing">Create</PrimaryButton>
</div>
</form>
@ -114,21 +140,41 @@ const update = () => {
<form @submit.prevent="update" class="space-y-4">
<div>
<InputLabel for="nameEdit" value="Name" />
<TextInput id="nameEdit" v-model="editForm.name" type="text" class="mt-1 block w-full" />
<TextInput
id="nameEdit"
v-model="editForm.name"
type="text"
class="mt-1 block w-full"
/>
<InputError :message="editForm.errors.name" class="mt-1" />
</div>
<div>
<InputLabel for="descEdit" value="Description" />
<TextInput id="descEdit" v-model="editForm.description" type="text" class="mt-1 block w-full" />
<TextInput
id="descEdit"
v-model="editForm.description"
type="text"
class="mt-1 block w-full"
/>
<InputError :message="editForm.errors.description" class="mt-1" />
</div>
<div class="flex items-center gap-2">
<input id="activeEdit" type="checkbox" v-model="editForm.active" />
<label for="activeEdit">Active</label>
</div>
<div class="flex items-center gap-2">
<input id="excludeEdit" type="checkbox" v-model="editForm.exclude" />
<label for="excludeEdit">Exclude</label>
</div>
<div class="flex justify-end gap-2 mt-4">
<button type="button" @click="closeEdit" class="px-4 py-2 rounded bg-gray-200 hover:bg-gray-300">Cancel</button>
<button
type="button"
@click="closeEdit"
class="px-4 py-2 rounded bg-gray-200 hover:bg-gray-300"
>
Cancel
</button>
<PrimaryButton :disabled="editForm.processing">Save</PrimaryButton>
</div>
</form>
@ -142,6 +188,7 @@ const update = () => {
<th class="py-2 pr-4">Name</th>
<th class="py-2 pr-4">Description</th>
<th class="py-2 pr-4">Active</th>
<th class="py-2 pr-4">Exclude</th>
<th class="py-2 pr-4">Actions</th>
</tr>
</thead>
@ -152,12 +199,29 @@ const update = () => {
<td class="py-2 pr-4">{{ s.description }}</td>
<td class="py-2 pr-4">
<span class="inline-flex items-center gap-1">
<span :class="s.active ? 'bg-green-500' : 'bg-gray-400'" class="inline-block w-2 h-2 rounded-full"></span>
{{ s.active ? 'Yes' : 'No' }}
<span
:class="s.active ? 'bg-green-500' : 'bg-gray-400'"
class="inline-block w-2 h-2 rounded-full"
></span>
{{ s.active ? "Yes" : "No" }}
</span>
</td>
<td class="py-2 pr-4">
<button class="text-indigo-600 hover:text-indigo-800" @click="openEdit(s)">Edit</button>
<span class="inline-flex items-center gap-1">
<span
:class="s.exclude ? 'bg-green-500' : 'bg-gray-400'"
class="inline-block w-2 h-2 rounded-full"
></span>
{{ s.exclude ? "Yes" : "No" }}
</span>
</td>
<td class="py-2 pr-4">
<button
class="text-indigo-600 hover:text-indigo-800"
@click="openEdit(s)"
>
Edit
</button>
<!-- Delete intentionally skipped as requested -->
</td>
</tr>