feat(manager): expenses, the supplier book and tabs on v2 (platform-v2 step 6.13a)
- Finance -> Expenses: still owed, late, filters, new/edit (supplier picked or added there), pay in parts, details with payments, delete - CRM -> Contacts: the supplier book with what each one is owed - Finance -> Tabs: open (who owes most first) / settled, charges and payments, pay (cash/card/other), close, forgive - backend: expenses.manage / tabs.manage (the list also reports.view, a customer's tabs also crm.manage, put-on-tab needs tabs.manage); payment <= due (expense.over_due), total >= paid (expense.below_paid), categories/kinds/methods checked, null clears a field; live events expenses.changed / tabs.changed; GET /api/expenses ?from=&to= venue dates (the report uses it); tab_status=settled - fix: an expense with a due date failed to save (naive date refused by D-11) - @xenia/i18n/number subpath; tests: test_finance.py (6), finance.test.mjs (5), e2e manager.mjs finance flow Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
// Expenses, the supplier book and tabs as plain data (plan step 6.13a).
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import {
|
||||
contactBody, contactForm, contactProblems, daysBetween, expenseBody, expenseForm, expenseProblems, expenseTotals,
|
||||
filterContacts, filterExpenses, filterTabs, isLate, owedByContact, paymentAmount, tabActions, tabTotals,
|
||||
} from '../src/pages/finance/financeLogic.js'
|
||||
|
||||
const key = iso => iso.slice(0, 10)
|
||||
const expenses = [
|
||||
{ id: 1, description: 'Γάλα Νοεμβρίου', category: 'supplier', contact_id: 7, contact_name: 'Dairy Nikos', total_amount: 120, paid_amount: 70, due_amount: 50, status: 'partial', due_date: '2026-11-18T22:00:00Z' },
|
||||
{ id: 2, description: 'Rent', category: 'rent', contact_id: null, total_amount: 800, paid_amount: 800, due_amount: 0, status: 'paid', due_date: '2026-11-01T00:00:00Z' },
|
||||
{ id: 3, description: 'Feta', category: 'supplier', contact_id: 7, contact_name: 'Dairy Nikos', total_amount: 40, paid_amount: 0, due_amount: 40, status: 'due', due_date: null, notes: 'two tins' },
|
||||
]
|
||||
|
||||
test('an expense form: typed amounts with a comma, the due date as a venue date, cleared fields as null', () => {
|
||||
const f = expenseForm(expenses[0], key)
|
||||
assert.equal(f.due_date, '2026-11-18')
|
||||
assert.equal(f.contact_id, '7')
|
||||
assert.deepEqual(expenseProblems({ ...f, total_amount: '120,50' }, 70), [])
|
||||
assert.deepEqual(expenseProblems({ ...f, description: ' ', total_amount: '0' }), ['description', 'total_amount'])
|
||||
assert.deepEqual(expenseProblems({ ...f, total_amount: '60' }, 70), ['below_paid'])
|
||||
assert.deepEqual(expenseBody({ ...f, total_amount: '120,50', contact_id: '', due_date: '', notes: ' ' }),
|
||||
{ description: 'Γάλα Νοεμβρίου', category: 'supplier', contact_id: null, total_amount: 120.5, due_date: null, notes: null })
|
||||
assert.equal(expenseForm(null).category, 'supplier')
|
||||
})
|
||||
|
||||
test('a payment is above zero and no more than what is due', () => {
|
||||
assert.equal(paymentAmount('50', 50), 50)
|
||||
assert.equal(paymentAmount('20,25', 50), 20.25)
|
||||
assert.equal(paymentAmount('50,01', 50), null)
|
||||
assert.equal(paymentAmount('0', 50), null)
|
||||
assert.equal(paymentAmount('abc', 50), null)
|
||||
})
|
||||
|
||||
test('filters, totals, late ones and what each supplier is owed', () => {
|
||||
assert.deepEqual(filterExpenses(expenses, { status: 'unpaid' }).map(e => e.id), [1, 3])
|
||||
assert.deepEqual(filterExpenses(expenses, { category: 'rent' }).map(e => e.id), [2])
|
||||
assert.deepEqual(filterExpenses(expenses, { q: 'nikos tins' }).map(e => e.id), [3])
|
||||
assert.deepEqual(filterExpenses(expenses, { q: 'γάλα' }).map(e => e.id), [1])
|
||||
assert.equal(isLate(expenses[0], '2026-11-19', key), true)
|
||||
assert.equal(isLate(expenses[1], '2026-11-19', key), false) // paid
|
||||
assert.deepEqual(expenseTotals(expenses, '2026-11-19', key), { total: 960, paid: 870, due: 90, unpaid: 2, late: 1 })
|
||||
assert.deepEqual(owedByContact(expenses), { 7: { due: 90, count: 2 } })
|
||||
})
|
||||
|
||||
test('a contact: name needed, a wrong e-mail caught, empty fields cleared', () => {
|
||||
const f = contactForm(null)
|
||||
assert.deepEqual(contactProblems(f), ['name'])
|
||||
assert.deepEqual(contactProblems({ ...f, name: 'A', email: 'nope' }), ['email'])
|
||||
assert.deepEqual(contactBody({ ...f, name: ' Dairy ', phone: ' ' }), { name: 'Dairy', type: 'supplier', phone: null, email: null, address: null, notes: null })
|
||||
const list = [{ id: 1, name: 'Dairy Nikos', type: 'supplier', phone: '2310' }, { id: 2, name: 'DEH', type: 'utility' }]
|
||||
assert.deepEqual(filterContacts(list, { type: 'utility' }).map(c => c.id), [2])
|
||||
assert.deepEqual(filterContacts(list, { q: '2310' }).map(c => c.id), [1])
|
||||
})
|
||||
|
||||
test('tabs: what can be done, days open, search and what is owed', () => {
|
||||
const owing = { status: 'open', balance: 12, entries: [{}] }
|
||||
assert.deepEqual(tabActions(owing), { pay: true, close: false, forgive: true })
|
||||
assert.deepEqual(tabActions({ ...owing, balance: 0 }), { pay: false, close: true, forgive: false })
|
||||
assert.deepEqual(tabActions({ ...owing, status: 'closed', balance: 0 }), { pay: false, close: false, forgive: false })
|
||||
assert.equal(daysBetween('2026-11-01', '2026-11-19'), 18)
|
||||
assert.equal(daysBetween('2026-11-19', '2026-11-19'), 0)
|
||||
const tabs = [{ customer_name: 'Kostas «Big K»', customer_phone: '697', balance: 12 }, { customer_name: 'Anna', balance: 3.5 }]
|
||||
assert.equal(filterTabs(tabs, 'big').length, 1)
|
||||
assert.deepEqual(tabTotals(tabs), { owed: 15.5, count: 2 })
|
||||
})
|
||||
Reference in New Issue
Block a user