@@ -4,6 +4,8 @@ import { ILedgerSummary } from './i-ledger-summary';
44import { LedgerItemType } from './ledger-item-type' ;
55import { IRentalSavings , RentalSingleFamily } from '../properties' ;
66import currency from '../formatters/currency' ;
7+ import { cloneDateUtc } from '../utils/data-clone-date' ;
8+ import { differenceInMonths } from 'date-fns' ;
79
810export interface ILedgerCollection {
911 getBalance ( date : Date ) : number ;
@@ -25,20 +27,27 @@ export interface ILedgerCollection {
2527 clone ( ) : ILedgerCollection ;
2628}
2729
30+ export type LedgerItemPredicate = ( x : LedgerItem , index : number ) => boolean ;
31+
2832export class LedgerCollection implements ILedgerCollection {
2933 private collection : IterableQuery < LedgerItem > ;
3034
3135 private getSummaryByType ( collection : IterableQuery < LedgerItem > , type : LedgerItemType ) : number {
3236 if ( ! collection ) {
3337 return 0 ;
3438 }
39+
3540 return collection . filter ( ( x ) => x . typeMatches ( type ) ) . sum ( ( x ) => x . amount ) || 0 ;
3641 }
3742
3843 constructor ( ) {
3944 this . collection = itiriri ( [ ] ) ;
4045 }
4146
47+ filter ( pred : LedgerItemPredicate ) : LedgerItem [ ] {
48+ return this . collection . filter ( pred ) . toArray ( ) ;
49+ }
50+
4251 getBalance ( date : Date ) : number {
4352 return this . isEmpty ( )
4453 ? 0
@@ -113,7 +122,7 @@ export class LedgerCollection implements ILedgerCollection {
113122 }
114123
115124 const result : ILedgerSummary = {
116- date : new Date ( Date . UTC ( date . getUTCFullYear ( ) , date . getUTCMonth ( ) , 1 ) ) ,
125+ date : cloneDateUtc ( date ) ,
117126 balance : 0 ,
118127 cashFlow : 0 ,
119128 averageCashFlow : 0 ,
@@ -131,14 +140,14 @@ export class LedgerCollection implements ILedgerCollection {
131140 return result ;
132141 }
133142
134- const salary = this . getSummaryByType ( boundary , LedgerItemType . Salary ) ;
135143 result . cashFlow = this . getSummaryByType ( boundary , LedgerItemType . CashFlow ) ;
136144 result . averageCashFlow = currency (
137145 boundary . filter ( ( x ) => x . type === LedgerItemType . CashFlow ) . average ( ( x ) => x . amount ) || 0
138146 ) ;
139147 result . equity = this . getSummaryByType ( boundary , LedgerItemType . Equity ) ;
140148 result . purchases = this . getSummaryByType ( boundary , LedgerItemType . Purchase ) ;
141- result . balance = result . cashFlow + salary + result . equity - result . purchases || 0 ;
149+ result . purchases = this . getSummaryByType ( boundary , LedgerItemType . Purchase ) ;
150+ result . balance = this . collection . filter ( ( li ) => li . dateNotGreaterThan ( date ) ) . sum ( ( x ) => x . amount ) || 0 ;
142151
143152 return result ;
144153 }
@@ -157,12 +166,14 @@ export class LedgerCollection implements ILedgerCollection {
157166 } ;
158167 }
159168
169+ const cashFlowSum = summaries . sum ( ( x ) => x . cashFlow || 0 ) ;
170+
160171 return {
161172 date : summaries . first ( ) . date ,
162- balance : summaries . sum ( ( x ) => x . balance || 0 ) ,
173+ balance : summaries . last ( ) . balance ,
163174 equity : summaries . sum ( ( x ) => x . equity || 0 ) ,
164- cashFlow : summaries . sum ( ( x ) => x . cashFlow || 0 ) ,
165- averageCashFlow : currency ( summaries . average ( ( x ) => x . cashFlow || 0 ) ) ,
175+ cashFlow : cashFlowSum ,
176+ averageCashFlow : currency ( cashFlowSum / summaries . length ( ) ) ,
166177 purchases : summaries . sum ( ( x ) => x . purchases || 0 ) ,
167178 } ;
168179 }
@@ -183,11 +194,19 @@ export class LedgerCollection implements ILedgerCollection {
183194 }
184195
185196 const collection = [ ] ;
186- for ( let month = boundary . first ( ) . created . getUTCMonth ( ) ; month < 12 ; month ++ ) {
187- collection . push ( this . getSummaryMonth ( new Date ( Date . UTC ( year , month , 1 ) ) ) ) ;
197+
198+ //need to determine monthDiff between boundary
199+ const totalMonths = differenceInMonths ( boundary . first ( ) . created , boundary . last ( ) . created ) ;
200+
201+ if ( totalMonths === 0 ) {
202+ collection . push ( this . getSummaryMonth ( boundary . first ( ) . created ) ) ;
203+ } else {
204+ for ( let month = boundary . first ( ) . created . getUTCMonth ( ) ; month < 12 ; month ++ ) {
205+ collection . push ( this . getSummaryMonth ( new Date ( Date . UTC ( year , month , 1 ) ) ) ) ;
206+ }
188207 }
189208
190- return collection ;
209+ return collection ; //?
191210 }
192211
193212 clone ( ) : ILedgerCollection {
0 commit comments