143 lines
5.5 KiB
TypeScript
143 lines
5.5 KiB
TypeScript
/**
|
|
* Helpers purs de payload de l'ecran « Ajouter un fournisseur » (M2 Commercial),
|
|
* partages avec la future modification (96) — miroir de `clientEdit.ts` (M1).
|
|
*
|
|
* Scoping STRICT des payloads (mode strict, aligne ERP-74/RG) : 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,
|
|
MAIN_REQUIRED_NON_NULLABLE_KEYS,
|
|
omitEmptyRequired,
|
|
RIB_REQUIRED_NON_NULLABLE_KEYS,
|
|
} from '~/modules/commercial/utils/supplierFormRules'
|
|
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
|
|
}
|
|
|
|
/**
|
|
* Payload du bloc principal — groupe supplier:write:main UNIQUEMENT.
|
|
* companyName omis si vide -> 422 NotBlank au lieu d'un 400 de type (ERP-119).
|
|
*/
|
|
export function buildMainPayload(main: MainFormDraft): Record<string, unknown> {
|
|
return omitEmptyRequired({
|
|
companyName: main.companyName,
|
|
categories: main.categoryIris,
|
|
}, MAIN_REQUIRED_NON_NULLABLE_KEYS)
|
|
}
|
|
|
|
/** 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): Record<string, unknown> {
|
|
return omitEmptyRequired({
|
|
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)
|
|
}
|
|
|
|
/** Payload d'un RIB (sous-ressource supplier_rib). */
|
|
export function buildRibPayload(rib: SupplierRibFormDraft): Record<string, unknown> {
|
|
return omitEmptyRequired({
|
|
label: rib.label,
|
|
bic: rib.bic,
|
|
iban: rib.iban,
|
|
}, RIB_REQUIRED_NON_NULLABLE_KEYS)
|
|
}
|