Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fea325e10f | |||
| e139d234a9 |
+1
-1
@@ -1,2 +1,2 @@
|
||||
parameters:
|
||||
app.version: '0.1.81'
|
||||
app.version: '0.1.82'
|
||||
|
||||
@@ -883,6 +883,7 @@ Cf. § 2.6. Pattern Shared standard.
|
||||
|
||||
### Onglet Comptabilité
|
||||
|
||||
- **RG-1.30** _(ajoutée — correctif incohérence spec-front/spec-back)_ : à la **validation complète de l'onglet Comptabilité**, les six champs scalaires `siren`, `accountNumber`, `tvaMode`, `nTva`, `paymentDelay`, `paymentType` sont **obligatoires** (alignement sur spec-front § Onglet Comptabilité). Colonnes `nullable` en base (l'onglet est rempli dans un second temps, et l'onglet principal ne les envoie pas) + validateur contextuel `ClientAccountingCompletenessValidator` invoqué par le `ClientProcessor` — même parti que RG-1.04 (Information). Déclenchement : uniquement quand **les six champs sont présents dans le payload** (le front les envoie toujours ensemble via « Valider ») ; un PATCH ciblant un sous-ensemble de champs comptables (édition ponctuelle) n'est pas soumis à la complétude. Chaque champ manquant → 422 sur son `propertyPath` (mapping inline front, ERP-101). `bank` reste hors complétude (conditionnel RG-1.12).
|
||||
- **RG-1.12** : Le champ `bank` est visible et obligatoire **uniquement** si `paymentType.code = 'VIREMENT'`. Validation server-side dans le `ClientProcessor` : si `payment_type.code = VIREMENT` et `bank IS NULL` → 422.
|
||||
- **RG-1.13** : Les champs RIB (`label`, `bic`, `iban`) sont obligatoires si **au moins un bloc RIB est présent ET** `paymentType.code = 'LCR'`. C'est-à-dire :
|
||||
- Si `paymentType.code = LCR` ET `client.ribs.count() = 0` → 422 « Au moins un RIB est obligatoire pour le type LCR ».
|
||||
|
||||
@@ -168,13 +168,18 @@
|
||||
"prospect": "Prospect",
|
||||
"delivery": "Adresse de livraison",
|
||||
"billing": "Facturation",
|
||||
"addressType": "Type d'adresse",
|
||||
"addressTypeProspect": "Prospect",
|
||||
"addressTypeDelivery": "Livraison",
|
||||
"addressTypeBilling": "Facturation",
|
||||
"addressTypeDeliveryBilling": "Adresse + Facturation",
|
||||
"categories": "Catégorie",
|
||||
"country": "Pays",
|
||||
"postalCode": "Code postal",
|
||||
"city": "Ville",
|
||||
"street": "Adresse",
|
||||
"streetComplement": "Adresse complémentaire",
|
||||
"sites": "Sites Starseed",
|
||||
"sites": "Sites",
|
||||
"contacts": "Contact(s) rattaché(s)",
|
||||
"billingEmail": "Email de facturation",
|
||||
"remove": "Supprimer l'adresse",
|
||||
|
||||
@@ -10,34 +10,53 @@
|
||||
@click="$emit('remove')"
|
||||
/>
|
||||
|
||||
<!-- Usage de l'adresse : Prospect exclusif de Livraison/Facturation
|
||||
(RG-1.06/07/08). L'exclusivite est appliquee au toggle (cocher l'un
|
||||
decoche l'autre) plutot qu'en masquant les options. -->
|
||||
<MalioCheckbox
|
||||
:model-value="model.isProspect"
|
||||
:label="t('commercial.clients.form.address.prospect')"
|
||||
group-class="self-center"
|
||||
<!-- Usage de l'adresse : Select unique (plus simple pour l'utilisateur)
|
||||
remplacant les 3 cases. Les options encodent les combinaisons valides
|
||||
(exclusivite Prospect, RG-1.06/07/08) ; le back recoit toujours les
|
||||
drapeaux isProspect / isDelivery / isBilling (aucune RG modifiee). -->
|
||||
<MalioSelect
|
||||
:model-value="addressType"
|
||||
:options="addressTypeOptions"
|
||||
:label="t('commercial.clients.form.address.addressType')"
|
||||
:readonly="readonly"
|
||||
@update:model-value="(v: boolean) => toggleFlag('isProspect', v)"
|
||||
/>
|
||||
<MalioCheckbox
|
||||
:model-value="model.isDelivery"
|
||||
:label="t('commercial.clients.form.address.delivery')"
|
||||
group-class="self-center"
|
||||
:readonly="readonly"
|
||||
@update:model-value="(v: boolean) => toggleFlag('isDelivery', v)"
|
||||
/>
|
||||
<MalioCheckbox
|
||||
:model-value="model.isBilling"
|
||||
:label="t('commercial.clients.form.address.billing')"
|
||||
group-class="self-center"
|
||||
:readonly="readonly"
|
||||
@update:model-value="(v: boolean) => toggleFlag('isBilling', v)"
|
||||
:required="true"
|
||||
@update:model-value="onAddressTypeChange"
|
||||
/>
|
||||
|
||||
<!-- Cellule vide : laisse un trou en position 4 (ligne 1) pour que
|
||||
Categorie reparte au debut de la ligne suivante. -->
|
||||
<div aria-hidden="true" />
|
||||
<!-- Sites Starseed : multiselect a tags (>= 1 obligatoire, RG-1.10). -->
|
||||
<MalioSelectCheckbox
|
||||
:model-value="model.siteIris"
|
||||
:options="siteOptions"
|
||||
:label="t('commercial.clients.form.address.sites')"
|
||||
:display-tag="true"
|
||||
:readonly="readonly"
|
||||
:required="true"
|
||||
@update:model-value="(v: (string | number)[]) => update('siteIris', v.map(String))"
|
||||
/>
|
||||
|
||||
<MalioSelectCheckbox
|
||||
:model-value="model.contactIris"
|
||||
:options="contactOptions"
|
||||
:label="t('commercial.clients.form.address.contacts')"
|
||||
:display-tag="true"
|
||||
:readonly="readonly"
|
||||
@update:model-value="(v: (string | number)[]) => update('contactIris', v.map(String))"
|
||||
/>
|
||||
|
||||
<!-- Email de facturation : ligne 1 colonne 4, visible/obligatoire
|
||||
seulement si Facturation (RG-1.11). Sinon un filler comble la
|
||||
colonne pour que Categorie reparte au debut de la ligne 2. -->
|
||||
<MalioInputEmail
|
||||
v-if="isBillingEmailRequired(model)"
|
||||
:model-value="model.billingEmail"
|
||||
:label="t('commercial.clients.form.address.billingEmail')"
|
||||
:required="true"
|
||||
:readonly="readonly"
|
||||
:lowercase="true"
|
||||
:error="errors?.billingEmail"
|
||||
@update:model-value="(v: string) => update('billingEmail', v)"
|
||||
/>
|
||||
<div v-else aria-hidden="true" />
|
||||
|
||||
<MalioSelectCheckbox
|
||||
:model-value="model.categoryIris"
|
||||
@@ -134,47 +153,15 @@
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- Sites Starseed : cases a cocher inline (>= 1 obligatoire, RG-1.10). -->
|
||||
<div class="flex justify-between">
|
||||
<MalioCheckbox
|
||||
v-for="site in siteOptions"
|
||||
:key="site.value"
|
||||
:model-value="model.siteIris.includes(site.value)"
|
||||
:label="site.label"
|
||||
group-class="w-auto self-center"
|
||||
:readonly="readonly"
|
||||
@update:model-value="(v: boolean) => toggleSite(site.value, v)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<MalioSelectCheckbox
|
||||
:model-value="model.contactIris"
|
||||
:options="contactOptions"
|
||||
:label="t('commercial.clients.form.address.contacts')"
|
||||
:display-tag="true"
|
||||
:readonly="readonly"
|
||||
@update:model-value="(v: (string | number)[]) => update('contactIris', v.map(String))"
|
||||
/>
|
||||
|
||||
<!-- Email de facturation : visible/obligatoire seulement si Facturation
|
||||
est coche (RG-1.11). -->
|
||||
<MalioInputText
|
||||
v-if="isBillingEmailRequired(model)"
|
||||
:model-value="model.billingEmail"
|
||||
:label="t('commercial.clients.form.address.billingEmail')"
|
||||
:required="true"
|
||||
:readonly="readonly"
|
||||
:error="errors?.billingEmail"
|
||||
@update:model-value="(v: string) => update('billingEmail', v)"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
applyProspectExclusivity,
|
||||
addressFlagsFromType,
|
||||
addressTypeFromFlags,
|
||||
isBillingEmailRequired,
|
||||
type AddressFlagsDraft,
|
||||
type AddressType,
|
||||
} from '~/modules/commercial/utils/clientFormRules'
|
||||
import { useAddressAutocomplete, type AddressSuggestion } from '~/shared/composables/useAddressAutocomplete'
|
||||
import type { CategoryOption, RefOption } from '~/modules/commercial/composables/useClientReferentials'
|
||||
@@ -213,6 +200,23 @@ const autocomplete = useAddressAutocomplete()
|
||||
|
||||
const model = computed(() => props.modelValue)
|
||||
|
||||
// Type d'adresse (Select unique) derive des drapeaux back. null tant qu'aucun
|
||||
// drapeau n'est pose -> champ vide + bouton « Valider » bloque (cf. parent).
|
||||
const addressType = computed<AddressType | null>(() => addressTypeFromFlags(model.value))
|
||||
|
||||
const addressTypeOptions = computed<RefOption[]>(() => [
|
||||
{ value: 'prospect', label: t('commercial.clients.form.address.addressTypeProspect') },
|
||||
{ value: 'delivery', label: t('commercial.clients.form.address.addressTypeDelivery') },
|
||||
{ value: 'billing', label: t('commercial.clients.form.address.addressTypeBilling') },
|
||||
{ value: 'delivery_billing', label: t('commercial.clients.form.address.addressTypeDeliveryBilling') },
|
||||
])
|
||||
|
||||
/** Applique le type choisi en repercutant les 3 drapeaux back (immutabilite). */
|
||||
function onAddressTypeChange(value: string | number | null): void {
|
||||
if (value === null) return
|
||||
emit('update:modelValue', { ...props.modelValue, ...addressFlagsFromType(value as AddressType) })
|
||||
}
|
||||
|
||||
// Mode degrade : service BAN indisponible → Ville/Adresse en saisie libre.
|
||||
const degraded = ref(false)
|
||||
// Villes proposees par la BAN (alimentees a la saisie du code postal).
|
||||
@@ -254,25 +258,6 @@ function update<K extends keyof AddressFormDraft>(field: K, value: AddressFormDr
|
||||
emit('update:modelValue', { ...props.modelValue, [field]: value })
|
||||
}
|
||||
|
||||
/** Coche/decoche un site Starseed rattache a l'adresse (M2M par IRI, RG-1.10). */
|
||||
function toggleSite(siteIri: string, selected: boolean): void {
|
||||
const current = props.modelValue.siteIris
|
||||
const next = selected
|
||||
? [...current, siteIri]
|
||||
: current.filter(iri => iri !== siteIri)
|
||||
update('siteIris', next)
|
||||
}
|
||||
|
||||
/** Applique l'exclusivite Prospect / (Livraison|Facturation) au changement. */
|
||||
function toggleFlag(field: keyof AddressFlagsDraft, value: boolean): void {
|
||||
const flags = applyProspectExclusivity(
|
||||
{ isProspect: model.value.isProspect, isDelivery: model.value.isDelivery, isBilling: model.value.isBilling },
|
||||
field,
|
||||
value,
|
||||
)
|
||||
emit('update:modelValue', { ...props.modelValue, ...flags })
|
||||
}
|
||||
|
||||
/** Bascule définitivement en mode degrade et previent le parent (toast unique). */
|
||||
function enterDegraded(): void {
|
||||
if (!degraded.value) {
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
:model-value="model.email"
|
||||
:label="t('commercial.clients.form.contact.email')"
|
||||
:readonly="readonly"
|
||||
:lowercase="true"
|
||||
:error="errors?.email"
|
||||
@update:model-value="(v: string) => update('email', v)"
|
||||
/>
|
||||
|
||||
@@ -52,3 +52,71 @@ describe('useClientFormErrors', () => {
|
||||
expect(f.addressErrors.value[0]).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
// Construit une erreur facon useApi : 422 avec violations Hydra.
|
||||
function http422(path: string, message: string) {
|
||||
return { response: { status: 422, _data: { violations: [{ propertyPath: path, message }] } } }
|
||||
}
|
||||
|
||||
/**
|
||||
* `submitRows` factorise la soumission d'une collection de blocs (contacts /
|
||||
* adresses / RIB) : on tente TOUS les blocs et on collecte les erreurs par index
|
||||
* sans stopper au premier echec (ERP-110 / ERP-101).
|
||||
*/
|
||||
describe('useClientFormErrors.submitRows', () => {
|
||||
it('tente TOUS les blocs et mappe les erreurs par index, sans stopper au premier echec', async () => {
|
||||
const { contactErrors, submitRows } = useClientFormErrors()
|
||||
const seen: number[] = []
|
||||
const onUnmapped = vi.fn()
|
||||
|
||||
const saveRow = async (_row: unknown, index: number) => {
|
||||
seen.push(index)
|
||||
if (index === 1) throw http422('email', 'Email invalide')
|
||||
}
|
||||
|
||||
const hasError = await submitRows(
|
||||
[{ a: 0 }, { a: 1 }, { a: 2 }],
|
||||
contactErrors,
|
||||
saveRow,
|
||||
onUnmapped,
|
||||
)
|
||||
|
||||
expect(seen).toEqual([0, 1, 2]) // tous les blocs tentes
|
||||
expect(hasError).toBe(true)
|
||||
expect(contactErrors.value[1]).toEqual({ email: 'Email invalide' })
|
||||
expect(contactErrors.value[0]).toBeUndefined()
|
||||
expect(onUnmapped).not.toHaveBeenCalled() // 422 mappee, pas de fallback
|
||||
})
|
||||
|
||||
it('delegue le fallback onUnmappedError pour une erreur non mappable et marque hasError', async () => {
|
||||
const { ribErrors, submitRows } = useClientFormErrors()
|
||||
const onUnmapped = vi.fn()
|
||||
|
||||
const hasError = await submitRows(
|
||||
[{ a: 0 }],
|
||||
ribErrors,
|
||||
async () => { throw { response: { status: 500, _data: {} } } },
|
||||
onUnmapped,
|
||||
)
|
||||
|
||||
expect(hasError).toBe(true)
|
||||
expect(onUnmapped).toHaveBeenCalledTimes(1)
|
||||
expect(ribErrors.value[0]).toBeUndefined()
|
||||
})
|
||||
|
||||
it('saute les lignes filtrees par shouldSkip et renvoie false si tout passe', async () => {
|
||||
const { contactErrors, submitRows } = useClientFormErrors()
|
||||
const saved: number[] = []
|
||||
|
||||
const hasError = await submitRows(
|
||||
[{ skip: true }, { skip: false }],
|
||||
contactErrors,
|
||||
async (_row, index) => { saved.push(index) },
|
||||
vi.fn(),
|
||||
(row: { skip: boolean }) => row.skip,
|
||||
)
|
||||
|
||||
expect(saved).toEqual([1])
|
||||
expect(hasError).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -43,6 +43,44 @@ export function useClientFormErrors() {
|
||||
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
|
||||
* (non remplis). Retourne true si au moins un bloc a echoue (le caller ne valide
|
||||
* alors pas l'onglet et n'affiche pas de toast succes).
|
||||
*/
|
||||
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++) {
|
||||
// L'index reste borne par rows.length : la ligne existe forcement.
|
||||
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,
|
||||
@@ -51,5 +89,6 @@ export function useClientFormErrors() {
|
||||
addressErrors,
|
||||
ribErrors,
|
||||
mapRowError,
|
||||
submitRows,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -410,11 +410,15 @@ import {
|
||||
type MainFormDraft,
|
||||
} from '~/modules/commercial/utils/clientEdit'
|
||||
import {
|
||||
addressTypeFromFlags,
|
||||
buildClientFormTabKeys,
|
||||
hasAllRequiredAccountingFields,
|
||||
hasAtLeastOneValidContact,
|
||||
isBankRequiredForPaymentType,
|
||||
isBillingEmailRequired,
|
||||
isContactBlank,
|
||||
isContactNamed,
|
||||
isRibBlank,
|
||||
isRibRequiredForPaymentType,
|
||||
} from '~/modules/commercial/utils/clientFormRules'
|
||||
import {
|
||||
@@ -628,7 +632,7 @@ const {
|
||||
contactErrors,
|
||||
addressErrors,
|
||||
ribErrors,
|
||||
mapRowError,
|
||||
submitRows,
|
||||
} = useClientFormErrors()
|
||||
|
||||
// ── Bloc principal ───────────────────────────────────────────────────────────
|
||||
@@ -742,11 +746,14 @@ async function submitContacts(): Promise<void> {
|
||||
}
|
||||
removedContactIds.value = []
|
||||
|
||||
for (let index = 0; index < contacts.value.length; index++) {
|
||||
const contact = contacts.value[index]
|
||||
if (!isContactNamed(contact)) continue
|
||||
const body = buildContactPayload(contact)
|
||||
try {
|
||||
// On tente TOUS les blocs (collecte des erreurs par index, ERP-110). Seuls
|
||||
// les blocs TOTALEMENT vides sont ignores : un bloc partiellement rempli
|
||||
// sans nom (email seul) est soumis -> 422 RG-1.05 inline sous le bloc.
|
||||
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 }>(
|
||||
`/clients/${clientId}/contacts`,
|
||||
@@ -759,15 +766,15 @@ async function submitContacts(): Promise<void> {
|
||||
else {
|
||||
await api.patch(`/client_contacts/${contact.id}`, body, { toast: false })
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
// 422 → erreurs inline sous les champs de CETTE ligne ; on stoppe.
|
||||
if (!mapRowError(error, contactErrors, index)) {
|
||||
showError(error)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
},
|
||||
error => showError(error),
|
||||
// On ne saute QUE les amorces neuves (id null) totalement vides. Un
|
||||
// bloc existant vide est soumis -> 422 RG-1.05 inline (sinon la modif
|
||||
// serait perdue en silence avec un faux toast de succes).
|
||||
contact => contact.id === null && isContactBlank(contact),
|
||||
)
|
||||
// Tant qu'un bloc reste en erreur : pas de toast succes.
|
||||
if (hasError) return
|
||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||
}
|
||||
catch (e) {
|
||||
@@ -783,7 +790,8 @@ const canValidateAddresses = computed(() =>
|
||||
addresses.value.length > 0
|
||||
&& addresses.value.every((a) => {
|
||||
const filledBillingEmail = a.billingEmail !== null && a.billingEmail.trim() !== ''
|
||||
return a.siteIris.length >= 1
|
||||
return addressTypeFromFlags(a) !== null
|
||||
&& a.siteIris.length >= 1
|
||||
&& a.categoryIris.length >= 1
|
||||
&& (!isBillingEmailRequired(a) || filledBillingEmail)
|
||||
}),
|
||||
@@ -824,10 +832,12 @@ async function submitAddresses(): Promise<void> {
|
||||
}
|
||||
removedAddressIds.value = []
|
||||
|
||||
for (let index = 0; index < addresses.value.length; index++) {
|
||||
const address = addresses.value[index]
|
||||
const body = buildAddressPayload(address, isBillingEmailRequired(address))
|
||||
try {
|
||||
// On tente TOUS les blocs d'adresse (collecte des erreurs par index, ERP-110).
|
||||
const hasError = await submitRows(
|
||||
addresses.value,
|
||||
addressErrors,
|
||||
async (address) => {
|
||||
const body = buildAddressPayload(address, isBillingEmailRequired(address))
|
||||
if (address.id === null) {
|
||||
const created = await api.post<{ id: number }>(
|
||||
`/clients/${clientId}/addresses`,
|
||||
@@ -839,14 +849,10 @@ async function submitAddresses(): Promise<void> {
|
||||
else {
|
||||
await api.patch(`/client_addresses/${address.id}`, body, { toast: false })
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if (!mapRowError(error, addressErrors, index)) {
|
||||
showError(error)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
},
|
||||
error => showError(error),
|
||||
)
|
||||
if (hasError) return
|
||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||
}
|
||||
catch (e) {
|
||||
@@ -875,6 +881,7 @@ function ribIsComplete(rib: { label: string | null, bic: string | null, iban: st
|
||||
}
|
||||
|
||||
const canValidateAccounting = computed(() => {
|
||||
if (!hasAllRequiredAccountingFields(accounting)) return false
|
||||
if (isBankRequired.value && accounting.bankIri === null) return false
|
||||
if (isRibRequired.value && !ribs.value.some(ribIsComplete)) return false
|
||||
return true
|
||||
@@ -905,6 +912,9 @@ async function submitAccounting(): Promise<void> {
|
||||
if (accountingReadonly.value || !canValidateAccounting.value || tabSubmitting.value) return
|
||||
tabSubmitting.value = true
|
||||
accountingErrors.clearErrors()
|
||||
// Reset des erreurs RIB des le debut : l'etape 1 (PATCH scalaires) peut
|
||||
// echouer et `return` avant submitRows (qui porte sinon le reset), laissant
|
||||
// des erreurs de RIB obsoletes affichees sous les blocs.
|
||||
ribErrors.value = []
|
||||
try {
|
||||
// 1) PATCH des scalaires comptables (erreurs inline sur leurs champs).
|
||||
@@ -921,12 +931,14 @@ async function submitAccounting(): Promise<void> {
|
||||
}
|
||||
removedRibIds.value = []
|
||||
|
||||
// 2) POST/PATCH des RIB (erreurs inline par ligne).
|
||||
for (let index = 0; index < ribs.value.length; index++) {
|
||||
const rib = ribs.value[index]
|
||||
if (!ribIsComplete(rib)) continue
|
||||
const body = buildRibPayload(rib)
|
||||
try {
|
||||
// 2) POST/PATCH des RIB (erreurs inline par ligne, tous les blocs tentes).
|
||||
// Seuls les blocs RIB TOTALEMENT vides sont ignores : un RIB partiel (ex.
|
||||
// IBAN seul) est soumis -> 422 NotBlank (label / bic / iban) inline.
|
||||
const ribHasError = await submitRows(
|
||||
ribs.value,
|
||||
ribErrors,
|
||||
async (rib) => {
|
||||
const body = buildRibPayload(rib)
|
||||
if (rib.id === null) {
|
||||
const created = await api.post<{ id: number }>(
|
||||
`/clients/${clientId}/ribs`,
|
||||
@@ -938,14 +950,14 @@ async function submitAccounting(): Promise<void> {
|
||||
else {
|
||||
await api.patch(`/client_ribs/${rib.id}`, body, { toast: false })
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if (!mapRowError(error, ribErrors, index)) {
|
||||
showError(error)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
},
|
||||
error => showError(error),
|
||||
// On ne saute QUE les amorces neuves (id null) totalement vides. Un
|
||||
// RIB existant vide est soumis -> 422 NotBlank inline (sinon la modif
|
||||
// serait perdue en silence avec un faux toast de succes).
|
||||
rib => rib.id === null && isRibBlank(rib),
|
||||
)
|
||||
if (ribHasError) return
|
||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||
}
|
||||
catch (e) {
|
||||
|
||||
@@ -380,12 +380,16 @@ import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { useClientReferentials, type RefOption } from '~/modules/commercial/composables/useClientReferentials'
|
||||
import { useClientFormErrors } from '~/modules/commercial/composables/useClientFormErrors'
|
||||
import {
|
||||
addressTypeFromFlags,
|
||||
buildClientFormTabKeys,
|
||||
CLIENT_FORM_PLACEHOLDER_TABS,
|
||||
hasAllRequiredAccountingFields,
|
||||
hasAtLeastOneValidContact,
|
||||
isBankRequiredForPaymentType,
|
||||
isBillingEmailRequired,
|
||||
isContactBlank,
|
||||
isContactNamed,
|
||||
isRibBlank,
|
||||
isRibRequiredForPaymentType,
|
||||
} from '~/modules/commercial/utils/clientFormRules'
|
||||
import {
|
||||
@@ -441,7 +445,7 @@ const {
|
||||
contactErrors,
|
||||
addressErrors,
|
||||
ribErrors,
|
||||
mapRowError,
|
||||
submitRows,
|
||||
} = useClientFormErrors()
|
||||
|
||||
useHead({ title: t('commercial.clients.form.title') })
|
||||
@@ -676,23 +680,22 @@ function askRemoveContact(index: number): void {
|
||||
async function submitContacts(): Promise<void> {
|
||||
if (clientId.value === null || !canValidateContacts.value || tabSubmitting.value) return
|
||||
tabSubmitting.value = true
|
||||
contactErrors.value = []
|
||||
try {
|
||||
for (let index = 0; index < contacts.value.length; index++) {
|
||||
const contact = contacts.value[index]
|
||||
// On ignore les blocs totalement vides (ni nom ni prenom).
|
||||
if (!isContactNamed(contact)) continue
|
||||
|
||||
const body = {
|
||||
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,
|
||||
}
|
||||
|
||||
try {
|
||||
// On tente TOUS les blocs (collecte des erreurs par index, ERP-110). Seuls
|
||||
// les blocs TOTALEMENT vides sont ignores : un bloc partiellement rempli
|
||||
// sans nom (email seul) est soumis -> 422 RG-1.05 inline sous le bloc.
|
||||
const hasError = await submitRows(
|
||||
contacts.value,
|
||||
contactErrors,
|
||||
async (contact) => {
|
||||
const body = {
|
||||
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,
|
||||
}
|
||||
if (contact.id === null) {
|
||||
const created = await api.post<ContactResponse>(
|
||||
`/clients/${clientId.value}/contacts`,
|
||||
@@ -705,16 +708,15 @@ async function submitContacts(): Promise<void> {
|
||||
else {
|
||||
await api.patch(`/client_contacts/${contact.id}`, body, { toast: false })
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
// 422 → erreurs inline sous les champs de CETTE ligne ; on stoppe
|
||||
// a la premiere ligne en echec (les suivantes ne sont pas tentees).
|
||||
if (!mapRowError(error, contactErrors, index)) {
|
||||
toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) })
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
},
|
||||
error => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||
// On ne saute QUE les amorces neuves (id null) totalement vides. Un
|
||||
// bloc existant vide est soumis -> 422 RG-1.05 inline (sinon la modif
|
||||
// serait perdue en silence avec un faux toast de succes).
|
||||
contact => contact.id === null && isContactBlank(contact),
|
||||
)
|
||||
// Tant qu'un bloc reste en erreur : pas de validation d'onglet ni de toast succes.
|
||||
if (hasError) return
|
||||
completeTab('contact')
|
||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||
}
|
||||
@@ -748,12 +750,14 @@ const countryOptions: RefOption[] = [
|
||||
{ value: 'Espagne', label: 'Espagne' },
|
||||
]
|
||||
|
||||
// RG-1.10 (>= 1 site) + RG-1.11 (email facturation si Facturation) sur chaque adresse.
|
||||
// Type d'adresse (Select) obligatoire + RG-1.10 (>= 1 site) + RG-1.11 (email
|
||||
// facturation si Facturation) sur chaque adresse.
|
||||
const canValidateAddresses = computed(() =>
|
||||
addresses.value.length > 0
|
||||
&& addresses.value.every((a) => {
|
||||
const filledBillingEmail = a.billingEmail !== null && a.billingEmail.trim() !== ''
|
||||
return a.siteIris.length >= 1
|
||||
return addressTypeFromFlags(a) !== null
|
||||
&& a.siteIris.length >= 1
|
||||
&& a.categoryIris.length >= 1
|
||||
&& (!isBillingEmailRequired(a) || filledBillingEmail)
|
||||
}),
|
||||
@@ -784,26 +788,26 @@ function onAddressDegraded(): void {
|
||||
async function submitAddresses(): Promise<void> {
|
||||
if (clientId.value === null || !canValidateAddresses.value || tabSubmitting.value) return
|
||||
tabSubmitting.value = true
|
||||
addressErrors.value = []
|
||||
try {
|
||||
for (let index = 0; index < addresses.value.length; index++) {
|
||||
const address = addresses.value[index]
|
||||
const body = {
|
||||
isProspect: address.isProspect,
|
||||
isDelivery: address.isDelivery,
|
||||
isBilling: address.isBilling,
|
||||
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,
|
||||
billingEmail: isBillingEmailRequired(address) ? (address.billingEmail || null) : null,
|
||||
}
|
||||
|
||||
try {
|
||||
// On tente TOUS les blocs d'adresse (collecte des erreurs par index, ERP-110).
|
||||
const hasError = await submitRows(
|
||||
addresses.value,
|
||||
addressErrors,
|
||||
async (address) => {
|
||||
const body = {
|
||||
isProspect: address.isProspect,
|
||||
isDelivery: address.isDelivery,
|
||||
isBilling: address.isBilling,
|
||||
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,
|
||||
billingEmail: isBillingEmailRequired(address) ? (address.billingEmail || null) : null,
|
||||
}
|
||||
if (address.id === null) {
|
||||
const created = await api.post<{ id: number }>(
|
||||
`/clients/${clientId.value}/addresses`,
|
||||
@@ -815,14 +819,10 @@ async function submitAddresses(): Promise<void> {
|
||||
else {
|
||||
await api.patch(`/client_addresses/${address.id}`, body, { toast: false })
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if (!mapRowError(error, addressErrors, index)) {
|
||||
toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) })
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
},
|
||||
error => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||
)
|
||||
if (hasError) return
|
||||
completeTab('address')
|
||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||
}
|
||||
@@ -864,8 +864,11 @@ function ribIsComplete(rib: RibFormDraft): boolean {
|
||||
return filled(rib.label) && filled(rib.bic) && filled(rib.iban)
|
||||
}
|
||||
|
||||
// RG-1.30 : les 6 champs scalaires obligatoires (comme les onglets Contact /
|
||||
// Adresse, le bouton reste desactive tant que l'onglet n'est pas complet).
|
||||
// RG-1.12 : banque requise si VIREMENT. RG-1.13 : >= 1 RIB complet si LCR.
|
||||
const canValidateAccounting = computed(() => {
|
||||
if (!hasAllRequiredAccountingFields(accounting)) return false
|
||||
if (isBankRequired.value && (accounting.bankIri === null)) return false
|
||||
if (isRibRequired.value && !ribs.value.some(ribIsComplete)) return false
|
||||
return true
|
||||
@@ -893,6 +896,9 @@ async function submitAccounting(): Promise<void> {
|
||||
if (clientId.value === null || !canValidateAccounting.value || tabSubmitting.value) return
|
||||
tabSubmitting.value = true
|
||||
accountingErrors.clearErrors()
|
||||
// Reset des erreurs RIB des le debut : l'etape 1 (PATCH scalaires) peut
|
||||
// echouer et `return` avant submitRows (qui porte sinon le reset), laissant
|
||||
// des erreurs de RIB obsoletes affichees sous les blocs.
|
||||
ribErrors.value = []
|
||||
try {
|
||||
// 1) PATCH des scalaires comptables (erreurs inline sur leurs champs).
|
||||
@@ -912,30 +918,33 @@ async function submitAccounting(): Promise<void> {
|
||||
return
|
||||
}
|
||||
|
||||
// 2) POST/PATCH des RIB (erreurs inline par ligne).
|
||||
for (let index = 0; index < ribs.value.length; index++) {
|
||||
const rib = ribs.value[index]
|
||||
if (!ribIsComplete(rib)) continue
|
||||
try {
|
||||
// 2) POST/PATCH des RIB (erreurs inline par ligne, tous les blocs tentes).
|
||||
// Seuls les blocs RIB TOTALEMENT vides sont ignores : un RIB partiel (ex.
|
||||
// IBAN seul) est soumis -> 422 NotBlank (label / bic / iban) inline.
|
||||
const ribHasError = await submitRows(
|
||||
ribs.value,
|
||||
ribErrors,
|
||||
async (rib) => {
|
||||
const body = { label: rib.label, bic: rib.bic, iban: rib.iban }
|
||||
if (rib.id === null) {
|
||||
const created = await api.post<{ id: number }>(
|
||||
`/clients/${clientId.value}/ribs`,
|
||||
{ label: rib.label, bic: rib.bic, iban: rib.iban },
|
||||
body,
|
||||
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||
)
|
||||
rib.id = created.id
|
||||
}
|
||||
else {
|
||||
await api.patch(`/client_ribs/${rib.id}`, { label: rib.label, bic: rib.bic, iban: rib.iban }, { toast: false })
|
||||
await api.patch(`/client_ribs/${rib.id}`, body, { toast: false })
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if (!mapRowError(error, ribErrors, index)) {
|
||||
toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) })
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
},
|
||||
error => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||
// On ne saute QUE les amorces neuves (id null) totalement vides. Un
|
||||
// RIB existant vide est soumis -> 422 NotBlank inline (sinon la modif
|
||||
// serait perdue en silence avec un faux toast de succes).
|
||||
rib => rib.id === null && isRibBlank(rib),
|
||||
)
|
||||
if (ribHasError) return
|
||||
|
||||
completeTab('accounting')
|
||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||
|
||||
@@ -1,17 +1,36 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
addressFlagsFromType,
|
||||
addressTypeFromFlags,
|
||||
applyProspectExclusivity,
|
||||
buildClientFormTabKeys,
|
||||
canSelectDeliveryOrBilling,
|
||||
canSelectProspect,
|
||||
hasAllRequiredAccountingFields,
|
||||
hasAtLeastOneValidContact,
|
||||
isBankRequiredForPaymentType,
|
||||
isBillingEmailRequired,
|
||||
isBlankRow,
|
||||
isContactBlank,
|
||||
isContactNamed,
|
||||
isRibBlank,
|
||||
isRibRequiredForPaymentType,
|
||||
type ContactDraft,
|
||||
type ContactFillableDraft,
|
||||
} from '../clientFormRules'
|
||||
|
||||
/** Bloc contact totalement vide (amorce par defaut). */
|
||||
function blankContact(): ContactFillableDraft {
|
||||
return {
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
jobTitle: null,
|
||||
phonePrimary: null,
|
||||
phoneSecondary: null,
|
||||
email: null,
|
||||
}
|
||||
}
|
||||
|
||||
describe('buildClientFormTabKeys (gating onglet Comptabilite + onglets edit-only)', () => {
|
||||
it('inclut l onglet accounting si l utilisateur a accounting.view', () => {
|
||||
expect(buildClientFormTabKeys(true)).toContain('accounting')
|
||||
@@ -59,6 +78,49 @@ describe('isContactNamed (RG-1.05)', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('isBlankRow (primitive : toutes les valeurs vides)', () => {
|
||||
it('vrai si toutes les valeurs sont nulles / vides / espaces', () => {
|
||||
expect(isBlankRow([null, undefined, '', ' '])).toBe(true)
|
||||
expect(isBlankRow([])).toBe(true)
|
||||
})
|
||||
|
||||
it('faux des qu une valeur porte un caractere non-espace', () => {
|
||||
expect(isBlankRow([null, 'x', ''])).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isRibBlank (bloc RIB totalement vide vs partiellement rempli)', () => {
|
||||
it('vrai si label / bic / iban sont tous vides', () => {
|
||||
expect(isRibBlank({ label: null, bic: null, iban: null })).toBe(true)
|
||||
expect(isRibBlank({ label: ' ', bic: '', iban: null })).toBe(true)
|
||||
})
|
||||
|
||||
it('faux si un IBAN seul est saisi (bloc a soumettre -> 422 NotBlank inline)', () => {
|
||||
expect(isRibBlank({ label: null, bic: null, iban: 'FR1420041010050500013M02606' })).toBe(false)
|
||||
})
|
||||
|
||||
it('faux si seul le libelle est saisi', () => {
|
||||
expect(isRibBlank({ label: 'Compte courant', bic: null, iban: null })).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isContactBlank (bloc totalement vide vs partiellement rempli)', () => {
|
||||
it('vrai si aucun champ saisissable n est rempli', () => {
|
||||
expect(isContactBlank(blankContact())).toBe(true)
|
||||
expect(isContactBlank({ ...blankContact(), firstName: ' ', email: '' })).toBe(true)
|
||||
})
|
||||
|
||||
it('faux si un email seul est saisi (bloc a soumettre -> 422 RG-1.05 inline)', () => {
|
||||
expect(isContactBlank({ ...blankContact(), email: 'jean@acme.fr' })).toBe(false)
|
||||
})
|
||||
|
||||
it('faux si seul un telephone, une fonction ou un nom est saisi', () => {
|
||||
expect(isContactBlank({ ...blankContact(), phonePrimary: '0612345678' })).toBe(false)
|
||||
expect(isContactBlank({ ...blankContact(), jobTitle: 'Directeur' })).toBe(false)
|
||||
expect(isContactBlank({ ...blankContact(), firstName: 'Alice' })).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('hasAtLeastOneValidContact (RG-1.14)', () => {
|
||||
it('faux sur une liste vide', () => {
|
||||
expect(hasAtLeastOneValidContact([])).toBe(false)
|
||||
@@ -137,6 +199,32 @@ describe('isBillingEmailRequired (RG-1.11)', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('type d\'adresse (Select front) <-> drapeaux back', () => {
|
||||
it('addressFlagsFromType mappe chaque type vers les bons drapeaux', () => {
|
||||
expect(addressFlagsFromType('prospect')).toEqual({ isProspect: true, isDelivery: false, isBilling: false })
|
||||
expect(addressFlagsFromType('delivery')).toEqual({ isProspect: false, isDelivery: true, isBilling: false })
|
||||
expect(addressFlagsFromType('billing')).toEqual({ isProspect: false, isDelivery: false, isBilling: true })
|
||||
expect(addressFlagsFromType('delivery_billing')).toEqual({ isProspect: false, isDelivery: true, isBilling: true })
|
||||
})
|
||||
|
||||
it('addressTypeFromFlags reconstruit le type (Prospect prioritaire, livraison+facturation groupes)', () => {
|
||||
expect(addressTypeFromFlags({ isProspect: true, isDelivery: false, isBilling: false })).toBe('prospect')
|
||||
expect(addressTypeFromFlags({ isProspect: false, isDelivery: true, isBilling: false })).toBe('delivery')
|
||||
expect(addressTypeFromFlags({ isProspect: false, isDelivery: false, isBilling: true })).toBe('billing')
|
||||
expect(addressTypeFromFlags({ isProspect: false, isDelivery: true, isBilling: true })).toBe('delivery_billing')
|
||||
})
|
||||
|
||||
it('addressTypeFromFlags retourne null quand aucun drapeau (amorce vierge -> bouton bloque)', () => {
|
||||
expect(addressTypeFromFlags({ isProspect: false, isDelivery: false, isBilling: false })).toBeNull()
|
||||
})
|
||||
|
||||
it('aller-retour type -> drapeaux -> type stable pour les 4 types', () => {
|
||||
for (const type of ['prospect', 'delivery', 'billing', 'delivery_billing'] as const) {
|
||||
expect(addressTypeFromFlags(addressFlagsFromType(type))).toBe(type)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('regles type de reglement (RG-1.12 / RG-1.13)', () => {
|
||||
it('banque obligatoire si VIREMENT', () => {
|
||||
expect(isBankRequiredForPaymentType('VIREMENT')).toBe(true)
|
||||
@@ -150,3 +238,36 @@ describe('regles type de reglement (RG-1.12 / RG-1.13)', () => {
|
||||
expect(isRibRequiredForPaymentType(null)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('hasAllRequiredAccountingFields (RG-1.30)', () => {
|
||||
const complete = {
|
||||
siren: '123456789',
|
||||
accountNumber: '00012345678',
|
||||
nTva: 'FR12345678901',
|
||||
tvaModeIri: '/api/tva_modes/1',
|
||||
paymentDelayIri: '/api/payment_delays/1',
|
||||
paymentTypeIri: '/api/payment_types/1',
|
||||
}
|
||||
|
||||
it('vrai quand les six champs obligatoires sont remplis', () => {
|
||||
expect(hasAllRequiredAccountingFields(complete)).toBe(true)
|
||||
})
|
||||
|
||||
it('faux si un champ est manquant (null ou vide apres trim)', () => {
|
||||
expect(hasAllRequiredAccountingFields({ ...complete, siren: null })).toBe(false)
|
||||
expect(hasAllRequiredAccountingFields({ ...complete, accountNumber: ' ' })).toBe(false)
|
||||
expect(hasAllRequiredAccountingFields({ ...complete, tvaModeIri: null })).toBe(false)
|
||||
expect(hasAllRequiredAccountingFields({ ...complete, paymentTypeIri: null })).toBe(false)
|
||||
})
|
||||
|
||||
it('faux quand tout est vide (onglet non rempli)', () => {
|
||||
expect(hasAllRequiredAccountingFields({
|
||||
siren: null,
|
||||
accountNumber: null,
|
||||
nTva: null,
|
||||
tvaModeIri: null,
|
||||
paymentDelayIri: null,
|
||||
paymentTypeIri: null,
|
||||
})).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -86,6 +86,58 @@ export function hasAtLeastOneValidContact(contacts: ContactDraft[]): boolean {
|
||||
return contacts.some(isContactNamed)
|
||||
}
|
||||
|
||||
/**
|
||||
* Primitive reutilisable : vrai si TOUTES les valeurs fournies sont vides (null /
|
||||
* undefined / espaces uniquement). 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-1.05 (« prenom ou nom obligatoire ») 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 (label / bic / iban) inline plutot que d'etre saute silencieusement.
|
||||
*/
|
||||
export function isRibBlank(rib: RibFillableDraft): boolean {
|
||||
return isBlankRow([rib.label, rib.bic, rib.iban])
|
||||
}
|
||||
|
||||
/**
|
||||
* RG-1.06/07/08 : une adresse de prospection est exclusive d'une adresse de
|
||||
* livraison/facturation. Prospect n'est selectionnable que si ni Livraison ni
|
||||
@@ -135,6 +187,45 @@ export function isBillingEmailRequired(flags: AddressFlagsDraft): boolean {
|
||||
return flags.isBilling
|
||||
}
|
||||
|
||||
/**
|
||||
* Type d'adresse expose a l'utilisateur (Select unique remplacant les trois
|
||||
* cases a cocher). Sucre purement front : le back continue de recevoir les
|
||||
* drapeaux isProspect / isDelivery / isBilling (aucune RG modifiee). Les seules
|
||||
* combinaisons proposees respectent l'exclusivite Prospect (RG-1.06/07/08).
|
||||
*/
|
||||
export type AddressType = 'prospect' | 'delivery' | 'billing' | 'delivery_billing'
|
||||
|
||||
/**
|
||||
* Mappe le type d'adresse choisi vers les trois drapeaux back.
|
||||
* « Adresse + Facturation » = livraison ET facturation sur la meme adresse.
|
||||
*/
|
||||
export function addressFlagsFromType(type: AddressType): AddressFlagsDraft {
|
||||
switch (type) {
|
||||
case 'prospect':
|
||||
return { isProspect: true, isDelivery: false, isBilling: false }
|
||||
case 'delivery':
|
||||
return { isProspect: false, isDelivery: true, isBilling: false }
|
||||
case 'billing':
|
||||
return { isProspect: false, isDelivery: false, isBilling: true }
|
||||
case 'delivery_billing':
|
||||
return { isProspect: false, isDelivery: true, isBilling: true }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstruit le type d'adresse a partir des drapeaux (consultation / edition
|
||||
* d'une adresse persistee, ou amorce vierge). Retourne null si aucun drapeau
|
||||
* n'est positionne — le Select reste alors a saisir (et bloque la validation).
|
||||
*/
|
||||
export function addressTypeFromFlags(flags: AddressFlagsDraft): AddressType | null {
|
||||
if (flags.isProspect) return 'prospect'
|
||||
if (flags.isDelivery && flags.isBilling) return 'delivery_billing'
|
||||
if (flags.isDelivery) return 'delivery'
|
||||
if (flags.isBilling) return 'billing'
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
/** Code stable du type de reglement « virement » (cf. PaymentType.code, RG-1.12). */
|
||||
const PAYMENT_TYPE_TRANSFER = 'VIREMENT'
|
||||
|
||||
@@ -156,3 +247,32 @@ export function isBankRequiredForPaymentType(code: string | null | undefined): b
|
||||
export function isRibRequiredForPaymentType(code: string | null | undefined): boolean {
|
||||
return code === PAYMENT_TYPE_LCR
|
||||
}
|
||||
|
||||
/** Sous-ensemble du brouillon comptable portant les six champs obligatoires. */
|
||||
export interface AccountingRequiredDraft {
|
||||
siren: string | null
|
||||
accountNumber: string | null
|
||||
nTva: string | null
|
||||
tvaModeIri: string | null
|
||||
paymentDelayIri: string | null
|
||||
paymentTypeIri: string | null
|
||||
}
|
||||
|
||||
/**
|
||||
* RG-1.30 : les six champs scalaires de l'onglet Comptabilite sont obligatoires
|
||||
* pour valider l'onglet (SIREN, N de compte, Mode de TVA, N de TVA, Delai de
|
||||
* reglement, Type de reglement). bank / RIB restent conditionnels (RG-1.12 /
|
||||
* RG-1.13) et sont evalues a part. Miroir front du
|
||||
* ClientAccountingCompletenessValidator : meme gate que les onglets Contact /
|
||||
* Adresse (bouton « Valider » desactive tant que l'onglet n'est pas complet).
|
||||
*/
|
||||
export function hasAllRequiredAccountingFields(accounting: AccountingRequiredDraft): boolean {
|
||||
const filled = (v: string | null): boolean => v !== null && v.trim() !== ''
|
||||
|
||||
return filled(accounting.siren)
|
||||
&& filled(accounting.accountNumber)
|
||||
&& filled(accounting.nTva)
|
||||
&& filled(accounting.tvaModeIri)
|
||||
&& filled(accounting.paymentDelayIri)
|
||||
&& filled(accounting.paymentTypeIri)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Commercial\Application\Validator;
|
||||
|
||||
use ApiPlatform\Validator\Exception\ValidationException;
|
||||
use App\Module\Commercial\Domain\Entity\Client;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
|
||||
/**
|
||||
* Validator metier (spec-front § 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-1.12) et les RIB aussi
|
||||
* (RG-1.13) : ils ne sont pas couverts ici.
|
||||
*
|
||||
* Calque sur ClientInformationCompletenessValidator (RG-1.04) : 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 ClientProcessor uniquement quand le payload porte les six champs
|
||||
* (= une validation d'onglet), jamais sur un PATCH ciblant un seul champ comptable.
|
||||
*
|
||||
* 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 ClientAccountingCompletenessValidator
|
||||
{
|
||||
public function validate(Client $client): void
|
||||
{
|
||||
// Map champ -> valeur courante des champs obligatoires de l'onglet.
|
||||
$fields = [
|
||||
'siren' => $client->getSiren(),
|
||||
'accountNumber' => $client->getAccountNumber(),
|
||||
'tvaMode' => $client->getTvaMode(),
|
||||
'nTva' => $client->getNTva(),
|
||||
'paymentDelay' => $client->getPaymentDelay(),
|
||||
'paymentType' => $client->getPaymentType(),
|
||||
];
|
||||
|
||||
$violations = new ConstraintViolationList();
|
||||
|
||||
foreach ($fields as $property => $value) {
|
||||
if ($this->isMissing($value)) {
|
||||
$violations->add(new ConstraintViolation(
|
||||
'Ce champ est obligatoire.',
|
||||
null,
|
||||
[],
|
||||
$client,
|
||||
$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);
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,11 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
uriVariables: [
|
||||
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
||||
],
|
||||
// read:false : pas de stade lecture du parent. Le Link toProperty
|
||||
// resoudrait l'enfant (SELECT ClientAddress ... WHERE client = :id) et
|
||||
// casse en NonUniqueResult des >= 2 enfants. Le parent est rattache
|
||||
// manuellement par ClientAddressProcessor::linkParent (404 si absent).
|
||||
read: false,
|
||||
security: "is_granted('commercial.clients.manage')",
|
||||
normalizationContext: ['groups' => ['client_address:read']],
|
||||
denormalizationContext: ['groups' => ['client_address:write']],
|
||||
|
||||
@@ -50,6 +50,11 @@ use Symfony\Component\Validator\Constraints as Assert;
|
||||
uriVariables: [
|
||||
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
||||
],
|
||||
// read:false : pas de stade lecture du parent. Le Link toProperty
|
||||
// resoudrait l'enfant (SELECT ClientContact ... WHERE client = :id) et
|
||||
// casse en NonUniqueResult des >= 2 enfants. Le parent est rattache
|
||||
// manuellement par ClientContactProcessor::linkParent (404 si absent).
|
||||
read: false,
|
||||
security: "is_granted('commercial.clients.manage')",
|
||||
normalizationContext: ['groups' => ['client_contact:read']],
|
||||
denormalizationContext: ['groups' => ['client_contact:write']],
|
||||
|
||||
@@ -54,6 +54,11 @@ use Symfony\Component\Validator\Constraints as Assert;
|
||||
uriVariables: [
|
||||
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
||||
],
|
||||
// read:false : pas de stade lecture du parent. Le Link toProperty
|
||||
// resoudrait l'enfant (SELECT ClientRib ... WHERE client = :id) et
|
||||
// casse en NonUniqueResult des >= 2 enfants. Le parent est rattache
|
||||
// manuellement par ClientRibProcessor::linkParent (404 si absent).
|
||||
read: false,
|
||||
security: "is_granted('commercial.clients.accounting.manage')",
|
||||
normalizationContext: ['groups' => ['client_rib:read']],
|
||||
denormalizationContext: ['groups' => ['client_rib:write']],
|
||||
|
||||
+8
-2
@@ -12,6 +12,7 @@ use App\Module\Commercial\Domain\Entity\Client;
|
||||
use App\Module\Commercial\Domain\Entity\ClientAddress;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Processor d'ecriture de la sous-ressource Adresse d'un client (M1, § 4.5).
|
||||
@@ -75,9 +76,14 @@ final class ClientAddressProcessor implements ProcessorInterface
|
||||
? $clientId
|
||||
: $this->em->getRepository(Client::class)->find($clientId);
|
||||
|
||||
if ($client instanceof Client) {
|
||||
$address->setClient($client);
|
||||
// read:false sur le POST : sans stade lecture, un parent introuvable n'est
|
||||
// plus intercepte en amont -> 404 explicite (sinon 500 au persist sur la
|
||||
// contrainte client_id NOT NULL).
|
||||
if (!$client instanceof Client) {
|
||||
throw new NotFoundHttpException('Client introuvable.');
|
||||
}
|
||||
|
||||
$address->setClient($client);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+8
-2
@@ -14,6 +14,7 @@ use App\Module\Commercial\Domain\Entity\ClientContact;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
|
||||
@@ -88,9 +89,14 @@ final class ClientContactProcessor implements ProcessorInterface
|
||||
? $clientId
|
||||
: $this->em->getRepository(Client::class)->find($clientId);
|
||||
|
||||
if ($client instanceof Client) {
|
||||
$contact->setClient($client);
|
||||
// read:false sur le POST : sans stade lecture, un parent introuvable n'est
|
||||
// plus intercepte en amont -> 404 explicite (sinon 500 au persist sur la
|
||||
// contrainte client_id NOT NULL).
|
||||
if (!$client instanceof Client) {
|
||||
throw new NotFoundHttpException('Client introuvable.');
|
||||
}
|
||||
|
||||
$contact->setClient($client);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,6 +8,7 @@ use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use ApiPlatform\Validator\Exception\ValidationException;
|
||||
use App\Module\Commercial\Application\Service\ClientFieldNormalizer;
|
||||
use App\Module\Commercial\Application\Validator\ClientAccountingCompletenessValidator;
|
||||
use App\Module\Commercial\Application\Validator\ClientInformationCompletenessValidator;
|
||||
use App\Module\Commercial\Domain\Entity\Client;
|
||||
use App\Shared\Domain\Contract\BusinessRoleAwareInterface;
|
||||
@@ -75,6 +76,14 @@ final class ClientProcessor implements ProcessorInterface
|
||||
'paymentType', 'bank',
|
||||
];
|
||||
|
||||
/**
|
||||
* Champs comptables obligatoires a la validation complete de l'onglet
|
||||
* (spec-front § Onglet Comptabilite). bank est exclu : conditionnel (RG-1.12).
|
||||
*/
|
||||
private const array ACCOUNTING_REQUIRED_FIELDS = [
|
||||
'siren', 'accountNumber', 'tvaMode', 'nTva', 'paymentDelay', 'paymentType',
|
||||
];
|
||||
|
||||
/** Champ d'archivage (groupe client:write:archive). */
|
||||
private const string ARCHIVE_FIELD = 'isArchived';
|
||||
|
||||
@@ -100,6 +109,7 @@ final class ClientProcessor implements ProcessorInterface
|
||||
private readonly ProcessorInterface $persistProcessor,
|
||||
private readonly ClientFieldNormalizer $normalizer,
|
||||
private readonly ClientInformationCompletenessValidator $informationValidator,
|
||||
private readonly ClientAccountingCompletenessValidator $accountingValidator,
|
||||
private readonly Security $security,
|
||||
private readonly RequestStack $requestStack,
|
||||
private readonly EntityManagerInterface $em,
|
||||
@@ -125,6 +135,7 @@ final class ClientProcessor implements ProcessorInterface
|
||||
|
||||
$this->validateDistributorBroker($data);
|
||||
$this->validateAccountingConsistency($data);
|
||||
$this->validateAccountingCompleteness($data);
|
||||
$this->validateInformationCompleteness($data);
|
||||
|
||||
try {
|
||||
@@ -486,6 +497,29 @@ final class ClientProcessor implements ProcessorInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* spec-front § Onglet Comptabilite : a la validation COMPLETE de l'onglet
|
||||
* (les six champs obligatoires presents dans le payload — le front les envoie
|
||||
* toujours ensemble), chacun doit etre renseigne, sinon 422 par champ. On ne
|
||||
* declenche pas sur un PATCH ciblant un sous-ensemble de champs comptables :
|
||||
* ce n'est pas une validation d'onglet (edition ponctuelle preservee). bank /
|
||||
* RIB restent geres par validateAccountingConsistency (RG-1.12 / RG-1.13).
|
||||
*
|
||||
* Colonnes nullable en base + validateur contextuel (meme parti que RG-1.04) :
|
||||
* un Assert\NotBlank sur l'entite casserait le POST de l'onglet principal, qui
|
||||
* n'envoie aucun champ comptable.
|
||||
*/
|
||||
private function validateAccountingCompleteness(Client $data): void
|
||||
{
|
||||
// Declenche uniquement si TOUS les champs requis sont presents dans le
|
||||
// payload (= soumission d'onglet, pas un PATCH partiel cible).
|
||||
if ([] !== array_diff(self::ACCOUNTING_REQUIRED_FIELDS, $this->payloadKeys())) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->accountingValidator->validate($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* RG-1.04 (durcie ERP-74) : si l'utilisateur porte le role metier
|
||||
* Commerciale, TOUS les champs de l'onglet Information sont obligatoires sur
|
||||
|
||||
+8
-2
@@ -12,6 +12,7 @@ use App\Module\Commercial\Domain\Entity\ClientRib;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Processor d'ecriture de la sous-ressource RIB d'un client (M1, § 4.5).
|
||||
@@ -77,9 +78,14 @@ final class ClientRibProcessor implements ProcessorInterface
|
||||
? $clientId
|
||||
: $this->em->getRepository(Client::class)->find($clientId);
|
||||
|
||||
if ($client instanceof Client) {
|
||||
$rib->setClient($client);
|
||||
// read:false sur le POST : sans stade lecture, un parent introuvable n'est
|
||||
// plus intercepte en amont -> 404 explicite (sinon 500 au persist sur la
|
||||
// contrainte client_id NOT NULL).
|
||||
if (!$client instanceof Client) {
|
||||
throw new NotFoundHttpException('Client introuvable.');
|
||||
}
|
||||
|
||||
$rib->setClient($client);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Tests\Module\Commercial\Api;
|
||||
|
||||
use App\Module\Commercial\Domain\Entity\Client as ClientEntity;
|
||||
use App\Module\Commercial\Domain\Entity\ClientAddress;
|
||||
use App\Module\Commercial\Domain\Entity\ClientContact;
|
||||
use App\Module\Commercial\Domain\Entity\ClientRib;
|
||||
use App\Module\Commercial\Domain\Entity\PaymentType;
|
||||
@@ -94,6 +95,70 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
||||
self::assertSame('L\'adresse email n\'est pas valide.', $byPath['email']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression ERP-110 (bug subresource Link toProperty) : POST d'un contact sur
|
||||
* un client qui en a DEJA >= 2 ne doit pas exploser en 500
|
||||
* (NonUniqueResultException sur la resolution du parent), mais creer (201).
|
||||
*/
|
||||
public function testPostContactOnClientWithTwoExistingContactsReturns201(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$seed = $this->seedClient('Contact Multi');
|
||||
$this->seedContact($seed, 'Alpha');
|
||||
$this->seedContact($seed, 'Beta');
|
||||
|
||||
$client->request('POST', '/api/clients/'.$seed->getId().'/contacts', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => ['firstName' => 'Gamma'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Meme contexte (>= 2 contacts existants) : un email invalide doit produire
|
||||
* une 422 par champ (la validation est bien atteinte), pas une 500.
|
||||
*/
|
||||
public function testPostInvalidContactOnPopulatedClientReturns422OnField(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$seed = $this->seedClient('Contact Multi Bad');
|
||||
$this->seedContact($seed, 'Alpha');
|
||||
$this->seedContact($seed, 'Beta');
|
||||
|
||||
$response = $client->request('POST', '/api/clients/'.$seed->getId().'/contacts', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => ['firstName' => 'Gamma', 'email' => 'pas-un-email'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = [];
|
||||
foreach ($response->toArray(false)['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
self::assertArrayHasKey('email', $byPath);
|
||||
self::assertSame('L\'adresse email n\'est pas valide.', $byPath['email']);
|
||||
}
|
||||
|
||||
/**
|
||||
* ERP-110 : avec read:false sur le POST, un parent introuvable n'est plus
|
||||
* intercepte au stade lecture. Le 404 est desormais porte par
|
||||
* ClientContactProcessor::linkParent (sinon 500 au persist sur client_id
|
||||
* NOT NULL). Le payload est valide pour atteindre le processor (apres la
|
||||
* validation).
|
||||
*/
|
||||
public function testPostContactOnMissingClientReturns404(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
|
||||
$client->request('POST', '/api/clients/999999/contacts', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => ['firstName' => 'Orphan'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(404);
|
||||
}
|
||||
|
||||
public function testPatchContactNormalizes(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
@@ -201,6 +266,61 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression ERP-110 : POST d'une adresse sur un client qui en a DEJA >= 2 ne
|
||||
* doit pas exploser en 500 (NonUniqueResult sur la resolution du parent). Le
|
||||
* POST porte un site + une categorie (RG-1.10 / RG-1.29) pour etre valide.
|
||||
*/
|
||||
public function testPostAddressOnClientWithTwoExistingAddressesReturns201(): void
|
||||
{
|
||||
$this->skipIfSitesModuleDisabled();
|
||||
$client = $this->createAdminClient();
|
||||
$seed = $this->seedClient('Addr Multi');
|
||||
$siteIri = $this->firstSiteIri();
|
||||
$category = $this->createCategory('SECTEUR');
|
||||
$this->seedAddress($seed, 'Bordeaux');
|
||||
$this->seedAddress($seed, 'Lyon');
|
||||
|
||||
$client->request('POST', '/api/clients/'.$seed->getId().'/addresses', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => [
|
||||
'postalCode' => '75001',
|
||||
'city' => 'Paris',
|
||||
'street' => '2 rue Neuve',
|
||||
'sites' => [$siteIri],
|
||||
'categories' => ['/api/categories/'.$category->getId()],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(201);
|
||||
}
|
||||
|
||||
/**
|
||||
* ERP-110 : POST adresse sur un client inexistant -> 404 porte par
|
||||
* ClientAddressProcessor::linkParent (read:false). Payload valide (site +
|
||||
* categorie, RG-1.10 / RG-1.29) pour atteindre le processor.
|
||||
*/
|
||||
public function testPostAddressOnMissingClientReturns404(): void
|
||||
{
|
||||
$this->skipIfSitesModuleDisabled();
|
||||
$client = $this->createAdminClient();
|
||||
$siteIri = $this->firstSiteIri();
|
||||
$category = $this->createCategory('SECTEUR');
|
||||
|
||||
$client->request('POST', '/api/clients/999999/addresses', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => [
|
||||
'postalCode' => '75001',
|
||||
'city' => 'Paris',
|
||||
'street' => '2 rue Neuve',
|
||||
'sites' => [$siteIri],
|
||||
'categories' => ['/api/categories/'.$category->getId()],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(404);
|
||||
}
|
||||
|
||||
// === RIBs ===
|
||||
|
||||
public function testPostRibByAdminReturns201(): void
|
||||
@@ -239,6 +359,43 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression ERP-110 : POST d'un RIB sur un client qui en a DEJA >= 2 ne doit
|
||||
* pas exploser en 500 (NonUniqueResult sur la resolution du parent). L'admin
|
||||
* porte commercial.clients.accounting.manage requis par le POST.
|
||||
*/
|
||||
public function testPostRibOnClientWithTwoExistingRibsReturns201(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$seed = $this->seedClient('Rib Multi');
|
||||
$this->seedRib($seed, 'Compte 1');
|
||||
$this->seedRib($seed, 'Compte 2');
|
||||
|
||||
$client->request('POST', '/api/clients/'.$seed->getId().'/ribs', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => ['label' => 'Compte 3', 'bic' => self::VALID_BIC, 'iban' => self::VALID_IBAN],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(201);
|
||||
}
|
||||
|
||||
/**
|
||||
* ERP-110 : POST RIB sur un client inexistant -> 404 porte par
|
||||
* ClientRibProcessor::linkParent (read:false). L'admin porte
|
||||
* commercial.clients.accounting.manage ; payload valide (BIC / IBAN).
|
||||
*/
|
||||
public function testPostRibOnMissingClientReturns404(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
|
||||
$client->request('POST', '/api/clients/999999/ribs', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => ['label' => 'Orphan', 'bic' => self::VALID_BIC, 'iban' => self::VALID_IBAN],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(404);
|
||||
}
|
||||
|
||||
public function testDeleteRibNonLcrReturns204(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
@@ -306,13 +463,34 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* Seede un ClientRib valide rattache a un client (sans passer par l'API).
|
||||
* Seede une adresse minimale valide en base (sans passer par l'API) : seules
|
||||
* les colonnes NOT NULL sont posees (CP / ville / rue). Les M2M sites /
|
||||
* categories restent vides — non contraints en base, suffisant pour peupler
|
||||
* un client de plusieurs adresses.
|
||||
*/
|
||||
private function seedRib(ClientEntity $client): ClientRib
|
||||
private function seedAddress(ClientEntity $client, string $city): ClientAddress
|
||||
{
|
||||
$em = $this->getEm();
|
||||
$address = new ClientAddress();
|
||||
$address->setClient($client);
|
||||
$address->setPostalCode('33000');
|
||||
$address->setCity($city);
|
||||
$address->setStreet('1 rue du Test');
|
||||
$em->persist($address);
|
||||
$em->flush();
|
||||
|
||||
return $address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seede un ClientRib valide rattache a un client (sans passer par l'API). Le
|
||||
* libelle est parametrable pour seeder plusieurs RIB distincts.
|
||||
*/
|
||||
private function seedRib(ClientEntity $client, string $label = 'Seed RIB'): ClientRib
|
||||
{
|
||||
$em = $this->getEm();
|
||||
$rib = new ClientRib();
|
||||
$rib->setLabel('Seed RIB');
|
||||
$rib->setLabel($label);
|
||||
$rib->setBic(self::VALID_BIC);
|
||||
$rib->setIban(self::VALID_IBAN);
|
||||
$rib->setClient($client);
|
||||
|
||||
@@ -8,11 +8,14 @@ use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use ApiPlatform\Validator\Exception\ValidationException;
|
||||
use App\Module\Commercial\Application\Service\ClientFieldNormalizer;
|
||||
use App\Module\Commercial\Application\Validator\ClientAccountingCompletenessValidator;
|
||||
use App\Module\Commercial\Application\Validator\ClientInformationCompletenessValidator;
|
||||
use App\Module\Commercial\Domain\Entity\Bank;
|
||||
use App\Module\Commercial\Domain\Entity\Client;
|
||||
use App\Module\Commercial\Domain\Entity\ClientRib;
|
||||
use App\Module\Commercial\Domain\Entity\PaymentDelay;
|
||||
use App\Module\Commercial\Domain\Entity\PaymentType;
|
||||
use App\Module\Commercial\Domain\Entity\TvaMode;
|
||||
use App\Module\Commercial\Infrastructure\ApiPlatform\State\Processor\ClientProcessor;
|
||||
use App\Shared\Domain\Contract\BusinessRoleAwareInterface;
|
||||
use App\Shared\Domain\Security\BusinessRoles;
|
||||
@@ -280,6 +283,65 @@ final class ClientProcessorTest extends TestCase
|
||||
self::assertInstanceOf(Client::class, $processor->process($client, $this->operation()));
|
||||
}
|
||||
|
||||
public function testFullAccountingSubmitWithEmptyFieldsIsUnprocessable(): void
|
||||
{
|
||||
// spec-front § Onglet Comptabilite : une validation complete de l'onglet
|
||||
// (les 6 champs presents dans le payload) avec des valeurs vides -> 422.
|
||||
// C'est le bug corrige : avant, le back acceptait un onglet tout vide.
|
||||
$client = $this->minimalClient(); // aucun champ comptable renseigne
|
||||
|
||||
$processor = $this->makeProcessor(
|
||||
granted: ['commercial.clients.accounting.manage'],
|
||||
payload: $this->emptyAccountingPayload(),
|
||||
);
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
$processor->process($client, $this->operation());
|
||||
}
|
||||
|
||||
public function testFullAccountingSubmitWithAllFieldsPasses(): void
|
||||
{
|
||||
// Les 6 champs obligatoires renseignes + type de reglement neutre
|
||||
// (ni VIREMENT ni LCR -> ni banque ni RIB requis) -> 200.
|
||||
$client = $this->minimalClient();
|
||||
$client->setSiren('123456789');
|
||||
$client->setAccountNumber('00012345678');
|
||||
$client->setTvaMode(new TvaMode());
|
||||
$client->setNTva('FR12345678901');
|
||||
$client->setPaymentDelay(new PaymentDelay());
|
||||
$client->setPaymentType($this->paymentType('CHEQUE'));
|
||||
|
||||
$processor = $this->makeProcessor(
|
||||
granted: ['commercial.clients.accounting.manage'],
|
||||
payload: $this->emptyAccountingPayload(),
|
||||
);
|
||||
|
||||
self::assertInstanceOf(Client::class, $processor->process($client, $this->operation()));
|
||||
}
|
||||
|
||||
public function testPartialAccountingPatchSkipsCompleteness(): void
|
||||
{
|
||||
// Un PATCH ciblant un seul champ comptable n'est pas une validation
|
||||
// d'onglet : la completude n'est pas exigee (les autres champs restent
|
||||
// vides) -> 200. Preserve l'edition ponctuelle (ex. Compta corrige le SIREN).
|
||||
$client = $this->minimalClient();
|
||||
$client->setSiren('999999999');
|
||||
|
||||
$processor = $this->makeProcessor(
|
||||
granted: ['commercial.clients.accounting.manage'],
|
||||
payload: ['siren' => '999999999'],
|
||||
managed: true,
|
||||
originalData: [
|
||||
'siren' => '111111111',
|
||||
'companyName' => 'TEST CO',
|
||||
'triageService' => false,
|
||||
'isArchived' => false,
|
||||
],
|
||||
);
|
||||
|
||||
self::assertInstanceOf(Client::class, $processor->process($client, $this->operation()));
|
||||
}
|
||||
|
||||
public function testCommercialeIncompleteInformationIsUnprocessable(): void
|
||||
{
|
||||
// RG-1.04 : role Commerciale + onglet Information incomplet -> 422.
|
||||
@@ -379,6 +441,7 @@ final class ClientProcessorTest extends TestCase
|
||||
$persist,
|
||||
new ClientFieldNormalizer(),
|
||||
new ClientInformationCompletenessValidator(),
|
||||
new ClientAccountingCompletenessValidator(),
|
||||
$security,
|
||||
$requestStack,
|
||||
$em,
|
||||
@@ -398,6 +461,25 @@ final class ClientProcessorTest extends TestCase
|
||||
return $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Payload simulant une validation complete de l'onglet Comptabilite : les 6
|
||||
* champs obligatoires presents (le front les envoie toujours ensemble). Les
|
||||
* valeurs importent peu — la completude est evaluee sur l'etat de l'entite.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function emptyAccountingPayload(): array
|
||||
{
|
||||
return [
|
||||
'siren' => null,
|
||||
'accountNumber' => null,
|
||||
'tvaMode' => null,
|
||||
'nTva' => null,
|
||||
'paymentDelay' => null,
|
||||
'paymentType' => null,
|
||||
];
|
||||
}
|
||||
|
||||
private function paymentType(string $code): PaymentType
|
||||
{
|
||||
$type = new PaymentType();
|
||||
|
||||
Reference in New Issue
Block a user