feat(front) : onglet comptabilite prestataire (ERP-144) (#106)
Auto Tag Develop / tag (push) Successful in 8s

Empilée sur ERP-143 (#105).

## Périmètre ERP-144
Onglet **Comptabilité** de l'écran `/providers/new` — gated par permission + blocs RIB conditionnels.

- Champs (`Malio*`) : SIREN / Numéro de compte / Mode de TVA (`/api/tva_modes`) / N° de TVA / Délai (`/api/payment_delays`) / Type de règlement (`/api/payment_types`) / Banque (`/api/banks`).
- **RG-3.07** : Banque visible **et** obligatoire **seulement si** Type = `VIREMENT` (affichage conditionnel + payload `bank` forcé à null sinon).
- **RG-3.08** : blocs RIB (Libellé/BIC/IBAN) affichés et requis si Type = `LCR` ; « + RIB » gated (dernier RIB complet) / Supprimer (modal). À la validation, **POST des RIB AVANT** le PATCH des scalaires (le back valide RG-3.08 sur le PATCH).
- **Gating** : onglet présent uniquement si `technique.providers.accounting.view` ; **éditable** uniquement si `.manage` (sinon lecture seule). Masqué pour Bureau/Commerciale.
- « Valider » → PATCH `/api/providers/{id}` (groupe `provider:write:accounting`) + sous-ressource RIBs (`/providers/{id}/ribs` + `/provider_ribs/{id}`). Erreurs 422 inline (scalaires) et par ligne (RIB).
- `useProviderReferentials.loadAccounting()` (chargé seulement si l'onglet est accessible). Helpers purs `utils/forms/providerAccounting.ts`.
- i18n `technique.providers.form.accounting` + `confirmDelete.rib`.

> NB : les placeholders **Rapports / Échanges** relèvent des écrans Consultation/Modification (ERP-145) — le flux de **création** ne porte que 3 onglets (Contact/Adresse/Comptabilité), conformément à la spec.

## Conformité
- `useApi()` only ; `Malio*` only ; pas de masque email ; aucun texte FR en dur ; pas d'import inter-module (helpers ré-implémentés côté Technique, règle ABSOLUE n°1).

## Vérifications
- Vitest : 454/454 (18 nouveaux : helpers compta RG-3.07/3.08, workflow VIREMENT/LCR, ordre RIB→scalaires, 422 inline + par ligne, lecture seule sans manage).
- ESLint : OK.
- `nuxi typecheck` : 0 erreur sur les fichiers source du ticket.
- Golden path navigateur : page compile, onglet Comptabilité visible (gating accounting.view OK pour admin). Contenu de l'onglet gaté derrière le déverrouillage des 3 onglets (multiselect `Malio` non pilotable en a11y) — couvert par les tests unitaires + typecheck.

Reviewed-on: #106
Co-authored-by: tristan <tristan@yuno.malio.fr>
Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #106.
This commit is contained in:
2026-06-15 09:15:20 +00:00
committed by Autin
parent 17aa61d014
commit c25c33116d
8 changed files with 793 additions and 5 deletions
@@ -19,8 +19,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
const mockPost = vi.hoisted(() => vi.fn())
const mockPatch = vi.hoisted(() => vi.fn())
// Permission accounting.view pilotable par test (presence de l'onglet Comptabilite).
const permState = vi.hoisted(() => ({ accountingView: false }))
// Permissions comptables pilotables par test (presence/edition de l'onglet Comptabilite).
const permState = vi.hoisted(() => ({ accountingView: false, accountingManage: false }))
vi.stubGlobal('useApi', () => ({
get: vi.fn(),
@@ -37,7 +37,11 @@ vi.stubGlobal('useToast', () => ({
info: vi.fn(),
}))
vi.stubGlobal('usePermissions', () => ({
can: (perm: string) => perm === 'technique.providers.accounting.view' ? permState.accountingView : true,
can: (perm: string) => {
if (perm === 'technique.providers.accounting.view') return permState.accountingView
if (perm === 'technique.providers.accounting.manage') return permState.accountingManage
return true
},
}))
const { useProviderForm, buildProviderCreateTabKeys } = await import('../useProviderForm')
@@ -62,6 +66,7 @@ describe('useProviderForm', () => {
mockPost.mockReset()
mockPatch.mockReset()
permState.accountingView = false
permState.accountingManage = false
})
it('RG-3.03/RG-3.09 (front) : bloque le POST si aucun site / aucune categorie', async () => {
@@ -208,6 +213,7 @@ describe('useProviderForm — onglet Contact (ERP-142)', () => {
mockPost.mockReset()
mockPatch.mockReset()
permState.accountingView = false
permState.accountingManage = false
})
/** Place le formulaire en etat « prestataire cree » (onglet Contact accessible). */
@@ -315,6 +321,7 @@ describe('useProviderForm — onglet Adresse (ERP-143)', () => {
mockPost.mockReset()
mockPatch.mockReset()
permState.accountingView = false
permState.accountingManage = false
})
/** Place le formulaire en etat « prestataire cree » (onglet Adresse accessible). */
@@ -406,3 +413,175 @@ describe('useProviderForm — onglet Adresse (ERP-143)', () => {
expect(form.isValidated('address')).toBe(false)
})
})
describe('useProviderForm — onglet Comptabilite (ERP-144)', () => {
const TVA = '/api/tva_modes/1'
const DELAY = '/api/payment_delays/1'
const TYPE = '/api/payment_types/3'
const BANK = '/api/banks/2'
beforeEach(() => {
mockPost.mockReset()
mockPatch.mockReset()
permState.accountingView = true
permState.accountingManage = true
})
/** Prestataire cree, onglet Comptabilite editable (view + manage). */
function createdForm() {
const form = useProviderForm()
form.providerId.value = 7
return form
}
/** Remplit les scalaires comptables communs. */
function fillScalars(form: ProviderForm): void {
form.accounting.siren = '123456789'
form.accounting.accountNumber = '4010'
form.accounting.tvaModeIri = TVA
form.accounting.nTva = 'FR123'
form.accounting.paymentDelayIri = DELAY
form.accounting.paymentTypeIri = TYPE
}
it('lecture seule sans accounting.manage (Compta consultation / autres roles)', () => {
permState.accountingManage = false
const form = createdForm()
expect(form.accountingReadonly.value).toBe(true)
permState.accountingManage = true
const form2 = createdForm()
expect(form2.accountingReadonly.value).toBe(false)
})
it('RG-3.07 : setPaymentType(VIREMENT) garde la banque ; un autre type la vide', () => {
const form = createdForm()
form.accounting.bankIri = BANK
// Type VIREMENT -> banque requise, conservee.
form.setPaymentType(TYPE, true, false)
expect(form.accounting.bankIri).toBe(BANK)
// Type non-VIREMENT -> banque videe (sans objet).
form.setPaymentType(TYPE, false, false)
expect(form.accounting.bankIri).toBeNull()
})
it('RG-3.08 : setPaymentType(LCR) garantit au moins un bloc RIB', () => {
const form = createdForm()
expect(form.ribs.value).toHaveLength(0)
form.setPaymentType(TYPE, false, true)
expect(form.ribs.value).toHaveLength(1)
})
it('« + RIB » desactive tant que le dernier RIB est incomplet (RG-3.08)', () => {
const form = createdForm()
form.setPaymentType(TYPE, false, true)
expect(form.canAddRib.value).toBe(false)
const rib = form.ribs.value[0]
if (rib) {
rib.label = 'Compte'
rib.bic = 'BNPAFRPP'
rib.iban = 'FR76...'
}
expect(form.canAddRib.value).toBe(true)
})
it('VIREMENT : PATCH des scalaires avec banque, aucun appel RIB', async () => {
mockPatch.mockResolvedValueOnce({})
const form = createdForm()
fillScalars(form)
form.accounting.bankIri = BANK
const ok = await form.submitAccounting(true, false, vi.fn())
expect(ok).toBe(true)
expect(mockPost).not.toHaveBeenCalled()
expect(mockPatch).toHaveBeenCalledWith(
'/providers/7',
expect.objectContaining({ paymentType: TYPE, bank: BANK, siren: '123456789' }),
{ toast: false },
)
expect(form.isValidated('accounting')).toBe(true)
})
it('hors VIREMENT : la banque part a null dans le payload (RG-3.07)', async () => {
mockPatch.mockResolvedValueOnce({})
const form = createdForm()
fillScalars(form)
form.accounting.bankIri = BANK // residu : doit etre ignore (isBankRequired=false)
await form.submitAccounting(false, false, vi.fn())
const body = mockPatch.mock.calls[0]?.[1] as Record<string, unknown>
expect(body.bank).toBeNull()
})
it('LCR : POST des RIB AVANT le PATCH des scalaires (ordre RG-3.08)', async () => {
mockPost.mockResolvedValueOnce({ id: 50 })
mockPatch.mockResolvedValueOnce({})
const form = createdForm()
fillScalars(form)
form.setPaymentType(TYPE, false, true)
const rib = form.ribs.value[0]
if (rib) {
rib.label = 'Compte'
rib.bic = 'BNPAFRPP'
rib.iban = 'FR76...'
}
const ok = await form.submitAccounting(false, true, vi.fn())
expect(ok).toBe(true)
expect(mockPost).toHaveBeenCalledWith(
'/providers/7/ribs',
expect.objectContaining({ label: 'Compte', bic: 'BNPAFRPP', iban: 'FR76...' }),
{ headers: { Accept: 'application/ld+json' }, toast: false },
)
expect(form.ribs.value[0]?.id).toBe(50)
// Le PATCH des scalaires intervient APRES la creation du RIB.
expect(mockPatch).toHaveBeenCalledWith('/providers/7', expect.any(Object), { toast: false })
})
it('422 sur les scalaires (bank) : mapping inline, onglet non finalise', async () => {
mockPatch.mockRejectedValueOnce({
response: {
status: 422,
_data: { violations: [{ propertyPath: 'bank', message: 'La banque est obligatoire pour un virement.' }] },
},
})
const form = createdForm()
fillScalars(form)
const ok = await form.submitAccounting(true, false, vi.fn())
expect(ok).toBe(false)
expect(form.accountingErrors.errors.bank).toBe('La banque est obligatoire pour un virement.')
expect(form.isValidated('accounting')).toBe(false)
})
it('LCR : 422 RIB par ligne -> pas de PATCH des scalaires', async () => {
mockPost.mockRejectedValueOnce({
response: {
status: 422,
_data: { violations: [{ propertyPath: 'iban', message: 'L\'IBAN est obligatoire.' }] },
},
})
const form = createdForm()
fillScalars(form)
form.setPaymentType(TYPE, false, true)
const rib = form.ribs.value[0]
if (rib) {
rib.label = 'Compte'
rib.bic = 'BNPAFRPP'
}
const ok = await form.submitAccounting(false, true, vi.fn())
expect(ok).toBe(false)
expect(form.ribErrors.value[0]?.iban).toBe('L\'IBAN est obligatoire.')
expect(mockPatch).not.toHaveBeenCalled()
})
})
@@ -2,15 +2,20 @@ import { computed, reactive, ref, type Ref } from 'vue'
import { useFormErrors } from '~/shared/composables/useFormErrors'
import { mapViolationsToRecord } from '~/shared/utils/api'
import {
emptyProviderAccounting,
emptyProviderAddress,
emptyProviderContact,
emptyProviderMain,
emptyProviderRib,
type ProviderAccountingDraft,
type ProviderAddressFormDraft,
type ProviderAddressResponse,
type ProviderContactFormDraft,
type ProviderContactResponse,
type ProviderMainDraft,
type ProviderMainResponse,
type ProviderRibFormDraft,
type ProviderRibResponse,
} from '~/modules/technique/types/providerForm'
import {
buildProviderContactPayload,
@@ -20,6 +25,12 @@ import {
buildProviderAddressPayload,
isProviderAddressValid,
} from '~/modules/technique/utils/forms/providerAddress'
import {
buildProviderAccountingPayload,
buildProviderRibPayload,
isRibBlank,
isRibComplete,
} from '~/modules/technique/utils/forms/providerAccounting'
/**
* Workflow de l'ecran « Ajouter un prestataire » (M3 Technique, ERP-141) —
@@ -72,6 +83,7 @@ export function useProviderForm() {
// ── Onglets : ordre + gating progressif ───────────────────────────────────
const canAccountingView = computed(() => can('technique.providers.accounting.view'))
const canAccountingManage = computed(() => can('technique.providers.accounting.manage'))
const tabKeys = computed(() => buildProviderCreateTabKeys(canAccountingView.value))
// Index du dernier onglet deverrouille (-1 tant que le prestataire n'est pas cree).
@@ -370,6 +382,130 @@ export function useProviderForm() {
}
}
// ── Onglet Comptabilite (ERP-144) ─────────────────────────────────────────
const accounting = reactive<ProviderAccountingDraft>(emptyProviderAccounting())
const ribs = ref<ProviderRibFormDraft[]>([])
const accountingErrors = useFormErrors()
// Erreurs 422 par ligne de RIB (alignees sur l'index du v-for).
const ribErrors = ref<Record<string, string>[]>([])
// L'onglet est editable seulement avec accounting.manage (sinon lecture seule).
const accountingReadonly = computed(() => isValidated('accounting') || !canAccountingManage.value)
/**
* Met a jour le type de reglement (IRI) en propageant ses RG inter-champs :
* - hors VIREMENT (RG-3.07) : on vide la banque (sans objet) ;
* - LCR (RG-3.08) : on garantit au moins un bloc RIB visible ; hors LCR, on
* purge les erreurs de RIB (les blocs sont conserves mais non persistes).
* `isBankRequired` / `isRibRequired` sont calcules par l'appelant (page) a
* partir du code resolu via les referentiels.
*/
function setPaymentType(iri: string | null, isBankRequired: boolean, isRibRequired: boolean): void {
accounting.paymentTypeIri = iri
if (!isBankRequired) {
accounting.bankIri = null
}
if (isRibRequired) {
if (ribs.value.length === 0) {
ribs.value.push(emptyProviderRib())
}
}
else {
ribErrors.value = []
}
}
// « + RIB » desactive tant que le dernier bloc RIB n'est pas complet (RG-3.08).
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(emptyProviderRib())
}
}
function removeRib(index: number): void {
ribs.value.splice(index, 1)
ribErrors.value.splice(index, 1)
// Garde au moins un bloc RIB visible (sous LCR).
if (ribs.value.length === 0) {
ribs.value.push(emptyProviderRib())
}
}
/**
* Valide l'onglet Comptabilite : (1) sous LCR, POST/PATCH des RIB d'abord
* (le back valide RG-3.08 sur le PATCH scalaires, les RIB doivent donc exister
* AVANT) ; (2) PATCH des scalaires comptables (groupe provider:write:accounting,
* banque envoyee seulement si VIREMENT — RG-3.07). Erreurs RIB par ligne ;
* erreurs scalaires inline (bank/paymentType). Retourne true si l'onglet a ete
* valide.
*/
async function submitAccounting(
isBankRequired: boolean,
isRibRequired: boolean,
onRibError: (error: unknown) => void,
): Promise<boolean> {
if (providerId.value === null || tabSubmitting.value) {
return false
}
tabSubmitting.value = true
accountingErrors.clearErrors()
try {
// 1) RIB d'abord, uniquement sous LCR. Une amorce vide neuve est sautee
// s'il reste un autre RIB soumettable ; sinon (LCR sans aucun RIB rempli)
// on la soumet pour declencher la 422 NotBlank inline.
if (isRibRequired) {
const hasSubmittableRib = ribs.value.some(r => r.id !== null || !isRibBlank(r))
const ribHasError = await submitRows(
ribs.value,
ribErrors,
async (rib) => {
const body = buildProviderRibPayload(rib)
if (rib.id === null) {
const created = await api.post<ProviderRibResponse>(
`/providers/${providerId.value}/ribs`,
body,
{ headers: { Accept: 'application/ld+json' }, toast: false },
)
rib.id = created.id
}
else {
await api.patch(`/provider_ribs/${rib.id}`, body, { toast: false })
}
},
onRibError,
rib => hasSubmittableRib && rib.id === null && isRibBlank(rib),
)
if (ribHasError) {
return false
}
}
// 2) PATCH des scalaires comptables (erreurs inline sur leurs champs).
try {
await api.patch(
`/providers/${providerId.value}`,
buildProviderAccountingPayload(accounting, isBankRequired),
{ toast: false },
)
}
catch (error) {
accountingErrors.handleApiError(error, { fallbackMessage: t('technique.providers.toast.error') })
return false
}
completeTab('accounting')
return true
}
finally {
tabSubmitting.value = false
}
}
return {
// etat
main,
@@ -380,6 +516,7 @@ export function useProviderForm() {
mainErrors,
// onglets
canAccountingView,
canAccountingManage,
tabKeys,
activeTab,
unlockedIndex,
@@ -399,6 +536,17 @@ export function useProviderForm() {
addAddress,
removeAddress,
submitAddresses,
// comptabilite
accounting,
ribs,
accountingErrors,
ribErrors,
accountingReadonly,
setPaymentType,
canAddRib,
addRib,
removeRib,
submitAccounting,
// actions
validateMainFront,
buildMainPayload,
@@ -28,10 +28,20 @@ export interface RefOption {
label: string
}
/** Option de type de reglement enrichie de son code stable (RG-3.07 / RG-3.08). */
export interface PaymentTypeOption extends RefOption {
code: string
}
interface HydraMember {
'@id': string
}
interface ReferentialMember extends HydraMember {
code: string
label: string
}
interface CategoryMember extends HydraMember {
code: string
name: string
@@ -55,6 +65,11 @@ export function useProviderReferentials() {
const categories = ref<RefOption[]>([])
const sites = ref<RefOption[]>([])
const countries = ref<RefOption[]>([])
// Referentiels comptables (charges a la demande via loadAccounting).
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>(
@@ -88,10 +103,34 @@ export function useProviderReferentials() {
])
}
/**
* Charge les referentiels comptables (onglet Comptabilite, ERP-144). Appele
* uniquement quand l'utilisateur peut voir l'onglet (accounting.view). Resilient
* (allSettled) : un referentiel en echec reste vide.
*/
async function loadAccounting(): Promise<void> {
await Promise.allSettled([
fetchAll<ReferentialMember>('/tva_modes')
.then((list) => { tvaModes.value = list.map(t => ({ value: t['@id'], label: t.label })) }),
fetchAll<ReferentialMember>('/payment_delays')
.then((list) => { paymentDelays.value = list.map(d => ({ value: d['@id'], label: d.label })) }),
// Le code stable du type sert les RG-3.07 (VIREMENT) / RG-3.08 (LCR).
fetchAll<ReferentialMember>('/payment_types')
.then((list) => { paymentTypes.value = list.map(p => ({ value: p['@id'], label: p.label, code: p.code })) }),
fetchAll<ReferentialMember>('/banks')
.then((list) => { banks.value = list.map(b => ({ value: b['@id'], label: b.label })) }),
])
}
return {
categories,
sites,
countries,
tvaModes,
paymentDelays,
paymentTypes,
banks,
loadMain,
loadAccounting,
}
}