1
1
import { Injectable } from '@nestjs/common' ;
2
- import { Nil } from '@seedcompany/ common' ;
2
+ import { oneLine } from 'common-tags ' ;
3
3
import { clamp , round } from 'lodash' ;
4
- import { CalendarDate , fiscalQuarter , fiscalYear } from '~/common' ;
4
+ import {
5
+ CalendarDate ,
6
+ fiscalQuarter ,
7
+ fiscalQuarterLabel ,
8
+ fiscalYear ,
9
+ } from '~/common' ;
5
10
import { Column , Row } from '~/common/xlsx.util' ;
6
11
import { Downloadable } from '../file/dto' ;
7
12
import { Pnp , ProgressSheet } from '../pnp' ;
@@ -18,26 +23,33 @@ export class ProgressSummaryExtractor {
18
23
const pnp = await Pnp . fromDownloadable ( file ) ;
19
24
const sheet = pnp . progress ;
20
25
26
+ if ( ! ( sheet . reportingQuarter && date <= sheet . reportingQuarter . end ) ) {
27
+ const cells = sheet . reportingQuarterCells ;
28
+ result . addProblem ( {
29
+ severity : 'Error' ,
30
+ groups : 'Mismatched Reporting Quarter' ,
31
+ message : oneLine `
32
+ The PnP's Reporting Quarter
33
+ (_${
34
+ sheet . reportingQuarter
35
+ ? fiscalQuarterLabel ( sheet . reportingQuarter . start )
36
+ : 'undetermined'
37
+ } _ \`${ cells . quarter . ref } \`/\`${ cells . year . ref } \`)
38
+ needs to be *at least* the quarter of this CORD report
39
+ (_${ fiscalQuarterLabel ( date ) } _).
40
+ ` ,
41
+ source : cells . quarter ,
42
+ } ) ;
43
+ return { } ;
44
+ }
45
+
21
46
const currentFiscalYear = fiscalYear ( date ) ;
22
47
const yearRow = findFiscalYearRow ( sheet , currentFiscalYear ) ;
23
48
const quarterCol = sheet . columnForQuarterSummary ( fiscalQuarter ( date ) ) ;
24
49
return {
25
- reportPeriod : summaryFrom (
26
- yearRow ,
27
- quarterCol ,
28
- quarterCol ,
29
- result ,
30
- 'Quarterly' ,
31
- currentFiscalYear ,
32
- ) ,
33
- fiscalYear : summaryFrom (
34
- yearRow ,
35
- ...sheet . columnsForFiscalYear ,
36
- result ,
37
- 'Yearly' ,
38
- currentFiscalYear ,
39
- ) ,
40
- cumulative : findLatestCumulative ( yearRow , result , currentFiscalYear ) ,
50
+ reportPeriod : summaryFrom ( yearRow , quarterCol , quarterCol ) ,
51
+ fiscalYear : summaryFrom ( yearRow , ...sheet . columnsForFiscalYear ) ,
52
+ cumulative : findLatestCumulative ( yearRow ) ,
41
53
} ;
42
54
}
43
55
}
@@ -51,21 +63,11 @@ const findFiscalYearRow = (sheet: ProgressSheet, fiscalYear: number) => {
51
63
throw new Error ( 'Unable to find fiscal year in pnp file' ) ;
52
64
} ;
53
65
54
- const findLatestCumulative = (
55
- currentYear : Row < ProgressSheet > ,
56
- result : PnpProgressExtractionResult ,
57
- currentFiscalYear : number ,
58
- ) => {
66
+ const findLatestCumulative = ( currentYear : Row < ProgressSheet > ) => {
59
67
const { sheet } = currentYear ;
60
68
// eslint-disable-next-line no-constant-condition
61
69
while ( true ) {
62
- const summary = summaryFrom (
63
- currentYear ,
64
- ...sheet . columnsForCumulative ,
65
- result ,
66
- 'Cumulative' ,
67
- currentFiscalYear ,
68
- ) ;
70
+ const summary = summaryFrom ( currentYear , ...sheet . columnsForCumulative ) ;
69
71
if ( summary ) {
70
72
return summary ;
71
73
}
@@ -80,41 +82,10 @@ const summaryFrom = (
80
82
fiscalYear : Row < ProgressSheet > ,
81
83
plannedColumn : Column ,
82
84
actualColumn : Column ,
83
- result : PnpProgressExtractionResult ,
84
- period : string ,
85
- currentFiscalYear : number ,
86
- ) : Progress | null => {
87
- const plannedCell = fiscalYear . cell ( plannedColumn ) ;
88
- const actualCell = fiscalYear . cell ( actualColumn ) ;
89
- let planned = plannedCell . asNumber ;
90
- let actual = actualCell . asNumber ;
91
- if ( ! planned && + plannedColumn !== + actualColumn ) {
92
- result . addProblem ( {
93
- severity : 'Error' ,
94
- groups : [
95
- 'Missing progress summary percents' ,
96
- `The _${ period } _ summary percents for _FY${ currentFiscalYear } _ is missing` ,
97
- ] ,
98
- message : `The _${ period } Planned_ percent for _FY${ currentFiscalYear } _ \`${ plannedCell . ref } \` is missing` ,
99
- source : plannedCell ,
100
- } ) ;
101
- }
102
- if ( ! actual ) {
103
- result . addProblem ( {
104
- severity : 'Error' ,
105
- groups : [
106
- 'Missing progress summary percents' ,
107
- `The _${ period } _ summary percents for _FY${ currentFiscalYear } _ is missing` ,
108
- ] ,
109
- message : `The _${ period } Actual_ percent for _FY${ currentFiscalYear } _ \`${ actualCell . ref } \` is missing` ,
110
- source : actualCell ,
111
- } ) ;
112
- }
113
- if ( ! planned && ! actual ) {
114
- return null ;
115
- }
116
- const normalize = ( val : number | Nil ) => {
117
- if ( ! val ) return 0 ;
85
+ ) : Progress => {
86
+ let planned = fiscalYear . cell ( plannedColumn ) . asNumber ?? 0 ;
87
+ let actual = fiscalYear . cell ( actualColumn ) . asNumber ?? 0 ;
88
+ const normalize = ( val : number ) => {
118
89
if ( round ( val , 4 ) > 1 ) {
119
90
// The PnP has the macro option to do calculations as percents or verse equivalents.
120
91
// We need to standardize as percents here.
0 commit comments