feat: add drawer cash in/out adjustments with balance reconciliation

- New drawer_adjustment table (type: cash_in/cash_out, amount, reason)
- POST/GET /drawer/:id/adjustments endpoints
- Drawer close calculation now includes adjustments: expected = opening + sales + cash_in - cash_out
- DrawerAdjustmentSchema for input validation
- 5 new tests (44 total POS tests passing)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
ryan
2026-04-04 20:24:55 +00:00
parent 24ddb17ca8
commit 3ed2707a66
8 changed files with 180 additions and 6 deletions

View File

@@ -1,8 +1,8 @@
import { eq, and, count, sum, type Column } from 'drizzle-orm'
import { eq, and, count, sum, sql, type Column } from 'drizzle-orm'
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import { drawerSessions, transactions } from '../db/schema/pos.js'
import { drawerSessions, drawerAdjustments, transactions } from '../db/schema/pos.js'
import { ConflictError, NotFoundError } from '../lib/errors.js'
import type { DrawerOpenInput, DrawerCloseInput, PaginationInput } from '@lunarfront/shared/schemas'
import type { DrawerOpenInput, DrawerCloseInput, DrawerAdjustmentInput, PaginationInput } from '@lunarfront/shared/schemas'
import { withPagination, withSort, paginatedResponse } from '../utils/pagination.js'
export const DrawerService = {
@@ -47,9 +47,20 @@ export const DrawerService = {
)
)
const cashIn = parseFloat(cashTotals?.total ?? '0') + parseFloat(cashTotals?.rounding ?? '0')
// Calculate net drawer adjustments (cash_in adds, cash_out subtracts)
const [adjTotals] = await db
.select({
cashIn: sql<string>`coalesce(sum(case when ${drawerAdjustments.type} = 'cash_in' then ${drawerAdjustments.amount} else 0 end), 0)`,
cashOut: sql<string>`coalesce(sum(case when ${drawerAdjustments.type} = 'cash_out' then ${drawerAdjustments.amount} else 0 end), 0)`,
})
.from(drawerAdjustments)
.where(eq(drawerAdjustments.drawerSessionId, sessionId))
const salesCashIn = parseFloat(cashTotals?.total ?? '0') + parseFloat(cashTotals?.rounding ?? '0')
const adjCashIn = parseFloat(adjTotals?.cashIn ?? '0')
const adjCashOut = parseFloat(adjTotals?.cashOut ?? '0')
const openingBalance = parseFloat(session.openingBalance)
const expectedBalance = openingBalance + cashIn
const expectedBalance = openingBalance + salesCashIn + adjCashIn - adjCashOut
const closingBalance = input.closingBalance
const overShort = closingBalance - expectedBalance
@@ -93,6 +104,31 @@ export const DrawerService = {
return session ?? null
},
async addAdjustment(db: PostgresJsDatabase<any>, sessionId: string, input: DrawerAdjustmentInput, createdBy: string, approvedBy?: string) {
const session = await this.getById(db, sessionId)
if (!session) throw new NotFoundError('Drawer session')
if (session.status === 'closed') throw new ConflictError('Cannot adjust a closed drawer')
const [adjustment] = await db
.insert(drawerAdjustments)
.values({
drawerSessionId: sessionId,
type: input.type,
amount: input.amount.toString(),
reason: input.reason,
createdBy,
})
.returning()
return adjustment
},
async getAdjustments(db: PostgresJsDatabase<any>, sessionId: string) {
return db
.select()
.from(drawerAdjustments)
.where(eq(drawerAdjustments.drawerSessionId, sessionId))
},
async list(db: PostgresJsDatabase<any>, params: PaginationInput) {
const sortableColumns: Record<string, Column> = {
opened_at: drawerSessions.openedAt,