Skip to content

Commit

Permalink
Merge pull request #312 from AgenceBio/fix/exports-empty-cultures
Browse files Browse the repository at this point in the history
  • Loading branch information
thom4parisot authored Feb 12, 2024
2 parents f43b518 + a9f05e5 commit af7bc57
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/components/Features/ExportStrategies/BaseExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class BaseExporter {
* @param {Feature[]} features
* @returns {String}
*/
export function generateAutresInfos (features, { withAnnotations = false, withCulture = true, withDate = true, withName = true, withNotes = true, withSurface = true, withVariete = true, pivot = null, initialCulture, permissions = {} } = {}) {
export function generateAutresInfos (features, { withAnnotations = false, withCode = true, withCulture = true, withDate = true, withName = true, withNotes = true, withSurface = true, withVariete = true, pivot = null, initialCulture, permissions = {} } = {}) {
const dateFmnt = new Intl.DateTimeFormat('fr-FR', {
timeZone: 'Europe/Paris',
day: 'numeric',
Expand All @@ -77,7 +77,7 @@ export function generateAutresInfos (features, { withAnnotations = false, withCu
.map(c => ([
// if we refine on a given culture, we certainly have a cell with its label
// so we don't make it redundant
pivot || (initialCulture === c.CPF) || !withCulture ? '' : cultureLabel(c, { withCode: true }),
pivot || (initialCulture === c.CPF) || !withCulture ? '' : cultureLabel(c, { withCode }),
withVariete && c.variete ? c.variete : '',
withDate && c.date_semis ? `semis le ${dateFmnt.format(new Date(c.date_semis))}` : '',
withSurface && c.surface ? `${c.surface}ha` : ''
Expand Down
11 changes: 7 additions & 4 deletions src/components/Features/ExportStrategies/BaseExporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ describe('generateAutresInfos', () => {
expect(generateAutresInfos(features, { initialCulture: '01.13.41.1' })).toBe('1.1, Chantenay à cœur rouge, semis le 31/03/2023, 1ha ; 1.2, Coucou')

expect(generateAutresInfos(features, { withNotes: false })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 31/03/2023, 1ha ; 1.2, 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withNotes: false, withName: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 31/03/2023, 1ha ; 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withNotes: false, withName: false, withVariete: false })).toBe('01.13.41.1 Carottes, semis le 31/03/2023, 1ha ; 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withNotes: false, withName: false, withDate: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, 1ha ; 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withNotes: false, withName: false, withDate: false, withSurface: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge ; 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withName: false, withNotes: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 31/03/2023, 1ha ; 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withName: false, withNotes: false, withVariete: false })).toBe('01.13.41.1 Carottes, semis le 31/03/2023, 1ha ; 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withDate: false, withName: false, withNotes: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, 1ha ; 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withDate: false, withName: false, withNotes: false, withSurface: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge ; 01.13.41.1 Carottes')

expect(generateAutresInfos(features, { withCode: false, withCulture: true, withDate: false, withName: false, withNotes: false, withSurface: false })).toBe('Carottes, Chantenay à cœur rouge ; Carottes')
expect(generateAutresInfos(features, { withCode: false, withCulture: false, withDate: false, withName: false, withNotes: false, withSurface: false })).toBe('Chantenay à cœur rouge')
})

test('with two features with multiple cultures each', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/Features/ExportStrategies/CertipaqExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ const getSheet = ({ featureCollection, operator, permissions }) => {

sheet_add_aoa(sheet, featureCollection.features.map(({ geometry, properties: props, id }) => {
const surfaceHa = (surface(geometry) / 10_000).toLocaleString('fr-FR', { maximumFractionDigits: 2 })
const culture = fromCodeCpf(props.cultures?.at(0)?.CPF)
const culture = props.cultures?.at(0) ? fromCodeCpf(props.cultures?.at(0).CPF) : { libelle_code_cpf: '[ERREUR] culture absente' }

return [
// Commune #A
props.COMMUNE_LABEL,
// Ilot #B
featureName({ properties: props }, { ilotLabel: '', parcelleLabel: '', separator: '_', placeholder: '' }),
// Libellé Culture #C
culture?.libelle_code_cpf ?? `[ERREUR] culture inconnue (${props.cultures?.at(0)?.CPF})`,
culture?.libelle_code_cpf ?? `[ERREUR] culture inconnue (${props.cultures?.at(0).CPF})`,
// Variété / infos #D
generateAutresInfos([{ id, geometry, properties: props }], { withDate: false, withName: false, withNotes: true, withSurface: false, withVariete: true, initialCulture: culture?.code_cpf }),
generateAutresInfos([{ id, geometry, properties: props }], { withCode: false, withDate: false, withName: false, withNotes: true, withSurface: false, withVariete: true, initialCulture: culture?.code_cpf }),
// C0 - AB - C1 - C2 - C3
props.conversion_niveau === 'CONV' ? surfaceHa : '',
props.conversion_niveau === 'AB' ? surfaceHa : '',
Expand Down
25 changes: 22 additions & 3 deletions src/components/Features/ExportStrategies/CertipaqExporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ describe('CertipaqExporter', () => {
'',
'2_1',
'Luzerne',
'01.19.10.7 Trèfle, 4 feuilles',
'Trèfle, 4 feuilles',
'',
'1,05',
'',
'',
'',
new Date('2021-01-01T00:00:00.000Z'),
'01.19.10.7 Trèfle, semis le 01/03/2023, Réduction de conversion (Dérogation acceptée)',
'0.70ha / 01.19.10.7 Trèfle, semis le 01/03/2023, 0.30ha, Réduction de conversion (Dérogation acceptée)',
'',
'',
'',
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('CertipaqExporter', () => {
'',
'3_1',
'[ERREUR] culture inconnue (01.19.99)',
'01.19.99 Culture inconnue',
'Culture inconnue',
'',
'',
'',
Expand All @@ -131,6 +131,25 @@ describe('CertipaqExporter', () => {
'',
'5',
''
],
[
'',
'4_1',
'[ERREUR] culture absente',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'6',
''
]
]

Expand Down
8 changes: 4 additions & 4 deletions src/components/Features/ExportStrategies/CertisExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const getSheet = ({ featureCollection, operator, permissions, record }) => {

sheet_add_aoa(sheet, featureCollection.features.map(({ geometry, properties }) => {
const surfaceHa = surface(geometry) / 10_000
const culture = fromCodeCpf(properties.cultures.at(0)?.CPF)
const culture = properties.cultures.at(0) ? fromCodeCpf(properties.cultures.at(0)?.CPF) : { libelle_code_cpf: '[ERREUR] culture absente' }

return [
// Nom de la parcelle
Expand All @@ -95,11 +95,11 @@ const getSheet = ({ featureCollection, operator, permissions, record }) => {
// Précédent\n(année n-1)
'',
// Type de culture
culture?.code_cpf ?? `[ERREUR] culture inconnue (${properties.cultures.at(0)?.CPF})`,
culture?.code_cpf,
// Liste secondaire
culture?.libelle_code_cpf,
culture?.libelle_code_cpf ?? `[ERREUR] culture inconnue (${properties.cultures.at(0)?.CPF})`,
// Espèces implantées
generateAutresInfos([{ properties }], { withDate: false, withName: false, withNotes: false, withSurface: true, withVariete: true, initialCulture: culture?.code_cpf }),
generateAutresInfos([{ properties }], { withDate: false, withName: false, withNotes: false, withSurface: false, withVariete: true, initialCulture: culture?.code_cpf }),
// Degré de conversion de la parcelle/ilot
properties.conversion_niveau,
// Date de semis/implantation
Expand Down
29 changes: 28 additions & 1 deletion src/components/Features/ExportStrategies/CertisExporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ describe('CertisExporter', () => {
1.0464881572673355,
'',
'',
'[ERREUR] culture inconnue (01.19.99)',
'',
'[ERREUR] culture inconnue (01.19.99)',
'01.19.99 Culture inconnue',
'',
'01.19.99 Culture inconnue',
Expand All @@ -179,6 +179,33 @@ describe('CertisExporter', () => {
'',
'',
'5'
],
[
'',
'4',
'1',
1.0464881572673355,
'',
'',
'',
'[ERREUR] culture absente',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'6'
]
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const getSheet = ({ featureCollection, permissions }) => {

sheet_add_aoa(sheet, [
[
culture?.libelle_code_cpf ?? `[ERREUR] culture inconnue (${mainKey})`,
mainKey === '__nogroup__' ? '[ERREUR] culture absente' : (culture?.libelle_code_cpf ?? `[ERREUR] culture inconnue (${mainKey})`),
surface / 10_000,
autresInfos,
features.at(0).properties.conversion_niveau ?? '',
Expand Down
12 changes: 10 additions & 2 deletions src/components/Features/ExportStrategies/CertisudExporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ describe('CertisudExporter', () => {
'',
'5'
],
[
'[ERREUR] culture absente',
1.0464881572673355,
'4.1',
'',
'',
'6'
],
[
'Luzerne',
// expect.closeTo(2.1), // in vite@5 + vitest@1
Expand All @@ -48,7 +56,7 @@ describe('CertisudExporter', () => {
'Luzerne',
// expect.closeTo(1.0), // in vite@5 + vitest@1
1.0464881572673355,
'2.1, Réduction de conversion (Dérogation acceptée)',
'2.1, 0.70ha, Réduction de conversion (Dérogation acceptée)',
'AB',
new Date('2021-01-01T00:00:00.000Z'),
'3'
Expand All @@ -57,7 +65,7 @@ describe('CertisudExporter', () => {
'Trèfle',
// expect.closeTo(1.0), // in vite@5 + vitest@1
1.0464881572673355,
'2.1, 4 feuilles, semis le 01/03/2023, Réduction de conversion (Dérogation acceptée)',
'2.1, 4 feuilles, semis le 01/03/2023, 0.30ha, Réduction de conversion (Dérogation acceptée)',
'AB',
new Date('2021-01-01T00:00:00.000Z'),
'3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const getSheet = ({ featureCollection, operator, permissions }) => {

utils.sheet_add_aoa(sheet, featureCollection.features.map(({ geometry, properties: props }) => {
const surfaceHa = surface(geometry) / 10_000
const culture = fromCodeCpf(props.cultures.at(0)?.CPF)
const culture = props.cultures.at(0) ? fromCodeCpf(props.cultures.at(0)?.CPF) : { libelle_code_cpf: '[ERREUR] culture absente' }

return [
props.cadastre,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('ControlUnionExporter', () => {
1.0464881572673355,
new Date('2021-01-01T00:00:00.000Z'),
'AB',
'2.1, 01.19.10.7 Trèfle, 4 feuilles, semis le 01/03/2023, Réduction de conversion (Dérogation acceptée)',
'2.1, 0.70ha / 01.19.10.7 Trèfle, 4 feuilles, semis le 01/03/2023, 0.30ha, Réduction de conversion (Dérogation acceptée)',
'3',
'01.19.10.8'
],
Expand All @@ -78,6 +78,16 @@ describe('ControlUnionExporter', () => {
'3.1, 01.19.99 Culture inconnue',
'5',
''
],
[
'',
'[ERREUR] culture absente',
1.0464881572673355,
'',
'',
'4.1',
'6',
''
]
]

Expand Down
7 changes: 3 additions & 4 deletions src/components/Features/ExportStrategies/DefaultExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ const getSheet = ({ featureCollection, operator, permissions }) => {
sheet_add_aoa(sheet, featureCollection.features.map(({ geometry, properties: props, id }) => {
const [ilotId, parcelleId] = [props.NUMERO_I, props.NUMERO_P]
const firstCulture = props.cultures.at(0)
const label = firstCulture?.TYPE_LIBELLE ?? fromCodeCpf(firstCulture?.CPF)?.libelle_code_cpf ?? ''
const culture = firstCulture ? fromCodeCpf(firstCulture?.CPF) : { libelle_code_cpf: '[ERREUR] culture absente' }
const surfaceHa = surface(geometry) / 10_000
const isPac = Boolean(props.PACAGE)
const culture = firstCulture.CPF ?? firstCulture.TYPE ?? ''

return [
String(props.id),
ilotId,
parcelleId,
surfaceHa,
culture,
label,
firstCulture?.CPF,
culture?.libelle_code_cpf ?? `[ERREUR] culture inconnue (${firstCulture?.CPF})`,
props.PACAGE,
props.conversion_niveau,
props.engagement_date ? new Date(props.engagement_date) : '',
Expand Down
16 changes: 15 additions & 1 deletion src/components/Features/ExportStrategies/DefaultExporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('DefaultExporter', () => {
'AB',
new Date('2021-01-01T00:00:00.000Z'),
'',
'01.19.10.7 Trèfle, 4 feuilles, semis le 01/03/2023, Réduction de conversion (Dérogation acceptée)',
'0.70ha / 01.19.10.7 Trèfle, 4 feuilles, semis le 01/03/2023, 0.30ha, Réduction de conversion (Dérogation acceptée)',
''
],
[
Expand All @@ -97,11 +97,25 @@ describe('DefaultExporter', () => {
'1',
1.0464881572673355,
'01.19.99',
'[ERREUR] culture inconnue (01.19.99)',
'',
'',
'',
'',
'',
''
],
[
'6',
'4',
'1',
1.0464881572673355,
'',
'[ERREUR] culture absente',
'',
'',
'',
'',
'',
''
]
Expand Down
6 changes: 3 additions & 3 deletions src/components/Features/ExportStrategies/OcaciaExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const getSheet = ({ featureCollection, permissions }) => {

sheet_add_aoa(sheet, featureCollection.features.map(({ id, geometry, properties: props }) => {
const surfaceHa = surface(geometry) / 10_000
const culture = fromCodeCpf(props.cultures.at(0)?.CPF)
const culture = props.cultures.at(0) ? fromCodeCpf(props.cultures.at(0)?.CPF) : { libelle_code_cpf: '[ERREUR] culture absente' }

return [
// Commune
Expand All @@ -57,7 +57,7 @@ const getSheet = ({ featureCollection, permissions }) => {
// N° Cadastre
props.cadastre,
// Variété / infos
generateAutresInfos([{ id, geometry, properties: props }], { withDate: false, withName: false, withNotes: false, withVariete: true, initialCulture: culture?.code_cpf }),
generateAutresInfos([{ id, geometry, properties: props }], { withDate: false, withName: false, withNotes: false, withSurface: false, withVariete: true, initialCulture: culture?.code_cpf }),
// C0 - AB - C1 - C2 - C3
props.conversion_niveau === 'CONV' ? surfaceHa : '',
props.conversion_niveau === 'AB' ? surfaceHa : '',
Expand All @@ -67,7 +67,7 @@ const getSheet = ({ featureCollection, permissions }) => {
// Date conv #K
props.engagement_date ? new Date(props.engagement_date) : '',
// Observation / date de semis
generateAutresInfos([{ id, geometry, properties: props }], { withAnnotations: true, withDate: true, withName: false, withNotes: true, withVariete: false, initialCulture: culture?.code_cpf, permissions }),
generateAutresInfos([{ id, geometry, properties: props }], { withAnnotations: true, withDate: true, withName: false, withNotes: true, withSurface: true, withVariete: false, initialCulture: culture?.code_cpf, permissions }),
// Précédent
'',
// Anté précédent
Expand Down
22 changes: 21 additions & 1 deletion src/components/Features/ExportStrategies/OcaciaExporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('OcaciaExporter', () => {
'',
'',
new Date('2021-01-01T00:00:00.000Z'),
'01.19.10.7 Trèfle, semis le 01/03/2023, Réduction de conversion (Dérogation acceptée)',
'0.70ha / 01.19.10.7 Trèfle, semis le 01/03/2023, 0.30ha, Réduction de conversion (Dérogation acceptée)',
'',
'',
'',
Expand Down Expand Up @@ -138,6 +138,26 @@ describe('OcaciaExporter', () => {
'',
'',
'5'
],
[
'',
'4.1',
'[ERREUR] culture absente',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'6'
]
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const getSheet = ({ featureCollection, permissions }) => {
]

featureCollection.features.forEach(({ geometry, properties }, index) => {
const firstCulture = fromCodeCpf(properties.cultures.at(0)?.CPF)
const firstCulture = properties.cultures.at(0) ? fromCodeCpf(properties.cultures.at(0)?.CPF) : { code_cpf: '[ERREUR] culture absente' }
const autresInfos = generateAutresInfos([ { properties }], { withName: false, withAnnotations: true, initialCulture: firstCulture?.code_cpf, permissions })
const rowIndex = 2 + index

Expand Down
Loading

0 comments on commit af7bc57

Please sign in to comment.