feat: gérer les constructeurs multiples
This commit is contained in:
@@ -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>
|
||||
`)
|
||||
|
||||
Reference in New Issue
Block a user