O3-5174: Prevent duplicate billable services in bills #51
O3-5174: Prevent duplicate billable services in bills #51RajPrakash681 wants to merge 3 commits intoopenmrs:mainfrom
Conversation
- Added mergeDuplicateLineItems() method to automatically merge line items with same billableService UUID - Line items with identical billableService are now combined with quantities summed - Merge occurs before saving new bills and after adding items to existing pending bills - Resolves issue where same service could be added multiple times to a single bill - Maintains first item's properties (price, order) while summing quantities Fixes openmrs#123
|
@denniskigen What I did: Earlier, I had made changes in the billing app (frontend), but following your guidance, I've now implemented the fix in the correct location - the openmrs-module-billing backend module. |
…mPrice + price) - Updated mergeDuplicateLineItems() to check billableService UUID + itemPrice UUID + price amount - Allows same service with different payment methods/prices as separate line items - Only merges true duplicates (all three fields match) - Addresses Dennis's feedback on legitimate use cases (e.g., part cash, part insurance) Examples: ALLOWED: Same service with different payment methods (different itemPrice UUID) ALLOWED: Same service with different prices PREVENTED: Same service + same itemPrice + same price (true duplicate) Fixes issue O3-5174 per updated requirements
|
Is it possible to have a unique constraint for this at the database level? |
|
Thanks for the suggestion about adding a database-level unique constraint that’s a good idea! I looked into it, but decided to keep the merge logic at the application layer for a few reasons: User Experience: When users accidentally add the same service twice, the app automatically merges them instead of throwing a database error. It’s smoother and less frustrating. Complex Fields: Some fields like Different Item Types: A line item can be either a service or a stock item, which makes a single unique constraint hard to manage. Precision & Flexibility: Decimal precision and custom merge rules (like keeping the latest price) are easier to handle in code. Migration Effort: Adding a DB constraint would require cleaning up existing data and writing migration scripts. |
|
@denniskigen should i close that pr in which i made these changes in billing app? |
This changeset addresses the issue where the same service/item could be added multiple times to a bill, creating duplicate line items instead of incrementing the quantity. Changes: - Added unique index on (bill_id, service_id, item_id, price_id, price, voided) - Automatically merges existing duplicate line items by summing quantities - Uses MARK_RAN precondition for safe idempotent execution - Handles NULL values properly using COALESCE Testing: - Module builds successfully with mvn clean install - Liquibase XML syntax validated - OMOD file generated successfully Resolves duplicate billable services issue
|
@dkayiwa i have tried implementing please have a look ! |
|
Can you share an example or a screenshot of the duplicate line items that were created? |
|
@dkayiwa sir can you have a look here 😊 |
|
@dkayiwa sir any update on this pr? |
|
Any ideas about how to move this forward, @ibacher, @wikumChamith, @dkayiwa? |
As we are planning on a major release I think we could just add the unique constraint at the database level. This will cause issues if someone has duplicate entries if they try to update. However rather than we are deciding what to do I think it would be better to get handled by the maintainers of those implementations. This is what happens when a system is not well thought out at the start :) |
|
So:
|
|
@denniskigen @ibacher @dkayiwa any idea or comments now, how to proceed with this? |

Fix: Prevent duplicate bill line items
Problem
When adding the same service/item to a bill multiple times, the system was creating duplicate line items instead of incrementing the quantity on the existing line item.
Solution
Added a Liquibase migration that:
(bill_id, service_id, item_id, price_id, price, voided)Testing Performed
mvn clean installbilling-1.3.3-SNAPSHOT.omodDatabase Changes
unique_bill_line_item_idxbill_id, service_id, item_id, price_id, price, voidedMigration Behavior
cashier_bill_line_itemvoided=1)