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

View File

@@ -1,7 +1,7 @@
<template>
<div class="space-y-2 constructeur-select">
<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">
<input
v-model="searchTerm"
@@ -33,13 +33,17 @@
:key="option.id"
type="button"
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">
<span class="font-medium">{{ option.name }}</span>
<span class="text-xs text-gray-500">
{{ [option.email, option.phone].filter(Boolean).join(' • ') || '—' }}
</span>
<div class="flex items-center justify-between gap-3">
<div class="flex flex-col">
<span class="font-medium">{{ option.name }}</span>
<span class="text-xs text-gray-500">
{{ formatConstructeurContact(option) || '—' }}
</span>
</div>
<IconLucideCheck v-if="isSelected(option.id)" class="w-4 h-4 text-primary" aria-hidden="true" />
</div>
</button>
</div>
@@ -49,10 +53,25 @@
</button>
</div>
<div v-if="selectedConstructeur" class="text-xs text-gray-500">
<span class="font-medium">{{ selectedConstructeur.name }}</span>
<span v-if="selectedConstructeur.email"> {{ selectedConstructeur.email }}</span>
<span v-if="selectedConstructeur.phone"> {{ selectedConstructeur.phone }}</span>
<div class="flex flex-wrap gap-2 min-h-[1.5rem]">
<span v-if="!selectedConstructeurs.length" class="text-sm text-gray-500">
Aucun constructeur sélectionné
</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>
<dialog class="modal" :class="{ 'modal-open': openCreateModal }">
@@ -94,89 +113,131 @@
</div>
</template>
<script setup>
<script setup lang="ts">
import { ref, watch, computed, onMounted, onBeforeUnmount } from 'vue'
import type { PropType } from 'vue'
import FieldEmail from '~/components/form/FieldEmail.vue'
import FieldPhone from '~/components/form/FieldPhone.vue'
import { useConstructeurs } from '~/composables/useConstructeurs'
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({
modelValue: {
type: String,
default: null
type: Array as PropType<string[]>,
default: () => [],
},
label: {
type: String,
default: ''
default: '',
},
placeholder: {
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 searchTerm = ref('')
const openDropdown = ref(false)
const openCreateModal = ref(false)
const creating = ref(false)
const options = ref([])
let searchTimeout = null
const options = ref<ConstructeurSummary[]>([])
const selectedIds = ref<string[]>([])
let searchTimeout: ReturnType<typeof setTimeout> | null = null
let lastSearchTerm = ''
const applyOptions = (items = []) => {
const selectedId = props.modelValue
const cloned = [...items]
const limited = cloned.slice(0, 10)
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)
const uniqueOptions = (items: ConstructeurSummary[] = []) => {
const seen = new Map<string, ConstructeurSummary>()
items.forEach((item) => {
if (item && typeof item === 'object' && typeof item.id === 'string') {
seen.set(item.id, item)
}
}
})
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({
name: '',
email: '',
phone: ''
phone: '',
})
const selectedConstructeur = computed(() =>
constructeurs.value.find(item => item.id === props.modelValue) || null
)
const optionLookup = computed(() => {
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(
() => props.modelValue,
(newValue) => {
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 }
)
const selectedConstructeurs = computed<ConstructeurSummary[]>(() => {
if (!selectedIds.value.length) {
return []
}
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) {
applyOptions(constructeurs.value)
applyOptions(constructeurs.value as ConstructeurSummary[])
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)
if (result.success) {
applyOptions(result.data || [])
@@ -186,14 +247,18 @@ async function ensureOptionsLoaded (force = false) {
const onSearch = () => {
openDropdown.value = true
clearTimeout(searchTimeout)
if (searchTimeout) {
clearTimeout(searchTimeout)
}
searchTimeout = setTimeout(async () => {
if (!searchTerm.value && constructeurs.value.length) {
applyOptions(constructeurs.value)
applyOptions(constructeurs.value as ConstructeurSummary[])
lastSearchTerm = ''
return
}
if (searchTerm.value === lastSearchTerm) { return }
if (searchTerm.value === lastSearchTerm) {
return
}
const result = await searchConstructeurs(searchTerm.value)
if (result.success) {
applyOptions(result.data || [])
@@ -202,10 +267,18 @@ const onSearch = () => {
}, 250)
}
const selectOption = (option) => {
emit('update:modelValue', option.id)
openDropdown.value = false
searchTerm.value = option.name
const toggleOption = (option: ConstructeurSummary) => {
const ids = new Set(selectedIds.value)
if (ids.has(option.id)) {
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 = () => {
@@ -216,31 +289,24 @@ const closeCreateModal = () => {
const handleCreate = async () => {
creating.value = true
const payload = { ...createForm.value }
if (!payload.phone) { delete payload.phone }
if (!payload.email) { delete payload.email }
if (!payload.phone) {
delete payload.phone
}
if (!payload.email) {
delete payload.email
}
const result = await createConstructeur(payload)
creating.value = false
if (result.success) {
emit('update:modelValue', result.data.id)
searchTerm.value = result.data.name
emitSelection([...selectedIds.value, result.data.id])
searchTerm.value = ''
closeCreateModal()
await ensureOptionsLoaded(true)
}
}
watch(
constructeurs,
(list) => {
applyOptions(list || [])
if (!searchTerm.value) {
lastSearchTerm = ''
}
},
{ immediate: true }
)
const clickHandler = (event) => {
const element = event.target
const clickHandler = (event: Event) => {
const element = event.target as HTMLElement | null
if (element && element.closest) {
if (
element.closest('.menu') ||
@@ -254,6 +320,39 @@ const clickHandler = (event) => {
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(() => {
window.addEventListener('click', clickHandler)
ensureOptionsLoaded()
@@ -261,6 +360,24 @@ onMounted(() => {
onBeforeUnmount(() => {
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>

View File

@@ -68,22 +68,33 @@
</div>
<div>
<span class="font-medium">Constructeur:</span>
<span v-if="!isEditMode" class="ml-2">
<span class="font-medium">{{
piece.constructeur?.name || "Non défini"
}}</span>
<span v-if="piece.constructeur" class="block text-xs text-gray-500">
{{
[piece.constructeur?.email, piece.constructeur?.phone]
.filter(Boolean)
.join(" ")
}}
<div v-if="!isEditMode" class="ml-2">
<div v-if="pieceConstructeursDisplay.length" class="space-y-1">
<div
v-for="constructeur in pieceConstructeursDisplay"
:key="constructeur.id"
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>
<span v-else class="font-medium">
Non défini
</span>
</span>
</div>
<ConstructeurSelect
v-else
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"
/>
</div>
@@ -353,6 +364,7 @@
<script setup>
import { reactive, onMounted, watch, ref, computed } from "vue";
import ConstructeurSelect from "./ConstructeurSelect.vue";
import { useConstructeurs } from "~/composables/useConstructeurs";
import { useCustomFields } from "~/composables/useCustomFields";
import { useToast } from "~/composables/useToast";
import { useDocuments } from "~/composables/useDocuments";
@@ -361,6 +373,11 @@ import { canPreviewDocument, isImageDocument, isPdfDocument } from "~/utils/docu
import DocumentUpload from "~/components/DocumentUpload.vue";
import DocumentPreviewModal from "~/components/DocumentPreviewModal.vue";
import IconLucidePackage from "~icons/lucide/package";
import {
formatConstructeurContact as formatConstructeurContactSummary,
resolveConstructeurs,
uniqueConstructeurIds,
} from "~/shared/constructeurUtils";
const props = defineProps({
piece: {
@@ -716,8 +733,39 @@ const candidateCustomFields = computed(() => {
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) => {
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();
};
@@ -971,7 +1019,7 @@ const updatePiece = () => {
...props.piece,
...pieceData,
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 { useToast } from './useToast'
import { useApi } from './useApi'
import { buildConstructeurRequestPayload } from '~/shared/constructeurUtils'
const composants = ref([])
const loading = ref(false)
@@ -27,7 +28,7 @@ const loadComposants = async () => {
const createComposant = async (composantData) => {
loading.value = true
try {
const result = await post('/composants', composantData)
const result = await post('/composants', buildConstructeurRequestPayload(composantData))
if (result.success) {
composants.value.push(result.data)
const displayName = result.data?.name
@@ -48,7 +49,7 @@ const loadComposants = async () => {
const updateComposantData = async (id, composantData) => {
loading.value = true
try {
const result = await patch(`/composants/${id}`, composantData)
const result = await patch(`/composants/${id}`, buildConstructeurRequestPayload(composantData))
if (result.success) {
const updated = result.data
const index = composants.value.findIndex(comp => comp.id === id)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -260,6 +260,7 @@ import { useToast } from '~/composables/useToast'
import { useCustomFields } from '~/composables/useCustomFields'
import { useDocuments } from '~/composables/useDocuments'
import { formatPieceStructurePreview } from '~/shared/modelUtils'
import { uniqueConstructeurIds } from '~/shared/constructeurUtils'
import type { PieceModelStructure } from '~/shared/types/inventory'
import type { ModelType } from '~/services/modelTypes'
@@ -283,7 +284,7 @@ const submitting = ref(false)
const creationForm = reactive({
name: '' as string,
reference: '' as string,
constructeurId: null as string | null,
constructeurIds: [] as string[],
prix: '' as string,
})
@@ -381,7 +382,7 @@ const getStructureCustomFields = (structure: PieceModelStructure | null) => Arra
const clearCreationForm = () => {
creationForm.name = ''
creationForm.reference = ''
creationForm.constructeurId = null
creationForm.constructeurIds = []
creationForm.prix = ''
lastSuggestedName.value = ''
}
@@ -401,8 +402,8 @@ const submitCreation = async () => {
payload.reference = reference
}
if (creationForm.constructeurId) {
payload.constructeurId = creationForm.constructeurId
if (creationForm.constructeurIds.length) {
payload.constructeurIds = uniqueConstructeurIds(creationForm.constructeurIds)
}
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,
createEmptyPieceModelStructure,
} from './types/inventory'
import { uniqueConstructeurIds } from './constructeurUtils'
export const isPlainObject = (value: unknown): value is Record<string, unknown> => {
return value !== null && typeof value === 'object' && !Array.isArray(value)
@@ -686,7 +687,7 @@ export const formatStructurePreview = (structure: any) => {
export interface DefinitionOverridePayload {
name?: string
reference?: string
constructeurId?: string | null
constructeurIds?: string[]
prix?: number
}
@@ -711,8 +712,14 @@ export const sanitizeDefinitionOverrides = (definition: any): DefinitionOverride
}
}
if (definition.constructeurId !== undefined && definition.constructeurId !== null && definition.constructeurId !== '') {
payload.constructeurId = definition.constructeurId
const constructeurIds = uniqueConstructeurIds(
definition.constructeurIds,
definition.constructeurId,
definition.constructeur,
definition.constructeurs,
)
if (constructeurIds.length) {
payload.constructeurIds = constructeurIds
}
if (definition.prix !== undefined && definition.prix !== null && definition.prix !== '') {

View File

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