Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class Payment extends BaseInstanceCustomizableData<PaymentMode, PaymentAt

private Bill bill;

private CashPoint cashPoint;

private BigDecimal amount;

private BigDecimal amountTendered;
Expand Down Expand Up @@ -80,4 +82,12 @@ public Bill getBill() {
public void setBill(Bill bill) {
this.bill = bill;
}

public CashPoint getCashPoint() {
return cashPoint;
}

public void setCashPoint(CashPoint cashPoint) {
this.cashPoint = cashPoint;
}
}
1 change: 1 addition & 0 deletions api/src/main/resources/Bill.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@

<property name="amount" type="java.math.BigDecimal" column="amount" not-null="true"/>
<property name="amountTendered" type="java.math.BigDecimal" column="amount_tendered" not-null="true"/>
<many-to-one name="cashPoint" class="org.openmrs.module.billing.api.model.CashPoint" column="cash_point_id" not-null="false"/>

<set name="attributes" lazy="false" inverse="true" cascade="all-delete-orphan">
<key column="bill_payment_id"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
creator="1" date_created="2012-01-01 00:00:00.0" voided="false"
uuid="4028814B39B565A20139B9645D7B0007"/>
<cashier_bill_payment bill_payment_id="0" bill_id="0" payment_mode_id="0" amount="913.06" amount_tendered="1000.00"
creator="1" date_created="2012-01-01 00:00:00.0" voided="false"
cash_point_id="0" creator="1" date_created="2012-01-01 00:00:00.0" voided="false"
uuid="4028814B39B565A20139B9674C510008"/>
<cashier_bill_payment_attribute bill_payment_attribute_id="0" bill_payment_id="0" payment_mode_attribute_type_id="0"
value_reference="test 1 value" uuid="4028814B39B565A20139B96998DF0009"/>
Expand All @@ -59,7 +59,7 @@
creator="1" date_created="2012-02-01 00:00:00.0" voided="false"
uuid="5028814B39B565A20139B95FB3440005"/>
<cashier_bill_payment bill_payment_id="1" bill_id="1" payment_mode_id="0" amount="300.00" amount_tendered="300.00"
creator="1" date_created="2012-02-01 00:00:00.0" voided="false"
cash_point_id="0" creator="1" date_created="2012-02-01 00:00:00.0" voided="false"
uuid="5028814B39B565A20139B9674C510008"/>

<cashier_bill bill_id="2" receipt_number="test 3 receipt number" provider_id="0" patient_id="2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
creator="1" date_created="2012-01-01 00:00:00.0" voided="false"
uuid="4028814B39B565A20139B9645D7B0007"/>
<cashier_bill_payment bill_payment_id="0" bill_id="0" payment_mode_id="0" amount="913.06" amount_tendered="1000.00"
creator="1" date_created="2012-01-01 00:00:00.0" voided="false"
cash_point_id="0" creator="1" date_created="2012-01-01 00:00:00.0" voided="false"
uuid="4028814B39B565A20139B9674C510008"/>
<cashier_bill_payment_attribute bill_payment_attribute_id="0" bill_payment_id="0" payment_mode_attribute_type_id="0"
value_reference="test 1 value" uuid="4028814B39B565A20139B96998DF0009"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +42,7 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -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;
Expand All @@ -69,6 +77,7 @@ public DelegatingResourceDescription getCreatableProperties() {
description.addProperty("attributes");
description.addProperty("amount");
description.addProperty("amountTendered");
description.addProperty("cashPoint");

return description;
}
Expand All @@ -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<PaymentAttribute> attributes) {
if (instance.getAttributes() == null) {
Expand Down Expand Up @@ -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);
Expand All @@ -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<Provider> 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);
Expand Down
32 changes: 32 additions & 0 deletions omod/src/main/resources/liquibase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1095,4 +1095,36 @@
baseTableName="bill_exemption_rule" baseColumnNames="voided_by"
referencedTableName="users" referencedColumnNames="user_id"/>
</changeSet>

<changeSet id="openmrs.billing-004-20251206-add-cashpoint-to-payment" author="RajPrakash681">
<comment>O3-5198: Add cash_point_id column to cashier_bill_payment table to track where each payment was taken</comment>
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="cashier_bill_payment" columnName="cash_point_id"/>
</not>
</preConditions>
<addColumn tableName="cashier_bill_payment">
<column name="cash_point_id" type="int">
<constraints nullable="true"/>
</column>
</addColumn>
<addForeignKeyConstraint constraintName="cashier_bill_payment_cash_point_id_fk"
baseTableName="cashier_bill_payment" baseColumnNames="cash_point_id"
referencedTableName="cashier_cash_point" referencedColumnNames="cash_point_id"/>
<createIndex tableName="cashier_bill_payment" indexName="cashier_bill_payment_cash_point_id_idx">
<column name="cash_point_id"/>
</createIndex>
</changeSet>

<changeSet id="openmrs.billing-005-20251206-migrate-existing-payment-cashpoints" author="RajPrakash681">
<comment>O3-5198: Migrate existing payments to use the cash_point_id from their associated bill</comment>
<preConditions onFail="MARK_RAN">
<columnExists tableName="cashier_bill_payment" columnName="cash_point_id"/>
</preConditions>
<sql>
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
</sql>
</changeSet>
</databaseChangeLog>
Loading