Fix usage generation in billing

pagination
Lasantha Dharmakeerthi 2 months ago
commit b40c5a625d

@ -250,11 +250,11 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
try { try {
Connection conn = getConnection(); Connection conn = getConnection();
String sql = "SELECT d.ID AS DEVICE_ID, " + String sql = "SELECT d.ID AS DEVICE_ID, " +
"DEVICE_IDENTIFICATION, " + "d.DEVICE_IDENTIFICATION, " +
"DESCRIPTION, " + "DESCRIPTION, " +
"NAME, " + "NAME, " +
"DATE_OF_ENROLMENT, " + "DATE_OF_ENROLMENT, " +
"LAST_UPDATED_TIMESTAMP, " + "d.LAST_UPDATED_TIMESTAMP, " +
"STATUS, " + "STATUS, " +
"DATE_OF_LAST_UPDATE, " + "DATE_OF_LAST_UPDATE, " +
"TIMESTAMPDIFF(DAY, ?, DATE_OF_ENROLMENT) as DAYS_SINCE_ENROLLED " + "TIMESTAMPDIFF(DAY, ?, DATE_OF_ENROLMENT) as DAYS_SINCE_ENROLLED " +
@ -291,12 +291,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "select d.ID AS DEVICE_ID, " + String sql = "select d.ID AS DEVICE_ID, " +
"DEVICE_IDENTIFICATION, " + "d.DEVICE_IDENTIFICATION, " +
"DESCRIPTION, " + "DESCRIPTION, " +
"NAME, " + "NAME, " +
"DATE_OF_ENROLMENT, " + "DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, " + "DATE_OF_LAST_UPDATE, " +
"d1.LAST_UPDATED_TIMESTAMP, " + "d.LAST_UPDATED_TIMESTAMP, " +
"STATUS, " + "STATUS, " +
"TIMESTAMPDIFF(DAY, DATE_OF_LAST_UPDATE, DATE_OF_ENROLMENT) AS DAYS_USED " + "TIMESTAMPDIFF(DAY, DATE_OF_LAST_UPDATE, DATE_OF_ENROLMENT) AS DAYS_USED " +
"from DM_DEVICE d, DM_ENROLMENT e " + "from DM_DEVICE d, DM_ENROLMENT e " +
@ -336,11 +336,11 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "select d.ID AS DEVICE_ID, " + String sql = "select d.ID AS DEVICE_ID, " +
"DEVICE_IDENTIFICATION, " + "d.DEVICE_IDENTIFICATION, " +
"DESCRIPTION, " + "DESCRIPTION, " +
"NAME, " + "NAME, " +
"DATE_OF_ENROLMENT, " + "DATE_OF_ENROLMENT, " +
"LAST_UPDATED_TIMESTAMP, " + "d.LAST_UPDATED_TIMESTAMP, " +
"STATUS, " + "STATUS, " +
"DATE_OF_LAST_UPDATE, " + "DATE_OF_LAST_UPDATE, " +
"TIMESTAMPDIFF(DAY, ?, ?) as DAYS_SINCE_ENROLLED " + "TIMESTAMPDIFF(DAY, ?, ?) as DAYS_SINCE_ENROLLED " +
@ -377,12 +377,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "select d.ID AS DEVICE_ID, " + String sql = "select d.ID AS DEVICE_ID, " +
"DEVICE_IDENTIFICATION, " + "d.DEVICE_IDENTIFICATION, " +
"DESCRIPTION, " + "DESCRIPTION, " +
"NAME, " + "NAME, " +
"DATE_OF_ENROLMENT, " + "DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, " + "DATE_OF_LAST_UPDATE, " +
"LAST_UPDATED_TIMESTAMP, " + "d.LAST_UPDATED_TIMESTAMP, " +
"STATUS, " + "STATUS, " +
"TIMESTAMPDIFF(DAY, DATE_OF_LAST_UPDATE, ?) AS DAYS_USED " + "TIMESTAMPDIFF(DAY, DATE_OF_LAST_UPDATE, ?) AS DAYS_USED " +
"from DM_DEVICE d, DM_ENROLMENT e " + "from DM_DEVICE d, DM_ENROLMENT e " +
@ -425,9 +425,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT " + String sql = "SELECT " +
"DM_DEVICE.ID AS DEVICE_ID, " + "DM_DEVICE.ID AS DEVICE_ID, " +
"DEVICE_IDENTIFICATION, " + "d.DEVICE_IDENTIFICATION, " +
"DESCRIPTION, " + "DESCRIPTION, " +
"LAST_UPDATED_TIMESTAMP, " + "d.LAST_UPDATED_TIMESTAMP, " +
"DM_DEVICE.NAME AS DEVICE_NAME, " + "DM_DEVICE.NAME AS DEVICE_NAME, " +
"DEVICE_TYPE, " + "DEVICE_TYPE, " +
"DM_ENROLMENT.ID AS ENROLMENT_ID, " + "DM_ENROLMENT.ID AS ENROLMENT_ID, " +

@ -1176,50 +1176,57 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
public double generateCost(List<Device> allDevices, Timestamp startDate, Timestamp endDate, Cost tenantCost, List<Device> deviceStatusNotAvailable, double totalCost) throws DeviceManagementException { public double generateCost(List<Device> allDevices, Timestamp startDate, Timestamp endDate, Cost tenantCost, List<Device> deviceStatusNotAvailable, double totalCost) throws DeviceManagementException {
List<DeviceStatus> deviceStatus; List<DeviceStatus> deviceStatus;
for (Device device : allDevices) { try {
long dateDiff = 0; for (Device device : allDevices) {
deviceStatus = getDeviceStatusHistoryInsideTransaction(device, null, endDate, true); long dateDiff = 0;
if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) { int tenantId = this.getTenantId();
if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") deviceStatus = deviceStatusDAO.getStatus(device.getId(), tenantId, null, endDate, true);
|| String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) {
if (deviceStatus.get(0).getUpdateTime().getTime() >= startDate.getTime()) { if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")
dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - startDate.getTime(); || String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) {
if (deviceStatus.get(0).getUpdateTime().getTime() >= startDate.getTime()) {
dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - startDate.getTime();
}
} else if (!deviceStatus.isEmpty() && (!String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")
&& !String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) {
dateDiff = endDate.getTime() - startDate.getTime();
} }
} else if (!deviceStatus.isEmpty() && (!String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") } else {
&& !String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")
dateDiff = endDate.getTime() - startDate.getTime(); || String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) {
} if (deviceStatus.get(0).getUpdateTime().getTime() >= device.getEnrolmentInfo().getDateOfEnrolment()) {
} else { dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - device.getEnrolmentInfo().getDateOfEnrolment();
if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") }
|| String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { } else if (!deviceStatus.isEmpty() && (!String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")
if (deviceStatus.get(0).getUpdateTime().getTime() >= device.getEnrolmentInfo().getDateOfEnrolment()) { && !String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) {
dateDiff = deviceStatus.get(0).getUpdateTime().getTime() - device.getEnrolmentInfo().getDateOfEnrolment(); dateDiff = endDate.getTime() - device.getEnrolmentInfo().getDateOfEnrolment();
} }
} else if (!deviceStatus.isEmpty() && (!String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED")
&& !String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) {
dateDiff = endDate.getTime() - device.getEnrolmentInfo().getDateOfEnrolment();
} }
}
// Convert dateDiff to days as a decimal value // Convert dateDiff to days as a decimal value
double dateDiffInDays = (double) dateDiff / (24 * 60 * 60 * 1000); double dateDiffInDays = (double) dateDiff / (24 * 60 * 60 * 1000);
if (dateDiffInDays % 1 >= 0.9) { if (dateDiffInDays % 1 >= 0.9) {
dateDiffInDays = Math.ceil(dateDiffInDays); dateDiffInDays = Math.ceil(dateDiffInDays);
} }
long dateInDays = (long) dateDiffInDays; long dateInDays = (long) dateDiffInDays;
double cost = 0; double cost = 0;
if (tenantCost != null) { if (tenantCost != null) {
cost = (tenantCost.getCost() / 365) * dateInDays; cost = (tenantCost.getCost() / 365) * dateInDays;
} }
totalCost += cost; totalCost += cost;
device.setCost(Math.round(cost * 100.0) / 100.0); device.setCost(Math.round(cost * 100.0) / 100.0);
long totalDays = dateInDays + device.getDaysUsed(); long totalDays = dateInDays + device.getDaysUsed();
device.setDaysUsed((int) totalDays); device.setDaysUsed((int) totalDays);
if (deviceStatus.isEmpty()) { if (deviceStatus.isEmpty()) {
deviceStatusNotAvailable.add(device); deviceStatusNotAvailable.add(device);
}
} }
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred in retrieving status history for a device in billing.";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} }
return totalCost; return totalCost;
} }
@ -2233,33 +2240,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
} }
/*
This is just to avoid breaking the billing functionality as it required to call getDeviceStatusHistory method
without transaction handling.
*/
private List<DeviceStatus> getDeviceStatusHistoryInsideTransaction(
Device device, Date fromDate, Date toDate, boolean billingStatus)
throws DeviceManagementException {
if (log.isDebugEnabled()) {
log.debug("get status history of device: " + device.getDeviceIdentifier());
}
try {
DeviceManagementDAOFactory.getConnection();
int tenantId = this.getTenantId();
return deviceStatusDAO.getStatus(device.getId(), tenantId, fromDate, toDate, billingStatus);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred in retrieving status history for device :" + device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";
log.info(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Override @Override
public List<DeviceStatus> getDeviceStatusHistory(Device device, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementException { public List<DeviceStatus> getDeviceStatusHistory(Device device, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {

Loading…
Cancel
Save