diff --git a/api/src/main/java/org/openmrs/module/billing/api/model/Payment.java b/api/src/main/java/org/openmrs/module/billing/api/model/Payment.java
index 85946031..170d7be2 100644
--- a/api/src/main/java/org/openmrs/module/billing/api/model/Payment.java
+++ b/api/src/main/java/org/openmrs/module/billing/api/model/Payment.java
@@ -28,6 +28,8 @@ public class Payment extends BaseInstanceCustomizableData
+
diff --git a/api/src/test/resources/org/openmrs/module/billing/api/include/BillTest.xml b/api/src/test/resources/org/openmrs/module/billing/api/include/BillTest.xml
index b2fb47b7..0390e006 100644
--- a/api/src/test/resources/org/openmrs/module/billing/api/include/BillTest.xml
+++ b/api/src/test/resources/org/openmrs/module/billing/api/include/BillTest.xml
@@ -43,7 +43,7 @@
creator="1" date_created="2012-01-01 00:00:00.0" voided="false"
uuid="4028814B39B565A20139B9645D7B0007"/>
@@ -59,7 +59,7 @@
creator="1" date_created="2012-02-01 00:00:00.0" voided="false"
uuid="5028814B39B565A20139B95FB3440005"/>
diff --git a/omod/src/main/java/org/openmrs/module/billing/web/rest/resource/PaymentResource.java b/omod/src/main/java/org/openmrs/module/billing/web/rest/resource/PaymentResource.java
index f8090da0..5fce2c09 100644
--- a/omod/src/main/java/org/openmrs/module/billing/web/rest/resource/PaymentResource.java
+++ b/omod/src/main/java/org/openmrs/module/billing/web/rest/resource/PaymentResource.java
@@ -13,14 +13,20 @@
*/
package org.openmrs.module.billing.web.rest.resource;
+import org.openmrs.Provider;
+import org.openmrs.api.ProviderService;
import org.openmrs.api.context.Context;
import org.openmrs.module.billing.web.base.resource.BaseRestDataResource;
import org.openmrs.module.billing.api.IBillService;
+import org.openmrs.module.billing.api.ICashPointService;
import org.openmrs.module.billing.api.IPaymentModeService;
+import org.openmrs.module.billing.api.ITimesheetService;
import org.openmrs.module.billing.api.model.Bill;
+import org.openmrs.module.billing.api.model.CashPoint;
import org.openmrs.module.billing.api.model.Payment;
import org.openmrs.module.billing.api.model.PaymentAttribute;
import org.openmrs.module.billing.api.model.PaymentMode;
+import org.openmrs.module.billing.api.model.Timesheet;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
import org.openmrs.module.webservices.rest.web.annotation.PropertySetter;
@@ -36,6 +42,7 @@
import java.math.BigDecimal;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
@@ -54,6 +61,7 @@ public DelegatingResourceDescription getRepresentationDescription(Representation
description.addProperty("attributes");
description.addProperty("amount");
description.addProperty("amountTendered");
+ description.addProperty("cashPoint", Representation.REF);
description.addProperty("dateCreated");
description.addProperty("voided");
return description;
@@ -69,6 +77,7 @@ public DelegatingResourceDescription getCreatableProperties() {
description.addProperty("attributes");
description.addProperty("amount");
description.addProperty("amountTendered");
+ description.addProperty("cashPoint");
return description;
}
@@ -86,6 +95,16 @@ public void setPaymentMode(Payment instance, String uuid) {
instance.setInstanceType(mode);
}
+ @PropertySetter("cashPoint")
+ public void setCashPoint(Payment instance, String uuid) {
+ ICashPointService service = Context.getService(ICashPointService.class);
+ CashPoint cashPoint = service.getByUuid(uuid);
+ if (cashPoint == null) {
+ throw new ObjectNotFoundException();
+ }
+ instance.setCashPoint(cashPoint);
+ }
+
@PropertySetter("attributes")
public void setPaymentAttributes(Payment instance, Set attributes) {
if (instance.getAttributes() == null) {
@@ -131,6 +150,11 @@ public Long getPaymentDate(Payment instance) {
@Override
public Payment save(Payment delegate) {
+ // Auto-load CashPoint if not explicitly provided
+ if (delegate.getCashPoint() == null) {
+ loadPaymentCashPoint(delegate);
+ }
+
IBillService service = Context.getService(IBillService.class);
Bill bill = delegate.getBill();
bill.addPayment(delegate);
@@ -139,6 +163,40 @@ public Payment save(Payment delegate) {
return delegate;
}
+ /**
+ * Loads the CashPoint for a payment from the current user's timesheet.
+ * Falls back to the bill's CashPoint if no timesheet is found.
+ */
+ private void loadPaymentCashPoint(Payment payment) {
+ Provider currentProvider = getCurrentProvider();
+ if (currentProvider != null) {
+ ITimesheetService timesheetService = Context.getService(ITimesheetService.class);
+ Timesheet timesheet = timesheetService.getCurrentTimesheet(currentProvider);
+ if (timesheet != null && timesheet.getCashPoint() != null) {
+ payment.setCashPoint(timesheet.getCashPoint());
+ return;
+ }
+ }
+ // Fallback: use the bill's CashPoint
+ Bill bill = payment.getBill();
+ if (bill != null && bill.getCashPoint() != null) {
+ payment.setCashPoint(bill.getCashPoint());
+ }
+ }
+
+ /**
+ * Gets the Provider associated with the current authenticated user.
+ */
+ private Provider getCurrentProvider() {
+ ProviderService providerService = Context.getProviderService();
+ Collection providers = providerService.getProvidersByPerson(
+ Context.getAuthenticatedUser().getPerson());
+ if (!providers.isEmpty()) {
+ return providers.iterator().next();
+ }
+ return null;
+ }
+
@Override
protected void delete(Payment delegate, String reason, RequestContext context) {
delete(delegate.getBill().getUuid(), delegate.getUuid(), reason, context);
diff --git a/omod/src/main/resources/liquibase.xml b/omod/src/main/resources/liquibase.xml
index 118d17c3..c1d6e7fb 100644
--- a/omod/src/main/resources/liquibase.xml
+++ b/omod/src/main/resources/liquibase.xml
@@ -1095,4 +1095,36 @@
baseTableName="bill_exemption_rule" baseColumnNames="voided_by"
referencedTableName="users" referencedColumnNames="user_id"/>
+
+
+ O3-5198: Add cash_point_id column to cashier_bill_payment table to track where each payment was taken
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ O3-5198: Migrate existing payments to use the cash_point_id from their associated bill
+
+
+
+
+ UPDATE cashier_bill_payment bp
+ SET cash_point_id = (SELECT b.cash_point_id FROM cashier_bill b WHERE b.bill_id = bp.bill_id)
+ WHERE bp.cash_point_id IS NULL
+
+
\ No newline at end of file