Skip to content

fix: increase unit_price decimal precision from 2 to 4 (closes #66)#70

Open
marcm0de wants to merge 1 commit intolegions-developer:mainfrom
marcm0de:fix/decimal-precision-unit-price
Open

fix: increase unit_price decimal precision from 2 to 4 (closes #66)#70
marcm0de wants to merge 1 commit intolegions-developer:mainfrom
marcm0de:fix/decimal-precision-unit-price

Conversation

@marcm0de
Copy link
Copy Markdown

Summary

Fixes #66 — unit price loses decimal precision when saved to database.

Problem

The unit_price column uses NUMERIC(10,2), which truncates anything beyond 2 decimal places. Setting a unit price of 0.141 saves as 0.14.

Fix

Changed scale from 2 to 4 in the schema definition: NUMERIC(10,4).

This supports up to 4 decimal places (e.g., 0.0375 for per-unit energy costs) while keeping existing 2-decimal data unaffected.

Files Changed

  • packages/db/src/schema/invoice.tsunitPrice column scale: 2 → 4

Note

A database migration will be needed to alter the existing column for deployed instances.

…s-developer#66)

The unit_price column in invoice_items used NUMERIC(10,2) which
truncated values beyond 2 decimal places. For example, 0.141 would
be stored as 0.14 and lose the third decimal place on page reload.

Changed to NUMERIC(10,4) to support up to 4 decimal places, which
covers common use cases like per-unit costs (e.g., $0.0375/kWh).

Note: Existing data with 2 decimal places is unaffected — the
migration just adds capacity for more precision.
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 21, 2026

@marcm0de is attempting to deploy a commit to the Invoicely OSS Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 21, 2026

📝 Walkthrough

Walkthrough

Updated the invoice_items.unitPrice column in the database schema to increase the numeric scale from 2 to 4, enabling storage of unit prices with up to four decimal places instead of two, directly addressing the precision loss issue when saving invoices to the database.

Changes

Cohort / File(s) Summary
Schema Update
packages/db/src/schema/invoice.ts
Modified unitPrice column definition to increase Numeric scale from 2 to 4 (precision remains 10), preserving all other column attributes.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change—increasing unit_price decimal precision from 2 to 4—and references the closed issue #66.
Description check ✅ Passed The description provides clear context about the problem, the fix applied, affected files, and notes about required migrations, all directly related to the changeset.
Linked Issues check ✅ Passed The PR successfully addresses issue #66 by updating the schema to support 4 decimal places, resolving the precision loss when saving unit prices.
Out of Scope Changes check ✅ Passed Only the intended schema change to increase unit_price scale from 2 to 4 is present; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can enable review details to help with troubleshooting, context usage and more.

Enable the reviews.review_details setting to include review details such as the model used, the time taken for each step and more in the review comments.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/db/src/schema/invoice.ts (1)

98-98: Consider aligning value column precision for consistency.

The invoiceDetailsBillingDetails.value column still uses scale: 2. If billing details (taxes, fees, discounts) could also require higher precision, consider updating this column to scale: 4 as well. If the difference is intentional (e.g., percentages vs unit prices), no action needed.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 758cba98-7f6d-4cad-b9dc-a851f4b1470b

📥 Commits

Reviewing files that changed from the base of the PR and between c4fc5d4 and 545fdbc.

📒 Files selected for processing (1)
  • packages/db/src/schema/invoice.ts

@IntegerAlex
Copy link
Copy Markdown

@marcm0de Unit prices, in general Price should be upto 2 decimal only. Unit quantity can be upto 4 decimal or more if it is required as per business requirements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Loss of decimal precision when invoice is saved in database

2 participants