diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index 884fdfa1cae..8e03917f55f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -45,6 +45,7 @@ public final class DeviceManagementConstants { public static final String API_RESOURCE_PERMISSION_CACHE = "API_RESOURCE_CACHE_CACHE"; public static final String GEOFENCE_CACHE = "GEOFENCE_CACHE"; public static final String META_KEY = "PER_DEVICE_COST"; + public static final String ACTIVE_STATUS = "ACTIVE"; public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification"; public static final String URL_SEPERATOR = "/"; 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 b92ab554003..d9bb601faeb 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 @@ -129,6 +129,7 @@ import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.stratos.common.beans.TenantInfoBean; import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -946,7 +947,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv PaginationResult paginationResult = new PaginationResult(); double totalCost = 0.0; boolean allDevicesBilledDateIsValid = true; - String lastBilledDates = ""; ArrayList lastBilledDatesList = new ArrayList<>(); List invalidDevices = new ArrayList<>(); List removeBillingPeriodInvalidDevices = new ArrayList<>() ; @@ -954,7 +954,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { MetadataManagementDAOFactory.beginTransaction(); - Metadata metadata = metadataDAO.getMetadata(-1234, DeviceManagementConstants.META_KEY); + Metadata metadata = metadataDAO.getMetadata(MultitenantConstants.SUPER_TENANT_ID, DeviceManagementConstants.META_KEY); Gson g = new Gson(); Collection costData = null; @@ -970,7 +970,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv long dateDiff = 0; List deviceStatus = device.getDeviceStatusInfo(); - boolean lastBilledDate = false; + boolean firstDateBilled = false; boolean deviceStatusIsValid = false; List deviceBilling = billingDAO.getBilling(device.getId(), startDate, endDate); @@ -992,10 +992,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (!billDateIsInvalid) { for (int i = 0; i < deviceStatus.size(); i++) { - if (deviceStatus.get(i).getStatus().toString().equals("ACTIVE")) { + if (DeviceManagementConstants.ACTIVE_STATUS.equals(deviceStatus.get(i).getStatus().toString())) { if (deviceStatus.size() > i + 1) { - if (!lastBilledDate) { - lastBilledDate = true; + if (!firstDateBilled) { + firstDateBilled = true; if (device.getEnrolmentInfo().getLastBilledDate() == 0) { deviceStatusIsValid = true; dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime()); @@ -1011,11 +1011,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv dateDiff = dateDiff + (deviceStatus.get(i + 1).getUpdateTime().getTime() - deviceStatus.get(i).getUpdateTime().getTime()); } } - } else { // The last status update calculation is done in this block + } else { + // If only one status row is retrieved this block is executed - if (!lastBilledDate) { - lastBilledDate = true; - // Is executed if there is no lastBilled date and if the updates time is before the enddate + if (!firstDateBilled) { + firstDateBilled = true; + + // Is executed if there is no lastBilled date and if the updates time is before the end date if (device.getEnrolmentInfo().getLastBilledDate() == 0) { if (endDate.getTime() >= deviceStatus.get(i).getUpdateTime().getTime()) { deviceStatusIsValid = true; @@ -1066,11 +1068,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (device.getEnrolmentInfo().getLastBilledDate() != 0) { Date date = new Date(device.getEnrolmentInfo().getLastBilledDate()); Format format = new SimpleDateFormat("yyyy MM dd"); - if (!lastBilledDatesList.contains(lastBilledDatesList.add(format.format(date)))) { - lastBilledDatesList.add(format.format(date)); - } - lastBilledDates = lastBilledDates + ' ' + lastBilledDatesList; + for (String lastBillDate : lastBilledDatesList) { + if (!lastBillDate.equals(format.format(date))) { + lastBilledDatesList.add(format.format(date)); + } + } } removeBillingPeriodInvalidDevices.add(device); } @@ -1098,14 +1101,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if(!removeBillingPeriodInvalidDevices.isEmpty() && removeBillingPeriodInvalidDevices.size() == allDevices.size()) { allDevicesBilledDateIsValid = false; - paginationResult.setMessage("Invalid bill period last billed dates of devices are " +lastBilledDates); + paginationResult.setMessage("Invalid bill period."); } if(!removeStatusUpdateInvalidDevices.isEmpty() && removeStatusUpdateInvalidDevices.size() == allDevices.size()) { allDevicesBilledDateIsValid = false; if (paginationResult.getMessage() != null){ paginationResult.setMessage(paginationResult.getMessage() + " and no device updates within entered bill period."); } else { - paginationResult.setMessage("Devices have not been updated within the given period or entered end date comes before the last billed date"); + paginationResult.setMessage("Devices have not been updated within the given period or entered end date comes before the " + + "last billed date."); } } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml index 3143511d1af..1d13cea4d7e 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml @@ -25,10 +25,12 @@ 10000 - true - true - true - true + false + false + false + false + false + true diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 6372b87e166..c03341d4785 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -52,6 +52,17 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE ( CONSTRAINT uk_DM_DEVICE UNIQUE (NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) ); +CREATE TABLE IF NOT EXISTS DM_BILLING ( + INVOICE_ID INTEGER auto_increment NOT NULL, + TENANT_ID INTEGER default 0, + DEVICE_ID INT default NULL, + BILLING_START TIMESTAMP not null, + BILLING_END TIMESTAMP not null, + PRIMARY KEY (INVOICE_ID), + CONSTRAINT FK_DM_BILLING_DM_DEVICE + FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) +); + CREATE TABLE IF NOT EXISTS DM_DEVICE_PROPERTIES ( DEVICE_TYPE_NAME VARCHAR(300) NOT NULL, DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, @@ -103,6 +114,7 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( DATE_OF_ENROLMENT TIMESTAMP DEFAULT NULL, DATE_OF_LAST_UPDATE TIMESTAMP DEFAULT NULL, TENANT_ID INT NOT NULL, + LAST_BILLED_DATE BIGINT DEFAULT 0, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index bd301d6641d..4dc0bba047c 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -66,6 +66,18 @@ CREATE TABLE DM_DEVICE ( REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_BILLING]') AND TYPE IN (N'U')) +CREATE TABLE DM_BILLING ( + INVOICE_ID INTEGER IDENTITY(1,1) NOT NULL, + TENANT_ID INTEGER DEFAULT 0, + DEVICE_ID INTEGER DEFAULT NULL, + BILLING_START DATETIME2 NOT NULL, + BILLING_END DATETIME2 NOT NULL, + PRIMARY KEY (INVOICE_ID), + CONSTRAINT FK_DM_BILLING_DM_DEVICE2 FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); + IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_PROPERTIES]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_PROPERTIES ( DEVICE_TYPE_NAME VARCHAR(300) NOT NULL, @@ -146,6 +158,7 @@ CREATE TABLE DM_ENROLMENT ( DATE_OF_ENROLMENT DATETIME2 DEFAULT NULL, DATE_OF_LAST_UPDATE DATETIME2 DEFAULT NULL, TENANT_ID INTEGER NOT NULL, + LAST_BILLED_DATE BIGINT DEFAULT 0, PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index b191a177b77..d967f50e637 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -59,6 +59,17 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE ( REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION )ENGINE = InnoDB; +CREATE TABLE IF NOT EXISTS DM_BILLING ( + INVOICE_ID INTEGER AUTO_INCREMENT NOT NULL, + TENANT_ID INTEGER DEFAULT 0, + DEVICE_ID INTEGER DEFAULT NULL, + BILLING_START TIMESTAMP NOT NULL, + BILLING_END TIMESTAMP NOT NULL, + PRIMARY KEY (INVOICE_ID), + CONSTRAINT fk_DM_BILLING_DM_DEVICE2 FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +)ENGINE = InnoDB; + CREATE INDEX IDX_DM_DEVICE ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID); CREATE INDEX IDX_DM_DEVICE_TYPE_ID_DEVICE_IDENTIFICATION ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID,DEVICE_IDENTIFICATION); @@ -118,6 +129,7 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL, DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL, TENANT_ID INT NOT NULL, + LAST_BILLED_DATE BIGINT DEFAULT 0, PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 878d6095436..7059538ce52 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -124,6 +124,19 @@ WHEN (NEW.ID IS NULL) END; / +CREATE TABLE DM_BILLING ( + INVOICE_ID NUMBER(10) NOT NULL, + TENANT_ID NUMBER(10) DEFAULT 0, + DEVICE_ID NUMBER(10) DEFAULT NULL, + BILLING_START TIMESTAMP NOT NULL, + BILLING_END TIMESTAMP NOT NULL, + CONSTRAINT PK_DM_BILLING PRIMARY KEY (INVOICE_ID), + CONSTRAINT fk_DM_BILLING_DM_DEVICE2 + FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) +) +/ + CREATE TABLE DM_DEVICE_PROPERTIES ( DEVICE_TYPE_NAME VARCHAR2(300) NOT NULL, DEVICE_IDENTIFICATION VARCHAR2(300) NOT NULL, @@ -208,6 +221,7 @@ CREATE TABLE DM_ENROLMENT ( DATE_OF_ENROLMENT TIMESTAMP(0) DEFAULT NULL, DATE_OF_LAST_UPDATE TIMESTAMP(0) DEFAULT NULL, TENANT_ID NUMBER(10) NOT NULL, + LAST_BILLED_DATE BIGINT DEFAULT 0, CONSTRAINT PK_DM_ENROLMENT PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index a9f08b45f86..978cc7344da 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -57,6 +57,17 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE ( REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +CREATE TABLE IF NOT EXISTS DM_BILLING ( + INVOICE_ID INTEGER DEFAULT NEXTVAL ('DM_BILLING_seq') NOT NULL, + TENANT_ID INTEGER DEFAULT 0, + DEVICE_ID INTEGER DEFAULT NULL, + BILLING_START TIMESTAMP(0) NOT NULL, + BILLING_END TIMESTAMP(0) NOT NULL, + PRIMARY KEY (INVOICE_ID), + CONSTRAINT fk_DM_BILLING_DM_DEVICE2 FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + ); + CREATE INDEX IDX_DM_DEVICE ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID); CREATE INDEX IDX_DM_DEVICE_TYPE_ID_DEVICE_IDENTIFICATION ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID,DEVICE_IDENTIFICATION); @@ -116,6 +127,7 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( DATE_OF_ENROLMENT TIMESTAMP(0) NULL DEFAULT NULL, DATE_OF_LAST_UPDATE TIMESTAMP(0) NULL DEFAULT NULL, TENANT_ID INTEGER NOT NULL, + LAST_BILLED_DATE BIGINT DEFAULT 0, PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION