Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c1ce940c98 | |||
| c594a76d47 | |||
| 59bae8c5e6 | |||
| 477f77a6b5 | |||
| d6790dd37d |
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
parameters:
|
parameters:
|
||||||
app.version: '0.1.103'
|
app.version: '0.1.106'
|
||||||
|
|||||||
@@ -116,6 +116,81 @@
|
|||||||
"loading": "Chargement du fournisseur…",
|
"loading": "Chargement du fournisseur…",
|
||||||
"notFound": "Fournisseur introuvable.",
|
"notFound": "Fournisseur introuvable.",
|
||||||
"save": "Valider"
|
"save": "Valider"
|
||||||
|
},
|
||||||
|
"form": {
|
||||||
|
"title": "Ajouter un fournisseur",
|
||||||
|
"back": "Précédent",
|
||||||
|
"submit": "Valider",
|
||||||
|
"duplicateCompany": "Un fournisseur portant ce nom de société existe déjà.",
|
||||||
|
"main": {
|
||||||
|
"companyName": "Nom du fournisseur (Entreprise)",
|
||||||
|
"categories": "Catégorie"
|
||||||
|
},
|
||||||
|
"information": {
|
||||||
|
"description": "Description",
|
||||||
|
"competitors": "Concurrent",
|
||||||
|
"foundedAt": "Date de création",
|
||||||
|
"employeesCount": "Nombre de salariés",
|
||||||
|
"revenueAmount": "CA",
|
||||||
|
"profitAmount": "Résultat",
|
||||||
|
"directorName": "Dirigeant",
|
||||||
|
"volumeForecast": "Volume prévisionnel"
|
||||||
|
},
|
||||||
|
"contact": {
|
||||||
|
"title": "Contact {n}",
|
||||||
|
"lastName": "Nom",
|
||||||
|
"firstName": "Prénom",
|
||||||
|
"jobTitle": "Fonction",
|
||||||
|
"email": "Email",
|
||||||
|
"phonePrimary": "Téléphone",
|
||||||
|
"phoneSecondary": "Téléphone (2)",
|
||||||
|
"addPhone": "Ajouter un numéro",
|
||||||
|
"remove": "Supprimer le contact",
|
||||||
|
"add": "Nouveau contact"
|
||||||
|
},
|
||||||
|
"address": {
|
||||||
|
"title": "Adresse {n}",
|
||||||
|
"addressType": "Type d'adresse",
|
||||||
|
"addressTypeProspect": "Prospect",
|
||||||
|
"addressTypeDepart": "Départ",
|
||||||
|
"addressTypeRendu": "Rendu",
|
||||||
|
"categories": "Catégorie",
|
||||||
|
"country": "Pays",
|
||||||
|
"postalCode": "Code postal",
|
||||||
|
"city": "Ville",
|
||||||
|
"street": "Adresse",
|
||||||
|
"streetNotFound": "Adresse introuvable ? Saisissez-la directement.",
|
||||||
|
"streetComplement": "Adresse complémentaire",
|
||||||
|
"sites": "Sites",
|
||||||
|
"contacts": "Contact(s) rattaché(s)",
|
||||||
|
"bennes": "Benne(s)",
|
||||||
|
"triageProvider": "Prestation de triage",
|
||||||
|
"remove": "Supprimer l'adresse",
|
||||||
|
"add": "Nouvelle adresse",
|
||||||
|
"degraded": "Service d'adresse indisponible : saisie de la ville et de l'adresse en mode libre."
|
||||||
|
},
|
||||||
|
"accounting": {
|
||||||
|
"siren": "SIREN",
|
||||||
|
"accountNumber": "Numéro de compte",
|
||||||
|
"tvaMode": "Mode de TVA",
|
||||||
|
"nTva": "N° de TVA",
|
||||||
|
"paymentDelay": "Délai de règlement",
|
||||||
|
"paymentType": "Type de règlement",
|
||||||
|
"bank": "Banque",
|
||||||
|
"ribLabel": "Libellé",
|
||||||
|
"ribBic": "BIC",
|
||||||
|
"ribIban": "IBAN",
|
||||||
|
"addRib": "Ajouter un RIB",
|
||||||
|
"removeRib": "Supprimer le RIB"
|
||||||
|
},
|
||||||
|
"confirmDelete": {
|
||||||
|
"title": "Confirmer la suppression",
|
||||||
|
"contact": "Supprimer ce contact ?",
|
||||||
|
"address": "Supprimer cette adresse ?",
|
||||||
|
"rib": "Supprimer ce RIB ?",
|
||||||
|
"cancel": "Annuler",
|
||||||
|
"confirm": "Confirmer"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"clients": {
|
"clients": {
|
||||||
|
|||||||
@@ -0,0 +1,314 @@
|
|||||||
|
<template>
|
||||||
|
<div class="relative grid grid-cols-4 gap-x-[44px] gap-y-4 bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]">
|
||||||
|
<!-- Suppression : modal de confirmation cote parent. -->
|
||||||
|
<MalioButtonIcon
|
||||||
|
v-if="removable && !readonly"
|
||||||
|
icon="mdi:delete-outline"
|
||||||
|
variant="ghost"
|
||||||
|
button-class="absolute top-3 right-3"
|
||||||
|
v-bind="{ ariaLabel: t('commercial.suppliers.form.address.remove') }"
|
||||||
|
@click="$emit('remove')"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<!-- Type d'adresse : Prospect / Depart / Rendu (RG-2.09). Select en attendant
|
||||||
|
l'arbitrage metier (radio vs select) ; l'erreur 422 (propertyPath
|
||||||
|
`addressType`) s'affiche via la prop native :error de MalioSelect. -->
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="model.addressType"
|
||||||
|
:options="addressTypeOptions"
|
||||||
|
:label="t('commercial.suppliers.form.address.addressType')"
|
||||||
|
:readonly="readonly"
|
||||||
|
empty-option-label=""
|
||||||
|
:required="true"
|
||||||
|
:error="errors?.addressType"
|
||||||
|
@update:model-value="(v: string | number | null) => update('addressType', v === null ? null : (v as SupplierAddressType))"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<!-- Sites Starseed : multiselect a tags (>= 1 obligatoire, RG-2.06). -->
|
||||||
|
<MalioSelectCheckbox
|
||||||
|
:model-value="model.siteIris"
|
||||||
|
:options="siteOptions"
|
||||||
|
:label="t('commercial.suppliers.form.address.sites')"
|
||||||
|
:display-tag="true"
|
||||||
|
:readonly="readonly"
|
||||||
|
:required="true"
|
||||||
|
:error="errors?.sites"
|
||||||
|
@update:model-value="(v: (string | number)[]) => update('siteIris', v.map(String))"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<!-- Contacts rattaches (M2M, facultatif). -->
|
||||||
|
<MalioSelectCheckbox
|
||||||
|
:model-value="model.contactIris"
|
||||||
|
:options="contactOptions"
|
||||||
|
:label="t('commercial.suppliers.form.address.contacts')"
|
||||||
|
:display-tag="true"
|
||||||
|
:readonly="readonly"
|
||||||
|
@update:model-value="(v: (string | number)[]) => update('contactIris', v.map(String))"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<!-- Filler : aligne le debut de ligne suivant sur la grille (le bloc client
|
||||||
|
porte ici l'email de facturation, absent cote fournisseur). -->
|
||||||
|
<div aria-hidden="true" />
|
||||||
|
|
||||||
|
<!-- Categories de type FOURNISSEUR (>= 1 obligatoire, RG-2.10). -->
|
||||||
|
<MalioSelectCheckbox
|
||||||
|
:model-value="model.categoryIris"
|
||||||
|
:options="categoryOptions"
|
||||||
|
:label="t('commercial.suppliers.form.address.categories')"
|
||||||
|
:display-tag="true"
|
||||||
|
:readonly="readonly"
|
||||||
|
:required="true"
|
||||||
|
:error="errors?.categories"
|
||||||
|
@update:model-value="(v: (string | number)[]) => update('categoryIris', v.map(String))"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="model.country"
|
||||||
|
:options="countryOptions"
|
||||||
|
:label="t('commercial.suppliers.form.address.country')"
|
||||||
|
:readonly="readonly"
|
||||||
|
:required="true"
|
||||||
|
@update:model-value="(v: string | number | null) => update('country', String(v ?? 'France'))"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="model.postalCode"
|
||||||
|
:label="t('commercial.suppliers.form.address.postalCode')"
|
||||||
|
:mask="POSTAL_CODE_MASK"
|
||||||
|
:readonly="readonly"
|
||||||
|
:required="true"
|
||||||
|
:error="errors?.postalCode"
|
||||||
|
@update:model-value="onPostalCodeChange"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<!-- Ville : MalioSelect alimente par le code postal (BAN). Saisie libre si BAN indispo. -->
|
||||||
|
<MalioSelect
|
||||||
|
v-if="!degraded"
|
||||||
|
:model-value="model.city"
|
||||||
|
:options="cityOptions"
|
||||||
|
:label="t('commercial.suppliers.form.address.city')"
|
||||||
|
:readonly="readonly"
|
||||||
|
empty-option-label=""
|
||||||
|
:required="true"
|
||||||
|
:error="errors?.city"
|
||||||
|
@update:model-value="(v: string | number | null) => update('city', v === null ? null : String(v))"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-else
|
||||||
|
:model-value="model.city"
|
||||||
|
:label="t('commercial.suppliers.form.address.city')"
|
||||||
|
:readonly="readonly"
|
||||||
|
:required="true"
|
||||||
|
:error="errors?.city"
|
||||||
|
@update:model-value="(v: string) => update('city', v)"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<!-- Adresse (BAN) sur 2 colonnes + Adresse complementaire. allow-create : le
|
||||||
|
texte saisi est conserve si la BAN ne propose rien (saisie manuelle). -->
|
||||||
|
<div class="col-span-2">
|
||||||
|
<MalioInputAutocomplete
|
||||||
|
v-if="!readonly"
|
||||||
|
:model-value="model.street"
|
||||||
|
:options="addressOptions"
|
||||||
|
:loading="addressLoading"
|
||||||
|
:min-search-length="3"
|
||||||
|
:label="t('commercial.suppliers.form.address.street')"
|
||||||
|
:readonly="readonly"
|
||||||
|
:required="true"
|
||||||
|
:error="errors?.street"
|
||||||
|
:allow-create="true"
|
||||||
|
:no-results-text="t('commercial.suppliers.form.address.streetNotFound')"
|
||||||
|
@update:model-value="(v: string | number | null) => update('street', v === null ? null : String(v))"
|
||||||
|
@search="onAddressSearch"
|
||||||
|
@select="onAddressSelect"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-else
|
||||||
|
:model-value="model.street"
|
||||||
|
:label="t('commercial.suppliers.form.address.street')"
|
||||||
|
:readonly="readonly"
|
||||||
|
:required="true"
|
||||||
|
:error="errors?.street"
|
||||||
|
@update:model-value="(v: string) => update('street', v)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-span-1">
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="model.streetComplement"
|
||||||
|
:label="t('commercial.suppliers.form.address.streetComplement')"
|
||||||
|
:readonly="readonly"
|
||||||
|
:error="errors?.streetComplement"
|
||||||
|
@update:model-value="(v: string) => update('streetComplement', v)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Bennes : stepper (specifique fournisseur, defaut 0). -->
|
||||||
|
<MalioInputNumber
|
||||||
|
:model-value="model.bennes"
|
||||||
|
:label="t('commercial.suppliers.form.address.bennes')"
|
||||||
|
:min="0"
|
||||||
|
:readonly="readonly"
|
||||||
|
:error="errors?.bennes"
|
||||||
|
@update:model-value="(v: string) => update('bennes', v)"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<!-- Prestation de triage : booleen porte par l'adresse (specifique fournisseur). -->
|
||||||
|
<MalioCheckbox
|
||||||
|
id="address-triage-provider"
|
||||||
|
:label="t('commercial.suppliers.form.address.triageProvider')"
|
||||||
|
:model-value="model.triageProvider"
|
||||||
|
group-class="self-center"
|
||||||
|
:readonly="readonly"
|
||||||
|
@update:model-value="(v: boolean) => update('triageProvider', v)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { useAddressAutocomplete, type AddressSuggestion } from '~/shared/composables/useAddressAutocomplete'
|
||||||
|
import type { CategoryOption, RefOption } from '~/modules/commercial/composables/useSupplierReferentials'
|
||||||
|
import type { SupplierAddressFormDraft, SupplierAddressType } from '~/modules/commercial/types/supplierForm'
|
||||||
|
|
||||||
|
// Masque code postal FR : 5 chiffres.
|
||||||
|
const POSTAL_CODE_MASK = '#####'
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
/** Brouillon de l'adresse (v-model). */
|
||||||
|
modelValue: SupplierAddressFormDraft
|
||||||
|
title: string
|
||||||
|
/** Categories autorisees sur une adresse (type FOURNISSEUR). */
|
||||||
|
categoryOptions: CategoryOption[]
|
||||||
|
/** Sites Starseed disponibles. */
|
||||||
|
siteOptions: RefOption[]
|
||||||
|
/** Contacts deja saisis, rattachables a l'adresse. */
|
||||||
|
contactOptions: RefOption[]
|
||||||
|
/** Pays disponibles (France par defaut). */
|
||||||
|
countryOptions: RefOption[]
|
||||||
|
removable?: boolean
|
||||||
|
readonly?: boolean
|
||||||
|
/** Erreurs serveur 422 de cette ligne, indexees par champ (ERP-101). */
|
||||||
|
errors?: Record<string, string>
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
'update:modelValue': [value: SupplierAddressFormDraft]
|
||||||
|
'remove': []
|
||||||
|
/** Emis une fois quand le service d'autocompletion bascule en indisponible. */
|
||||||
|
'degraded': []
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const { t } = useI18n()
|
||||||
|
const autocomplete = useAddressAutocomplete()
|
||||||
|
|
||||||
|
const model = computed(() => props.modelValue)
|
||||||
|
|
||||||
|
const addressTypeOptions = computed<{ value: SupplierAddressType, label: string }[]>(() => [
|
||||||
|
{ value: 'PROSPECT', label: t('commercial.suppliers.form.address.addressTypeProspect') },
|
||||||
|
{ value: 'DEPART', label: t('commercial.suppliers.form.address.addressTypeDepart') },
|
||||||
|
{ value: 'RENDU', label: t('commercial.suppliers.form.address.addressTypeRendu') },
|
||||||
|
])
|
||||||
|
|
||||||
|
// Repli saisie libre de la VILLE quand la BAN est indisponible (recuperable).
|
||||||
|
const degraded = ref(false)
|
||||||
|
let unavailableNotified = false
|
||||||
|
const banCityOptions = ref<RefOption[]>([])
|
||||||
|
const banAddressOptions = ref<RefOption[]>([])
|
||||||
|
|
||||||
|
// Options ville effectives : on garantit que la ville courante figure toujours
|
||||||
|
// dans la liste, sinon MalioSelect afficherait un champ vide en lecture seule.
|
||||||
|
const cityOptions = computed<RefOption[]>(() => {
|
||||||
|
const current = props.modelValue.city
|
||||||
|
if (current && !banCityOptions.value.some(o => o.value === current)) {
|
||||||
|
return [{ value: current, label: current }, ...banCityOptions.value]
|
||||||
|
}
|
||||||
|
return banCityOptions.value
|
||||||
|
})
|
||||||
|
|
||||||
|
// Meme garantie pour le champ Adresse : la rue courante doit toujours figurer
|
||||||
|
// dans les options, sinon MalioInputAutocomplete laisse le champ vide.
|
||||||
|
const addressOptions = computed<RefOption[]>(() => {
|
||||||
|
const current = props.modelValue.street
|
||||||
|
if (current && !banAddressOptions.value.some(o => o.value === current)) {
|
||||||
|
return [{ value: current, label: current }, ...banAddressOptions.value]
|
||||||
|
}
|
||||||
|
return banAddressOptions.value
|
||||||
|
})
|
||||||
|
const addressLoading = ref(false)
|
||||||
|
// Conserve les suggestions d'adresse pour retrouver ville/CP au moment du select.
|
||||||
|
let lastAddressSuggestions: AddressSuggestion[] = []
|
||||||
|
|
||||||
|
/** Emet un nouveau brouillon avec le champ modifie (immutabilite). */
|
||||||
|
function update<K extends keyof SupplierAddressFormDraft>(field: K, value: SupplierAddressFormDraft[K]): void {
|
||||||
|
emit('update:modelValue', { ...props.modelValue, [field]: value })
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Previent le parent (toast unique) que l'autocompletion est indisponible. */
|
||||||
|
function notifyUnavailable(): void {
|
||||||
|
if (!unavailableNotified) {
|
||||||
|
unavailableNotified = true
|
||||||
|
emit('degraded')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Saisie du code postal → met a jour le champ + interroge la BAN pour la ville. */
|
||||||
|
async function onPostalCodeChange(value: string): Promise<void> {
|
||||||
|
update('postalCode', value)
|
||||||
|
|
||||||
|
const digits = (value ?? '').replace(/\D/g, '')
|
||||||
|
if (digits.length < 5) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const suggestions = await autocomplete.searchCity(digits)
|
||||||
|
banCityOptions.value = suggestions.map(s => ({ value: s.city, label: s.city }))
|
||||||
|
degraded.value = false
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
degraded.value = true
|
||||||
|
notifyUnavailable()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Recherche d'adresse assistee (event de MalioInputAutocomplete). */
|
||||||
|
async function onAddressSearch(query: string): Promise<void> {
|
||||||
|
// La BAN exige au moins 3 caracteres : on n'envoie rien en deca (evite un 400).
|
||||||
|
if (query.trim().length < 3) {
|
||||||
|
banAddressOptions.value = []
|
||||||
|
return
|
||||||
|
}
|
||||||
|
addressLoading.value = true
|
||||||
|
try {
|
||||||
|
const postalCode = (model.value.postalCode ?? '').replace(/\D/g, '') || undefined
|
||||||
|
const suggestions = await autocomplete.searchAddress(query, postalCode)
|
||||||
|
lastAddressSuggestions = suggestions
|
||||||
|
banAddressOptions.value = suggestions.map(s => ({ value: s.street, label: s.label }))
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
// Erreur transitoire : on vide les suggestions, la prochaine frappe reessaie.
|
||||||
|
banAddressOptions.value = []
|
||||||
|
notifyUnavailable()
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
addressLoading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Selection d'une suggestion d'adresse → remplit rue + ville + CP. */
|
||||||
|
function onAddressSelect(option: { label: string, value: string | number } | null): void {
|
||||||
|
if (option === null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const suggestion = lastAddressSuggestions.find(s => s.street === option.value)
|
||||||
|
if (!suggestion) {
|
||||||
|
update('street', String(option.value))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
emit('update:modelValue', {
|
||||||
|
...props.modelValue,
|
||||||
|
street: suggestion.street,
|
||||||
|
city: suggestion.city,
|
||||||
|
postalCode: suggestion.postalCode,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</script>
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
<template>
|
||||||
|
<div class="relative grid grid-cols-4 gap-x-[44px] gap-y-4 bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]">
|
||||||
|
<!-- Suppression : ouvre une modal de confirmation cote parent. Masquee si
|
||||||
|
non supprimable (1er bloc, RG-2.13) ou en lecture seule. -->
|
||||||
|
<MalioButtonIcon
|
||||||
|
v-if="removable && !readonly"
|
||||||
|
icon="mdi:delete-outline"
|
||||||
|
variant="ghost"
|
||||||
|
button-class="absolute top-3 right-3"
|
||||||
|
v-bind="{ ariaLabel: t('commercial.suppliers.form.contact.remove') }"
|
||||||
|
@click="$emit('remove')"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="model.lastName"
|
||||||
|
:label="t('commercial.suppliers.form.contact.lastName')"
|
||||||
|
:readonly="readonly"
|
||||||
|
:error="errors?.lastName"
|
||||||
|
@update:model-value="(v: string) => update('lastName', v)"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="model.firstName"
|
||||||
|
:label="t('commercial.suppliers.form.contact.firstName')"
|
||||||
|
:readonly="readonly"
|
||||||
|
:error="errors?.firstName"
|
||||||
|
@update:model-value="(v: string) => update('firstName', v)"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="model.jobTitle"
|
||||||
|
:label="t('commercial.suppliers.form.contact.jobTitle')"
|
||||||
|
:readonly="readonly"
|
||||||
|
:error="errors?.jobTitle"
|
||||||
|
@update:model-value="(v: string) => update('jobTitle', v)"
|
||||||
|
/>
|
||||||
|
<MalioInputEmail
|
||||||
|
:model-value="model.email"
|
||||||
|
:label="t('commercial.suppliers.form.contact.email')"
|
||||||
|
:readonly="readonly"
|
||||||
|
:lowercase="true"
|
||||||
|
:error="errors?.email"
|
||||||
|
@update:model-value="(v: string) => update('email', v)"
|
||||||
|
/>
|
||||||
|
<MalioInputPhone
|
||||||
|
:model-value="model.phonePrimary"
|
||||||
|
:label="t('commercial.suppliers.form.contact.phonePrimary')"
|
||||||
|
:mask="PHONE_MASK"
|
||||||
|
:readonly="readonly"
|
||||||
|
:error="errors?.phonePrimary"
|
||||||
|
:addable="!model.hasSecondaryPhone && !readonly"
|
||||||
|
:add-button-label="t('commercial.suppliers.form.contact.addPhone')"
|
||||||
|
@update:model-value="(v: string) => update('phonePrimary', v)"
|
||||||
|
@add="revealSecondaryPhone"
|
||||||
|
/>
|
||||||
|
<MalioInputPhone
|
||||||
|
v-if="model.hasSecondaryPhone"
|
||||||
|
:model-value="model.phoneSecondary"
|
||||||
|
:label="t('commercial.suppliers.form.contact.phoneSecondary')"
|
||||||
|
:mask="PHONE_MASK"
|
||||||
|
:readonly="readonly"
|
||||||
|
:error="errors?.phoneSecondary"
|
||||||
|
@update:model-value="(v: string) => update('phoneSecondary', v)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import type { SupplierContactFormDraft } from '~/modules/commercial/types/supplierForm'
|
||||||
|
|
||||||
|
// Masque telephone FR : 5 groupes de 2 chiffres (la normalisation finale reste serveur).
|
||||||
|
const PHONE_MASK = '## ## ## ## ##'
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
/** Brouillon du contact (v-model). */
|
||||||
|
modelValue: SupplierContactFormDraft
|
||||||
|
/** Titre du bloc (ex: « Contact 1 »). */
|
||||||
|
title: string
|
||||||
|
/** Affiche l'icone de suppression (1er bloc non supprimable, RG-2.13). */
|
||||||
|
removable?: boolean
|
||||||
|
/** Bloc en lecture seule (onglet valide). */
|
||||||
|
readonly?: boolean
|
||||||
|
/** Erreurs serveur 422 de cette ligne, indexees par champ (ERP-101). */
|
||||||
|
errors?: Record<string, string>
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
'update:modelValue': [value: SupplierContactFormDraft]
|
||||||
|
'remove': []
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const { t } = useI18n()
|
||||||
|
|
||||||
|
// Alias local pour la lisibilite du template.
|
||||||
|
const model = computed(() => props.modelValue)
|
||||||
|
|
||||||
|
/** Emet un nouveau brouillon avec le champ modifie (immutabilite). */
|
||||||
|
function update<K extends keyof SupplierContactFormDraft>(field: K, value: SupplierContactFormDraft[K]): void {
|
||||||
|
emit('update:modelValue', { ...props.modelValue, [field]: value })
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Revele le 2e numero (max 1 secondaire, le « + » disparait). */
|
||||||
|
function revealSecondaryPhone(): void {
|
||||||
|
emit('update:modelValue', { ...props.modelValue, hasSecondaryPhone: true })
|
||||||
|
}
|
||||||
|
</script>
|
||||||
@@ -0,0 +1,178 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||||
|
import { mount, flushPromises } from '@vue/test-utils'
|
||||||
|
import { defineComponent, h, ref, computed } from 'vue'
|
||||||
|
import { emptyAddress } from '~/modules/commercial/types/supplierForm'
|
||||||
|
import SupplierAddressBlock from '../SupplierAddressBlock.vue'
|
||||||
|
|
||||||
|
// Mocks controlables du composable BAN (hoisted).
|
||||||
|
const { searchCityMock, searchAddressMock } = vi.hoisted(() => ({
|
||||||
|
searchCityMock: vi.fn(),
|
||||||
|
searchAddressMock: vi.fn(),
|
||||||
|
}))
|
||||||
|
vi.mock('~/shared/composables/useAddressAutocomplete', () => ({
|
||||||
|
useAddressAutocomplete: () => ({
|
||||||
|
searchCity: searchCityMock,
|
||||||
|
searchAddress: searchAddressMock,
|
||||||
|
}),
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Auto-imports Nuxt/Vue utilises sans import explicite par le composant.
|
||||||
|
vi.stubGlobal('useI18n', () => ({ t: (key: string) => key }))
|
||||||
|
vi.stubGlobal('ref', ref)
|
||||||
|
vi.stubGlobal('computed', computed)
|
||||||
|
|
||||||
|
// Stub de MalioInputAutocomplete : expose les `value` des options + allowCreate.
|
||||||
|
const MalioInputAutocompleteStub = defineComponent({
|
||||||
|
name: 'MalioInputAutocomplete',
|
||||||
|
props: {
|
||||||
|
modelValue: { type: [String, Number, null], default: undefined },
|
||||||
|
options: { type: Array as () => { value: string | number, label: string }[], default: () => [] },
|
||||||
|
loading: { type: Boolean, default: false },
|
||||||
|
minSearchLength: { type: Number, default: 0 },
|
||||||
|
label: { type: String, default: '' },
|
||||||
|
readonly: { type: Boolean, default: false },
|
||||||
|
allowCreate: { type: Boolean, default: false },
|
||||||
|
},
|
||||||
|
emits: ['update:modelValue', 'search', 'select'],
|
||||||
|
setup(props) {
|
||||||
|
return () => h('div', {
|
||||||
|
'data-testid': 'addr-autocomplete',
|
||||||
|
'data-options': JSON.stringify(props.options.map(o => o.value)),
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
function mountBlock(overrides: Record<string, unknown> = {}, errors?: Record<string, string>) {
|
||||||
|
return mount(SupplierAddressBlock, {
|
||||||
|
props: {
|
||||||
|
modelValue: { ...emptyAddress(), ...overrides },
|
||||||
|
title: 'Adresse 1',
|
||||||
|
categoryOptions: [],
|
||||||
|
siteOptions: [],
|
||||||
|
contactOptions: [],
|
||||||
|
countryOptions: [],
|
||||||
|
...(errors ? { errors } : {}),
|
||||||
|
},
|
||||||
|
global: {
|
||||||
|
stubs: {
|
||||||
|
MalioButtonIcon: true,
|
||||||
|
MalioCheckbox: true,
|
||||||
|
MalioInputNumber: true,
|
||||||
|
MalioSelect: true,
|
||||||
|
MalioSelectCheckbox: true,
|
||||||
|
MalioInputText: true,
|
||||||
|
MalioInputAutocomplete: MalioInputAutocompleteStub,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('SupplierAddressBlock — specificites M2 (type, bennes, triage)', () => {
|
||||||
|
it('rend un select de type d\'adresse (en attendant l\'arbitrage metier)', () => {
|
||||||
|
const wrapper = mountBlock()
|
||||||
|
const addressTypeSelect = wrapper.findAll('malio-select-stub').find(
|
||||||
|
el => el.attributes('label') === 'commercial.suppliers.form.address.addressType',
|
||||||
|
)
|
||||||
|
expect(addressTypeSelect).toBeDefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rend le stepper Bennes et la case Prestation de triage (champs specifiques fournisseur)', () => {
|
||||||
|
const wrapper = mountBlock()
|
||||||
|
expect(wrapper.find('malio-input-number-stub').exists()).toBe(true)
|
||||||
|
expect(wrapper.find('malio-checkbox-stub').exists()).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('ne rend aucun champ d\'email de facturation (difference M1)', () => {
|
||||||
|
const wrapper = mountBlock()
|
||||||
|
// Aucun MalioInputEmail dans le bloc adresse fournisseur.
|
||||||
|
expect(wrapper.find('malio-input-email-stub').exists()).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('SupplierAddressBlock — mapping erreur par champ (ERP-101)', () => {
|
||||||
|
it('affiche l\'erreur serveur du type d\'adresse (propertyPath addressType) sur le select', () => {
|
||||||
|
const wrapper = mountBlock({}, { addressType: 'Le type d\'adresse est obligatoire.' })
|
||||||
|
const addressTypeSelect = wrapper.findAll('malio-select-stub').find(
|
||||||
|
el => el.attributes('label') === 'commercial.suppliers.form.address.addressType',
|
||||||
|
)
|
||||||
|
expect(addressTypeSelect?.attributes('error')).toBe('Le type d\'adresse est obligatoire.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('affiche les erreurs serveur sur sites et categories', () => {
|
||||||
|
const wrapper = mountBlock({}, {
|
||||||
|
sites: 'Au moins un site est obligatoire.',
|
||||||
|
categories: 'Au moins une catégorie est obligatoire.',
|
||||||
|
})
|
||||||
|
const checkboxes = wrapper.findAll('malio-select-checkbox-stub')
|
||||||
|
const sitesField = checkboxes.find(el => el.attributes('label') === 'commercial.suppliers.form.address.sites')
|
||||||
|
const categoriesField = checkboxes.find(el => el.attributes('label') === 'commercial.suppliers.form.address.categories')
|
||||||
|
|
||||||
|
expect(sitesField?.attributes('error')).toBe('Au moins un site est obligatoire.')
|
||||||
|
expect(categoriesField?.attributes('error')).toBe('Au moins une catégorie est obligatoire.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('affiche l\'erreur serveur sur le code postal', () => {
|
||||||
|
const wrapper = mountBlock({}, { postalCode: 'Code postal invalide.' })
|
||||||
|
const field = wrapper.findAll('malio-input-text-stub').find(
|
||||||
|
el => el.attributes('label') === 'commercial.suppliers.form.address.postalCode',
|
||||||
|
)
|
||||||
|
expect(field?.attributes('error')).toBe('Code postal invalide.')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('SupplierAddressBlock — autocompletion adresse (BAN) robuste', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
searchAddressMock.mockReset()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('n\'appelle pas la BAN en deca de 3 caracteres', async () => {
|
||||||
|
const wrapper = mountBlock()
|
||||||
|
wrapper.findComponent(MalioInputAutocompleteStub).vm.$emit('search', 'ab')
|
||||||
|
await flushPromises()
|
||||||
|
expect(searchAddressMock).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('relance la recherche apres une erreur (pas de bascule definitive)', async () => {
|
||||||
|
searchAddressMock
|
||||||
|
.mockRejectedValueOnce(new Error('BAN indisponible'))
|
||||||
|
.mockResolvedValueOnce([
|
||||||
|
{ label: '8 Boulevard du Port, Paris', street: '8 Boulevard du Port', postalCode: '75001', city: 'Paris' },
|
||||||
|
])
|
||||||
|
|
||||||
|
const wrapper = mountBlock()
|
||||||
|
const auto = wrapper.findComponent(MalioInputAutocompleteStub)
|
||||||
|
|
||||||
|
auto.vm.$emit('search', 'boulevard du port')
|
||||||
|
await flushPromises()
|
||||||
|
auto.vm.$emit('search', 'boulevard du porte')
|
||||||
|
await flushPromises()
|
||||||
|
|
||||||
|
expect(searchAddressMock).toHaveBeenCalledTimes(2)
|
||||||
|
expect(wrapper.find('[data-testid="addr-autocomplete"]').exists()).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('emet « degraded » une seule fois malgre plusieurs erreurs', async () => {
|
||||||
|
searchAddressMock.mockRejectedValue(new Error('BAN indisponible'))
|
||||||
|
|
||||||
|
const wrapper = mountBlock()
|
||||||
|
const auto = wrapper.findComponent(MalioInputAutocompleteStub)
|
||||||
|
|
||||||
|
auto.vm.$emit('search', 'rue de la paix')
|
||||||
|
await flushPromises()
|
||||||
|
auto.vm.$emit('search', 'rue de la paixx')
|
||||||
|
await flushPromises()
|
||||||
|
|
||||||
|
expect(wrapper.emitted('degraded')).toHaveLength(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('active allow-create sur le champ Adresse (saisie manuelle libre)', () => {
|
||||||
|
const wrapper = mountBlock()
|
||||||
|
expect(wrapper.findComponent(MalioInputAutocompleteStub).props('allowCreate')).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('inclut la rue courante dans les options meme sans recherche BAN', () => {
|
||||||
|
const wrapper = mountBlock({ street: '8 Boulevard du Port' })
|
||||||
|
const values = JSON.parse(wrapper.find('[data-testid="addr-autocomplete"]').attributes('data-options') ?? '[]')
|
||||||
|
expect(values).toContain('8 Boulevard du Port')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
import { describe, it, expect, vi } from 'vitest'
|
||||||
|
import { mount } from '@vue/test-utils'
|
||||||
|
import { defineComponent, h, ref, computed } from 'vue'
|
||||||
|
import { emptyContact } from '~/modules/commercial/types/supplierForm'
|
||||||
|
import SupplierContactBlock from '../SupplierContactBlock.vue'
|
||||||
|
|
||||||
|
// Auto-imports Nuxt/Vue utilises sans import explicite par le composant.
|
||||||
|
vi.stubGlobal('useI18n', () => ({ t: (key: string) => key }))
|
||||||
|
vi.stubGlobal('ref', ref)
|
||||||
|
vi.stubGlobal('computed', computed)
|
||||||
|
|
||||||
|
/** Stub d'un champ Malio qui re-expose la prop `error` recue dans un data-* attribut. */
|
||||||
|
function errorProbe(testid: string) {
|
||||||
|
return defineComponent({
|
||||||
|
name: `Probe-${testid}`,
|
||||||
|
props: {
|
||||||
|
modelValue: { type: [String, Number, null], default: undefined },
|
||||||
|
error: { type: String, default: '' },
|
||||||
|
label: { type: String, default: '' },
|
||||||
|
readonly: { type: Boolean, default: false },
|
||||||
|
},
|
||||||
|
setup(props) {
|
||||||
|
return () => h('div', { 'data-testid': testid, 'data-error': props.error })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function mountBlock(errors?: Record<string, string>) {
|
||||||
|
return mount(SupplierContactBlock, {
|
||||||
|
props: {
|
||||||
|
modelValue: emptyContact(),
|
||||||
|
title: 'Contact 1',
|
||||||
|
...(errors ? { errors } : {}),
|
||||||
|
},
|
||||||
|
global: {
|
||||||
|
stubs: {
|
||||||
|
MalioButtonIcon: true,
|
||||||
|
MalioInputPhone: true,
|
||||||
|
MalioInputText: errorProbe('contact-text'),
|
||||||
|
MalioInputEmail: errorProbe('contact-email'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('SupplierContactBlock — mapping erreur par champ (ERP-101)', () => {
|
||||||
|
it('affiche l\'erreur serveur sur le champ email via la prop errors', () => {
|
||||||
|
const wrapper = mountBlock({ email: 'Adresse e-mail invalide.' })
|
||||||
|
expect(wrapper.find('[data-testid="contact-email"]').attributes('data-error')).toBe('Adresse e-mail invalide.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('laisse les champs sans erreur quand errors est absent', () => {
|
||||||
|
const wrapper = mountBlock()
|
||||||
|
expect(wrapper.find('[data-testid="contact-email"]').attributes('data-error')).toBe('')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
|
// Mocks des composables auto-importes par Nuxt (indisponibles sous happy-dom).
|
||||||
|
const mockGet = vi.hoisted(() => vi.fn())
|
||||||
|
const mockPatch = vi.hoisted(() => vi.fn())
|
||||||
|
|
||||||
|
vi.stubGlobal('useApi', () => ({
|
||||||
|
get: mockGet,
|
||||||
|
post: vi.fn(),
|
||||||
|
put: vi.fn(),
|
||||||
|
patch: mockPatch,
|
||||||
|
delete: vi.fn(),
|
||||||
|
}))
|
||||||
|
|
||||||
|
const { useSupplier } = await import('../useSupplier')
|
||||||
|
|
||||||
|
const SAMPLE = { '@id': '/api/suppliers/85', id: 85, companyName: 'DOD59393F 862875', isArchived: false }
|
||||||
|
|
||||||
|
describe('useSupplier', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockGet.mockReset()
|
||||||
|
mockPatch.mockReset()
|
||||||
|
mockGet.mockResolvedValue(SAMPLE)
|
||||||
|
mockPatch.mockResolvedValue({ ...SAMPLE, isArchived: true })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('charge le detail via GET /suppliers/{id} en Hydra, sans toast', async () => {
|
||||||
|
const { supplier, load } = useSupplier(85)
|
||||||
|
await load()
|
||||||
|
|
||||||
|
expect(mockGet).toHaveBeenCalledWith(
|
||||||
|
'/suppliers/85',
|
||||||
|
{},
|
||||||
|
expect.objectContaining({
|
||||||
|
headers: { Accept: 'application/ld+json' },
|
||||||
|
toast: false,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
expect(supplier.value).toEqual(SAMPLE)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('bascule loading pendant le chargement et le retombe a false', async () => {
|
||||||
|
const { loading, load } = useSupplier(85)
|
||||||
|
const promise = load()
|
||||||
|
expect(loading.value).toBe(true)
|
||||||
|
await promise
|
||||||
|
expect(loading.value).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('marque error et laisse supplier null si le GET echoue (404...)', async () => {
|
||||||
|
mockGet.mockRejectedValueOnce(new Error('not found'))
|
||||||
|
const { supplier, error, load } = useSupplier(99)
|
||||||
|
await load()
|
||||||
|
expect(error.value).toBe(true)
|
||||||
|
expect(supplier.value).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('archive() PATCHe { isArchived: true } sans toast puis RECHARGE le detail complet', async () => {
|
||||||
|
// 1er GET = chargement initial, 2e GET = rechargement post-archivage.
|
||||||
|
mockGet.mockResolvedValueOnce(SAMPLE)
|
||||||
|
mockGet.mockResolvedValueOnce({ ...SAMPLE, isArchived: true })
|
||||||
|
const { supplier, load, archive } = useSupplier(85)
|
||||||
|
await load()
|
||||||
|
await archive()
|
||||||
|
|
||||||
|
expect(mockPatch).toHaveBeenCalledWith(
|
||||||
|
'/suppliers/85',
|
||||||
|
{ isArchived: true },
|
||||||
|
expect.objectContaining({ toast: false }),
|
||||||
|
)
|
||||||
|
// Le detail est re-fetch (le PATCH ne renvoie pas l'embed complet).
|
||||||
|
expect(mockGet).toHaveBeenCalledTimes(2)
|
||||||
|
expect(supplier.value?.isArchived).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('restore() PATCHe { isArchived: false } (payload isArchived SEUL)', async () => {
|
||||||
|
const { load, restore } = useSupplier(85)
|
||||||
|
await load()
|
||||||
|
await restore()
|
||||||
|
|
||||||
|
expect(mockPatch).toHaveBeenCalledWith(
|
||||||
|
'/suppliers/85',
|
||||||
|
{ isArchived: false },
|
||||||
|
expect.objectContaining({ toast: false }),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('propage l\'erreur (ex: 403 sans permission archive, 409 conflit homonyme) au lieu de l\'avaler', async () => {
|
||||||
|
const forbidden = { response: { status: 403 } }
|
||||||
|
mockPatch.mockRejectedValueOnce(forbidden)
|
||||||
|
const { load, archive } = useSupplier(85)
|
||||||
|
await load()
|
||||||
|
await expect(archive()).rejects.toBe(forbidden)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||||
|
|
||||||
|
// `useApi` est un auto-import Nuxt : on le stubbe globalement pour intercepter
|
||||||
|
// les appels de chargement des referentiels et controler les reponses Hydra.
|
||||||
|
const mockGet = vi.hoisted(() => vi.fn())
|
||||||
|
vi.stubGlobal('useApi', () => ({ get: mockGet }))
|
||||||
|
|
||||||
|
const { useSupplierReferentials } = await import('../useSupplierReferentials')
|
||||||
|
|
||||||
|
describe('useSupplierReferentials', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockGet.mockReset()
|
||||||
|
mockGet.mockResolvedValue({ member: [] })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('charge les categories filtrees sur le type FOURNISSEUR (RG-2.10)', async () => {
|
||||||
|
await useSupplierReferentials().loadCommon()
|
||||||
|
|
||||||
|
expect(mockGet).toHaveBeenCalledWith(
|
||||||
|
'/categories',
|
||||||
|
expect.objectContaining({ pagination: 'false', typeCode: 'FOURNISSEUR' }),
|
||||||
|
expect.objectContaining({ toast: false }),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('mappe les categories en options { value: IRI, label: name, code }', async () => {
|
||||||
|
mockGet.mockImplementation((url: string) => {
|
||||||
|
if (url === '/categories') {
|
||||||
|
return Promise.resolve({ member: [{ '@id': '/api/categories/9', code: 'NEGOCIANT', name: 'Négociant' }] })
|
||||||
|
}
|
||||||
|
return Promise.resolve({ member: [] })
|
||||||
|
})
|
||||||
|
|
||||||
|
const refs = useSupplierReferentials()
|
||||||
|
await refs.loadCommon()
|
||||||
|
|
||||||
|
expect(refs.categories.value).toEqual([{ value: '/api/categories/9', label: 'Négociant', code: 'NEGOCIANT' }])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('ne charge ni distributeurs ni courtiers (absents du modele fournisseur)', async () => {
|
||||||
|
await useSupplierReferentials().loadCommon()
|
||||||
|
|
||||||
|
const urls = mockGet.mock.calls.map(c => c[0])
|
||||||
|
expect(urls).not.toContain('/clients')
|
||||||
|
expect(urls).toEqual(
|
||||||
|
expect.arrayContaining(['/categories', '/sites', '/tva_modes', '/payment_delays', '/payment_types', '/banks']),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('reste resilient : un referentiel en echec n\'empeche pas les autres', async () => {
|
||||||
|
mockGet.mockImplementation((url: string) => {
|
||||||
|
if (url === '/categories') return Promise.reject(new Error('403'))
|
||||||
|
if (url === '/banks') return Promise.resolve({ member: [{ '@id': '/api/banks/1', code: 'SG', label: 'Société Générale' }] })
|
||||||
|
return Promise.resolve({ member: [] })
|
||||||
|
})
|
||||||
|
|
||||||
|
const refs = useSupplierReferentials()
|
||||||
|
await refs.loadCommon()
|
||||||
|
|
||||||
|
expect(refs.categories.value).toEqual([])
|
||||||
|
expect(refs.banks.value).toEqual([{ value: '/api/banks/1', label: 'Société Générale' }])
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
import { ref } from 'vue'
|
||||||
|
import type { SupplierDetail } from '~/modules/commercial/utils/supplierConsultation'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chargement et actions d'archivage d'un fournisseur unique (ecran « Consultation
|
||||||
|
* fournisseur », ERP-95). Miroir de `useClient` (M1). Lit le detail embarque via
|
||||||
|
* `GET /api/suppliers/{id}` (contacts / adresses / ribs sous `supplier:item:read` /
|
||||||
|
* `supplier:read:accounting`) et expose les bascules d'archivage (PATCH `isArchived`
|
||||||
|
* SEUL — tout autre champ => 422).
|
||||||
|
*
|
||||||
|
* L'en-tete `Accept: application/ld+json` est impose pour obtenir le payload
|
||||||
|
* Hydra complet (sans lui, API Platform 4 renvoie une representation reduite).
|
||||||
|
*
|
||||||
|
* Etat 100 % local a l'instance (refs) — aucune persistance URL. Les erreurs
|
||||||
|
* d'archivage/restauration (notamment le 409 d'homonyme actif a la restauration)
|
||||||
|
* sont PROPAGEES a l'appelant, qui decide du toast a afficher.
|
||||||
|
*/
|
||||||
|
export function useSupplier(id: number | string) {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const supplier = ref<SupplierDetail | null>(null)
|
||||||
|
const loading = ref(false)
|
||||||
|
const error = ref(false)
|
||||||
|
|
||||||
|
/** Recupere le detail complet (embed contacts/adresses/ribs + comptabilite). */
|
||||||
|
function fetchDetail(): Promise<SupplierDetail> {
|
||||||
|
return api.get<SupplierDetail>(
|
||||||
|
`/suppliers/${id}`,
|
||||||
|
{},
|
||||||
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Charge le detail du fournisseur. En cas d'echec : `error = true`, `supplier = null`. */
|
||||||
|
async function load(): Promise<void> {
|
||||||
|
loading.value = true
|
||||||
|
error.value = false
|
||||||
|
try {
|
||||||
|
supplier.value = await fetchDetail()
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
error.value = true
|
||||||
|
supplier.value = null
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bascule l'archivage (PATCH `isArchived` SEUL — tout autre champ => 422),
|
||||||
|
* puis RECHARGE le detail complet : la reponse du PATCH ne porte que le groupe
|
||||||
|
* `supplier:read` (ni l'embed contacts/adresses/ribs ni les libelles des
|
||||||
|
* referentiels comptables), un simple merge laisserait l'affichage incoherent.
|
||||||
|
* Toute erreur (notamment le 409 d'homonyme actif a la restauration) est
|
||||||
|
* propagee a l'appelant AVANT le rechargement.
|
||||||
|
*/
|
||||||
|
async function setArchived(isArchived: boolean): Promise<void> {
|
||||||
|
await api.patch(`/suppliers/${id}`, { isArchived }, { toast: false })
|
||||||
|
supplier.value = await fetchDetail()
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
supplier,
|
||||||
|
loading,
|
||||||
|
error,
|
||||||
|
load,
|
||||||
|
archive: () => setArchived(true),
|
||||||
|
restore: () => setArchived(false),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
/**
|
||||||
|
* Composable d'erreurs partage des ecrans fournisseur (creation + edition, M2
|
||||||
|
* Commercial). Miroir de `useClientFormErrors` (M1) :
|
||||||
|
* - un `useFormErrors` par groupe scalaire (Principal / Information /
|
||||||
|
* Comptabilite) : violations 422 affichees inline sous chaque champ ;
|
||||||
|
* - un tableau d'erreurs PAR LIGNE pour chaque collection (contacts /
|
||||||
|
* adresses / RIB), aligne sur l'index du `v-for`.
|
||||||
|
*
|
||||||
|
* `mapRowError` ne toaste PAS lui-meme : il retourne un booleen (true = mappe
|
||||||
|
* inline). Chaque page conserve ainsi son propre fallback dans le `catch`.
|
||||||
|
*/
|
||||||
|
import { ref, type Ref } from 'vue'
|
||||||
|
import { mapViolationsToRecord } from '~/shared/utils/api'
|
||||||
|
|
||||||
|
export function useSupplierFormErrors() {
|
||||||
|
const mainErrors = useFormErrors()
|
||||||
|
const informationErrors = useFormErrors()
|
||||||
|
const accountingErrors = useFormErrors()
|
||||||
|
const contactErrors = ref<Record<string, string>[]>([])
|
||||||
|
const addressErrors = ref<Record<string, string>[]>([])
|
||||||
|
const ribErrors = ref<Record<string, string>[]>([])
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mappe l'erreur d'une ligne de collection sur le tableau cible (par index).
|
||||||
|
* 422 avec violations exploitables → erreurs inline sous les champs de la
|
||||||
|
* ligne + retourne true. Sinon → ne touche pas la cible et retourne false.
|
||||||
|
*/
|
||||||
|
function mapRowError(
|
||||||
|
error: unknown,
|
||||||
|
target: Ref<Record<string, string>[]>,
|
||||||
|
index: number,
|
||||||
|
): boolean {
|
||||||
|
const response = (error as { response?: { status?: number, _data?: unknown } })?.response
|
||||||
|
const mapped = response?.status === 422 ? mapViolationsToRecord(response._data) : {}
|
||||||
|
if (Object.keys(mapped).length > 0) {
|
||||||
|
target.value[index] = mapped
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Soumet TOUS les blocs d'une collection (contacts / adresses / RIB) en
|
||||||
|
* collectant les erreurs par index : on n'arrete PAS au premier bloc en echec
|
||||||
|
* (decision ERP-110 / ERP-101). Reinitialise le tableau d'erreurs cible, tente
|
||||||
|
* chaque ligne via `saveRow`, mappe les 422 inline (mapRowError) ou delegue le
|
||||||
|
* fallback a `onUnmappedError`. `shouldSkip` permet d'ignorer les blocs vides.
|
||||||
|
* Retourne true si au moins un bloc a echoue.
|
||||||
|
*/
|
||||||
|
async function submitRows<T>(
|
||||||
|
rows: T[],
|
||||||
|
target: Ref<Record<string, string>[]>,
|
||||||
|
saveRow: (row: T, index: number) => Promise<void>,
|
||||||
|
onUnmappedError: (error: unknown, index: number) => void,
|
||||||
|
shouldSkip?: (row: T, index: number) => boolean,
|
||||||
|
): Promise<boolean> {
|
||||||
|
target.value = []
|
||||||
|
let hasError = false
|
||||||
|
for (let index = 0; index < rows.length; index++) {
|
||||||
|
const row = rows[index] as T
|
||||||
|
if (shouldSkip?.(row, index)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await saveRow(row, index)
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
if (!mapRowError(error, target, index)) {
|
||||||
|
onUnmappedError(error, index)
|
||||||
|
}
|
||||||
|
hasError = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasError
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
mainErrors,
|
||||||
|
informationErrors,
|
||||||
|
accountingErrors,
|
||||||
|
contactErrors,
|
||||||
|
addressErrors,
|
||||||
|
ribErrors,
|
||||||
|
mapRowError,
|
||||||
|
submitRows,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Charge les referentiels (listes courtes) alimentant les selects de l'ecran
|
||||||
|
* « Ajouter un fournisseur » : categories (type FOURNISSEUR), sites, modes de TVA,
|
||||||
|
* delais et types de reglement, banques. Miroir de `useClientReferentials` (M1).
|
||||||
|
*
|
||||||
|
* Toutes les collections sont recuperees en entier via l'echappatoire prevue
|
||||||
|
* `?pagination=false` (referentiels de quelques dizaines d'entrees max), avec
|
||||||
|
* l'en-tete `Accept: application/ld+json` impose par API Platform 4 pour obtenir
|
||||||
|
* l'enveloppe Hydra (`member`). Les valeurs d'option sont les IRI Hydra (`@id`)
|
||||||
|
* renvoyees telles quelles dans les payloads POST/PATCH (relations M:1 / M:N).
|
||||||
|
*
|
||||||
|
* Difference M2 : pas de distributeurs/courtiers (absents du modele fournisseur).
|
||||||
|
*
|
||||||
|
* Etat 100 % local a l'instance (refs) — aucune persistance URL.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** Option generique au format attendu par MalioSelect / MalioSelectCheckbox. */
|
||||||
|
export interface RefOption {
|
||||||
|
value: string
|
||||||
|
label: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Option de type de reglement enrichie de son code stable (RG-2.07 / RG-2.08). */
|
||||||
|
export interface PaymentTypeOption extends RefOption {
|
||||||
|
code: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Option de categorie enrichie de son code stable. */
|
||||||
|
export interface CategoryOption extends RefOption {
|
||||||
|
code: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface HydraMember {
|
||||||
|
'@id': string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CategoryMember extends HydraMember {
|
||||||
|
code: string
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SiteMember extends HydraMember {
|
||||||
|
name: string
|
||||||
|
postalCode: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ReferentialMember extends HydraMember {
|
||||||
|
code: string
|
||||||
|
label: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const LD_JSON_HEADERS = { Accept: 'application/ld+json' }
|
||||||
|
|
||||||
|
export function useSupplierReferentials() {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const categories = ref<CategoryOption[]>([])
|
||||||
|
const sites = ref<RefOption[]>([])
|
||||||
|
const tvaModes = ref<RefOption[]>([])
|
||||||
|
const paymentDelays = ref<RefOption[]>([])
|
||||||
|
const paymentTypes = ref<PaymentTypeOption[]>([])
|
||||||
|
const banks = ref<RefOption[]>([])
|
||||||
|
|
||||||
|
/** Recupere une collection complete (pagination desactivee) en Hydra. */
|
||||||
|
async function fetchAll<T extends HydraMember>(
|
||||||
|
url: string,
|
||||||
|
query: Record<string, string | string[]> = {},
|
||||||
|
): Promise<T[]> {
|
||||||
|
const res = await api.get<{ member?: T[] }>(
|
||||||
|
url,
|
||||||
|
{ pagination: 'false', ...query },
|
||||||
|
{ headers: LD_JSON_HEADERS, toast: false },
|
||||||
|
)
|
||||||
|
return res.member ?? []
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Charge en parallele les referentiels communs.
|
||||||
|
*
|
||||||
|
* Chargement RESILIENT (Promise.allSettled) : chaque referentiel est isole.
|
||||||
|
* Necessaire pour les roles metier qui n'ont pas toutes les permissions de
|
||||||
|
* lecture (ex. Compta a `commercial.suppliers.view` mais pas forcement
|
||||||
|
* `catalog.categories.view` ni `sites.view`). Un referentiel en echec reste
|
||||||
|
* simplement vide.
|
||||||
|
*/
|
||||||
|
async function loadCommon(): Promise<void> {
|
||||||
|
await Promise.allSettled([
|
||||||
|
// Taxonomie multi-types (ERP-84) : un fournisseur ne porte que des
|
||||||
|
// categories de type FOURNISSEUR (RG-2.10) -> on filtre cote API.
|
||||||
|
fetchAll<CategoryMember>('/categories', { typeCode: 'FOURNISSEUR' })
|
||||||
|
.then((cats) => { categories.value = cats.map(c => ({ value: c['@id'], label: c.name, code: c.code })) }),
|
||||||
|
fetchAll<SiteMember>('/sites')
|
||||||
|
// Libelle = numero de departement (2 premiers chiffres du code
|
||||||
|
// postal du site), ex: 86100 -> « 86 ».
|
||||||
|
.then((sitesList) => { sites.value = sitesList.map(s => ({ value: s['@id'], label: (s.postalCode ?? '').slice(0, 2) })) }),
|
||||||
|
fetchAll<ReferentialMember>('/tva_modes')
|
||||||
|
.then((tva) => { tvaModes.value = tva.map(t => ({ value: t['@id'], label: t.label })) }),
|
||||||
|
fetchAll<ReferentialMember>('/payment_delays')
|
||||||
|
.then((delays) => { paymentDelays.value = delays.map(d => ({ value: d['@id'], label: d.label })) }),
|
||||||
|
fetchAll<ReferentialMember>('/payment_types')
|
||||||
|
.then((types) => { paymentTypes.value = types.map(t => ({ value: t['@id'], label: t.label, code: t.code })) }),
|
||||||
|
fetchAll<ReferentialMember>('/banks')
|
||||||
|
.then((banksList) => { banks.value = banksList.map(b => ({ value: b['@id'], label: b.label })) }),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
categories,
|
||||||
|
sites,
|
||||||
|
tvaModes,
|
||||||
|
paymentDelays,
|
||||||
|
paymentTypes,
|
||||||
|
banks,
|
||||||
|
loadCommon,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -303,7 +303,7 @@
|
|||||||
class="relative bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]"
|
class="relative bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]"
|
||||||
>
|
>
|
||||||
<MalioButtonIcon
|
<MalioButtonIcon
|
||||||
v-if="!accountingReadonly"
|
v-if="!accountingReadonly && visibleRibs.length > 1"
|
||||||
icon="mdi:delete-outline"
|
icon="mdi:delete-outline"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
button-class="absolute top-3 right-3"
|
button-class="absolute top-3 right-3"
|
||||||
@@ -689,7 +689,7 @@ async function submitMain(): Promise<void> {
|
|||||||
mainSubmitting.value = true
|
mainSubmitting.value = true
|
||||||
mainErrors.clearErrors()
|
mainErrors.clearErrors()
|
||||||
try {
|
try {
|
||||||
const updated = await api.patch<ClientDetail>(`/clients/${clientId}`, buildMainPayload(main), {
|
const updated = await api.patch<ClientDetail>(`/clients/${clientId}`, buildMainPayload(main, { forUpdate: true }), {
|
||||||
headers: { Accept: 'application/ld+json' },
|
headers: { Accept: 'application/ld+json' },
|
||||||
toast: false,
|
toast: false,
|
||||||
})
|
})
|
||||||
@@ -859,7 +859,10 @@ async function submitAddresses(): Promise<void> {
|
|||||||
addresses.value,
|
addresses.value,
|
||||||
addressErrors,
|
addressErrors,
|
||||||
async (address) => {
|
async (address) => {
|
||||||
const body = buildAddressPayload(address, isBillingEmailRequired(address))
|
// Edition d'une adresse existante : champ requis vide envoye en `''`
|
||||||
|
// (NotBlank 422) au lieu d'etre omis — sinon le PATCH garderait
|
||||||
|
// l'ancienne valeur (faux 200). Creation (id null) : omit classique.
|
||||||
|
const body = buildAddressPayload(address, isBillingEmailRequired(address), { forUpdate: address.id !== null })
|
||||||
if (address.id === null) {
|
if (address.id === null) {
|
||||||
const created = await api.post<{ id: number }>(
|
const created = await api.post<{ id: number }>(
|
||||||
`/clients/${clientId}/addresses`,
|
`/clients/${clientId}/addresses`,
|
||||||
@@ -950,13 +953,18 @@ async function submitAccounting(): Promise<void> {
|
|||||||
try {
|
try {
|
||||||
// 1) POST/PATCH des RIB d'abord (erreurs inline par ligne, tous les blocs
|
// 1) POST/PATCH des RIB d'abord (erreurs inline par ligne, tous les blocs
|
||||||
// tentes). Le back exige >=1 RIB persiste pour valider une LCR a l'etape 2.
|
// tentes). Le back exige >=1 RIB persiste pour valider une LCR a l'etape 2.
|
||||||
// Seuls les blocs RIB TOTALEMENT vides sont ignores : un RIB partiel (ex.
|
// On ne saute une amorce neuve vide QUE s'il reste un autre RIB soumettable :
|
||||||
// IBAN seul) est soumis -> 422 NotBlank (label / bic / iban) inline.
|
// sinon (ex. l'unique RIB existant supprime, remplace par un bloc vide), on la
|
||||||
|
// soumet pour declencher la 422 NotBlank inline plutot que de laisser le DELETE
|
||||||
|
// echouer en « dernier RIB d'une LCR » (message plat sans propertyPath).
|
||||||
|
const hasSubmittableRib = ribs.value.some(r => r.id !== null || !isRibBlank(r))
|
||||||
const ribHasError = await submitRows(
|
const ribHasError = await submitRows(
|
||||||
ribs.value,
|
ribs.value,
|
||||||
ribErrors,
|
ribErrors,
|
||||||
async (rib) => {
|
async (rib) => {
|
||||||
const body = buildRibPayload(rib)
|
// Edition d'un RIB existant : champ requis vide envoye en `''` (NotBlank
|
||||||
|
// 422) au lieu d'etre omis (sinon le PATCH garderait l'ancienne valeur).
|
||||||
|
const body = buildRibPayload(rib, { forUpdate: rib.id !== null })
|
||||||
if (rib.id === null) {
|
if (rib.id === null) {
|
||||||
const created = await api.post<{ id: number }>(
|
const created = await api.post<{ id: number }>(
|
||||||
`/clients/${clientId}/ribs`,
|
`/clients/${clientId}/ribs`,
|
||||||
@@ -970,10 +978,10 @@ async function submitAccounting(): Promise<void> {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
error => showError(error),
|
error => showError(error),
|
||||||
// On ne saute QUE les amorces neuves (id null) totalement vides. Un
|
// On ne saute une amorce neuve (id null) totalement vide que si un autre RIB
|
||||||
// RIB existant vide est soumis -> 422 NotBlank inline (sinon la modif
|
// est soumettable. Un RIB existant vide est toujours soumis -> 422 NotBlank
|
||||||
// serait perdue en silence avec un faux toast de succes).
|
// inline (sinon la modif serait perdue en silence avec un faux toast succes).
|
||||||
rib => rib.id === null && isRibBlank(rib),
|
rib => hasSubmittableRib && rib.id === null && isRibBlank(rib),
|
||||||
)
|
)
|
||||||
if (ribHasError) return
|
if (ribHasError) return
|
||||||
|
|
||||||
|
|||||||
@@ -302,7 +302,7 @@
|
|||||||
>
|
>
|
||||||
<!-- ariaLabel via v-bind objet (prop camelCase ; aria-* serait un attribut HTML). -->
|
<!-- ariaLabel via v-bind objet (prop camelCase ; aria-* serait un attribut HTML). -->
|
||||||
<MalioButtonIcon
|
<MalioButtonIcon
|
||||||
v-if="!accountingReadonly"
|
v-if="!accountingReadonly && visibleRibs.length > 1"
|
||||||
icon="mdi:delete-outline"
|
icon="mdi:delete-outline"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
button-class="absolute top-3 right-3"
|
button-class="absolute top-3 right-3"
|
||||||
@@ -920,8 +920,9 @@ async function submitAccounting(): Promise<void> {
|
|||||||
try {
|
try {
|
||||||
// 1) POST/PATCH des RIB d'abord (erreurs inline par ligne, tous les blocs
|
// 1) POST/PATCH des RIB d'abord (erreurs inline par ligne, tous les blocs
|
||||||
// tentes). Le back exige >=1 RIB persiste pour valider une LCR a l'etape 2.
|
// tentes). Le back exige >=1 RIB persiste pour valider une LCR a l'etape 2.
|
||||||
// Seuls les blocs RIB TOTALEMENT vides sont ignores : un RIB partiel (ex.
|
// On ne saute une amorce neuve vide QUE s'il reste un autre RIB soumettable :
|
||||||
// IBAN seul) est soumis -> 422 NotBlank (label / bic / iban) inline.
|
// sinon (LCR sans aucun RIB rempli) on la soumet -> 422 NotBlank inline.
|
||||||
|
const hasSubmittableRib = ribs.value.some(r => r.id !== null || !isRibBlank(r))
|
||||||
const ribHasError = await submitRows(
|
const ribHasError = await submitRows(
|
||||||
ribs.value,
|
ribs.value,
|
||||||
ribErrors,
|
ribErrors,
|
||||||
@@ -941,10 +942,10 @@ async function submitAccounting(): Promise<void> {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
error => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
error => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||||
// On ne saute QUE les amorces neuves (id null) totalement vides. Un
|
// On ne saute une amorce neuve (id null) totalement vide que si un autre RIB
|
||||||
// RIB existant vide est soumis -> 422 NotBlank inline (sinon la modif
|
// est soumettable. Un RIB existant vide est toujours soumis -> 422 NotBlank
|
||||||
// serait perdue en silence avec un faux toast de succes).
|
// inline (sinon la modif serait perdue en silence avec un faux toast succes).
|
||||||
rib => rib.id === null && isRibBlank(rib),
|
rib => hasSubmittableRib && rib.id === null && isRibBlank(rib),
|
||||||
)
|
)
|
||||||
if (ribHasError) return
|
if (ribHasError) return
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,927 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<!-- En-tete : retour consultation + nom du fournisseur. -->
|
||||||
|
<div class="flex items-center gap-3 pt-11">
|
||||||
|
<MalioButtonIcon
|
||||||
|
icon="mdi:arrow-left-bold"
|
||||||
|
icon-size="24"
|
||||||
|
variant="ghost"
|
||||||
|
v-bind="{ ariaLabel: t('commercial.suppliers.edit.back') }"
|
||||||
|
@click="goBack"
|
||||||
|
/>
|
||||||
|
<h1 class="text-[30px] font-semibold text-m-primary">{{ headerTitle }}</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Etats de chargement / introuvable. -->
|
||||||
|
<p v-if="loading" class="mt-12 text-center text-black/60">{{ t('commercial.suppliers.edit.loading') }}</p>
|
||||||
|
<p v-else-if="error" class="mt-12 text-center text-m-danger">{{ t('commercial.suppliers.edit.notFound') }}</p>
|
||||||
|
|
||||||
|
<template v-else-if="supplier">
|
||||||
|
<!-- ── Bloc principal (pre-rempli, editable si `manage`) ──────────────
|
||||||
|
Conserve en modification (miroir client) ; edite via son propre
|
||||||
|
PATCH scope sur le groupe supplier:write:main. Readonly pour les
|
||||||
|
roles sans `manage` (ex. Compta). Pas de contact inline (ERP-106). -->
|
||||||
|
<div class="mt-[48px] grid grid-cols-3 xl:grid-cols-4 gap-x-[44px] gap-y-4">
|
||||||
|
<MalioInputText
|
||||||
|
v-model="main.companyName"
|
||||||
|
:label="t('commercial.suppliers.form.main.companyName')"
|
||||||
|
:required="true"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:error="mainErrors.errors.companyName"
|
||||||
|
/>
|
||||||
|
<MalioSelectCheckbox
|
||||||
|
:model-value="main.categoryIris"
|
||||||
|
:options="mainCategoryOptions"
|
||||||
|
:label="t('commercial.suppliers.form.main.categories')"
|
||||||
|
:display-tag="true"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:required="true"
|
||||||
|
:error="mainErrors.errors.categories"
|
||||||
|
@update:model-value="(v: (string | number)[]) => main.categoryIris = v.map(String)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="!businessReadonly" class="mt-12 flex justify-center">
|
||||||
|
<MalioButton
|
||||||
|
variant="primary"
|
||||||
|
:label="t('commercial.suppliers.edit.save')"
|
||||||
|
:disabled="mainSubmitting"
|
||||||
|
@click="submitMain"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ── Onglets : navigation LIBRE, edition independante par onglet ──── -->
|
||||||
|
<MalioTabList v-model="activeTab" :tabs="tabs" :max-visible-tabs="5" :max-width="1100" class="mt-[60px]">
|
||||||
|
<!-- Onglet Information -->
|
||||||
|
<template #information>
|
||||||
|
<div class="mt-12 grid grid-cols-4 gap-x-[44px] gap-y-4 bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]">
|
||||||
|
<!-- pt-1/pb-1 alignent le textarea (h-full) sur les inputs. -->
|
||||||
|
<MalioInputTextArea
|
||||||
|
v-model="information.description"
|
||||||
|
:label="t('commercial.suppliers.form.information.description')"
|
||||||
|
resize="none"
|
||||||
|
group-class="row-span-2 pt-1 pb-1"
|
||||||
|
text-input="h-full text-lg"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:error="informationErrors.errors.description"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="information.competitors"
|
||||||
|
:label="t('commercial.suppliers.form.information.competitors')"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:error="informationErrors.errors.competitors"
|
||||||
|
/>
|
||||||
|
<MalioDate
|
||||||
|
v-model="information.foundedAt"
|
||||||
|
:label="t('commercial.suppliers.form.information.foundedAt')"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:editable="true"
|
||||||
|
:error="informationErrors.errors.foundedAt"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="information.employeesCount"
|
||||||
|
:label="t('commercial.suppliers.form.information.employeesCount')"
|
||||||
|
:mask="EMPLOYEES_MASK"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:error="informationErrors.errors.employeesCount"
|
||||||
|
/>
|
||||||
|
<MalioInputAmount
|
||||||
|
v-model="information.revenueAmount"
|
||||||
|
:label="t('commercial.suppliers.form.information.revenueAmount')"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:error="informationErrors.errors.revenueAmount"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="information.directorName"
|
||||||
|
:label="t('commercial.suppliers.form.information.directorName')"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:error="informationErrors.errors.directorName"
|
||||||
|
/>
|
||||||
|
<MalioInputAmount
|
||||||
|
v-model="information.profitAmount"
|
||||||
|
:label="t('commercial.suppliers.form.information.profitAmount')"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:error="informationErrors.errors.profitAmount"
|
||||||
|
/>
|
||||||
|
<!-- Volume previsionnel : specifique fournisseur (entier). -->
|
||||||
|
<MalioInputText
|
||||||
|
v-model="information.volumeForecast"
|
||||||
|
:label="t('commercial.suppliers.form.information.volumeForecast')"
|
||||||
|
:mask="VOLUME_FORECAST_MASK"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:error="informationErrors.errors.volumeForecast"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div v-if="!businessReadonly" class="mt-12 flex justify-center">
|
||||||
|
<MalioButton
|
||||||
|
variant="primary"
|
||||||
|
:label="t('commercial.suppliers.edit.save')"
|
||||||
|
:disabled="tabSubmitting"
|
||||||
|
@click="submitInformation"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglet Contacts -->
|
||||||
|
<template #contacts>
|
||||||
|
<div class="mt-12 flex flex-col gap-6">
|
||||||
|
<SupplierContactBlock
|
||||||
|
v-for="(contact, index) in contacts"
|
||||||
|
:key="contact.id ?? `new-${index}`"
|
||||||
|
:model-value="contact"
|
||||||
|
:title="t('commercial.suppliers.form.contact.title', { n: index + 1 })"
|
||||||
|
:removable="contacts.length > 1"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:errors="contactErrors[index]"
|
||||||
|
@update:model-value="(v) => contacts[index] = v"
|
||||||
|
@remove="askRemoveContact(index)"
|
||||||
|
/>
|
||||||
|
<div v-if="!businessReadonly" class="flex justify-center gap-6">
|
||||||
|
<MalioButton
|
||||||
|
variant="secondary"
|
||||||
|
icon-name="mdi:add-bold"
|
||||||
|
icon-position="left"
|
||||||
|
:label="t('commercial.suppliers.form.contact.add')"
|
||||||
|
:disabled="!canAddContact"
|
||||||
|
@click="addContact"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
variant="primary"
|
||||||
|
:label="t('commercial.suppliers.edit.save')"
|
||||||
|
:disabled="tabSubmitting"
|
||||||
|
@click="submitContacts"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglet Adresses -->
|
||||||
|
<template #addresses>
|
||||||
|
<div class="mt-12 flex flex-col gap-6">
|
||||||
|
<SupplierAddressBlock
|
||||||
|
v-for="(address, index) in addresses"
|
||||||
|
:key="address.id ?? `new-${index}`"
|
||||||
|
:model-value="address"
|
||||||
|
:title="t('commercial.suppliers.form.address.title', { n: index + 1 })"
|
||||||
|
:category-options="mainCategoryOptions"
|
||||||
|
:site-options="siteOptions"
|
||||||
|
:contact-options="contactOptions"
|
||||||
|
:country-options="countryOptions"
|
||||||
|
:removable="addresses.length > 1"
|
||||||
|
:readonly="businessReadonly"
|
||||||
|
:errors="addressErrors[index]"
|
||||||
|
@update:model-value="(v) => addresses[index] = v"
|
||||||
|
@remove="askRemoveAddress(index)"
|
||||||
|
@degraded="onAddressDegraded"
|
||||||
|
/>
|
||||||
|
<div v-if="!businessReadonly" class="flex justify-center gap-6">
|
||||||
|
<MalioButton
|
||||||
|
variant="secondary"
|
||||||
|
icon-name="mdi:add-bold"
|
||||||
|
icon-position="left"
|
||||||
|
:label="t('commercial.suppliers.form.address.add')"
|
||||||
|
:disabled="!canAddAddress"
|
||||||
|
@click="addAddress"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
variant="primary"
|
||||||
|
:label="t('commercial.suppliers.edit.save')"
|
||||||
|
:disabled="tabSubmitting"
|
||||||
|
@click="submitAddresses"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglet Comptabilite (present uniquement si accounting.view ;
|
||||||
|
editable uniquement si accounting.manage). -->
|
||||||
|
<template v-if="canAccountingView" #accounting>
|
||||||
|
<div class="mt-12 flex flex-col gap-6">
|
||||||
|
<div class="bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]">
|
||||||
|
<div class="grid grid-cols-4 gap-x-[44px] gap-y-4">
|
||||||
|
<MalioInputText
|
||||||
|
v-model="accounting.siren"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.siren')"
|
||||||
|
:mask="SIREN_MASK"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.siren"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="accounting.accountNumber"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.accountNumber')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.accountNumber"
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="accounting.tvaModeIri"
|
||||||
|
:options="tvaModeOptions"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.tvaMode')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
empty-option-label=""
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.tvaMode"
|
||||||
|
@update:model-value="(v: string | number | null) => accounting.tvaModeIri = v === null ? null : String(v)"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="accounting.nTva"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.nTva')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.nTva"
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="accounting.paymentDelayIri"
|
||||||
|
:options="paymentDelayOptions"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.paymentDelay')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
empty-option-label=""
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.paymentDelay"
|
||||||
|
@update:model-value="(v: string | number | null) => accounting.paymentDelayIri = v === null ? null : String(v)"
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="accounting.paymentTypeIri"
|
||||||
|
:options="paymentTypeOptions"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.paymentType')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
empty-option-label=""
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.paymentType"
|
||||||
|
@update:model-value="onPaymentTypeChange"
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
v-if="isBankRequired"
|
||||||
|
:model-value="accounting.bankIri"
|
||||||
|
:options="bankOptions"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.bank')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
empty-option-label=""
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.bank"
|
||||||
|
@update:model-value="(v: string | number | null) => accounting.bankIri = v === null ? null : String(v)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Blocs RIB — affiches uniquement si type de reglement = LCR (RG-2.08). -->
|
||||||
|
<div
|
||||||
|
v-for="(rib, index) in visibleRibs"
|
||||||
|
:key="rib.id ?? `new-${index}`"
|
||||||
|
class="relative bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]"
|
||||||
|
>
|
||||||
|
<MalioButtonIcon
|
||||||
|
v-if="!accountingReadonly && visibleRibs.length > 1"
|
||||||
|
icon="mdi:delete-outline"
|
||||||
|
variant="ghost"
|
||||||
|
button-class="absolute top-3 right-3"
|
||||||
|
v-bind="{ ariaLabel: t('commercial.suppliers.form.accounting.removeRib') }"
|
||||||
|
@click="askRemoveRib(index)"
|
||||||
|
/>
|
||||||
|
<div class="grid grid-cols-4 gap-x-[44px] gap-y-4">
|
||||||
|
<MalioInputText
|
||||||
|
v-model="rib.label"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.ribLabel')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="isRibRequired"
|
||||||
|
:error="ribErrors[index]?.label"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="rib.bic"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.ribBic')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="isRibRequired"
|
||||||
|
:error="ribErrors[index]?.bic"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="rib.iban"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.ribIban')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="isRibRequired"
|
||||||
|
:error="ribErrors[index]?.iban"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="!accountingReadonly" class="flex justify-center gap-6">
|
||||||
|
<MalioButton
|
||||||
|
v-if="isRibRequired"
|
||||||
|
variant="secondary"
|
||||||
|
icon-name="mdi:add-bold"
|
||||||
|
icon-position="left"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.addRib')"
|
||||||
|
:disabled="!canAddRib"
|
||||||
|
@click="addRib"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
variant="primary"
|
||||||
|
:label="t('commercial.suppliers.edit.save')"
|
||||||
|
:disabled="tabSubmitting"
|
||||||
|
@click="submitAccounting"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglets non encore implementes : frame vide (navigation libre). -->
|
||||||
|
<template #transport><ComingSoonPlaceholder /></template>
|
||||||
|
<template #statistics><ComingSoonPlaceholder /></template>
|
||||||
|
<template #reports><ComingSoonPlaceholder /></template>
|
||||||
|
<template #exchanges><ComingSoonPlaceholder /></template>
|
||||||
|
</MalioTabList>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Modal de confirmation generique (suppression contact / adresse / RIB). -->
|
||||||
|
<MalioModal v-model="confirmModal.open" modal-class="max-w-md">
|
||||||
|
<template #header>
|
||||||
|
<h2 class="text-[24px] font-bold">{{ t('commercial.suppliers.form.confirmDelete.title') }}</h2>
|
||||||
|
</template>
|
||||||
|
<p>{{ confirmModal.message }}</p>
|
||||||
|
<template #footer>
|
||||||
|
<MalioButton
|
||||||
|
variant="secondary"
|
||||||
|
button-class="flex-1"
|
||||||
|
:label="t('commercial.suppliers.form.confirmDelete.cancel')"
|
||||||
|
@click="confirmModal.open = false"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
variant="danger"
|
||||||
|
button-class="flex-1"
|
||||||
|
:label="t('commercial.suppliers.form.confirmDelete.confirm')"
|
||||||
|
@click="runConfirm"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</MalioModal>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, onMounted, reactive, ref } from 'vue'
|
||||||
|
import { useSupplier } from '~/modules/commercial/composables/useSupplier'
|
||||||
|
import { useSupplierReferentials, type CategoryOption, type RefOption } from '~/modules/commercial/composables/useSupplierReferentials'
|
||||||
|
import { useSupplierFormErrors } from '~/modules/commercial/composables/useSupplierFormErrors'
|
||||||
|
import {
|
||||||
|
canEditSupplier,
|
||||||
|
categoryOptionsOf,
|
||||||
|
referentialOptionOf,
|
||||||
|
siteOptionsOf,
|
||||||
|
mapContactToDraft,
|
||||||
|
mapAddressToDraft,
|
||||||
|
mapRibToDraft,
|
||||||
|
type SupplierDetail,
|
||||||
|
} from '~/modules/commercial/utils/supplierConsultation'
|
||||||
|
import {
|
||||||
|
buildAccountingPayload,
|
||||||
|
buildAddressPayload,
|
||||||
|
buildContactPayload,
|
||||||
|
buildInformationPayload,
|
||||||
|
buildMainPayload,
|
||||||
|
buildRibPayload,
|
||||||
|
mapAccountingFormDraft,
|
||||||
|
mapInformationDraft,
|
||||||
|
mapMainDraft,
|
||||||
|
resolveTabEditability,
|
||||||
|
type AccountingFormDraft,
|
||||||
|
type InformationFormDraft,
|
||||||
|
type MainFormDraft,
|
||||||
|
type SupplierEditAbilities,
|
||||||
|
} from '~/modules/commercial/utils/supplierEdit'
|
||||||
|
import {
|
||||||
|
buildSupplierFormTabKeys,
|
||||||
|
isAddressValid,
|
||||||
|
isBankRequiredForPaymentType,
|
||||||
|
isContactBlank,
|
||||||
|
isContactNamed,
|
||||||
|
isRibBlank,
|
||||||
|
isRibComplete,
|
||||||
|
isRibRequiredForPaymentType,
|
||||||
|
} from '~/modules/commercial/utils/supplierFormRules'
|
||||||
|
import {
|
||||||
|
emptyAddress,
|
||||||
|
emptyContact,
|
||||||
|
emptyRib,
|
||||||
|
type SupplierAddressFormDraft,
|
||||||
|
type SupplierContactFormDraft,
|
||||||
|
type SupplierRibFormDraft,
|
||||||
|
} from '~/modules/commercial/types/supplierForm'
|
||||||
|
import { extractApiErrorMessage } from '~/shared/utils/api'
|
||||||
|
import { readHistoryTab } from '~/shared/utils/historyTab'
|
||||||
|
|
||||||
|
// Masques de saisie (la normalisation finale reste serveur).
|
||||||
|
const SIREN_MASK = '#########'
|
||||||
|
const EMPLOYEES_MASK = '#######'
|
||||||
|
// Volume previsionnel : champ texte borne aux chiffres (entier >= 0 cote back).
|
||||||
|
const VOLUME_FORECAST_MASK = '##########'
|
||||||
|
|
||||||
|
const { t } = useI18n()
|
||||||
|
const api = useApi()
|
||||||
|
const toast = useToast()
|
||||||
|
const route = useRoute()
|
||||||
|
const router = useRouter()
|
||||||
|
const { can, canAny } = usePermissions()
|
||||||
|
|
||||||
|
// Gating de la route : l'edition exige de pouvoir editer au moins un onglet
|
||||||
|
// (`manage` OU `accounting.manage`). Usine et roles en lecture seule sont
|
||||||
|
// rediriges vers le repertoire (lui-meme protege).
|
||||||
|
if (!canEditSupplier(canAny)) {
|
||||||
|
await navigateTo('/suppliers')
|
||||||
|
}
|
||||||
|
|
||||||
|
const supplierId = route.params.id as string
|
||||||
|
|
||||||
|
const { supplier, loading, error, load } = useSupplier(supplierId)
|
||||||
|
const referentials = useSupplierReferentials()
|
||||||
|
|
||||||
|
// ── Permissions / editabilite par zone (option 1 ERP-74) ────────────────────
|
||||||
|
const abilities = computed<SupplierEditAbilities>(() => ({
|
||||||
|
canManage: can('commercial.suppliers.manage'),
|
||||||
|
canAccountingView: can('commercial.suppliers.accounting.view'),
|
||||||
|
canAccountingManage: can('commercial.suppliers.accounting.manage'),
|
||||||
|
}))
|
||||||
|
const editability = computed(() => resolveTabEditability(abilities.value))
|
||||||
|
// Bloc principal + onglets Information / Contacts / Adresses.
|
||||||
|
const businessReadonly = computed(() => !editability.value.businessEditable)
|
||||||
|
const canAccountingView = computed(() => editability.value.accountingVisible)
|
||||||
|
const accountingReadonly = computed(() => !editability.value.accountingEditable)
|
||||||
|
|
||||||
|
const headerTitle = computed(() => supplier.value?.companyName ?? t('commercial.suppliers.edit.title'))
|
||||||
|
|
||||||
|
// ── Brouillons editables (pre-remplis depuis le detail) ─────────────────────
|
||||||
|
const main = reactive<MainFormDraft>(mapMainDraft({} as SupplierDetail))
|
||||||
|
const information = reactive<InformationFormDraft>(mapInformationDraft({} as SupplierDetail))
|
||||||
|
const accounting = reactive<AccountingFormDraft>(mapAccountingFormDraft({} as SupplierDetail))
|
||||||
|
const contacts = ref<SupplierContactFormDraft[]>([])
|
||||||
|
const addresses = ref<SupplierAddressFormDraft[]>([])
|
||||||
|
const ribs = ref<SupplierRibFormDraft[]>([])
|
||||||
|
|
||||||
|
// Ids des sous-ressources existantes supprimees (DELETE differe au « Valider »).
|
||||||
|
const removedContactIds = ref<number[]>([])
|
||||||
|
const removedAddressIds = ref<number[]>([])
|
||||||
|
const removedRibIds = ref<number[]>([])
|
||||||
|
|
||||||
|
const mainSubmitting = ref(false)
|
||||||
|
const tabSubmitting = ref(false)
|
||||||
|
const addressDegradedNotified = ref(false)
|
||||||
|
|
||||||
|
/** Recopie le detail charge dans les brouillons editables. */
|
||||||
|
function hydrate(detail: SupplierDetail): void {
|
||||||
|
Object.assign(main, mapMainDraft(detail))
|
||||||
|
Object.assign(information, mapInformationDraft(detail))
|
||||||
|
Object.assign(accounting, mapAccountingFormDraft(detail))
|
||||||
|
contacts.value = (detail.contacts ?? []).map(mapContactToDraft)
|
||||||
|
addresses.value = (detail.addresses ?? []).map(mapAddressToDraft)
|
||||||
|
ribs.value = (detail.ribs ?? []).map(mapRibToDraft)
|
||||||
|
// Chaque bloc reste visible meme vide : si une collection est vide, on amorce
|
||||||
|
// un bloc vierge (non persiste tant qu'incomplet — cf. submit*/canAdd*).
|
||||||
|
if (contacts.value.length === 0) contacts.value.push(emptyContact())
|
||||||
|
if (addresses.value.length === 0) addresses.value.push(emptyAddress())
|
||||||
|
// RIB : amorce un bloc vide seulement si le type de reglement est une LCR
|
||||||
|
// (sinon la section reste masquee — RG-2.08).
|
||||||
|
if (isRibRequired.value && ribs.value.length === 0) ribs.value.push(emptyRib())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Options de selects (referentiels UNION valeurs courantes de l'embed) ─────
|
||||||
|
// L'union garantit que les valeurs deja posees s'affichent meme quand le
|
||||||
|
// referentiel complet n'est pas chargeable (roles metier sans
|
||||||
|
// catalog.categories.view / sites.view → 403, cf. matrice § 2.7).
|
||||||
|
function mergeOptions<T extends { value: string }>(primary: T[], extra: T[]): T[] {
|
||||||
|
const seen = new Set(primary.map(o => o.value))
|
||||||
|
return [...primary, ...extra.filter(o => !seen.has(o.value))]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Categories issues de l'embed (fournisseur + adresses), role-independantes.
|
||||||
|
const embedCategoryOptions = computed<CategoryOption[]>(() => {
|
||||||
|
const fromSupplier = categoryOptionsOf(supplier.value?.categories)
|
||||||
|
const fromAddresses = (supplier.value?.addresses ?? []).flatMap(a => categoryOptionsOf(a.categories))
|
||||||
|
return mergeOptions(fromSupplier, fromAddresses)
|
||||||
|
})
|
||||||
|
// Toutes les categories de type FOURNISSEUR sont autorisees, sur le bloc principal
|
||||||
|
// comme sur une adresse (pas de restriction Distributeur/Courtier comme au M1 — RG-2.10).
|
||||||
|
const mainCategoryOptions = computed(() => mergeOptions(referentials.categories.value, embedCategoryOptions.value))
|
||||||
|
|
||||||
|
const embedSiteOptions = computed<RefOption[]>(() =>
|
||||||
|
mergeOptions([], (supplier.value?.addresses ?? []).flatMap(a => siteOptionsOf(a.sites))),
|
||||||
|
)
|
||||||
|
const siteOptions = computed(() => mergeOptions(referentials.sites.value, embedSiteOptions.value))
|
||||||
|
|
||||||
|
// Contacts deja persistes (iri non null), rattachables a une adresse (M2M).
|
||||||
|
const contactOptions = computed<RefOption[]>(() =>
|
||||||
|
contacts.value
|
||||||
|
.filter(c => c.iri !== null)
|
||||||
|
.map(c => ({
|
||||||
|
value: c.iri as string,
|
||||||
|
label: [c.firstName, c.lastName].filter(Boolean).join(' ') || (c.email ?? ''),
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
|
||||||
|
const countryOptions: RefOption[] = [
|
||||||
|
{ value: 'France', label: 'France' },
|
||||||
|
{ value: 'Espagne', label: 'Espagne' },
|
||||||
|
]
|
||||||
|
|
||||||
|
// Selects comptables : referentiel UNION valeur courante de l'embed (libelle).
|
||||||
|
const tvaModeOptions = computed(() => mergeOptions(referentials.tvaModes.value, referentialOptionOf(supplier.value?.tvaMode)))
|
||||||
|
const paymentDelayOptions = computed(() => mergeOptions(referentials.paymentDelays.value, referentialOptionOf(supplier.value?.paymentDelay)))
|
||||||
|
const paymentTypeOptions = computed(() => mergeOptions(
|
||||||
|
referentials.paymentTypes.value.map(p => ({ value: p.value, label: p.label })),
|
||||||
|
referentialOptionOf(supplier.value?.paymentType),
|
||||||
|
))
|
||||||
|
const bankOptions = computed(() => mergeOptions(referentials.banks.value, referentialOptionOf(supplier.value?.bank)))
|
||||||
|
|
||||||
|
// ── Onglets : navigation libre (3 actifs + Compta + 4 coquilles) ────────────
|
||||||
|
const tabKeys = computed(() => buildSupplierFormTabKeys(canAccountingView.value, { includeEditOnlyTabs: true }))
|
||||||
|
|
||||||
|
const TAB_ICONS: Record<string, string> = {
|
||||||
|
information: 'mdi:account-outline',
|
||||||
|
contacts: 'mdi:account-box-plus-outline',
|
||||||
|
addresses: 'mdi:map-marker-outline',
|
||||||
|
transport: 'mdi:truck-delivery-outline',
|
||||||
|
accounting: 'mdi:bank-circle-outline',
|
||||||
|
statistics: 'mdi:finance',
|
||||||
|
reports: 'mdi:file-document-edit-outline',
|
||||||
|
exchanges: 'mdi:account-group-outline',
|
||||||
|
}
|
||||||
|
|
||||||
|
const tabs = computed(() => tabKeys.value.map(key => ({
|
||||||
|
key,
|
||||||
|
label: t(`commercial.suppliers.tab.${key}`),
|
||||||
|
icon: TAB_ICONS[key],
|
||||||
|
})))
|
||||||
|
|
||||||
|
// Onglet initial : repris de la consultation (history.state), sinon Information.
|
||||||
|
const activeTab = ref(readHistoryTab(tabKeys.value) ?? 'information')
|
||||||
|
|
||||||
|
// ── Navigation ──────────────────────────────────────────────────────────────
|
||||||
|
/** Retour consultation en conservant l'onglet courant (via history.state). */
|
||||||
|
function goBack(): void {
|
||||||
|
router.push({ path: `/suppliers/${supplierId}`, state: { tab: activeTab.value } })
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message d'erreur a afficher : violation 422 / detail renvoye par le serveur,
|
||||||
|
* sinon un libelle generique. Le 409 d'unicite de nom (bloc principal) est
|
||||||
|
* traduit explicitement par l'appelant.
|
||||||
|
*/
|
||||||
|
function apiErrorMessage(e: unknown): string {
|
||||||
|
const data = (e as { data?: unknown })?.data
|
||||||
|
return extractApiErrorMessage(data) || t('commercial.suppliers.toast.error')
|
||||||
|
}
|
||||||
|
|
||||||
|
function showError(e: unknown): void {
|
||||||
|
toast.error({ title: t('commercial.suppliers.toast.error'), message: apiErrorMessage(e) })
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Erreurs de validation par champ (ERP-101) ───────────────────────────────
|
||||||
|
const {
|
||||||
|
mainErrors,
|
||||||
|
informationErrors,
|
||||||
|
accountingErrors,
|
||||||
|
contactErrors,
|
||||||
|
addressErrors,
|
||||||
|
ribErrors,
|
||||||
|
submitRows,
|
||||||
|
} = useSupplierFormErrors()
|
||||||
|
|
||||||
|
// ── Bloc principal ───────────────────────────────────────────────────────────
|
||||||
|
/** PATCH /suppliers/{id} — groupe supplier:write:main UNIQUEMENT (mode strict). */
|
||||||
|
async function submitMain(): Promise<void> {
|
||||||
|
if (businessReadonly.value || mainSubmitting.value) return
|
||||||
|
mainSubmitting.value = true
|
||||||
|
mainErrors.clearErrors()
|
||||||
|
try {
|
||||||
|
const updated = await api.patch<SupplierDetail>(`/suppliers/${supplierId}`, buildMainPayload(main, { forUpdate: true }), {
|
||||||
|
headers: { Accept: 'application/ld+json' },
|
||||||
|
toast: false,
|
||||||
|
})
|
||||||
|
// Reaffiche les valeurs normalisees renvoyees par le serveur (UPPERCASE, RG-2.12).
|
||||||
|
Object.assign(main, mapMainDraft(updated))
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
// 409 = doublon nom de societe → erreur inline + toast ; 422 → mapping
|
||||||
|
// inline par champ ; autre → toast de fallback. Cf. ERP-101.
|
||||||
|
const status = (e as { response?: { status?: number } })?.response?.status
|
||||||
|
if (status === 409) {
|
||||||
|
const message = t('commercial.suppliers.form.duplicateCompany')
|
||||||
|
mainErrors.setError('companyName', message)
|
||||||
|
toast.error({ title: t('commercial.suppliers.toast.error'), message })
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mainErrors.handleApiError(e, { fallbackMessage: t('commercial.suppliers.toast.error') })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
mainSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Onglet Information ───────────────────────────────────────────────────────
|
||||||
|
/** PATCH /suppliers/{id} — groupe supplier:write:information UNIQUEMENT. */
|
||||||
|
async function submitInformation(): Promise<void> {
|
||||||
|
if (businessReadonly.value || tabSubmitting.value) return
|
||||||
|
tabSubmitting.value = true
|
||||||
|
informationErrors.clearErrors()
|
||||||
|
try {
|
||||||
|
await api.patch(`/suppliers/${supplierId}`, buildInformationPayload(information), { toast: false })
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
informationErrors.handleApiError(e, { fallbackMessage: t('commercial.suppliers.toast.error') })
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
tabSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Onglet Contacts ───────────────────────────────────────────────────────────
|
||||||
|
const canAddContact = computed(() => {
|
||||||
|
const last = contacts.value[contacts.value.length - 1]
|
||||||
|
return last === undefined || isContactNamed(last)
|
||||||
|
})
|
||||||
|
function addContact(): void {
|
||||||
|
if (canAddContact.value) contacts.value.push(emptyContact())
|
||||||
|
}
|
||||||
|
|
||||||
|
function askRemoveContact(index: number): void {
|
||||||
|
askConfirm(t('commercial.suppliers.form.confirmDelete.contact'), () => {
|
||||||
|
const removed = contacts.value[index]
|
||||||
|
if (removed?.id != null) removedContactIds.value.push(removed.id)
|
||||||
|
contacts.value.splice(index, 1)
|
||||||
|
contactErrors.value.splice(index, 1)
|
||||||
|
// Garde au moins un bloc visible (cf. amorce a l'hydratation).
|
||||||
|
if (contacts.value.length === 0) contacts.value.push(emptyContact())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Valide l'onglet Contacts : DELETE des contacts retires (existants), puis
|
||||||
|
* POST/PATCH des blocs restants sur la sous-ressource. Strictement scope a la
|
||||||
|
* collection contacts (endpoints supplier_contact dedies).
|
||||||
|
*/
|
||||||
|
async function submitContacts(): Promise<void> {
|
||||||
|
if (businessReadonly.value || tabSubmitting.value) return
|
||||||
|
tabSubmitting.value = true
|
||||||
|
contactErrors.value = []
|
||||||
|
try {
|
||||||
|
for (const id of removedContactIds.value) {
|
||||||
|
await api.delete(`/supplier_contacts/${id}`, {}, { toast: false })
|
||||||
|
}
|
||||||
|
removedContactIds.value = []
|
||||||
|
|
||||||
|
// RG-2.13 : au moins un contact requis. Si l'onglet ne contient QUE des
|
||||||
|
// amorces neuves vides, on les soumet -> 422 RG-2.04 inline (nom OU prenom).
|
||||||
|
const hasSubmittableContact = contacts.value.some(c => c.id !== null || !isContactBlank(c))
|
||||||
|
const hasError = await submitRows(
|
||||||
|
contacts.value,
|
||||||
|
contactErrors,
|
||||||
|
async (contact) => {
|
||||||
|
const body = buildContactPayload(contact)
|
||||||
|
if (contact.id === null) {
|
||||||
|
const created = await api.post<{ '@id'?: string, id: number }>(
|
||||||
|
`/suppliers/${supplierId}/contacts`,
|
||||||
|
body,
|
||||||
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
|
)
|
||||||
|
contact.id = created.id
|
||||||
|
contact.iri = created['@id'] ?? null
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await api.patch(`/supplier_contacts/${contact.id}`, body, { toast: false })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error => showError(error),
|
||||||
|
contact => hasSubmittableContact && contact.id === null && isContactBlank(contact),
|
||||||
|
)
|
||||||
|
if (hasError) return
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
showError(e)
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
tabSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Onglet Adresses ───────────────────────────────────────────────────────────
|
||||||
|
// « + Adresse » desactive tant que la derniere adresse n'est pas valide.
|
||||||
|
const canAddAddress = computed(() => {
|
||||||
|
const last = addresses.value[addresses.value.length - 1]
|
||||||
|
return last !== undefined && isAddressValid(last)
|
||||||
|
})
|
||||||
|
|
||||||
|
function addAddress(): void {
|
||||||
|
if (canAddAddress.value) addresses.value.push(emptyAddress())
|
||||||
|
}
|
||||||
|
|
||||||
|
function askRemoveAddress(index: number): void {
|
||||||
|
askConfirm(t('commercial.suppliers.form.confirmDelete.address'), () => {
|
||||||
|
const removed = addresses.value[index]
|
||||||
|
if (removed?.id != null) removedAddressIds.value.push(removed.id)
|
||||||
|
addresses.value.splice(index, 1)
|
||||||
|
addressErrors.value.splice(index, 1)
|
||||||
|
// Garde au moins un bloc visible (cf. amorce a l'hydratation).
|
||||||
|
if (addresses.value.length === 0) addresses.value.push(emptyAddress())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function onAddressDegraded(): void {
|
||||||
|
if (addressDegradedNotified.value) return
|
||||||
|
addressDegradedNotified.value = true
|
||||||
|
toast.warning({
|
||||||
|
title: t('commercial.suppliers.toast.error'),
|
||||||
|
message: t('commercial.suppliers.form.address.degraded'),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Valide l'onglet Adresses : DELETE des adresses retirees puis POST/PATCH. */
|
||||||
|
async function submitAddresses(): Promise<void> {
|
||||||
|
if (businessReadonly.value || tabSubmitting.value) return
|
||||||
|
tabSubmitting.value = true
|
||||||
|
addressErrors.value = []
|
||||||
|
try {
|
||||||
|
for (const id of removedAddressIds.value) {
|
||||||
|
await api.delete(`/supplier_addresses/${id}`, {}, { toast: false })
|
||||||
|
}
|
||||||
|
removedAddressIds.value = []
|
||||||
|
|
||||||
|
const hasError = await submitRows(
|
||||||
|
addresses.value,
|
||||||
|
addressErrors,
|
||||||
|
async (address) => {
|
||||||
|
// Edition d'une adresse existante : champ requis vide envoye en `''`
|
||||||
|
// (NotBlank 422) au lieu d'etre omis — sinon le PATCH garderait
|
||||||
|
// l'ancienne valeur (faux 200). Creation (id null) : omit classique.
|
||||||
|
const body = buildAddressPayload(address, { forUpdate: address.id !== null })
|
||||||
|
if (address.id === null) {
|
||||||
|
const created = await api.post<{ id: number }>(
|
||||||
|
`/suppliers/${supplierId}/addresses`,
|
||||||
|
body,
|
||||||
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
|
)
|
||||||
|
address.id = created.id
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await api.patch(`/supplier_addresses/${address.id}`, body, { toast: false })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error => showError(error),
|
||||||
|
)
|
||||||
|
if (hasError) return
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
showError(e)
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
tabSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Onglet Comptabilite ──────────────────────────────────────────────────────
|
||||||
|
const selectedPaymentTypeCode = computed(() =>
|
||||||
|
referentials.paymentTypes.value.find(p => p.value === accounting.paymentTypeIri)?.code ?? null,
|
||||||
|
)
|
||||||
|
const isBankRequired = computed(() => isBankRequiredForPaymentType(selectedPaymentTypeCode.value))
|
||||||
|
const isRibRequired = computed(() => isRibRequiredForPaymentType(selectedPaymentTypeCode.value))
|
||||||
|
|
||||||
|
// Les blocs RIB ne sont affiches que pour une LCR (RG-2.08).
|
||||||
|
const visibleRibs = computed(() => isRibRequired.value ? ribs.value : [])
|
||||||
|
|
||||||
|
function onPaymentTypeChange(value: string | number | null): void {
|
||||||
|
accounting.paymentTypeIri = value === null ? null : String(value)
|
||||||
|
if (!isBankRequired.value) accounting.bankIri = null
|
||||||
|
// Les RIB n'ont de sens que pour une LCR (RG-2.08) : on amorce un bloc vide
|
||||||
|
// quand LCR est choisi, sinon on vide la liste — les RIB deja persistes sont
|
||||||
|
// marques pour suppression serveur au prochain enregistrement.
|
||||||
|
if (isRibRequired.value) {
|
||||||
|
if (ribs.value.length === 0) ribs.value.push(emptyRib())
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (const rib of ribs.value) {
|
||||||
|
if (rib.id != null) removedRibIds.value.push(rib.id)
|
||||||
|
}
|
||||||
|
ribs.value = []
|
||||||
|
ribErrors.value = []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// « + RIB » desactive tant que le dernier bloc RIB n'est pas complet.
|
||||||
|
const canAddRib = computed(() => {
|
||||||
|
const last = ribs.value[ribs.value.length - 1]
|
||||||
|
return last !== undefined && isRibComplete(last)
|
||||||
|
})
|
||||||
|
|
||||||
|
function addRib(): void {
|
||||||
|
if (canAddRib.value) ribs.value.push(emptyRib())
|
||||||
|
}
|
||||||
|
|
||||||
|
function askRemoveRib(index: number): void {
|
||||||
|
askConfirm(t('commercial.suppliers.form.confirmDelete.rib'), () => {
|
||||||
|
const removed = ribs.value[index]
|
||||||
|
if (removed?.id != null) removedRibIds.value.push(removed.id)
|
||||||
|
ribs.value.splice(index, 1)
|
||||||
|
ribErrors.value.splice(index, 1)
|
||||||
|
// Garde au moins un bloc RIB visible (cf. amorce a l'hydratation).
|
||||||
|
if (ribs.value.length === 0) ribs.value.push(emptyRib())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Valide l'onglet Comptabilite : POST/PATCH des RIB sur la sous-ressource PUIS
|
||||||
|
* PATCH des scalaires (groupe supplier:write:accounting, exige accounting.manage
|
||||||
|
* cote back) PUIS DELETE des RIB retires. Les RIB crees d'abord : le back valide
|
||||||
|
* RG-2.08 (LCR => au moins un RIB persiste) sur le PATCH scalaires. Aucun champ
|
||||||
|
* main/information dans le payload (mode strict RG-2.16 : sinon 403 sur tout le payload).
|
||||||
|
*/
|
||||||
|
async function submitAccounting(): Promise<void> {
|
||||||
|
if (accountingReadonly.value || tabSubmitting.value) return
|
||||||
|
tabSubmitting.value = true
|
||||||
|
accountingErrors.clearErrors()
|
||||||
|
try {
|
||||||
|
// 1) POST/PATCH des RIB d'abord (erreurs inline par ligne, tous les blocs
|
||||||
|
// tentes). On ne saute une amorce neuve vide QUE s'il reste un autre RIB
|
||||||
|
// soumettable : sinon (ex. l'unique RIB existant supprime, remplace par un
|
||||||
|
// bloc vide), on la soumet pour declencher la 422 NotBlank inline plutot que
|
||||||
|
// de laisser le DELETE echouer en « dernier RIB d'une LCR » (message plat).
|
||||||
|
const hasSubmittableRib = ribs.value.some(r => r.id !== null || !isRibBlank(r))
|
||||||
|
const ribHasError = await submitRows(
|
||||||
|
ribs.value,
|
||||||
|
ribErrors,
|
||||||
|
async (rib) => {
|
||||||
|
// Edition d'un RIB existant : champ requis vide envoye en `''` (NotBlank
|
||||||
|
// 422) au lieu d'etre omis (sinon le PATCH garderait l'ancienne valeur).
|
||||||
|
const body = buildRibPayload(rib, { forUpdate: rib.id !== null })
|
||||||
|
if (rib.id === null) {
|
||||||
|
const created = await api.post<{ id: number }>(
|
||||||
|
`/suppliers/${supplierId}/ribs`,
|
||||||
|
body,
|
||||||
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
|
)
|
||||||
|
rib.id = created.id
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await api.patch(`/supplier_ribs/${rib.id}`, body, { toast: false })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error => showError(error),
|
||||||
|
rib => hasSubmittableRib && rib.id === null && isRibBlank(rib),
|
||||||
|
)
|
||||||
|
if (ribHasError) return
|
||||||
|
|
||||||
|
// 2) PATCH des scalaires comptables (erreurs inline sur leurs champs).
|
||||||
|
try {
|
||||||
|
await api.patch(`/suppliers/${supplierId}`, buildAccountingPayload(accounting, isBankRequired.value), { toast: false })
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
accountingErrors.handleApiError(error, { fallbackMessage: t('commercial.suppliers.toast.error') })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3) DELETE des RIB retires : APRES le PATCH scalaires (si on quitte LCR, le
|
||||||
|
// guard back n'autorise la suppression du dernier RIB qu'une fois le type change).
|
||||||
|
for (const id of removedRibIds.value) {
|
||||||
|
await api.delete(`/supplier_ribs/${id}`, {}, { toast: false })
|
||||||
|
}
|
||||||
|
removedRibIds.value = []
|
||||||
|
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
showError(e)
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
tabSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Modal de confirmation generique ──────────────────────────────────────────
|
||||||
|
const confirmModal = reactive({
|
||||||
|
open: false,
|
||||||
|
message: '',
|
||||||
|
action: null as null | (() => void),
|
||||||
|
})
|
||||||
|
|
||||||
|
function askConfirm(message: string, action: () => void): void {
|
||||||
|
confirmModal.message = message
|
||||||
|
confirmModal.action = action
|
||||||
|
confirmModal.open = true
|
||||||
|
}
|
||||||
|
|
||||||
|
function runConfirm(): void {
|
||||||
|
confirmModal.action?.()
|
||||||
|
confirmModal.action = null
|
||||||
|
confirmModal.open = false
|
||||||
|
}
|
||||||
|
|
||||||
|
useHead({ title: headerTitle })
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
// Referentiels en best-effort (echec non bloquant : l'embed alimente les
|
||||||
|
// libelles des valeurs courantes).
|
||||||
|
referentials.loadCommon().catch(() => {})
|
||||||
|
await load()
|
||||||
|
if (supplier.value) hydrate(supplier.value)
|
||||||
|
})
|
||||||
|
</script>
|
||||||
@@ -0,0 +1,454 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<!-- En-tete : retour repertoire + nom du fournisseur + actions (Modifier / Archiver|Restaurer). -->
|
||||||
|
<div class="flex items-center gap-3 pt-11">
|
||||||
|
<MalioButtonIcon
|
||||||
|
icon="mdi:arrow-left-bold"
|
||||||
|
icon-size="24"
|
||||||
|
variant="ghost"
|
||||||
|
v-bind="{ ariaLabel: t('commercial.suppliers.consultation.back') }"
|
||||||
|
@click="goBack"
|
||||||
|
/>
|
||||||
|
<h1 class="text-[30px] font-semibold text-m-primary">{{ headerTitle }}</h1>
|
||||||
|
|
||||||
|
<!-- gap-12 = 48px : meme espacement que Ajouter / Filtres du repertoire. -->
|
||||||
|
<div class="ml-auto flex items-center gap-12">
|
||||||
|
<MalioButton
|
||||||
|
v-if="canEdit"
|
||||||
|
variant="secondary"
|
||||||
|
icon-name="mdi:pencil-outline"
|
||||||
|
icon-position="left"
|
||||||
|
:label="t('commercial.suppliers.action.edit')"
|
||||||
|
@click="goEdit"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
v-if="showArchive"
|
||||||
|
variant="secondary"
|
||||||
|
icon-name="mdi:archive-arrow-down-outline"
|
||||||
|
icon-position="left"
|
||||||
|
:label="t('commercial.suppliers.action.archive')"
|
||||||
|
@click="askToggleArchive"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
v-if="showRestore"
|
||||||
|
variant="secondary"
|
||||||
|
icon-name="mdi:archive-arrow-up-outline"
|
||||||
|
icon-position="left"
|
||||||
|
:label="t('commercial.suppliers.action.restore')"
|
||||||
|
@click="askToggleArchive"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Etats de chargement / introuvable. -->
|
||||||
|
<p v-if="loading" class="mt-12 text-center text-black/60">{{ t('commercial.suppliers.consultation.loading') }}</p>
|
||||||
|
<p v-else-if="error" class="mt-12 text-center text-m-danger">{{ t('commercial.suppliers.consultation.notFound') }}</p>
|
||||||
|
|
||||||
|
<template v-else-if="supplier">
|
||||||
|
<!-- ── Formulaire principal (lecture seule) ──────────────────────── -->
|
||||||
|
<div class="mt-[48px] grid grid-cols-3 xl:grid-cols-4 gap-x-[44px] gap-y-4">
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="supplier.companyName"
|
||||||
|
:label="t('commercial.suppliers.form.main.companyName')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioSelectCheckbox
|
||||||
|
:model-value="categoryIris"
|
||||||
|
:options="mainCategoryOptions"
|
||||||
|
:label="t('commercial.suppliers.form.main.categories')"
|
||||||
|
:display-tag="true"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ── Onglets (navigation libre, tout en lecture seule) ─────────── -->
|
||||||
|
<MalioTabList v-model="activeTab" :tabs="tabs" :max-visible-tabs="5" :max-width="1100" class="mt-[60px]">
|
||||||
|
<!-- Onglet Information -->
|
||||||
|
<template #information>
|
||||||
|
<div class="mt-12 grid grid-cols-4 gap-x-[44px] gap-y-4 bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]">
|
||||||
|
<!-- pt-1/pb-1 alignent le textarea (h-full) en haut ET en bas
|
||||||
|
sur les inputs (champ 40px centre dans un h-12). -->
|
||||||
|
<MalioInputTextArea
|
||||||
|
:model-value="information.description"
|
||||||
|
:label="t('commercial.suppliers.form.information.description')"
|
||||||
|
resize="none"
|
||||||
|
group-class="row-span-2 pt-1 pb-1"
|
||||||
|
text-input="h-full text-lg"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="information.competitors"
|
||||||
|
:label="t('commercial.suppliers.form.information.competitors')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioDate
|
||||||
|
:model-value="information.foundedAt"
|
||||||
|
:label="t('commercial.suppliers.form.information.foundedAt')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="information.employeesCount"
|
||||||
|
:label="t('commercial.suppliers.form.information.employeesCount')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioInputAmount
|
||||||
|
:model-value="information.revenueAmount"
|
||||||
|
:label="t('commercial.suppliers.form.information.revenueAmount')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="information.directorName"
|
||||||
|
:label="t('commercial.suppliers.form.information.directorName')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioInputAmount
|
||||||
|
:model-value="information.profitAmount"
|
||||||
|
:label="t('commercial.suppliers.form.information.profitAmount')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<!-- Volume previsionnel : specifique fournisseur (entier). -->
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="information.volumeForecast"
|
||||||
|
:label="t('commercial.suppliers.form.information.volumeForecast')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglet Contacts -->
|
||||||
|
<template #contacts>
|
||||||
|
<div class="mt-12 flex flex-col gap-6">
|
||||||
|
<SupplierContactBlock
|
||||||
|
v-for="(contact, index) in contacts"
|
||||||
|
:key="contact.id ?? index"
|
||||||
|
:model-value="contact"
|
||||||
|
:title="t('commercial.suppliers.form.contact.title', { n: index + 1 })"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglet Adresses -->
|
||||||
|
<template #addresses>
|
||||||
|
<div class="mt-12 flex flex-col gap-6">
|
||||||
|
<SupplierAddressBlock
|
||||||
|
v-for="(view, index) in addressViews"
|
||||||
|
:key="view.draft.id ?? index"
|
||||||
|
:model-value="view.draft"
|
||||||
|
:title="t('commercial.suppliers.form.address.title', { n: index + 1 })"
|
||||||
|
:category-options="view.categoryOptions"
|
||||||
|
:site-options="allSiteOptions"
|
||||||
|
:contact-options="contactOptions"
|
||||||
|
:country-options="countryOptions"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglet Comptabilite (present uniquement si accounting.view). -->
|
||||||
|
<template v-if="canAccountingView" #accounting>
|
||||||
|
<div class="mt-12 flex flex-col gap-6">
|
||||||
|
<div class="bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]">
|
||||||
|
<div class="grid grid-cols-4 gap-x-[44px] gap-y-4">
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="accounting.siren"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.siren')"
|
||||||
|
:mask="SIREN_MASK"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="accounting.accountNumber"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.accountNumber')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="accounting.tvaModeIri"
|
||||||
|
:options="tvaModeOptions"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.tvaMode')"
|
||||||
|
empty-option-label=""
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="accounting.nTva"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.nTva')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="accounting.paymentDelayIri"
|
||||||
|
:options="paymentDelayOptions"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.paymentDelay')"
|
||||||
|
empty-option-label=""
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="accounting.paymentTypeIri"
|
||||||
|
:options="paymentTypeOptions"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.paymentType')"
|
||||||
|
empty-option-label=""
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
v-if="accounting.bankIri"
|
||||||
|
:model-value="accounting.bankIri"
|
||||||
|
:options="bankOptions"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.bank')"
|
||||||
|
empty-option-label=""
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Blocs RIB (0..n), lecture seule. -->
|
||||||
|
<div
|
||||||
|
v-for="(rib, index) in ribs"
|
||||||
|
:key="rib.id ?? index"
|
||||||
|
class="bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]"
|
||||||
|
>
|
||||||
|
<div class="grid grid-cols-4 gap-x-[44px] gap-y-4">
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="rib.label"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.ribLabel')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="rib.bic"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.ribBic')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
:model-value="rib.iban"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.ribIban')"
|
||||||
|
readonly
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglets non encore implementes : frame vide (navigation libre). -->
|
||||||
|
<template #transport><ComingSoonPlaceholder /></template>
|
||||||
|
<template #statistics><ComingSoonPlaceholder /></template>
|
||||||
|
<template #reports><ComingSoonPlaceholder /></template>
|
||||||
|
<template #exchanges><ComingSoonPlaceholder /></template>
|
||||||
|
</MalioTabList>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Modal de confirmation Archiver / Restaurer. -->
|
||||||
|
<MalioModal v-model="confirmOpen" modal-class="max-w-md">
|
||||||
|
<template #header>
|
||||||
|
<h2 class="text-[24px] font-bold">
|
||||||
|
{{ isArchived ? t('commercial.suppliers.consultation.confirmRestore.title') : t('commercial.suppliers.consultation.confirmArchive.title') }}
|
||||||
|
</h2>
|
||||||
|
</template>
|
||||||
|
<p>{{ isArchived ? t('commercial.suppliers.consultation.confirmRestore.message') : t('commercial.suppliers.consultation.confirmArchive.message') }}</p>
|
||||||
|
<template #footer>
|
||||||
|
<MalioButton
|
||||||
|
variant="secondary"
|
||||||
|
button-class="flex-1"
|
||||||
|
:label="t('commercial.suppliers.form.confirmDelete.cancel')"
|
||||||
|
@click="confirmOpen = false"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
:variant="isArchived ? 'primary' : 'danger'"
|
||||||
|
button-class="flex-1"
|
||||||
|
:label="t('commercial.suppliers.form.confirmDelete.confirm')"
|
||||||
|
:disabled="toggling"
|
||||||
|
@click="confirmToggleArchive"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</MalioModal>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, onMounted, ref } from 'vue'
|
||||||
|
import { useSupplier } from '~/modules/commercial/composables/useSupplier'
|
||||||
|
import { buildSupplierFormTabKeys } from '~/modules/commercial/utils/supplierFormRules'
|
||||||
|
import { readHistoryTab } from '~/shared/utils/historyTab'
|
||||||
|
import {
|
||||||
|
canEditSupplier,
|
||||||
|
categoryOptionsOf,
|
||||||
|
contactOptionsOf,
|
||||||
|
emptyAddress,
|
||||||
|
mapAccountingDraft,
|
||||||
|
mapAddressView,
|
||||||
|
mapContactToDraft,
|
||||||
|
mapRibToDraft,
|
||||||
|
referentialOptionOf,
|
||||||
|
showArchiveAction,
|
||||||
|
showRestoreAction,
|
||||||
|
type SelectOption,
|
||||||
|
type SupplierDetail,
|
||||||
|
} from '~/modules/commercial/utils/supplierConsultation'
|
||||||
|
import { emptyContact } from '~/modules/commercial/types/supplierForm'
|
||||||
|
|
||||||
|
// Masque d'affichage (purement visuel, la donnee reste celle du serveur).
|
||||||
|
const SIREN_MASK = '#########'
|
||||||
|
|
||||||
|
const { t } = useI18n()
|
||||||
|
const route = useRoute()
|
||||||
|
const router = useRouter()
|
||||||
|
const toast = useToast()
|
||||||
|
const { can, canAny } = usePermissions()
|
||||||
|
const authStore = useAuthStore()
|
||||||
|
|
||||||
|
// Gating de la route : la consultation exige `view`. Usine (sans view) est
|
||||||
|
// redirige vers le repertoire (lui-meme protege). Cf. matrice § 2.7.
|
||||||
|
if (!can('commercial.suppliers.view')) {
|
||||||
|
await navigateTo('/suppliers')
|
||||||
|
}
|
||||||
|
|
||||||
|
const supplierId = route.params.id as string
|
||||||
|
|
||||||
|
const { supplier, loading, error, load, archive, restore } = useSupplier(supplierId)
|
||||||
|
|
||||||
|
// ── Permissions / visibilite des actions ───────────────────────────────────
|
||||||
|
const canAccountingView = computed(() => can('commercial.suppliers.accounting.view'))
|
||||||
|
const canEdit = computed(() => canEditSupplier(canAny))
|
||||||
|
const isArchived = computed(() => supplier.value?.isArchived === true)
|
||||||
|
const showArchive = computed(() => showArchiveAction(can, isArchived.value))
|
||||||
|
const showRestore = computed(() => showRestoreAction(can, isArchived.value))
|
||||||
|
|
||||||
|
const headerTitle = computed(() => supplier.value?.companyName ?? t('commercial.suppliers.consultation.title'))
|
||||||
|
|
||||||
|
// ── Donnees derivees du payload (lecture seule) ────────────────────────────
|
||||||
|
const categoryIris = computed(() => (supplier.value?.categories ?? []).map(c => c['@id']))
|
||||||
|
|
||||||
|
const information = computed(() => ({
|
||||||
|
description: supplier.value?.description ?? null,
|
||||||
|
competitors: supplier.value?.competitors ?? null,
|
||||||
|
// MalioDate attend strictement YYYY-MM-DD : on tronque l'ISO datetime renvoye.
|
||||||
|
foundedAt: supplier.value?.foundedAt ? supplier.value.foundedAt.slice(0, 10) : null,
|
||||||
|
employeesCount: supplier.value?.employeesCount != null ? String(supplier.value.employeesCount) : null,
|
||||||
|
revenueAmount: supplier.value?.revenueAmount ?? null,
|
||||||
|
profitAmount: supplier.value?.profitAmount ?? null,
|
||||||
|
directorName: supplier.value?.directorName ?? null,
|
||||||
|
volumeForecast: supplier.value?.volumeForecast != null ? String(supplier.value.volumeForecast) : null,
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Chaque bloc reste visible meme vide en consultation : si la collection est
|
||||||
|
// vide, on affiche un bloc vierge en lecture seule (pas de message « Aucun … »).
|
||||||
|
const contacts = computed(() => {
|
||||||
|
const list = (supplier.value?.contacts ?? []).map(mapContactToDraft)
|
||||||
|
return list.length ? list : [emptyContact()]
|
||||||
|
})
|
||||||
|
// Vue par adresse : brouillon + options (sites/categories) propres a l'adresse.
|
||||||
|
const addressViews = computed(() => {
|
||||||
|
const views = (supplier.value?.addresses ?? []).map(mapAddressView)
|
||||||
|
return views.length ? views : [{ draft: emptyAddress(), siteOptions: [], categoryOptions: [] }]
|
||||||
|
})
|
||||||
|
// Exception au placeholder ci-dessus : on n'affiche AUCUN bloc RIB quand le
|
||||||
|
// fournisseur n'en a pas (un RIB n'existe que pour un reglement LCR — RG-2.08).
|
||||||
|
const ribs = computed(() => (supplier.value?.ribs ?? []).map(mapRibToDraft))
|
||||||
|
// Draft comptable (tout null si l'utilisateur n'a pas accounting.view).
|
||||||
|
const accounting = computed(() => mapAccountingDraft(supplier.value ?? ({} as SupplierDetail)))
|
||||||
|
|
||||||
|
// ── Options des selects (construites depuis l'EMBED, jamais via un GET de
|
||||||
|
// referentiel : /categories et /sites sont en 403 pour les roles metier
|
||||||
|
// non-admin, ce qui laisserait les libelles vides). ───────────────────────
|
||||||
|
const mainCategoryOptions = computed(() => categoryOptionsOf(supplier.value?.categories))
|
||||||
|
const contactOptions = computed(() => contactOptionsOf(supplier.value?.contacts))
|
||||||
|
|
||||||
|
// Liste COMPLETE des sites disponibles, issue de /api/me (groupe me:read — donc
|
||||||
|
// pas de 403 pour les roles metier, contrairement a GET /sites). Libelle = numero
|
||||||
|
// de departement (2 premiers chiffres du code postal). Permet d'afficher TOUJOURS
|
||||||
|
// toutes les cases « Sites » (86 / 17 / 82) dans le bloc adresse, meme celles non
|
||||||
|
// rattachees a l'adresse consultee (les rattachees restent cochees via siteIris).
|
||||||
|
const allSiteOptions = computed<SelectOption[]>(() =>
|
||||||
|
(authStore.user?.sites ?? []).map(s => ({
|
||||||
|
value: `/api/sites/${s.id}`,
|
||||||
|
label: (s.postalCode ?? '').slice(0, 2),
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
|
||||||
|
const countryOptions: SelectOption[] = [
|
||||||
|
{ value: 'France', label: 'France' },
|
||||||
|
{ value: 'Espagne', label: 'Espagne' },
|
||||||
|
]
|
||||||
|
|
||||||
|
// Selects comptables : libelle issu de l'embed (option unique ou vide).
|
||||||
|
const tvaModeOptions = computed(() => referentialOptionOf(supplier.value?.tvaMode))
|
||||||
|
const paymentDelayOptions = computed(() => referentialOptionOf(supplier.value?.paymentDelay))
|
||||||
|
const paymentTypeOptions = computed(() => referentialOptionOf(supplier.value?.paymentType))
|
||||||
|
const bankOptions = computed(() => referentialOptionOf(supplier.value?.bank))
|
||||||
|
|
||||||
|
// ── Onglets : navigation LIBRE (pas de sequence forcee en consultation) ────
|
||||||
|
// 3 onglets actifs (Information, Contacts, Adresses, + Comptabilite si droit) et
|
||||||
|
// 4 coquilles (Transport, Statistiques, Rapports, Echanges).
|
||||||
|
const tabKeys = computed(() => buildSupplierFormTabKeys(canAccountingView.value, { includeEditOnlyTabs: true }))
|
||||||
|
|
||||||
|
const TAB_ICONS: Record<string, string> = {
|
||||||
|
information: 'mdi:account-outline',
|
||||||
|
contacts: 'mdi:account-box-plus-outline',
|
||||||
|
addresses: 'mdi:map-marker-outline',
|
||||||
|
transport: 'mdi:truck-delivery-outline',
|
||||||
|
accounting: 'mdi:bank-circle-outline',
|
||||||
|
statistics: 'mdi:finance',
|
||||||
|
reports: 'mdi:file-document-edit-outline',
|
||||||
|
exchanges: 'mdi:account-group-outline',
|
||||||
|
}
|
||||||
|
|
||||||
|
const tabs = computed(() => tabKeys.value.map(key => ({
|
||||||
|
key,
|
||||||
|
label: t(`commercial.suppliers.tab.${key}`),
|
||||||
|
icon: TAB_ICONS[key],
|
||||||
|
})))
|
||||||
|
|
||||||
|
// Onglet initial : repris de l'edition au retour (history.state), sinon Information.
|
||||||
|
const activeTab = ref(readHistoryTab(tabKeys.value) ?? 'information')
|
||||||
|
|
||||||
|
// ── Navigation ─────────────────────────────────────────────────────────────
|
||||||
|
function goBack(): void {
|
||||||
|
router.push('/suppliers')
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Bascule en edition en conservant l'onglet courant (via history.state). */
|
||||||
|
function goEdit(): void {
|
||||||
|
router.push({ path: `/suppliers/${supplierId}/edit`, state: { tab: activeTab.value } })
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Archivage / Restauration ────────────────────────────────────────────────
|
||||||
|
const confirmOpen = ref(false)
|
||||||
|
const toggling = ref(false)
|
||||||
|
|
||||||
|
function askToggleArchive(): void {
|
||||||
|
confirmOpen.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Confirme l'archivage ou la restauration (PATCH isArchived seul). Gere le 409
|
||||||
|
* de conflit d'homonyme actif a la restauration avec un message dedie.
|
||||||
|
*/
|
||||||
|
async function confirmToggleArchive(): Promise<void> {
|
||||||
|
if (toggling.value) return
|
||||||
|
toggling.value = true
|
||||||
|
const restoring = isArchived.value
|
||||||
|
try {
|
||||||
|
if (restoring) {
|
||||||
|
await restore()
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.restoreSuccess') })
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await archive()
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.archiveSuccess') })
|
||||||
|
}
|
||||||
|
confirmOpen.value = false
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
const status = (e as { response?: { status?: number } })?.response?.status
|
||||||
|
toast.error({
|
||||||
|
title: t('commercial.suppliers.toast.error'),
|
||||||
|
message: restoring && status === 409
|
||||||
|
? t('commercial.suppliers.toast.restoreConflict')
|
||||||
|
: t('commercial.suppliers.toast.error'),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
toggling.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
useHead({ title: headerTitle })
|
||||||
|
|
||||||
|
onMounted(load)
|
||||||
|
</script>
|
||||||
@@ -0,0 +1,866 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<!-- En-tete : retour vers le repertoire + titre. -->
|
||||||
|
<div class="flex items-center gap-3 pt-11">
|
||||||
|
<MalioButtonIcon
|
||||||
|
icon="mdi:arrow-left-bold"
|
||||||
|
icon-size="24"
|
||||||
|
variant="ghost"
|
||||||
|
v-bind="{ ariaLabel: t('commercial.suppliers.form.back') }"
|
||||||
|
@click="goBack"
|
||||||
|
/>
|
||||||
|
<h1 class="text-[30px] font-semibold text-m-primary">{{ t('commercial.suppliers.form.title') }}</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ── Formulaire principal (pre-onglets) ─────────────────────────────
|
||||||
|
Sans validation de ce bloc, les onglets restent inaccessibles. Au
|
||||||
|
succes du POST, les champs passent en lecture seule et on bascule
|
||||||
|
automatiquement sur l'onglet Information. Pas de contact inline (ERP-106). -->
|
||||||
|
<div class="mt-[48px] grid grid-cols-3 xl:grid-cols-4 gap-x-[44px] gap-y-4">
|
||||||
|
<MalioInputText
|
||||||
|
v-model="main.companyName"
|
||||||
|
:label="t('commercial.suppliers.form.main.companyName')"
|
||||||
|
:required="true"
|
||||||
|
:readonly="mainLocked"
|
||||||
|
:error="mainErrors.errors.companyName"
|
||||||
|
/>
|
||||||
|
<MalioSelectCheckbox
|
||||||
|
:model-value="main.categoryIris"
|
||||||
|
:options="referentials.categories.value"
|
||||||
|
:label="t('commercial.suppliers.form.main.categories')"
|
||||||
|
:display-tag="true"
|
||||||
|
:readonly="mainLocked"
|
||||||
|
:required="true"
|
||||||
|
:error="mainErrors.errors.categories"
|
||||||
|
@update:model-value="(v: (string | number)[]) => main.categoryIris = v.map(String)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="!mainLocked" class="mt-12 flex justify-center">
|
||||||
|
<MalioButton
|
||||||
|
variant="primary"
|
||||||
|
:label="t('commercial.suppliers.form.submit')"
|
||||||
|
:disabled="mainSubmitting"
|
||||||
|
@click="submitMain"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ── Onglets a validation incrementale ─────────────────────────────-->
|
||||||
|
<MalioTabList v-model="activeTab" :tabs="tabs" class="mt-[60px]">
|
||||||
|
<!-- Onglet Information -->
|
||||||
|
<template #information>
|
||||||
|
<div class="mt-12 grid grid-cols-4 gap-x-[44px] gap-y-4 bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]">
|
||||||
|
<MalioInputTextArea
|
||||||
|
v-model="information.description"
|
||||||
|
:label="t('commercial.suppliers.form.information.description')"
|
||||||
|
resize="none"
|
||||||
|
group-class="row-span-2 pt-1 pb-1"
|
||||||
|
text-input="h-full text-lg"
|
||||||
|
:readonly="isValidated('information')"
|
||||||
|
:error="informationErrors.errors.description"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="information.competitors"
|
||||||
|
:label="t('commercial.suppliers.form.information.competitors')"
|
||||||
|
:readonly="isValidated('information')"
|
||||||
|
:error="informationErrors.errors.competitors"
|
||||||
|
/>
|
||||||
|
<MalioDate
|
||||||
|
v-model="information.foundedAt"
|
||||||
|
:label="t('commercial.suppliers.form.information.foundedAt')"
|
||||||
|
:readonly="isValidated('information')"
|
||||||
|
:editable="true"
|
||||||
|
:error="informationErrors.errors.foundedAt"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="information.employeesCount"
|
||||||
|
:label="t('commercial.suppliers.form.information.employeesCount')"
|
||||||
|
:mask="EMPLOYEES_MASK"
|
||||||
|
:readonly="isValidated('information')"
|
||||||
|
:error="informationErrors.errors.employeesCount"
|
||||||
|
/>
|
||||||
|
<MalioInputAmount
|
||||||
|
v-model="information.revenueAmount"
|
||||||
|
:label="t('commercial.suppliers.form.information.revenueAmount')"
|
||||||
|
:readonly="isValidated('information')"
|
||||||
|
:error="informationErrors.errors.revenueAmount"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="information.directorName"
|
||||||
|
:label="t('commercial.suppliers.form.information.directorName')"
|
||||||
|
:readonly="isValidated('information')"
|
||||||
|
:error="informationErrors.errors.directorName"
|
||||||
|
/>
|
||||||
|
<MalioInputAmount
|
||||||
|
v-model="information.profitAmount"
|
||||||
|
:label="t('commercial.suppliers.form.information.profitAmount')"
|
||||||
|
:readonly="isValidated('information')"
|
||||||
|
:error="informationErrors.errors.profitAmount"
|
||||||
|
/>
|
||||||
|
<!-- Volume previsionnel : specifique fournisseur. Champ texte
|
||||||
|
masque (chiffres uniquement) ; l'entier est resolu au PATCH. -->
|
||||||
|
<MalioInputText
|
||||||
|
v-model="information.volumeForecast"
|
||||||
|
:label="t('commercial.suppliers.form.information.volumeForecast')"
|
||||||
|
:mask="VOLUME_FORECAST_MASK"
|
||||||
|
:readonly="isValidated('information')"
|
||||||
|
:error="informationErrors.errors.volumeForecast"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div v-if="!isValidated('information')" class="mt-12 flex justify-center">
|
||||||
|
<MalioButton
|
||||||
|
variant="primary"
|
||||||
|
:label="t('commercial.suppliers.form.submit')"
|
||||||
|
:disabled="tabSubmitting || supplierId === null"
|
||||||
|
@click="submitInformation"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglet Contacts -->
|
||||||
|
<template #contacts>
|
||||||
|
<div class="mt-12 flex flex-col gap-6">
|
||||||
|
<SupplierContactBlock
|
||||||
|
v-for="(contact, index) in contacts"
|
||||||
|
:key="index"
|
||||||
|
:model-value="contact"
|
||||||
|
:title="t('commercial.suppliers.form.contact.title', { n: index + 1 })"
|
||||||
|
:removable="index > 0"
|
||||||
|
:readonly="isValidated('contacts')"
|
||||||
|
:errors="contactErrors[index]"
|
||||||
|
@update:model-value="(v) => contacts[index] = v"
|
||||||
|
@remove="askRemoveContact(index)"
|
||||||
|
/>
|
||||||
|
<div v-if="!isValidated('contacts')" class="flex justify-center gap-6">
|
||||||
|
<MalioButton
|
||||||
|
variant="secondary"
|
||||||
|
icon-name="mdi:add-bold"
|
||||||
|
icon-position="left"
|
||||||
|
:label="t('commercial.suppliers.form.contact.add')"
|
||||||
|
:disabled="!canAddContact"
|
||||||
|
@click="addContact"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
variant="primary"
|
||||||
|
:label="t('commercial.suppliers.form.submit')"
|
||||||
|
:disabled="tabSubmitting"
|
||||||
|
@click="submitContacts"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglet Adresses -->
|
||||||
|
<template #addresses>
|
||||||
|
<div class="mt-12 flex flex-col gap-6">
|
||||||
|
<SupplierAddressBlock
|
||||||
|
v-for="(address, index) in addresses"
|
||||||
|
:key="index"
|
||||||
|
:model-value="address"
|
||||||
|
:title="t('commercial.suppliers.form.address.title', { n: index + 1 })"
|
||||||
|
:category-options="referentials.categories.value"
|
||||||
|
:site-options="referentials.sites.value"
|
||||||
|
:contact-options="contactOptions"
|
||||||
|
:country-options="countryOptions"
|
||||||
|
:removable="index > 0"
|
||||||
|
:readonly="isValidated('addresses')"
|
||||||
|
:errors="addressErrors[index]"
|
||||||
|
@update:model-value="(v) => addresses[index] = v"
|
||||||
|
@remove="askRemoveAddress(index)"
|
||||||
|
@degraded="onAddressDegraded"
|
||||||
|
/>
|
||||||
|
<div v-if="!isValidated('addresses')" class="flex justify-center gap-6">
|
||||||
|
<MalioButton
|
||||||
|
variant="secondary"
|
||||||
|
icon-name="mdi:add-bold"
|
||||||
|
icon-position="left"
|
||||||
|
:label="t('commercial.suppliers.form.address.add')"
|
||||||
|
:disabled="!canAddAddress"
|
||||||
|
@click="addAddress"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
variant="primary"
|
||||||
|
:label="t('commercial.suppliers.form.submit')"
|
||||||
|
:disabled="tabSubmitting"
|
||||||
|
@click="submitAddresses"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglet Comptabilite (present uniquement si accounting.view) -->
|
||||||
|
<template v-if="canAccountingView" #accounting>
|
||||||
|
<div class="mt-12 flex flex-col gap-6">
|
||||||
|
<div class="bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]">
|
||||||
|
<div class="grid grid-cols-4 gap-x-[44px] gap-y-4">
|
||||||
|
<MalioInputText
|
||||||
|
v-model="accounting.siren"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.siren')"
|
||||||
|
:mask="SIREN_MASK"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.siren"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="accounting.accountNumber"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.accountNumber')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.accountNumber"
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="accounting.tvaModeIri"
|
||||||
|
:options="referentials.tvaModes.value"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.tvaMode')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
empty-option-label=""
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.tvaMode"
|
||||||
|
@update:model-value="(v: string | number | null) => accounting.tvaModeIri = v === null ? null : String(v)"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="accounting.nTva"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.nTva')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.nTva"
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="accounting.paymentDelayIri"
|
||||||
|
:options="referentials.paymentDelays.value"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.paymentDelay')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
empty-option-label=""
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.paymentDelay"
|
||||||
|
@update:model-value="(v: string | number | null) => accounting.paymentDelayIri = v === null ? null : String(v)"
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
:model-value="accounting.paymentTypeIri"
|
||||||
|
:options="referentials.paymentTypes.value"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.paymentType')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
empty-option-label=""
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.paymentType"
|
||||||
|
@update:model-value="onPaymentTypeChange"
|
||||||
|
/>
|
||||||
|
<MalioSelect
|
||||||
|
v-if="isBankRequired"
|
||||||
|
:model-value="accounting.bankIri"
|
||||||
|
:options="referentials.banks.value"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.bank')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
empty-option-label=""
|
||||||
|
:required="true"
|
||||||
|
:error="accountingErrors.errors.bank"
|
||||||
|
@update:model-value="(v: string | number | null) => accounting.bankIri = v === null ? null : String(v)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Blocs RIB — affiches uniquement si type de reglement = LCR (RG-2.08). -->
|
||||||
|
<div
|
||||||
|
v-for="(rib, index) in visibleRibs"
|
||||||
|
:key="index"
|
||||||
|
class="relative bg-white py-4 pl-[28px] pr-[60px] shadow-[0_4px_4px_0_rgba(0,0,0,0.25)]"
|
||||||
|
>
|
||||||
|
<MalioButtonIcon
|
||||||
|
v-if="!accountingReadonly && visibleRibs.length > 1"
|
||||||
|
icon="mdi:delete-outline"
|
||||||
|
variant="ghost"
|
||||||
|
button-class="absolute top-3 right-3"
|
||||||
|
v-bind="{ ariaLabel: t('commercial.suppliers.form.accounting.removeRib') }"
|
||||||
|
@click="askRemoveRib(index)"
|
||||||
|
/>
|
||||||
|
<div class="grid grid-cols-4 gap-x-[44px] gap-y-4">
|
||||||
|
<MalioInputText
|
||||||
|
v-model="rib.label"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.ribLabel')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="isRibRequired"
|
||||||
|
:error="ribErrors[index]?.label"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="rib.bic"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.ribBic')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="isRibRequired"
|
||||||
|
:error="ribErrors[index]?.bic"
|
||||||
|
/>
|
||||||
|
<MalioInputText
|
||||||
|
v-model="rib.iban"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.ribIban')"
|
||||||
|
:readonly="accountingReadonly"
|
||||||
|
:required="isRibRequired"
|
||||||
|
:error="ribErrors[index]?.iban"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="!accountingReadonly" class="flex justify-center gap-6">
|
||||||
|
<MalioButton
|
||||||
|
v-if="isRibRequired"
|
||||||
|
variant="secondary"
|
||||||
|
icon-name="mdi:add-bold"
|
||||||
|
icon-position="left"
|
||||||
|
:label="t('commercial.suppliers.form.accounting.addRib')"
|
||||||
|
:disabled="!canAddRib"
|
||||||
|
@click="addRib"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
variant="primary"
|
||||||
|
:label="t('commercial.suppliers.form.submit')"
|
||||||
|
:disabled="tabSubmitting"
|
||||||
|
@click="submitAccounting"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Onglet placeholder : frame vide, passage automatique. -->
|
||||||
|
<template #transport><ComingSoonPlaceholder /></template>
|
||||||
|
</MalioTabList>
|
||||||
|
|
||||||
|
<!-- Modal de confirmation generique (suppression contact/adresse/RIB). -->
|
||||||
|
<MalioModal v-model="confirmModal.open" modal-class="max-w-md">
|
||||||
|
<template #header>
|
||||||
|
<h2 class="text-[24px] font-bold">{{ t('commercial.suppliers.form.confirmDelete.title') }}</h2>
|
||||||
|
</template>
|
||||||
|
<p>{{ confirmModal.message }}</p>
|
||||||
|
<template #footer>
|
||||||
|
<MalioButton
|
||||||
|
variant="secondary"
|
||||||
|
button-class="flex-1"
|
||||||
|
:label="t('commercial.suppliers.form.confirmDelete.cancel')"
|
||||||
|
@click="confirmModal.open = false"
|
||||||
|
/>
|
||||||
|
<MalioButton
|
||||||
|
variant="danger"
|
||||||
|
button-class="flex-1"
|
||||||
|
:label="t('commercial.suppliers.form.confirmDelete.confirm')"
|
||||||
|
@click="runConfirm"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</MalioModal>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||||
|
import { useSupplierReferentials, type RefOption } from '~/modules/commercial/composables/useSupplierReferentials'
|
||||||
|
import { useSupplierFormErrors } from '~/modules/commercial/composables/useSupplierFormErrors'
|
||||||
|
import {
|
||||||
|
buildSupplierFormTabKeys,
|
||||||
|
SUPPLIER_FORM_PLACEHOLDER_TABS,
|
||||||
|
isAddressValid,
|
||||||
|
isBankRequiredForPaymentType,
|
||||||
|
isContactBlank,
|
||||||
|
isContactNamed,
|
||||||
|
isRibBlank,
|
||||||
|
isRibComplete,
|
||||||
|
isRibRequiredForPaymentType,
|
||||||
|
lastFillableTabKey,
|
||||||
|
} from '~/modules/commercial/utils/supplierFormRules'
|
||||||
|
import {
|
||||||
|
buildAccountingPayload,
|
||||||
|
buildAddressPayload,
|
||||||
|
buildContactPayload,
|
||||||
|
buildInformationPayload,
|
||||||
|
buildMainPayload,
|
||||||
|
buildRibPayload,
|
||||||
|
} from '~/modules/commercial/utils/supplierEdit'
|
||||||
|
import {
|
||||||
|
emptyAddress,
|
||||||
|
emptyContact,
|
||||||
|
emptyRib,
|
||||||
|
type SupplierAddressFormDraft,
|
||||||
|
type SupplierContactFormDraft,
|
||||||
|
type SupplierRibFormDraft,
|
||||||
|
} from '~/modules/commercial/types/supplierForm'
|
||||||
|
import { extractApiErrorMessage } from '~/shared/utils/api'
|
||||||
|
|
||||||
|
// Masques de saisie (la normalisation finale reste serveur).
|
||||||
|
const SIREN_MASK = '#########'
|
||||||
|
const EMPLOYEES_MASK = '#######'
|
||||||
|
// Volume previsionnel : champ texte borne aux chiffres (entier >= 0 cote back).
|
||||||
|
const VOLUME_FORECAST_MASK = '##########'
|
||||||
|
|
||||||
|
const { t } = useI18n()
|
||||||
|
const api = useApi()
|
||||||
|
const toast = useToast()
|
||||||
|
const router = useRouter()
|
||||||
|
const { can } = usePermissions()
|
||||||
|
|
||||||
|
/** Retour vers le repertoire fournisseurs (fleche d'en-tete). */
|
||||||
|
function goBack(): void {
|
||||||
|
router.push('/suppliers')
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message d'erreur a afficher dans un toast a partir d'une erreur d'API. Retourne
|
||||||
|
* TOUJOURS une chaine (le composant de toast plante sur `undefined`).
|
||||||
|
*/
|
||||||
|
function apiErrorMessage(error: unknown): string {
|
||||||
|
const data = (error as { data?: unknown })?.data
|
||||||
|
return extractApiErrorMessage(data) || t('commercial.suppliers.toast.error')
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Erreurs de validation par champ (ERP-101) ───────────────────────────────
|
||||||
|
const {
|
||||||
|
mainErrors,
|
||||||
|
informationErrors,
|
||||||
|
accountingErrors,
|
||||||
|
contactErrors,
|
||||||
|
addressErrors,
|
||||||
|
ribErrors,
|
||||||
|
submitRows,
|
||||||
|
} = useSupplierFormErrors()
|
||||||
|
|
||||||
|
useHead({ title: t('commercial.suppliers.form.title') })
|
||||||
|
|
||||||
|
// Gating de la route : la creation est reservee a `manage`. Compta (accounting
|
||||||
|
// seul) et Usine sont rediriges vers le repertoire.
|
||||||
|
if (!can('commercial.suppliers.manage')) {
|
||||||
|
await navigateTo('/suppliers')
|
||||||
|
}
|
||||||
|
|
||||||
|
const canAccountingView = computed(() => can('commercial.suppliers.accounting.view'))
|
||||||
|
const canAccountingManage = computed(() => can('commercial.suppliers.accounting.manage'))
|
||||||
|
|
||||||
|
const referentials = useSupplierReferentials()
|
||||||
|
|
||||||
|
// ── Etat du fournisseur cree ────────────────────────────────────────────────
|
||||||
|
const supplierId = ref<number | null>(null)
|
||||||
|
const mainLocked = ref(false)
|
||||||
|
const mainSubmitting = ref(false)
|
||||||
|
const tabSubmitting = ref(false)
|
||||||
|
|
||||||
|
// ── Formulaire principal ────────────────────────────────────────────────────
|
||||||
|
const main = reactive({
|
||||||
|
companyName: null as string | null,
|
||||||
|
categoryIris: [] as string[],
|
||||||
|
})
|
||||||
|
|
||||||
|
/** POST /suppliers (groupe supplier:write:main). Au succes : verrouille + bascule Information. */
|
||||||
|
async function submitMain(): Promise<void> {
|
||||||
|
if (mainSubmitting.value) return
|
||||||
|
mainSubmitting.value = true
|
||||||
|
mainErrors.clearErrors()
|
||||||
|
try {
|
||||||
|
const created = await api.post<SupplierResponse>('/suppliers', buildMainPayload(main), {
|
||||||
|
headers: { Accept: 'application/ld+json' },
|
||||||
|
toast: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
supplierId.value = created.id
|
||||||
|
// Reaffiche la valeur normalisee renvoyee par le serveur (UPPERCASE, RG-2.12).
|
||||||
|
main.companyName = created.companyName ?? main.companyName
|
||||||
|
|
||||||
|
mainLocked.value = true
|
||||||
|
// Information est facultatif : on deverrouille jusqu'a Contacts (index 1).
|
||||||
|
unlockedIndex.value = tabIndex('contacts')
|
||||||
|
activeTab.value = 'information'
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.createSuccess') })
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
// 409 = doublon nom de societe (RG d'unicite) → erreur inline + toast ;
|
||||||
|
// 422 → mapping inline par champ ; autre → toast de fallback (ERP-101).
|
||||||
|
const status = (error as { response?: { status?: number } })?.response?.status
|
||||||
|
if (status === 409) {
|
||||||
|
const message = t('commercial.suppliers.form.duplicateCompany')
|
||||||
|
mainErrors.setError('companyName', message)
|
||||||
|
toast.error({ title: t('commercial.suppliers.toast.error'), message })
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mainErrors.handleApiError(error, { fallbackMessage: t('commercial.suppliers.toast.error') })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
mainSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Onglets : ordre + gating progressif ─────────────────────────────────────
|
||||||
|
const activeTab = ref('information')
|
||||||
|
// Index du dernier onglet deverrouille (-1 tant que le fournisseur n'est pas cree).
|
||||||
|
const unlockedIndex = ref(-1)
|
||||||
|
// Onglets valides (passent en lecture seule).
|
||||||
|
const validated = reactive<Record<string, boolean>>({})
|
||||||
|
|
||||||
|
const tabKeys = computed(() => buildSupplierFormTabKeys(canAccountingView.value))
|
||||||
|
|
||||||
|
// Dernier onglet REMPLISSABLE par le role : sa validation cloture l'ajout.
|
||||||
|
const lastFillableTab = computed(() => lastFillableTabKey(tabKeys.value))
|
||||||
|
|
||||||
|
// Icone (Iconify) affichee dans l'onglet, par cle.
|
||||||
|
const TAB_ICONS: Record<string, string> = {
|
||||||
|
information: 'mdi:account-outline',
|
||||||
|
contacts: 'mdi:account-box-plus-outline',
|
||||||
|
addresses: 'mdi:map-marker-outline',
|
||||||
|
transport: 'mdi:truck-delivery-outline',
|
||||||
|
accounting: 'mdi:bank-circle-outline',
|
||||||
|
}
|
||||||
|
|
||||||
|
const tabs = computed(() => tabKeys.value.map((key, index) => ({
|
||||||
|
key,
|
||||||
|
label: t(`commercial.suppliers.tab.${key}`),
|
||||||
|
icon: TAB_ICONS[key],
|
||||||
|
disabled: index > unlockedIndex.value,
|
||||||
|
})))
|
||||||
|
|
||||||
|
function isValidated(key: string): boolean {
|
||||||
|
return validated[key] === true
|
||||||
|
}
|
||||||
|
|
||||||
|
function tabIndex(key: string): number {
|
||||||
|
return tabKeys.value.indexOf(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marque l'onglet valide. Si c'est le dernier onglet remplissable, l'ajout est
|
||||||
|
* termine : toast final + redirection vers la liste, et on retourne true. Sinon,
|
||||||
|
* deverrouille et avance a l'onglet suivant, et retourne false.
|
||||||
|
*/
|
||||||
|
function completeTab(key: string): boolean {
|
||||||
|
validated[key] = true
|
||||||
|
if (key === lastFillableTab.value) {
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.addComplete') })
|
||||||
|
router.push('/suppliers')
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
const next = tabKeys.value[tabIndex(key) + 1]
|
||||||
|
unlockedIndex.value = Math.max(unlockedIndex.value, tabIndex(key) + 1)
|
||||||
|
if (next) activeTab.value = next
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Passage automatique sur les onglets coquille (Transport).
|
||||||
|
watch(activeTab, (key) => {
|
||||||
|
if ((SUPPLIER_FORM_PLACEHOLDER_TABS as readonly string[]).includes(key)) {
|
||||||
|
const next = tabKeys.value[tabIndex(key) + 1]
|
||||||
|
unlockedIndex.value = Math.max(unlockedIndex.value, tabIndex(key) + 1)
|
||||||
|
if (next) activeTab.value = next
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// ── Onglet Information ──────────────────────────────────────────────────────
|
||||||
|
const information = reactive({
|
||||||
|
description: null as string | null,
|
||||||
|
competitors: null as string | null,
|
||||||
|
foundedAt: null as string | null,
|
||||||
|
employeesCount: null as string | null,
|
||||||
|
revenueAmount: null as string | null,
|
||||||
|
profitAmount: null as string | null,
|
||||||
|
directorName: null as string | null,
|
||||||
|
volumeForecast: null as string | null,
|
||||||
|
})
|
||||||
|
|
||||||
|
/** PATCH /suppliers/{id} — mode strict : uniquement les champs du groupe information. */
|
||||||
|
async function submitInformation(): Promise<void> {
|
||||||
|
if (supplierId.value === null || tabSubmitting.value) return
|
||||||
|
tabSubmitting.value = true
|
||||||
|
informationErrors.clearErrors()
|
||||||
|
try {
|
||||||
|
await api.patch(`/suppliers/${supplierId.value}`, buildInformationPayload(information), { toast: false })
|
||||||
|
if (completeTab('information')) return
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
informationErrors.handleApiError(error, { fallbackMessage: t('commercial.suppliers.toast.error') })
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
tabSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Onglet Contacts ─────────────────────────────────────────────────────────
|
||||||
|
const contacts = ref<SupplierContactFormDraft[]>([emptyContact()])
|
||||||
|
|
||||||
|
// « + Nouveau contact » desactive tant que le dernier bloc n'a ni nom ni prenom.
|
||||||
|
const canAddContact = computed(() => {
|
||||||
|
const last = contacts.value[contacts.value.length - 1]
|
||||||
|
return last !== undefined && isContactNamed(last)
|
||||||
|
})
|
||||||
|
|
||||||
|
function addContact(): void {
|
||||||
|
if (canAddContact.value) contacts.value.push(emptyContact())
|
||||||
|
}
|
||||||
|
|
||||||
|
function askRemoveContact(index: number): void {
|
||||||
|
askConfirm(t('commercial.suppliers.form.confirmDelete.contact'), () => {
|
||||||
|
contacts.value.splice(index, 1)
|
||||||
|
contactErrors.value.splice(index, 1)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/** POST/PATCH des contacts sur la sous-ressource /suppliers/{id}/contacts. */
|
||||||
|
async function submitContacts(): Promise<void> {
|
||||||
|
if (supplierId.value === null || tabSubmitting.value) return
|
||||||
|
tabSubmitting.value = true
|
||||||
|
try {
|
||||||
|
// RG-2.13 : au moins un contact requis. Si l'onglet ne contient QUE des
|
||||||
|
// amorces vides, on les soumet pour declencher la 422 RG-2.04 inline.
|
||||||
|
const hasSubmittableContact = contacts.value.some(c => c.id !== null || !isContactBlank(c))
|
||||||
|
const hasError = await submitRows(
|
||||||
|
contacts.value,
|
||||||
|
contactErrors,
|
||||||
|
async (contact) => {
|
||||||
|
const body = buildContactPayload(contact)
|
||||||
|
if (contact.id === null) {
|
||||||
|
const created = await api.post<ContactResponse>(
|
||||||
|
`/suppliers/${supplierId.value}/contacts`,
|
||||||
|
body,
|
||||||
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
|
)
|
||||||
|
contact.id = created.id
|
||||||
|
contact.iri = created['@id'] ?? null
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await api.patch(`/supplier_contacts/${contact.id}`, body, { toast: false })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error => toast.error({ title: t('commercial.suppliers.toast.error'), message: apiErrorMessage(error) }),
|
||||||
|
contact => hasSubmittableContact && contact.id === null && isContactBlank(contact),
|
||||||
|
)
|
||||||
|
if (hasError) return
|
||||||
|
if (completeTab('contacts')) return
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
tabSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Onglet Adresses ─────────────────────────────────────────────────────────
|
||||||
|
const addresses = ref<SupplierAddressFormDraft[]>([emptyAddress()])
|
||||||
|
const addressDegradedNotified = ref(false)
|
||||||
|
|
||||||
|
// Contacts deja crees, rattachables a une adresse (M2M, via leur IRI).
|
||||||
|
const contactOptions = computed<RefOption[]>(() =>
|
||||||
|
contacts.value
|
||||||
|
.filter(c => c.iri !== null)
|
||||||
|
.map(c => ({
|
||||||
|
value: c.iri as string,
|
||||||
|
label: [c.firstName, c.lastName].filter(Boolean).join(' ') || (c.email ?? ''),
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Pays disponibles (France preselectionnee par defaut sur chaque adresse).
|
||||||
|
const countryOptions: RefOption[] = [
|
||||||
|
{ value: 'France', label: 'France' },
|
||||||
|
{ value: 'Espagne', label: 'Espagne' },
|
||||||
|
]
|
||||||
|
|
||||||
|
// « + Adresse » desactive tant que la derniere adresse n'est pas valide.
|
||||||
|
const canAddAddress = computed(() => {
|
||||||
|
const last = addresses.value[addresses.value.length - 1]
|
||||||
|
return last !== undefined && isAddressValid(last)
|
||||||
|
})
|
||||||
|
|
||||||
|
function addAddress(): void {
|
||||||
|
if (canAddAddress.value) addresses.value.push(emptyAddress())
|
||||||
|
}
|
||||||
|
|
||||||
|
function askRemoveAddress(index: number): void {
|
||||||
|
askConfirm(t('commercial.suppliers.form.confirmDelete.address'), () => {
|
||||||
|
addresses.value.splice(index, 1)
|
||||||
|
addressErrors.value.splice(index, 1)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Avertit une seule fois quand l'autocompletion d'adresse bascule en degrade. */
|
||||||
|
function onAddressDegraded(): void {
|
||||||
|
if (addressDegradedNotified.value) return
|
||||||
|
addressDegradedNotified.value = true
|
||||||
|
toast.warning({
|
||||||
|
title: t('commercial.suppliers.toast.error'),
|
||||||
|
message: t('commercial.suppliers.form.address.degraded'),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/** POST/PATCH des adresses sur la sous-ressource /suppliers/{id}/addresses. */
|
||||||
|
async function submitAddresses(): Promise<void> {
|
||||||
|
if (supplierId.value === null || tabSubmitting.value) return
|
||||||
|
tabSubmitting.value = true
|
||||||
|
try {
|
||||||
|
const hasError = await submitRows(
|
||||||
|
addresses.value,
|
||||||
|
addressErrors,
|
||||||
|
async (address) => {
|
||||||
|
const body = buildAddressPayload(address)
|
||||||
|
if (address.id === null) {
|
||||||
|
const created = await api.post<{ id: number }>(
|
||||||
|
`/suppliers/${supplierId.value}/addresses`,
|
||||||
|
body,
|
||||||
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
|
)
|
||||||
|
address.id = created.id
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await api.patch(`/supplier_addresses/${address.id}`, body, { toast: false })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error => toast.error({ title: t('commercial.suppliers.toast.error'), message: apiErrorMessage(error) }),
|
||||||
|
)
|
||||||
|
if (hasError) return
|
||||||
|
if (completeTab('addresses')) return
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
tabSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Onglet Comptabilite ─────────────────────────────────────────────────────
|
||||||
|
const accounting = reactive({
|
||||||
|
siren: null as string | null,
|
||||||
|
accountNumber: null as string | null,
|
||||||
|
tvaModeIri: null as string | null,
|
||||||
|
nTva: null as string | null,
|
||||||
|
paymentDelayIri: null as string | null,
|
||||||
|
paymentTypeIri: null as string | null,
|
||||||
|
bankIri: null as string | null,
|
||||||
|
})
|
||||||
|
const ribs = ref<SupplierRibFormDraft[]>([])
|
||||||
|
|
||||||
|
// L'onglet est editable seulement avec accounting.manage (sinon lecture seule).
|
||||||
|
const accountingReadonly = computed(() => isValidated('accounting') || !canAccountingManage.value)
|
||||||
|
|
||||||
|
// Code du type de reglement selectionne (pour RG-2.07 / RG-2.08).
|
||||||
|
const selectedPaymentTypeCode = computed(() =>
|
||||||
|
referentials.paymentTypes.value.find(p => p.value === accounting.paymentTypeIri)?.code ?? null,
|
||||||
|
)
|
||||||
|
const isBankRequired = computed(() => isBankRequiredForPaymentType(selectedPaymentTypeCode.value))
|
||||||
|
const isRibRequired = computed(() => isRibRequiredForPaymentType(selectedPaymentTypeCode.value))
|
||||||
|
|
||||||
|
// Les blocs RIB ne sont affiches que pour une LCR (RG-2.08).
|
||||||
|
const visibleRibs = computed(() => isRibRequired.value ? ribs.value : [])
|
||||||
|
|
||||||
|
function onPaymentTypeChange(value: string | number | null): void {
|
||||||
|
accounting.paymentTypeIri = value === null ? null : String(value)
|
||||||
|
// La banque n'a de sens que pour un virement : on la vide sinon (RG-2.07).
|
||||||
|
if (!isBankRequired.value) accounting.bankIri = null
|
||||||
|
// Les RIB n'ont de sens que pour une LCR (RG-2.08) : amorce un bloc vide quand
|
||||||
|
// LCR est choisi, vide la liste sinon (pas de RIB fantome soumis).
|
||||||
|
if (isRibRequired.value) {
|
||||||
|
if (ribs.value.length === 0) ribs.value.push(emptyRib())
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ribs.value = []
|
||||||
|
ribErrors.value = []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// « + RIB » desactive tant que le dernier bloc RIB n'est pas complet.
|
||||||
|
const canAddRib = computed(() => {
|
||||||
|
const last = ribs.value[ribs.value.length - 1]
|
||||||
|
return last !== undefined && isRibComplete(last)
|
||||||
|
})
|
||||||
|
|
||||||
|
function addRib(): void {
|
||||||
|
if (canAddRib.value) ribs.value.push(emptyRib())
|
||||||
|
}
|
||||||
|
|
||||||
|
function askRemoveRib(index: number): void {
|
||||||
|
askConfirm(t('commercial.suppliers.form.confirmDelete.rib'), () => {
|
||||||
|
ribs.value.splice(index, 1)
|
||||||
|
ribErrors.value.splice(index, 1)
|
||||||
|
// Garde au moins un bloc RIB visible.
|
||||||
|
if (ribs.value.length === 0) ribs.value.push(emptyRib())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Valide l'onglet Comptabilite : POST/PATCH des RIB sur /suppliers/{id}/ribs PUIS
|
||||||
|
* PATCH des scalaires (groupe supplier:write:accounting). Les RIB d'abord : le back
|
||||||
|
* valide RG-2.08 (LCR => au moins un RIB persiste) sur le PATCH scalaires, les RIB
|
||||||
|
* doivent donc exister en base AVANT. Deux appels distincts (mode strict).
|
||||||
|
*/
|
||||||
|
async function submitAccounting(): Promise<void> {
|
||||||
|
if (supplierId.value === null || tabSubmitting.value) return
|
||||||
|
tabSubmitting.value = true
|
||||||
|
accountingErrors.clearErrors()
|
||||||
|
try {
|
||||||
|
// 1) POST/PATCH des RIB d'abord (erreurs inline par ligne). On ne saute une
|
||||||
|
// amorce neuve vide QUE s'il reste un autre RIB soumettable : sinon (LCR sans
|
||||||
|
// aucun RIB rempli) on la soumet pour declencher la 422 NotBlank inline.
|
||||||
|
const hasSubmittableRib = ribs.value.some(r => r.id !== null || !isRibBlank(r))
|
||||||
|
const ribHasError = await submitRows(
|
||||||
|
ribs.value,
|
||||||
|
ribErrors,
|
||||||
|
async (rib) => {
|
||||||
|
const body = buildRibPayload(rib)
|
||||||
|
if (rib.id === null) {
|
||||||
|
const created = await api.post<{ id: number }>(
|
||||||
|
`/suppliers/${supplierId.value}/ribs`,
|
||||||
|
body,
|
||||||
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
|
)
|
||||||
|
rib.id = created.id
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await api.patch(`/supplier_ribs/${rib.id}`, body, { toast: false })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error => toast.error({ title: t('commercial.suppliers.toast.error'), message: apiErrorMessage(error) }),
|
||||||
|
rib => hasSubmittableRib && rib.id === null && isRibBlank(rib),
|
||||||
|
)
|
||||||
|
if (ribHasError) return
|
||||||
|
|
||||||
|
// 2) PATCH des scalaires comptables (erreurs inline sur leurs champs).
|
||||||
|
try {
|
||||||
|
await api.patch(
|
||||||
|
`/suppliers/${supplierId.value}`,
|
||||||
|
buildAccountingPayload(accounting, isBankRequired.value),
|
||||||
|
{ toast: false },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
accountingErrors.handleApiError(error, { fallbackMessage: t('commercial.suppliers.toast.error') })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (completeTab('accounting')) return
|
||||||
|
toast.success({ title: t('commercial.suppliers.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
tabSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Modal de confirmation generique ─────────────────────────────────────────
|
||||||
|
const confirmModal = reactive({
|
||||||
|
open: false,
|
||||||
|
message: '',
|
||||||
|
action: null as null | (() => void),
|
||||||
|
})
|
||||||
|
|
||||||
|
function askConfirm(message: string, action: () => void): void {
|
||||||
|
confirmModal.message = message
|
||||||
|
confirmModal.action = action
|
||||||
|
confirmModal.open = true
|
||||||
|
}
|
||||||
|
|
||||||
|
function runConfirm(): void {
|
||||||
|
confirmModal.action?.()
|
||||||
|
confirmModal.action = null
|
||||||
|
confirmModal.open = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Types de reponse API ────────────────────────────────────────────────────
|
||||||
|
interface SupplierResponse {
|
||||||
|
id: number
|
||||||
|
companyName: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ContactResponse {
|
||||||
|
'@id'?: string
|
||||||
|
id: number
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
// Echec du chargement des referentiels non bloquant : les selects restent vides.
|
||||||
|
referentials.loadCommon().catch(() => {})
|
||||||
|
})
|
||||||
|
</script>
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
/**
|
||||||
|
* Types « brouillon » de l'ecran « Ajouter un fournisseur » (M2 Commercial).
|
||||||
|
*
|
||||||
|
* Miroir de `types/clientForm.ts` (M1). Ces interfaces decrivent l'etat LOCAL du
|
||||||
|
* formulaire (refs Vue), distinct des DTO de l'API : elles portent en plus des
|
||||||
|
* champs purement UI (`hasSecondaryPhone`) et l'`iri` Hydra des entites creees
|
||||||
|
* (necessaire pour rattacher une adresse a des contacts deja persistes, M2M).
|
||||||
|
* Partage par la page de creation et les blocs `SupplierContactBlock` /
|
||||||
|
* `SupplierAddressBlock` (reutilises par la consultation/modification 95/96).
|
||||||
|
*
|
||||||
|
* Differences M2 vs M1 (cf. spec-front § « Differences notables ») :
|
||||||
|
* - Adresse : type via enum exclusif `addressType` (PROSPECT/DEPART/RENDU,
|
||||||
|
* RG-2.09) — pas de drapeaux isProspect/isDelivery/isBilling.
|
||||||
|
* - Adresse : champs specifiques fournisseur `bennes` (nombre) et
|
||||||
|
* `triageProvider` (prestation de triage). Pas d'email de facturation.
|
||||||
|
* - Pas de relation Distributeur/Courtier ni de triage sur le bloc principal.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** Type d'adresse fournisseur (enum exclusif RG-2.09). */
|
||||||
|
export type SupplierAddressType = 'PROSPECT' | 'DEPART' | 'RENDU'
|
||||||
|
|
||||||
|
/** Un contact du fournisseur (onglet Contacts). */
|
||||||
|
export interface SupplierContactFormDraft {
|
||||||
|
/** Id serveur une fois le contact cree (null tant que non persiste). */
|
||||||
|
id: number | null
|
||||||
|
/** IRI Hydra du contact cree — utilise pour le rattachement M2M cote adresse. */
|
||||||
|
iri: string | null
|
||||||
|
firstName: string | null
|
||||||
|
lastName: string | null
|
||||||
|
jobTitle: string | null
|
||||||
|
phonePrimary: string | null
|
||||||
|
phoneSecondary: string | null
|
||||||
|
email: string | null
|
||||||
|
/** UI : le 2e numero a ete revele via le bouton « + ». */
|
||||||
|
hasSecondaryPhone: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Une adresse du fournisseur (onglet Adresses). */
|
||||||
|
export interface SupplierAddressFormDraft {
|
||||||
|
id: number | null
|
||||||
|
/** Type exclusif Prospect / Depart / Rendu (RG-2.09). null tant que non choisi. */
|
||||||
|
addressType: SupplierAddressType | null
|
||||||
|
country: string
|
||||||
|
postalCode: string | null
|
||||||
|
city: string | null
|
||||||
|
street: string | null
|
||||||
|
streetComplement: string | null
|
||||||
|
/** IRI des categories rattachees (type FOURNISSEUR, RG-2.10). */
|
||||||
|
categoryIris: string[]
|
||||||
|
/** IRI des sites Starseed rattaches (>= 1 obligatoire — RG-2.06). */
|
||||||
|
siteIris: string[]
|
||||||
|
/** IRI des contacts rattaches (= blocs Contact deja crees). */
|
||||||
|
contactIris: string[]
|
||||||
|
/** Nombre de bennes (stepper, defaut 0). Chaine pour MalioInputNumber, convertie au payload. */
|
||||||
|
bennes: string | null
|
||||||
|
/** Prestation de triage (specifique fournisseur, portee par l'adresse — RG). */
|
||||||
|
triageProvider: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Un RIB du fournisseur (onglet Comptabilite). */
|
||||||
|
export interface SupplierRibFormDraft {
|
||||||
|
id: number | null
|
||||||
|
label: string | null
|
||||||
|
bic: string | null
|
||||||
|
iban: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Fabrique un contact vierge. */
|
||||||
|
export function emptyContact(): SupplierContactFormDraft {
|
||||||
|
return {
|
||||||
|
id: null,
|
||||||
|
iri: null,
|
||||||
|
firstName: null,
|
||||||
|
lastName: null,
|
||||||
|
jobTitle: null,
|
||||||
|
phonePrimary: null,
|
||||||
|
phoneSecondary: null,
|
||||||
|
email: null,
|
||||||
|
hasSecondaryPhone: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Fabrique une adresse vierge (pays prerempli « France », 0 benne). */
|
||||||
|
export function emptyAddress(): SupplierAddressFormDraft {
|
||||||
|
return {
|
||||||
|
id: null,
|
||||||
|
addressType: null,
|
||||||
|
country: 'France',
|
||||||
|
postalCode: null,
|
||||||
|
city: null,
|
||||||
|
street: null,
|
||||||
|
streetComplement: null,
|
||||||
|
categoryIris: [],
|
||||||
|
siteIris: [],
|
||||||
|
contactIris: [],
|
||||||
|
bennes: '0',
|
||||||
|
triageProvider: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Fabrique un RIB vierge. */
|
||||||
|
export function emptyRib(): SupplierRibFormDraft {
|
||||||
|
return {
|
||||||
|
id: null,
|
||||||
|
label: null,
|
||||||
|
bic: null,
|
||||||
|
iban: null,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -211,6 +211,38 @@ describe('buildContactPayload / buildAddressPayload / buildRibPayload', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Bug edition : en PATCH (merge), une cle de champ requis OMISE laisse la valeur
|
||||||
|
// serveur inchangee -> faux 200 quand l'utilisateur vide le champ. En `forUpdate`,
|
||||||
|
// on envoie `''` (chaine valide, pas de 400 de type) -> NotBlank 422 inline.
|
||||||
|
describe('forUpdate (EDITION/PATCH) : champ requis vide -> `\'\'` au lieu d\'etre omis', () => {
|
||||||
|
it('buildMainPayload : companyName vide envoye en `\'\'`', () => {
|
||||||
|
const payload = buildMainPayload(mainDraft({ companyName: '' }), { forUpdate: true })
|
||||||
|
expect('companyName' in payload).toBe(true)
|
||||||
|
expect(payload.companyName).toBe('')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('buildAddressPayload : postalCode / city / street vides envoyes en `\'\'`', () => {
|
||||||
|
const address: AddressFormDraft = {
|
||||||
|
id: 7, isProspect: false, isDelivery: true, isBilling: false, isBroker: false, isDistributor: false, country: 'France',
|
||||||
|
postalCode: '', city: null, street: '1 rue X', streetComplement: null,
|
||||||
|
categoryIris: ['/api/categories/2'], siteIris: ['/api/sites/1'], contactIris: [],
|
||||||
|
billingEmail: null, billingEmailSecondary: null, hasSecondaryBillingEmail: false,
|
||||||
|
}
|
||||||
|
const payload = buildAddressPayload(address, false, { forUpdate: true })
|
||||||
|
expect(payload.postalCode).toBe('')
|
||||||
|
expect(payload.city).toBe('')
|
||||||
|
// Un champ requis renseigne reste tel quel.
|
||||||
|
expect(payload.street).toBe('1 rue X')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('buildRibPayload : label / bic vides envoyes en `\'\'`, iban conserve', () => {
|
||||||
|
const payload = buildRibPayload({ id: 4, label: '', bic: null, iban: 'FR7612345' }, { forUpdate: true })
|
||||||
|
expect(payload.label).toBe('')
|
||||||
|
expect(payload.bic).toBe('')
|
||||||
|
expect(payload.iban).toBe('FR7612345')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('mapMainDraft — pre-remplissage bloc principal', () => {
|
describe('mapMainDraft — pre-remplissage bloc principal', () => {
|
||||||
it('resout la relation et extrait les IRI (sans contact inline)', () => {
|
it('resout la relation et extrait les IRI (sans contact inline)', () => {
|
||||||
const client = {
|
const client = {
|
||||||
|
|||||||
@@ -0,0 +1,224 @@
|
|||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import {
|
||||||
|
canEditSupplier,
|
||||||
|
categoryOptionsOf,
|
||||||
|
contactOptionsOf,
|
||||||
|
iriOf,
|
||||||
|
mapAccountingDraft,
|
||||||
|
mapAddressToDraft,
|
||||||
|
mapAddressView,
|
||||||
|
mapContactToDraft,
|
||||||
|
mapRibToDraft,
|
||||||
|
referentialOptionOf,
|
||||||
|
showArchiveAction,
|
||||||
|
showRestoreAction,
|
||||||
|
siteOptionsOf,
|
||||||
|
type SupplierDetail,
|
||||||
|
} from '../supplierConsultation'
|
||||||
|
|
||||||
|
describe('iriOf', () => {
|
||||||
|
it('retourne l\'@id d\'une relation embarquee (objet)', () => {
|
||||||
|
expect(iriOf({ '@id': '/api/payment_types/14', code: 'LCR' })).toBe('/api/payment_types/14')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('retourne la chaine telle quelle si la relation est deja un IRI', () => {
|
||||||
|
expect(iriOf('/api/banks/3')).toBe('/api/banks/3')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('retourne null pour une relation absente (null / undefined / skip_null_values)', () => {
|
||||||
|
expect(iriOf(null)).toBeNull()
|
||||||
|
expect(iriOf(undefined)).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('mapContactToDraft', () => {
|
||||||
|
it('formate les telephones en XX XX XX XX XX et conserve l\'iri', () => {
|
||||||
|
const draft = mapContactToDraft({
|
||||||
|
'@id': '/api/supplier_contacts/39',
|
||||||
|
id: 39,
|
||||||
|
firstName: 'Marie',
|
||||||
|
lastName: 'Martin',
|
||||||
|
jobTitle: 'Responsable achats',
|
||||||
|
phonePrimary: '0612345678',
|
||||||
|
email: 'marie.martin@seed.test',
|
||||||
|
})
|
||||||
|
expect(draft.id).toBe(39)
|
||||||
|
expect(draft.iri).toBe('/api/supplier_contacts/39')
|
||||||
|
expect(draft.phonePrimary).toBe('06 12 34 56 78')
|
||||||
|
expect(draft.hasSecondaryPhone).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('revele le 2e telephone quand phoneSecondary est present', () => {
|
||||||
|
const draft = mapContactToDraft({
|
||||||
|
'@id': '/api/supplier_contacts/40',
|
||||||
|
id: 40,
|
||||||
|
phonePrimary: '0600000000',
|
||||||
|
phoneSecondary: '0611111111',
|
||||||
|
})
|
||||||
|
expect(draft.hasSecondaryPhone).toBe(true)
|
||||||
|
expect(draft.phoneSecondary).toBe('06 11 11 11 11')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('mapAddressToDraft', () => {
|
||||||
|
it('mappe l\'enum addressType, les champs fournisseur et extrait les iris', () => {
|
||||||
|
const draft = mapAddressToDraft({
|
||||||
|
'@id': '/api/supplier_addresses/33',
|
||||||
|
id: 33,
|
||||||
|
addressType: 'DEPART',
|
||||||
|
country: 'France',
|
||||||
|
postalCode: '86000',
|
||||||
|
city: 'Poitiers',
|
||||||
|
street: '12 rue des Acacias',
|
||||||
|
bennes: 3,
|
||||||
|
triageProvider: true,
|
||||||
|
sites: [{ '@id': '/api/sites/87', name: 'Chatellerault', color: '#056CF2' }],
|
||||||
|
categories: [{ '@id': '/api/categories/2279', code: 'NEGOCIANT' }],
|
||||||
|
contacts: [{ '@id': '/api/supplier_contacts/39' }, '/api/supplier_contacts/41'],
|
||||||
|
})
|
||||||
|
expect(draft.addressType).toBe('DEPART')
|
||||||
|
expect(draft.siteIris).toEqual(['/api/sites/87'])
|
||||||
|
expect(draft.categoryIris).toEqual(['/api/categories/2279'])
|
||||||
|
expect(draft.contactIris).toEqual(['/api/supplier_contacts/39', '/api/supplier_contacts/41'])
|
||||||
|
// bennes (entier) → chaine pour MalioInputNumber.
|
||||||
|
expect(draft.bennes).toBe('3')
|
||||||
|
expect(draft.triageProvider).toBe(true)
|
||||||
|
expect(draft.city).toBe('Poitiers')
|
||||||
|
expect(draft.country).toBe('France')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('tolere les champs absents (defauts : France, bennes « 0 », triage faux, type null)', () => {
|
||||||
|
const draft = mapAddressToDraft({ '@id': '/api/supplier_addresses/9', id: 9 })
|
||||||
|
expect(draft.addressType).toBeNull()
|
||||||
|
expect(draft.siteIris).toEqual([])
|
||||||
|
expect(draft.categoryIris).toEqual([])
|
||||||
|
expect(draft.contactIris).toEqual([])
|
||||||
|
expect(draft.country).toBe('France')
|
||||||
|
expect(draft.bennes).toBe('0')
|
||||||
|
expect(draft.triageProvider).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('mapRibToDraft', () => {
|
||||||
|
it('mappe label / bic / iban et l\'id serveur', () => {
|
||||||
|
const draft = mapRibToDraft({ '@id': '/api/supplier_ribs/27', id: 27, label: 'Compte principal', bic: 'BNPAFRPPXXX', iban: 'FR14...' })
|
||||||
|
expect(draft).toEqual({ id: 27, label: 'Compte principal', bic: 'BNPAFRPPXXX', iban: 'FR14...' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('mapAccountingDraft', () => {
|
||||||
|
it('mappe les scalaires et resout les iris des referentiels embarques', () => {
|
||||||
|
const acc = mapAccountingDraft({
|
||||||
|
'@id': '/api/suppliers/85',
|
||||||
|
id: 85,
|
||||||
|
siren: '123456789',
|
||||||
|
accountNumber: 'F0001',
|
||||||
|
nTva: 'FR00123456789',
|
||||||
|
tvaMode: { '@id': '/api/tva_modes/30' },
|
||||||
|
paymentDelay: { '@id': '/api/payment_delays/11' },
|
||||||
|
paymentType: { '@id': '/api/payment_types/14', code: 'LCR' },
|
||||||
|
bank: { '@id': '/api/banks/3' },
|
||||||
|
} as SupplierDetail)
|
||||||
|
expect(acc).toEqual({
|
||||||
|
siren: '123456789',
|
||||||
|
accountNumber: 'F0001',
|
||||||
|
nTva: 'FR00123456789',
|
||||||
|
tvaModeIri: '/api/tva_modes/30',
|
||||||
|
paymentDelayIri: '/api/payment_delays/11',
|
||||||
|
paymentTypeIri: '/api/payment_types/14',
|
||||||
|
bankIri: '/api/banks/3',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renvoie des null quand les champs comptables sont absents (gating par omission, sans accounting.view)', () => {
|
||||||
|
const acc = mapAccountingDraft({} as SupplierDetail)
|
||||||
|
expect(acc).toEqual({
|
||||||
|
siren: null,
|
||||||
|
accountNumber: null,
|
||||||
|
nTva: null,
|
||||||
|
tvaModeIri: null,
|
||||||
|
paymentDelayIri: null,
|
||||||
|
paymentTypeIri: null,
|
||||||
|
bankIri: null,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('options construites depuis l\'embed (role-independantes)', () => {
|
||||||
|
it('categoryOptionsOf expose value=IRI, label=nom, code', () => {
|
||||||
|
expect(categoryOptionsOf([{ '@id': '/api/categories/2279', name: 'Negociant', code: 'NEGOCIANT' }])).toEqual([
|
||||||
|
{ value: '/api/categories/2279', label: 'Negociant', code: 'NEGOCIANT' },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('siteOptionsOf expose value=IRI, label=nom', () => {
|
||||||
|
expect(siteOptionsOf([{ '@id': '/api/sites/87', name: 'Chatellerault', color: '#000' }])).toEqual([
|
||||||
|
{ value: '/api/sites/87', label: 'Chatellerault' },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('contactOptionsOf compose le libelle (nom complet, sinon email)', () => {
|
||||||
|
expect(contactOptionsOf([
|
||||||
|
{ '@id': '/api/supplier_contacts/1', id: 1, firstName: 'Marie', lastName: 'Martin' },
|
||||||
|
{ '@id': '/api/supplier_contacts/2', id: 2, email: 'a@b.fr' },
|
||||||
|
])).toEqual([
|
||||||
|
{ value: '/api/supplier_contacts/1', label: 'Marie Martin' },
|
||||||
|
{ value: '/api/supplier_contacts/2', label: 'a@b.fr' },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('referentialOptionOf : option unique depuis l\'embed, vide pour IRI nu / absent', () => {
|
||||||
|
expect(referentialOptionOf({ '@id': '/api/payment_types/14', label: 'LCR' })).toEqual([
|
||||||
|
{ value: '/api/payment_types/14', label: 'LCR' },
|
||||||
|
])
|
||||||
|
expect(referentialOptionOf('/api/banks/3')).toEqual([])
|
||||||
|
expect(referentialOptionOf(null)).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('mapAddressView assemble brouillon + options propres a l\'adresse', () => {
|
||||||
|
const view = mapAddressView({
|
||||||
|
'@id': '/api/supplier_addresses/33',
|
||||||
|
id: 33,
|
||||||
|
addressType: 'RENDU',
|
||||||
|
city: 'Poitiers',
|
||||||
|
sites: [{ '@id': '/api/sites/87', name: 'Chatellerault' }],
|
||||||
|
categories: [{ '@id': '/api/categories/2279', name: 'Negociant', code: 'NEGOCIANT' }],
|
||||||
|
})
|
||||||
|
expect(view.draft.id).toBe(33)
|
||||||
|
expect(view.draft.addressType).toBe('RENDU')
|
||||||
|
expect(view.siteOptions).toEqual([{ value: '/api/sites/87', label: 'Chatellerault' }])
|
||||||
|
expect(view.categoryOptions).toEqual([{ value: '/api/categories/2279', label: 'Negociant', code: 'NEGOCIANT' }])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('canEditSupplier', () => {
|
||||||
|
const can = (granted: string[]) => (codes: string[]) => codes.some(c => granted.includes(c))
|
||||||
|
|
||||||
|
it('visible pour manage', () => {
|
||||||
|
expect(canEditSupplier(can(['commercial.suppliers.manage']))).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('visible pour accounting.manage (role Compta)', () => {
|
||||||
|
expect(canEditSupplier(can(['commercial.suppliers.accounting.manage']))).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('masque sans aucune des deux permissions (role Usine)', () => {
|
||||||
|
expect(canEditSupplier(can(['commercial.suppliers.view']))).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('showArchiveAction / showRestoreAction', () => {
|
||||||
|
const can = (granted: string[]) => (code: string) => granted.includes(code)
|
||||||
|
|
||||||
|
it('Archiver : visible avec la permission archive ET fournisseur non archive', () => {
|
||||||
|
expect(showArchiveAction(can(['commercial.suppliers.archive']), false)).toBe(true)
|
||||||
|
expect(showArchiveAction(can(['commercial.suppliers.archive']), true)).toBe(false)
|
||||||
|
expect(showArchiveAction(can([]), false)).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Restaurer : visible avec la permission archive ET fournisseur archive', () => {
|
||||||
|
expect(showRestoreAction(can(['commercial.suppliers.archive']), true)).toBe(true)
|
||||||
|
expect(showRestoreAction(can(['commercial.suppliers.archive']), false)).toBe(false)
|
||||||
|
expect(showRestoreAction(can([]), true)).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,218 @@
|
|||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import {
|
||||||
|
buildAccountingPayload,
|
||||||
|
buildAddressPayload,
|
||||||
|
buildContactPayload,
|
||||||
|
buildInformationPayload,
|
||||||
|
buildMainPayload,
|
||||||
|
buildRibPayload,
|
||||||
|
mapAccountingFormDraft,
|
||||||
|
mapInformationDraft,
|
||||||
|
mapMainDraft,
|
||||||
|
resolveTabEditability,
|
||||||
|
} from '../supplierEdit'
|
||||||
|
import type { SupplierDetail } from '~/modules/commercial/utils/supplierConsultation'
|
||||||
|
import { emptyAddress, emptyContact, emptyRib } from '~/modules/commercial/types/supplierForm'
|
||||||
|
|
||||||
|
describe('buildMainPayload (groupe supplier:write:main)', () => {
|
||||||
|
it('envoie companyName + categories quand renseignes', () => {
|
||||||
|
expect(buildMainPayload({ companyName: 'ACME', categoryIris: ['/api/categories/1'] })).toEqual({
|
||||||
|
companyName: 'ACME',
|
||||||
|
categories: ['/api/categories/1'],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('CREATION : omet companyName vide (-> 422 NotBlank, ERP-119)', () => {
|
||||||
|
const payload = buildMainPayload({ companyName: null, categoryIris: [] })
|
||||||
|
expect('companyName' in payload).toBe(false)
|
||||||
|
expect(payload.categories).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('EDITION (forUpdate) : companyName vide envoye en `\'\'` (PATCH -> 422 NotBlank, pas un faux 200)', () => {
|
||||||
|
const payload = buildMainPayload({ companyName: '', categoryIris: [] }, { forUpdate: true })
|
||||||
|
expect('companyName' in payload).toBe(true)
|
||||||
|
expect(payload.companyName).toBe('')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('buildInformationPayload (groupe supplier:write:information)', () => {
|
||||||
|
const base = {
|
||||||
|
description: null, competitors: null, foundedAt: null, employeesCount: null,
|
||||||
|
revenueAmount: null, profitAmount: null, directorName: null, volumeForecast: null,
|
||||||
|
}
|
||||||
|
|
||||||
|
it('convertit employeesCount et volumeForecast en nombre, null si vide', () => {
|
||||||
|
expect(buildInformationPayload({ ...base, employeesCount: '42', volumeForecast: '1000' })).toMatchObject({
|
||||||
|
employeesCount: 42,
|
||||||
|
volumeForecast: 1000,
|
||||||
|
})
|
||||||
|
expect(buildInformationPayload(base)).toMatchObject({ employeesCount: null, volumeForecast: null })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('buildContactPayload (sous-ressource supplier_contact)', () => {
|
||||||
|
it('n\'envoie le 2e telephone que si revele (hasSecondaryPhone)', () => {
|
||||||
|
const contact = { ...emptyContact(), phonePrimary: '0102030405', phoneSecondary: '0607080910' }
|
||||||
|
expect(buildContactPayload({ ...contact, hasSecondaryPhone: false }).phoneSecondary).toBeNull()
|
||||||
|
expect(buildContactPayload({ ...contact, hasSecondaryPhone: true }).phoneSecondary).toBe('0607080910')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('buildAddressPayload (sous-ressource supplier_address — specificites M2)', () => {
|
||||||
|
it('envoie addressType (enum), bennes (nombre) et triageProvider', () => {
|
||||||
|
const address = {
|
||||||
|
...emptyAddress(),
|
||||||
|
addressType: 'RENDU' as const,
|
||||||
|
postalCode: '86100', city: 'Châtellerault', street: '1 rue de la Paix',
|
||||||
|
siteIris: ['/api/sites/1'], categoryIris: ['/api/categories/2'],
|
||||||
|
bennes: '3', triageProvider: true,
|
||||||
|
}
|
||||||
|
expect(buildAddressPayload(address)).toMatchObject({
|
||||||
|
addressType: 'RENDU',
|
||||||
|
bennes: 3,
|
||||||
|
triageProvider: true,
|
||||||
|
sites: ['/api/sites/1'],
|
||||||
|
categories: ['/api/categories/2'],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('bennes null quand le champ est vide', () => {
|
||||||
|
expect(buildAddressPayload({ ...emptyAddress(), bennes: '' }).bennes).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('omet postalCode / city / street vides (-> 422 NotBlank, ERP-119)', () => {
|
||||||
|
const payload = buildAddressPayload({ ...emptyAddress(), addressType: 'PROSPECT' })
|
||||||
|
expect('postalCode' in payload).toBe(false)
|
||||||
|
expect('city' in payload).toBe(false)
|
||||||
|
expect('street' in payload).toBe(false)
|
||||||
|
// Les champs non requis restent presents.
|
||||||
|
expect('streetComplement' in payload).toBe(true)
|
||||||
|
expect(payload.addressType).toBe('PROSPECT')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('omet addressType quand aucun radio n\'est choisi (-> 422 NotBlank au lieu d\'un 400 de type)', () => {
|
||||||
|
// emptyAddress() laisse addressType a null : la cle doit etre absente du
|
||||||
|
// payload pour que le back renvoie une 422 propertyPath addressType.
|
||||||
|
const payload = buildAddressPayload(emptyAddress())
|
||||||
|
expect('addressType' in payload).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('EDITION (forUpdate) : un champ requis vide est envoye en `\'\'` (et NON omis) pour declencher la 422 NotBlank au PATCH', () => {
|
||||||
|
// Bug edition : omettre la cle d'un champ requis vide laisse le PATCH garder
|
||||||
|
// l'ancienne valeur (faux 200). En `forUpdate`, on envoie `''` -> NotBlank 422.
|
||||||
|
const payload = buildAddressPayload({ ...emptyAddress(), addressType: 'DEPART', postalCode: '' }, { forUpdate: true })
|
||||||
|
expect('postalCode' in payload).toBe(true)
|
||||||
|
expect(payload.postalCode).toBe('')
|
||||||
|
// Un champ requis renseigne reste tel quel.
|
||||||
|
expect(payload.addressType).toBe('DEPART')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('n\'expose jamais d\'email de facturation (difference M1)', () => {
|
||||||
|
const payload = buildAddressPayload({ ...emptyAddress(), addressType: 'DEPART' })
|
||||||
|
expect('billingEmail' in payload).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('buildAccountingPayload (groupe supplier:write:accounting)', () => {
|
||||||
|
const base = {
|
||||||
|
siren: '123456789', accountNumber: '00012345678', nTva: 'FR123',
|
||||||
|
tvaModeIri: '/api/tva_modes/1', paymentDelayIri: '/api/payment_delays/1',
|
||||||
|
paymentTypeIri: '/api/payment_types/1', bankIri: '/api/banks/1',
|
||||||
|
}
|
||||||
|
|
||||||
|
it('envoie la banque seulement si requise (VIREMENT, RG-2.07)', () => {
|
||||||
|
expect(buildAccountingPayload(base, true).bank).toBe('/api/banks/1')
|
||||||
|
expect(buildAccountingPayload(base, false).bank).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('buildRibPayload (sous-ressource supplier_rib)', () => {
|
||||||
|
it('omet les champs requis vides (-> 422 NotBlank, ERP-119)', () => {
|
||||||
|
const payload = buildRibPayload({ ...emptyRib(), iban: 'FR1420041010050500013M02606' })
|
||||||
|
expect('label' in payload).toBe(false)
|
||||||
|
expect('bic' in payload).toBe(false)
|
||||||
|
expect(payload.iban).toBe('FR1420041010050500013M02606')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('mapMainDraft — pre-remplissage bloc principal (companyName + categories, pas de relation M2)', () => {
|
||||||
|
it('extrait companyName et les IRI de categories', () => {
|
||||||
|
const draft = mapMainDraft({
|
||||||
|
'@id': '/api/suppliers/85', id: 85,
|
||||||
|
companyName: 'DOD862875',
|
||||||
|
categories: [{ '@id': '/api/categories/2279', code: 'NEGOCIANT' }],
|
||||||
|
} as SupplierDetail)
|
||||||
|
expect(draft.companyName).toBe('DOD862875')
|
||||||
|
expect(draft.categoryIris).toEqual(['/api/categories/2279'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('gere les cles omises (skip_null_values) sans planter', () => {
|
||||||
|
const draft = mapMainDraft({ '@id': '/api/suppliers/2', id: 2 } as SupplierDetail)
|
||||||
|
expect(draft.companyName).toBeNull()
|
||||||
|
expect(draft.categoryIris).toEqual([])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('mapInformationDraft — pre-remplissage onglet Information (+ volumeForecast M2)', () => {
|
||||||
|
it('tronque foundedAt, stringifie employeesCount et volumeForecast', () => {
|
||||||
|
const draft = mapInformationDraft({
|
||||||
|
'@id': '/api/suppliers/85', id: 85,
|
||||||
|
foundedAt: '2008-04-01T00:00:00+02:00', employeesCount: 42, volumeForecast: 8000,
|
||||||
|
} as SupplierDetail)
|
||||||
|
expect(draft.foundedAt).toBe('2008-04-01')
|
||||||
|
expect(draft.employeesCount).toBe('42')
|
||||||
|
expect(draft.volumeForecast).toBe('8000')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('cles omises -> null (volumeForecast inclus)', () => {
|
||||||
|
const draft = mapInformationDraft({ '@id': '/api/suppliers/1', id: 1 } as SupplierDetail)
|
||||||
|
expect(draft.foundedAt).toBeNull()
|
||||||
|
expect(draft.employeesCount).toBeNull()
|
||||||
|
expect(draft.volumeForecast).toBeNull()
|
||||||
|
expect(draft.description).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('mapAccountingFormDraft — pre-remplissage onglet Comptabilite', () => {
|
||||||
|
it('extrait les scalaires et les IRI des referentiels embarques', () => {
|
||||||
|
const draft = mapAccountingFormDraft({
|
||||||
|
'@id': '/api/suppliers/85', id: 85,
|
||||||
|
siren: '123456789', accountNumber: 'F0001', nTva: 'FR00123456789',
|
||||||
|
tvaMode: { '@id': '/api/tva_modes/30', label: 'France (ventes)' },
|
||||||
|
paymentType: '/api/payment_types/14',
|
||||||
|
} as SupplierDetail)
|
||||||
|
expect(draft.siren).toBe('123456789')
|
||||||
|
expect(draft.tvaModeIri).toBe('/api/tva_modes/30')
|
||||||
|
expect(draft.paymentTypeIri).toBe('/api/payment_types/14')
|
||||||
|
expect(draft.bankIri).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('cles comptables absentes (gating par omission) -> scalaires/IRI null', () => {
|
||||||
|
const draft = mapAccountingFormDraft({ '@id': '/api/suppliers/1', id: 1 } as SupplierDetail)
|
||||||
|
expect(draft.siren).toBeNull()
|
||||||
|
expect(draft.tvaModeIri).toBeNull()
|
||||||
|
expect(draft.bankIri).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('resolveTabEditability — gating par role (matrice § 2.7)', () => {
|
||||||
|
it('Admin : tout editable', () => {
|
||||||
|
expect(resolveTabEditability({ canManage: true, canAccountingView: true, canAccountingManage: true }))
|
||||||
|
.toEqual({ businessEditable: true, accountingVisible: true, accountingEditable: true })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Bureau / Commerciale (manage seul) : metier editable, Comptabilite masquee', () => {
|
||||||
|
expect(resolveTabEditability({ canManage: true, canAccountingView: false, canAccountingManage: false }))
|
||||||
|
.toEqual({ businessEditable: true, accountingVisible: false, accountingEditable: false })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Compta (accounting seul) : metier readonly, Comptabilite editable', () => {
|
||||||
|
expect(resolveTabEditability({ canManage: false, canAccountingView: true, canAccountingManage: true }))
|
||||||
|
.toEqual({ businessEditable: false, accountingVisible: true, accountingEditable: true })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Sans permission d\'edition : rien d\'editable', () => {
|
||||||
|
expect(resolveTabEditability({ canManage: false, canAccountingView: false, canAccountingManage: false }))
|
||||||
|
.toEqual({ businessEditable: false, accountingVisible: false, accountingEditable: false })
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,190 @@
|
|||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import {
|
||||||
|
buildSupplierFormTabKeys,
|
||||||
|
hasAtLeastOneValidContact,
|
||||||
|
isAddressValid,
|
||||||
|
isBankRequiredForPaymentType,
|
||||||
|
isBlankRow,
|
||||||
|
isContactBlank,
|
||||||
|
isContactNamed,
|
||||||
|
isRibBlank,
|
||||||
|
isRibComplete,
|
||||||
|
isRibRequiredForPaymentType,
|
||||||
|
lastFillableTabKey,
|
||||||
|
omitEmptyRequired,
|
||||||
|
type AddressValidityDraft,
|
||||||
|
type ContactDraft,
|
||||||
|
type ContactFillableDraft,
|
||||||
|
} from '../supplierFormRules'
|
||||||
|
|
||||||
|
/** Bloc contact totalement vide (amorce par defaut). */
|
||||||
|
function blankContact(): ContactFillableDraft {
|
||||||
|
return {
|
||||||
|
firstName: null,
|
||||||
|
lastName: null,
|
||||||
|
jobTitle: null,
|
||||||
|
phonePrimary: null,
|
||||||
|
phoneSecondary: null,
|
||||||
|
email: null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('buildSupplierFormTabKeys (gating onglet Comptabilite + onglets edit-only)', () => {
|
||||||
|
it('inclut l onglet accounting si l utilisateur a accounting.view', () => {
|
||||||
|
expect(buildSupplierFormTabKeys(true)).toContain('accounting')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('exclut l onglet accounting sinon (Bureau / Commerciale)', () => {
|
||||||
|
expect(buildSupplierFormTabKeys(false)).not.toContain('accounting')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('a la creation, ordre = information / contacts / addresses / transport (+ accounting si vu)', () => {
|
||||||
|
expect(buildSupplierFormTabKeys(true)).toEqual(['information', 'contacts', 'addresses', 'transport', 'accounting'])
|
||||||
|
expect(buildSupplierFormTabKeys(false)).toEqual(['information', 'contacts', 'addresses', 'transport'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('a la creation, exclut Statistiques / Rapports / Echanges', () => {
|
||||||
|
const keys = buildSupplierFormTabKeys(true)
|
||||||
|
expect(keys).not.toContain('statistics')
|
||||||
|
expect(keys).not.toContain('reports')
|
||||||
|
expect(keys).not.toContain('exchanges')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('en modification (includeEditOnlyTabs), ajoute les onglets edit-only en fin', () => {
|
||||||
|
expect(buildSupplierFormTabKeys(true, { includeEditOnlyTabs: true })).toEqual([
|
||||||
|
'information', 'contacts', 'addresses', 'transport', 'accounting', 'statistics', 'reports', 'exchanges',
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('lastFillableTabKey (redirection fin d\'ajout, role-aware)', () => {
|
||||||
|
it('addresses pour un role sans Comptabilite (Bureau / Commerciale)', () => {
|
||||||
|
expect(lastFillableTabKey(buildSupplierFormTabKeys(false))).toBe('addresses')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accounting pour un role avec accounting.view (Admin)', () => {
|
||||||
|
expect(lastFillableTabKey(buildSupplierFormTabKeys(true))).toBe('accounting')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('ignore les onglets placeholder (Transport en dernier ne compte pas)', () => {
|
||||||
|
expect(lastFillableTabKey(['information', 'contacts', 'addresses', 'transport'])).toBe('addresses')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('undefined si aucun onglet remplissable (que des placeholders)', () => {
|
||||||
|
expect(lastFillableTabKey(['transport', 'statistics'])).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('isContactNamed (RG-2.04)', () => {
|
||||||
|
it('vrai si le prenom ou le nom est renseigne', () => {
|
||||||
|
expect(isContactNamed({ firstName: 'Alice', lastName: null })).toBe(true)
|
||||||
|
expect(isContactNamed({ firstName: null, lastName: 'Martin' })).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux si les deux sont vides ou espaces uniquement', () => {
|
||||||
|
expect(isContactNamed({ firstName: null, lastName: null })).toBe(false)
|
||||||
|
expect(isContactNamed({ firstName: ' ', lastName: '' })).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('hasAtLeastOneValidContact (RG-2.13)', () => {
|
||||||
|
it('faux sur une liste vide ou sans contact nomme', () => {
|
||||||
|
expect(hasAtLeastOneValidContact([])).toBe(false)
|
||||||
|
const contacts: ContactDraft[] = [{ firstName: null, lastName: null }, { firstName: '', lastName: ' ' }]
|
||||||
|
expect(hasAtLeastOneValidContact(contacts)).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('vrai des qu un contact a un nom ou un prenom', () => {
|
||||||
|
expect(hasAtLeastOneValidContact([{ firstName: null, lastName: null }, { firstName: 'Bob', lastName: null }])).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('isBlankRow / isContactBlank / isRibBlank (blocs vides vs partiels)', () => {
|
||||||
|
it('isBlankRow vrai si toutes les valeurs sont vides', () => {
|
||||||
|
expect(isBlankRow([null, undefined, '', ' '])).toBe(true)
|
||||||
|
expect(isBlankRow([null, 'x', ''])).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('isContactBlank faux si un email seul est saisi (bloc a soumettre -> 422 RG-2.04 inline)', () => {
|
||||||
|
expect(isContactBlank(blankContact())).toBe(true)
|
||||||
|
expect(isContactBlank({ ...blankContact(), email: 'jean@acme.fr' })).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('isRibBlank faux si un IBAN seul est saisi (bloc a soumettre -> 422 NotBlank inline)', () => {
|
||||||
|
expect(isRibBlank({ label: null, bic: null, iban: null })).toBe(true)
|
||||||
|
expect(isRibBlank({ label: null, bic: null, iban: 'FR1420041010050500013M02606' })).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('isRibComplete (gating « + RIB » + RG-2.08)', () => {
|
||||||
|
it('vrai quand label + BIC + IBAN sont remplis', () => {
|
||||||
|
expect(isRibComplete({ label: 'Compte courant', bic: 'BNPAFRPP', iban: 'FR1420041010050500013M02606' })).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux si un champ manque', () => {
|
||||||
|
expect(isRibComplete({ label: null, bic: 'BNPAFRPP', iban: 'FR14...' })).toBe(false)
|
||||||
|
expect(isRibComplete({ label: 'Compte', bic: ' ', iban: 'FR14...' })).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('regles type de reglement (RG-2.07 / RG-2.08)', () => {
|
||||||
|
it('banque obligatoire si VIREMENT', () => {
|
||||||
|
expect(isBankRequiredForPaymentType('VIREMENT')).toBe(true)
|
||||||
|
expect(isBankRequiredForPaymentType('LCR')).toBe(false)
|
||||||
|
expect(isBankRequiredForPaymentType(null)).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('RIB obligatoire si LCR', () => {
|
||||||
|
expect(isRibRequiredForPaymentType('LCR')).toBe(true)
|
||||||
|
expect(isRibRequiredForPaymentType('VIREMENT')).toBe(false)
|
||||||
|
expect(isRibRequiredForPaymentType(null)).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('isAddressValid (enum addressType, RG-2.06/2.09/2.10 ; pas d\'email facturation)', () => {
|
||||||
|
function validAddress(): AddressValidityDraft {
|
||||||
|
return {
|
||||||
|
addressType: 'DEPART',
|
||||||
|
categoryIris: ['/api/categories/1'],
|
||||||
|
siteIris: ['/api/sites/1'],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it('vrai quand type + >= 1 site + >= 1 categorie', () => {
|
||||||
|
expect(isAddressValid(validAddress())).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux si le type d\'adresse n\'est pas renseigne (amorce vierge)', () => {
|
||||||
|
expect(isAddressValid({ ...validAddress(), addressType: null })).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux si aucun site (RG-2.06)', () => {
|
||||||
|
expect(isAddressValid({ ...validAddress(), siteIris: [] })).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux si aucune categorie (RG-2.10)', () => {
|
||||||
|
expect(isAddressValid({ ...validAddress(), categoryIris: [] })).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepte les trois valeurs d\'enum PROSPECT / DEPART / RENDU', () => {
|
||||||
|
for (const type of ['PROSPECT', 'DEPART', 'RENDU'] as const) {
|
||||||
|
expect(isAddressValid({ ...validAddress(), addressType: type })).toBe(true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('omitEmptyRequired (ERP-119 : 422 NotBlank au lieu de 400 de type)', () => {
|
||||||
|
it('retire les cles requises vides et conserve le reste', () => {
|
||||||
|
const payload = omitEmptyRequired(
|
||||||
|
{ companyName: null, sites: ['/api/sites/1'] },
|
||||||
|
['companyName'],
|
||||||
|
)
|
||||||
|
expect('companyName' in payload).toBe(false)
|
||||||
|
expect(payload.sites).toEqual(['/api/sites/1'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('false / 0 ne sont pas consideres vides (booleens / nombres preserves)', () => {
|
||||||
|
const payload = omitEmptyRequired({ triageProvider: false, bennes: 0 }, ['triageProvider', 'bennes'])
|
||||||
|
expect(payload).toEqual({ triageProvider: false, bennes: 0 })
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -23,6 +23,7 @@ import {
|
|||||||
} from '~/modules/commercial/utils/clientConsultation'
|
} from '~/modules/commercial/utils/clientConsultation'
|
||||||
import {
|
import {
|
||||||
ADDRESS_REQUIRED_NON_NULLABLE_KEYS,
|
ADDRESS_REQUIRED_NON_NULLABLE_KEYS,
|
||||||
|
blankEmptyRequired,
|
||||||
MAIN_REQUIRED_NON_NULLABLE_KEYS,
|
MAIN_REQUIRED_NON_NULLABLE_KEYS,
|
||||||
omitEmptyRequired,
|
omitEmptyRequired,
|
||||||
RIB_REQUIRED_NON_NULLABLE_KEYS,
|
RIB_REQUIRED_NON_NULLABLE_KEYS,
|
||||||
@@ -139,12 +140,35 @@ export function mapAccountingFormDraft(client: ClientDetail): AccountingFormDraf
|
|||||||
|
|
||||||
// ── Scoping strict des payloads PATCH ────────────────────────────────────────
|
// ── Scoping strict des payloads PATCH ────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options de construction d'un payload d'ecriture.
|
||||||
|
* - `forUpdate: false` (defaut, CREATION/POST) : champs requis vides OMIS -> 422
|
||||||
|
* NotBlank (le back ne reçoit pas la cle, la propriete garde son defaut).
|
||||||
|
* - `forUpdate: true` (EDITION/PATCH d'une ligne existante) : champs requis vides
|
||||||
|
* envoyes en `''` -> 422 NotBlank (sinon une cle omise laisse la valeur serveur
|
||||||
|
* inchangee, faux 200 — cf. blankEmptyRequired).
|
||||||
|
*/
|
||||||
|
export interface BuildPayloadOptions {
|
||||||
|
forUpdate?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Selectionne le finaliseur des champs requis selon création (omit) vs édition (blank). */
|
||||||
|
function finalizeRequired<T extends Record<string, unknown>>(
|
||||||
|
payload: T,
|
||||||
|
requiredKeys: readonly string[],
|
||||||
|
options: BuildPayloadOptions,
|
||||||
|
): T {
|
||||||
|
return options.forUpdate
|
||||||
|
? blankEmptyRequired(payload, requiredKeys)
|
||||||
|
: omitEmptyRequired(payload, requiredKeys)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Payload du bloc principal — groupe client:write:main UNIQUEMENT. La relation
|
* Payload du bloc principal — groupe client:write:main UNIQUEMENT. La relation
|
||||||
* Distributeur/Courtier est mutuellement exclusive (RG-1.03) : on ne renseigne
|
* Distributeur/Courtier est mutuellement exclusive (RG-1.03) : on ne renseigne
|
||||||
* que la FK correspondant au type choisi, l'autre est forcee a null.
|
* que la FK correspondant au type choisi, l'autre est forcee a null.
|
||||||
*/
|
*/
|
||||||
export function buildMainPayload(main: MainFormDraft): Record<string, unknown> {
|
export function buildMainPayload(main: MainFormDraft, options: BuildPayloadOptions = {}): Record<string, unknown> {
|
||||||
// companyName omis si vide -> 422 NotBlank au lieu d'un 400 de type (ERP-119).
|
// companyName omis si vide -> 422 NotBlank au lieu d'un 400 de type (ERP-119).
|
||||||
// relationType : champ transitoire (non persiste cote back) qui porte
|
// relationType : champ transitoire (non persiste cote back) qui porte
|
||||||
// l'intention UI « ce client depend d'un distributeur / courtier ». Il sert
|
// l'intention UI « ce client depend d'un distributeur / courtier ». Il sert
|
||||||
@@ -152,14 +176,14 @@ export function buildMainPayload(main: MainFormDraft): Record<string, unknown> {
|
|||||||
// la FK correspondante devient obligatoire -> 422 sur distributor / broker.
|
// la FK correspondante devient obligatoire -> 422 sur distributor / broker.
|
||||||
// Sans equivalent derivable cote back (FK nullable), c'est la seule facon de
|
// Sans equivalent derivable cote back (FK nullable), c'est la seule facon de
|
||||||
// rester sur « on soumet, le back tranche » plutot qu'une garde front-only.
|
// rester sur « on soumet, le back tranche » plutot qu'une garde front-only.
|
||||||
return omitEmptyRequired({
|
return finalizeRequired({
|
||||||
companyName: main.companyName,
|
companyName: main.companyName,
|
||||||
categories: main.categoryIris,
|
categories: main.categoryIris,
|
||||||
relationType: main.relationType,
|
relationType: main.relationType,
|
||||||
distributor: main.relationType === 'distributeur' ? main.distributorIri : null,
|
distributor: main.relationType === 'distributeur' ? main.distributorIri : null,
|
||||||
broker: main.relationType === 'courtier' ? main.brokerIri : null,
|
broker: main.relationType === 'courtier' ? main.brokerIri : null,
|
||||||
triageService: main.triageService,
|
triageService: main.triageService,
|
||||||
}, MAIN_REQUIRED_NON_NULLABLE_KEYS)
|
}, MAIN_REQUIRED_NON_NULLABLE_KEYS, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Payload de l'onglet Information — groupe client:write:information UNIQUEMENT. */
|
/** Payload de l'onglet Information — groupe client:write:information UNIQUEMENT. */
|
||||||
@@ -211,9 +235,10 @@ export function buildContactPayload(contact: ContactFormDraft): Record<string, u
|
|||||||
export function buildAddressPayload(
|
export function buildAddressPayload(
|
||||||
address: AddressFormDraft,
|
address: AddressFormDraft,
|
||||||
isBillingEmailRequired: boolean,
|
isBillingEmailRequired: boolean,
|
||||||
|
options: BuildPayloadOptions = {},
|
||||||
): Record<string, unknown> {
|
): Record<string, unknown> {
|
||||||
// postalCode / city / street omis si vides -> 422 NotBlank (ERP-119).
|
// postalCode / city / street : omis a la creation, `''` en edition -> 422 NotBlank (ERP-119).
|
||||||
return omitEmptyRequired({
|
return finalizeRequired({
|
||||||
isProspect: address.isProspect,
|
isProspect: address.isProspect,
|
||||||
isDelivery: address.isDelivery,
|
isDelivery: address.isDelivery,
|
||||||
isBilling: address.isBilling,
|
isBilling: address.isBilling,
|
||||||
@@ -229,18 +254,18 @@ export function buildAddressPayload(
|
|||||||
contacts: address.contactIris,
|
contacts: address.contactIris,
|
||||||
billingEmail: isBillingEmailRequired ? (address.billingEmail || null) : null,
|
billingEmail: isBillingEmailRequired ? (address.billingEmail || null) : null,
|
||||||
billingEmailSecondary: isBillingEmailRequired && address.hasSecondaryBillingEmail ? (address.billingEmailSecondary || null) : null,
|
billingEmailSecondary: isBillingEmailRequired && address.hasSecondaryBillingEmail ? (address.billingEmailSecondary || null) : null,
|
||||||
}, ADDRESS_REQUIRED_NON_NULLABLE_KEYS)
|
}, ADDRESS_REQUIRED_NON_NULLABLE_KEYS, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Payload d'un RIB (sous-ressource client_rib). */
|
/** Payload d'un RIB (sous-ressource client_rib). */
|
||||||
export function buildRibPayload(rib: RibFormDraft): Record<string, unknown> {
|
export function buildRibPayload(rib: RibFormDraft, options: BuildPayloadOptions = {}): Record<string, unknown> {
|
||||||
// label / bic / iban omis si vides -> 422 NotBlank au lieu d'un 400 de type
|
// label / bic / iban : omis a la creation, `''` en edition -> 422 NotBlank au lieu
|
||||||
// sur un RIB partiel (ex. IBAN seul). ERP-119.
|
// d'un 400 de type (ou d'un faux 200 PATCH qui garderait l'ancienne valeur). ERP-119.
|
||||||
return omitEmptyRequired({
|
return finalizeRequired({
|
||||||
label: rib.label,
|
label: rib.label,
|
||||||
bic: rib.bic,
|
bic: rib.bic,
|
||||||
iban: rib.iban,
|
iban: rib.iban,
|
||||||
}, RIB_REQUIRED_NON_NULLABLE_KEYS)
|
}, RIB_REQUIRED_NON_NULLABLE_KEYS, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Gating par permission ────────────────────────────────────────────────────
|
// ── Gating par permission ────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -419,3 +419,28 @@ export function omitEmptyRequired<T extends Record<string, unknown>>(
|
|||||||
|
|
||||||
return payload
|
return payload
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variante PATCH (edition d'une ligne EXISTANTE) : remplace les cles requises
|
||||||
|
* laissees vides par une chaine vide `''` au lieu de les OMETTRE.
|
||||||
|
*
|
||||||
|
* Pourquoi pas `omitEmptyRequired` en edition : un PATCH a semantique merge — une
|
||||||
|
* cle absente laisse la valeur serveur INCHANGEE. Vider un champ requis puis valider
|
||||||
|
* renverrait alors un 200 trompeur (l'ancienne valeur est conservee). En envoyant
|
||||||
|
* `''` (chaine valide), on evite le 400 de type (« must be string, NULL given ») et
|
||||||
|
* le Validator `NotBlank(trim)` rejette la valeur -> 422 avec propertyPath, mappee
|
||||||
|
* inline sous le champ. Mute et retourne le payload.
|
||||||
|
*/
|
||||||
|
export function blankEmptyRequired<T extends Record<string, unknown>>(
|
||||||
|
payload: T,
|
||||||
|
requiredKeys: readonly string[],
|
||||||
|
): T {
|
||||||
|
for (const key of requiredKeys) {
|
||||||
|
const value = payload[key]
|
||||||
|
if (value === null || value === undefined || value === '') {
|
||||||
|
(payload as Record<string, unknown>)[key] = ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,301 @@
|
|||||||
|
/**
|
||||||
|
* Helpers purs de l'ecran « Consultation fournisseur » (M2 Commercial, lecture
|
||||||
|
* seule). Miroir de `clientConsultation.ts` (M1), adapte aux differences M2.
|
||||||
|
*
|
||||||
|
* Mappent le payload `GET /api/suppliers/{id}` (relations embarquees, cf. groupe
|
||||||
|
* `supplier:item:read` + `supplier:read:accounting`) vers les brouillons « plats »
|
||||||
|
* partages avec les blocs reutilisables `SupplierContactBlock` / `SupplierAddressBlock`
|
||||||
|
* et l'onglet Comptabilite. Ne touchent ni a l'API ni a l'etat reactif : testables
|
||||||
|
* unitairement (cf. supplierConsultation.spec.ts).
|
||||||
|
*
|
||||||
|
* Rappels de contrat back (verifies sur le JSON reel fige — ERP-92, spec-back § 4.0.bis) :
|
||||||
|
* - les relations ManyToOne (tvaMode/paymentDelay/paymentType/bank) sont
|
||||||
|
* serialisees en OBJETS embarques (`{id, code, label}`), pas en IRI nu ;
|
||||||
|
* - les champs nuls sont OMIS du JSON (skip_null_values) → toujours lire avec `?? null` ;
|
||||||
|
* - les champs comptables et `ribs` sont TOTALEMENT ABSENTS (cle omise, pas `null`)
|
||||||
|
* sans permission accounting.view (gate serveur via SupplierReadGroupContextBuilder).
|
||||||
|
*
|
||||||
|
* Differences M2 vs M1 :
|
||||||
|
* - Adresse via enum `addressType` (PROSPECT/DEPART/RENDU, RG-2.09) — pas de
|
||||||
|
* drapeaux isProspect/isDelivery/isBilling.
|
||||||
|
* - Adresse : champs specifiques fournisseur `bennes` (nombre) et `triageProvider`.
|
||||||
|
* Pas d'email de facturation.
|
||||||
|
* - Information : champ specifique fournisseur `volumeForecast`.
|
||||||
|
* - Pas de relation Distributeur/Courtier ni de triage sur le bloc principal.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { formatPhoneFR } from '~/shared/utils/phone'
|
||||||
|
import {
|
||||||
|
emptyAddress,
|
||||||
|
type SupplierAddressFormDraft,
|
||||||
|
type SupplierAddressType,
|
||||||
|
type SupplierContactFormDraft,
|
||||||
|
type SupplierRibFormDraft,
|
||||||
|
} from '~/modules/commercial/types/supplierForm'
|
||||||
|
|
||||||
|
/** Reference Hydra embarquee minimale (@id toujours present). */
|
||||||
|
export interface HydraRef {
|
||||||
|
'@id': string
|
||||||
|
[key: string]: unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Une relation peut etre embarquee (objet), un IRI nu (chaine) ou absente. */
|
||||||
|
export type Relation = HydraRef | string | null | undefined
|
||||||
|
|
||||||
|
/** Site embarque dans une adresse (groupe site:read). */
|
||||||
|
export interface SiteRead extends HydraRef {
|
||||||
|
name?: string
|
||||||
|
color?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Categorie embarquee (groupe category:read). */
|
||||||
|
export interface CategoryRead extends HydraRef {
|
||||||
|
code?: string
|
||||||
|
name?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Contact embarque (groupe supplier_contact:read). */
|
||||||
|
export interface ContactRead extends HydraRef {
|
||||||
|
id: number
|
||||||
|
firstName?: string | null
|
||||||
|
lastName?: string | null
|
||||||
|
jobTitle?: string | null
|
||||||
|
phonePrimary?: string | null
|
||||||
|
phoneSecondary?: string | null
|
||||||
|
email?: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Adresse embarquee (groupe supplier_address:read). */
|
||||||
|
export interface AddressRead extends HydraRef {
|
||||||
|
id: number
|
||||||
|
addressType?: SupplierAddressType | null
|
||||||
|
country?: string | null
|
||||||
|
postalCode?: string | null
|
||||||
|
city?: string | null
|
||||||
|
street?: string | null
|
||||||
|
streetComplement?: string | null
|
||||||
|
bennes?: number | null
|
||||||
|
triageProvider?: boolean
|
||||||
|
sites?: SiteRead[]
|
||||||
|
categories?: CategoryRead[]
|
||||||
|
// L'embed M2M des contacts d'adresse peut etre un objet (partiel) ou un IRI nu.
|
||||||
|
contacts?: Array<HydraRef | string>
|
||||||
|
}
|
||||||
|
|
||||||
|
/** RIB embarque (groupe supplier:read:accounting, present ssi accounting.view). */
|
||||||
|
export interface RibRead extends HydraRef {
|
||||||
|
id: number
|
||||||
|
label?: string | null
|
||||||
|
bic?: string | null
|
||||||
|
iban?: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detail d'un fournisseur tel que renvoye par `GET /api/suppliers/{id}`. Tous les
|
||||||
|
* champs sont optionnels : skip_null_values cote serveur et gating accounting
|
||||||
|
* peuvent omettre n'importe quelle cle.
|
||||||
|
*/
|
||||||
|
export interface SupplierDetail extends HydraRef {
|
||||||
|
id: number
|
||||||
|
companyName?: string | null
|
||||||
|
isArchived?: boolean
|
||||||
|
categories?: CategoryRead[]
|
||||||
|
contacts?: ContactRead[]
|
||||||
|
addresses?: AddressRead[]
|
||||||
|
ribs?: RibRead[]
|
||||||
|
// Onglet Information
|
||||||
|
description?: string | null
|
||||||
|
competitors?: string | null
|
||||||
|
foundedAt?: string | null
|
||||||
|
employeesCount?: number | null
|
||||||
|
revenueAmount?: string | null
|
||||||
|
profitAmount?: string | null
|
||||||
|
directorName?: string | null
|
||||||
|
/** Volume previsionnel (entier, specifique fournisseur). */
|
||||||
|
volumeForecast?: number | null
|
||||||
|
// Onglet Comptabilite (present ssi accounting.view)
|
||||||
|
siren?: string | null
|
||||||
|
accountNumber?: string | null
|
||||||
|
nTva?: string | null
|
||||||
|
tvaMode?: Relation
|
||||||
|
paymentDelay?: Relation
|
||||||
|
paymentType?: Relation
|
||||||
|
bank?: Relation
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Etat « plat » de l'onglet Comptabilite (miroir lecture du formulaire). */
|
||||||
|
export interface AccountingDraft {
|
||||||
|
siren: string | null
|
||||||
|
accountNumber: string | null
|
||||||
|
nTva: string | null
|
||||||
|
tvaModeIri: string | null
|
||||||
|
paymentDelayIri: string | null
|
||||||
|
paymentTypeIri: string | null
|
||||||
|
bankIri: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Option de select ({ value, label }) construite a partir de l'embed. */
|
||||||
|
export interface SelectOption {
|
||||||
|
value: string
|
||||||
|
label: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Option de categorie enrichie de son code (compatible CategoryOption des blocs). */
|
||||||
|
export interface CategorySelectOption extends SelectOption {
|
||||||
|
code: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vue d'une adresse pour la consultation : le brouillon + ses options de select
|
||||||
|
* construites a partir de l'embed (sites/categories propres a CETTE adresse).
|
||||||
|
*/
|
||||||
|
export interface AddressView {
|
||||||
|
draft: SupplierAddressFormDraft
|
||||||
|
siteOptions: SelectOption[]
|
||||||
|
categoryOptions: CategorySelectOption[]
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Extrait l'IRI d'une relation (objet embarque, IRI nu, ou null si absente). */
|
||||||
|
export function iriOf(relation: Relation): string | null {
|
||||||
|
if (relation === null || relation === undefined) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
if (typeof relation === 'string') {
|
||||||
|
return relation
|
||||||
|
}
|
||||||
|
return relation['@id'] ?? null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Mappe un contact embarque vers un brouillon (telephones formates XX XX XX XX XX). */
|
||||||
|
export function mapContactToDraft(contact: ContactRead): SupplierContactFormDraft {
|
||||||
|
const phoneSecondary = contact.phoneSecondary ?? null
|
||||||
|
return {
|
||||||
|
id: contact.id,
|
||||||
|
iri: contact['@id'] ?? null,
|
||||||
|
firstName: contact.firstName ?? null,
|
||||||
|
lastName: contact.lastName ?? null,
|
||||||
|
jobTitle: contact.jobTitle ?? null,
|
||||||
|
phonePrimary: contact.phonePrimary ? formatPhoneFR(contact.phonePrimary) : null,
|
||||||
|
phoneSecondary: phoneSecondary ? formatPhoneFR(phoneSecondary) : null,
|
||||||
|
email: contact.email ?? null,
|
||||||
|
hasSecondaryPhone: phoneSecondary !== null && phoneSecondary !== '',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mappe une adresse embarquee vers un brouillon (IRI extraits des sous-collections).
|
||||||
|
* `bennes` (entier) est converti en chaine pour MalioInputNumber (defaut « 0 »).
|
||||||
|
*/
|
||||||
|
export function mapAddressToDraft(address: AddressRead): SupplierAddressFormDraft {
|
||||||
|
return {
|
||||||
|
id: address.id,
|
||||||
|
addressType: address.addressType ?? null,
|
||||||
|
country: address.country ?? 'France',
|
||||||
|
postalCode: address.postalCode ?? null,
|
||||||
|
city: address.city ?? null,
|
||||||
|
street: address.street ?? null,
|
||||||
|
streetComplement: address.streetComplement ?? null,
|
||||||
|
categoryIris: (address.categories ?? []).map(c => c['@id']),
|
||||||
|
siteIris: (address.sites ?? []).map(s => s['@id']),
|
||||||
|
contactIris: (address.contacts ?? []).map(c => (typeof c === 'string' ? c : c['@id'])),
|
||||||
|
bennes: address.bennes != null ? String(address.bennes) : '0',
|
||||||
|
triageProvider: address.triageProvider ?? false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Mappe un RIB embarque vers un brouillon. */
|
||||||
|
export function mapRibToDraft(rib: RibRead): SupplierRibFormDraft {
|
||||||
|
return {
|
||||||
|
id: rib.id,
|
||||||
|
label: rib.label ?? null,
|
||||||
|
bic: rib.bic ?? null,
|
||||||
|
iban: rib.iban ?? null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Mappe les champs comptables du fournisseur (scalaires + IRI des referentiels). */
|
||||||
|
export function mapAccountingDraft(supplier: SupplierDetail): AccountingDraft {
|
||||||
|
return {
|
||||||
|
siren: supplier.siren ?? null,
|
||||||
|
accountNumber: supplier.accountNumber ?? null,
|
||||||
|
nTva: supplier.nTva ?? null,
|
||||||
|
tvaModeIri: iriOf(supplier.tvaMode),
|
||||||
|
paymentDelayIri: iriOf(supplier.paymentDelay),
|
||||||
|
paymentTypeIri: iriOf(supplier.paymentType),
|
||||||
|
bankIri: iriOf(supplier.bank),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options de categories (value=IRI, label=nom, code) construites depuis l'embed.
|
||||||
|
* Source role-independante : evite de dependre de `GET /categories` (403 pour les
|
||||||
|
* roles metier non-admin), qui laisserait les libelles vides.
|
||||||
|
*/
|
||||||
|
export function categoryOptionsOf(categories: CategoryRead[] | undefined): CategorySelectOption[] {
|
||||||
|
return (categories ?? []).map(c => ({
|
||||||
|
value: c['@id'],
|
||||||
|
label: c.name ?? c.code ?? c['@id'],
|
||||||
|
code: c.code ?? '',
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Options de sites (value=IRI, label=nom) construites depuis l'embed d'une adresse. */
|
||||||
|
export function siteOptionsOf(sites: SiteRead[] | undefined): SelectOption[] {
|
||||||
|
return (sites ?? []).map(s => ({ value: s['@id'], label: s.name ?? s['@id'] }))
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Options de contacts (value=IRI, label=nom complet ou email) depuis l'embed fournisseur. */
|
||||||
|
export function contactOptionsOf(contacts: ContactRead[] | undefined): SelectOption[] {
|
||||||
|
return (contacts ?? []).map(c => ({
|
||||||
|
value: c['@id'],
|
||||||
|
label: [c.firstName, c.lastName].filter(Boolean).join(' ') || (c.email ?? c['@id']),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liste a une seule option (ou vide) construite depuis un referentiel embarque
|
||||||
|
* (TvaMode / PaymentDelay / PaymentType / Bank) pour alimenter un MalioSelect en
|
||||||
|
* lecture seule. Le libelle vient de l'embed (`label` ou `name`), jamais d'un
|
||||||
|
* `GET` de referentiel — l'affichage reste correct quel que soit le role.
|
||||||
|
*/
|
||||||
|
export function referentialOptionOf(relation: Relation): SelectOption[] {
|
||||||
|
if (!relation || typeof relation === 'string') {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
const label = (relation.label as string | undefined)
|
||||||
|
?? (relation.name as string | undefined)
|
||||||
|
?? relation['@id']
|
||||||
|
return [{ value: relation['@id'], label }]
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Vue d'une adresse (brouillon + options de select propres a l'adresse). */
|
||||||
|
export function mapAddressView(address: AddressRead): AddressView {
|
||||||
|
return {
|
||||||
|
draft: mapAddressToDraft(address),
|
||||||
|
siteOptions: siteOptionsOf(address.sites),
|
||||||
|
categoryOptions: categoryOptionsOf(address.categories),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bouton « Modifier » : visible si l'utilisateur peut editer au moins un onglet
|
||||||
|
* — `manage` (formulaire/onglets metier) OU `accounting.manage` (le role Compta
|
||||||
|
* doit pouvoir ouvrir l'edition pour son onglet Comptabilite). Le readonly fin
|
||||||
|
* par onglet est gere sur l'ecran d'edition (96).
|
||||||
|
*/
|
||||||
|
export function canEditSupplier(canAny: (codes: string[]) => boolean): boolean {
|
||||||
|
return canAny(['commercial.suppliers.manage', 'commercial.suppliers.accounting.manage'])
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Bouton « Archiver » : permission archive ET fournisseur encore actif. */
|
||||||
|
export function showArchiveAction(can: (code: string) => boolean, isArchived: boolean): boolean {
|
||||||
|
return can('commercial.suppliers.archive') && !isArchived
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Bouton « Restaurer » : permission archive ET fournisseur deja archive. */
|
||||||
|
export function showRestoreAction(can: (code: string) => boolean, isArchived: boolean): boolean {
|
||||||
|
return can('commercial.suppliers.archive') && isArchived
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Brouillon d'adresse vierge (reexport pour la page : 1 bloc vide si aucune adresse). */
|
||||||
|
export { emptyAddress }
|
||||||
@@ -0,0 +1,250 @@
|
|||||||
|
/**
|
||||||
|
* Helpers purs des ecrans « Ajouter » / « Modifier » un fournisseur (M2
|
||||||
|
* Commercial) — miroir de `clientEdit.ts` (M1). Deux responsabilites, toutes deux
|
||||||
|
* testables unitairement (cf. supplierEdit.spec.ts) :
|
||||||
|
* 1. Pre-remplissage : mapper le payload `GET /api/suppliers/{id}` (embed +
|
||||||
|
* scalaires) vers les brouillons « plats » edites par la page de modification.
|
||||||
|
* 2. Scoping STRICT des payloads PATCH (mode strict RG-2.16 / ERP-74) : chaque
|
||||||
|
* onglet n'envoie QUE les champs de SON groupe de serialisation, jamais un
|
||||||
|
* payload mixte (un champ hors-permission = 403 sur l'integralite cote back).
|
||||||
|
*
|
||||||
|
* Ces helpers ne touchent ni a l'API ni a l'etat reactif.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
ADDRESS_REQUIRED_NON_NULLABLE_KEYS,
|
||||||
|
blankEmptyRequired,
|
||||||
|
MAIN_REQUIRED_NON_NULLABLE_KEYS,
|
||||||
|
omitEmptyRequired,
|
||||||
|
RIB_REQUIRED_NON_NULLABLE_KEYS,
|
||||||
|
} from '~/modules/commercial/utils/supplierFormRules'
|
||||||
|
import { iriOf, type SupplierDetail } from '~/modules/commercial/utils/supplierConsultation'
|
||||||
|
import type {
|
||||||
|
SupplierAddressFormDraft,
|
||||||
|
SupplierContactFormDraft,
|
||||||
|
SupplierRibFormDraft,
|
||||||
|
} from '~/modules/commercial/types/supplierForm'
|
||||||
|
|
||||||
|
/** Etat « plat » du bloc principal (groupe supplier:write:main). */
|
||||||
|
export interface MainFormDraft {
|
||||||
|
companyName: string | null
|
||||||
|
/** IRI des categories rattachees (M2M, type FOURNISSEUR). */
|
||||||
|
categoryIris: string[]
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Etat « plat » de l'onglet Information (groupe supplier:write:information). */
|
||||||
|
export interface InformationFormDraft {
|
||||||
|
description: string | null
|
||||||
|
competitors: string | null
|
||||||
|
/** Date de creation de l'entreprise au format YYYY-MM-DD (MalioDate). */
|
||||||
|
foundedAt: string | null
|
||||||
|
/** Nombre de salaries en chaine (saisie masquee), converti en number au PATCH. */
|
||||||
|
employeesCount: string | null
|
||||||
|
revenueAmount: string | null
|
||||||
|
profitAmount: string | null
|
||||||
|
directorName: string | null
|
||||||
|
/** Volume previsionnel (entier >= 0, specifique fournisseur) en chaine. */
|
||||||
|
volumeForecast: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Etat « plat » de l'onglet Comptabilite (groupe supplier:write:accounting). */
|
||||||
|
export interface AccountingFormDraft {
|
||||||
|
siren: string | null
|
||||||
|
accountNumber: string | null
|
||||||
|
nTva: string | null
|
||||||
|
tvaModeIri: string | null
|
||||||
|
paymentDelayIri: string | null
|
||||||
|
paymentTypeIri: string | null
|
||||||
|
bankIri: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Permissions de l'utilisateur courant pertinentes pour l'edition d'un fournisseur. */
|
||||||
|
export interface SupplierEditAbilities {
|
||||||
|
/** `commercial.suppliers.manage` : bloc principal + onglets metier. */
|
||||||
|
canManage: boolean
|
||||||
|
/** `commercial.suppliers.accounting.view` : visibilite de l'onglet Comptabilite. */
|
||||||
|
canAccountingView: boolean
|
||||||
|
/** `commercial.suppliers.accounting.manage` : edition de l'onglet Comptabilite. */
|
||||||
|
canAccountingManage: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Editabilite resolue par zone d'onglet (deduite des permissions). */
|
||||||
|
export interface TabEditability {
|
||||||
|
/** Bloc principal + onglets Information / Contacts / Adresses editables. */
|
||||||
|
businessEditable: boolean
|
||||||
|
/** Onglet Comptabilite present (affiche). */
|
||||||
|
accountingVisible: boolean
|
||||||
|
/** Onglet Comptabilite editable. */
|
||||||
|
accountingEditable: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Pre-remplissage (GET detail -> brouillons) ──────────────────────────────
|
||||||
|
|
||||||
|
/** Mappe le detail fournisseur vers le brouillon du bloc principal. */
|
||||||
|
export function mapMainDraft(supplier: SupplierDetail): MainFormDraft {
|
||||||
|
return {
|
||||||
|
companyName: supplier.companyName ?? null,
|
||||||
|
categoryIris: (supplier.categories ?? []).map(c => c['@id']),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Mappe le detail fournisseur vers le brouillon de l'onglet Information. */
|
||||||
|
export function mapInformationDraft(supplier: SupplierDetail): InformationFormDraft {
|
||||||
|
return {
|
||||||
|
description: supplier.description ?? null,
|
||||||
|
competitors: supplier.competitors ?? null,
|
||||||
|
// MalioDate attend strictement YYYY-MM-DD : on tronque l'ISO datetime.
|
||||||
|
foundedAt: supplier.foundedAt ? supplier.foundedAt.slice(0, 10) : null,
|
||||||
|
employeesCount: supplier.employeesCount != null ? String(supplier.employeesCount) : null,
|
||||||
|
revenueAmount: supplier.revenueAmount ?? null,
|
||||||
|
profitAmount: supplier.profitAmount ?? null,
|
||||||
|
directorName: supplier.directorName ?? null,
|
||||||
|
// Volume previsionnel (entier, specifique fournisseur) en chaine pour la saisie.
|
||||||
|
volumeForecast: supplier.volumeForecast != null ? String(supplier.volumeForecast) : null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Mappe les champs comptables du detail vers le brouillon de l'onglet (scalaires + IRI). */
|
||||||
|
export function mapAccountingFormDraft(supplier: SupplierDetail): AccountingFormDraft {
|
||||||
|
return {
|
||||||
|
siren: supplier.siren ?? null,
|
||||||
|
accountNumber: supplier.accountNumber ?? null,
|
||||||
|
nTva: supplier.nTva ?? null,
|
||||||
|
tvaModeIri: iriOf(supplier.tvaMode),
|
||||||
|
paymentDelayIri: iriOf(supplier.paymentDelay),
|
||||||
|
paymentTypeIri: iriOf(supplier.paymentType),
|
||||||
|
bankIri: iriOf(supplier.bank),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resout l'editabilite par zone a partir des permissions (option 1 ERP-74,
|
||||||
|
* miroir UI du re-gating champ-par-champ du SupplierProcessor) :
|
||||||
|
* - bloc principal + Information/Contacts/Adresses : editables ssi `manage` ;
|
||||||
|
* - Comptabilite : visible ssi `accounting.view`, editable ssi `accounting.manage`.
|
||||||
|
*
|
||||||
|
* Produit le comportement attendu :
|
||||||
|
* - Admin : tout editable.
|
||||||
|
* - Bureau / Commerciale (manage, sans accounting) : metier editable, Compta masquee.
|
||||||
|
* - Compta (accounting seul, sans manage) : metier readonly, Compta editable.
|
||||||
|
*/
|
||||||
|
export function resolveTabEditability(abilities: SupplierEditAbilities): TabEditability {
|
||||||
|
return {
|
||||||
|
businessEditable: abilities.canManage,
|
||||||
|
accountingVisible: abilities.canAccountingView,
|
||||||
|
accountingEditable: abilities.canAccountingManage,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Scoping strict des payloads PATCH/POST ──────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options de construction d'un payload d'ecriture.
|
||||||
|
* - `forUpdate: false` (defaut, CREATION/POST) : les champs requis vides sont OMIS
|
||||||
|
* -> 422 NotBlank a l'insert (le back ne reçoit pas la cle).
|
||||||
|
* - `forUpdate: true` (EDITION/PATCH d'une ligne existante) : les champs requis
|
||||||
|
* vides sont envoyes en `''` -> 422 NotBlank (sinon une cle omise laisse la valeur
|
||||||
|
* serveur inchangee, faux 200 — cf. blankEmptyRequired).
|
||||||
|
*/
|
||||||
|
export interface BuildPayloadOptions {
|
||||||
|
forUpdate?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Selectionne le finaliseur des champs requis selon création (omit) vs édition (blank). */
|
||||||
|
function finalizeRequired<T extends Record<string, unknown>>(
|
||||||
|
payload: T,
|
||||||
|
requiredKeys: readonly string[],
|
||||||
|
options: BuildPayloadOptions,
|
||||||
|
): T {
|
||||||
|
return options.forUpdate
|
||||||
|
? blankEmptyRequired(payload, requiredKeys)
|
||||||
|
: omitEmptyRequired(payload, requiredKeys)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Payload du bloc principal — groupe supplier:write:main UNIQUEMENT.
|
||||||
|
* companyName vide -> 422 NotBlank (omis a la creation, `''` en edition — ERP-119).
|
||||||
|
*/
|
||||||
|
export function buildMainPayload(main: MainFormDraft, options: BuildPayloadOptions = {}): Record<string, unknown> {
|
||||||
|
return finalizeRequired({
|
||||||
|
companyName: main.companyName,
|
||||||
|
categories: main.categoryIris,
|
||||||
|
}, MAIN_REQUIRED_NON_NULLABLE_KEYS, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Payload de l'onglet Information — groupe supplier:write:information UNIQUEMENT. */
|
||||||
|
export function buildInformationPayload(information: InformationFormDraft): Record<string, unknown> {
|
||||||
|
return {
|
||||||
|
description: information.description || null,
|
||||||
|
competitors: information.competitors || null,
|
||||||
|
foundedAt: information.foundedAt || null,
|
||||||
|
employeesCount: information.employeesCount ? Number(information.employeesCount) : null,
|
||||||
|
revenueAmount: information.revenueAmount || null,
|
||||||
|
profitAmount: information.profitAmount || null,
|
||||||
|
directorName: information.directorName || null,
|
||||||
|
volumeForecast: information.volumeForecast ? Number(information.volumeForecast) : null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Payload des scalaires de l'onglet Comptabilite — groupe supplier:write:accounting
|
||||||
|
* UNIQUEMENT (les RIB passent par la sous-ressource /suppliers/{id}/ribs). La
|
||||||
|
* banque n'a de sens que pour un Virement (RG-2.07) : forcee a null sinon.
|
||||||
|
*/
|
||||||
|
export function buildAccountingPayload(
|
||||||
|
accounting: AccountingFormDraft,
|
||||||
|
isBankRequired: boolean,
|
||||||
|
): Record<string, unknown> {
|
||||||
|
return {
|
||||||
|
siren: accounting.siren || null,
|
||||||
|
accountNumber: accounting.accountNumber || null,
|
||||||
|
tvaMode: accounting.tvaModeIri,
|
||||||
|
nTva: accounting.nTva || null,
|
||||||
|
paymentDelay: accounting.paymentDelayIri,
|
||||||
|
paymentType: accounting.paymentTypeIri,
|
||||||
|
bank: isBankRequired ? accounting.bankIri : null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Payload d'un contact (sous-ressource supplier_contact). */
|
||||||
|
export function buildContactPayload(contact: SupplierContactFormDraft): Record<string, unknown> {
|
||||||
|
return {
|
||||||
|
firstName: contact.firstName || null,
|
||||||
|
lastName: contact.lastName || null,
|
||||||
|
jobTitle: contact.jobTitle || null,
|
||||||
|
phonePrimary: contact.phonePrimary || null,
|
||||||
|
phoneSecondary: contact.hasSecondaryPhone ? (contact.phoneSecondary || null) : null,
|
||||||
|
email: contact.email || null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Payload d'une adresse (sous-ressource supplier_address). postalCode / city /
|
||||||
|
* street omis si vides -> 422 NotBlank (ERP-119). Specifique fournisseur :
|
||||||
|
* `bennes` (entier, 0 par defaut) + `triageProvider` (booleen). Pas d'email de
|
||||||
|
* facturation (difference M1).
|
||||||
|
*/
|
||||||
|
export function buildAddressPayload(address: SupplierAddressFormDraft, options: BuildPayloadOptions = {}): Record<string, unknown> {
|
||||||
|
return finalizeRequired({
|
||||||
|
addressType: address.addressType,
|
||||||
|
country: address.country,
|
||||||
|
postalCode: address.postalCode || null,
|
||||||
|
city: address.city || null,
|
||||||
|
street: address.street || null,
|
||||||
|
streetComplement: address.streetComplement || null,
|
||||||
|
categories: address.categoryIris,
|
||||||
|
sites: address.siteIris,
|
||||||
|
contacts: address.contactIris,
|
||||||
|
bennes: address.bennes !== null && address.bennes !== '' ? Number(address.bennes) : null,
|
||||||
|
triageProvider: address.triageProvider,
|
||||||
|
}, ADDRESS_REQUIRED_NON_NULLABLE_KEYS, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Payload d'un RIB (sous-ressource supplier_rib). */
|
||||||
|
export function buildRibPayload(rib: SupplierRibFormDraft, options: BuildPayloadOptions = {}): Record<string, unknown> {
|
||||||
|
return finalizeRequired({
|
||||||
|
label: rib.label,
|
||||||
|
bic: rib.bic,
|
||||||
|
iban: rib.iban,
|
||||||
|
}, RIB_REQUIRED_NON_NULLABLE_KEYS, options)
|
||||||
|
}
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
/**
|
||||||
|
* Regles metier pures de l'ecran « Ajouter un fournisseur » (M2 Commercial).
|
||||||
|
*
|
||||||
|
* Miroir de `clientFormRules.ts` (M1), centralisees ici (hors composant) pour
|
||||||
|
* rester testables unitairement et partagees entre la creation et les ecrans
|
||||||
|
* d'edition/consultation (95/96). Ces helpers ne touchent ni a l'API ni a l'etat
|
||||||
|
* reactif : ils prennent des brouillons « plats » et retournent des booleens.
|
||||||
|
*
|
||||||
|
* Le back reste la source de verite (les RG sont re-validees serveur, mode
|
||||||
|
* strict) ; ces regles ne servent qu'au feedback UI immediat (gating de boutons).
|
||||||
|
*
|
||||||
|
* Differences M2 vs M1 :
|
||||||
|
* - Adresse via enum `addressType` (PROSPECT/DEPART/RENDU, RG-2.09) — pas de
|
||||||
|
* drapeaux ni d'exclusivite a gerer cote front (le radio est exclusif par nature).
|
||||||
|
* - Pas d'email de facturation, pas de relation Distributeur/Courtier.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { SupplierAddressType } from '~/modules/commercial/types/supplierForm'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Onglets « coquille » (non encore implementes) : frame vide, passage
|
||||||
|
* automatique a l'onglet suivant (aligne M1).
|
||||||
|
*/
|
||||||
|
export const SUPPLIER_FORM_PLACEHOLDER_TABS = ['transport', 'statistics', 'reports', 'exchanges'] as const
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Onglets affiches uniquement en MODIFICATION/CONSULTATION (jamais a la
|
||||||
|
* creation) : Statistiques / Rapports / Echanges. A rebrancher dans les ecrans
|
||||||
|
* 95/96 via l'option `includeEditOnlyTabs`.
|
||||||
|
*/
|
||||||
|
export const SUPPLIER_FORM_EDIT_ONLY_TABS = ['statistics', 'reports', 'exchanges'] as const
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit l'ordre des onglets du formulaire fournisseur.
|
||||||
|
* - L'onglet Comptabilite n'est present que si l'utilisateur a `accounting.view`
|
||||||
|
* (Bureau / Commerciale ne le voient pas).
|
||||||
|
* - Les onglets edit-only sont exclus par defaut (creation) ; passer
|
||||||
|
* `includeEditOnlyTabs: true` pour les afficher en modification/consultation.
|
||||||
|
* Ordre aligne sur la spec M2 § Ecran « Ajouter un fournisseur » (barre 5 onglets).
|
||||||
|
*/
|
||||||
|
export function buildSupplierFormTabKeys(
|
||||||
|
canAccountingView: boolean,
|
||||||
|
options: { includeEditOnlyTabs?: boolean } = {},
|
||||||
|
): string[] {
|
||||||
|
const keys = ['information', 'contacts', 'addresses', 'transport']
|
||||||
|
if (canAccountingView) {
|
||||||
|
keys.push('accounting')
|
||||||
|
}
|
||||||
|
if (options.includeEditOnlyTabs) {
|
||||||
|
keys.push(...SUPPLIER_FORM_EDIT_ONLY_TABS)
|
||||||
|
}
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dernier onglet REMPLISSABLE d'un jeu d'onglets : le dernier qui n'est pas un
|
||||||
|
* placeholder. Role-aware sans regle ad hoc — il suffit de lui passer les
|
||||||
|
* `tabKeys` deja filtres par permission. Sa validation marque la fin de l'ajout.
|
||||||
|
*/
|
||||||
|
export function lastFillableTabKey(tabKeys: string[]): string | undefined {
|
||||||
|
return [...tabKeys].reverse().find(
|
||||||
|
key => !(SUPPLIER_FORM_PLACEHOLDER_TABS as readonly string[]).includes(key),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Sous-ensemble d'un contact necessaire aux regles de nommage (RG-2.04/2.13). */
|
||||||
|
export interface ContactDraft {
|
||||||
|
firstName: string | null
|
||||||
|
lastName: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Vrai si une chaine porte au moins un caractere non-espace. */
|
||||||
|
function isFilled(value: string | null | undefined): boolean {
|
||||||
|
return value !== null && value !== undefined && value.trim() !== ''
|
||||||
|
}
|
||||||
|
|
||||||
|
/** RG-2.04 : un contact est valide des qu'il porte un nom OU un prenom. */
|
||||||
|
export function isContactNamed(contact: ContactDraft): boolean {
|
||||||
|
return isFilled(contact.firstName) || isFilled(contact.lastName)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RG-2.13 : l'onglet Contacts ne peut etre finalise que s'il reste au moins un
|
||||||
|
* contact nomme (nom ou prenom).
|
||||||
|
*/
|
||||||
|
export function hasAtLeastOneValidContact(contacts: ContactDraft[]): boolean {
|
||||||
|
return contacts.some(isContactNamed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Primitive reutilisable : vrai si TOUTES les valeurs fournies sont vides. Sert a
|
||||||
|
* detecter un bloc de collection totalement vide (amorce non remplie). Un bloc qui
|
||||||
|
* porte la moindre donnee n'est PAS « blank » : il doit etre soumis pour declencher
|
||||||
|
* sa 422 inline plutot que d'etre saute silencieusement.
|
||||||
|
*/
|
||||||
|
export function isBlankRow(values: (string | null | undefined)[]): boolean {
|
||||||
|
return values.every(value => !isFilled(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Champs saisissables d'un bloc contact (pour detecter un bloc totalement vide). */
|
||||||
|
export interface ContactFillableDraft extends ContactDraft {
|
||||||
|
jobTitle: string | null
|
||||||
|
phonePrimary: string | null
|
||||||
|
phoneSecondary: string | null
|
||||||
|
email: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vrai si AUCUN champ saisissable du bloc contact n'est rempli. Distingue un bloc
|
||||||
|
* d'amorce vide (a ignorer au submit) d'un bloc partiellement rempli sans nom
|
||||||
|
* (email/telephone/fonction seul) : ce dernier doit etre soumis pour declencher la
|
||||||
|
* 422 RG-2.04 affichee inline.
|
||||||
|
*/
|
||||||
|
export function isContactBlank(contact: ContactFillableDraft): boolean {
|
||||||
|
return isBlankRow([
|
||||||
|
contact.firstName,
|
||||||
|
contact.lastName,
|
||||||
|
contact.jobTitle,
|
||||||
|
contact.phonePrimary,
|
||||||
|
contact.phoneSecondary,
|
||||||
|
contact.email,
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Champs saisissables d'un bloc RIB (pour detecter un bloc totalement vide). */
|
||||||
|
export interface RibFillableDraft {
|
||||||
|
label: string | null
|
||||||
|
bic: string | null
|
||||||
|
iban: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vrai si AUCUN champ du bloc RIB n'est rempli. Un RIB partiellement rempli (ex.
|
||||||
|
* IBAN seul) n'est PAS « blank » : il doit etre soumis pour declencher les 422
|
||||||
|
* NotBlank inline plutot que d'etre saute silencieusement.
|
||||||
|
*/
|
||||||
|
export function isRibBlank(rib: RibFillableDraft): boolean {
|
||||||
|
return isBlankRow([rib.label, rib.bic, rib.iban])
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RG-2.08 : un RIB est complet quand ses trois champs sont remplis (label, BIC,
|
||||||
|
* IBAN). Predicat partage entre le gating du bouton « + RIB » et la validation de
|
||||||
|
* l'onglet (au moins un RIB complet si reglement LCR).
|
||||||
|
*/
|
||||||
|
export function isRibComplete(rib: RibFillableDraft): boolean {
|
||||||
|
return isFilled(rib.label) && isFilled(rib.bic) && isFilled(rib.iban)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sous-ensemble d'une adresse necessaire a sa validite par-bloc : type (enum),
|
||||||
|
* sites et categories rattaches.
|
||||||
|
*/
|
||||||
|
export interface AddressValidityDraft {
|
||||||
|
addressType: SupplierAddressType | null
|
||||||
|
categoryIris: string[]
|
||||||
|
siteIris: string[]
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validite par-bloc d'une adresse : type renseigne (RG-2.09), >= 1 site (RG-2.06)
|
||||||
|
* et >= 1 categorie (RG-2.10). Predicat partage entre le gating du bouton
|
||||||
|
* « + Adresse » (le dernier bloc doit etre valide avant d'en ajouter un autre) et
|
||||||
|
* la validation de l'onglet (toutes les adresses valides). Pas d'email de
|
||||||
|
* facturation cote fournisseur (difference M1).
|
||||||
|
*/
|
||||||
|
export function isAddressValid(address: AddressValidityDraft): boolean {
|
||||||
|
return address.addressType !== null
|
||||||
|
&& address.siteIris.length >= 1
|
||||||
|
&& address.categoryIris.length >= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Code stable du type de reglement « virement » (RG-2.07). */
|
||||||
|
const PAYMENT_TYPE_TRANSFER = 'VIREMENT'
|
||||||
|
|
||||||
|
/** Code stable du type de reglement « lettre de change » (RG-2.08). */
|
||||||
|
const PAYMENT_TYPE_LCR = 'LCR'
|
||||||
|
|
||||||
|
/** RG-2.07 : la banque est obligatoire lorsque le type de reglement est un virement. */
|
||||||
|
export function isBankRequiredForPaymentType(code: string | null | undefined): boolean {
|
||||||
|
return code === PAYMENT_TYPE_TRANSFER
|
||||||
|
}
|
||||||
|
|
||||||
|
/** RG-2.08 : au moins un RIB complet est obligatoire lorsque le type de reglement est une LCR. */
|
||||||
|
export function isRibRequiredForPaymentType(code: string | null | undefined): boolean {
|
||||||
|
return code === PAYMENT_TYPE_LCR
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Champs requis adosses a une colonne NON-nullable (ERP-119) ───────────────
|
||||||
|
// Memes contraintes qu'au M1 : un champ requis (NotBlank) porte par une colonne
|
||||||
|
// Doctrine NON nullable rejette `null` en 400 de TYPE avant le Validator. Parade :
|
||||||
|
// OMETTRE la cle du payload quand elle est vide -> le back produit une 422 NotBlank
|
||||||
|
// avec propertyPath, mappee en rouge sous le champ.
|
||||||
|
export const MAIN_REQUIRED_NON_NULLABLE_KEYS = ['companyName'] as const
|
||||||
|
// addressType : colonne non-nullable + NotBlank cote back. Envoyer `null` (radio
|
||||||
|
// non choisi) provoque un 400 de TYPE a la deserialisation AVANT le Validator
|
||||||
|
// (« must be string, NULL given ») -> pas de violation, pas d'erreur inline. On
|
||||||
|
// omet donc la cle quand elle est vide pour obtenir une 422 NotBlank propertyPath.
|
||||||
|
export const ADDRESS_REQUIRED_NON_NULLABLE_KEYS = ['addressType', 'postalCode', 'city', 'street'] as const
|
||||||
|
export const RIB_REQUIRED_NON_NULLABLE_KEYS = ['label', 'bic', 'iban'] as const
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retire d'un payload d'ecriture les cles requises laissees vides (null / '' /
|
||||||
|
* undefined), pour laisser le back produire une 422 NotBlank par champ plutot
|
||||||
|
* qu'un 400 de type sur une colonne non-nullable. Mute et retourne le payload.
|
||||||
|
*/
|
||||||
|
export function omitEmptyRequired<T extends Record<string, unknown>>(
|
||||||
|
payload: T,
|
||||||
|
requiredKeys: readonly string[],
|
||||||
|
): T {
|
||||||
|
for (const key of requiredKeys) {
|
||||||
|
const value = payload[key]
|
||||||
|
if (value === null || value === undefined || value === '') {
|
||||||
|
delete payload[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variante PATCH (edition d'une ligne EXISTANTE) : remplace les cles requises
|
||||||
|
* laissees vides par une chaine vide `''` au lieu de les OMETTRE.
|
||||||
|
*
|
||||||
|
* Pourquoi pas `omitEmptyRequired` en edition : un PATCH a semantique merge — une
|
||||||
|
* cle absente laisse la valeur serveur INCHANGEE. Vider un champ requis puis valider
|
||||||
|
* renverrait alors un 200 trompeur (l'ancienne valeur est conservee). En envoyant
|
||||||
|
* `''`, la propriete `?string` est bien deserialisee (pas de 400 de type, contrairement
|
||||||
|
* a `null` sur une colonne non-nullable), puis le Validator `NotBlank(trim)` la rejette
|
||||||
|
* -> 422 avec propertyPath, mappee inline sous le champ. Mute et retourne le payload.
|
||||||
|
*/
|
||||||
|
export function blankEmptyRequired<T extends Record<string, unknown>>(
|
||||||
|
payload: T,
|
||||||
|
requiredKeys: readonly string[],
|
||||||
|
): T {
|
||||||
|
for (const key of requiredKeys) {
|
||||||
|
const value = payload[key]
|
||||||
|
if (value === null || value === undefined || value === '') {
|
||||||
|
(payload as Record<string, unknown>)[key] = ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload
|
||||||
|
}
|
||||||
+78
@@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Module\Commercial\Application\Validator;
|
||||||
|
|
||||||
|
use ApiPlatform\Validator\Exception\ValidationException;
|
||||||
|
use App\Module\Commercial\Domain\Entity\Supplier;
|
||||||
|
use Symfony\Component\Validator\ConstraintViolation;
|
||||||
|
use Symfony\Component\Validator\ConstraintViolationList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validator metier (spec-front M2 § Onglet Comptabilite) : a la soumission
|
||||||
|
* complete de l'onglet Comptabilite, les six champs scalaires obligatoires
|
||||||
|
* doivent etre renseignes (SIREN, Numero de compte, Mode de TVA, N de TVA, Delai
|
||||||
|
* de reglement, Type de reglement). La banque reste conditionnelle (RG-2.07) et
|
||||||
|
* les RIB aussi (RG-2.08) : ils ne sont pas couverts ici (Assert\Callback sur
|
||||||
|
* l'entite Supplier — validatePaymentTypeConsistency).
|
||||||
|
*
|
||||||
|
* Parti pris (miroir ClientAccountingCompletenessValidator M1) : colonnes nullable
|
||||||
|
* en base + validateur contextuel, plutot qu'un Assert\NotBlank sur l'entite (qui
|
||||||
|
* casserait le POST de l'onglet principal, lequel n'envoie aucun champ comptable).
|
||||||
|
*
|
||||||
|
* Invoque par le SupplierProcessor uniquement quand le payload porte les six
|
||||||
|
* champs (= une validation d'onglet), jamais sur un PATCH ciblant un seul champ.
|
||||||
|
*
|
||||||
|
* Leve une ValidationException (HTTP 422) listant chaque champ manquant, par
|
||||||
|
* coherence avec les violations Symfony rendues par API Platform (mapping inline
|
||||||
|
* front via useFormErrors, ERP-101).
|
||||||
|
*/
|
||||||
|
final class SupplierAccountingCompletenessValidator
|
||||||
|
{
|
||||||
|
public function validate(Supplier $supplier): void
|
||||||
|
{
|
||||||
|
// Map champ -> valeur courante des champs obligatoires de l'onglet.
|
||||||
|
$fields = [
|
||||||
|
'siren' => $supplier->getSiren(),
|
||||||
|
'accountNumber' => $supplier->getAccountNumber(),
|
||||||
|
'tvaMode' => $supplier->getTvaMode(),
|
||||||
|
'nTva' => $supplier->getNTva(),
|
||||||
|
'paymentDelay' => $supplier->getPaymentDelay(),
|
||||||
|
'paymentType' => $supplier->getPaymentType(),
|
||||||
|
];
|
||||||
|
|
||||||
|
$violations = new ConstraintViolationList();
|
||||||
|
|
||||||
|
foreach ($fields as $property => $value) {
|
||||||
|
if ($this->isMissing($value)) {
|
||||||
|
$violations->add(new ConstraintViolation(
|
||||||
|
'Ce champ est obligatoire.',
|
||||||
|
null,
|
||||||
|
[],
|
||||||
|
$supplier,
|
||||||
|
$property,
|
||||||
|
$value,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($violations) > 0) {
|
||||||
|
throw new ValidationException($violations);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Une valeur est manquante si null ou, pour une chaine, vide apres trim. Les
|
||||||
|
* references (TvaMode / PaymentDelay / PaymentType) ne sont manquantes que
|
||||||
|
* lorsqu'elles valent null.
|
||||||
|
*/
|
||||||
|
private function isMissing(mixed $value): bool
|
||||||
|
{
|
||||||
|
if (null === $value) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return is_string($value) && '' === trim($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
-82
@@ -1,82 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace App\Module\Commercial\Application\Validator;
|
|
||||||
|
|
||||||
use ApiPlatform\Validator\Exception\ValidationException;
|
|
||||||
use App\Module\Commercial\Domain\Entity\Supplier;
|
|
||||||
use Symfony\Component\Validator\ConstraintViolation;
|
|
||||||
use Symfony\Component\Validator\ConstraintViolationList;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validator metier RG-2.03 (completude Information cote fournisseur) :
|
|
||||||
* pour un utilisateur portant le role metier Commerciale, TOUS les champs de
|
|
||||||
* l'onglet Information sont obligatoires sur POST comme sur tout PATCH,
|
|
||||||
* independamment des champs reellement envoyes.
|
|
||||||
*
|
|
||||||
* Invoque par le SupplierProcessor des que l'utilisateur courant porte le role
|
|
||||||
* Commerciale (detection du role cote back). Pour les autres roles, ces champs
|
|
||||||
* restent optionnels — le validator n'est pas appele.
|
|
||||||
*
|
|
||||||
* NEW vs Client : ajoute le champ `volumeForecast` (volume previsionnel),
|
|
||||||
* specifique fournisseur.
|
|
||||||
*
|
|
||||||
* Leve une ValidationException (HTTP 422) listant chaque champ manquant, chaque
|
|
||||||
* violation portant son propertyPath (consommable par extractApiViolations,
|
|
||||||
* ERP-101), par coherence avec les violations Symfony rendues par API Platform.
|
|
||||||
*/
|
|
||||||
final class SupplierInformationCompletenessValidator
|
|
||||||
{
|
|
||||||
public function validate(Supplier $supplier): void
|
|
||||||
{
|
|
||||||
// Map champ -> valeur courante de l'onglet Information.
|
|
||||||
$fields = [
|
|
||||||
'description' => $supplier->getDescription(),
|
|
||||||
'competitors' => $supplier->getCompetitors(),
|
|
||||||
'foundedAt' => $supplier->getFoundedAt(),
|
|
||||||
'employeesCount' => $supplier->getEmployeesCount(),
|
|
||||||
'revenueAmount' => $supplier->getRevenueAmount(),
|
|
||||||
'directorName' => $supplier->getDirectorName(),
|
|
||||||
'profitAmount' => $supplier->getProfitAmount(),
|
|
||||||
'volumeForecast' => $supplier->getVolumeForecast(),
|
|
||||||
];
|
|
||||||
|
|
||||||
$violations = new ConstraintViolationList();
|
|
||||||
|
|
||||||
foreach ($fields as $property => $value) {
|
|
||||||
if ($this->isMissing($value)) {
|
|
||||||
$violations->add(new ConstraintViolation(
|
|
||||||
// Pas de nom de champ technique dans le message : la violation est
|
|
||||||
// deja rattachee au bon champ via son propertyPath (mappe inline
|
|
||||||
// cote front par useFormErrors).
|
|
||||||
'Ce champ est obligatoire pour le rôle Commerciale.',
|
|
||||||
null,
|
|
||||||
[],
|
|
||||||
$supplier,
|
|
||||||
$property,
|
|
||||||
$value,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($violations) > 0) {
|
|
||||||
throw new ValidationException($violations);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Une valeur est manquante si null ou, pour une chaine, vide apres trim. Les
|
|
||||||
* zeros numeriques (employeesCount = 0, profitAmount = "0.00",
|
|
||||||
* volumeForecast = 0) sont des valeurs valides : on ne les considere pas
|
|
||||||
* manquants.
|
|
||||||
*/
|
|
||||||
private function isMissing(mixed $value): bool
|
|
||||||
{
|
|
||||||
if (null === $value) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return is_string($value) && '' === trim($value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -328,8 +328,11 @@ class Supplier implements TimestampableInterface, BlamableInterface
|
|||||||
* chaque 422 porte un propertyPath exploitable par extractApiViolations
|
* chaque 422 porte un propertyPath exploitable par extractApiViolations
|
||||||
* (mapping inline sous le champ, pas un toast — convention ERP-101).
|
* (mapping inline sous le champ, pas un toast — convention ERP-101).
|
||||||
* - RG-2.07 : paymentType = VIREMENT impose une banque -> violation sur `bank`.
|
* - RG-2.07 : paymentType = VIREMENT impose une banque -> violation sur `bank`.
|
||||||
* - RG-2.08 : paymentType = LCR impose au moins un RIB -> violation sur `ribs`
|
* - RG-2.08 : paymentType = LCR impose au moins un RIB -> violation sur
|
||||||
* (le 409 sur DELETE du dernier RIB en LCR est porte par ERP-88).
|
* `paymentType` (miroir client : `ribs` n'a pas de champ de formulaire ou
|
||||||
|
* s'ancrer quand la liste est vide ; l'erreur s'affiche donc sous le select
|
||||||
|
* « Type de règlement », bindé cote front). Le 409 sur DELETE du dernier RIB
|
||||||
|
* en LCR est porte par ERP-88.
|
||||||
*
|
*
|
||||||
* Ces champs vivant dans le groupe d'ecriture comptable (absent du POST, qui
|
* Ces champs vivant dans le groupe d'ecriture comptable (absent du POST, qui
|
||||||
* n'expose que supplier:write:main), la contrainte ne mord en pratique que
|
* n'expose que supplier:write:main), la contrainte ne mord en pratique que
|
||||||
@@ -349,7 +352,7 @@ class Supplier implements TimestampableInterface, BlamableInterface
|
|||||||
|
|
||||||
if (self::PAYMENT_TYPE_LCR === $paymentCode && $this->ribs->isEmpty()) {
|
if (self::PAYMENT_TYPE_LCR === $paymentCode && $this->ribs->isEmpty()) {
|
||||||
$context->buildViolation('Au moins un RIB est obligatoire pour le type de règlement LCR.')
|
$context->buildViolation('Au moins un RIB est obligatoire pour le type de règlement LCR.')
|
||||||
->atPath('ribs')
|
->atPath('paymentType')
|
||||||
->addViolation()
|
->addViolation()
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -199,12 +199,14 @@ class SupplierAddress implements TimestampableInterface, BlamableInterface
|
|||||||
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
||||||
private Collection $contacts;
|
private Collection $contacts;
|
||||||
|
|
||||||
// RG-2.10 : categories d'adresse de type FOURNISSEUR (controle au Processor).
|
// RG-2.10 : au moins une categorie de type FOURNISSEUR par adresse (le type est
|
||||||
|
// controle par validateCategoryType ; le minimum par Assert\Count, miroir sites).
|
||||||
/** @var Collection<int, CategoryInterface> */
|
/** @var Collection<int, CategoryInterface> */
|
||||||
#[ORM\ManyToMany(targetEntity: CategoryInterface::class)]
|
#[ORM\ManyToMany(targetEntity: CategoryInterface::class)]
|
||||||
#[ORM\JoinTable(name: 'supplier_address_category')]
|
#[ORM\JoinTable(name: 'supplier_address_category')]
|
||||||
#[ORM\JoinColumn(name: 'supplier_address_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
|
#[ORM\JoinColumn(name: 'supplier_address_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
|
||||||
#[ORM\InverseJoinColumn(name: 'category_id', referencedColumnName: 'id', onDelete: 'RESTRICT')]
|
#[ORM\InverseJoinColumn(name: 'category_id', referencedColumnName: 'id', onDelete: 'RESTRICT')]
|
||||||
|
#[Assert\Count(min: 1, minMessage: 'Au moins une catégorie est obligatoire.')]
|
||||||
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
||||||
private Collection $categories;
|
private Collection $categories;
|
||||||
|
|
||||||
|
|||||||
+34
-42
@@ -7,10 +7,8 @@ namespace App\Module\Commercial\Infrastructure\ApiPlatform\State\Processor;
|
|||||||
use ApiPlatform\Metadata\Operation;
|
use ApiPlatform\Metadata\Operation;
|
||||||
use ApiPlatform\State\ProcessorInterface;
|
use ApiPlatform\State\ProcessorInterface;
|
||||||
use App\Module\Commercial\Application\Service\SupplierFieldNormalizer;
|
use App\Module\Commercial\Application\Service\SupplierFieldNormalizer;
|
||||||
use App\Module\Commercial\Application\Validator\SupplierInformationCompletenessValidator;
|
use App\Module\Commercial\Application\Validator\SupplierAccountingCompletenessValidator;
|
||||||
use App\Module\Commercial\Domain\Entity\Supplier;
|
use App\Module\Commercial\Domain\Entity\Supplier;
|
||||||
use App\Shared\Domain\Contract\BusinessRoleAwareInterface;
|
|
||||||
use App\Shared\Domain\Security\BusinessRoles;
|
|
||||||
use DateTimeImmutable;
|
use DateTimeImmutable;
|
||||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
@@ -43,19 +41,17 @@ use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|||||||
* collisions d'unicite en 409 (RG-2.11 doublon de nom ; RG-2.15 conflit de
|
* collisions d'unicite en 409 (RG-2.11 doublon de nom ; RG-2.15 conflit de
|
||||||
* restauration).
|
* restauration).
|
||||||
*
|
*
|
||||||
* Validators metier (ERP-89). Decision figee : ce processor ne porte QUE
|
* Validators metier (ERP-89). Ce processor porte la completude Comptabilite : a
|
||||||
* RG-2.03 (completude Information exigee pour le role Commerciale — detection du
|
* la validation complete de l'onglet (les six scalaires obligatoires presents
|
||||||
* role cote back, non exprimable en contrainte d'entite). Les RG inter-champs
|
* dans le payload), chacun doit etre renseigne. (RG-2.03 « Information obligatoire
|
||||||
* RG-2.07 (Virement -> banque), RG-2.08 (LCR -> >= 1 RIB) et RG-2.10 (categorie
|
* pour la Commerciale » a ete retiree, miroir client M1 — l'onglet Information est
|
||||||
* de type FOURNISSEUR) sont portees par des Assert\Callback + ->atPath() sur
|
* desormais entierement facultatif, quel que soit le role.)
|
||||||
* l'entite Supplier (jouees par API Platform AVANT ce processor), pour que
|
|
||||||
* chaque 422 porte un propertyPath consommable par extractApiViolations
|
|
||||||
* (mapping inline, pas un toast — convention ERP-101).
|
|
||||||
*
|
*
|
||||||
* Note : la validation Symfony (Assert\NotBlank, Assert\Count sur categories,
|
* Les RG inter-champs RG-2.07 (Virement -> banque), RG-2.08 (LCR -> >= 1 RIB) et
|
||||||
* les Callback RG-2.07/2.08/2.10...) est jouee par API Platform AVANT ce
|
* RG-2.10 (categorie de type FOURNISSEUR) sont portees par des Assert\Callback +
|
||||||
* processor ; on n'y traite donc que les regles non exprimables en simples
|
* ->atPath() sur l'entite Supplier (jouees par API Platform AVANT ce processor),
|
||||||
* contraintes d'entite (RG-2.03, qui depend du role de l'utilisateur courant).
|
* pour que chaque 422 porte un propertyPath consommable par extractApiViolations
|
||||||
|
* (mapping inline, pas un toast — convention ERP-101).
|
||||||
*
|
*
|
||||||
* @implements ProcessorInterface<Supplier, Supplier>
|
* @implements ProcessorInterface<Supplier, Supplier>
|
||||||
*/
|
*/
|
||||||
@@ -78,6 +74,14 @@ final class SupplierProcessor implements ProcessorInterface
|
|||||||
'paymentType', 'bank',
|
'paymentType', 'bank',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Champs comptables obligatoires a la validation complete de l'onglet
|
||||||
|
* (spec-front M2 § Onglet Comptabilite). bank est exclu : conditionnel (RG-2.07).
|
||||||
|
*/
|
||||||
|
private const array ACCOUNTING_REQUIRED_FIELDS = [
|
||||||
|
'siren', 'accountNumber', 'tvaMode', 'nTva', 'paymentDelay', 'paymentType',
|
||||||
|
];
|
||||||
|
|
||||||
/** Champ d'archivage (groupe supplier:write:archive). */
|
/** Champ d'archivage (groupe supplier:write:archive). */
|
||||||
private const string ARCHIVE_FIELD = 'isArchived';
|
private const string ARCHIVE_FIELD = 'isArchived';
|
||||||
|
|
||||||
@@ -102,7 +106,7 @@ final class SupplierProcessor implements ProcessorInterface
|
|||||||
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
||||||
private readonly ProcessorInterface $persistProcessor,
|
private readonly ProcessorInterface $persistProcessor,
|
||||||
private readonly SupplierFieldNormalizer $normalizer,
|
private readonly SupplierFieldNormalizer $normalizer,
|
||||||
private readonly SupplierInformationCompletenessValidator $informationValidator,
|
private readonly SupplierAccountingCompletenessValidator $accountingValidator,
|
||||||
private readonly Security $security,
|
private readonly Security $security,
|
||||||
private readonly RequestStack $requestStack,
|
private readonly RequestStack $requestStack,
|
||||||
private readonly EntityManagerInterface $em,
|
private readonly EntityManagerInterface $em,
|
||||||
@@ -132,7 +136,7 @@ final class SupplierProcessor implements ProcessorInterface
|
|||||||
// normalisees des deux cotes (l'etat persiste l'a deja ete).
|
// normalisees des deux cotes (l'etat persiste l'a deja ete).
|
||||||
$this->guardManage($data);
|
$this->guardManage($data);
|
||||||
|
|
||||||
$this->validateInformationCompleteness($data);
|
$this->validateAccountingCompleteness($data);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||||
@@ -262,35 +266,23 @@ final class SupplierProcessor implements ProcessorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RG-2.03 : si l'utilisateur porte le role metier Commerciale, TOUS les
|
* spec-front M2 § Onglet Comptabilite : a la validation COMPLETE de l'onglet
|
||||||
* champs de l'onglet Information sont obligatoires sur POST comme sur TOUT
|
* (les six champs obligatoires presents dans le payload — le front les envoie
|
||||||
* PATCH — independamment des champs reellement envoyes. Garantit qu'un
|
* toujours ensemble), chacun doit etre renseigne, sinon 422 par champ. On ne
|
||||||
* fournisseur cree/edite par une Commerciale ne reste jamais avec un onglet
|
* declenche pas sur un PATCH ciblant un sous-ensemble de champs comptables :
|
||||||
* Information incomplet. Pour les autres roles, ces champs restent optionnels.
|
* ce n'est pas une validation d'onglet (edition ponctuelle preservee). bank /
|
||||||
*
|
* RIB restent geres par validatePaymentTypeConsistency sur l'entite (RG-2.07 /
|
||||||
* Consequence (cf. spec § 7, miroir RG-1.04) : le POST n'exposant que
|
* RG-2.08). Miroir du ClientProcessor (M1).
|
||||||
* supplier:write:main, une Commerciale obtient 422 sur tout POST tant que
|
|
||||||
* l'Information n'est pas complete -> la completude se fait via les PATCH
|
|
||||||
* supplier:write:information.
|
|
||||||
*/
|
*/
|
||||||
private function validateInformationCompleteness(Supplier $data): void
|
private function validateAccountingCompleteness(Supplier $data): void
|
||||||
{
|
{
|
||||||
if ($this->currentUserIsCommerciale()) {
|
// Declenche uniquement si TOUS les champs requis sont presents dans le
|
||||||
$this->informationValidator->validate($data);
|
// payload (= soumission d'onglet, pas un PATCH partiel cible).
|
||||||
|
if ([] !== array_diff(self::ACCOUNTING_REQUIRED_FIELDS, $this->payloadKeys())) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$this->accountingValidator->validate($data);
|
||||||
* Detection du role metier Commerciale cote back (jamais front), via le
|
|
||||||
* contrat BusinessRoleAwareInterface (pas d'import de User — regle ABSOLUE
|
|
||||||
* n°1). Identique au ClientProcessor (M1).
|
|
||||||
*/
|
|
||||||
private function currentUserIsCommerciale(): bool
|
|
||||||
{
|
|
||||||
$user = $this->security->getUser();
|
|
||||||
|
|
||||||
return $user instanceof BusinessRoleAwareInterface
|
|
||||||
&& $user->hasBusinessRole(BusinessRoles::COMMERCIALE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -152,7 +152,8 @@ abstract class AbstractSupplierApiTestCase extends AbstractCommercialApiTestCase
|
|||||||
$supplier->setCompanyName(mb_strtoupper($companyName.' '.$suffix, 'UTF-8'));
|
$supplier->setCompanyName(mb_strtoupper($companyName.' '.$suffix, 'UTF-8'));
|
||||||
$supplier->addCategory($this->supplierCategory('NEGOCIANT'));
|
$supplier->addCategory($this->supplierCategory('NEGOCIANT'));
|
||||||
|
|
||||||
// Onglet Information complet (RG-2.03 : exige pour la Commerciale).
|
// Onglet Information complet : donnees de reference pour les tests de
|
||||||
|
// lecture / serialisation / comptabilite (l'Information est facultative).
|
||||||
$supplier->setDescription('Fournisseur de test complet.');
|
$supplier->setDescription('Fournisseur de test complet.');
|
||||||
$supplier->setCompetitors('Concurrent A, Concurrent B');
|
$supplier->setCompetitors('Concurrent A, Concurrent B');
|
||||||
$supplier->setFoundedAt(new DateTimeImmutable('2008-04-01'));
|
$supplier->setFoundedAt(new DateTimeImmutable('2008-04-01'));
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ final class SupplierAccountingApiTest extends AbstractSupplierApiTestCase
|
|||||||
|
|
||||||
// === RG-2.08 : LCR impose au moins un RIB ===
|
// === RG-2.08 : LCR impose au moins un RIB ===
|
||||||
|
|
||||||
public function testLcrWithoutRibReturns422OnRibsPath(): void
|
public function testLcrWithoutRibReturns422OnPaymentTypePath(): void
|
||||||
{
|
{
|
||||||
$client = $this->createAdminClient();
|
$client = $this->createAdminClient();
|
||||||
$seed = $this->seedSupplier('Lcr No Rib');
|
$seed = $this->seedSupplier('Lcr No Rib');
|
||||||
@@ -60,7 +60,9 @@ final class SupplierAccountingApiTest extends AbstractSupplierApiTestCase
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
self::assertResponseStatusCodeSame(422);
|
self::assertResponseStatusCodeSame(422);
|
||||||
self::assertArrayHasKey('ribs', $this->violationsByPath($response->toArray(false)));
|
// Miroir client : violation portee sur `paymentType` (select « Type de
|
||||||
|
// règlement »), `ribs` n'ayant pas de champ de formulaire pour l'ancrer.
|
||||||
|
self::assertArrayHasKey('paymentType', $this->violationsByPath($response->toArray(false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testLcrWithRibReturns200(): void
|
public function testLcrWithRibReturns200(): void
|
||||||
@@ -77,5 +79,58 @@ final class SupplierAccountingApiTest extends AbstractSupplierApiTestCase
|
|||||||
self::assertResponseStatusCodeSame(200);
|
self::assertResponseStatusCodeSame(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === Completude de l'onglet Comptabilite (six scalaires obligatoires) ===
|
||||||
|
|
||||||
|
/**
|
||||||
|
* spec-front M2 § Onglet Comptabilite : a la validation COMPLETE de l'onglet
|
||||||
|
* (les six champs requis presents dans le payload), chacun vide doit renvoyer
|
||||||
|
* une 422 sur son propre propertyPath (mapping inline front, ERP-101). Miroir
|
||||||
|
* du comportement client (ClientAccountingCompletenessValidator).
|
||||||
|
*/
|
||||||
|
public function testIncompleteAccountingTabReturns422OnEachField(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedSupplier('Accounting Incomplete');
|
||||||
|
|
||||||
|
$response = $client->request('PATCH', '/api/suppliers/'.$seed->getId(), [
|
||||||
|
'headers' => ['Content-Type' => self::MERGE, 'Accept' => self::LD],
|
||||||
|
'json' => [
|
||||||
|
'siren' => null,
|
||||||
|
'accountNumber' => null,
|
||||||
|
'tvaMode' => null,
|
||||||
|
'nTva' => null,
|
||||||
|
'paymentDelay' => null,
|
||||||
|
'paymentType' => null,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(422);
|
||||||
|
$paths = $this->violationsByPath($response->toArray(false));
|
||||||
|
self::assertArrayHasKey('siren', $paths);
|
||||||
|
self::assertArrayHasKey('accountNumber', $paths);
|
||||||
|
self::assertArrayHasKey('tvaMode', $paths);
|
||||||
|
self::assertArrayHasKey('nTva', $paths);
|
||||||
|
self::assertArrayHasKey('paymentDelay', $paths);
|
||||||
|
self::assertArrayHasKey('paymentType', $paths);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Un PATCH ciblant un sous-ensemble de champs comptables n'est PAS une
|
||||||
|
* validation d'onglet : la completude ne se declenche pas (edition ponctuelle
|
||||||
|
* preservee, cf. validateAccountingCompleteness).
|
||||||
|
*/
|
||||||
|
public function testPartialAccountingPatchSkipsCompleteness(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedSupplier('Accounting Partial');
|
||||||
|
|
||||||
|
$client->request('PATCH', '/api/suppliers/'.$seed->getId(), [
|
||||||
|
'headers' => ['Content-Type' => self::MERGE],
|
||||||
|
'json' => ['nTva' => 'FR12345678901'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(200);
|
||||||
|
}
|
||||||
|
|
||||||
// violationsByPath() : helper mutualise dans AbstractSupplierApiTestCase.
|
// violationsByPath() : helper mutualise dans AbstractSupplierApiTestCase.
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ use Symfony\Component\Console\Output\NullOutput;
|
|||||||
/**
|
/**
|
||||||
* Matrice RBAC complete du repertoire fournisseurs par role metier (spec-back M2
|
* Matrice RBAC complete du repertoire fournisseurs par role metier (spec-back M2
|
||||||
* § 2.9 + ERP-90). Valide 200/403 par verbe et par onglet pour
|
* § 2.9 + ERP-90). Valide 200/403 par verbe et par onglet pour
|
||||||
* bureau / compta / commerciale / usine, le gating des champs comptables en
|
* bureau / compta / commerciale / usine et le gating des champs comptables en
|
||||||
* lecture (omission de cle) et le durcissement RG-2.03 (Commerciale) au POST/PATCH.
|
* lecture (omission de cle).
|
||||||
*
|
*
|
||||||
* Les comptes demo et la matrice sont seedes via la commande reelle
|
* Les comptes demo et la matrice sont seedes via la commande reelle
|
||||||
* `app:seed-rbac --with-demo-users` (le MEME chemin qu'en recette), idempotente —
|
* `app:seed-rbac --with-demo-users` (le MEME chemin qu'en recette), idempotente —
|
||||||
@@ -23,7 +23,7 @@ use Symfony\Component\Console\Output\NullOutput;
|
|||||||
* Matrice § 2.9 (ERP-90) — rappel :
|
* Matrice § 2.9 (ERP-90) — rappel :
|
||||||
* - bureau : suppliers.view + manage (ni accounting, ni archive)
|
* - bureau : suppliers.view + manage (ni accounting, ni archive)
|
||||||
* - compta : suppliers.view + accounting.view + accounting.manage (PAS manage)
|
* - compta : suppliers.view + accounting.view + accounting.manage (PAS manage)
|
||||||
* - commerciale : suppliers.view + manage (PAS accounting), durcie RG-2.03
|
* - commerciale : suppliers.view + manage (PAS accounting)
|
||||||
* - usine : aucune permission (403 partout)
|
* - usine : aucune permission (403 partout)
|
||||||
* - archive : admin seul (aucun role metier)
|
* - archive : admin seul (aucun role metier)
|
||||||
*
|
*
|
||||||
@@ -93,7 +93,7 @@ final class SupplierRBACMatrixTest extends AbstractSupplierApiTestCase
|
|||||||
$client->request('GET', '/api/suppliers', ['headers' => ['Accept' => self::LD]]);
|
$client->request('GET', '/api/suppliers', ['headers' => ['Accept' => self::LD]]);
|
||||||
self::assertResponseStatusCodeSame(200);
|
self::assertResponseStatusCodeSame(200);
|
||||||
|
|
||||||
// manage : creation OK (bureau n'est pas gate par RG-2.03)
|
// manage : creation OK
|
||||||
$client->request('POST', '/api/suppliers', [
|
$client->request('POST', '/api/suppliers', [
|
||||||
'headers' => ['Content-Type' => self::LD],
|
'headers' => ['Content-Type' => self::LD],
|
||||||
'json' => $this->validMainPayload('Bureau Created', $cat->getId()),
|
'json' => $this->validMainPayload('Bureau Created', $cat->getId()),
|
||||||
@@ -211,15 +211,13 @@ final class SupplierRBACMatrixTest extends AbstractSupplierApiTestCase
|
|||||||
self::assertResponseStatusCodeSame(200);
|
self::assertResponseStatusCodeSame(200);
|
||||||
|
|
||||||
// manage : la creation passe la security d'operation (pas un 403 comme
|
// manage : la creation passe la security d'operation (pas un 403 comme
|
||||||
// Compta) mais bute sur RG-2.03 (onglet Information incomplet) -> 422.
|
// Compta) -> 201. L'onglet Information est facultatif (RG-2.03 retiree,
|
||||||
$response = $client->request('POST', '/api/suppliers', [
|
// miroir client M1) : une Commerciale cree avec le seul onglet principal.
|
||||||
|
$client->request('POST', '/api/suppliers', [
|
||||||
'headers' => ['Content-Type' => self::LD],
|
'headers' => ['Content-Type' => self::LD],
|
||||||
'json' => $this->validMainPayload('Commerciale Post'),
|
'json' => $this->validMainPayload('Commerciale Post'),
|
||||||
]);
|
]);
|
||||||
self::assertResponseStatusCodeSame(422);
|
self::assertResponseStatusCodeSame(201);
|
||||||
// Le 422 doit bien etre celui de RG-2.03 (onglet Information) et non un
|
|
||||||
// 422 orthogonal : on exige une violation sur un champ de completude.
|
|
||||||
self::assertArrayHasKey('description', $this->violationsByPath($response->toArray(false)));
|
|
||||||
|
|
||||||
// PAS accounting : edition onglet Comptabilite refusee
|
// PAS accounting : edition onglet Comptabilite refusee
|
||||||
$client->request('PATCH', '/api/suppliers/'.$seed->getId(), [
|
$client->request('PATCH', '/api/suppliers/'.$seed->getId(), [
|
||||||
@@ -251,50 +249,6 @@ final class SupplierRBACMatrixTest extends AbstractSupplierApiTestCase
|
|||||||
self::assertArrayNotHasKey('ribs', $data);
|
self::assertArrayNotHasKey('ribs', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRG203CommercialePostIncompleteIs422AdminIs201(): void
|
|
||||||
{
|
|
||||||
$cat = $this->supplierCategory('NEGOCIANT');
|
|
||||||
|
|
||||||
// RG-2.03 : Commerciale POST sans onglet Information complet -> 422.
|
|
||||||
$commerciale = $this->authAs('commerciale');
|
|
||||||
$response = $commerciale->request('POST', '/api/suppliers', [
|
|
||||||
'headers' => ['Content-Type' => self::LD],
|
|
||||||
'json' => $this->validMainPayload('RG203 Commerciale', $cat->getId()),
|
|
||||||
]);
|
|
||||||
self::assertResponseStatusCodeSame(422);
|
|
||||||
self::assertArrayHasKey('description', $this->violationsByPath($response->toArray(false)));
|
|
||||||
|
|
||||||
// Meme payload par un Admin (non gate par RG-2.03) -> 201.
|
|
||||||
$admin = $this->createAdminClient();
|
|
||||||
$admin->request('POST', '/api/suppliers', [
|
|
||||||
'headers' => ['Content-Type' => self::LD],
|
|
||||||
'json' => $this->validMainPayload('RG203 Admin', $cat->getId()),
|
|
||||||
]);
|
|
||||||
self::assertResponseStatusCodeSame(201);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRG203CommercialePatchIncompleteIs422(): void
|
|
||||||
{
|
|
||||||
// RG-2.03 : tout PATCH par une Commerciale exige l'Information complete.
|
|
||||||
// Le fournisseur seede a une Information vide -> meme un PATCH du nom -> 422.
|
|
||||||
$seed = $this->seedSupplier('Commerciale Patch Incomplete');
|
|
||||||
$commerciale = $this->authAs('commerciale');
|
|
||||||
|
|
||||||
$response = $commerciale->request('PATCH', '/api/suppliers/'.$seed->getId(), [
|
|
||||||
'headers' => ['Content-Type' => self::MERGE],
|
|
||||||
'json' => ['companyName' => 'Commerciale Renamed'],
|
|
||||||
]);
|
|
||||||
self::assertResponseStatusCodeSame(422);
|
|
||||||
self::assertArrayHasKey('description', $this->violationsByPath($response->toArray(false)));
|
|
||||||
|
|
||||||
// Le meme PATCH par un Admin passe (non gate par RG-2.03) -> 200.
|
|
||||||
$admin = $this->createAdminClient();
|
|
||||||
$admin->request('PATCH', '/api/suppliers/'.$seed->getId(), [
|
|
||||||
'headers' => ['Content-Type' => self::MERGE],
|
|
||||||
'json' => ['companyName' => 'Admin Renamed'],
|
|
||||||
]);
|
|
||||||
self::assertResponseStatusCodeSame(200);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function authAs(string $role): Client
|
private function authAs(string $role): Client
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -172,8 +172,9 @@ final class SupplierSubResourceApiTest extends AbstractSupplierApiTestCase
|
|||||||
public function testPostAddressWithIncoherentCityAndPostalCodeReturns201(): void
|
public function testPostAddressWithIncoherentCityAndPostalCodeReturns201(): void
|
||||||
{
|
{
|
||||||
$this->skipIfSitesModuleDisabled();
|
$this->skipIfSitesModuleDisabled();
|
||||||
$client = $this->createAdminClient();
|
$client = $this->createAdminClient();
|
||||||
$seed = $this->seedSupplier('Address Incoherent');
|
$seed = $this->seedSupplier('Address Incoherent');
|
||||||
|
$category = $this->supplierCategory('NEGOCIANT');
|
||||||
|
|
||||||
// RG-2.05 : pas de controle strict de coherence CP/ville cote serveur.
|
// RG-2.05 : pas de controle strict de coherence CP/ville cote serveur.
|
||||||
$client->request('POST', '/api/suppliers/'.$seed->getId().'/addresses', [
|
$client->request('POST', '/api/suppliers/'.$seed->getId().'/addresses', [
|
||||||
@@ -184,6 +185,7 @@ final class SupplierSubResourceApiTest extends AbstractSupplierApiTestCase
|
|||||||
'city' => 'Marseille',
|
'city' => 'Marseille',
|
||||||
'street' => '1 rue du Test',
|
'street' => '1 rue du Test',
|
||||||
'sites' => [$this->firstSiteIri()],
|
'sites' => [$this->firstSiteIri()],
|
||||||
|
'categories' => ['/api/categories/'.$category->getId()],
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -217,9 +219,10 @@ final class SupplierSubResourceApiTest extends AbstractSupplierApiTestCase
|
|||||||
public function testPostAddressWithEachValidTypeReturns201(): void
|
public function testPostAddressWithEachValidTypeReturns201(): void
|
||||||
{
|
{
|
||||||
$this->skipIfSitesModuleDisabled();
|
$this->skipIfSitesModuleDisabled();
|
||||||
$client = $this->createAdminClient();
|
$client = $this->createAdminClient();
|
||||||
$seed = $this->seedSupplier('Address Types');
|
$seed = $this->seedSupplier('Address Types');
|
||||||
$siteIri = $this->firstSiteIri();
|
$siteIri = $this->firstSiteIri();
|
||||||
|
$category = $this->supplierCategory('NEGOCIANT');
|
||||||
|
|
||||||
foreach (['PROSPECT', 'DEPART', 'RENDU'] as $type) {
|
foreach (['PROSPECT', 'DEPART', 'RENDU'] as $type) {
|
||||||
$client->request('POST', '/api/suppliers/'.$seed->getId().'/addresses', [
|
$client->request('POST', '/api/suppliers/'.$seed->getId().'/addresses', [
|
||||||
@@ -230,6 +233,7 @@ final class SupplierSubResourceApiTest extends AbstractSupplierApiTestCase
|
|||||||
'city' => 'Châtellerault',
|
'city' => 'Châtellerault',
|
||||||
'street' => '1 rue du Test',
|
'street' => '1 rue du Test',
|
||||||
'sites' => [$siteIri],
|
'sites' => [$siteIri],
|
||||||
|
'categories' => ['/api/categories/'.$category->getId()],
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
self::assertResponseStatusCodeSame(201, sprintf('addressType=%s doit etre accepte.', $type));
|
self::assertResponseStatusCodeSame(201, sprintf('addressType=%s doit etre accepte.', $type));
|
||||||
|
|||||||
@@ -87,12 +87,14 @@ final class SupplierValidationTest extends TestCase
|
|||||||
|
|
||||||
// === RG-2.08 : LCR impose au moins un RIB ===
|
// === RG-2.08 : LCR impose au moins un RIB ===
|
||||||
|
|
||||||
public function testLcrWithoutRibIsRejectedOnRibsPath(): void
|
public function testLcrWithoutRibIsRejectedOnPaymentTypePath(): void
|
||||||
{
|
{
|
||||||
$supplier = $this->validSupplier();
|
$supplier = $this->validSupplier();
|
||||||
$supplier->setPaymentType($this->paymentType('LCR'));
|
$supplier->setPaymentType($this->paymentType('LCR'));
|
||||||
|
|
||||||
self::assertContains('ribs', $this->violationPaths($supplier));
|
// Miroir client : la violation LCR -> >= 1 RIB est portee sur `paymentType`
|
||||||
|
// (affichee sous le select « Type de règlement », `ribs` n'ayant pas de champ).
|
||||||
|
self::assertContains('paymentType', $this->violationPaths($supplier));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testLcrWithRibPasses(): void
|
public function testLcrWithRibPasses(): void
|
||||||
@@ -101,7 +103,7 @@ final class SupplierValidationTest extends TestCase
|
|||||||
$supplier->setPaymentType($this->paymentType('LCR'));
|
$supplier->setPaymentType($this->paymentType('LCR'));
|
||||||
$supplier->addRib(new SupplierRib());
|
$supplier->addRib(new SupplierRib());
|
||||||
|
|
||||||
self::assertNotContains('ribs', $this->violationPaths($supplier));
|
self::assertNotContains('paymentType', $this->violationPaths($supplier));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNeutralPaymentTypeRequiresNeitherBankNorRib(): void
|
public function testNeutralPaymentTypeRequiresNeitherBankNorRib(): void
|
||||||
|
|||||||
@@ -1,129 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace App\Tests\Module\Commercial\Unit;
|
|
||||||
|
|
||||||
use ApiPlatform\Validator\Exception\ValidationException;
|
|
||||||
use App\Module\Commercial\Application\Validator\SupplierInformationCompletenessValidator;
|
|
||||||
use App\Module\Commercial\Domain\Entity\Supplier;
|
|
||||||
use DateTimeImmutable;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests unitaires du SupplierInformationCompletenessValidator (RG-2.03) : pour le
|
|
||||||
* role Commerciale, TOUS les champs de l'onglet Information sont obligatoires.
|
|
||||||
* Chaque champ manquant produit une violation portant son propertyPath (ERP-101).
|
|
||||||
*
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
final class SupplierInformationCompletenessValidatorTest extends TestCase
|
|
||||||
{
|
|
||||||
public function testCompleteInformationPasses(): void
|
|
||||||
{
|
|
||||||
$supplier = $this->completeSupplier();
|
|
||||||
|
|
||||||
$this->validator()->validate($supplier);
|
|
||||||
|
|
||||||
// Aucune exception levee : la completude est satisfaite.
|
|
||||||
$this->addToAssertionCount(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testEmptyInformationListsEveryMissingField(): void
|
|
||||||
{
|
|
||||||
$supplier = new Supplier();
|
|
||||||
$supplier->setCompanyName('Recycla SAS'); // onglet principal, hors Information
|
|
||||||
|
|
||||||
try {
|
|
||||||
$this->validator()->validate($supplier);
|
|
||||||
self::fail('Une ValidationException etait attendue (onglet Information vide).');
|
|
||||||
} catch (ValidationException $e) {
|
|
||||||
$paths = [];
|
|
||||||
foreach ($e->getConstraintViolationList() as $violation) {
|
|
||||||
$paths[] = $violation->getPropertyPath();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Les 8 champs Information (dont volumeForecast, NEW vs Client) sont
|
|
||||||
// tous signales d'un coup, chacun sous son propre propertyPath.
|
|
||||||
sort($paths);
|
|
||||||
self::assertSame([
|
|
||||||
'competitors',
|
|
||||||
'description',
|
|
||||||
'directorName',
|
|
||||||
'employeesCount',
|
|
||||||
'foundedAt',
|
|
||||||
'profitAmount',
|
|
||||||
'revenueAmount',
|
|
||||||
'volumeForecast',
|
|
||||||
], $paths);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testPartialInformationReportsOnlyMissingFields(): void
|
|
||||||
{
|
|
||||||
$supplier = $this->completeSupplier();
|
|
||||||
$supplier->setDirectorName(null);
|
|
||||||
$supplier->setVolumeForecast(null);
|
|
||||||
|
|
||||||
try {
|
|
||||||
$this->validator()->validate($supplier);
|
|
||||||
self::fail('Une ValidationException etait attendue (2 champs manquants).');
|
|
||||||
} catch (ValidationException $e) {
|
|
||||||
$paths = [];
|
|
||||||
foreach ($e->getConstraintViolationList() as $violation) {
|
|
||||||
$paths[] = $violation->getPropertyPath();
|
|
||||||
}
|
|
||||||
|
|
||||||
sort($paths);
|
|
||||||
self::assertSame(['directorName', 'volumeForecast'], $paths);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testZeroNumericValuesAreNotMissing(): void
|
|
||||||
{
|
|
||||||
// employeesCount = 0, profitAmount = "0.00", volumeForecast = 0 sont des
|
|
||||||
// valeurs valides (un zero n'est pas une absence) -> pas de violation.
|
|
||||||
$supplier = $this->completeSupplier();
|
|
||||||
$supplier->setEmployeesCount(0);
|
|
||||||
$supplier->setProfitAmount('0.00');
|
|
||||||
$supplier->setVolumeForecast(0);
|
|
||||||
|
|
||||||
$this->validator()->validate($supplier);
|
|
||||||
|
|
||||||
$this->addToAssertionCount(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBlankStringIsMissing(): void
|
|
||||||
{
|
|
||||||
// Une chaine vide apres trim compte comme manquante.
|
|
||||||
$supplier = $this->completeSupplier();
|
|
||||||
$supplier->setDescription(' ');
|
|
||||||
|
|
||||||
$this->expectException(ValidationException::class);
|
|
||||||
$this->validator()->validate($supplier);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fournisseur dont l'onglet Information est entierement renseigne.
|
|
||||||
*/
|
|
||||||
private function completeSupplier(): Supplier
|
|
||||||
{
|
|
||||||
$supplier = new Supplier();
|
|
||||||
$supplier->setCompanyName('Recycla SAS');
|
|
||||||
$supplier->setDescription('Specialiste du recyclage');
|
|
||||||
$supplier->setCompetitors('Concurrent A, Concurrent B');
|
|
||||||
$supplier->setFoundedAt(new DateTimeImmutable('2010-01-01'));
|
|
||||||
$supplier->setEmployeesCount(42);
|
|
||||||
$supplier->setRevenueAmount('1000000.00');
|
|
||||||
$supplier->setDirectorName('Marie Durand');
|
|
||||||
$supplier->setProfitAmount('150000.00');
|
|
||||||
$supplier->setVolumeForecast(5000);
|
|
||||||
|
|
||||||
return $supplier;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function validator(): SupplierInformationCompletenessValidator
|
|
||||||
{
|
|
||||||
return new SupplierInformationCompletenessValidator();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,244 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace App\Tests\Module\Commercial\Unit;
|
|
||||||
|
|
||||||
use ApiPlatform\Metadata\Operation;
|
|
||||||
use ApiPlatform\State\ProcessorInterface;
|
|
||||||
use ApiPlatform\Validator\Exception\ValidationException;
|
|
||||||
use App\Module\Commercial\Application\Service\SupplierFieldNormalizer;
|
|
||||||
use App\Module\Commercial\Application\Validator\SupplierInformationCompletenessValidator;
|
|
||||||
use App\Module\Commercial\Domain\Entity\Supplier;
|
|
||||||
use App\Module\Commercial\Infrastructure\ApiPlatform\State\Processor\SupplierProcessor;
|
|
||||||
use App\Shared\Domain\Contract\BusinessRoleAwareInterface;
|
|
||||||
use App\Shared\Domain\Security\BusinessRoles;
|
|
||||||
use DateTimeImmutable;
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use Doctrine\ORM\UnitOfWork;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
|
||||||
use Symfony\Component\Security\Core\User\UserInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests unitaires du SupplierProcessor — perimetre ERP-89 : detection du role
|
|
||||||
* Commerciale cote back (RG-2.03). Les autres responsabilites du processor
|
|
||||||
* (gating accounting / archive / mode strict) sont heritees d'ERP-87 et testees
|
|
||||||
* a leur niveau ; les RG inter-champs (RG-2.07/2.08/2.10) sont des contraintes
|
|
||||||
* d'entite (cf. SupplierValidationTest), non portees ici.
|
|
||||||
*
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
final class SupplierProcessorTest extends TestCase
|
|
||||||
{
|
|
||||||
public function testCommercialeIncompleteInformationIsUnprocessable(): void
|
|
||||||
{
|
|
||||||
// RG-2.03 : role Commerciale + onglet Information incomplet -> 422, meme
|
|
||||||
// sur un POST (les champs Information n'y sont pas renseignables).
|
|
||||||
$supplier = $this->minimalSupplier();
|
|
||||||
$supplier->setDescription('Une description'); // les autres champs Information restent null
|
|
||||||
|
|
||||||
$processor = $this->makeProcessor(
|
|
||||||
payload: ['description' => 'Une description'],
|
|
||||||
user: $this->commercialeUser(),
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->expectException(ValidationException::class);
|
|
||||||
$processor->process($supplier, $this->operation());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCommercialeIncompleteInformationOnMainOnlyPatchIsUnprocessable(): void
|
|
||||||
{
|
|
||||||
// RG-2.03 : pour une Commerciale, la completude Information est exigee
|
|
||||||
// meme quand le payload ne touche PAS l'onglet Information (ici
|
|
||||||
// companyName seul) -> 422.
|
|
||||||
$supplier = $this->minimalSupplier();
|
|
||||||
$supplier->setCompanyName('Renamed Co');
|
|
||||||
|
|
||||||
$processor = $this->makeProcessor(
|
|
||||||
granted: ['commercial.suppliers.manage'],
|
|
||||||
payload: ['companyName' => 'Renamed Co'],
|
|
||||||
user: $this->commercialeUser(),
|
|
||||||
managed: true,
|
|
||||||
originalData: [
|
|
||||||
'companyName' => 'TEST CO',
|
|
||||||
'isArchived' => false,
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->expectException(ValidationException::class);
|
|
||||||
$processor->process($supplier, $this->operation());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCommercialeCompleteInformationPasses(): void
|
|
||||||
{
|
|
||||||
// RG-2.03 satisfaite : tous les champs Information renseignes -> 200.
|
|
||||||
$supplier = $this->completeInformationSupplier();
|
|
||||||
|
|
||||||
$processor = $this->makeProcessor(
|
|
||||||
granted: ['commercial.suppliers.manage'],
|
|
||||||
payload: ['description' => 'desc'],
|
|
||||||
user: $this->commercialeUser(),
|
|
||||||
);
|
|
||||||
|
|
||||||
self::assertInstanceOf(Supplier::class, $processor->process($supplier, $this->operation()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testNonCommercialeSkipsInformationCompleteness(): void
|
|
||||||
{
|
|
||||||
// Meme onglet Information incomplet, mais user non-Commerciale -> aucun
|
|
||||||
// blocage (la completude est specifique a la Commerciale).
|
|
||||||
$supplier = $this->minimalSupplier();
|
|
||||||
$supplier->setDescription('Une description');
|
|
||||||
|
|
||||||
$processor = $this->makeProcessor(
|
|
||||||
payload: ['description' => 'Une description'],
|
|
||||||
user: null,
|
|
||||||
);
|
|
||||||
|
|
||||||
self::assertInstanceOf(Supplier::class, $processor->process($supplier, $this->operation()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testAdminIncompleteInformationPasses(): void
|
|
||||||
{
|
|
||||||
// Distinct du cas user=null : un utilisateur AUTHENTIFIE mais non-Commerciale
|
|
||||||
// (ici un admin, BusinessRoleAwareInterface renvoyant false pour tout role
|
|
||||||
// metier) n'est pas soumis a la completude Information -> 200 malgre un
|
|
||||||
// onglet Information incomplet. Prouve que le gate porte bien sur le ROLE
|
|
||||||
// metier Commerciale, et pas sur « il y a un utilisateur connecte ».
|
|
||||||
$supplier = $this->minimalSupplier();
|
|
||||||
$supplier->setDescription('Une description');
|
|
||||||
|
|
||||||
$processor = $this->makeProcessor(
|
|
||||||
payload: ['description' => 'Une description'],
|
|
||||||
user: $this->adminUser(),
|
|
||||||
);
|
|
||||||
|
|
||||||
self::assertInstanceOf(Supplier::class, $processor->process($supplier, $this->operation()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param list<string> $granted Permissions accordees a l'utilisateur courant
|
|
||||||
* @param array<string, mixed> $payload Corps JSON simule de la requete
|
|
||||||
* @param bool $managed true = entite geree par l'ORM (PATCH), false = creation (POST)
|
|
||||||
* @param array<string, mixed> $originalData Etat persiste simule (getOriginalEntityData)
|
|
||||||
*/
|
|
||||||
private function makeProcessor(
|
|
||||||
array $granted = [],
|
|
||||||
array $payload = [],
|
|
||||||
?UserInterface $user = null,
|
|
||||||
bool $managed = false,
|
|
||||||
array $originalData = [],
|
|
||||||
): SupplierProcessor {
|
|
||||||
$persist = new class implements ProcessorInterface {
|
|
||||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
|
||||||
{
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
$security = $this->createStub(Security::class);
|
|
||||||
$security->method('isGranted')->willReturnCallback(
|
|
||||||
static fn (mixed $attribute): bool => is_string($attribute) && in_array($attribute, $granted, true),
|
|
||||||
);
|
|
||||||
$security->method('getUser')->willReturn($user);
|
|
||||||
|
|
||||||
$requestStack = new RequestStack();
|
|
||||||
$requestStack->push(new Request([], [], [], [], [], [], json_encode($payload, JSON_THROW_ON_ERROR)));
|
|
||||||
|
|
||||||
$uow = $this->createMock(UnitOfWork::class);
|
|
||||||
$uow->method('getOriginalEntityData')->willReturn($originalData);
|
|
||||||
|
|
||||||
$em = $this->createMock(EntityManagerInterface::class);
|
|
||||||
$em->method('contains')->willReturn($managed);
|
|
||||||
$em->method('getUnitOfWork')->willReturn($uow);
|
|
||||||
|
|
||||||
return new SupplierProcessor(
|
|
||||||
$persist,
|
|
||||||
new SupplierFieldNormalizer(),
|
|
||||||
new SupplierInformationCompletenessValidator(),
|
|
||||||
$security,
|
|
||||||
$requestStack,
|
|
||||||
$em,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function minimalSupplier(): Supplier
|
|
||||||
{
|
|
||||||
$supplier = new Supplier();
|
|
||||||
$supplier->setCompanyName('Test Co');
|
|
||||||
|
|
||||||
return $supplier;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function completeInformationSupplier(): Supplier
|
|
||||||
{
|
|
||||||
$supplier = $this->minimalSupplier();
|
|
||||||
$supplier->setDescription('desc');
|
|
||||||
$supplier->setCompetitors('concurrents');
|
|
||||||
$supplier->setFoundedAt(new DateTimeImmutable('2010-01-01'));
|
|
||||||
$supplier->setEmployeesCount(10);
|
|
||||||
$supplier->setRevenueAmount('1000.00');
|
|
||||||
$supplier->setDirectorName('Marie Durand');
|
|
||||||
$supplier->setProfitAmount('100.00');
|
|
||||||
$supplier->setVolumeForecast(500);
|
|
||||||
|
|
||||||
return $supplier;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function operation(): Operation
|
|
||||||
{
|
|
||||||
return $this->createStub(Operation::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Utilisateur authentifie non-Commerciale (profil admin) : porte
|
|
||||||
* BusinessRoleAwareInterface mais ne reconnait aucun role metier. Sert a
|
|
||||||
* distinguer « pas de role Commerciale » de « pas d'utilisateur » (null).
|
|
||||||
*/
|
|
||||||
private function adminUser(): UserInterface
|
|
||||||
{
|
|
||||||
return new class implements UserInterface, BusinessRoleAwareInterface {
|
|
||||||
public function hasBusinessRole(string $roleCode): bool
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getRoles(): array
|
|
||||||
{
|
|
||||||
return ['ROLE_ADMIN'];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function eraseCredentials(): void {}
|
|
||||||
|
|
||||||
public function getUserIdentifier(): string
|
|
||||||
{
|
|
||||||
return 'admin-test';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private function commercialeUser(): UserInterface
|
|
||||||
{
|
|
||||||
return new class implements UserInterface, BusinessRoleAwareInterface {
|
|
||||||
public function hasBusinessRole(string $roleCode): bool
|
|
||||||
{
|
|
||||||
return BusinessRoles::COMMERCIALE === $roleCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getRoles(): array
|
|
||||||
{
|
|
||||||
return ['ROLE_USER'];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function eraseCredentials(): void {}
|
|
||||||
|
|
||||||
public function getUserIdentifier(): string
|
|
||||||
{
|
|
||||||
return 'commerciale-test';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user