Skip to content

Commit

Permalink
Merge pull request #320 from AgenceBio/feature/sort-exports
Browse files Browse the repository at this point in the history
  • Loading branch information
thom4parisot authored Feb 19, 2024
2 parents d20ceb7 + 8da0851 commit 623fdd1
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 229 deletions.
325 changes: 196 additions & 129 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion src/components/Features/ExportStrategies/BaseExporter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { utils } from 'xlsx'
import { cultureLabel, featureName } from "../index.js"
import { cultureLabel, featureName, sortByAccessor } from "../index.js"
import { getAnnotationLabel } from '../../../referentiels/ab.js'

const { sheet_to_csv, sheet_to_json, json_to_sheet } = utils
Expand All @@ -17,6 +17,18 @@ export default class BaseExporter {
this.permissions = permissions
}

getSortedFeatures () {
return [...this.featureCollection.features].sort(
sortByAccessor((f) => {
return featureName(f, {
ilotLabel: '',
parcelleLabel: '',
separator: '.'
})
})
)
}

toJSON () {
const ws = this.getSheet()
return sheet_to_json(ws, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const { aoa_to_sheet, book_append_sheet, book_new, sheet_add_aoa } = utils
* @param {{ featureCollection: FeatureCollection, operator: {}}} params
* @returns {WorkSheet}
*/
const getSheet = ({ featureCollection, operator }) => {
function getSheet () {
const { featureCollection, operator } = this
const notification = operator.notifications?.find(({ status }) => status === 'ACTIVE') ?? {}

// First sheet
Expand Down Expand Up @@ -102,9 +103,7 @@ class BureauVeritasExporter extends BaseExporter {
extension = 'xlsx'
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

getSheet() {
return getSheet({ featureCollection: this.featureCollection, operator: this.operator })
}
getSheet = getSheet

toFileData() {
const sheet = this.getSheet()
Expand Down
13 changes: 4 additions & 9 deletions src/components/Features/ExportStrategies/CertipaqExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import BaseExporter, { generateAutresInfos } from "@/components/Features/ExportS
const { aoa_to_sheet, sheet_add_aoa } = utils
const { decode_range: R } = utils

const getSheet = ({ featureCollection, operator, permissions }) => {
function getSheet () {
const { featureCollection, operator, permissions } = this
const notification = operator.notifications?.find(({ status }) => status === 'ACTIVE') ?? {}

// First sheet
Expand Down Expand Up @@ -71,11 +72,7 @@ const getSheet = ({ featureCollection, operator, permissions }) => {
placeholder: ''
}

const sortedFeatures = [...featureCollection.features].sort((a, b) => {
return featureName(a, ilotOptions).localeCompare(b, ilotOptions)
})

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

Expand Down Expand Up @@ -176,9 +173,7 @@ class CertipaqExporter extends BaseExporter {
mimetype = "text/csv"
range = "A5:Q999"

getSheet() {
return getSheet({ featureCollection: this.featureCollection, operator: this.operator, permissions: this.permissions } )
}
getSheet = getSheet

toFileData() {
const sheet = this.getSheet()
Expand Down
20 changes: 10 additions & 10 deletions src/components/Features/ExportStrategies/CertipaqExporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,40 +115,40 @@ describe('CertipaqExporter', () => {
],
[
'',
'4_1',
'[ERREUR] culture absente',
'',
'',
'3_1',
'[ERREUR] culture inconnue (01.19.99)',
'Culture inconnue',
'',
'',
'',
'',
'',
'',
'01.19.99 Culture inconnue',
'',
'',
'',
'',
'6',
'5',
''
],
[
'',
'3_1',
'[ERREUR] culture inconnue (01.19.99)',
'Culture inconnue',
'4_1',
'[ERREUR] culture absente',
'',
'',
'',
'',
'',
'',
'01.19.99 Culture inconnue',
'',
'',
'',
'',
'5',
'',
'',
'6',
''
]
]
Expand Down
14 changes: 4 additions & 10 deletions src/components/Features/ExportStrategies/CertisExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import BaseExporter, { generateAutresInfos } from "@/components/Features/ExportS
const { aoa_to_sheet, sheet_add_aoa, book_append_sheet, book_new } = utils
const { decode_range: R } = utils

const getSheet = ({ featureCollection, operator, permissions, record }) => {
function getSheet () {
const { featureCollection, operator, permissions, record } = this
const sheet = aoa_to_sheet([
[],
[],
Expand Down Expand Up @@ -77,7 +78,7 @@ const getSheet = ({ featureCollection, operator, permissions, record }) => {
{ wch: 16 },
]

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

Expand Down Expand Up @@ -180,14 +181,7 @@ class OcaciaExporter extends BaseExporter {
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
range = "A7:Y999"

getSheet() {
return getSheet({
featureCollection: this.featureCollection,
operator: this.operator,
permissions: this.permissions,
record: this.record
})
}
getSheet = getSheet

toFileData() {
const sheet = this.getSheet()
Expand Down
20 changes: 10 additions & 10 deletions src/components/Features/ExportStrategies/CertisExporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ describe('CertisExporter', () => {
],
[
'',
'4',
'3',
'1',
1.0464881572673355,
'',
'',
'',
'[ERREUR] culture absente',
'',
'',
'[ERREUR] culture inconnue (01.19.99)',
'01.19.99 Culture inconnue',
'',
'01.19.99 Culture inconnue',
'',
'',
'',
Expand All @@ -178,20 +178,18 @@ describe('CertisExporter', () => {
'',
'',
'',
'6'
'5'
],
[
'',
'3',
'4',
'1',
1.0464881572673355,
'',
'',
'',
'[ERREUR] culture inconnue (01.19.99)',
'01.19.99 Culture inconnue',
'[ERREUR] culture absente',
'',
'01.19.99 Culture inconnue',
'',
'',
'',
Expand All @@ -205,7 +203,9 @@ describe('CertisExporter', () => {
'',
'',
'',
'5'
'',
'',
'6'
]
]

Expand Down
7 changes: 3 additions & 4 deletions src/components/Features/ExportStrategies/CertisudExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const { aoa_to_sheet, book_append_sheet, book_new, sheet_add_aoa } = utils
* @param {{ featureCollection: FeatureCollection, operator: {}}} params
* @returns {WorkSheet}
*/
const getSheet = ({ featureCollection, permissions }) => {
function getSheet () {
const { featureCollection, permissions } = this
const sheet = aoa_to_sheet([
[
'Cultures en place lors du contrôle',
Expand Down Expand Up @@ -75,9 +76,7 @@ export default class CertisudExporter extends BaseExporter {
extension = 'xlsx'
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

getSheet() {
return getSheet({ featureCollection: this.featureCollection, operator: this.operator, permissions: this.permissions })
}
getSheet = getSheet

toFileData() {
const sheet = this.getSheet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { utils, write } from "xlsx";
import { surface } from "@/components/Features/index.js"
import { fromCodeCpf } from "@agencebio/rosetta-cultures"

const getSheet = ({ featureCollection, operator, permissions }) => {
function getSheet () {
const { featureCollection, operator, permissions } = this
// First sheet
// First sheet: customer informations (via `customer`)
const sheet = utils.aoa_to_sheet([
Expand Down Expand Up @@ -50,7 +51,7 @@ const getSheet = ({ featureCollection, operator, permissions }) => {
utils.decode_range('A3:E3')
]

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

Expand Down Expand Up @@ -80,9 +81,7 @@ class ControlUnionExporter extends BaseExporter {
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
range = 'A6:H999'

getSheet() {
return getSheet({ featureCollection: this.featureCollection, operator: this.operator, permissions: this.permissions } )
}
getSheet = getSheet

toFileData() {
const sheet = this.getSheet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ describe('ControlUnionExporter', () => {
],
[
'',
'[ERREUR] culture absente',
'[ERREUR] culture inconnue (01.19.99)',
1.0464881572673355,
'',
'',
'4.1',
'6',
'3.1, 01.19.99 Culture inconnue',
'5',
''
],
[
'',
'[ERREUR] culture inconnue (01.19.99)',
'[ERREUR] culture absente',
1.0464881572673355,
'',
'',
'3.1, 01.19.99 Culture inconnue',
'5',
'4.1',
'6',
''
]
]
Expand Down
9 changes: 4 additions & 5 deletions src/components/Features/ExportStrategies/DefaultExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import BaseExporter, { generateAutresInfos } from "@/components/Features/ExportS
const { book_new, aoa_to_sheet, sheet_add_aoa, book_append_sheet } = utils
const { decode_range: R } = utils

const getSheet = ({ featureCollection, operator, permissions }) => {
function getSheet () {
const { featureCollection, operator, permissions } = this
// First sheet
// First sheet: customer informations (via `customer`)
const sheet = aoa_to_sheet([
Expand Down Expand Up @@ -38,7 +39,7 @@ const getSheet = ({ featureCollection, operator, permissions }) => {
['Identifiant CartoBio', 'N°Ilot', 'N°Parcelle', 'Surfaces graphique (ha)', 'Code culture', 'Libellé culture', 'PACAGE', 'Niveau de conversion', 'Date de conversion', 'Pac / Hors Pac / Cueillette', 'Commentaire agriculteur', 'Notes d\'audit'],
], { origin: 'A6'})

sheet_add_aoa(sheet, featureCollection.features.map(({ geometry, properties: props, id }) => {
sheet_add_aoa(sheet, this.getSortedFeatures().map(({ geometry, properties: props, id }) => {
const [ilotId, parcelleId] = [props.NUMERO_I, props.NUMERO_P]
const firstCulture = props.cultures.at(0)
const culture = firstCulture ? fromCodeCpf(firstCulture?.CPF) : { libelle_code_cpf: '[ERREUR] culture absente' }
Expand Down Expand Up @@ -77,9 +78,7 @@ class DefaultExporter extends BaseExporter {
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
range = 'A6:L999'

getSheet() {
return getSheet({ featureCollection: this.featureCollection, operator: this.operator, permissions: this.permissions })
}
getSheet = getSheet

toFileData() {
const sheet = this.getSheet()
Expand Down
16 changes: 8 additions & 8 deletions src/components/Features/ExportStrategies/DefaultExporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ describe('DefaultExporter', () => {
''
],
[
'6',
'4',
'5',
'3',
'1',
1.0464881572673355,
'',
'[ERREUR] culture absente',
'01.19.99',
'[ERREUR] culture inconnue (01.19.99)',
'',
'',
'',
Expand All @@ -106,12 +106,12 @@ describe('DefaultExporter', () => {
''
],
[
'5',
'3',
'6',
'4',
'1',
1.0464881572673355,
'01.19.99',
'[ERREUR] culture inconnue (01.19.99)',
'',
'[ERREUR] culture absente',
'',
'',
'',
Expand Down
9 changes: 4 additions & 5 deletions src/components/Features/ExportStrategies/OcaciaExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import BaseExporter, { generateAutresInfos } from "@/components/Features/ExportS
const { aoa_to_sheet, sheet_add_aoa, book_append_sheet, book_new } = utils
const { decode_range: R } = utils

const getSheet = ({ featureCollection, permissions }) => {
function getSheet () {
const { featureCollection, permissions } = this
const sheet = aoa_to_sheet([
['' , '', '', '', '', 'Surfaces en ha', '', '', '', '', '', '', '', '', 'Dernier intrant non autorisé en AB', '', '', ''],
['' , '', '', '', '', '0', '0', '0', '0', '0', '', '', '', '', '', '', '', ''],
Expand Down Expand Up @@ -43,7 +44,7 @@ const getSheet = ({ featureCollection, permissions }) => {
{ wch: 16 },
]

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

Expand Down Expand Up @@ -126,9 +127,7 @@ class OcaciaExporter extends BaseExporter {
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
range = "A3:R999"

getSheet() {
return getSheet({ featureCollection: this.featureCollection, operator: this.operator, permissions: this.permissions } )
}
getSheet = getSheet

toFileData() {
const sheet = this.getSheet()
Expand Down
Loading

0 comments on commit 623fdd1

Please sign in to comment.