diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java index 31bf740d95a..5a9c4fa55d4 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java @@ -248,7 +248,7 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe devices.setList((List) result.getData()); devices.setDeviceCount(result.getTotalDeviceCount()); devices.setMessage(result.getMessage()); -// devices.setTotalCost(result.getTotalCost()); + devices.setTotalCost(result.getTotalCost()); devices.setBillPeriod(startDate.toString() + " - " + endDate.toString()); return Response.status(Response.Status.OK).entity(devices).build(); } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 4f4624aa84a..8413b1499f1 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1036,76 +1036,39 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv */ public BillingResponse calculateUsage(String tenantDomain, Timestamp startDate, Timestamp endDate, List allDevices) throws MetadataManagementDAOException, DeviceManagementException { - // All code related to cost calculation has being commented out to comply with the current requirements BillingResponse billingResponse = new BillingResponse(); List deviceStatusNotAvailable = new ArrayList<>(); -// double totalCost = 0.0; + double totalCost = 0.0; try { -// MetadataManagementService meta = DeviceManagementDataHolder -// .getInstance().getMetadataManagementService(); -// Metadata metadata = meta.retrieveMetadata(DeviceManagementConstants.META_KEY); -// -// Gson g = new Gson(); -// Collection costData = null; -// -// Type collectionType = new TypeToken>() { -// }.getType(); -// if (metadata != null) { -// costData = g.fromJson(metadata.getMetaValue(), collectionType); -// for (Cost tenantCost : costData) { -// if (tenantCost.getTenantDomain().equals(tenantDomain)) { - for (Device device : allDevices) { - long dateDiff = 0; - device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, endDate, true)); - List deviceStatus = device.getDeviceStatusInfo(); - if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) { - if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") - || 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") - || String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { - if (deviceStatus.get(0).getUpdateTime().getTime() >= device.getEnrolmentInfo().getDateOfEnrolment()) { - dateDiff = deviceStatus.get(0).getUpdateTime().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 - double dateDiffInDays = (double) dateDiff / (24 * 60 * 60 * 1000); - - if (dateDiffInDays % 1 >= 0.9) { - dateDiffInDays = Math.ceil(dateDiffInDays); - } - - long dateInDays = (long) dateDiffInDays; -// double cost = (tenantCost.getCost() / 365) * dateInDays; -// totalCost += cost; -// device.setCost(Math.round(cost * 100.0) / 100.0); - long totalDays = dateInDays + device.getDaysUsed(); - device.setDaysUsed((int) totalDays); - if (deviceStatus.isEmpty()) { - deviceStatusNotAvailable.add(device); - } - } - -// } -// } -// } + MetadataManagementService meta = DeviceManagementDataHolder + .getInstance().getMetadataManagementService(); + Metadata metadata = meta.retrieveMetadata(DeviceManagementConstants.META_KEY); + + Gson g = new Gson(); + Collection costData = null; + int tenantIdContext = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + + Type collectionType = new TypeToken>() { + }.getType(); + if (tenantIdContext == MultitenantConstants.SUPER_TENANT_ID && metadata != null) { + costData = g.fromJson(metadata.getMetaValue(), collectionType); + for (Cost tenantCost : costData) { + if (tenantCost.getTenantDomain().equals(tenantDomain)) { + totalCost = generateCost(allDevices, startDate, endDate, tenantCost, deviceStatusNotAvailable, totalCost); + } + } + } else { + totalCost = generateCost(allDevices, startDate, endDate, null, deviceStatusNotAvailable, totalCost); + } } catch (DeviceManagementException e) { String msg = "Error occurred calculating cost of devices"; log.error(msg, e); throw new DeviceManagementException(msg, e); + } catch (MetadataManagementException e) { + String msg = "Error when retrieving metadata of billing feature"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } if (!deviceStatusNotAvailable.isEmpty()) { @@ -1122,16 +1085,65 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv billingResponse.setStartDate(startDate.toString()); billingResponse.setEndDate(endDate.toString()); billingResponse.setBillPeriod(calStart.get(Calendar.YEAR) + " - " + calEnd.get(Calendar.YEAR)); -// billingResponse.setTotalCostPerYear(Math.round(totalCost * 100.0) / 100.0); + billingResponse.setTotalCostPerYear(Math.round(totalCost * 100.0) / 100.0); billingResponse.setDeviceCount(allDevices.size()); return billingResponse; } + public double generateCost(List allDevices, Timestamp startDate, Timestamp endDate, Cost tenantCost, List deviceStatusNotAvailable, double totalCost) throws DeviceManagementException { + for (Device device : allDevices) { + long dateDiff = 0; + device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, endDate, true)); + List deviceStatus = device.getDeviceStatusInfo(); + if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) { + if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") + || 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") + || String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { + if (deviceStatus.get(0).getUpdateTime().getTime() >= device.getEnrolmentInfo().getDateOfEnrolment()) { + dateDiff = deviceStatus.get(0).getUpdateTime().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 + double dateDiffInDays = (double) dateDiff / (24 * 60 * 60 * 1000); + + if (dateDiffInDays % 1 >= 0.9) { + dateDiffInDays = Math.ceil(dateDiffInDays); + } + + long dateInDays = (long) dateDiffInDays; + double cost = 0; + if (tenantCost != null) { + cost = (tenantCost.getCost() / 365) * dateInDays; + } + totalCost += cost; + device.setCost(Math.round(cost * 100.0) / 100.0); + long totalDays = dateInDays + device.getDaysUsed(); + device.setDaysUsed((int) totalDays); + if (deviceStatus.isEmpty()) { + deviceStatusNotAvailable.add(device); + } + } + return totalCost; + } + @Override public PaginationResult createBillingFile(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException { - // All code related to cost calculation has being commented out to comply with the current requirements PaginationResult paginationResult = new PaginationResult(); List allDevices = new ArrayList<>(); List billingResponseList = new ArrayList<>(); @@ -1193,7 +1205,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv BillingResponse billingResponse = calculateUsage(tenantDomain, newStartDate, newEndDate, allDevicesPerYear); billingResponseList.add(billingResponse); allDevices.addAll(billingResponse.getDevice()); -// totalCost = totalCost + billingResponse.getTotalCostPerYear(); + totalCost = totalCost + billingResponse.getTotalCostPerYear(); deviceCount = deviceCount + billingResponse.getDeviceCount(); LocalDateTime nextStartDate = oneYearAfterStart.plusDays(1).with(LocalTime.of(00, 00, 00)); startDate = Timestamp.valueOf(nextStartDate); @@ -1217,7 +1229,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv BillingResponse billingResponse = calculateUsage(tenantDomain, startDate, endDate, allDevicesPerRemainingDays); billingResponseList.add(billingResponse); allDevices.addAll(billingResponse.getDevice()); -// totalCost = totalCost + billingResponse.getTotalCostPerYear(); + totalCost = totalCost + billingResponse.getTotalCostPerYear(); deviceCount = deviceCount + billingResponse.getDeviceCount(); } @@ -1229,7 +1241,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv BillingResponse billingResponse = new BillingResponse("all", Math.round(totalCost * 100.0) / 100.0, allDevices, calStart.get(Calendar.YEAR) + " - " + calEnd.get(Calendar.YEAR), initialStartDate.toString(), endDate.toString(), allDevices.size()); billingResponseList.add(billingResponse); paginationResult.setData(billingResponseList); -// paginationResult.setTotalCost(Math.round(totalCost * 100.0) / 100.0); + paginationResult.setTotalCost(Math.round(totalCost * 100.0) / 100.0); paginationResult.setTotalDeviceCount(deviceCount); BillingCacheManagerImpl.getInstance().addBillingToCache(paginationResult, tenantDomain, initialStartDate, endDate); return paginationResult;