diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java index 2e7376ec81e..6b195dc6160 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java @@ -22,6 +22,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; +import java.math.BigInteger; @ApiModel(value = "EnrolmentInfo", description = "This class carries all information related to a devices enrollment" + " status.") @@ -48,6 +49,8 @@ public class EnrolmentInfo implements Serializable { private Long dateOfEnrolment; @ApiModelProperty(name = "dateOfLastUpdate", value = "Date of the device's last update. This value is not necessary.", required = false ) private Long dateOfLastUpdate; + @ApiModelProperty(name = "lastBilledDate", value = "Date of the device's last update. This value is not necessary.", required = false ) + private Long lastBilledDate; @ApiModelProperty(name = "ownership", value = "Defines the ownership details. The ownership type can be any of the" + " following values.\n" + "BYOD - Bring your own device (BYOD).\n" + @@ -100,6 +103,14 @@ public class EnrolmentInfo implements Serializable { this.dateOfLastUpdate = dateOfLastUpdate; } + public Long getLastBilledDate() { + return lastBilledDate; + } + + public void setLastBilledDate(Long lastBilledDate) { + this.lastBilledDate = lastBilledDate; + } + public OwnerShip getOwnership() { return ownership; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/cost/mgt/Costdata.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/cost/mgt/Costdata.java index 28d4fe0eed3..7c96af8c442 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/cost/mgt/Costdata.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/cost/mgt/Costdata.java @@ -30,8 +30,8 @@ public class Costdata { private String tenantDomain; private Double cost; - private Timestamp subscriptionBeginning; - private Timestamp subscriptionEnd; + private long subscriptionBeginning; + private long subscriptionEnd; @XmlElement(name = "tenantDomain", required = true) public String getTenantDomain() { @@ -52,20 +52,20 @@ public class Costdata { } @XmlElement(name = "subscriptionBeginning", required = true) - public Timestamp getSubscriptionBeginning() { + public long getSubscriptionBeginning() { return subscriptionBeginning; } - public void setSubscriptionBeginning(Timestamp subscriptionBeginning) { + public void setSubscriptionBeginning(long subscriptionBeginning) { this.subscriptionBeginning = subscriptionBeginning; } @XmlElement(name = "subscriptionEnd", required = true) - public Timestamp getSubscriptionEnd() { + public long getSubscriptionEnd() { return subscriptionEnd; } - public void setSubscriptionEnd(Timestamp subscriptionEnd) { + public void setSubscriptionEnd(long subscriptionEnd) { this.subscriptionEnd = subscriptionEnd; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index c79e46ebc88..f651b88a30b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -191,8 +191,11 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql ="select DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DATE_OF_ENROLMENT, STATUS,\n" + - "TIMESTAMPDIFF('DAY', CURDATE(), DATE_OF_ENROLMENT) as DAYS_SINCE_ENROLLED from DM_DEVICE d, DM_ENROLMENT e\n" + +// String sql ="select DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DATE_OF_ENROLMENT, LAST_BILLED_DATE AS BILLED_DATE,STATUS,\n" + +// "TIMESTAMPDIFF('DAY', CURDATE(), DATE_OF_ENROLMENT) as DAYS_SINCE_ENROLLED from DM_DEVICE d, DM_ENROLMENT e\n" + +// "where e.TENANT_ID= ? and d.ID=e.DEVICE_ID and STATUS !='REMOVED' LIMIT 10"; + String sql ="select DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DATE_OF_ENROLMENT, LAST_BILLED_DATE,STATUS,\n" + + "TIMESTAMPDIFF('DAY', DATE_OF_ENROLMENT, CURDATE()) as DAYS_SINCE_ENROLLED from DM_DEVICE d, DM_ENROLMENT e\n" + "where e.TENANT_ID= ? and d.ID=e.DEVICE_ID and STATUS !='REMOVED' LIMIT 10"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -219,7 +222,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "select DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, STATUS, " + + String sql = "select DEVICE_IDENTIFICATION, DESCRIPTION, NAME AS DEVICE_NAME, DATE_OF_ENROLMENT, LAST_BILLED_DATE, DATE_OF_LAST_UPDATE, STATUS, " + "TIMESTAMPDIFF('DAY', DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE) AS DAYS_USED from DM_DEVICE d, DM_ENROLMENT e" + " where e.TENANT_ID=? and d.ID=e.DEVICE_ID and STATUS ='REMOVED'\n"; stmt = conn.prepareStatement(sql); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index bd43651a808..045e4f8d48d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -154,8 +154,10 @@ public final class DeviceManagementDAOUtil { } public static EnrolmentInfo loadEnrolmentBilling(ResultSet rs, Boolean removedDevices) throws SQLException { + System.out.println("-----------------DAOO 222------------------------------"); EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); + enrolmentInfo.setLastBilledDate(rs.getLong("LAST_BILLED_DATE")); enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS"))); if (removedDevices) { enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); @@ -209,6 +211,7 @@ public final class DeviceManagementDAOUtil { } public static DeviceBilling loadDeviceBilling(ResultSet rs, Boolean removedDevices) throws SQLException { + System.out.println("-----------------DAOO 111------------------------------"); DeviceBilling device = new DeviceBilling(); device.setName(rs.getString("DEVICE_NAME")); device.setDescription(rs.getString("DESCRIPTION")); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index c61871cae9a..ce18d4c57d0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -978,6 +978,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.debug("Get devices with pagination " + request.toString() + " and requiredDeviceInfo: " + requireDeviceInfo); } + System.out.println("--------------------COREEEE LAYERR-------------------"); PaginationResult paginationResult = new PaginationResult(); Double totalCost = 0.0; List allDevices; @@ -988,8 +989,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { DeviceManagementDAOFactory.openConnection(); allDevices = deviceDAO.getDeviceBillList(request, tenantId); + allRemovedDevices = deviceDAO.getRemovedDeviceBillList(request,tenantId); count = deviceDAO.getDeviceCount(request, tenantId); + System.out.println("-----------------HERE------------------------------"); + String metaKey = "PER_DEVICE_COST"; MetadataManagementDAOFactory.openConnection(); Metadata metadata = metadataDAO.getMetadata(tenantId, metaKey); @@ -997,12 +1001,36 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv Gson g = new Gson(); Type collectionType = new TypeToken>(){}.getType(); - Collection costdata = g.fromJson(metadata.getMetaValue(), collectionType); + Collection costdata = g.fromJson(metadata.getMetaValue(), collectionType); // change name for (Costdata test: costdata) { if (test.getTenantDomain().equals(tenantDomain)) { for (DeviceBilling device: allDevices) { - long dateDiff = test.getSubscriptionEnd().getTime()-device.getEnrolmentInfo().getDateOfEnrolment(); + long dateDiff; + if (device.getEnrolmentInfo().getLastBilledDate() == 0) { + dateDiff = test.getSubscriptionEnd()-device.getEnrolmentInfo().getDateOfEnrolment(); + } else { + dateDiff = test.getSubscriptionEnd()-device.getEnrolmentInfo().getLastBilledDate(); + } +// dateDiff = test.getSubscriptionEnd().getTime()-device.getEnrolmentInfo().getDateOfEnrolment(); + long dateInDays = dateDiff / (1000*60*60*24); + double cost = (test.getCost()/365)*dateInDays; + totalCost = cost + totalCost; + device.setCost(cost); + } + } + } + + for (Costdata test: costdata) { + if (test.getTenantDomain().equals(tenantDomain)) { + for (DeviceBilling device: allRemovedDevices) { + long dateDiff; +// long dateDiff = device.getEnrolmentInfo().getDateOfLastUpdate()-device.getEnrolmentInfo().getDateOfEnrolment(); + if (device.getEnrolmentInfo().getLastBilledDate() == 0) { + dateDiff = test.getSubscriptionEnd()-device.getEnrolmentInfo().getDateOfEnrolment(); + } else { + dateDiff = test.getSubscriptionEnd()-device.getEnrolmentInfo().getLastBilledDate(); + } long dateInDays = dateDiff / (1000*60*60*24); double cost = (test.getCost()/365)*dateInDays; totalCost = cost + totalCost; @@ -1011,7 +1039,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } - allRemovedDevices = this.getRemovedDeviceListWithCost(paginationResult, request, costdata, tenantDomain, totalCost); +// allRemovedDevices = this.getRemovedDeviceListWithCost(paginationResult, request, costdata, tenantDomain, totalCost); allDevices.addAll(allRemovedDevices); } catch (DeviceManagementDAOException e) { @@ -1030,6 +1058,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } paginationResult.setData(allDevices); + paginationResult.setTotalCost(totalCost); paginationResult.setRecordsFiltered(count); paginationResult.setRecordsTotal(count); return paginationResult;