Skip to content

Commit

Permalink
Merge pull request #264 from AgenceBio/fix/certipaq-export
Browse files Browse the repository at this point in the history
  • Loading branch information
thom4parisot authored Nov 22, 2023
2 parents 75012f3 + 83aebb0 commit 62bf84a
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 36 deletions.
26 changes: 11 additions & 15 deletions src/components/Features/ExportStrategies/BaseExporter.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { utils } from 'xlsx'
import { cultureLabel, featureName } from "../index.js"

const frNumbers = new Intl.NumberFormat('fr-FR', {
style: 'decimal',
minimumSignificantDigits: 2,
maximumSignificantDigits: 2
})

export default class BaseExporter {
label = ''
extension = ''
mimetype = ''
origin = 'A1'
range = null

constructor ({ featureCollection, operator, record }) {
this.featureCollection = featureCollection
Expand All @@ -20,20 +14,22 @@ export default class BaseExporter {
}

toJSON () {
return utils.sheet_to_json(this.getSheet(), { header: 1, origin })
const ws = this.getSheet()
return utils.sheet_to_json(ws, {
blankrows: false,
defval: '',
header: 1,
range: this.range ?? ws['!ref']
})
}
}

export function humanNumbers (float) {
return frNumbers.format(float)
}

/**
*
* @param {Feature[]} features
* @returns {String}
*/
export function generateAutresInfos (features, { withName = true, withNotes = true, withDate = true, withSurface = true, withVariete = true, pivot = null, initialCulture } = {}) {
export function generateAutresInfos (features, { withDate = true, withName = true, withNotes = true, withSurface = true, withVariete = true, pivot = null, initialCulture } = {}) {
return features.map(feature => {
const name = withName ? featureName(feature, { ilotLabel: '', parcelleLabel: '', separator: '.', placeholder: '' }) : ''
const notes = withNotes ? feature.properties.auditeur_notes : ''
Expand All @@ -47,8 +43,8 @@ export function generateAutresInfos (features, { withName = true, withNotes = tr
// 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) ? '' : cultureLabel(c, { withCode: true }),
withVariete ? c.variete : '',
withDate ? c.date_semis : '',
withVariete && c.variete ? c.variete : '',
withDate && c.date_semis ? `semis le ${c.date_semis}` : '',
withSurface && c.surface ? `${c.surface}ha` : ''
].filter(d => d).join(', '))
)
Expand Down
20 changes: 10 additions & 10 deletions src/components/Features/ExportStrategies/BaseExporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ describe('generateAutresInfos', () => {
}
]

expect(generateAutresInfos(features)).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31, 1ha ; 1.2, 01.13.41.1 Carottes, Coucou')
expect(generateAutresInfos(features)).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31, 1ha ; 1.2, 01.13.41.1 Carottes, Coucou')

// in case we display by feature, and we have
expect(generateAutresInfos(features, { initialCulture: undefined })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31, 1ha ; 1.2, 01.13.41.1 Carottes, Coucou')
expect(generateAutresInfos(features, { initialCulture: '01.13.41.1' })).toBe('1.1, Chantenay à cœur rouge, 2023-03-31, 1ha ; 1.2, Coucou')
expect(generateAutresInfos(features, { initialCulture: undefined })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31, 1ha ; 1.2, 01.13.41.1 Carottes, Coucou')
expect(generateAutresInfos(features, { initialCulture: '01.13.41.1' })).toBe('1.1, Chantenay à cœur rouge, semis le 2023-03-31, 1ha ; 1.2, Coucou')

expect(generateAutresInfos(features, { withNotes: false })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31, 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, 2023-03-31, 1ha ; 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withNotes: false, withName: false, withVariete: false })).toBe('01.13.41.1 Carottes, 2023-03-31, 1ha ; 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withNotes: false })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31, 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 2023-03-31, 1ha ; 01.13.41.1 Carottes')
expect(generateAutresInfos(features, { withNotes: false, withName: false, withVariete: false })).toBe('01.13.41.1 Carottes, semis le 2023-03-31, 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')
})
Expand Down Expand Up @@ -83,9 +83,9 @@ describe('generateAutresInfos', () => {
}
]

expect(generateAutresInfos(features)).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 1.2, 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, 2022-02-01, Coucou')
expect(generateAutresInfos(features, { withNotes: false })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 1.2, 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, 2022-02-01')
expect(generateAutresInfos(features, { withNotes: false, withName: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, 2022-02-01')
expect(generateAutresInfos(features)).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 1.2, 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, semis le 2022-02-01, Coucou')
expect(generateAutresInfos(features, { withNotes: false })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 1.2, 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, semis le 2022-02-01')
expect(generateAutresInfos(features, { withNotes: false, withName: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, semis le 2022-02-01')
})

test('with two features with a pivot on a given culture', () => {
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('generateAutresInfos', () => {
}
]

expect(generateAutresInfos(features, { pivot: '01.13.41.1' })).toBe('1.1, Chantenay à cœur rouge, 2023-03-31 / Nantaise de Grasseval, Coucou ! ; 1.2')
expect(generateAutresInfos(features, { pivot: '01.13.41.1' })).toBe('1.1, Chantenay à cœur rouge, semis le 2023-03-31 / Nantaise de Grasseval, Coucou ! ; 1.2')
expect(generateAutresInfos(features, { pivot: '01.13.41.1', withDate: false, withNotes: false })).toBe('1.1, Chantenay à cœur rouge / Nantaise de Grasseval ; 1.2')
})
})
17 changes: 9 additions & 8 deletions src/components/Features/ExportStrategies/CertipaqExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ const getSheet = ({ featureCollection, operator }) => {
const culture = fromCodeCpf(props.cultures.at(0)?.CPF)

return [
// Commune
// Commune #A
props.COMMUNE_LABEL,
// Ilot
// Ilot #B
featureName({ properties: props }, { ilotLabel: '', parcelleLabel: '', separator: '_', placeholder: '' }),
// Culture
// Culture #C
culture?.libelle_code_cpf ?? `[ERREUR] culture inconnue`,
// Variété / infos
'',
// Variété / infos #D
generateAutresInfos([{ id, geometry, properties: props }], { 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 All @@ -83,8 +83,8 @@ const getSheet = ({ featureCollection, operator }) => {
props.conversion_niveau === 'C3' ? surfaceHa : '',
// Date conv
props.engagement_date ? new Date(props.engagement_date) : '',
// Observation / date de semis
generateAutresInfos([{ id, geometry, properties: props }], { withName: false, initialCulture: culture?.code_cpf }),
// Observation / date de semis #K
generateAutresInfos([{ id, geometry, properties: props }], { withDate: true, withName: false, withNotes: false, withSurface: true, withVariete: false, initialCulture: culture?.code_cpf }),
// Précédent
'',
// Anté précédent
Expand All @@ -93,7 +93,7 @@ const getSheet = ({ featureCollection, operator }) => {
'',
// Date
'',
// ParcelleId
// ParcelleId #P
String(id),
]
}), { origin: 'A6', cellDates: true })
Expand Down Expand Up @@ -159,6 +159,7 @@ class CertipaqExporter extends BaseExporter {
label = "Tableur"
extension = "csv"
mimetype = "text/csv"
range = "A5:P999"

getSheet() {
return getSheet({ featureCollection: this.featureCollection, operator: this.operator } )
Expand Down
126 changes: 126 additions & 0 deletions src/components/Features/ExportStrategies/CertipaqExporter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { describe, test, expect } from 'vitest'
import Exporter from './CertipaqExporter.js'
import record from '@/components/Features/__fixtures__/record-for-exports.json' assert { type: 'json' }

describe('CertipaqExporter', () => {
test('list by features', () => {
const exporter = new Exporter({
featureCollection: record.parcelles,
operator: record.operator,
record: record
})

const expectation = [
[
"Commune",
"Ilot",
"Culture",
"Variété / infos",
"C0",
"AB",
"C1",
"C2",
"C3",
"Date conv",
"Observation / date de semis",
"Précédent",
"Anté précédent",
"Produit",
"Date",
"Id. CartoBio"
],
[
'',
'',
'Luzerne',
'',
'',
'',
'1,05',
'',
'',
new Date('2023-01-01T00:00:00.000Z'),
'',
'',
'',
'',
'',
'1'
],
[
'',
'',
'Luzerne',
'',
'',
'',
'1,05',
'',
'',
new Date('2023-01-01T00:00:00.000Z'),
'',
'',
'',
'',
'',
'2'
],
[
'',
'',
'Luzerne',
' / 01.19.10.7 Trèfle, 4 feuilles',
'',
'1,05',
'',
'',
'',
new Date('2021-01-01T00:00:00.000Z'),
' / 01.19.10.7 Trèfle, semis le 2023-03-01',
'',
'',
'',
'',
'3'
],
[
'',
'',
'Trèfle',
'4 feuilles',
'',
'1,05',
'',
'',
'',
new Date('2015-01-01T00:00:00.000Z'),
'semis le 2023-03-01',
'',
'',
'',
'',
'4'
],
[
'',
'',
'[ERREUR] culture inconnue',
'01.19.99 Culture inconnue',
'',
'',
'',
'',
'',
'',
'01.19.99 Culture inconnue',
'',
'',
'',
'',
'5'
]
]

expect(exporter.toJSON()).toEqual(expectation)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ describe('CertisudExporter', () => {
'Trèfle',
// expect.closeTo(1.0), // in vite@5 + vitest@1
1.0464881572673355,
'4 feuilles, 2023-03-01',
'4 feuilles, semis le 2023-03-01',
'AB',
new Date('2021-01-01T00:00:00.000Z'),
],
[
'Trèfle',
// expect.closeTo(1.0), // in vite@5 + vitest@1
1.0464881572673355,
'4 feuilles, 2023-03-01',
'4 feuilles, semis le 2023-03-01',
'AB',
new Date('2015-01-01T00:00:00.000Z'),
],
Expand Down
13 changes: 12 additions & 1 deletion src/components/Features/__fixtures__/record-for-exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
"record_id": "054f0d70-c3da-448f-823e-81fcf7c2bf6e",
"operator": {
"id": 1,
"nom": "test"
"nom": "test",
"notifications": [
{
"id": 1,
"status": "ACTIVE",
"organismeCertificateur": {
"id": 999,
"nom": "CartobiOC",
"numeroControleEu": "FR-BIO-999"
}
}
]
},
"parcelles": {
"type": "FeatureCollection",
Expand Down

0 comments on commit 62bf84a

Please sign in to comment.