1616import { LogManager } from 'aurelia-framework' ;
1717import { Condition , CurrencyCode } from '../util/common-models' ;
1818import { asCurrencyValueConverter } from '../resources/value-converters/as-currency-formatted' ;
19-
19+ import { I18N } from 'aurelia-i18n' ;
2020import * as reportStyles from './report-styles.json' ;
2121
2222import _ from 'lodash' ;
@@ -25,14 +25,69 @@ const logger = LogManager.getLogger('report-helper');
2525
2626export class ReportHelper {
2727
28+ countries = [ ] ;
29+
2830 static inject ( ) {
29- return [ ReportValueConverter ] ;
31+ return [ ReportValueConverter , I18N ] ;
3032 }
3133
32- constructor ( reportValueConverter ) {
34+ constructor ( reportValueConverter , i18next ) {
3335 this . converter = reportValueConverter ;
36+ this . i18next = i18next ;
3437 }
3538
39+ buildReport ( stamps , countries , reportValue , options ) {
40+ let reportModel = _ . get ( options , 'model' , { } ) ;
41+ let title = _ . get ( reportModel , 'title' , this . i18next . tr ( 'reports.defaultTitle' ) ) ;
42+ let includeCountries = _ . get ( reportModel , 'includeCountries' , false ) ;
43+ let includeNotes = _ . get ( reportModel , 'includeNotes' , false ) ;
44+
45+ let columns = [
46+ { name : ' ' , type : 'issues' , value : 'stampOwnerships[0]' } ,
47+ { name : this . i18next . tr ( 'reports.number' ) , type : 'catalogueNumber' , value : 'activeCatalogueNumber.number' } ,
48+ { name : this . i18next . tr ( 'reports.description' ) , type : 'text' , value : 'rate' , additional : [ 'description' ] , width : '*' } ,
49+ { name : this . i18next . tr ( 'reports.condition' ) , type : 'condition' , value : 'activeCatalogueNumber.condition' } ,
50+ {
51+ name : this . i18next . tr ( 'reports.value' ) ,
52+ type : 'currencyValue' ,
53+ value : 'activeCatalogueNumber.value' ,
54+ additional : [ 'activeCatalogueNumber.code' ]
55+ }
56+ ] ;
57+ if ( includeCountries ) {
58+ columns . splice ( 1 , 0 , { name : this . i18next . tr ( 'reports.country' ) , type : 'country' , value : 'countryRef' } ) ;
59+ }
60+ if ( includeNotes ) {
61+ let index = includeCountries ? 4 : 3 ;
62+ columns . splice ( index , 0 , { name : this . i18next . tr ( 'reports.notes' ) , type : 'notes' , value : 'stampOwnerships[0]' , width : '*' } ) ;
63+ }
64+
65+ let tmodel = this . generateTableModel ( stamps , countries , {
66+ cols : columns
67+ } ) ;
68+ let styles = this . getStandardStyleDefinition ( ) ;
69+ let opts = {
70+ content : [ ]
71+ } ;
72+
73+ opts . content . push ( this . generateText ( `Report: ${ title } ` , 'header' ) ) ;
74+ opts . content . push ( this . generateText ( `Total number of stamps: ${ stamps . length } ` , 'text' ) ) ;
75+ opts . content . push ( this . generateText ( `Total value: ${ reportValue } ` , 'text' ) ) ;
76+ opts . content . push ( {
77+ table : tmodel , style : 'table' , layout : {
78+ hLineColor : ( i , node ) => {
79+ return '#aaa' ;
80+ } ,
81+ vLineColor : ( i , node ) => {
82+ return '#aaa' ;
83+ }
84+ }
85+ } ) ;
86+ opts . styles = styles ;
87+ return opts ;
88+ }
89+
90+
3691 getStandardStyleDefinition ( ) {
3792 return reportStyles ;
3893 }
@@ -44,48 +99,69 @@ export class ReportHelper {
4499 } ;
45100 }
46101
47- generateTableModel ( stamps , config ) {
102+ generateTableModel ( stamps , countries , config ) {
48103 let model = {
49- body : [ ]
104+ body : [ ] ,
105+ widths : [ ]
50106 } ;
51107 if ( ! _ . isEmpty ( config . cols ) ) {
52108 let tr = [ ] ;
109+
53110 _ . forEach ( config . cols , col => {
54111 tr . push ( { text : col . name || col . type , style : 'tableHeader' } ) ;
112+ model . widths . push ( col . width || 'auto' ) ;
55113 } ) ;
56114 model . headerRows = 1 ;
57- model . widths = [ 'auto' , '*' , 'auto' , 'auto' ] ;
58115 model . body . push ( tr ) ;
59- }
60116
61- _ . forEach ( stamps , stamp => {
62- let row = [ ] ;
63- _ . forEach ( config . cols , col => {
64- let val = _ . get ( stamp , col . value ) ;
65- switch ( col . type ) {
66- case 'catalogueNumber' :
67- row . push ( val ) ;
68- break ;
69- case 'condition' :
70- row . push ( this . converter . fromCondition ( val ) ) ;
71- break ;
72- case 'currencyValue' :
73- let currencyCode = _ . get ( stamp , col . code , 'EUR' ) ;
74- let v = this . converter . fromCurrencyValue ( val , currencyCode ) ;
75- row . push ( v ) ;
76- break ;
77- case 'text' :
78- _ . forEach ( col . additional , a => {
79- val += ' ' + _ . get ( stamp , a , '' ) ;
80- } ) ;
81- row . push ( val ) ;
82- break ;
83- }
117+ _ . forEach ( stamps , stamp => {
118+ let row = [ ] ;
119+ _ . forEach ( config . cols , col => {
120+ row . push ( this . generateTableCellValue ( stamp , col , countries ) ) ;
121+ } ) ;
122+ model . body . push ( row ) ;
84123 } ) ;
85- model . body . push ( row ) ;
86- } ) ;
124+ }
87125 return model ;
88126 }
127+
128+ generateTableCellValue ( stamp , col , countries ) {
129+ let val = _ . get ( stamp , col . value ) ;
130+ let result = '' ;
131+ switch ( col . type ) {
132+ case 'catalogueNumber' :
133+ result = val ;
134+ break ;
135+ case 'condition' :
136+ result = this . converter . fromCondition ( val ) ;
137+ break ;
138+ case 'currencyValue' :
139+ let currencyCode = _ . get ( stamp , col . code , CurrencyCode . USD . key ) ;
140+ result = this . converter . fromCurrencyValue ( val , currencyCode ) ;
141+ break ;
142+ case 'country' :
143+ let c = _ . find ( countries , { id : val } ) ;
144+ result = c ? c . name : '' ;
145+ break ;
146+ case 'issues' :
147+ if ( val && val . deception > 0 ) {
148+ result = '\u0394' ;
149+ } else if ( val && val . defects > 0 ) {
150+ result = '\u00D7' ;
151+ }
152+ break ;
153+ case 'notes' :
154+ result = ( val && val . notes ) ? val . notes : '' ;
155+ break ;
156+ case 'text' :
157+ _ . forEach ( col . additional , a => {
158+ val += ' ' + _ . get ( stamp , a , '' ) ;
159+ } ) ;
160+ result = val ;
161+ break ;
162+ }
163+ return result ;
164+ }
89165}
90166
91167export class ReportValueConverter {
@@ -108,7 +184,7 @@ export class ReportValueConverter {
108184 let value = '' ;
109185 switch ( condition ) {
110186 case Condition . MINT . ordinal :
111- case Condition . MINT_HH :
187+ case Condition . MINT_HH . ordinal :
112188 value = '*' ;
113189 break ;
114190 case Condition . MINT_NH . ordinal :
@@ -119,7 +195,7 @@ export class ReportValueConverter {
119195 break ;
120196 case Condition . USED . ordinal :
121197 case Condition . CTO . ordinal :
122- value = 'u' ;
198+ value = 'u' ; // '\u00f8';
123199 break ;
124200 case Condition . MANUSCRIPT . ordinal :
125201 value = '~' ;
0 commit comments