const { settleLine, applyPendingResolutions } = require('../src/utils/cashupUtils'); describe('settleLine', () => { test('card settled net of fees, with the fees written off, leaves nothing unaccounted', () => { const s = settleLine({ method: 'card', expected: 12450, input: { bankAmount: '12080.40', writeOffs: [{ category: 'card_fees', amount: '369.60', reason: 'Yoco 2.95%' }] }, }); expect(s.actual).toBe(12080.4); expect(s.writeOffTotal).toBe(369.6); expect(s.unaccounted).toBe(0); expect(s.variance).toBe(-369.6); expect(s.writeOffs).toEqual([{ category: 'card_fees', amount: 369.6, reason: 'Yoco 2.95%' }]); }); test('EFT with a POP not yet in the bank counts the pending money as accounted for', () => { const s = settleLine({ method: 'eft', expected: 8200, input: { bankAmount: 7200, pendingAmount: 1000, pendingReference: 'POP #417' } }); expect(s.actual).toBe(8200); expect(s.pendingAmount).toBe(1000); expect(s.pendingReference).toBe('POP #417'); expect(s.unaccounted).toBe(0); }); test('a pending reference is dropped when nothing is pending', () => { const s = settleLine({ method: 'eft', expected: 100, input: { bankAmount: 100, pendingReference: 'POP' } }); expect(s.pendingReference).toBeNull(); }); test('counted cash that was not deposited is on hand, and a shortfall shows as unaccounted', () => { const s = settleLine({ method: 'cash', expected: 3000, counted: 2950, input: { bankAmount: 2000, onHandAmount: 999 } }); expect(s.onHandAmount).toBe(950); // derived from the count, not the typed 999 expect(s.actual).toBe(2950); expect(s.unaccounted).toBe(50); }); test('a cash shortfall written off leaves nothing unaccounted', () => { const s = settleLine({ method: 'cash', expected: 3000, counted: 2950, input: { bankAmount: 2950, writeOffs: [{ category: 'cash_shortfall', amount: 50 }] } }); expect(s.onHandAmount).toBe(0); expect(s.unaccounted).toBe(0); }); test('a deposit fee comes out of the counted cash, not on top of it', () => { // Counted R1000; the bank credited R941.50 after a R58.50 deposit fee; nothing left on hand. const s = settleLine({ method: 'cash', expected: 1000, counted: 1000, input: { bankAmount: 941.5, writeOffs: [{ category: 'bank_charges', amount: 58.5 }] } }); expect(s.onHandAmount).toBe(0); expect(s.actual).toBe(941.5); expect(s.unaccounted).toBe(0); }); test('a cash shortfall write-off does not change what is on hand', () => { const s = settleLine({ method: 'cash', expected: 1000, counted: 980, input: { bankAmount: 480, writeOffs: [{ category: 'cash_shortfall', amount: 20 }] } }); expect(s.onHandAmount).toBe(500); expect(s.unaccounted).toBe(0); }); test('banking more than was counted, counting the deposit fee, is rejected', () => { expect(() => settleLine({ method: 'cash', expected: 1000, counted: 1000, input: { bankAmount: 1000, writeOffs: [{ category: 'bank_charges', amount: 58.5 }] } })).toThrow('More cash was deposited'); }); test('cash never has a pending amount', () => { const s = settleLine({ method: 'cash', expected: 500, input: { onHandAmount: 500, pendingAmount: 200 } }); expect(s.pendingAmount).toBeNull(); expect(s.actual).toBe(500); }); test('without a count, cash on hand can be typed in', () => { const s = settleLine({ method: 'cash', expected: 500, counted: null, input: { bankAmount: 300, onHandAmount: 200 } }); expect(s.actual).toBe(500); expect(s.unaccounted).toBe(0); }); test('depositing more cash than was counted is rejected', () => { expect(() => settleLine({ method: 'cash', expected: 500, counted: 400, input: { bankAmount: 450 } })).toThrow('More cash was deposited'); }); test('negative or non-numeric amounts are rejected', () => { expect(() => settleLine({ method: 'card', expected: 100, input: { bankAmount: -5 } })).toThrow('In bank'); expect(() => settleLine({ method: 'card', expected: 100, input: { pendingAmount: 'abc' } })).toThrow('Pending'); }); test('a write-off needs a known category and an amount over zero', () => { expect(() => settleLine({ method: 'card', expected: 100, input: { writeOffs: [{ category: 'lunch', amount: 5 }] } })).toThrow('what each write-off is for'); expect(() => settleLine({ method: 'card', expected: 100, input: { writeOffs: [{ category: 'other', amount: 0 }] } })).toThrow('more than R0'); }); test('blank write-off rows are ignored', () => { const s = settleLine({ method: 'card', expected: 100, input: { bankAmount: 100, writeOffs: [{ category: 'other', amount: '' }] } }); expect(s.writeOffs).toEqual([]); }); test('nothing entered leaves the method unreconciled', () => { const s = settleLine({ method: 'eft', expected: 100, input: {} }); expect(s.actual).toBeNull(); expect(s.variance).toBeNull(); expect(s.unaccounted).toBeNull(); }); test('amounts are rounded to cents', () => { const s = settleLine({ method: 'card', expected: 0.3, input: { bankAmount: 0.1, pendingAmount: 0.2 } }); expect(s.actual).toBe(0.3); expect(s.unaccounted).toBe(0); }); }); describe('applyPendingResolutions', () => { const line = { expectedAmount: 8200, actualAmount: 8200, bankAmount: 7200, onHandAmount: null, pendingAmount: 1000, pendingReference: 'POP #417', writeOffTotal: 0, writeOffs: [], }; test('with nothing settled yet, all the pending money is outstanding', () => { const s = applyPendingResolutions({ ...line, pendingResolutions: [] }); expect(s.pendingOutstanding).toBe(1000); expect(s.bank).toBe(7200); expect(s.unaccounted).toBe(0); }); test('money received later moves into the bank without changing actual', () => { const s = applyPendingResolutions({ ...line, pendingResolutions: [{ id: 'r1', outcome: 'received', amount: 600 }] }); expect(s.bank).toBe(7800); expect(s.actual).toBe(8200); expect(s.pendingOutstanding).toBe(400); expect(s.writeOffTotal).toBe(0); }); test('money written off later lowers actual and becomes a write-off', () => { const s = applyPendingResolutions({ ...line, pendingResolutions: [ { id: 'r1', outcome: 'received', amount: 600 }, { id: 'r2', outcome: 'written_off', amount: 400, category: 'other', notes: 'Never paid' }, ], }); expect(s.actual).toBe(7800); expect(s.writeOffTotal).toBe(400); expect(s.pendingOutstanding).toBe(0); expect(s.unaccounted).toBe(0); expect(s.writeOffs).toEqual([expect.objectContaining({ id: 'r2', category: 'other', amount: 400, reason: 'Never paid', afterClose: true })]); }); test('a line from before settlement existed reports its old variance as unaccounted', () => { const s = applyPendingResolutions({ expectedAmount: 1000, actualAmount: 980, variance: -20 }); expect(s.bank).toBeNull(); expect(s.pendingOutstanding).toBe(0); expect(s.unaccounted).toBe(20); }); });