diff --git a/care/emr/resources/invoice/sync_items.py b/care/emr/resources/invoice/sync_items.py index 33d4f67107..c440f8200a 100644 --- a/care/emr/resources/invoice/sync_items.py +++ b/care/emr/resources/invoice/sync_items.py @@ -58,7 +58,7 @@ def sync_invoice_items(invoice: Invoice): care_method=settings.INVOICE_FINAL_AMOUNT_ROUNDING_METHOD, ) if not invoice.is_refund and (invoice.total_net < 0 or invoice.total_gross < 0): - raise ValidationError("A Refund Ivoice is required for negative values") + raise ValidationError("A Refund Invoice is required for negative values") invoice.total_price_components = json.loads( json.dumps( summary["total_price_components"], diff --git a/care/emr/tests/test_invoice_api.py b/care/emr/tests/test_invoice_api.py index 125a2e7233..d72eb25e73 100644 --- a/care/emr/tests/test_invoice_api.py +++ b/care/emr/tests/test_invoice_api.py @@ -110,6 +110,49 @@ def test_attach_account_to_invoice_attaches_all_billable_charge_items(self): self.assertEqual(self.charge_item_1.paid_invoice, invoice) self.assertEqual(self.charge_item_2.paid_invoice, invoice) + def test_attach_account_to_invoice_rejects_negative_total_without_refund(self): + role = self.create_role_with_permissions( + [ + InvoicePermissions.can_read_invoice.name, + InvoicePermissions.can_write_invoice.name, + ] + ) + self.attach_role_facility_organization_user(self.organization, self.user, role) + self.client.force_authenticate(user=self.user) + + baker.make( + "emr.ChargeItem", + facility=self.facility, + patient=self.patient, + encounter=self.encounter, + account=self.account, + status=ChargeItemStatusOptions.billable.value, + quantity=Decimal("1.00"), + unit_price_components=[{"amount": -500, "monetary_component_type": "base"}], + total_price_components=[ + {"amount": -500, "monetary_component_type": "base"} + ], + total_price=Decimal("-500.00"), + ) + + invoice = baker.make( + "emr.Invoice", + facility=self.facility, + account=self.account, + patient=self.patient, + status=InvoiceStatusOptions.draft.value, + is_refund=False, + total_net=Decimal("0.00"), + total_gross=Decimal("0.00"), + ) + + response = self.client.post(self._get_url(invoice.external_id)) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual( + response.json()["errors"][0]["msg"], + "A Refund Invoice is required for negative values", + ) + def test_attach_account_to_invoice_requires_draft_status(self): role = self.create_role_with_permissions( [