feat: gérer les constructeurs multiples

This commit is contained in:
Matthieu
2025-10-28 16:37:10 +01:00
parent da447e4ea2
commit b752fba69a
14 changed files with 901 additions and 222 deletions

View File

@@ -32,7 +32,15 @@
Défini dans le catalogue Défini dans le catalogue
</span> </span>
<span v-if="component.reference" class="badge badge-outline badge-sm">{{ component.reference }}</span> <span v-if="component.reference" class="badge badge-outline badge-sm">{{ component.reference }}</span>
<span v-if="component.constructeur" class="badge badge-outline badge-sm">{{ component.constructeur?.name }}</span> <template v-if="componentConstructeursDisplay.length">
<span
v-for="constructeur in componentConstructeursDisplay"
:key="constructeur.id"
class="badge badge-outline badge-sm"
>
{{ constructeur.name }}
</span>
</template>
<span v-if="component.prix" class="badge badge-primary badge-sm">{{ component.prix }}</span> <span v-if="component.prix" class="badge badge-primary badge-sm">{{ component.prix }}</span>
<span <span
v-if="component.typeMachineComponentRequirement" v-if="component.typeMachineComponentRequirement"
@@ -94,16 +102,26 @@
<ConstructeurSelect <ConstructeurSelect
v-if="isEditMode" v-if="isEditMode"
class="w-full" class="w-full"
:model-value="component.constructeurId || component.constructeur?.id || null" :model-value="componentConstructeurIds"
@update:model-value="handleConstructeurChange" @update:model-value="handleConstructeurChange"
/> />
<div v-else class="input input-bordered input-sm bg-base-200"> <div v-else class="input input-bordered input-sm bg-base-200">
<div class="flex flex-col"> <div v-if="componentConstructeursDisplay.length" class="space-y-1">
<span class="font-medium">{{ component.constructeur?.name || 'Non défini' }}</span> <div
<span class="text-xs text-gray-500"> v-for="constructeur in componentConstructeursDisplay"
{{ [component.constructeur?.email, component.constructeur?.phone].filter(Boolean).join(' • ') }} :key="constructeur.id"
</span> class="flex flex-col"
>
<span class="font-medium">{{ constructeur.name }}</span>
<span
v-if="formatConstructeurContact(constructeur)"
class="text-xs text-gray-500"
>
{{ formatConstructeurContact(constructeur) }}
</span>
</div>
</div> </div>
<span v-else class="font-medium">Non défini</span>
</div> </div>
</div> </div>
</div> </div>
@@ -331,6 +349,12 @@ import DocumentPreviewModal from '~/components/DocumentPreviewModal.vue'
import IconLucideChevronRight from '~icons/lucide/chevron-right' import IconLucideChevronRight from '~icons/lucide/chevron-right'
import { useCustomFields } from '~/composables/useCustomFields' import { useCustomFields } from '~/composables/useCustomFields'
import { useToast } from '~/composables/useToast' import { useToast } from '~/composables/useToast'
import { useConstructeurs } from '~/composables/useConstructeurs'
import {
formatConstructeurContact as formatConstructeurContactSummary,
resolveConstructeurs,
uniqueConstructeurIds,
} from '~/shared/constructeurUtils'
const props = defineProps({ const props = defineProps({
component: { component: {
@@ -406,6 +430,28 @@ const childComponents = computed(() => {
return Array.isArray(list) ? list : [] return Array.isArray(list) ? list : []
}) })
const { constructeurs } = useConstructeurs()
const componentConstructeurIds = computed(() =>
uniqueConstructeurIds(
props.component,
Array.isArray(props.component.constructeurs) ? props.component.constructeurs : [],
props.component.constructeur ? [props.component.constructeur] : [],
),
)
const componentConstructeursDisplay = computed(() =>
resolveConstructeurs(
componentConstructeurIds.value,
Array.isArray(props.component.constructeurs) ? props.component.constructeurs : [],
props.component.constructeur ? [props.component.constructeur] : [],
constructeurs.value,
),
)
const formatConstructeurContact = (constructeur) =>
formatConstructeurContactSummary(constructeur)
const extractStructureCustomFields = (structure) => { const extractStructureCustomFields = (structure) => {
if (!structure || typeof structure !== 'object') { if (!structure || typeof structure !== 'object') {
return [] return []
@@ -686,7 +732,17 @@ watch(
) )
const handleConstructeurChange = async (value) => { const handleConstructeurChange = async (value) => {
props.component.constructeurId = value const ids = uniqueConstructeurIds(value)
props.component.constructeurIds = [...ids]
props.component.constructeurId = null
props.component.constructeur = null
props.component.constructeurs = resolveConstructeurs(
ids,
constructeurs.value,
Array.isArray(props.component.constructeurs) ? props.component.constructeurs : [],
)
await updateComponent() await updateComponent()
} }
@@ -723,7 +779,10 @@ const toggleCollapse = () => {
} }
const updateComponent = () => { const updateComponent = () => {
emit('update', props.component) emit('update', {
...props.component,
constructeurIds: componentConstructeurIds.value,
})
} }
function resolveFieldKey(field, index) { function resolveFieldKey(field, index) {

View File

@@ -1,7 +1,7 @@
<template> <template>
<div class="space-y-2 constructeur-select"> <div class="space-y-2 constructeur-select">
<label v-if="label" class="label"><span class="label-text">{{ label }}</span></label> <label v-if="label" class="label"><span class="label-text">{{ label }}</span></label>
<div class="flex items-center gap-2"> <div class="flex items-start gap-2">
<div class="relative flex-1"> <div class="relative flex-1">
<input <input
v-model="searchTerm" v-model="searchTerm"
@@ -33,13 +33,17 @@
:key="option.id" :key="option.id"
type="button" type="button"
class="w-full text-left px-3 py-2 hover:bg-base-200 focus:bg-base-200 focus:outline-none" class="w-full text-left px-3 py-2 hover:bg-base-200 focus:bg-base-200 focus:outline-none"
@click="selectOption(option)" :class="{ 'bg-base-200': isSelected(option.id) }"
@click="toggleOption(option)"
> >
<div class="flex flex-col"> <div class="flex items-center justify-between gap-3">
<span class="font-medium">{{ option.name }}</span> <div class="flex flex-col">
<span class="text-xs text-gray-500"> <span class="font-medium">{{ option.name }}</span>
{{ [option.email, option.phone].filter(Boolean).join(' • ') || '—' }} <span class="text-xs text-gray-500">
</span> {{ formatConstructeurContact(option) || '—' }}
</span>
</div>
<IconLucideCheck v-if="isSelected(option.id)" class="w-4 h-4 text-primary" aria-hidden="true" />
</div> </div>
</button> </button>
</div> </div>
@@ -49,10 +53,25 @@
</button> </button>
</div> </div>
<div v-if="selectedConstructeur" class="text-xs text-gray-500"> <div class="flex flex-wrap gap-2 min-h-[1.5rem]">
<span class="font-medium">{{ selectedConstructeur.name }}</span> <span v-if="!selectedConstructeurs.length" class="text-sm text-gray-500">
<span v-if="selectedConstructeur.email"> {{ selectedConstructeur.email }}</span> Aucun constructeur sélectionné
<span v-if="selectedConstructeur.phone"> {{ selectedConstructeur.phone }}</span> </span>
<span
v-for="constructeur in selectedConstructeurs"
:key="constructeur.id"
class="badge badge-outline gap-1"
>
<span>{{ constructeur.name }}</span>
<button
type="button"
class="btn btn-ghost btn-xs p-0"
aria-label="Retirer le constructeur"
@click="removeConstructeur(constructeur.id)"
>
<IconLucideX class="w-3 h-3" aria-hidden="true" />
</button>
</span>
</div> </div>
<dialog class="modal" :class="{ 'modal-open': openCreateModal }"> <dialog class="modal" :class="{ 'modal-open': openCreateModal }">
@@ -94,89 +113,131 @@
</div> </div>
</template> </template>
<script setup> <script setup lang="ts">
import { ref, watch, computed, onMounted, onBeforeUnmount } from 'vue' import { ref, watch, computed, onMounted, onBeforeUnmount } from 'vue'
import type { PropType } from 'vue'
import FieldEmail from '~/components/form/FieldEmail.vue' import FieldEmail from '~/components/form/FieldEmail.vue'
import FieldPhone from '~/components/form/FieldPhone.vue' import FieldPhone from '~/components/form/FieldPhone.vue'
import { useConstructeurs } from '~/composables/useConstructeurs' import { useConstructeurs } from '~/composables/useConstructeurs'
import IconLucideChevronsUpDown from '~icons/lucide/chevrons-up-down' import IconLucideChevronsUpDown from '~icons/lucide/chevrons-up-down'
import IconLucideCheck from '~icons/lucide/check'
import IconLucideX from '~icons/lucide/x'
import {
type ConstructeurSummary,
formatConstructeurContact,
resolveConstructeurs,
uniqueConstructeurIds,
} from '~/shared/constructeurUtils'
const props = defineProps({ const props = defineProps({
modelValue: { modelValue: {
type: String, type: Array as PropType<string[]>,
default: null default: () => [],
}, },
label: { label: {
type: String, type: String,
default: '' default: '',
}, },
placeholder: { placeholder: {
type: String, type: String,
default: 'Sélectionner ou créer un constructeur...' default: 'Sélectionner ou créer un constructeur...',
} },
}) })
const emit = defineEmits(['update:modelValue']) const emit = defineEmits<{
(e: 'update:modelValue', value: string[]): void
}>()
const { constructeurs, searchConstructeurs, createConstructeur } = useConstructeurs() const { constructeurs, searchConstructeurs, createConstructeur } = useConstructeurs()
const searchTerm = ref('') const searchTerm = ref('')
const openDropdown = ref(false) const openDropdown = ref(false)
const openCreateModal = ref(false) const openCreateModal = ref(false)
const creating = ref(false) const creating = ref(false)
const options = ref([]) const options = ref<ConstructeurSummary[]>([])
let searchTimeout = null const selectedIds = ref<string[]>([])
let searchTimeout: ReturnType<typeof setTimeout> | null = null
let lastSearchTerm = '' let lastSearchTerm = ''
const applyOptions = (items = []) => { const uniqueOptions = (items: ConstructeurSummary[] = []) => {
const selectedId = props.modelValue const seen = new Map<string, ConstructeurSummary>()
const cloned = [...items] items.forEach((item) => {
const limited = cloned.slice(0, 10) if (item && typeof item === 'object' && typeof item.id === 'string') {
seen.set(item.id, item)
if (selectedId && !limited.some(item => item.id === selectedId)) {
const selected = cloned.find(item => item.id === selectedId)
if (selected) {
if (limited.length >= 10) { limited.pop() }
limited.unshift(selected)
} }
} })
return Array.from(seen.values())
}
options.value = limited const applyOptions = (items: ConstructeurSummary[] = []) => {
const normalized = uniqueOptions(items)
const limited = normalized.slice(0, 10)
selectedIds.value.forEach((id) => {
if (!limited.some((item) => item.id === id)) {
const match =
normalized.find((item) => item.id === id) ||
constructeurs.value.find((item) => item.id === id)
if (match) {
if (limited.length >= 10) {
limited.pop()
}
limited.unshift(match)
}
}
})
options.value = uniqueOptions(limited)
} }
const createForm = ref({ const createForm = ref({
name: '', name: '',
email: '', email: '',
phone: '' phone: '',
}) })
const selectedConstructeur = computed(() => const optionLookup = computed(() => {
constructeurs.value.find(item => item.id === props.modelValue) || null const map = new Map<string, ConstructeurSummary>()
) constructeurs.value.forEach((item: ConstructeurSummary) => {
map.set(item.id, item)
})
options.value.forEach((item) => {
map.set(item.id, item)
})
return map
})
watch( const selectedConstructeurs = computed<ConstructeurSummary[]>(() => {
() => props.modelValue, if (!selectedIds.value.length) {
(newValue) => { return []
if (newValue && !selectedConstructeur.value) { }
// ensure current selection is loaded
ensureOptionsLoaded(true)
}
if (newValue) {
const match = constructeurs.value.find(item => item.id === newValue)
if (match) {
searchTerm.value = match.name
}
}
},
{ immediate: true }
)
async function ensureOptionsLoaded (force = false) { return selectedIds.value
.map((id) => optionLookup.value.get(id))
.filter((item): item is ConstructeurSummary => Boolean(item))
})
const isSelected = (id: string) => selectedIds.value.includes(id)
const emitSelection = (ids: string[]) => {
const normalized = uniqueConstructeurIds(ids)
selectedIds.value = normalized
emit('update:modelValue', normalized)
}
const ensureOptionsLoaded = async (force = false) => {
if (!force && !searchTerm.value && constructeurs.value.length) { if (!force && !searchTerm.value && constructeurs.value.length) {
applyOptions(constructeurs.value) applyOptions(constructeurs.value as ConstructeurSummary[])
return return
} }
if (!force && searchTerm.value === lastSearchTerm && options.value.length) { return }
if (options.value.length && !force) { return } if (!force && searchTerm.value === lastSearchTerm && options.value.length) {
return
}
if (options.value.length && !force) {
return
}
const result = await searchConstructeurs(searchTerm.value) const result = await searchConstructeurs(searchTerm.value)
if (result.success) { if (result.success) {
applyOptions(result.data || []) applyOptions(result.data || [])
@@ -186,14 +247,18 @@ async function ensureOptionsLoaded (force = false) {
const onSearch = () => { const onSearch = () => {
openDropdown.value = true openDropdown.value = true
clearTimeout(searchTimeout) if (searchTimeout) {
clearTimeout(searchTimeout)
}
searchTimeout = setTimeout(async () => { searchTimeout = setTimeout(async () => {
if (!searchTerm.value && constructeurs.value.length) { if (!searchTerm.value && constructeurs.value.length) {
applyOptions(constructeurs.value) applyOptions(constructeurs.value as ConstructeurSummary[])
lastSearchTerm = '' lastSearchTerm = ''
return return
} }
if (searchTerm.value === lastSearchTerm) { return } if (searchTerm.value === lastSearchTerm) {
return
}
const result = await searchConstructeurs(searchTerm.value) const result = await searchConstructeurs(searchTerm.value)
if (result.success) { if (result.success) {
applyOptions(result.data || []) applyOptions(result.data || [])
@@ -202,10 +267,18 @@ const onSearch = () => {
}, 250) }, 250)
} }
const selectOption = (option) => { const toggleOption = (option: ConstructeurSummary) => {
emit('update:modelValue', option.id) const ids = new Set(selectedIds.value)
openDropdown.value = false if (ids.has(option.id)) {
searchTerm.value = option.name ids.delete(option.id)
} else {
ids.add(option.id)
}
emitSelection(Array.from(ids))
}
const removeConstructeur = (id: string) => {
emitSelection(selectedIds.value.filter((item) => item !== id))
} }
const closeCreateModal = () => { const closeCreateModal = () => {
@@ -216,31 +289,24 @@ const closeCreateModal = () => {
const handleCreate = async () => { const handleCreate = async () => {
creating.value = true creating.value = true
const payload = { ...createForm.value } const payload = { ...createForm.value }
if (!payload.phone) { delete payload.phone } if (!payload.phone) {
if (!payload.email) { delete payload.email } delete payload.phone
}
if (!payload.email) {
delete payload.email
}
const result = await createConstructeur(payload) const result = await createConstructeur(payload)
creating.value = false creating.value = false
if (result.success) { if (result.success) {
emit('update:modelValue', result.data.id) emitSelection([...selectedIds.value, result.data.id])
searchTerm.value = result.data.name searchTerm.value = ''
closeCreateModal() closeCreateModal()
await ensureOptionsLoaded(true) await ensureOptionsLoaded(true)
} }
} }
watch( const clickHandler = (event: Event) => {
constructeurs, const element = event.target as HTMLElement | null
(list) => {
applyOptions(list || [])
if (!searchTerm.value) {
lastSearchTerm = ''
}
},
{ immediate: true }
)
const clickHandler = (event) => {
const element = event.target
if (element && element.closest) { if (element && element.closest) {
if ( if (
element.closest('.menu') || element.closest('.menu') ||
@@ -254,6 +320,39 @@ const clickHandler = (event) => {
openDropdown.value = false openDropdown.value = false
} }
watch(
() => props.modelValue,
(newValue) => {
selectedIds.value = uniqueConstructeurIds(newValue)
},
{ immediate: true },
)
watch(
selectedIds,
async (ids) => {
if (!ids.length) {
return
}
const missing = ids.some((id) => !optionLookup.value.get(id))
if (missing) {
await ensureOptionsLoaded(true)
}
},
{ immediate: true },
)
watch(
constructeurs,
(list) => {
applyOptions((list as ConstructeurSummary[]) || [])
if (!searchTerm.value) {
lastSearchTerm = ''
}
},
{ immediate: true },
)
onMounted(() => { onMounted(() => {
window.addEventListener('click', clickHandler) window.addEventListener('click', clickHandler)
ensureOptionsLoaded() ensureOptionsLoaded()
@@ -261,6 +360,24 @@ onMounted(() => {
onBeforeUnmount(() => { onBeforeUnmount(() => {
window.removeEventListener('click', clickHandler) window.removeEventListener('click', clickHandler)
clearTimeout(searchTimeout) if (searchTimeout) {
clearTimeout(searchTimeout)
}
}) })
watch(
selectedIds,
(ids) => {
// ensure options contain newly selected ids
const resolved = resolveConstructeurs(
ids,
constructeurs.value as ConstructeurSummary[],
options.value,
)
if (resolved.length) {
applyOptions([...resolved, ...options.value])
}
},
{ immediate: true },
)
</script> </script>

View File

@@ -68,22 +68,33 @@
</div> </div>
<div> <div>
<span class="font-medium">Constructeur:</span> <span class="font-medium">Constructeur:</span>
<span v-if="!isEditMode" class="ml-2"> <div v-if="!isEditMode" class="ml-2">
<span class="font-medium">{{ <div v-if="pieceConstructeursDisplay.length" class="space-y-1">
piece.constructeur?.name || "Non défini" <div
}}</span> v-for="constructeur in pieceConstructeursDisplay"
<span v-if="piece.constructeur" class="block text-xs text-gray-500"> :key="constructeur.id"
{{ class="flex flex-col"
[piece.constructeur?.email, piece.constructeur?.phone] >
.filter(Boolean) <span class="font-medium">
.join(" ") {{ constructeur.name }}
}} </span>
<span
v-if="formatConstructeurContact(constructeur)"
class="text-xs text-gray-500"
>
{{ formatConstructeurContact(constructeur) }}
</span>
</div>
</div>
<span v-else class="font-medium">
Non défini
</span> </span>
</span> </div>
<ConstructeurSelect <ConstructeurSelect
v-else v-else
class="w-full" class="w-full"
:model-value="piece.constructeurId || piece.constructeur?.id || null" :model-value="pieceConstructeurIds"
placeholder="Sélectionner un ou plusieurs constructeurs..."
@update:model-value="handleConstructeurChange" @update:model-value="handleConstructeurChange"
/> />
</div> </div>
@@ -353,6 +364,7 @@
<script setup> <script setup>
import { reactive, onMounted, watch, ref, computed } from "vue"; import { reactive, onMounted, watch, ref, computed } from "vue";
import ConstructeurSelect from "./ConstructeurSelect.vue"; import ConstructeurSelect from "./ConstructeurSelect.vue";
import { useConstructeurs } from "~/composables/useConstructeurs";
import { useCustomFields } from "~/composables/useCustomFields"; import { useCustomFields } from "~/composables/useCustomFields";
import { useToast } from "~/composables/useToast"; import { useToast } from "~/composables/useToast";
import { useDocuments } from "~/composables/useDocuments"; import { useDocuments } from "~/composables/useDocuments";
@@ -361,6 +373,11 @@ import { canPreviewDocument, isImageDocument, isPdfDocument } from "~/utils/docu
import DocumentUpload from "~/components/DocumentUpload.vue"; import DocumentUpload from "~/components/DocumentUpload.vue";
import DocumentPreviewModal from "~/components/DocumentPreviewModal.vue"; import DocumentPreviewModal from "~/components/DocumentPreviewModal.vue";
import IconLucidePackage from "~icons/lucide/package"; import IconLucidePackage from "~icons/lucide/package";
import {
formatConstructeurContact as formatConstructeurContactSummary,
resolveConstructeurs,
uniqueConstructeurIds,
} from "~/shared/constructeurUtils";
const props = defineProps({ const props = defineProps({
piece: { piece: {
@@ -716,8 +733,39 @@ const candidateCustomFields = computed(() => {
return Array.from(map.values()); return Array.from(map.values());
}); });
const { constructeurs } = useConstructeurs();
const pieceConstructeurIds = computed(() =>
uniqueConstructeurIds(
props.piece,
Array.isArray(props.piece.constructeurs) ? props.piece.constructeurs : [],
props.piece.constructeur ? [props.piece.constructeur] : [],
),
);
const pieceConstructeursDisplay = computed(() =>
resolveConstructeurs(
pieceConstructeurIds.value,
Array.isArray(props.piece.constructeurs) ? props.piece.constructeurs : [],
props.piece.constructeur ? [props.piece.constructeur] : [],
constructeurs.value,
),
);
const formatConstructeurContact = (constructeur) =>
formatConstructeurContactSummary(constructeur);
const handleConstructeurChange = (value) => { const handleConstructeurChange = (value) => {
props.piece.constructeurId = value; const ids = uniqueConstructeurIds(value);
props.piece.constructeurIds = [...ids];
props.piece.constructeurId = null;
props.piece.constructeur = null;
props.piece.constructeurs = resolveConstructeurs(
ids,
constructeurs.value,
Array.isArray(props.piece.constructeurs) ? props.piece.constructeurs : [],
);
updatePiece(); updatePiece();
}; };
@@ -971,7 +1019,7 @@ const updatePiece = () => {
...props.piece, ...props.piece,
...pieceData, ...pieceData,
prix: prixValue && prixValue !== "" ? parseFloat(prixValue) : null, prix: prixValue && prixValue !== "" ? parseFloat(prixValue) : null,
constructeurId: props.piece.constructeurId || null, constructeurIds: pieceConstructeurIds.value,
}); });
}; };

View File

@@ -1,6 +1,7 @@
import { ref } from 'vue' import { ref } from 'vue'
import { useToast } from './useToast' import { useToast } from './useToast'
import { useApi } from './useApi' import { useApi } from './useApi'
import { buildConstructeurRequestPayload } from '~/shared/constructeurUtils'
const composants = ref([]) const composants = ref([])
const loading = ref(false) const loading = ref(false)
@@ -27,7 +28,7 @@ const loadComposants = async () => {
const createComposant = async (composantData) => { const createComposant = async (composantData) => {
loading.value = true loading.value = true
try { try {
const result = await post('/composants', composantData) const result = await post('/composants', buildConstructeurRequestPayload(composantData))
if (result.success) { if (result.success) {
composants.value.push(result.data) composants.value.push(result.data)
const displayName = result.data?.name const displayName = result.data?.name
@@ -48,7 +49,7 @@ const loadComposants = async () => {
const updateComposantData = async (id, composantData) => { const updateComposantData = async (id, composantData) => {
loading.value = true loading.value = true
try { try {
const result = await patch(`/composants/${id}`, composantData) const result = await patch(`/composants/${id}`, buildConstructeurRequestPayload(composantData))
if (result.success) { if (result.success) {
const updated = result.data const updated = result.data
const index = composants.value.findIndex(comp => comp.id === id) const index = composants.value.findIndex(comp => comp.id === id)

View File

@@ -1,6 +1,7 @@
import { ref } from 'vue' import { ref } from 'vue'
import { useToast } from './useToast' import { useToast } from './useToast'
import { useApi } from './useApi' import { useApi } from './useApi'
import { buildConstructeurRequestPayload } from '~/shared/constructeurUtils'
const machines = ref([]) const machines = ref([])
const loading = ref(false) const loading = ref(false)
@@ -76,7 +77,7 @@ export function useMachines () {
const createMachine = async (machineData) => { const createMachine = async (machineData) => {
loading.value = true loading.value = true
try { try {
const result = await post('/machines', machineData) const result = await post('/machines', buildConstructeurRequestPayload(machineData))
if (result.success) { if (result.success) {
const createdMachine = normalizeMachineResponse(result.data) || const createdMachine = normalizeMachineResponse(result.data) ||
normalizeMachineResponse(result.data?.machine) || normalizeMachineResponse(result.data?.machine) ||
@@ -105,13 +106,13 @@ export function useMachines () {
// Les composants et pièces seront créés automatiquement // Les composants et pièces seront créés automatiquement
} }
return await createMachine(machineWithStructure) return await createMachine(buildConstructeurRequestPayload(machineWithStructure))
} }
const updateMachineData = async (id, machineData) => { const updateMachineData = async (id, machineData) => {
loading.value = true loading.value = true
try { try {
const result = await patch(`/machines/${id}`, machineData) const result = await patch(`/machines/${id}`, buildConstructeurRequestPayload(machineData))
if (result.success) { if (result.success) {
const updatedMachine = normalizeMachineResponse(result.data) || const updatedMachine = normalizeMachineResponse(result.data) ||
normalizeMachineResponse(result.data?.machine) || normalizeMachineResponse(result.data?.machine) ||

View File

@@ -1,6 +1,7 @@
import { ref } from 'vue' import { ref } from 'vue'
import { useToast } from './useToast' import { useToast } from './useToast'
import { useApi } from './useApi' import { useApi } from './useApi'
import { buildConstructeurRequestPayload } from '~/shared/constructeurUtils'
const pieces = ref([]) const pieces = ref([])
const loading = ref(false) const loading = ref(false)
@@ -27,7 +28,7 @@ export function usePieces () {
const createPiece = async (pieceData) => { const createPiece = async (pieceData) => {
loading.value = true loading.value = true
try { try {
const result = await post('/pieces', pieceData) const result = await post('/pieces', buildConstructeurRequestPayload(pieceData))
if (result.success) { if (result.success) {
pieces.value.push(result.data) pieces.value.push(result.data)
const displayName = result.data?.name const displayName = result.data?.name
@@ -48,7 +49,7 @@ export function usePieces () {
const updatePieceData = async (id, pieceData) => { const updatePieceData = async (id, pieceData) => {
loading.value = true loading.value = true
try { try {
const result = await patch(`/pieces/${id}`, pieceData) const result = await patch(`/pieces/${id}`, buildConstructeurRequestPayload(pieceData))
if (result.success) { if (result.success) {
const updated = result.data const updated = result.data
const index = pieces.value.findIndex(piece => piece.id === id) const index = pieces.value.findIndex(piece => piece.id === id)

View File

@@ -98,10 +98,10 @@
<span class="label-text">Constructeur</span> <span class="label-text">Constructeur</span>
</label> </label>
<ConstructeurSelect <ConstructeurSelect
v-model="editionForm.constructeurId" v-model="editionForm.constructeurIds"
class="w-full" class="w-full"
:disabled="saving" :disabled="saving"
placeholder="Rechercher un constructeur..." placeholder="Rechercher un ou plusieurs constructeurs..."
/> />
</div> </div>
</div> </div>
@@ -404,6 +404,7 @@ import { useApi } from '~/composables/useApi'
import { useToast } from '~/composables/useToast' import { useToast } from '~/composables/useToast'
import { useDocuments } from '~/composables/useDocuments' import { useDocuments } from '~/composables/useDocuments'
import { formatStructurePreview, normalizeStructureForEditor } from '~/shared/modelUtils' import { formatStructurePreview, normalizeStructureForEditor } from '~/shared/modelUtils'
import { uniqueConstructeurIds } from '~/shared/constructeurUtils'
import type { ComponentModelStructure } from '~/shared/types/inventory' import type { ComponentModelStructure } from '~/shared/types/inventory'
import type { ModelType } from '~/services/modelTypes' import type { ModelType } from '~/services/modelTypes'
import { getFileIcon } from '~/utils/fileIcons' import { getFileIcon } from '~/utils/fileIcons'
@@ -448,7 +449,7 @@ const selectedTypeId = ref<string>('')
const editionForm = reactive({ const editionForm = reactive({
name: '' as string, name: '' as string,
reference: '' as string, reference: '' as string,
constructeurId: null as string | null, constructeurIds: [] as string[],
prix: '' as string, prix: '' as string,
}) })
@@ -651,7 +652,11 @@ watch(
editionForm.name = currentComponent.name || '' editionForm.name = currentComponent.name || ''
editionForm.reference = currentComponent.reference || '' editionForm.reference = currentComponent.reference || ''
editionForm.constructeurId = currentComponent.constructeur?.id || currentComponent.constructeurId || null editionForm.constructeurIds = uniqueConstructeurIds(
currentComponent,
Array.isArray(currentComponent.constructeurs) ? currentComponent.constructeurs : [],
currentComponent.constructeur ? [currentComponent.constructeur] : [],
)
editionForm.prix = currentComponent.prix !== null && currentComponent.prix !== undefined ? String(currentComponent.prix) : '' editionForm.prix = currentComponent.prix !== null && currentComponent.prix !== undefined ? String(currentComponent.prix) : ''
customFieldInputs.value = buildCustomFieldInputs( customFieldInputs.value = buildCustomFieldInputs(
@@ -691,7 +696,7 @@ const submitEdition = async () => {
const reference = editionForm.reference.trim() const reference = editionForm.reference.trim()
payload.reference = reference ? reference : null payload.reference = reference ? reference : null
payload.constructeurId = editionForm.constructeurId || null payload.constructeurIds = uniqueConstructeurIds(editionForm.constructeurIds)
if (rawPrice) { if (rawPrice) {
const parsed = Number(rawPrice) const parsed = Number(rawPrice)

View File

@@ -71,10 +71,10 @@
<span class="label-text">Constructeur</span> <span class="label-text">Constructeur</span>
</label> </label>
<ConstructeurSelect <ConstructeurSelect
v-model="creationForm.constructeurId" v-model="creationForm.constructeurIds"
class="w-full" class="w-full"
:disabled="submitting || !selectedType" :disabled="submitting || !selectedType"
placeholder="Rechercher un constructeur..." placeholder="Rechercher un ou plusieurs constructeurs..."
/> />
</div> </div>
</div> </div>
@@ -339,6 +339,7 @@ import { useToast } from '~/composables/useToast'
import { useCustomFields } from '~/composables/useCustomFields' import { useCustomFields } from '~/composables/useCustomFields'
import { useDocuments } from '~/composables/useDocuments' import { useDocuments } from '~/composables/useDocuments'
import { formatStructurePreview, normalizeStructureForEditor } from '~/shared/modelUtils' import { formatStructurePreview, normalizeStructureForEditor } from '~/shared/modelUtils'
import { uniqueConstructeurIds } from '~/shared/constructeurUtils'
import type { import type {
ComponentModelPiece, ComponentModelPiece,
ComponentModelStructure, ComponentModelStructure,
@@ -376,7 +377,7 @@ const submitting = ref(false)
const creationForm = reactive({ const creationForm = reactive({
name: '' as string, name: '' as string,
reference: '' as string, reference: '' as string,
constructeurId: null as string | null, constructeurIds: [] as string[],
prix: '' as string, prix: '' as string,
}) })
const lastSuggestedName = ref('') const lastSuggestedName = ref('')
@@ -737,7 +738,7 @@ const resolveSubcomponentLabel = (node: Record<string, any>) => {
const clearCreationForm = () => { const clearCreationForm = () => {
creationForm.name = '' creationForm.name = ''
creationForm.reference = '' creationForm.reference = ''
creationForm.constructeurId = null creationForm.constructeurIds = []
creationForm.prix = '' creationForm.prix = ''
lastSuggestedName.value = '' lastSuggestedName.value = ''
structureAssignments.value = null structureAssignments.value = null
@@ -758,8 +759,8 @@ const submitCreation = async () => {
payload.reference = reference payload.reference = reference
} }
if (creationForm.constructeurId) { if (creationForm.constructeurIds.length) {
payload.constructeurId = creationForm.constructeurId payload.constructeurIds = uniqueConstructeurIds(creationForm.constructeurIds)
} }
const rawPrice = typeof creationForm.prix === 'string' const rawPrice = typeof creationForm.prix === 'string'

View File

@@ -143,19 +143,27 @@
v-if="isEditMode" v-if="isEditMode"
class="w-full" class="w-full"
:key="machine.value?.id" :key="machine.value?.id"
:model-value="machineConstructeurId" :model-value="machineConstructeurIds"
placeholder="Rechercher un constructeur..." placeholder="Rechercher un ou plusieurs constructeurs..."
@update:modelValue="handleMachineConstructeurChange" @update:modelValue="handleMachineConstructeurChange"
/> />
<div v-else class="input input-bordered bg-base-200"> <div v-else class="input input-bordered bg-base-200">
<div class="flex flex-col"> <div v-if="machineConstructeursDisplay.length" class="space-y-1">
<span class="font-medium"> <div
{{ machineConstructeurDisplay?.name || machineConstructeurContact }} v-for="constructeur in machineConstructeursDisplay"
</span> :key="constructeur.id"
<span v-if="machineConstructeurContact" class="text-xs text-gray-500"> class="flex flex-col"
{{ machineConstructeurContact }} >
</span> <span class="font-medium">{{ constructeur.name }}</span>
<span
v-if="formatConstructeurContactSummary(constructeur)"
class="text-xs text-gray-500"
>
{{ formatConstructeurContactSummary(constructeur) }}
</span>
</div>
</div> </div>
<span v-else class="font-medium">Non défini</span>
</div> </div>
</div> </div>
</div> </div>
@@ -542,6 +550,11 @@ import { useToast } from '~/composables/useToast'
import { useDocuments } from '~/composables/useDocuments' import { useDocuments } from '~/composables/useDocuments'
import { getFileIcon } from '~/utils/fileIcons' import { getFileIcon } from '~/utils/fileIcons'
import { sanitizeDefinitionOverrides, normalizeStructureForEditor } from '~/shared/modelUtils' import { sanitizeDefinitionOverrides, normalizeStructureForEditor } from '~/shared/modelUtils'
import {
resolveConstructeurs,
uniqueConstructeurIds,
formatConstructeurContact as formatConstructeurContactSummary,
} from '~/shared/constructeurUtils'
import { canPreviewDocument, isImageDocument, isPdfDocument } from '~/utils/documentPreview' import { canPreviewDocument, isImageDocument, isPdfDocument } from '~/utils/documentPreview'
import ComponentHierarchy from '~/components/ComponentHierarchy.vue' import ComponentHierarchy from '~/components/ComponentHierarchy.vue'
import DocumentUpload from '~/components/DocumentUpload.vue' import DocumentUpload from '~/components/DocumentUpload.vue'
@@ -606,26 +619,36 @@ const { constructeurs, loadConstructeurs } = useConstructeurs()
// Champs de la machine // Champs de la machine
const machineName = ref('') const machineName = ref('')
const machineReference = ref('') const machineReference = ref('')
const machineConstructeurId = ref(null) const machineConstructeurIds = ref([])
const machineConstructeurDisplay = computed(() => { const machineConstructeurId = computed({
const id = machineConstructeurId.value || machine.value?.constructeur?.id || machine.value?.constructeurId get: () => machineConstructeurIds.value[0] || null,
if (!id) return machine.value?.constructeur || null set: (value) => {
return constructeurs.value.find(item => item.id === id) || machine.value?.constructeur || null machineConstructeurIds.value = value ? [value] : []
},
}) })
const machineConstructeurContact = computed(() => { const machineConstructeursDisplay = computed(() => {
const constructeur = machineConstructeurDisplay.value const ids = uniqueConstructeurIds(
if (!constructeur) { machineConstructeurIds.value,
return '' machine.value?.constructeurIds,
} machine.value?.constructeurs,
return [constructeur.email, constructeur.phone].filter(Boolean).join(' • ') machine.value?.constructeur,
}) )
const hasMachineConstructeur = computed(() => { return resolveConstructeurs(
const constructeur = machineConstructeurDisplay.value ids,
if (!constructeur) { Array.isArray(machine.value?.constructeurs) ? machine.value?.constructeurs : [],
return false machine.value?.constructeur ? [machine.value.constructeur] : [],
} constructeurs.value,
return Boolean(constructeur.name || machineConstructeurContact.value) )
}) })
const machineConstructeurContact = computed(() =>
machineConstructeursDisplay.value
.map((constructeur) => formatConstructeurContactSummary(constructeur))
.filter(Boolean)
.join(' • '),
)
const hasMachineConstructeur = computed(
() => machineConstructeursDisplay.value.length > 0,
)
const machineDocumentFiles = ref([]) const machineDocumentFiles = ref([])
const machineDocumentsUploading = ref(false) const machineDocumentsUploading = ref(false)
@@ -826,7 +849,8 @@ const createComponentSelectionEntry = (requirement, source = null) => {
|| requirement?.typeComposant?.name || requirement?.typeComposant?.name
|| '', || '',
reference: source?.reference || '', reference: source?.reference || '',
constructeurId: source?.constructeurId || source?.constructeur?.id || null, constructeurIds: [],
constructeurId: null,
prix: prix:
source?.prix source?.prix
?? source?.price ?? source?.price
@@ -834,6 +858,16 @@ const createComponentSelectionEntry = (requirement, source = null) => {
}, },
} }
const definitionConstructeurIds = uniqueConstructeurIds(
link?.overrides?.constructeurIds,
link?.overrides?.constructeurId,
source?.constructeurIds,
source?.constructeurId,
source?.constructeur,
)
entry.definition.constructeurIds = definitionConstructeurIds
entry.definition.constructeurId = definitionConstructeurIds[0] || null
if (link?.overrides && isPlainObject(link.overrides)) { if (link?.overrides && isPlainObject(link.overrides)) {
entry.definition = { entry.definition = {
...entry.definition, ...entry.definition,
@@ -841,6 +875,14 @@ const createComponentSelectionEntry = (requirement, source = null) => {
} }
} }
const finalizedConstructeurIds = uniqueConstructeurIds(
entry.definition.constructeurIds,
entry.definition.constructeurId,
entry.definition.constructeur,
)
entry.definition.constructeurIds = finalizedConstructeurIds
entry.definition.constructeurId = finalizedConstructeurIds[0] || null
return entry return entry
} }
@@ -887,7 +929,8 @@ const createPieceSelectionEntry = (requirement, source = null) => {
|| requirement?.typePiece?.name || requirement?.typePiece?.name
|| '', || '',
reference: source?.reference || '', reference: source?.reference || '',
constructeurId: source?.constructeurId || source?.constructeur?.id || null, constructeurIds: [],
constructeurId: null,
prix: prix:
source?.prix source?.prix
?? source?.price ?? source?.price
@@ -895,6 +938,16 @@ const createPieceSelectionEntry = (requirement, source = null) => {
}, },
} }
const definitionConstructeurIds = uniqueConstructeurIds(
link?.overrides?.constructeurIds,
link?.overrides?.constructeurId,
source?.constructeurIds,
source?.constructeurId,
source?.constructeur,
)
entry.definition.constructeurIds = definitionConstructeurIds
entry.definition.constructeurId = definitionConstructeurIds[0] || null
if (link?.overrides && isPlainObject(link.overrides)) { if (link?.overrides && isPlainObject(link.overrides)) {
entry.definition = { entry.definition = {
...entry.definition, ...entry.definition,
@@ -902,6 +955,14 @@ const createPieceSelectionEntry = (requirement, source = null) => {
} }
} }
const finalizedConstructeurIds = uniqueConstructeurIds(
entry.definition.constructeurIds,
entry.definition.constructeurId,
entry.definition.constructeur,
)
entry.definition.constructeurIds = finalizedConstructeurIds
entry.definition.constructeurId = finalizedConstructeurIds[0] || null
return entry return entry
} }
@@ -945,7 +1006,9 @@ const setComponentRequirementConstructeur = (requirementId, index, value) => {
const entries = getComponentRequirementEntries(requirementId) const entries = getComponentRequirementEntries(requirementId)
const entry = entries[index] const entry = entries[index]
if (!entry) return if (!entry) return
entry.definition.constructeurId = value || null const ids = uniqueConstructeurIds(value)
entry.definition.constructeurIds = ids
entry.definition.constructeurId = ids[0] || null
} }
const addPieceSelectionEntry = (requirement) => { const addPieceSelectionEntry = (requirement) => {
@@ -976,7 +1039,9 @@ const setPieceRequirementConstructeur = (requirementId, index, value) => {
const entries = getPieceRequirementEntries(requirementId) const entries = getPieceRequirementEntries(requirementId)
const entry = entries[index] const entry = entries[index]
if (!entry) return if (!entry) return
entry.definition.constructeurId = value || null const ids = uniqueConstructeurIds(value)
entry.definition.constructeurIds = ids
entry.definition.constructeurId = ids[0] || null
} }
const collectPiecesForSkeleton = () => { const collectPiecesForSkeleton = () => {
@@ -1298,7 +1363,7 @@ const saveSkeletonConfiguration = async () => {
} }
const handleMachineConstructeurChange = async (value) => { const handleMachineConstructeurChange = async (value) => {
machineConstructeurId.value = value machineConstructeurIds.value = uniqueConstructeurIds(value)
await updateMachineInfo() await updateMachineInfo()
} }
@@ -1332,7 +1397,11 @@ const initMachineFields = () => {
if (machine.value) { if (machine.value) {
machineName.value = machine.value.name || '' machineName.value = machine.value.name || ''
machineReference.value = machine.value.reference || '' machineReference.value = machine.value.reference || ''
machineConstructeurId.value = machine.value.constructeurId || machine.value.constructeur?.id || null machineConstructeurIds.value = uniqueConstructeurIds(
machine.value.constructeurIds,
machine.value.constructeurs,
machine.value.constructeur,
)
} }
} }
@@ -1393,6 +1462,27 @@ const flattenComponents = (list = []) => {
const flattenedComponents = computed(() => flattenComponents(components.value)) const flattenedComponents = computed(() => flattenComponents(components.value))
const collectConstructeurs = (...sources) => {
const ids = uniqueConstructeurIds(...sources)
if (!ids.length) {
return []
}
const pools = sources
.flatMap((source) => {
if (Array.isArray(source)) {
return [source]
}
if (source && typeof source === 'object' && source.id) {
return [[source]]
}
return []
})
.filter(Boolean)
return resolveConstructeurs(ids, ...pools)
}
const componentRequirementGroups = computed(() => { const componentRequirementGroups = computed(() => {
const requirements = machine.value?.typeMachine?.componentRequirements || [] const requirements = machine.value?.typeMachine?.componentRequirements || []
if (!requirements.length) return [] if (!requirements.length) return []
@@ -1430,14 +1520,22 @@ const pieceRequirementGroups = computed(() => {
// Pièces rattachées à la machine directement // Pièces rattachées à la machine directement
machinePieces.value.forEach((piece) => { machinePieces.value.forEach((piece) => {
collected.push({ ...piece, parentComponentName: null }) collected.push({
...piece,
constructeurs: piece.constructeurs || [],
parentComponentName: null,
})
}) })
// Pièces rattachées aux composants // Pièces rattachées aux composants
flattenedComponents.value.forEach((component) => { flattenedComponents.value.forEach((component) => {
if (component.pieces && component.pieces.length) { if (component.pieces && component.pieces.length) {
component.pieces.forEach((piece) => { component.pieces.forEach((piece) => {
collected.push({ ...piece, parentComponentName: component.name }) collected.push({
...piece,
constructeurs: piece.constructeurs || [],
parentComponentName: component.name,
})
}) })
} }
}) })
@@ -2230,12 +2328,34 @@ const transformCustomFields = (pieces) => {
), ),
) )
const constructeurIds = uniqueConstructeurIds(
piece.constructeurIds,
piece.constructeurId,
piece.constructeur,
piece.originalPiece?.constructeurIds,
piece.originalPiece?.constructeurId,
piece.originalPiece?.constructeur,
)
const constructeursList = resolveConstructeurs(
constructeurIds,
Array.isArray(piece.constructeurs) ? piece.constructeurs : [],
piece.constructeur ? [piece.constructeur] : [],
Array.isArray(piece.originalPiece?.constructeurs)
? piece.originalPiece?.constructeurs
: [],
piece.originalPiece?.constructeur ? [piece.originalPiece.constructeur] : [],
constructeurs.value,
)
return { return {
...piece, ...piece,
customFields, customFields,
documents: piece.documents || [], documents: piece.documents || [],
constructeur: piece.constructeur || null, constructeurs: constructeursList,
constructeurId: piece.constructeurId || piece.constructeur?.id || null, constructeur: constructeursList[0] || piece.constructeur || null,
constructeurIds,
constructeurId: constructeurIds[0] || null,
typePieceId: piece.typePieceId typePieceId: piece.typePieceId
|| piece.typeMachinePieceRequirement?.typePieceId || piece.typeMachinePieceRequirement?.typePieceId
|| piece.typePiece?.id || piece.typePiece?.id
@@ -2307,14 +2427,34 @@ const transformComponentCustomFields = (componentsData) => {
? transformComponentCustomFields(component.sousComposants) ? transformComponentCustomFields(component.sousComposants)
: [] : []
const constructeurIds = uniqueConstructeurIds(
component.constructeurIds,
component.constructeurId,
component.constructeur,
actualComponent?.constructeurIds,
actualComponent?.constructeurId,
actualComponent?.constructeur,
)
const constructeursList = resolveConstructeurs(
constructeurIds,
Array.isArray(component.constructeurs) ? component.constructeurs : [],
component.constructeur ? [component.constructeur] : [],
Array.isArray(actualComponent?.constructeurs) ? actualComponent.constructeurs : [],
actualComponent?.constructeur ? [actualComponent.constructeur] : [],
constructeurs.value,
)
return { return {
...component, ...component,
customFields, customFields,
pieces, pieces,
subComponents, subComponents,
documents: component.documents || [], documents: component.documents || [],
constructeur: component.constructeur || null, constructeurs: constructeursList,
constructeurId: component.constructeurId || component.constructeur?.id || null, constructeur: constructeursList[0] || component.constructeur || null,
constructeurIds,
constructeurId: constructeurIds[0] || null,
typeComposantId: component.typeComposantId typeComposantId: component.typeComposantId
|| component.typeMachineComponentRequirement?.typeComposantId || component.typeMachineComponentRequirement?.typeComposantId
|| component.typeComposant?.id || component.typeComposant?.id
@@ -2354,13 +2494,27 @@ const syncMachineCustomFields = () => {
function mergePieceLists(existing = [], updates = []) { function mergePieceLists(existing = [], updates = []) {
if (!existing.length) { if (!existing.length) {
return updates return updates.map(piece => ({
...piece,
constructeurs: piece.constructeurs || [],
}))
} }
if (!updates.length) { if (!updates.length) {
return existing return existing.map(piece => ({
...piece,
constructeurs: piece.constructeurs || [],
}))
} }
const updateMap = new Map(updates.map(piece => [piece.id, piece])) const updateMap = new Map(
updates.map(piece => [
piece.id,
{
...piece,
constructeurs: piece.constructeurs || [],
},
]),
)
const merged = existing.map(piece => { const merged = existing.map(piece => {
const update = updateMap.get(piece.id) const update = updateMap.get(piece.id)
if (!update) { if (!update) {
@@ -2384,17 +2538,43 @@ function mergePieceLists(existing = [], updates = []) {
function mergeComponentTrees(existing = [], updates = []) { function mergeComponentTrees(existing = [], updates = []) {
if (!existing.length) { if (!existing.length) {
return updates return updates.map(component => ({
...component,
constructeurs: component.constructeurs || [],
pieces: (component.pieces || []).map(piece => ({
...piece,
constructeurs: piece.constructeurs || [],
})),
subComponents: mergeComponentTrees([], component.subComponents || []),
}))
} }
if (!updates.length) { if (!updates.length) {
return existing return existing
} }
const updateMap = new Map(updates.map(component => [component.id, component])) const updateMap = new Map(
updates.map(component => [
component.id,
{
...component,
constructeurs: component.constructeurs || [],
pieces: (component.pieces || []).map(piece => ({
...piece,
constructeurs: piece.constructeurs || [],
})),
subComponents: mergeComponentTrees([], component.subComponents || []),
},
]),
)
const merged = existing.map(component => { const merged = existing.map(component => {
const update = updateMap.get(component.id) const update = updateMap.get(component.id)
if (!update) { if (!update) {
return component return {
...component,
constructeurs: component.constructeurs || [],
pieces: mergePieceLists(component.pieces || [], []),
subComponents: mergeComponentTrees(component.subComponents || [], []),
}
} }
return { return {
...component, ...component,
@@ -2529,7 +2709,23 @@ const buildMachineHierarchyFromLinks = (componentLinks = [], pieceLinks = []) =>
skeletonOnly: !pieceId, skeletonOnly: !pieceId,
} }
return basePiece const constructeurs = collectConstructeurs(
appliedPiece.constructeurs,
appliedPiece.constructeur,
appliedPiece.constructeurIds,
appliedPiece.constructeurId,
originalPiece?.constructeurs,
originalPiece?.constructeur,
originalPiece?.constructeurIds,
originalPiece?.constructeurId,
)
return {
...basePiece,
constructeurs,
constructeur: constructeurs[0] || basePiece.constructeur || null,
constructeurId: constructeurs[0]?.id || basePiece.constructeurId || null,
}
} }
const createComponentNode = (link) => { const createComponentNode = (link) => {
@@ -2653,7 +2849,23 @@ const buildMachineHierarchyFromLinks = (componentLinks = [], pieceLinks = []) =>
skeletonOnly: !composantId, skeletonOnly: !composantId,
} }
return baseComponent const constructeurs = collectConstructeurs(
appliedComponent.constructeurs,
appliedComponent.constructeur,
appliedComponent.constructeurIds,
appliedComponent.constructeurId,
originalComponent?.constructeurs,
originalComponent?.constructeur,
originalComponent?.constructeurIds,
originalComponent?.constructeurId,
)
return {
...baseComponent,
constructeurs,
constructeur: constructeurs[0] || baseComponent.constructeur || null,
constructeurId: constructeurs[0]?.id || baseComponent.constructeurId || null,
}
} }
const rootComponents = (Array.isArray(componentLinks) ? componentLinks : []) const rootComponents = (Array.isArray(componentLinks) ? componentLinks : [])
@@ -2792,7 +3004,7 @@ const updateMachineInfo = async () => {
const result = await updateMachineApi(machine.value.id, { const result = await updateMachineApi(machine.value.id, {
name: machineName.value, name: machineName.value,
reference: machineReference.value, reference: machineReference.value,
constructeurId: machineConstructeurId.value || null constructeurIds: machineConstructeurIds.value
}) })
if (result.success) { if (result.success) {
const machinePayload = result.data?.machine && typeof result.data.machine === 'object' const machinePayload = result.data?.machine && typeof result.data.machine === 'object'
@@ -2806,7 +3018,11 @@ const updateMachineInfo = async () => {
documents: machinePayload.documents || machine.value.documents || [], documents: machinePayload.documents || machine.value.documents || [],
customFieldValues: machinePayload.customFieldValues || machine.value.customFieldValues || [], customFieldValues: machinePayload.customFieldValues || machine.value.customFieldValues || [],
} }
machineConstructeurId.value = machine.value.constructeurId || machine.value.constructeur?.id || null machineConstructeurIds.value = uniqueConstructeurIds(
machine.value.constructeurIds,
machine.value.constructeurs,
machine.value.constructeur,
)
const linksApplied = applyMachineLinks(result.data) const linksApplied = applyMachineLinks(result.data)
if (linksApplied && machine.value) { if (linksApplied && machine.value) {
@@ -2823,10 +3039,15 @@ const updateMachineInfo = async () => {
const updateComponent = async (updatedComponent) => { const updateComponent = async (updatedComponent) => {
try { try {
const prixValue = updatedComponent.prix const prixValue = updatedComponent.prix
const constructeurIds = uniqueConstructeurIds(
updatedComponent.constructeurIds,
updatedComponent.constructeurId,
updatedComponent.constructeur,
)
const result = await updateComposantApi(updatedComponent.id, { const result = await updateComposantApi(updatedComponent.id, {
name: updatedComponent.name, name: updatedComponent.name,
reference: updatedComponent.reference, reference: updatedComponent.reference,
constructeurId: updatedComponent.constructeurId || updatedComponent.constructeur?.id || null, constructeurIds,
prix: prixValue && prixValue !== '' ? parseFloat(prixValue) : null, prix: prixValue && prixValue !== '' ? parseFloat(prixValue) : null,
}) })
if (result.success) { if (result.success) {
@@ -2840,10 +3061,15 @@ const updateComponent = async (updatedComponent) => {
const updatePieceFromComponent = async (updatedPiece) => { const updatePieceFromComponent = async (updatedPiece) => {
try { try {
const constructeurIds = uniqueConstructeurIds(
updatedPiece.constructeurIds,
updatedPiece.constructeurId,
updatedPiece.constructeur,
)
const result = await updatePieceApi(updatedPiece.id, { const result = await updatePieceApi(updatedPiece.id, {
name: updatedPiece.name, name: updatedPiece.name,
reference: updatedPiece.reference, reference: updatedPiece.reference,
constructeurId: updatedPiece.constructeurId || updatedPiece.constructeur?.id || null, constructeurIds,
prix: updatedPiece.prix && updatedPiece.prix !== '' ? parseFloat(updatedPiece.prix) : null, prix: updatedPiece.prix && updatedPiece.prix !== '' ? parseFloat(updatedPiece.prix) : null,
}) })
if (result.success) { if (result.success) {
@@ -2870,10 +3096,15 @@ const updatePieceFromComponent = async (updatedPiece) => {
const updatePieceInfo = async (updatedPiece) => { const updatePieceInfo = async (updatedPiece) => {
try { try {
const constructeurIds = uniqueConstructeurIds(
updatedPiece.constructeurIds,
updatedPiece.constructeurId,
updatedPiece.constructeur,
)
const result = await updatePieceApi(updatedPiece.id, { const result = await updatePieceApi(updatedPiece.id, {
name: updatedPiece.name, name: updatedPiece.name,
reference: updatedPiece.reference, reference: updatedPiece.reference,
constructeurId: updatedPiece.constructeurId || updatedPiece.constructeur?.id || null, constructeurIds,
prix: updatedPiece.prix && updatedPiece.prix !== '' ? parseFloat(updatedPiece.prix) : null, prix: updatedPiece.prix && updatedPiece.prix !== '' ? parseFloat(updatedPiece.prix) : null,
}) })
if (result.success) { if (result.success) {

View File

@@ -98,10 +98,10 @@
<span class="label-text">Constructeur</span> <span class="label-text">Constructeur</span>
</label> </label>
<ConstructeurSelect <ConstructeurSelect
v-model="editionForm.constructeurId" v-model="editionForm.constructeurIds"
class="w-full" class="w-full"
:disabled="saving" :disabled="saving"
placeholder="Rechercher un constructeur..." placeholder="Rechercher un ou plusieurs constructeurs..."
/> />
</div> </div>
</div> </div>
@@ -365,6 +365,7 @@ import { useDocuments } from '~/composables/useDocuments'
import { getFileIcon } from '~/utils/fileIcons' import { getFileIcon } from '~/utils/fileIcons'
import { canPreviewDocument, isImageDocument, isPdfDocument } from '~/utils/documentPreview' import { canPreviewDocument, isImageDocument, isPdfDocument } from '~/utils/documentPreview'
import { formatPieceStructurePreview } from '~/shared/modelUtils' import { formatPieceStructurePreview } from '~/shared/modelUtils'
import { uniqueConstructeurIds } from '~/shared/constructeurUtils'
import type { PieceModelStructure } from '~/shared/types/inventory' import type { PieceModelStructure } from '~/shared/types/inventory'
import type { ModelType } from '~/services/modelTypes' import type { ModelType } from '~/services/modelTypes'
@@ -407,7 +408,7 @@ const selectedTypeId = ref<string>('')
const editionForm = reactive({ const editionForm = reactive({
name: '' as string, name: '' as string,
reference: '' as string, reference: '' as string,
constructeurId: null as string | null, constructeurIds: [] as string[],
prix: '' as string, prix: '' as string,
}) })
@@ -602,7 +603,11 @@ watch(
editionForm.name = currentPiece.name || '' editionForm.name = currentPiece.name || ''
editionForm.reference = currentPiece.reference || '' editionForm.reference = currentPiece.reference || ''
editionForm.constructeurId = currentPiece.constructeur?.id || currentPiece.constructeurId || null editionForm.constructeurIds = uniqueConstructeurIds(
currentPiece,
Array.isArray(currentPiece.constructeurs) ? currentPiece.constructeurs : [],
currentPiece.constructeur ? [currentPiece.constructeur] : [],
)
editionForm.prix = currentPiece.prix !== null && currentPiece.prix !== undefined ? String(currentPiece.prix) : '' editionForm.prix = currentPiece.prix !== null && currentPiece.prix !== undefined ? String(currentPiece.prix) : ''
customFieldInputs.value = buildCustomFieldInputs( customFieldInputs.value = buildCustomFieldInputs(
@@ -642,7 +647,7 @@ const submitEdition = async () => {
const reference = editionForm.reference.trim() const reference = editionForm.reference.trim()
payload.reference = reference ? reference : null payload.reference = reference ? reference : null
payload.constructeurId = editionForm.constructeurId || null payload.constructeurIds = uniqueConstructeurIds(editionForm.constructeurIds)
if (rawPrice) { if (rawPrice) {
const parsed = Number(rawPrice) const parsed = Number(rawPrice)

View File

@@ -260,6 +260,7 @@ import { useToast } from '~/composables/useToast'
import { useCustomFields } from '~/composables/useCustomFields' import { useCustomFields } from '~/composables/useCustomFields'
import { useDocuments } from '~/composables/useDocuments' import { useDocuments } from '~/composables/useDocuments'
import { formatPieceStructurePreview } from '~/shared/modelUtils' import { formatPieceStructurePreview } from '~/shared/modelUtils'
import { uniqueConstructeurIds } from '~/shared/constructeurUtils'
import type { PieceModelStructure } from '~/shared/types/inventory' import type { PieceModelStructure } from '~/shared/types/inventory'
import type { ModelType } from '~/services/modelTypes' import type { ModelType } from '~/services/modelTypes'
@@ -283,7 +284,7 @@ const submitting = ref(false)
const creationForm = reactive({ const creationForm = reactive({
name: '' as string, name: '' as string,
reference: '' as string, reference: '' as string,
constructeurId: null as string | null, constructeurIds: [] as string[],
prix: '' as string, prix: '' as string,
}) })
@@ -381,7 +382,7 @@ const getStructureCustomFields = (structure: PieceModelStructure | null) => Arra
const clearCreationForm = () => { const clearCreationForm = () => {
creationForm.name = '' creationForm.name = ''
creationForm.reference = '' creationForm.reference = ''
creationForm.constructeurId = null creationForm.constructeurIds = []
creationForm.prix = '' creationForm.prix = ''
lastSuggestedName.value = '' lastSuggestedName.value = ''
} }
@@ -401,8 +402,8 @@ const submitCreation = async () => {
payload.reference = reference payload.reference = reference
} }
if (creationForm.constructeurId) { if (creationForm.constructeurIds.length) {
payload.constructeurId = creationForm.constructeurId payload.constructeurIds = uniqueConstructeurIds(creationForm.constructeurIds)
} }
const rawPrice = typeof creationForm.prix === 'string' const rawPrice = typeof creationForm.prix === 'string'

View File

@@ -0,0 +1,115 @@
export interface ConstructeurSummary {
id: string;
name?: string | null;
email?: string | null;
phone?: string | null;
}
const isObject = (value: unknown): value is Record<string, unknown> =>
Boolean(value) && typeof value === 'object' && !Array.isArray(value);
const toStringId = (value: unknown): string | null => {
if (typeof value !== 'string') {
return null;
}
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : null;
};
export const uniqueConstructeurIds = (...sources: unknown[]): string[] => {
const ids = new Set<string>();
const pushId = (value: unknown) => {
const id = toStringId(value);
if (id) {
ids.add(id);
}
};
const explore = (value: unknown): void => {
if (!value) {
return;
}
if (Array.isArray(value)) {
value.forEach(explore);
return;
}
if (typeof value === 'string') {
pushId(value);
return;
}
if (isObject(value)) {
if (Array.isArray(value.constructeurIds)) {
value.constructeurIds.forEach(pushId);
}
if (value.constructeurId) {
pushId(value.constructeurId);
}
if (Array.isArray(value.constructeurs)) {
value.constructeurs.forEach(explore);
}
if (value.constructeur) {
explore(value.constructeur);
}
if (typeof value.id === 'string') {
pushId(value.id);
}
return;
}
};
sources.forEach(explore);
return Array.from(ids);
};
export const resolveConstructeurs = (
ids: string[],
...candidatePools: Array<ConstructeurSummary[] | null | undefined>
): ConstructeurSummary[] => {
if (!Array.isArray(ids) || ids.length === 0) {
return [];
}
const index = new Map<string, ConstructeurSummary>();
const register = (pool?: ConstructeurSummary[] | null) => {
if (!Array.isArray(pool)) {
return;
}
pool.forEach((entry) => {
if (entry && typeof entry === 'object' && typeof entry.id === 'string') {
index.set(entry.id, entry);
}
});
};
candidatePools.forEach(register);
return ids
.map((id) => index.get(id))
.filter((item): item is ConstructeurSummary => Boolean(item))
.map((item) => ({ ...item }));
};
export const formatConstructeurContact = (
constructeur?: ConstructeurSummary | null,
): string =>
[constructeur?.email, constructeur?.phone].filter(Boolean).join(' • ');
export const buildConstructeurRequestPayload = <T extends Record<string, any>>(
payload: T,
): T & { constructeurIds: string[] } => {
const ids = uniqueConstructeurIds(
payload?.constructeurIds,
payload?.constructeurId,
payload?.constructeur,
payload?.constructeurs,
);
const next = { ...payload } as Record<string, any>;
next.constructeurIds = ids;
delete next.constructeurId;
delete next.constructeur;
delete next.constructeurs;
return next as T & { constructeurIds: string[] };
};

View File

@@ -11,6 +11,7 @@ import {
type PieceModelStructureForEditor, type PieceModelStructureForEditor,
createEmptyPieceModelStructure, createEmptyPieceModelStructure,
} from './types/inventory' } from './types/inventory'
import { uniqueConstructeurIds } from './constructeurUtils'
export const isPlainObject = (value: unknown): value is Record<string, unknown> => { export const isPlainObject = (value: unknown): value is Record<string, unknown> => {
return value !== null && typeof value === 'object' && !Array.isArray(value) return value !== null && typeof value === 'object' && !Array.isArray(value)
@@ -686,7 +687,7 @@ export const formatStructurePreview = (structure: any) => {
export interface DefinitionOverridePayload { export interface DefinitionOverridePayload {
name?: string name?: string
reference?: string reference?: string
constructeurId?: string | null constructeurIds?: string[]
prix?: number prix?: number
} }
@@ -711,8 +712,14 @@ export const sanitizeDefinitionOverrides = (definition: any): DefinitionOverride
} }
} }
if (definition.constructeurId !== undefined && definition.constructeurId !== null && definition.constructeurId !== '') { const constructeurIds = uniqueConstructeurIds(
payload.constructeurId = definition.constructeurId definition.constructeurIds,
definition.constructeurId,
definition.constructeur,
definition.constructeurs,
)
if (constructeurIds.length) {
payload.constructeurIds = constructeurIds
} }
if (definition.prix !== undefined && definition.prix !== null && definition.prix !== '') { if (definition.prix !== undefined && definition.prix !== null && definition.prix !== '') {

View File

@@ -1,3 +1,8 @@
import {
uniqueConstructeurIds,
resolveConstructeurs,
} from '~/shared/constructeurUtils'
const formatSize = (size) => { const formatSize = (size) => {
if (size === undefined || size === null) { return '—' } if (size === undefined || size === null) { return '—' }
if (size === 0) { return '0 B' } if (size === 0) { return '0 B' }
@@ -59,9 +64,12 @@ const renderPrintPieces = (
const cards = pieces const cards = pieces
.map((piece, idx) => { .map((piece, idx) => {
const indexLabel = piece.indexPath ? piece.indexPath.join('.') : `${idx + 1}` const indexLabel = piece.indexPath ? piece.indexPath.join('.') : `${idx + 1}`
const constructeurBadge = piece.constructeur?.name const constructeurBadges = (piece.constructeurs || [])
? `<span class="print-badge print-badge--subtle">Constructeur: ${piece.constructeur.name}</span>` .map((constructeur, badgeIdx) => {
: '' const suffix = piece.constructeurs.length > 1 ? ` ${badgeIdx + 1}` : ''
return `<span class="print-badge print-badge--subtle">Constructeur${suffix}: ${constructeur.name}</span>`
})
.join('')
const customFields = (piece.customFields || []) const customFields = (piece.customFields || [])
.filter(field => field.value && field.value !== '—' && field.value !== '') .filter(field => field.value && field.value !== '—' && field.value !== '')
@@ -93,17 +101,24 @@ const renderPrintPieces = (
<div class="print-piece-title">${piece.name}</div> <div class="print-piece-title">${piece.name}</div>
<div class="print-piece-subtitle">${piece.reference || 'Référence non définie'}</div> <div class="print-piece-subtitle">${piece.reference || 'Référence non définie'}</div>
</div> </div>
${constructeurBadge} ${constructeurBadges}
</div> </div>
${piece.description ? `<p class="print-piece-description">${piece.description}</p>` : ''} ${piece.description ? `<p class="print-piece-description">${piece.description}</p>` : ''}
<div class="print-piece-meta"> <div class="print-piece-meta">
<div class="print-field-mini"> <div class="print-field-mini">
<label>Constructeur</label> <label>Constructeur(s)</label>
<span>${piece.constructeur?.name || '—'}</span> <span>${piece.constructeurs?.length
? piece.constructeurs.map(constructeur => constructeur.name).join(', ')
: '—'}</span>
</div> </div>
<div class="print-field-mini"> <div class="print-field-mini">
<label>Contact</label> <label>Contact(s)</label>
<span>${piece.constructeur?.contact || '—'}</span> <span>${piece.constructeurs?.length
? piece.constructeurs
.map(constructeur => constructeur.contact)
.filter(Boolean)
.join(' • ') || '—'
: '—'}</span>
</div> </div>
</div> </div>
${customFieldsBlock} ${customFieldsBlock}
@@ -128,8 +143,12 @@ const renderPrintComponents = (components = [], depth = 0, indexPath = []) => {
return components return components
.map((component, idx) => { .map((component, idx) => {
const badges = [] const badges = []
if (component.constructeur?.name) { if (component.constructeurs?.length) {
badges.push(`Constructeur: ${component.constructeur.name}`) const label = component.constructeurs.map((constructeur, badgeIdx) => {
const suffix = component.constructeurs.length > 1 ? ` ${badgeIdx + 1}` : ''
return `Constructeur${suffix}: ${constructeur.name}`
})
badges.push(...label)
} }
const sectionClass = `print-section print-section--component print-section-depth-${Math.min(depth, 3)}` const sectionClass = `print-section print-section--component print-section-depth-${Math.min(depth, 3)}`
const currentIndex = [...indexPath, idx + 1] const currentIndex = [...indexPath, idx + 1]
@@ -184,32 +203,79 @@ const normalizeCustomFields = (values = []) => {
const normalizeConstructeur = (constructeur) => { const normalizeConstructeur = (constructeur) => {
if (!constructeur) { return null } if (!constructeur) { return null }
return { return {
id: constructeur.id || null,
name: constructeur.name || '—', name: constructeur.name || '—',
contact: [constructeur.email, constructeur.phone].filter(Boolean).join(' • ') || '—' contact: [constructeur.email, constructeur.phone].filter(Boolean).join(' • ') || '—'
} }
} }
const normalizePiece = piece => ({ const normalizeConstructeurList = (...sources) => {
id: piece.id, const ids = uniqueConstructeurIds(...sources)
name: piece.name || 'Pièce sans nom', const pools = sources
description: piece.description || '', .flatMap((source) => {
reference: piece.reference || '', if (Array.isArray(source)) {
customFields: normalizeCustomFields(piece.customFieldValues || []), if (source.length && typeof source[0] === 'object') {
documents: normalizeDocuments(piece.documents || []), return [source]
constructeur: normalizeConstructeur(piece.constructeur), }
indexPath: piece.indexPath || null return []
}) }
if (source && typeof source === 'object' && 'id' in source) {
return [[source]]
}
return []
})
.filter(Boolean)
const resolved = resolveConstructeurs(ids, ...pools)
return resolved
.map(normalizeConstructeur)
.filter(Boolean)
}
const normalizeComponent = component => ({ const normalizePiece = piece => {
id: component.id, const constructeurs = normalizeConstructeurList(
name: component.name || 'Composant sans nom', piece.constructeurs,
description: component.description || '', piece.constructeur,
customFields: normalizeCustomFields(component.customFieldValues || []), piece.originalPiece?.constructeurs,
documents: normalizeDocuments(component.documents || []), piece.originalPiece?.constructeur,
pieces: (component.pieces || []).map(normalizePiece), piece.constructeurIds,
subComponents: (component.sousComposants || component.subComponents || []).map(normalizeComponent), piece.constructeurId,
constructeur: normalizeConstructeur(component.constructeur) )
})
return {
id: piece.id,
name: piece.name || 'Pièce sans nom',
description: piece.description || '',
reference: piece.reference || '',
customFields: normalizeCustomFields(piece.customFieldValues || []),
documents: normalizeDocuments(piece.documents || []),
constructeurs,
constructeur: constructeurs[0] || null,
indexPath: piece.indexPath || null
}
}
const normalizeComponent = component => {
const constructeurs = normalizeConstructeurList(
component.constructeurs,
component.constructeur,
component.originalComposant?.constructeurs,
component.originalComposant?.constructeur,
component.constructeurIds,
component.constructeurId,
)
return {
id: component.id,
name: component.name || 'Composant sans nom',
description: component.description || '',
customFields: normalizeCustomFields(component.customFieldValues || []),
documents: normalizeDocuments(component.documents || []),
pieces: (component.pieces || []).map(normalizePiece),
subComponents: (component.sousComposants || component.subComponents || []).map(normalizeComponent),
constructeurs,
constructeur: constructeurs[0] || null,
}
}
export const buildMachinePrintContext = ({ export const buildMachinePrintContext = ({
machine, machine,
@@ -255,6 +321,24 @@ export const buildMachinePrintContext = ({
machineBadges.push(`Ref: ${machineReference}`) machineBadges.push(`Ref: ${machineReference}`)
} }
const machineConstructeurs = normalizeConstructeurList(
machine?.constructeurs,
machine?.constructeur,
machine?.constructeurIds,
machine?.constructeurId,
)
const machineConstructeurNames = machineConstructeurs.length
? machineConstructeurs.map((constructeur) => constructeur.name).join(', ')
: ''
const machineConstructeurContacts = machineConstructeurs.length
? machineConstructeurs
.map((constructeur) => constructeur.contact)
.filter(Boolean)
.join(' • ')
: ''
const normalizedPieces = machinePieces const normalizedPieces = machinePieces
.map(normalizePiece) .map(normalizePiece)
.filter(piece => isPieceSelected(piece.id)) .filter(piece => isPieceSelected(piece.id))
@@ -300,7 +384,10 @@ export const buildMachinePrintContext = ({
site: machine?.site?.name || '', site: machine?.site?.name || '',
category: machine?.typeMachine?.category || '', category: machine?.typeMachine?.category || '',
badges: machineBadges, badges: machineBadges,
constructeur: normalizeConstructeur(machine?.constructeur), constructeurs: machineConstructeurs,
constructeur: machineConstructeurs[0] || null,
constructeurNames: machineConstructeurNames,
constructeurContacts: machineConstructeurContacts,
includeInfo: includeMachineInfo, includeInfo: includeMachineInfo,
customFields: includeMachineCustomFields customFields: includeMachineCustomFields
? normalizeCustomFields(machine?.customFieldValues || []) ? normalizeCustomFields(machine?.customFieldValues || [])
@@ -342,11 +429,11 @@ export const buildMachinePrintHtml = (context, styles) => {
<div class="print-section print-section--machine"> <div class="print-section print-section--machine">
<h3>Informations générales</h3> <h3>Informations générales</h3>
<div class="print-grid"> <div class="print-grid">
${renderPrintField('Nom', context.machine.name)} ${renderPrintField('Nom', context.machine.name)}
${renderPrintField('Référence', context.machine.reference, 'Non définie')} ${renderPrintField('Référence', context.machine.reference, 'Non définie')}
${renderPrintField('Site', context.machine.site, 'Non défini')} ${renderPrintField('Site', context.machine.site, 'Non défini')}
${renderPrintField('Constructeur', context.machine.constructeur?.name, 'Non défini')} ${renderPrintField('Constructeur(s)', context.machine.constructeurNames, 'Non défini')}
${renderPrintField('Contact Constructeur', context.machine.constructeur?.contact, 'Non défini')} ${renderPrintField('Contact(s) Constructeur(s)', context.machine.constructeurContacts, 'Non défini')}
</div> </div>
</div> </div>
`) `)