|
|
@ -1036,25 +1036,62 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public BillingResponse calculateUsage(String tenantDomain, Timestamp startDate, Timestamp endDate, List<Device> allDevices) throws MetadataManagementDAOException, DeviceManagementException {
|
|
|
|
public BillingResponse calculateUsage(String tenantDomain, Timestamp startDate, Timestamp endDate, List<Device> allDevices) throws MetadataManagementDAOException, DeviceManagementException {
|
|
|
|
|
|
|
|
|
|
|
|
// All code related to cost calculation has being commented out to comply with the current requirements
|
|
|
|
|
|
|
|
BillingResponse billingResponse = new BillingResponse();
|
|
|
|
BillingResponse billingResponse = new BillingResponse();
|
|
|
|
List<Device> deviceStatusNotAvailable = new ArrayList<>();
|
|
|
|
List<Device> deviceStatusNotAvailable = new ArrayList<>();
|
|
|
|
// double totalCost = 0.0;
|
|
|
|
double totalCost = 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// MetadataManagementService meta = DeviceManagementDataHolder
|
|
|
|
MetadataManagementService meta = DeviceManagementDataHolder
|
|
|
|
// .getInstance().getMetadataManagementService();
|
|
|
|
.getInstance().getMetadataManagementService();
|
|
|
|
// Metadata metadata = meta.retrieveMetadata(DeviceManagementConstants.META_KEY);
|
|
|
|
Metadata metadata = meta.retrieveMetadata(DeviceManagementConstants.META_KEY);
|
|
|
|
//
|
|
|
|
|
|
|
|
// Gson g = new Gson();
|
|
|
|
Gson g = new Gson();
|
|
|
|
// Collection<Cost> costData = null;
|
|
|
|
Collection<Cost> costData = null;
|
|
|
|
//
|
|
|
|
int tenantIdContext = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
// Type collectionType = new TypeToken<Collection<Cost>>() {
|
|
|
|
|
|
|
|
// }.getType();
|
|
|
|
Type collectionType = new TypeToken<Collection<Cost>>() {
|
|
|
|
// if (metadata != null) {
|
|
|
|
}.getType();
|
|
|
|
// costData = g.fromJson(metadata.getMetaValue(), collectionType);
|
|
|
|
if (tenantIdContext == MultitenantConstants.SUPER_TENANT_ID && metadata != null) {
|
|
|
|
// for (Cost tenantCost : costData) {
|
|
|
|
costData = g.fromJson(metadata.getMetaValue(), collectionType);
|
|
|
|
// if (tenantCost.getTenantDomain().equals(tenantDomain)) {
|
|
|
|
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()) {
|
|
|
|
|
|
|
|
allDevices.removeAll(deviceStatusNotAvailable);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Calendar calStart = Calendar.getInstance();
|
|
|
|
|
|
|
|
Calendar calEnd = Calendar.getInstance();
|
|
|
|
|
|
|
|
calStart.setTimeInMillis(startDate.getTime());
|
|
|
|
|
|
|
|
calEnd.setTimeInMillis(endDate.getTime());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
billingResponse.setDevice(allDevices);
|
|
|
|
|
|
|
|
billingResponse.setYear(String.valueOf(calStart.get(Calendar.YEAR)));
|
|
|
|
|
|
|
|
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.setDeviceCount(allDevices.size());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return billingResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double generateCost(List<Device> allDevices, Timestamp startDate, Timestamp endDate, Cost tenantCost, List<Device> deviceStatusNotAvailable, double totalCost) throws DeviceManagementException {
|
|
|
|
for (Device device : allDevices) {
|
|
|
|
for (Device device : allDevices) {
|
|
|
|
long dateDiff = 0;
|
|
|
|
long dateDiff = 0;
|
|
|
|
device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, endDate, true));
|
|
|
|
device.setDeviceStatusInfo(getDeviceStatusHistory(device, null, endDate, true));
|
|
|
@ -1089,49 +1126,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long dateInDays = (long) dateDiffInDays;
|
|
|
|
long dateInDays = (long) dateDiffInDays;
|
|
|
|
// double cost = (tenantCost.getCost() / 365) * dateInDays;
|
|
|
|
double cost = 0;
|
|
|
|
// totalCost += cost;
|
|
|
|
if (tenantCost != null) {
|
|
|
|
// device.setCost(Math.round(cost * 100.0) / 100.0);
|
|
|
|
cost = (tenantCost.getCost() / 365) * dateInDays;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
totalCost += cost;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalCost;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred calculating cost of devices";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!deviceStatusNotAvailable.isEmpty()) {
|
|
|
|
|
|
|
|
allDevices.removeAll(deviceStatusNotAvailable);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Calendar calStart = Calendar.getInstance();
|
|
|
|
|
|
|
|
Calendar calEnd = Calendar.getInstance();
|
|
|
|
|
|
|
|
calStart.setTimeInMillis(startDate.getTime());
|
|
|
|
|
|
|
|
calEnd.setTimeInMillis(endDate.getTime());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
billingResponse.setDevice(allDevices);
|
|
|
|
|
|
|
|
billingResponse.setYear(String.valueOf(calStart.get(Calendar.YEAR)));
|
|
|
|
|
|
|
|
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.setDeviceCount(allDevices.size());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return billingResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public PaginationResult createBillingFile(int tenantId, String tenantDomain, Timestamp startDate, Timestamp endDate) throws DeviceManagementException {
|
|
|
|
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();
|
|
|
|
PaginationResult paginationResult = new PaginationResult();
|
|
|
|
List<Device> allDevices = new ArrayList<>();
|
|
|
|
List<Device> allDevices = new ArrayList<>();
|
|
|
|
List<BillingResponse> billingResponseList = new ArrayList<>();
|
|
|
|
List<BillingResponse> billingResponseList = new ArrayList<>();
|
|
|
@ -1193,7 +1205,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
BillingResponse billingResponse = calculateUsage(tenantDomain, newStartDate, newEndDate, allDevicesPerYear);
|
|
|
|
BillingResponse billingResponse = calculateUsage(tenantDomain, newStartDate, newEndDate, allDevicesPerYear);
|
|
|
|
billingResponseList.add(billingResponse);
|
|
|
|
billingResponseList.add(billingResponse);
|
|
|
|
allDevices.addAll(billingResponse.getDevice());
|
|
|
|
allDevices.addAll(billingResponse.getDevice());
|
|
|
|
// totalCost = totalCost + billingResponse.getTotalCostPerYear();
|
|
|
|
totalCost = totalCost + billingResponse.getTotalCostPerYear();
|
|
|
|
deviceCount = deviceCount + billingResponse.getDeviceCount();
|
|
|
|
deviceCount = deviceCount + billingResponse.getDeviceCount();
|
|
|
|
LocalDateTime nextStartDate = oneYearAfterStart.plusDays(1).with(LocalTime.of(00, 00, 00));
|
|
|
|
LocalDateTime nextStartDate = oneYearAfterStart.plusDays(1).with(LocalTime.of(00, 00, 00));
|
|
|
|
startDate = Timestamp.valueOf(nextStartDate);
|
|
|
|
startDate = Timestamp.valueOf(nextStartDate);
|
|
|
@ -1217,7 +1229,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
BillingResponse billingResponse = calculateUsage(tenantDomain, startDate, endDate, allDevicesPerRemainingDays);
|
|
|
|
BillingResponse billingResponse = calculateUsage(tenantDomain, startDate, endDate, allDevicesPerRemainingDays);
|
|
|
|
billingResponseList.add(billingResponse);
|
|
|
|
billingResponseList.add(billingResponse);
|
|
|
|
allDevices.addAll(billingResponse.getDevice());
|
|
|
|
allDevices.addAll(billingResponse.getDevice());
|
|
|
|
// totalCost = totalCost + billingResponse.getTotalCostPerYear();
|
|
|
|
totalCost = totalCost + billingResponse.getTotalCostPerYear();
|
|
|
|
deviceCount = deviceCount + billingResponse.getDeviceCount();
|
|
|
|
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());
|
|
|
|
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);
|
|
|
|
billingResponseList.add(billingResponse);
|
|
|
|
paginationResult.setData(billingResponseList);
|
|
|
|
paginationResult.setData(billingResponseList);
|
|
|
|
// paginationResult.setTotalCost(Math.round(totalCost * 100.0) / 100.0);
|
|
|
|
paginationResult.setTotalCost(Math.round(totalCost * 100.0) / 100.0);
|
|
|
|
paginationResult.setTotalDeviceCount(deviceCount);
|
|
|
|
paginationResult.setTotalDeviceCount(deviceCount);
|
|
|
|
BillingCacheManagerImpl.getInstance().addBillingToCache(paginationResult, tenantDomain, initialStartDate, endDate);
|
|
|
|
BillingCacheManagerImpl.getInstance().addBillingToCache(paginationResult, tenantDomain, initialStartDate, endDate);
|
|
|
|
return paginationResult;
|
|
|
|
return paginationResult;
|
|
|
|