From cf7c4c6b0677ad7e3ffa38750fb5281a2875a062 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Wed, 18 Jan 2017 17:02:23 +0100 Subject: [PATCH 01/15] Added calculation for Period in PRO Snap --- src/classes/PROContractSnapshotBatch.cls | 60 ++++++++++---------- src/classes/ProjectReportOutController.cls | 24 +------- src/classes/SnapshotHelper.cls | 65 ++++++++++++++++++++++ src/classes/SnapshotHelper.cls-meta.xml | 4 ++ 4 files changed, 102 insertions(+), 51 deletions(-) create mode 100644 src/classes/SnapshotHelper.cls create mode 100644 src/classes/SnapshotHelper.cls-meta.xml diff --git a/src/classes/PROContractSnapshotBatch.cls b/src/classes/PROContractSnapshotBatch.cls index ce1b10d1..e7ef7405 100644 --- a/src/classes/PROContractSnapshotBatch.cls +++ b/src/classes/PROContractSnapshotBatch.cls @@ -15,38 +15,37 @@ global class PROContractSnapshotBatch implements Database.Batchable contracts) { - List insertList = new List(); - - for (DContract__c dcontract : contracts) { - PRO_Contract_Snapshot__c proContractSnapshot = new PRO_Contract_Snapshot__c(); - - proContractSnapshot.Budget_Used__c = dcontract.Budget_Used__c; - proContractSnapshot.Percent_Of_Work_Completed__c = dcontract.Percent_through_SOW_deliverables__c; - proContractSnapshot.Percent_Services_Budget_For_GS__c = dcontract.of_Services_budget_for_GS__c; - proContractSnapshot.Buffer_By_Deliverables_Complete__c = dcontract.Buffer_by_SOW_Completed__c; - proContractSnapshot.Execution_Efficiency__c = dcontract.Execution_Efficiency__c; - proContractSnapshot.Expected_Buffer__c = dcontract.Expected_Buffer__c; - proContractSnapshot.Expense_And_Travel_Spend__c = dcontract.Direct_Costs_Total_Expenses__c; - proContractSnapshot.Net_Income_By_Deliverables_Complete__c = dcontract.Net_Income_based_on_SOW_Completed__c; - proContractSnapshot.Product_Spend__c = dcontract.Product_Spend__c; - proContractSnapshot.Services_Spend__c = dcontract.Services_Spend__c; - proContractSnapshot.Total_Calculated_Costs__c = dcontract.Total_Costs__c; - proContractSnapshot.Contract__c = dcontract.Id; - proContractSnapshot.Name = dcontract.Name; - proContractSnapshot.Snapshot_Date__c = Date.today(); - - insertList.add(proContractSnapshot); - } + try { + List insertList = new List(); + Map oldSnapshots = SnapshotHelper.getOldPROSnapshot(contracts); + + for (DContract__c dcontract : contracts) { + PRO_Contract_Snapshot__c proContractSnapshot = new PRO_Contract_Snapshot__c(); + PRO_Contract_Snapshot__c oldSnapshot = new PRO_Contract_Snapshot__c(); + if (oldSnapshots.containsKey(dcontract.Id)) { + oldSnapshot = oldSnapshots.get(dcontract.Id); + } + proContractSnapshot = SnapshotHelper.fillPROSnapshot(proContractSnapshot, dcontract, oldSnapshot); - List insertResults = Database.insert(insertList, false); + // TODO Fetch last PRO snapshot and calculate other fields + // Todo add job for pipeline snapshot - working daily for calcukaction !!! - for(Integer i = 0; i < insertResults.size(); i++){ - if (!insertResults.get(i).isSuccess()){ - Database.Error error = insertResults.get(i).getErrors().get(0); - System.debug('DML error ' + error.getMessage()); - System.debug('Failed to create snapshot for Contract: ' + insertList.get(i).Contract__c); - failedReocrds.add(insertList.get(i)); + insertList.add(proContractSnapshot); } + + List insertResults = Database.insert(insertList, false); + + for(Integer i = 0; i < insertResults.size(); i++) { + if (!insertResults.get(i).isSuccess()){ + Database.Error error = insertResults.get(i).getErrors().get(0); + System.debug('DML error ' + error.getMessage()); + System.debug('Failed to create snapshot for Contract: ' + insertList.get(i).Contract__c); + failedReocrds.add(insertList.get(i)); + } + } + + } catch (Exception e) { + System.debug('ERROR', e); } } @@ -80,7 +79,8 @@ global class PROContractSnapshotBatch implements Database.Batchable GetContract() { List contractList = [SELECT Id, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c - FROM DContract__c WHERE Status__c = 'Active' AND Requires_Report_Out__c = 'No']; + GS_Net_Income_to_Date_Handover__c, GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, + GS_Net_Income_to_Date_Handover__c FROM DContract__c WHERE Status__c = 'Active' AND Requires_Report_Out__c = 'No']; return contractList; } } \ No newline at end of file diff --git a/src/classes/ProjectReportOutController.cls b/src/classes/ProjectReportOutController.cls index 2f6d1fba..a24f240a 100644 --- a/src/classes/ProjectReportOutController.cls +++ b/src/classes/ProjectReportOutController.cls @@ -230,6 +230,7 @@ public with sharing class ProjectReportOutController { Project_Report_Out__c proToUpdate = createPROToUpdate(proList.isEmpty() ? null : proList[0]); DContract__c dcontract = [SELECT Id, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c + GS_Execution_Budget__c, GS_Net_Income_to_Date_Handover__c, GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM DContract__c WHERE Id =: reportContractId]; dcontract.Last_Report_Out_Status__c = status; @@ -334,28 +335,9 @@ public with sharing class ProjectReportOutController { private PRO_Contract_Snapshot__c createProSnapshot(Id currentSnapshot, DContract__c dcontract) { PRO_Contract_Snapshot__c proContractSnapshot = new PRO_Contract_Snapshot__c(); if (currentSnapshot != null) { // we must update snapshot - proContractSnapshot = [SELECT Id, Budget_Used__c, Percent_Of_Work_Completed__c, Percent_Services_Budget_For_GS__c, Buffer_By_Deliverables_Complete__c, - Execution_Efficiency__c, Expected_Buffer__c, Expense_And_Travel_Spend__c, Net_Income_By_Deliverables_Complete__c, Product_Spend__c, - Services_Spend__c, Total_Calculated_Costs__c, Contract__c, Name, Snapshot_Date__c - FROM PRO_Contract_Snapshot__c WHERE Id =: currentSnapshot]; + proContractSnapshot.Id = currentSnapshot; } - - proContractSnapshot.Budget_Used__c = dcontract.Budget_Used__c; - proContractSnapshot.Percent_Of_Work_Completed__c = dcontract.Percent_through_SOW_deliverables__c; - proContractSnapshot.Percent_Services_Budget_For_GS__c = dcontract.of_Services_budget_for_GS__c; - proContractSnapshot.Buffer_By_Deliverables_Complete__c = dcontract.Buffer_by_SOW_Completed__c; - proContractSnapshot.Execution_Efficiency__c = dcontract.Execution_Efficiency__c; - proContractSnapshot.Expected_Buffer__c = dcontract.Expected_Buffer__c; - proContractSnapshot.Expense_And_Travel_Spend__c = dcontract.Direct_Costs_Total_Expenses__c; - proContractSnapshot.Net_Income_By_Deliverables_Complete__c = dcontract.Net_Income_based_on_SOW_Completed__c; - proContractSnapshot.Product_Spend__c = dcontract.Product_Spend__c; - proContractSnapshot.Services_Spend__c = dcontract.Services_Spend__c; - proContractSnapshot.Total_Calculated_Costs__c = dcontract.Total_Costs__c; - proContractSnapshot.Contract__c = dcontract.Id; - proContractSnapshot.Name = dcontract.Name; - proContractSnapshot.Snapshot_Date__c = Date.today(); - - return proContractSnapshot; + return fillPROSnapshot(proContractSnapshot, dcontract, SnapshotHelper.getLastPROSnapshot(dcontract)); } // Get a list of picklist values from an existing object field. diff --git a/src/classes/SnapshotHelper.cls b/src/classes/SnapshotHelper.cls new file mode 100644 index 00000000..6073824c --- /dev/null +++ b/src/classes/SnapshotHelper.cls @@ -0,0 +1,65 @@ +/** + * Helper class used for creating Snasphots objects in the System. + */ +public class SnapshotHelper { + + public static Map getOldPROSnapshot(List contracts) { + Map snapshotsMap = new Map(); + List snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c + GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: Date.today() + AND Contract__c IN: contracts ORDER BY Snapshot_Date__c DESC]; + + for (PRO_Contract_Snapshot__c sn : snapshotsList) { + if (!snapshotsMap.containcKey(sn.Contract__c)) { + snapshotsMap.put(sn.Contract__c, sn); + } + } + + return snapshotsMap; + } + + public static PRO_Contract_Snapshot__c getLastPROSnapshot(DContract__c dcontract) { + List snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c + GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: Date.today() + AND Contract__c =: dcontract ORDER BY Snapshot_Date__c DESC LIMIT 1]; + if (snapshotsList.size() > 0) { + return snapshotsList.get(0); + } + + return new PRO_Contract_Snapshot__c(); + } + + public static PRO_Contract_Snapshot__c fillPROSnapshot(PRO_Contract_Snapshot__c proContractSnapshot, DContract__c dcontract, PRO_Contract_Snapshot__c oldSnapshot) { + proContractSnapshot.Budget_Used__c = dcontract.Budget_Used__c; + proContractSnapshot.Percent_Of_Work_Completed__c = dcontract.Percent_through_SOW_deliverables__c; + proContractSnapshot.Percent_Services_Budget_For_GS__c = dcontract.of_Services_budget_for_GS__c; + proContractSnapshot.Buffer_By_Deliverables_Complete__c = dcontract.Buffer_by_SOW_Completed__c; + proContractSnapshot.Execution_Efficiency__c = dcontract.Execution_Efficiency__c; + proContractSnapshot.Expected_Buffer__c = dcontract.Expected_Buffer__c; + proContractSnapshot.Expense_And_Travel_Spend__c = dcontract.Direct_Costs_Total_Expenses__c; + proContractSnapshot.Net_Income_By_Deliverables_Complete__c = dcontract.Net_Income_based_on_SOW_Completed__c; + proContractSnapshot.Product_Spend__c = dcontract.Product_Spend__c; + proContractSnapshot.Services_Spend__c = dcontract.Services_Spend__c; + proContractSnapshot.Total_Calculated_Costs__c = dcontract.Total_Costs__c; + proContractSnapshot.Contract__c = dcontract.Id; + proContractSnapshot.Name = dcontract.Name; + proContractSnapshot.Snapshot_Date__c = Date.today(); + proContractSnapshot.GS_Execution_Budget__c = dcontract.GS_Execution_Budget__c; + proContractSnapshot.GS_Services_Completed_Handover__c = dcontract.GS_Services_Completed_Handover__c; + proContractSnapshot.GS_Services_Completed__c = dcontract.GS_Services_Completed__c; + proContractSnapshot.GS_Net_Income_to_Date__c = dcontract.GS_Net_Income_to_Date__c; + proContractSnapshot.GS_Net_Income_to_Date_Handover__c = dcontract.GS_Net_Income_to_Date_Handover__c; + + proContractSnapshot.Work_Completed_for_Period__c = (dcontract.Percent_through_SOW_deliverables__c != null ? dcontract.Percent_through_SOW_deliverables__c : 0) - (oldSnapshot.Percent_Of_Work_Completed__c != null ? oldSnapshot.Percent_Of_Work_Completed__c : 0); + proContractSnapshot.Services_Spend_for_Period__c = (dcontract.Services_Spend__c != null ? dcontract.Services_Spend__c : 0) - (oldSnapshot.Services_Spend__c != null ? oldSnapshot.Services_Spend__c : 0); + proContractSnapshot.Expense_Travel_Spend_for_Period__c = (dcontract.Direct_Costs_Total_Expenses__c != null ? dcontract.Direct_Costs_Total_Expenses__c : 0) - (oldSnapshot.Expense_And_Travel_Spend__c != null ? oldSnapshot.Expense_And_Travel_Spend__c : 0); + proContractSnapshot.Product_Spend_for_Period__c = (dcontract.Product_Spend__c != null ? dcontract.Product_Spend__c : 0) - (oldSnapshot.Product_Spend__c != null ? oldSnapshot.Product_Spend__c : 0); + proContractSnapshot.Total_Calculated_Costs_for_Period__c = (dcontract.Total_Costs__c != null ? dcontract.Total_Costs__c : 0) - (oldSnapshot.Total_Calculated_Costs__c != null ? oldSnapshot.Total_Calculated_Costs__c : 0); + proContractSnapshot.GS_Services_Handover_Completed_for_Per__c = (dcontract.GS_Services_Completed_Handover__c != null ? dcontract.GS_Services_Completed_Handover__c : 0) - (oldSnapshot.GS_Services_Completed_Handover__c != null ? oldSnapshot.GS_Services_Completed_Handover__c : 0); + proContractSnapshot.GS_Services_Completed_for_Period__c = (dcontract.GS_Services_Completed__c != null ? dcontract.GS_Services_Completed__c : 0) - (oldSnapshot.GS_Services_Completed__c != null ? oldSnapshot.GS_Services_Completed__c : 0); + proContractSnapshot.GS_Net_Income_for_Period__c = (dcontract.GS_Net_Income_to_Date__c != null ? dcontract.GS_Net_Income_to_Date__c : 0) - (oldSnapshot.GS_Net_Income_to_Date__c != null ? oldSnapshot.GS_Net_Income_to_Date__c : 0); + proContractSnapshot.GS_Net_Income_for_Period_by_Handover__c = (dcontract.GS_Net_Income_to_Date_Handover__c != null ? dcontract.GS_Net_Income_to_Date_Handover__c : 0) - (oldSnapshot.GS_Net_Income_to_Date_Handover__c != null ? oldSnapshot.GS_Net_Income_to_Date_Handover__c : 0); + + return proContractSnapshot; + } +} \ No newline at end of file diff --git a/src/classes/SnapshotHelper.cls-meta.xml b/src/classes/SnapshotHelper.cls-meta.xml new file mode 100644 index 00000000..3e5b29c4 --- /dev/null +++ b/src/classes/SnapshotHelper.cls-meta.xml @@ -0,0 +1,4 @@ + + + 36.0 + From fee6ea28697940c7f5addf40176c6aec9f91c704 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Fri, 20 Jan 2017 11:49:34 +0100 Subject: [PATCH 02/15] Fixed Snapshot test --- src/classes/PROContractSnapshotBatch.cls | 14 +++++++++----- src/classes/PROContractSnapshotScheduled.cls | 2 +- src/classes/ProjectReportOutController.cls | 6 +++--- src/classes/SnapshotHelper.cls | 14 +++++++------- src/classes/SnapshotHelper.cls-meta.xml | 3 ++- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/classes/PROContractSnapshotBatch.cls b/src/classes/PROContractSnapshotBatch.cls index e7ef7405..ad80df63 100644 --- a/src/classes/PROContractSnapshotBatch.cls +++ b/src/classes/PROContractSnapshotBatch.cls @@ -44,8 +44,12 @@ global class PROContractSnapshotBatch implements Database.Batchable GetContract() { List contractList = [SELECT Id, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, - Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c - GS_Net_Income_to_Date_Handover__c, GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, - GS_Net_Income_to_Date_Handover__c FROM DContract__c WHERE Status__c = 'Active' AND Requires_Report_Out__c = 'No']; + Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c, + GS_Net_Income_to_Date_Handover__c, GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Services_Completed_Handover__c + FROM DContract__c WHERE Status__c = 'Active' AND Requires_Report_Out__c = 'No']; return contractList; } } \ No newline at end of file diff --git a/src/classes/PROContractSnapshotScheduled.cls b/src/classes/PROContractSnapshotScheduled.cls index ce4f30b4..c20185ba 100644 --- a/src/classes/PROContractSnapshotScheduled.cls +++ b/src/classes/PROContractSnapshotScheduled.cls @@ -10,7 +10,7 @@ global class PROContractSnapshotScheduled implements Schedulable { List thursDayList = PROHelper.GetAllThursDayOfMonth(currentYear, currentMonth); if (PROHelper.IsThursDay(currentDate) && (thursDayList[1] == currentDate || thursDayList[3] == currentDate)) { - PROContractSnapshotBatch proContractSnapshotBatch = new PROContractSnapshotBatch(); + PROContractSnapshotBatch proContractSnapshotBatch = new PROContractSnapshotBatch(); database.executeBatch(proContractSnapshotBatch, 50); } } diff --git a/src/classes/ProjectReportOutController.cls b/src/classes/ProjectReportOutController.cls index a24f240a..6daa5cd0 100644 --- a/src/classes/ProjectReportOutController.cls +++ b/src/classes/ProjectReportOutController.cls @@ -229,8 +229,8 @@ public with sharing class ProjectReportOutController { Project_Report_Out__c proToUpdate = createPROToUpdate(proList.isEmpty() ? null : proList[0]); DContract__c dcontract = [SELECT Id, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, - Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c - GS_Execution_Budget__c, GS_Net_Income_to_Date_Handover__c, GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c + Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c, + GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c, GS_Services_Completed_Handover__c FROM DContract__c WHERE Id =: reportContractId]; dcontract.Last_Report_Out_Status__c = status; @@ -337,7 +337,7 @@ public with sharing class ProjectReportOutController { if (currentSnapshot != null) { // we must update snapshot proContractSnapshot.Id = currentSnapshot; } - return fillPROSnapshot(proContractSnapshot, dcontract, SnapshotHelper.getLastPROSnapshot(dcontract)); + return SnapshotHelper.fillPROSnapshot(proContractSnapshot, dcontract, SnapshotHelper.getLastPROSnapshot(dcontract)); } // Get a list of picklist values from an existing object field. diff --git a/src/classes/SnapshotHelper.cls b/src/classes/SnapshotHelper.cls index 6073824c..6d69aba0 100644 --- a/src/classes/SnapshotHelper.cls +++ b/src/classes/SnapshotHelper.cls @@ -4,13 +4,13 @@ public class SnapshotHelper { public static Map getOldPROSnapshot(List contracts) { - Map snapshotsMap = new Map(); - List snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c - GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: Date.today() + Map snapshotsMap = new Map(); + List snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c, + Contract__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: Date.today() AND Contract__c IN: contracts ORDER BY Snapshot_Date__c DESC]; for (PRO_Contract_Snapshot__c sn : snapshotsList) { - if (!snapshotsMap.containcKey(sn.Contract__c)) { + if (!snapshotsMap.containsKey(sn.Contract__c)) { snapshotsMap.put(sn.Contract__c, sn); } } @@ -19,9 +19,9 @@ public class SnapshotHelper { } public static PRO_Contract_Snapshot__c getLastPROSnapshot(DContract__c dcontract) { - List snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c - GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: Date.today() - AND Contract__c =: dcontract ORDER BY Snapshot_Date__c DESC LIMIT 1]; + List snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c, + Contract__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: Date.today() + AND Contract__c =: dcontract.Id ORDER BY Snapshot_Date__c DESC LIMIT 1]; if (snapshotsList.size() > 0) { return snapshotsList.get(0); } diff --git a/src/classes/SnapshotHelper.cls-meta.xml b/src/classes/SnapshotHelper.cls-meta.xml index 3e5b29c4..38aa015d 100644 --- a/src/classes/SnapshotHelper.cls-meta.xml +++ b/src/classes/SnapshotHelper.cls-meta.xml @@ -1,4 +1,5 @@ - + 36.0 + Active From 50ad6e7458a8ce65037f0f65aa960d686a1909e6 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Fri, 20 Jan 2017 13:59:07 +0100 Subject: [PATCH 03/15] Added calculation period field in Pipeline Snapshot --- src/classes/ForPeriodCalcSnapshotBatch.cls | 55 +++++++++++++++++++ .../ForPeriodCalcSnapshotBatch.cls-meta.xml | 4 ++ src/classes/GSRevenueCalculationBatch.cls | 47 ++++++++++++++++ .../GSRevenueCalculationBatch.cls-meta.xml | 4 ++ src/classes/ProjectReportOutController.cls | 4 +- src/classes/SnapshotHelper.cls | 47 ++++++++++++++++ 6 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 src/classes/ForPeriodCalcSnapshotBatch.cls create mode 100644 src/classes/ForPeriodCalcSnapshotBatch.cls-meta.xml create mode 100644 src/classes/GSRevenueCalculationBatch.cls create mode 100644 src/classes/GSRevenueCalculationBatch.cls-meta.xml diff --git a/src/classes/ForPeriodCalcSnapshotBatch.cls b/src/classes/ForPeriodCalcSnapshotBatch.cls new file mode 100644 index 00000000..1ca59f75 --- /dev/null +++ b/src/classes/ForPeriodCalcSnapshotBatch.cls @@ -0,0 +1,55 @@ +/** + * Calculates for period fields in Pipeline Snapshot custom object. + */ +public class ForPeriodCalcSnapshotBatch implements Database.Batchable, Database.Stateful { + + public ForPeriodCalcSnapshotBatch(Boolean forTotal){ + this.forTotal = forTotal; + } + + public List start(Database.BatchableContext context) { + if (this.forTotal == false) { + return [SELECT Id, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, + GS_Services_Completed_Handover__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c, + Work_Completed_for_Period__c, Expense_Travel_Spend_for_Period__c, GS_Net_Income_for_Period__c, GS_Net_Income_for_Period_by_Handover__c, + GS_Services_Handover_Completed_for_Per__c, GS_Services_Completed_for_Period__c, Product_Spend_for_Period__c, Services_Spend_for_Period__c, + Total_Calculated_Costs_for_Period__c, Month_Date__c + FROM Pipeline_Snapshot__c WHERE CreatedDate >= YESTERDAY AND Current_Stage__c != 'Total' AND Source_Type__c = 'Contract']; + } + return [SELECT Id, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, + GS_Services_Completed_Handover__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c, + Work_Completed_for_Period__c, Expense_Travel_Spend_for_Period__c, GS_Net_Income_for_Period__c, GS_Net_Income_for_Period_by_Handover__c, + GS_Services_Handover_Completed_for_Per__c, GS_Services_Completed_for_Period__c, Product_Spend_for_Period__c, Services_Spend_for_Period__c, + Total_Calculated_Costs_for_Period__c, Month_Date__c + FROM Pipeline_Snapshot__c WHERE CreatedDate >= YESTERDAY AND Current_Stage__c = 'Total' AND Source_Type__c = 'Contract']; + } + + public void execute(Database.BatchableContext context, List snapshots) { + List toUpdate = new List(); + try { + Map oldSnapshots = SnapshotHelper.getOldSnapshots(snapshots); + for (Pipeline_Snapshot__c sn : snapshots) { + Pipeline_Snapshot__c oldSn = new Pipeline_Snapshot__c(); + if (sn.Contract__c != null && oldSnapshots.containsKey(sn.Contract__c)) { + oldSn = oldSnapshots.get(sn.Contract__c); + } + sn = SnapshotHelper.calculateForPeriod(sn, oldSnapshots); + toUpdate.add(sn); + } + update toUpdate; + } catch(Exception ex) { + System.debug('ERROR:' + ex); + if (!Test.isRunningTest()) { + BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); + EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'For Period calculation error', 'Period calculation error', ex); + } + } + } + + public void finish(Database.BatchableContext context) { + if (this.forTotal == false) { + database.executeBatch(new ForPeriodCalcSnapshotBatch(true), 50); + } + } + +} \ No newline at end of file diff --git a/src/classes/ForPeriodCalcSnapshotBatch.cls-meta.xml b/src/classes/ForPeriodCalcSnapshotBatch.cls-meta.xml new file mode 100644 index 00000000..3e5b29c4 --- /dev/null +++ b/src/classes/ForPeriodCalcSnapshotBatch.cls-meta.xml @@ -0,0 +1,4 @@ + + + 36.0 + diff --git a/src/classes/GSRevenueCalculationBatch.cls b/src/classes/GSRevenueCalculationBatch.cls new file mode 100644 index 00000000..3168f997 --- /dev/null +++ b/src/classes/GSRevenueCalculationBatch.cls @@ -0,0 +1,47 @@ +/** + * Calculates GS revenue for Dimagi Contracts. It calcualtes it for one Business Unit. + */ +public without sharing class GSRevenueCalculationBatch implements Database.Batchable, Database.Stateful{ + + private List businessUnits; + private Business_Unit__c currentUnit; + private Double gsRevenue; + + public GSRevenueCalculationBatch(List businessUnits) { + this.businessUnits = businessUnits; + if (this.businessUnits.size() > 0) { + this.currentUnit = this.businessUnits.get(0); + this.businessUnits.remove(0); + } + } + + public List start(Database.BatchableContext BC) { + if (this.currentUnit != null) { + return [SELECT Id, GS_Execution_Budget__c, Percent_through_SOW_deliverables__c FROM DContract__c ]; + } + return new List(); + } + + public void execute(Database.BatchableContext BC, List scope) { + Map workMap = new Map(); // TODO Fetch 2 months history + for (DContract__c con : scope) { + Double val = workMap.get(con.Id); + if (val = null) { + val = con.Percent_through_SOW_deliverables__c; + } + this.gsRevenue += (val != null ? val / 100.00 : 0) * (con.GS_Execution_Budget__c != null ? con.GS_Execution_Budget__c : 0); + } + } + + public void finish(Database.BatchableContext BC) { + // TODO update contracts !!! + if (this.businessUnits.size() > 0) { + // TODO Calculate for other BU + //Database.executeBatch(new BatchCalculateEVDiff(Date.today().toStartOfMonth().addDays(-1), false)); + } + } + + private Map getHistoryMap() { + return null; + } +} \ No newline at end of file diff --git a/src/classes/GSRevenueCalculationBatch.cls-meta.xml b/src/classes/GSRevenueCalculationBatch.cls-meta.xml new file mode 100644 index 00000000..3e5b29c4 --- /dev/null +++ b/src/classes/GSRevenueCalculationBatch.cls-meta.xml @@ -0,0 +1,4 @@ + + + 36.0 + diff --git a/src/classes/ProjectReportOutController.cls b/src/classes/ProjectReportOutController.cls index 6daa5cd0..bc93f741 100644 --- a/src/classes/ProjectReportOutController.cls +++ b/src/classes/ProjectReportOutController.cls @@ -230,8 +230,8 @@ public with sharing class ProjectReportOutController { Project_Report_Out__c proToUpdate = createPROToUpdate(proList.isEmpty() ? null : proList[0]); DContract__c dcontract = [SELECT Id, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c, - GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c, GS_Services_Completed_Handover__c - FROM DContract__c WHERE Id =: reportContractId]; + GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c, GS_Services_Completed_Handover__c, + GS_Revenue__c FROM DContract__c WHERE Id =: reportContractId]; dcontract.Last_Report_Out_Status__c = status; dcontract.Percent_through_SOW_deliverables__c = proTabContractPercntgethroughSOW; diff --git a/src/classes/SnapshotHelper.cls b/src/classes/SnapshotHelper.cls index 6d69aba0..77ce04da 100644 --- a/src/classes/SnapshotHelper.cls +++ b/src/classes/SnapshotHelper.cls @@ -49,6 +49,7 @@ public class SnapshotHelper { proContractSnapshot.GS_Services_Completed__c = dcontract.GS_Services_Completed__c; proContractSnapshot.GS_Net_Income_to_Date__c = dcontract.GS_Net_Income_to_Date__c; proContractSnapshot.GS_Net_Income_to_Date_Handover__c = dcontract.GS_Net_Income_to_Date_Handover__c; + proContractSnapshot.GS_Revenue__c = dcontract.GS_Revenue__c; proContractSnapshot.Work_Completed_for_Period__c = (dcontract.Percent_through_SOW_deliverables__c != null ? dcontract.Percent_through_SOW_deliverables__c : 0) - (oldSnapshot.Percent_Of_Work_Completed__c != null ? oldSnapshot.Percent_Of_Work_Completed__c : 0); proContractSnapshot.Services_Spend_for_Period__c = (dcontract.Services_Spend__c != null ? dcontract.Services_Spend__c : 0) - (oldSnapshot.Services_Spend__c != null ? oldSnapshot.Services_Spend__c : 0); @@ -62,4 +63,50 @@ public class SnapshotHelper { return proContractSnapshot; } + + public static Map getOldSnapshots(List snapshots, Boolean forTotal) { + Set contracts = new Set(); + for (Pipeline_Snapshot__c sn : snapshots) { + if (sn.Contract__c != null) { + contracts.add(sn.Contract__c); + } + } + + Date monthDate = snapshots.get(0).Month_Date__c; + List oldSnapshots; + if (forTotal) { + oldSnapshots = [SELECT Id, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, + GS_Services_Completed_Handover__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM Pipeline_Snapshot__c + WHERE Contract__c IN: contracts AND Month_Date__c < :monthDate AND Month_Date__c > :monthDate.addMonths(-6) + AND Stage__c = 'Total' ORDER BY Month_Date__c DESC]; + } else { + oldSnapshots = [SELECT Id, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, + GS_Services_Completed_Handover__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM Pipeline_Snapshot__c + WHERE Contract__c IN: contracts AND Month_Date__c < :monthDate AND Month_Date__c > :monthDate.addMonths(-6) + AND Stage__c != 'Total' ORDER BY Month_Date__c DESC]; + } + + Map oldSnapShotsMap = new Map(); + for(Pipeline_Snapshot__c snapshotObject : oldSnapShotsList) { + if (!oldSnapShotsMap.containsKey(snapshotObject.Contract__c)) { + oldSnapShotsMap.put(snapshotObject.Contract__c, snapshotObject); + } + } + + return oldSnapShotsMap; + } + + public static Pipeline_Snapshot__c calculateForPeriod(Pipeline_Snapshot__c sn, Pipeline_Snapshot__c oldSn) { + sn.Work_Completed_for_Period__c = (sn.Work_Completed__c != null ? sn.Work_Completed__c : 0) - (oldSn.Work_Completed__c != null ? oldSn.Work_Completed__c : 0); + sn.Services_Spend_for_Period__c = (sn.Services_Spend__c != null ? sn.Services_Spend__c : 0) - (oldSn.Services_Spend__c != null ? oldSn.Services_Spend__c : 0); + sn.Expense_Travel_Spend_for_Period__c = (sn.Expense_Travel_Spend__c != null ? sn.Expense_Travel_Spend__c : 0) - (oldSn.Expense_Travel_Spend__c != null ? oldSn.Expense_Travel_Spend__c : 0); + sn.Product_Spend_for_Period__c = (sn.Product_Spend__c != null ? sn.Product_Spend__c : 0) - (oldSn.Product_Spend__c != null ? oldSn.Product_Spend__c : 0); + sn.Total_Calculated_Costs_for_Period__cd = (sn.Total_Calculated_Costs__c != null ? sn.Total_Calculated_Costs__c : 0) - (oldSn.Total_Calculated_Costs__c != null ? oldSn.Total_Calculated_Costs__c : 0); + sn.GS_Services_Completed_for_Period__c = (sn.GS_Services_Completed__c != null ? sn.GS_Services_Completed__c : 0) - (oldSn.GS_Services_Completed__c != null ? oldSn.GS_Services_Completed__c : 0); + sn.GS_Services_Handover_Completed_for_Per__c = (sn.GS_Services_Completed_Handover__c != null ? sn.GS_Services_Completed_Handover__c : 0) - (oldSn.GS_Services_Completed_Handover__c != null ? oldSn.GS_Services_Completed_Handover__c : 0); + sn.GS_Net_Income_for_Period__c = (sn.GS_Net_Income_to_Date__c != null ? sn.GS_Net_Income_to_Date__c : 0) - (oldSn.GS_Net_Income_to_Date__c != null ? oldSn.GS_Net_Income_to_Date__c : 0); + sn.GS_Net_Income_for_Period_by_Handover__c = (sn.GS_Net_Income_to_Date_Handover__c != null ? sn.GS_Net_Income_to_Date_Handover__c : 0) - (oldSn.GS_Net_Income_to_Date_Handover__c != null ? oldSn.GS_Net_Income_to_Date_Handover__c : 0); + + return sn; + } } \ No newline at end of file From 894c52ef15e31abe969487962c3a52b55b1e94b8 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Mon, 23 Jan 2017 15:32:36 +0100 Subject: [PATCH 04/15] For Period Snapshot Calculations and Test --- src/classes/ContractSnapshotTest.cls | 48 +++++++++++++++++-- src/classes/ForPeriodCalcSnapshotBatch.cls | 29 ++++++----- .../ForPeriodCalcSnapshotBatch.cls-meta.xml | 3 +- src/classes/PROContractSnapshotBatch.cls | 6 +-- src/classes/SnapshotHelper.cls | 46 ++++++++++-------- 5 files changed, 91 insertions(+), 41 deletions(-) diff --git a/src/classes/ContractSnapshotTest.cls b/src/classes/ContractSnapshotTest.cls index 4e766fc9..71eb9d48 100644 --- a/src/classes/ContractSnapshotTest.cls +++ b/src/classes/ContractSnapshotTest.cls @@ -1,4 +1,4 @@ -@isTest(SeeAllData=true) +@isTest public class ContractSnapshotTest { static testMethod void shouldCreateSnapshotAfterSubmitingPRO() { @@ -36,6 +36,11 @@ public class ContractSnapshotTest { static testMethod void jobShouldCreateSnapshotForActiveProject() { DContract__c dcontract = addContract(); + PRO_Contract_Snapshot__c proSn1 = new PRO_Contract_Snapshot__c(Contract__c = dcontract.Id, Percent_Of_Work_Completed__c = 4, Snapshot_Date__c = Date.today().addDays(-40)); + insert proSn1; + PRO_Contract_Snapshot__c proSn2 = new PRO_Contract_Snapshot__c(Contract__c = dcontract.Id, Percent_Of_Work_Completed__c = 11, Snapshot_Date__c = Date.today().addDays(-20)); + insert proSn2; + PROContractSnapshotBatch proContractSnapshotBatch = new PROContractSnapshotBatch(); Database.BatchableContext bc; proContractSnapshotBatch.execute(bc, proContractSnapshotBatch.start(bc)); @@ -43,13 +48,50 @@ public class ContractSnapshotTest { List snapshots = [SELECT Id, Name, Percent_Of_Work_Completed__c, Percent_Services_Budget_For_GS__c, Expected_Buffer__c, Expense_And_Travel_Spend__c, Services_Spend__c, Total_Calculated_Costs__c, Product_Spend__c, Net_Income_By_Deliverables_Complete__c, - Execution_Efficiency__c, Buffer_By_Deliverables_Complete__c, Budget_Used__c - FROM PRO_Contract_Snapshot__c WHERE Contract__c =: dcontract.Id]; + Execution_Efficiency__c, Buffer_By_Deliverables_Complete__c, Budget_Used__c, Work_Completed_for_Period__c + FROM PRO_Contract_Snapshot__c WHERE Contract__c =: dcontract.Id AND Id !=: proSn1.Id AND Id !=: proSn2.Id]; System.assertEquals(1, snapshots.size()); PRO_Contract_Snapshot__c snapshot = snapshots.get(0); assertSnapshotData(snapshot); + System.assertEquals(19, snapshot.Work_Completed_for_Period__c); + } + + static testMethod void shouldCalculateForPeriodInPipelineSnapshot() { + DContract__c dcontract1 = new DContract__c(Name = 'Sample Test 1', Services_Spend__c = 100, Total_Amount_of_Contract__c = 1000); + DContract__c dcontract2 = new DContract__c(Name = 'Sample Test 2', Services_Spend__c = 155, Total_Amount_of_Contract__c = 5500); + DContract__c dcontract3 = new DContract__c(Name = 'Sample Test 3', Services_Spend__c = 23, Total_Amount_of_Contract__c = 222); + insert dcontract1; + insert dcontract2; + insert dcontract3; + + Pipeline_Snapshot__c ps1 = new Pipeline_Snapshot__c(Source_Type__c = 'Contract', Month_Date__c = Date.today().addDays(-10), Contract__c = dcontract1.Id, Services_Spend__c = 10); + Pipeline_Snapshot__c ps2 = new Pipeline_Snapshot__c(Source_Type__c = 'Contract', Month_Date__c = Date.today(), Contract__c = dcontract1.Id, Services_Spend__c = 76); + Pipeline_Snapshot__c ps3 = new Pipeline_Snapshot__c(Source_Type__c = 'Contract', Month_Date__c = Date.today(), Contract__c = dcontract3.Id, Services_Spend__c = 100); + Pipeline_Snapshot__c ps4 = new Pipeline_Snapshot__c(Source_Type__c = 'Contract', Month_Date__c = Date.today().addDays(-50), Contract__c = dcontract2.Id, Services_Spend__c = 1); + Pipeline_Snapshot__c ps5 = new Pipeline_Snapshot__c(Source_Type__c = 'Contract', Month_Date__c = Date.today().addDays(-30), Contract__c = dcontract2.Id, Services_Spend__c = 8); + Pipeline_Snapshot__c ps6 = new Pipeline_Snapshot__c(Source_Type__c = 'Contract', Month_Date__c = Date.today(), Contract__c = dcontract2.Id, Services_Spend__c = 11); + insert ps1; + insert ps2; + insert ps3; + insert ps4; + insert ps5; + insert ps6; + Test.setCreatedDate(ps1.Id, DateTime.now().addDays(-6)); + Test.setCreatedDate(ps4.Id, DateTime.now().addDays(-6)); + Test.setCreatedDate(ps5.Id, DateTime.now().addDays(-6)); + + Test.startTest(); + Database.executeBatch(new ForPeriodCalcSnapshotBatch(false), 50); + Test.stopTest(); + + Pipeline_Snapshot__c psFromDB = [SELECT Id, Services_Spend_for_Period__c FROM Pipeline_Snapshot__c WHERE Id =: ps2.Id]; + System.assertEquals(66, psFromDB.Services_Spend_for_Period__c); + psFromDB = [SELECT Id, Services_Spend_for_Period__c FROM Pipeline_Snapshot__c WHERE Id =: ps4.Id]; + System.assertEquals(null, psFromDB.Services_Spend_for_Period__c); + psFromDB = [SELECT Id, Services_Spend_for_Period__c FROM Pipeline_Snapshot__c WHERE Id =: ps6.Id]; + System.assertEquals(3, psFromDB.Services_Spend_for_Period__c); } static private void assertSnapshotData(PRO_Contract_Snapshot__c snapshot) { diff --git a/src/classes/ForPeriodCalcSnapshotBatch.cls b/src/classes/ForPeriodCalcSnapshotBatch.cls index 1ca59f75..3669b508 100644 --- a/src/classes/ForPeriodCalcSnapshotBatch.cls +++ b/src/classes/ForPeriodCalcSnapshotBatch.cls @@ -3,37 +3,44 @@ */ public class ForPeriodCalcSnapshotBatch implements Database.Batchable, Database.Stateful { + private Boolean forTotal; + public ForPeriodCalcSnapshotBatch(Boolean forTotal){ this.forTotal = forTotal; } public List start(Database.BatchableContext context) { + List snaps = new List(); if (this.forTotal == false) { - return [SELECT Id, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, + snaps = [SELECT Id, Contract__c, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, + GS_Services_Completed_Handover__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c, + Work_Completed_for_Period__c, Expense_Travel_Spend_for_Period__c, GS_Net_Income_for_Period__c, GS_Net_Income_for_Period_by_Handover__c, + GS_Services_Handover_Completed_for_Per__c, GS_Services_Completed_for_Period__c, Product_Spend_for_Period__c, Services_Spend_for_Period__c, + Total_Calculated_Costs_for_Period__c, Month_Date__c + FROM Pipeline_Snapshot__c WHERE CreatedDate >= YESTERDAY AND Stage__c != 'Total' AND Source_Type__c = 'Contract']; + } else { + snaps = [SELECT Id, Contract__c, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, GS_Services_Completed_Handover__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c, Work_Completed_for_Period__c, Expense_Travel_Spend_for_Period__c, GS_Net_Income_for_Period__c, GS_Net_Income_for_Period_by_Handover__c, GS_Services_Handover_Completed_for_Per__c, GS_Services_Completed_for_Period__c, Product_Spend_for_Period__c, Services_Spend_for_Period__c, Total_Calculated_Costs_for_Period__c, Month_Date__c - FROM Pipeline_Snapshot__c WHERE CreatedDate >= YESTERDAY AND Current_Stage__c != 'Total' AND Source_Type__c = 'Contract']; + FROM Pipeline_Snapshot__c WHERE CreatedDate >= YESTERDAY AND Stage__c = 'Total' AND Source_Type__c = 'Contract']; } - return [SELECT Id, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, - GS_Services_Completed_Handover__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c, - Work_Completed_for_Period__c, Expense_Travel_Spend_for_Period__c, GS_Net_Income_for_Period__c, GS_Net_Income_for_Period_by_Handover__c, - GS_Services_Handover_Completed_for_Per__c, GS_Services_Completed_for_Period__c, Product_Spend_for_Period__c, Services_Spend_for_Period__c, - Total_Calculated_Costs_for_Period__c, Month_Date__c - FROM Pipeline_Snapshot__c WHERE CreatedDate >= YESTERDAY AND Current_Stage__c = 'Total' AND Source_Type__c = 'Contract']; + System.debug('snaps ' + snaps.size()); + return snaps; } public void execute(Database.BatchableContext context, List snapshots) { + System.debug('snapshots: ' + snapshots.size()); List toUpdate = new List(); try { - Map oldSnapshots = SnapshotHelper.getOldSnapshots(snapshots); + Map oldSnapshots = SnapshotHelper.getOldSnapshots(snapshots, forTotal); for (Pipeline_Snapshot__c sn : snapshots) { Pipeline_Snapshot__c oldSn = new Pipeline_Snapshot__c(); if (sn.Contract__c != null && oldSnapshots.containsKey(sn.Contract__c)) { oldSn = oldSnapshots.get(sn.Contract__c); } - sn = SnapshotHelper.calculateForPeriod(sn, oldSnapshots); + sn = SnapshotHelper.calculateForPeriod(sn, oldSn); toUpdate.add(sn); } update toUpdate; @@ -48,7 +55,7 @@ public class ForPeriodCalcSnapshotBatch implements Database.Batchable + 36.0 + Active diff --git a/src/classes/PROContractSnapshotBatch.cls b/src/classes/PROContractSnapshotBatch.cls index ad80df63..6ea3effb 100644 --- a/src/classes/PROContractSnapshotBatch.cls +++ b/src/classes/PROContractSnapshotBatch.cls @@ -26,10 +26,6 @@ global class PROContractSnapshotBatch implements Database.Batchable GetContract() { - List contractList = [SELECT Id, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, + List contractList = [SELECT Id, GS_Revenue__c, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c, GS_Net_Income_to_Date_Handover__c, GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Services_Completed_Handover__c FROM DContract__c WHERE Status__c = 'Active' AND Requires_Report_Out__c = 'No']; diff --git a/src/classes/SnapshotHelper.cls b/src/classes/SnapshotHelper.cls index 77ce04da..d1754b44 100644 --- a/src/classes/SnapshotHelper.cls +++ b/src/classes/SnapshotHelper.cls @@ -51,15 +51,17 @@ public class SnapshotHelper { proContractSnapshot.GS_Net_Income_to_Date_Handover__c = dcontract.GS_Net_Income_to_Date_Handover__c; proContractSnapshot.GS_Revenue__c = dcontract.GS_Revenue__c; - proContractSnapshot.Work_Completed_for_Period__c = (dcontract.Percent_through_SOW_deliverables__c != null ? dcontract.Percent_through_SOW_deliverables__c : 0) - (oldSnapshot.Percent_Of_Work_Completed__c != null ? oldSnapshot.Percent_Of_Work_Completed__c : 0); - proContractSnapshot.Services_Spend_for_Period__c = (dcontract.Services_Spend__c != null ? dcontract.Services_Spend__c : 0) - (oldSnapshot.Services_Spend__c != null ? oldSnapshot.Services_Spend__c : 0); - proContractSnapshot.Expense_Travel_Spend_for_Period__c = (dcontract.Direct_Costs_Total_Expenses__c != null ? dcontract.Direct_Costs_Total_Expenses__c : 0) - (oldSnapshot.Expense_And_Travel_Spend__c != null ? oldSnapshot.Expense_And_Travel_Spend__c : 0); - proContractSnapshot.Product_Spend_for_Period__c = (dcontract.Product_Spend__c != null ? dcontract.Product_Spend__c : 0) - (oldSnapshot.Product_Spend__c != null ? oldSnapshot.Product_Spend__c : 0); - proContractSnapshot.Total_Calculated_Costs_for_Period__c = (dcontract.Total_Costs__c != null ? dcontract.Total_Costs__c : 0) - (oldSnapshot.Total_Calculated_Costs__c != null ? oldSnapshot.Total_Calculated_Costs__c : 0); - proContractSnapshot.GS_Services_Handover_Completed_for_Per__c = (dcontract.GS_Services_Completed_Handover__c != null ? dcontract.GS_Services_Completed_Handover__c : 0) - (oldSnapshot.GS_Services_Completed_Handover__c != null ? oldSnapshot.GS_Services_Completed_Handover__c : 0); - proContractSnapshot.GS_Services_Completed_for_Period__c = (dcontract.GS_Services_Completed__c != null ? dcontract.GS_Services_Completed__c : 0) - (oldSnapshot.GS_Services_Completed__c != null ? oldSnapshot.GS_Services_Completed__c : 0); - proContractSnapshot.GS_Net_Income_for_Period__c = (dcontract.GS_Net_Income_to_Date__c != null ? dcontract.GS_Net_Income_to_Date__c : 0) - (oldSnapshot.GS_Net_Income_to_Date__c != null ? oldSnapshot.GS_Net_Income_to_Date__c : 0); - proContractSnapshot.GS_Net_Income_for_Period_by_Handover__c = (dcontract.GS_Net_Income_to_Date_Handover__c != null ? dcontract.GS_Net_Income_to_Date_Handover__c : 0) - (oldSnapshot.GS_Net_Income_to_Date_Handover__c != null ? oldSnapshot.GS_Net_Income_to_Date_Handover__c : 0); + if (oldSnapshot.Id != null) { + proContractSnapshot.Work_Completed_for_Period__c = (dcontract.Percent_through_SOW_deliverables__c != null ? dcontract.Percent_through_SOW_deliverables__c : 0) - (oldSnapshot.Percent_Of_Work_Completed__c != null ? oldSnapshot.Percent_Of_Work_Completed__c : 0); + proContractSnapshot.Services_Spend_for_Period__c = (dcontract.Services_Spend__c != null ? dcontract.Services_Spend__c : 0) - (oldSnapshot.Services_Spend__c != null ? oldSnapshot.Services_Spend__c : 0); + proContractSnapshot.Expense_Travel_Spend_for_Period__c = (dcontract.Direct_Costs_Total_Expenses__c != null ? dcontract.Direct_Costs_Total_Expenses__c : 0) - (oldSnapshot.Expense_And_Travel_Spend__c != null ? oldSnapshot.Expense_And_Travel_Spend__c : 0); + proContractSnapshot.Product_Spend_for_Period__c = (dcontract.Product_Spend__c != null ? dcontract.Product_Spend__c : 0) - (oldSnapshot.Product_Spend__c != null ? oldSnapshot.Product_Spend__c : 0); + proContractSnapshot.Total_Calculated_Costs_for_Period__c = (dcontract.Total_Costs__c != null ? dcontract.Total_Costs__c : 0) - (oldSnapshot.Total_Calculated_Costs__c != null ? oldSnapshot.Total_Calculated_Costs__c : 0); + proContractSnapshot.GS_Services_Handover_Completed_for_Per__c = (dcontract.GS_Services_Completed_Handover__c != null ? dcontract.GS_Services_Completed_Handover__c : 0) - (oldSnapshot.GS_Services_Completed_Handover__c != null ? oldSnapshot.GS_Services_Completed_Handover__c : 0); + proContractSnapshot.GS_Services_Completed_for_Period__c = (dcontract.GS_Services_Completed__c != null ? dcontract.GS_Services_Completed__c : 0) - (oldSnapshot.GS_Services_Completed__c != null ? oldSnapshot.GS_Services_Completed__c : 0); + proContractSnapshot.GS_Net_Income_for_Period__c = (dcontract.GS_Net_Income_to_Date__c != null ? dcontract.GS_Net_Income_to_Date__c : 0) - (oldSnapshot.GS_Net_Income_to_Date__c != null ? oldSnapshot.GS_Net_Income_to_Date__c : 0); + proContractSnapshot.GS_Net_Income_for_Period_by_Handover__c = (dcontract.GS_Net_Income_to_Date_Handover__c != null ? dcontract.GS_Net_Income_to_Date_Handover__c : 0) - (oldSnapshot.GS_Net_Income_to_Date_Handover__c != null ? oldSnapshot.GS_Net_Income_to_Date_Handover__c : 0); + } return proContractSnapshot; } @@ -75,19 +77,19 @@ public class SnapshotHelper { Date monthDate = snapshots.get(0).Month_Date__c; List oldSnapshots; if (forTotal) { - oldSnapshots = [SELECT Id, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, + oldSnapshots = [SELECT Id, Contract__c, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, GS_Services_Completed_Handover__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM Pipeline_Snapshot__c WHERE Contract__c IN: contracts AND Month_Date__c < :monthDate AND Month_Date__c > :monthDate.addMonths(-6) AND Stage__c = 'Total' ORDER BY Month_Date__c DESC]; } else { - oldSnapshots = [SELECT Id, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, + oldSnapshots = [SELECT Id, Contract__c, Work_Completed__c, Services_Spend__c, Expense_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed__c, GS_Services_Completed_Handover__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM Pipeline_Snapshot__c WHERE Contract__c IN: contracts AND Month_Date__c < :monthDate AND Month_Date__c > :monthDate.addMonths(-6) AND Stage__c != 'Total' ORDER BY Month_Date__c DESC]; } Map oldSnapShotsMap = new Map(); - for(Pipeline_Snapshot__c snapshotObject : oldSnapShotsList) { + for(Pipeline_Snapshot__c snapshotObject : oldSnapshots) { if (!oldSnapShotsMap.containsKey(snapshotObject.Contract__c)) { oldSnapShotsMap.put(snapshotObject.Contract__c, snapshotObject); } @@ -97,15 +99,17 @@ public class SnapshotHelper { } public static Pipeline_Snapshot__c calculateForPeriod(Pipeline_Snapshot__c sn, Pipeline_Snapshot__c oldSn) { - sn.Work_Completed_for_Period__c = (sn.Work_Completed__c != null ? sn.Work_Completed__c : 0) - (oldSn.Work_Completed__c != null ? oldSn.Work_Completed__c : 0); - sn.Services_Spend_for_Period__c = (sn.Services_Spend__c != null ? sn.Services_Spend__c : 0) - (oldSn.Services_Spend__c != null ? oldSn.Services_Spend__c : 0); - sn.Expense_Travel_Spend_for_Period__c = (sn.Expense_Travel_Spend__c != null ? sn.Expense_Travel_Spend__c : 0) - (oldSn.Expense_Travel_Spend__c != null ? oldSn.Expense_Travel_Spend__c : 0); - sn.Product_Spend_for_Period__c = (sn.Product_Spend__c != null ? sn.Product_Spend__c : 0) - (oldSn.Product_Spend__c != null ? oldSn.Product_Spend__c : 0); - sn.Total_Calculated_Costs_for_Period__cd = (sn.Total_Calculated_Costs__c != null ? sn.Total_Calculated_Costs__c : 0) - (oldSn.Total_Calculated_Costs__c != null ? oldSn.Total_Calculated_Costs__c : 0); - sn.GS_Services_Completed_for_Period__c = (sn.GS_Services_Completed__c != null ? sn.GS_Services_Completed__c : 0) - (oldSn.GS_Services_Completed__c != null ? oldSn.GS_Services_Completed__c : 0); - sn.GS_Services_Handover_Completed_for_Per__c = (sn.GS_Services_Completed_Handover__c != null ? sn.GS_Services_Completed_Handover__c : 0) - (oldSn.GS_Services_Completed_Handover__c != null ? oldSn.GS_Services_Completed_Handover__c : 0); - sn.GS_Net_Income_for_Period__c = (sn.GS_Net_Income_to_Date__c != null ? sn.GS_Net_Income_to_Date__c : 0) - (oldSn.GS_Net_Income_to_Date__c != null ? oldSn.GS_Net_Income_to_Date__c : 0); - sn.GS_Net_Income_for_Period_by_Handover__c = (sn.GS_Net_Income_to_Date_Handover__c != null ? sn.GS_Net_Income_to_Date_Handover__c : 0) - (oldSn.GS_Net_Income_to_Date_Handover__c != null ? oldSn.GS_Net_Income_to_Date_Handover__c : 0); + if (oldSn.Id != null) { + sn.Work_Completed_for_Period__c = (sn.Work_Completed__c != null ? sn.Work_Completed__c : 0) - (oldSn.Work_Completed__c != null ? oldSn.Work_Completed__c : 0); + sn.Services_Spend_for_Period__c = (sn.Services_Spend__c != null ? sn.Services_Spend__c : 0) - (oldSn.Services_Spend__c != null ? oldSn.Services_Spend__c : 0); + sn.Expense_Travel_Spend_for_Period__c = (sn.Expense_Travel_Spend__c != null ? sn.Expense_Travel_Spend__c : 0) - (oldSn.Expense_Travel_Spend__c != null ? oldSn.Expense_Travel_Spend__c : 0); + sn.Product_Spend_for_Period__c = (sn.Product_Spend__c != null ? sn.Product_Spend__c : 0) - (oldSn.Product_Spend__c != null ? oldSn.Product_Spend__c : 0); + sn.Total_Calculated_Costs_for_Period__c = (sn.Total_Calculated_Costs__c != null ? sn.Total_Calculated_Costs__c : 0) - (oldSn.Total_Calculated_Costs__c != null ? oldSn.Total_Calculated_Costs__c : 0); + sn.GS_Services_Completed_for_Period__c = (sn.GS_Services_Completed__c != null ? sn.GS_Services_Completed__c : 0) - (oldSn.GS_Services_Completed__c != null ? oldSn.GS_Services_Completed__c : 0); + sn.GS_Services_Handover_Completed_for_Per__c = (sn.GS_Services_Completed_Handover__c != null ? sn.GS_Services_Completed_Handover__c : 0) - (oldSn.GS_Services_Completed_Handover__c != null ? oldSn.GS_Services_Completed_Handover__c : 0); + sn.GS_Net_Income_for_Period__c = (sn.GS_Net_Income_to_Date__c != null ? sn.GS_Net_Income_to_Date__c : 0) - (oldSn.GS_Net_Income_to_Date__c != null ? oldSn.GS_Net_Income_to_Date__c : 0); + sn.GS_Net_Income_for_Period_by_Handover__c = (sn.GS_Net_Income_to_Date_Handover__c != null ? sn.GS_Net_Income_to_Date_Handover__c : 0) - (oldSn.GS_Net_Income_to_Date_Handover__c != null ? oldSn.GS_Net_Income_to_Date_Handover__c : 0); + } return sn; } From 87c0f3c481bdcc2c9809ae5d87ad5790912f292e Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Tue, 24 Jan 2017 13:40:52 +0100 Subject: [PATCH 05/15] Added test for GS Revenue calculation --- src/classes/ContractSnapshotTest.cls | 31 +++++++ src/classes/GSRevenueCalculationBatch.cls | 81 ++++++++++++++++--- .../GSRevenueCalculationBatch.cls-meta.xml | 3 +- 3 files changed, 102 insertions(+), 13 deletions(-) diff --git a/src/classes/ContractSnapshotTest.cls b/src/classes/ContractSnapshotTest.cls index 71eb9d48..4bb0bf34 100644 --- a/src/classes/ContractSnapshotTest.cls +++ b/src/classes/ContractSnapshotTest.cls @@ -94,6 +94,37 @@ public class ContractSnapshotTest { System.assertEquals(3, psFromDB.Services_Spend_for_Period__c); } + static testMethod void shouldCalculateGSRevenue() { + Business_Unit__c bu = new Business_Unit__c(Name = 'Inc'); + insert bu; + + DContract__c dcontract1 = new DContract__c(Name = 'Sample Test 1', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 10, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 1000); // 5 + DContract__c dcontract2 = new DContract__c(Name = 'Sample Test 2', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 20, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 2000); // 30 + DContract__c dcontract3 = new DContract__c(Name = 'Sample Test 3', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 30, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 3000); // 22 + DContract__c dcontract4 = new DContract__c(Name = 'Sample Test 4', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 40, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 4000); // 40 + insert dcontract1; + insert dcontract2; + insert dcontract3; + insert dcontract4; + + Test.startTest(); + Database.executeBatch(new GSRevenueCalculationBatch([SELECT Id FROM Business_Unit__c])); + Test.stopTest(); + + DContract__c conFromDB = [SELECT Id, GS_Revenue__c FROM DContract__c WHERE Id =: dcontract1.Id LIMIT 1]; + System.assertEquals(2910, conFromDB.GS_Revenue__c); + conFromDB = [SELECT Id, GS_Revenue__c FROM DContract__c WHERE Id =: dcontract2.Id LIMIT 1]; + System.assertEquals(2910, conFromDB.GS_Revenue__c); + conFromDB = [SELECT Id, GS_Revenue__c FROM DContract__c WHERE Id =: dcontract3.Id LIMIT 1]; + System.assertEquals(2910, conFromDB.GS_Revenue__c); + conFromDB = [SELECT Id, GS_Revenue__c FROM DContract__c WHERE Id =: dcontract4.Id LIMIT 1]; + System.assertEquals(2910, conFromDB.GS_Revenue__c); + } + static private void assertSnapshotData(PRO_Contract_Snapshot__c snapshot) { System.assertEquals('Sample Test Name', snapshot.Name); System.assertEquals(30, snapshot.Percent_Of_Work_Completed__c); diff --git a/src/classes/GSRevenueCalculationBatch.cls b/src/classes/GSRevenueCalculationBatch.cls index 3168f997..447e2b41 100644 --- a/src/classes/GSRevenueCalculationBatch.cls +++ b/src/classes/GSRevenueCalculationBatch.cls @@ -6,6 +6,7 @@ public without sharing class GSRevenueCalculationBatch implements Database.Batch private List businessUnits; private Business_Unit__c currentUnit; private Double gsRevenue; + private List contracts; public GSRevenueCalculationBatch(List businessUnits) { this.businessUnits = businessUnits; @@ -13,35 +14,91 @@ public without sharing class GSRevenueCalculationBatch implements Database.Batch this.currentUnit = this.businessUnits.get(0); this.businessUnits.remove(0); } + this.gsRevenue = 0; + this.contracts = new List(); } public List start(Database.BatchableContext BC) { if (this.currentUnit != null) { - return [SELECT Id, GS_Execution_Budget__c, Percent_through_SOW_deliverables__c FROM DContract__c ]; + return [SELECT Id, Name, GS_Execution_Budget__c, Percent_through_SOW_deliverables__c, GS_Revenue__c FROM DContract__c WHERE Prime_Contracting_Business_Unit__c =: this.currentUnit.Id]; } return new List(); } public void execute(Database.BatchableContext BC, List scope) { - Map workMap = new Map(); // TODO Fetch 2 months history - for (DContract__c con : scope) { - Double val = workMap.get(con.Id); - if (val = null) { - val = con.Percent_through_SOW_deliverables__c; + try { + Map workMap = getHistoryMap(scope); + for (DContract__c con : scope) { + Decimal val = workMap.get(con.Id); + if (val == null) { + val = con.Percent_through_SOW_deliverables__c; + } + this.gsRevenue += (val != null ? val / 100.00 : 0) * (con.GS_Execution_Budget__c != null ? con.GS_Execution_Budget__c : 0); + this.contracts.add(con); + } + } catch (Exception ex) { + System.debug('Error : ' + ex); + if (!Test.isRunningTest()) { + BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); + EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'GS Revenue calculation error', 'GS Revenue calculation error', ex); } - this.gsRevenue += (val != null ? val / 100.00 : 0) * (con.GS_Execution_Budget__c != null ? con.GS_Execution_Budget__c : 0); } } public void finish(Database.BatchableContext BC) { - // TODO update contracts !!! + try { + List toUpdate = new List(); + for (DContract__c con : this.contracts) { + if (this.gsRevenue != con.GS_Revenue__c) { + con.GS_Revenue__c = this.gsRevenue; + toUpdate.add(con); + } + } + update toUpdate; + } catch (Exception ex) { + System.debug('Error : ' + ex); + if (!Test.isRunningTest()) { + BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); + EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'GS Revenue calculation error', 'GS Revenue calculation error', ex); + } + } + if (this.businessUnits.size() > 0) { - // TODO Calculate for other BU - //Database.executeBatch(new BatchCalculateEVDiff(Date.today().toStartOfMonth().addDays(-1), false)); + Database.executeBatch(new GSRevenueCalculationBatch(this.businessUnits)); } } - private Map getHistoryMap() { - return null; + private Map getHistoryMap(List scope) { + if (Test.isRunningTest()) { + return getTestDate(scope); + } + + List historyList = [SELECT OldValue, parentId, CreatedDate FROM DContract__History WHERE parentId IN: scope AND Field = 'Percent_through_SOW_deliverables__c' + AND CreatedDate = THIS_MONTH ORDER BY CreatedDate ASC]; + Map historyMap = new Map (); + + for (DContract__History hRec : historyList) { + if (!historyMap.containsKey(hRec.parentId)) { + historyMap.put(hRec.parentId, hRec.OldValue != null ? (Decimal) hRec.OldValue : 0); + } + } + + return historyMap; } + + private Map getTestDate(List scope) { + Map historyMap = new Map (); + for (DContract__c con : scope) { + if (con.Name == 'Sample Test 1') { + historyMap.put(con.Id, 5); + } else if (con.Name == 'Sample Test 2') { + historyMap.put(con.Id, 30); + } else if (con.Name == 'Sample Test 3') { + historyMap.put(con.Id, 22); + } + } + + return historyMap; + } + } \ No newline at end of file diff --git a/src/classes/GSRevenueCalculationBatch.cls-meta.xml b/src/classes/GSRevenueCalculationBatch.cls-meta.xml index 3e5b29c4..38aa015d 100644 --- a/src/classes/GSRevenueCalculationBatch.cls-meta.xml +++ b/src/classes/GSRevenueCalculationBatch.cls-meta.xml @@ -1,4 +1,5 @@ - + 36.0 + Active From b74fc1186ef8f8a9bde4e375d947c79f97a7ef9c Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Wed, 25 Jan 2017 08:33:33 +0100 Subject: [PATCH 06/15] Added scheduler --- src/classes/BatchCalculateEVDiffSch.cls | 2 +- src/classes/GSRevenueCalculationBatch.cls | 33 +++++++++++-------- src/classes/GSRevenueCalculationScheduler.cls | 6 ++++ ...GSRevenueCalculationScheduler.cls-meta.xml | 5 +++ 4 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 src/classes/GSRevenueCalculationScheduler.cls create mode 100644 src/classes/GSRevenueCalculationScheduler.cls-meta.xml diff --git a/src/classes/BatchCalculateEVDiffSch.cls b/src/classes/BatchCalculateEVDiffSch.cls index 841c3b72..22c874af 100644 --- a/src/classes/BatchCalculateEVDiffSch.cls +++ b/src/classes/BatchCalculateEVDiffSch.cls @@ -1,7 +1,7 @@ /** * Calculates NetDiff for Closed opps in last month (Pipeline Snapshot wasn't created). */ -public class BatchCalculateEVDiffSch implements Schedulable{ +public class BatchCalculateEVDiffSch implements Schedulable { public void execute(SchedulableContext sc) { Database.executeBatch(new BatchCalculateEVDiff(Date.today().toStartOfMonth().addDays(-1), false)); diff --git a/src/classes/GSRevenueCalculationBatch.cls b/src/classes/GSRevenueCalculationBatch.cls index 447e2b41..4ad0022e 100644 --- a/src/classes/GSRevenueCalculationBatch.cls +++ b/src/classes/GSRevenueCalculationBatch.cls @@ -46,23 +46,30 @@ public without sharing class GSRevenueCalculationBatch implements Database.Batch } public void finish(Database.BatchableContext BC) { - try { - List toUpdate = new List(); - for (DContract__c con : this.contracts) { - if (this.gsRevenue != con.GS_Revenue__c) { - con.GS_Revenue__c = this.gsRevenue; - toUpdate.add(con); - } + List toUpdate = new List(); + for (DContract__c con : this.contracts) { + if (this.gsRevenue != con.GS_Revenue__c) { + con.GS_Revenue__c = this.gsRevenue; + toUpdate.add(con); } - update toUpdate; - } catch (Exception ex) { - System.debug('Error : ' + ex); - if (!Test.isRunningTest()) { - BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); - EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'GS Revenue calculation error', 'GS Revenue calculation error', ex); + } + List updateResults = Database.update(toUpdate, false); + + String emailBody = ''; + for(Integer i = 0; i < updateResults.size(); i++){ + if (!updateResults.get(i).isSuccess()){ + Database.Error error = updateResults.get(i).getErrors().get(0); + System.debug('Error : Fields - ' + error.getFields() + ' Message - ' + error.getMessage()); + emailBody += '' + toUpdate.get(i).Id + '' + error.getFields()+ '' + error.getStatusCode()+ '' + error.getMessage() + ''; } } + if (emailBody != '') { + BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); + emailBody = 'td>Fieldstd>Code' + emailBody + '
IdError Message

'; + EmailHelper.sendEmail(settings.Error_Emails__c.split(','), 'GS Revenue calculation error', emailBody); + } + if (this.businessUnits.size() > 0) { Database.executeBatch(new GSRevenueCalculationBatch(this.businessUnits)); } diff --git a/src/classes/GSRevenueCalculationScheduler.cls b/src/classes/GSRevenueCalculationScheduler.cls new file mode 100644 index 00000000..eb8c05d8 --- /dev/null +++ b/src/classes/GSRevenueCalculationScheduler.cls @@ -0,0 +1,6 @@ +public without sharing class GSRevenueCalculationScheduler implements Schedulable { + + public void execute(SchedulableContext sc) { + Database.executeBatch(new GSRevenueCalculationBatch([SELECT Id FROM Business_Unit__c])); + } +} \ No newline at end of file diff --git a/src/classes/GSRevenueCalculationScheduler.cls-meta.xml b/src/classes/GSRevenueCalculationScheduler.cls-meta.xml new file mode 100644 index 00000000..38aa015d --- /dev/null +++ b/src/classes/GSRevenueCalculationScheduler.cls-meta.xml @@ -0,0 +1,5 @@ + + + 36.0 + Active + From 5f1528734069a5ce95db4400cd1b33d22f531734 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Wed, 25 Jan 2017 11:17:31 +0100 Subject: [PATCH 07/15] Set 0 for new snapshots --- src/classes/GSRevenueCalculationBatch.cls | 2 ++ src/classes/SnapshotHelper.cls | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/classes/GSRevenueCalculationBatch.cls b/src/classes/GSRevenueCalculationBatch.cls index 4ad0022e..fd46f262 100644 --- a/src/classes/GSRevenueCalculationBatch.cls +++ b/src/classes/GSRevenueCalculationBatch.cls @@ -72,6 +72,8 @@ public without sharing class GSRevenueCalculationBatch implements Database.Batch if (this.businessUnits.size() > 0) { Database.executeBatch(new GSRevenueCalculationBatch(this.businessUnits)); + } else { + Database.executeBatch(new ForPeriodCalcSnapshotBatch(false), 50); } } diff --git a/src/classes/SnapshotHelper.cls b/src/classes/SnapshotHelper.cls index d1754b44..fcb6abae 100644 --- a/src/classes/SnapshotHelper.cls +++ b/src/classes/SnapshotHelper.cls @@ -61,6 +61,16 @@ public class SnapshotHelper { proContractSnapshot.GS_Services_Completed_for_Period__c = (dcontract.GS_Services_Completed__c != null ? dcontract.GS_Services_Completed__c : 0) - (oldSnapshot.GS_Services_Completed__c != null ? oldSnapshot.GS_Services_Completed__c : 0); proContractSnapshot.GS_Net_Income_for_Period__c = (dcontract.GS_Net_Income_to_Date__c != null ? dcontract.GS_Net_Income_to_Date__c : 0) - (oldSnapshot.GS_Net_Income_to_Date__c != null ? oldSnapshot.GS_Net_Income_to_Date__c : 0); proContractSnapshot.GS_Net_Income_for_Period_by_Handover__c = (dcontract.GS_Net_Income_to_Date_Handover__c != null ? dcontract.GS_Net_Income_to_Date_Handover__c : 0) - (oldSnapshot.GS_Net_Income_to_Date_Handover__c != null ? oldSnapshot.GS_Net_Income_to_Date_Handover__c : 0); + } else { + proContractSnapshot.Work_Completed_for_Period__c = 0; + proContractSnapshot.Services_Spend_for_Period__c = 0; + proContractSnapshot.Expense_Travel_Spend_for_Period__c = 0; + proContractSnapshot.Product_Spend_for_Period__c = 0; + proContractSnapshot.Total_Calculated_Costs_for_Period__c = 0; + proContractSnapshot.GS_Services_Handover_Completed_for_Per__c = 0; + proContractSnapshot.GS_Services_Completed_for_Period__c = 0; + proContractSnapshot.GS_Net_Income_for_Period__c = 0; + proContractSnapshot.GS_Net_Income_for_Period_by_Handover__c = 0; } return proContractSnapshot; @@ -109,6 +119,16 @@ public class SnapshotHelper { sn.GS_Services_Handover_Completed_for_Per__c = (sn.GS_Services_Completed_Handover__c != null ? sn.GS_Services_Completed_Handover__c : 0) - (oldSn.GS_Services_Completed_Handover__c != null ? oldSn.GS_Services_Completed_Handover__c : 0); sn.GS_Net_Income_for_Period__c = (sn.GS_Net_Income_to_Date__c != null ? sn.GS_Net_Income_to_Date__c : 0) - (oldSn.GS_Net_Income_to_Date__c != null ? oldSn.GS_Net_Income_to_Date__c : 0); sn.GS_Net_Income_for_Period_by_Handover__c = (sn.GS_Net_Income_to_Date_Handover__c != null ? sn.GS_Net_Income_to_Date_Handover__c : 0) - (oldSn.GS_Net_Income_to_Date_Handover__c != null ? oldSn.GS_Net_Income_to_Date_Handover__c : 0); + } else { + sn.Work_Completed_for_Period__c = 0; + sn.Services_Spend_for_Period__c = 0; + sn.Expense_Travel_Spend_for_Period__c = 0; + sn.Product_Spend_for_Period__c = 0; + sn.Total_Calculated_Costs_for_Period__c = 0; + sn.GS_Services_Completed_for_Period__c = 0; + sn.GS_Services_Handover_Completed_for_Per__c = 0; + sn.GS_Net_Income_for_Period__c = 0; + sn.GS_Net_Income_for_Period_by_Handover__c = 0; } return sn; From 4e41f06f9e306393f278d8ee82958b25844b9dd7 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Mon, 30 Jan 2017 13:18:35 +0100 Subject: [PATCH 08/15] Added New PRO Snapshot fields --- src/classes/ProjectReportOutController.cls | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/classes/ProjectReportOutController.cls b/src/classes/ProjectReportOutController.cls index bc93f741..7814561f 100644 --- a/src/classes/ProjectReportOutController.cls +++ b/src/classes/ProjectReportOutController.cls @@ -231,13 +231,33 @@ public with sharing class ProjectReportOutController { DContract__c dcontract = [SELECT Id, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c, GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c, GS_Services_Completed_Handover__c, - GS_Revenue__c FROM DContract__c WHERE Id =: reportContractId]; + GS_Revenue__c, Project_Quality_Overall_Status__c, Customer_Satisfaction_of_services__c FROM DContract__c WHERE Id =: reportContractId]; dcontract.Last_Report_Out_Status__c = status; dcontract.Percent_through_SOW_deliverables__c = proTabContractPercntgethroughSOW; PRO_Contract_Snapshot__c proContractSnapshot = createProSnapshot(proToUpdate.Contract_Snapshot__c == null ? null : proToUpdate.Contract_Snapshot__c, dcontract); + Decimal percent = 0; + if (status == 'Green') { + percent = 1; + } else if (status == 'Yellow') { + percent = 0.5; + } + Decimal projectQuality = percent * (dcontract.GS_Execution_Budget__c != null ? dcontract.GS_Execution_Budget__c : 0); + proContractSnapshot.Project_Quality_Overall_Status__c = projectQuality; + dcontract.Project_Quality_Overall_Status__c = projectQuality; + + percent = 0; + if (PartnerRelationship == 'Green') { + percent = 1; + } else if (PartnerRelationship == 'Yellow') { + percent = 0.5; + } + Decimal customerSatisfaction = percent * (dcontract.GS_Execution_Budget__c != null ? dcontract.GS_Execution_Budget__c : 0); + proContractSnapshot.Customer_Satisfaction_of_services__c = customerSatisfaction; + dcontract.Customer_Satisfaction_of_services__c = customerSatisfaction; + PROEmailContractSnoozeHelper.resetEmailReminder(reportContractId); upsert proContractSnapshot Id; proToUpdate.Contract_Snapshot__c = proContractSnapshot.Id; From 6388e63201283503c015a752bfbf9a7ab540a290 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Wed, 1 Feb 2017 15:05:10 +0100 Subject: [PATCH 09/15] Added GS Quality Batch --- src/classes/ContractSnapshotTest.cls | 225 ++++++++++++++++++ src/classes/GSOverallCalculationBatch.cls | 98 ++++++++ .../GSOverallCalculationBatch.cls-meta.xml | 5 + 3 files changed, 328 insertions(+) create mode 100644 src/classes/GSOverallCalculationBatch.cls create mode 100644 src/classes/GSOverallCalculationBatch.cls-meta.xml diff --git a/src/classes/ContractSnapshotTest.cls b/src/classes/ContractSnapshotTest.cls index 4bb0bf34..3b973be6 100644 --- a/src/classes/ContractSnapshotTest.cls +++ b/src/classes/ContractSnapshotTest.cls @@ -125,6 +125,231 @@ public class ContractSnapshotTest { System.assertEquals(2910, conFromDB.GS_Revenue__c); } + static testMethod void shouldCalculateGSQualitySituation1() { + Business_Unit__c bu = new Business_Unit__c(Name = 'Inc'); + insert bu; + DContract__c dcontract1 = new DContract__c(Name = 'Sample Test 1', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 10, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 1000); + DContract__c dcontract2 = new DContract__c(Name = 'Sample Test 2', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 20, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 2000); + DContract__c dcontract3 = new DContract__c(Name = 'Sample Test 3', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 30, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 3000); + DContract__c dcontract4 = new DContract__c(Name = 'Sample Test 4', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 40, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 4000); + insert dcontract1; + insert dcontract2; + insert dcontract3; + insert dcontract4; + + PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, + GS_Execution_Budget__c = 1367); + PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, + GS_Execution_Budget__c = 1333); + PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, + GS_Execution_Budget__c = 4567); + insert proS1; + insert proS2; + insert proS3; + insert proS4; + insert proS5; + insert proS6; + insert proS7; + insert proS8; + + Project_Report_Out__c pro1 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS1.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 9)); + Project_Report_Out__c pro2 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS2.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); + Project_Report_Out__c pro3 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS3.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); + + Project_Report_Out__c pro4 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS4.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 9)); + Project_Report_Out__c pro5 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS5.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); + + Project_Report_Out__c pro6 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS6.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); + Project_Report_Out__c pro7 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS7.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); + + Project_Report_Out__c pro8 = new Project_Report_Out__c(Contract__c = dcontract4.Id, Contract_Snapshot__c = proS8.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); + + insert pro1; + insert pro2; + insert pro3; + insert pro4; + insert pro5; + insert pro6; + insert pro7; + insert pro8; + + Test.startTest(); + Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 3, 8))); + Test.stopTest(); + + PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; + Decimal value = proDromDB.GS_Quality_Overall_Status__c; + System.assertEquals(24.98, value.setScale(2)); + value = proDromDB.GS_Quality_Customer_Satisfaction__c; + System.assertEquals(25.99, value.setScale(2)); + // Expected Value = (400 + 444 + 678 + 543) / (1000 + 1367 + 1333 + 4567) = 2065 / 8267 = 24,97 + // Expected Value (Customer) = (250 + 223 + 900 + 776) / (1000 + 1367 + 1333 + 4567) = 2149 / 8267 = 25,99 + } + + static testMethod void shouldCalculateGSQualitySituation2() { + Business_Unit__c bu = new Business_Unit__c(Name = 'Inc'); + insert bu; + DContract__c dcontract1 = new DContract__c(Name = 'Sample Test 1', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 10, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 1000); + DContract__c dcontract2 = new DContract__c(Name = 'Sample Test 2', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 20, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 2000); + DContract__c dcontract3 = new DContract__c(Name = 'Sample Test 3', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 30, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 3000); + DContract__c dcontract4 = new DContract__c(Name = 'Sample Test 4', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 40, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 4000); + insert dcontract1; + insert dcontract2; + insert dcontract3; + insert dcontract4; + + PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, + GS_Execution_Budget__c = 1367); + PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, + GS_Execution_Budget__c = 1333); + PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, + GS_Execution_Budget__c = 4567); + insert proS1; + insert proS2; + insert proS3; + insert proS4; + insert proS5; + insert proS6; + insert proS7; + insert proS8; + + Project_Report_Out__c pro1 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS1.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); + Project_Report_Out__c pro2 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS2.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); + Project_Report_Out__c pro3 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS3.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); + + Project_Report_Out__c pro4 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS4.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); + Project_Report_Out__c pro5 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS5.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); + + Project_Report_Out__c pro6 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS6.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); + Project_Report_Out__c pro7 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS7.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); + + Project_Report_Out__c pro8 = new Project_Report_Out__c(Contract__c = dcontract4.Id, Contract_Snapshot__c = proS8.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); + + insert pro1; + insert pro2; + insert pro3; + insert pro4; + insert pro5; + insert pro6; + insert pro7; + insert pro8; + + Test.startTest(); + Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 3, 22))); + Test.stopTest(); + + PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; + Decimal value = proDromDB.GS_Quality_Overall_Status__c; + System.assertEquals(24.98, value.setScale(2)); + value = proDromDB.GS_Quality_Customer_Satisfaction__c; + System.assertEquals(25.99, value.setScale(2)); + // Expected Value = (400 + 444 + 678 + 543) / (1000 + 1367 + 1333 + 4567) = 2065 / 8267 = 24,97 + // Expected Value (Customer) = (250 + 223 + 900 + 776) / (1000 + 1367 + 1333 + 4567) = 2149 / 8267 = 25,99 + } + + static testMethod void shouldCalculateGSQualitySituation3() { + Business_Unit__c bu = new Business_Unit__c(Name = 'Inc'); + insert bu; + DContract__c dcontract1 = new DContract__c(Name = 'Sample Test 1', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 10, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 1000); + DContract__c dcontract2 = new DContract__c(Name = 'Sample Test 2', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 20, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 2000); + DContract__c dcontract3 = new DContract__c(Name = 'Sample Test 3', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 30, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 3000); + DContract__c dcontract4 = new DContract__c(Name = 'Sample Test 4', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 40, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 4000); + insert dcontract1; + insert dcontract2; + insert dcontract3; + insert dcontract4; + + PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, + GS_Execution_Budget__c = 1367); + PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, + GS_Execution_Budget__c = 1333); + PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, + GS_Execution_Budget__c = 4567); + insert proS1; + insert proS2; + insert proS3; + insert proS4; + insert proS5; + insert proS6; + insert proS7; + insert proS8; + + Project_Report_Out__c pro1 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS1.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); + Project_Report_Out__c pro2 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS2.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); + Project_Report_Out__c pro3 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS3.Id, Report_Out_Date__c = Date.newInstance(2017, 4, 13)); + + Project_Report_Out__c pro4 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS4.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); + Project_Report_Out__c pro5 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS5.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); + + Project_Report_Out__c pro6 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS6.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); + Project_Report_Out__c pro7 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS7.Id, Report_Out_Date__c = Date.newInstance(2017, 4, 13)); + + Project_Report_Out__c pro8 = new Project_Report_Out__c(Contract__c = dcontract4.Id, Contract_Snapshot__c = proS8.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); + + insert pro1; + insert pro2; + insert pro3; + insert pro4; + insert pro5; + insert pro6; + insert pro7; + insert pro8; + + Test.startTest(); + Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 3, 27))); + Test.stopTest(); + + PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; + Decimal value = proDromDB.GS_Quality_Overall_Status__c; + System.assertEquals(24.98, value.setScale(2)); + value = proDromDB.GS_Quality_Customer_Satisfaction__c; + System.assertEquals(25.99, value.setScale(2)); + // Expected Value = (400 + 444 + 678 + 543) / (1000 + 1367 + 1333 + 4567) = 2065 / 8267 = 24,97 + // Expected Value (Customer) = (250 + 223 + 900 + 776) / (1000 + 1367 + 1333 + 4567) = 2149 / 8267 = 25,99 + } + static private void assertSnapshotData(PRO_Contract_Snapshot__c snapshot) { System.assertEquals('Sample Test Name', snapshot.Name); System.assertEquals(30, snapshot.Percent_Of_Work_Completed__c); diff --git a/src/classes/GSOverallCalculationBatch.cls b/src/classes/GSOverallCalculationBatch.cls new file mode 100644 index 00000000..320e77c1 --- /dev/null +++ b/src/classes/GSOverallCalculationBatch.cls @@ -0,0 +1,98 @@ +/** + * Calculates GS Quality for Overall Status and for Customer Satisfaction in PRO Snapshots. + */ +public without sharing class GSOverallCalculationBatch implements Database.Batchable, Database.Stateful { + + private Double gsOverall; + private Double gsSatisfaction; + private Double gsBudget; + private List proList; + private Date currentDate; + + public GSOverallCalculationBatch() { + this.gsOverall = 0; + this.gsSatisfaction = 0; + this.gsBudget = 0; + this.proList = new List(); + this.currentDate = Date.today(); + } + + public GSOverallCalculationBatch(Date currentDate) { + this.gsOverall = 0; + this.gsSatisfaction = 0; + this.gsBudget = 0; + this.proList = new List(); + this.currentDate = currentDate; + } + + public List start(Database.BatchableContext BC) { + List thursDayList = PROHelper.GetAllThursDayOfMonth(this.currentDate.year(), this.currentDate.month()); + List thursDayListLastMonth = PROHelper.GetAllThursDayOfMonth(this.currentDate.addMonths(-1).year(), this.currentDate.addMonths(-1).month()); + List thursDayListNextMonth = PROHelper.GetAllThursDayOfMonth(this.currentDate.addMonths(1).year(), this.currentDate.addMonths(1).month()); + Date startDate; + Date endDate; + if (this.currentDate < thursDayList[1]) { + startDate = thursDayListLastMonth[3]; + endDate = thursDayList[1]; + } else if (this.currentDate < thursDayList[3]) { + startDate = thursDayList[1]; + endDate = thursDayList[3]; + } else { + startDate = thursDayList[3]; + endDate = thursDayListNextMonth[1]; + } + + System.debug('Start date : ' + startDate); + System.debug('End date : ' + endDate); + return [SELECT Id, Report_Out_Date__c, Contract_Snapshot__c, Contract_Snapshot__r.GS_Quality_Overall_Status__c, Contract_Snapshot__r.GS_Quality_Customer_Satisfaction__c, + Contract_Snapshot__r.Project_Quality_Overall_Status__c, Contract_Snapshot__r.Customer_Satisfaction_of_services__c, Contract_Snapshot__r.GS_Execution_Budget__c + FROM Project_Report_Out__c WHERE Contract_Snapshot__c != '' AND Report_Out_Date__c >=: startDate AND Report_Out_Date__c <: endDate]; + } + + public void execute(Database.BatchableContext BC, List scope) { + try { + System.debug('scope ; ' + scope.size()); + for (Project_Report_Out__c pro : scope) { + System.debug(pro.Report_Out_Date__c); + this.gsOverall += pro.Contract_Snapshot__r.Project_Quality_Overall_Status__c != null ? pro.Contract_Snapshot__r.Project_Quality_Overall_Status__c : 0; + this.gsSatisfaction += pro.Contract_Snapshot__r.Customer_Satisfaction_of_services__c != null ? pro.Contract_Snapshot__r.Customer_Satisfaction_of_services__c : 0; + this.gsBudget += pro.Contract_Snapshot__r.GS_Execution_Budget__c != null ? pro.Contract_Snapshot__r.GS_Execution_Budget__c : 0; + this.proList.add(pro); + } + } catch (Exception ex) { + System.debug('Error : ' + ex); + if (!Test.isRunningTest()) { + BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); + EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'GS Quality calculation error', 'GS Quality calculation error', ex); + } + } + } + + public void finish(Database.BatchableContext BC) { + try { + List toUpdate = new List(); + System.debug('this.gsOverall : ' + this.gsOverall); + System.debug('this.gsSatisfaction : ' + this.gsSatisfaction); + System.debug('this.gsBudget : ' + this.gsBudget); + this.gsOverall = 100 * (this.gsOverall / this.gsBudget); + this.gsSatisfaction = 100 * (this.gsSatisfaction / this.gsBudget); + + for (Project_Report_Out__c pro : this.proList) { + if (this.gsOverall != pro.Contract_Snapshot__r.GS_Quality_Overall_Status__c || this.gsSatisfaction != pro.Contract_Snapshot__r.GS_Quality_Customer_Satisfaction__c) { + PRO_Contract_Snapshot__c snap = new PRO_Contract_Snapshot__c(Id = pro.Contract_Snapshot__c, GS_Quality_Overall_Status__c = this.gsOverall, GS_Quality_Customer_Satisfaction__c = this.gsSatisfaction); + toUpdate.add(snap); + } + } + if (toUpdate.size() > 0) { + update toUpdate; + } + + } catch (Exception ex) { + System.debug('Error : ' + ex); + if (!Test.isRunningTest()) { + BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); + EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'GS Quality calculation error', 'GS Quality calculation error', ex); + } + } + } +} \ No newline at end of file diff --git a/src/classes/GSOverallCalculationBatch.cls-meta.xml b/src/classes/GSOverallCalculationBatch.cls-meta.xml new file mode 100644 index 00000000..38aa015d --- /dev/null +++ b/src/classes/GSOverallCalculationBatch.cls-meta.xml @@ -0,0 +1,5 @@ + + + 36.0 + Active + From e6e77f6ccd17afe0109c272696da5f34b2a7d6bb Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Mon, 6 Feb 2017 11:18:24 +0100 Subject: [PATCH 10/15] GS Quality for BU --- src/classes/ContractSnapshotTest.cls | 6 +-- src/classes/GSOverallCalculationBatch.cls | 50 +++++++++++++++++++++-- src/classes/GSRevenueCalculationBatch.cls | 2 +- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/classes/ContractSnapshotTest.cls b/src/classes/ContractSnapshotTest.cls index 3b973be6..b5b45a7a 100644 --- a/src/classes/ContractSnapshotTest.cls +++ b/src/classes/ContractSnapshotTest.cls @@ -188,7 +188,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 3, 8))); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 8))); Test.stopTest(); PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; @@ -263,7 +263,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 3, 22))); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 22))); Test.stopTest(); PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; @@ -338,7 +338,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 3, 27))); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 27))); Test.stopTest(); PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; diff --git a/src/classes/GSOverallCalculationBatch.cls b/src/classes/GSOverallCalculationBatch.cls index 320e77c1..d5952bd1 100644 --- a/src/classes/GSOverallCalculationBatch.cls +++ b/src/classes/GSOverallCalculationBatch.cls @@ -3,13 +3,21 @@ */ public without sharing class GSOverallCalculationBatch implements Database.Batchable, Database.Stateful { + private List businessUnits; + private Business_Unit__c currentUnit; + private Double gsOverall; private Double gsSatisfaction; private Double gsBudget; private List proList; private Date currentDate; - public GSOverallCalculationBatch() { + public GSOverallCalculationBatch(List businessUnits) { + this.businessUnits = businessUnits; + if (this.businessUnits.size() > 0) { + this.currentUnit = this.businessUnits.get(0); + this.businessUnits.remove(0); + } this.gsOverall = 0; this.gsSatisfaction = 0; this.gsBudget = 0; @@ -17,7 +25,12 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch this.currentDate = Date.today(); } - public GSOverallCalculationBatch(Date currentDate) { + public GSOverallCalculationBatch(List businessUnits, Date currentDate) { + this.businessUnits = businessUnits; + if (this.businessUnits.size() > 0) { + this.currentUnit = this.businessUnits.get(0); + this.businessUnits.remove(0); + } this.gsOverall = 0; this.gsSatisfaction = 0; this.gsBudget = 0; @@ -44,9 +57,13 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch System.debug('Start date : ' + startDate); System.debug('End date : ' + endDate); - return [SELECT Id, Report_Out_Date__c, Contract_Snapshot__c, Contract_Snapshot__r.GS_Quality_Overall_Status__c, Contract_Snapshot__r.GS_Quality_Customer_Satisfaction__c, + if (this.currentUnit == null) { + return new List(); + } + return [SELECT Id, Contract__c, Report_Out_Date__c, Contract_Snapshot__c, Contract_Snapshot__r.GS_Quality_Overall_Status__c, Contract_Snapshot__r.GS_Quality_Customer_Satisfaction__c, Contract_Snapshot__r.Project_Quality_Overall_Status__c, Contract_Snapshot__r.Customer_Satisfaction_of_services__c, Contract_Snapshot__r.GS_Execution_Budget__c - FROM Project_Report_Out__c WHERE Contract_Snapshot__c != '' AND Report_Out_Date__c >=: startDate AND Report_Out_Date__c <: endDate]; + FROM Project_Report_Out__c WHERE Contract__r.Prime_Contracting_Business_Unit__c =: this.currentUnit.Id AND Contract_Snapshot__c != '' + AND Report_Out_Date__c >=: startDate AND Report_Out_Date__c <: endDate]; } public void execute(Database.BatchableContext BC, List scope) { @@ -77,12 +94,31 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch this.gsOverall = 100 * (this.gsOverall / this.gsBudget); this.gsSatisfaction = 100 * (this.gsSatisfaction / this.gsBudget); + List contractsToUpdate = new List(); for (Project_Report_Out__c pro : this.proList) { if (this.gsOverall != pro.Contract_Snapshot__r.GS_Quality_Overall_Status__c || this.gsSatisfaction != pro.Contract_Snapshot__r.GS_Quality_Customer_Satisfaction__c) { PRO_Contract_Snapshot__c snap = new PRO_Contract_Snapshot__c(Id = pro.Contract_Snapshot__c, GS_Quality_Overall_Status__c = this.gsOverall, GS_Quality_Customer_Satisfaction__c = this.gsSatisfaction); toUpdate.add(snap); } + contractsToUpdate.add(new DContract__c(Id = pro.Contract__c, GS_Quality_Overall_Status__c = this.gsOverall, + GS_Quality_Customer_Satisfaction__c = this.gsSatisfaction)); } + List updateResults = Database.update(contractsToUpdate, false); + String emailBody = ''; + for(Integer i = 0; i < updateResults.size(); i++){ + if (!updateResults.get(i).isSuccess()){ + Database.Error error = updateResults.get(i).getErrors().get(0); + System.debug('Error : Fields - ' + error.getFields() + ' Message - ' + error.getMessage()); + emailBody += '' + toUpdate.get(i).Id + '' + error.getFields()+ '' + error.getStatusCode()+ '' + error.getMessage() + ''; + } + } + + if (emailBody != '') { + BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); + emailBody = 'td>Fieldstd>Code' + emailBody + '
IdError Message

'; + EmailHelper.sendEmail(settings.Error_Emails__c.split(','), 'GS Quality calculation error', emailBody); + } + if (toUpdate.size() > 0) { update toUpdate; } @@ -94,5 +130,11 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'GS Quality calculation error', 'GS Quality calculation error', ex); } } + + if (this.businessUnits.size() > 0) { + Database.executeBatch(new GSOverallCalculationBatch(this.businessUnits, this.currentDate)); + } else { + Database.executeBatch(new ForPeriodCalcSnapshotBatch(false), 50); + } } } \ No newline at end of file diff --git a/src/classes/GSRevenueCalculationBatch.cls b/src/classes/GSRevenueCalculationBatch.cls index fd46f262..09367aac 100644 --- a/src/classes/GSRevenueCalculationBatch.cls +++ b/src/classes/GSRevenueCalculationBatch.cls @@ -73,7 +73,7 @@ public without sharing class GSRevenueCalculationBatch implements Database.Batch if (this.businessUnits.size() > 0) { Database.executeBatch(new GSRevenueCalculationBatch(this.businessUnits)); } else { - Database.executeBatch(new ForPeriodCalcSnapshotBatch(false), 50); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c])); } } From 8a685535417f83a8fc857d10f8a407113f1188b9 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Wed, 8 Feb 2017 09:45:26 +0100 Subject: [PATCH 11/15] Added Total calculations --- src/classes/ContractSnapshotTest.cls | 6 +-- src/classes/GSOverallCalculationBatch.cls | 60 +++++++++++++++-------- src/classes/GSRevenueCalculationBatch.cls | 2 +- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/classes/ContractSnapshotTest.cls b/src/classes/ContractSnapshotTest.cls index b5b45a7a..f7809707 100644 --- a/src/classes/ContractSnapshotTest.cls +++ b/src/classes/ContractSnapshotTest.cls @@ -188,7 +188,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 8))); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 8), false)); Test.stopTest(); PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; @@ -263,7 +263,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 22))); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 22), false)); Test.stopTest(); PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; @@ -338,7 +338,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 27))); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 27), false)); Test.stopTest(); PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; diff --git a/src/classes/GSOverallCalculationBatch.cls b/src/classes/GSOverallCalculationBatch.cls index d5952bd1..d33bd284 100644 --- a/src/classes/GSOverallCalculationBatch.cls +++ b/src/classes/GSOverallCalculationBatch.cls @@ -11,8 +11,9 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch private Double gsBudget; private List proList; private Date currentDate; + private Boolean total; - public GSOverallCalculationBatch(List businessUnits) { + public GSOverallCalculationBatch(List businessUnits, Boolean total) { this.businessUnits = businessUnits; if (this.businessUnits.size() > 0) { this.currentUnit = this.businessUnits.get(0); @@ -23,9 +24,10 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch this.gsBudget = 0; this.proList = new List(); this.currentDate = Date.today(); + this.total = total; } - public GSOverallCalculationBatch(List businessUnits, Date currentDate) { + public GSOverallCalculationBatch(List businessUnits, Date currentDate, Boolean total) { this.businessUnits = businessUnits; if (this.businessUnits.size() > 0) { this.currentUnit = this.businessUnits.get(0); @@ -36,6 +38,7 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch this.gsBudget = 0; this.proList = new List(); this.currentDate = currentDate; + this.total = total; } public List start(Database.BatchableContext BC) { @@ -57,6 +60,11 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch System.debug('Start date : ' + startDate); System.debug('End date : ' + endDate); + if (this.total == true) { + return [SELECT Id, Contract__c, Report_Out_Date__c, Contract_Snapshot__c, Contract_Snapshot__r.GS_Quality_Overall_Status__c, Contract_Snapshot__r.GS_Quality_Customer_Satisfaction__c, + Contract_Snapshot__r.Project_Quality_Overall_Status__c, Contract_Snapshot__r.Customer_Satisfaction_of_services__c, Contract_Snapshot__r.GS_Execution_Budget__c + FROM Project_Report_Out__c WHERE Contract_Snapshot__c != '' AND Report_Out_Date__c >=: startDate AND Report_Out_Date__c <: endDate]; + } if (this.currentUnit == null) { return new List(); } @@ -68,6 +76,7 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch public void execute(Database.BatchableContext BC, List scope) { try { + System.debug('Total : ' + this.total); System.debug('scope ; ' + scope.size()); for (Project_Report_Out__c pro : scope) { System.debug(pro.Report_Out_Date__c); @@ -88,6 +97,7 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch public void finish(Database.BatchableContext BC) { try { List toUpdate = new List(); + System.debug('Total : ' + this.total); System.debug('this.gsOverall : ' + this.gsOverall); System.debug('this.gsSatisfaction : ' + this.gsSatisfaction); System.debug('this.gsBudget : ' + this.gsBudget); @@ -96,27 +106,33 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch List contractsToUpdate = new List(); for (Project_Report_Out__c pro : this.proList) { - if (this.gsOverall != pro.Contract_Snapshot__r.GS_Quality_Overall_Status__c || this.gsSatisfaction != pro.Contract_Snapshot__r.GS_Quality_Customer_Satisfaction__c) { - PRO_Contract_Snapshot__c snap = new PRO_Contract_Snapshot__c(Id = pro.Contract_Snapshot__c, GS_Quality_Overall_Status__c = this.gsOverall, GS_Quality_Customer_Satisfaction__c = this.gsSatisfaction); - toUpdate.add(snap); - } - contractsToUpdate.add(new DContract__c(Id = pro.Contract__c, GS_Quality_Overall_Status__c = this.gsOverall, + if (this.total == false) { + toUpdate.add(new PRO_Contract_Snapshot__c(Id = pro.Contract_Snapshot__c, GS_Quality_Overall_Status__c = this.gsOverall, + GS_Quality_Customer_Satisfaction__c = this.gsSatisfaction)); + contractsToUpdate.add(new DContract__c(Id = pro.Contract__c, GS_Quality_Overall_Status__c = this.gsOverall, GS_Quality_Customer_Satisfaction__c = this.gsSatisfaction)); + } else { + contractsToUpdate.add(new DContract__c(Id = pro.Contract__c, GS_Quality_Total_Overall_Status__c = this.gsOverall, + GS_Quality_Total_Customer_Satisfaction__c = this.gsSatisfaction)); + } } - List updateResults = Database.update(contractsToUpdate, false); - String emailBody = ''; - for(Integer i = 0; i < updateResults.size(); i++){ - if (!updateResults.get(i).isSuccess()){ - Database.Error error = updateResults.get(i).getErrors().get(0); - System.debug('Error : Fields - ' + error.getFields() + ' Message - ' + error.getMessage()); - emailBody += '' + toUpdate.get(i).Id + '' + error.getFields()+ '' + error.getStatusCode()+ '' + error.getMessage() + ''; + + if (contractsToUpdate.size() > 0) { + List updateResults = Database.update(contractsToUpdate, false); + String emailBody = ''; + for(Integer i = 0; i < updateResults.size(); i++){ + if (!updateResults.get(i).isSuccess()){ + Database.Error error = updateResults.get(i).getErrors().get(0); + System.debug('Error : Fields - ' + error.getFields() + ' Message - ' + error.getMessage()); + emailBody += '' + toUpdate.get(i).Id + '' + error.getFields()+ '' + error.getStatusCode()+ '' + error.getMessage() + ''; + } } - } - if (emailBody != '') { - BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); - emailBody = 'td>Fieldstd>Code' + emailBody + '
IdError Message

'; - EmailHelper.sendEmail(settings.Error_Emails__c.split(','), 'GS Quality calculation error', emailBody); + if (emailBody != '') { + BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); + emailBody = 'td>Fieldstd>Code' + emailBody + '
IdError Message

'; + EmailHelper.sendEmail(settings.Error_Emails__c.split(','), 'GS Quality calculation error', emailBody); + } } if (toUpdate.size() > 0) { @@ -131,8 +147,10 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch } } - if (this.businessUnits.size() > 0) { - Database.executeBatch(new GSOverallCalculationBatch(this.businessUnits, this.currentDate)); + if (this.total == false && this.businessUnits.size() > 0) { + Database.executeBatch(new GSOverallCalculationBatch(this.businessUnits, this.currentDate, this.total)); + } else if (this.total == false) { + Database.executeBatch(new GSOverallCalculationBatch(new List(), this.currentDate, true)); } else { Database.executeBatch(new ForPeriodCalcSnapshotBatch(false), 50); } diff --git a/src/classes/GSRevenueCalculationBatch.cls b/src/classes/GSRevenueCalculationBatch.cls index 09367aac..9a4059f5 100644 --- a/src/classes/GSRevenueCalculationBatch.cls +++ b/src/classes/GSRevenueCalculationBatch.cls @@ -73,7 +73,7 @@ public without sharing class GSRevenueCalculationBatch implements Database.Batch if (this.businessUnits.size() > 0) { Database.executeBatch(new GSRevenueCalculationBatch(this.businessUnits)); } else { - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c])); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], false)); } } From d83b38d0bfbd5424a3359ea562d757eeca4de047 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Fri, 10 Feb 2017 13:35:20 +0100 Subject: [PATCH 12/15] Added Contract Statistic --- src/classes/ContractSnapshotTest.cls | 61 +++++++++++++++++++++-- src/classes/GSOverallCalculationBatch.cls | 59 ++++++++++++++-------- src/classes/GSRevenueCalculationBatch.cls | 2 +- 3 files changed, 98 insertions(+), 24 deletions(-) diff --git a/src/classes/ContractSnapshotTest.cls b/src/classes/ContractSnapshotTest.cls index f7809707..5ca936be 100644 --- a/src/classes/ContractSnapshotTest.cls +++ b/src/classes/ContractSnapshotTest.cls @@ -188,7 +188,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 8), false)); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 8), 0, 0, 0)); Test.stopTest(); PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; @@ -263,7 +263,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 22), false)); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 22), 0, 0, 0)); Test.stopTest(); PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; @@ -338,7 +338,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 27), false)); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 27), 0, 0, 0)); Test.stopTest(); PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; @@ -350,6 +350,61 @@ public class ContractSnapshotTest { // Expected Value (Customer) = (250 + 223 + 900 + 776) / (1000 + 1367 + 1333 + 4567) = 2149 / 8267 = 25,99 } + static testMethod void shouldCalculateGSQualityTotal() { + Business_Unit__c bu1 = new Business_Unit__c(Name = 'Inc'); + insert bu1; + Business_Unit__c bu2 = new Business_Unit__c(Name = 'Test BU'); + insert bu2; + DContract__c dcontract1 = new DContract__c(Name = 'Sample Test 1', Prime_Contracting_Business_Unit__c = bu1.Id, Percent_through_SOW_deliverables__c = 10, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 1000); + DContract__c dcontract2 = new DContract__c(Name = 'Sample Test 2', Prime_Contracting_Business_Unit__c = bu1.Id, Percent_through_SOW_deliverables__c = 20, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 2000); + DContract__c dcontract3 = new DContract__c(Name = 'Sample Test 3', Prime_Contracting_Business_Unit__c = bu2.Id, Percent_through_SOW_deliverables__c = 30, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 3000); + DContract__c dcontract4 = new DContract__c(Name = 'Sample Test 4', Prime_Contracting_Business_Unit__c = bu2.Id, Percent_through_SOW_deliverables__c = 40, Expected_Buffer__c = 0, + Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 4000); + insert dcontract1; + insert dcontract2; + insert dcontract3; + insert dcontract4; + + PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 200, Customer_Satisfaction_of_services__c = 100, + GS_Execution_Budget__c = 500); + PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 200, + GS_Execution_Budget__c = 900); + PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 600, Customer_Satisfaction_of_services__c = 300, + GS_Execution_Budget__c = 1000); + PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 800, Customer_Satisfaction_of_services__c = 400, + GS_Execution_Budget__c = 1500); + + insert proS1; + insert proS2; + insert proS3; + insert proS4; + + Project_Report_Out__c pro1 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS1.Id, Report_Out_Date__c = Date.newInstance(2017, 1, 26)); + Project_Report_Out__c pro2 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS2.Id, Report_Out_Date__c = Date.newInstance(2017, 1, 26)); + Project_Report_Out__c pro3 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS3.Id, Report_Out_Date__c = Date.newInstance(2017, 1, 26)); + Project_Report_Out__c pro4 = new Project_Report_Out__c(Contract__c = dcontract4.Id, Contract_Snapshot__c = proS4.Id, Report_Out_Date__c = Date.newInstance(2017, 1, 26)); + + insert pro1; + insert pro2; + insert pro3; + insert pro4; + + Test.startTest(); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 2, 8), 0, 0, 0)); + Test.stopTest(); + + Contract_Statistic__c proDromDB = [SELECT Id, GS_Quality_Total_Overall_Status__c, GS_Quality_Total_Customer_Satisfaction__c FROM Contract_Statistic__c LIMIT 1]; + Decimal value = proDromDB.GS_Quality_Total_Overall_Status__c; + System.assertEquals(51.28, value.setScale(2)); + value = proDromDB.GS_Quality_Total_Customer_Satisfaction__c; + System.assertEquals(25.64, value.setScale(2)); + // Expected Value = (200 + 400 + 600 + 800) / (500 + 900 + 1000 + 1500) = 2000 / 3900 = 51,28 + // Expected Value (Customer) = (100 + 200 + 300 + 400) / (500 + 900 + 1000 + 1500) = 1000 / 3900 = 25,64 + } + static private void assertSnapshotData(PRO_Contract_Snapshot__c snapshot) { System.assertEquals('Sample Test Name', snapshot.Name); System.assertEquals(30, snapshot.Percent_Of_Work_Completed__c); diff --git a/src/classes/GSOverallCalculationBatch.cls b/src/classes/GSOverallCalculationBatch.cls index d33bd284..74fad720 100644 --- a/src/classes/GSOverallCalculationBatch.cls +++ b/src/classes/GSOverallCalculationBatch.cls @@ -9,11 +9,15 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch private Double gsOverall; private Double gsSatisfaction; private Double gsBudget; + private Double gsTotalOverall; + private Double gsTotalSatisfaction; + private Double gsTotalBudget; private List proList; private Date currentDate; private Boolean total; + private Date cycleStartDate; - public GSOverallCalculationBatch(List businessUnits, Boolean total) { + public GSOverallCalculationBatch(List businessUnits) { this.businessUnits = businessUnits; if (this.businessUnits.size() > 0) { this.currentUnit = this.businessUnits.get(0); @@ -22,12 +26,14 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch this.gsOverall = 0; this.gsSatisfaction = 0; this.gsBudget = 0; + this.gsTotalOverall = 0; + this.gsTotalSatisfaction = 0; + this.gsTotalBudget = 0; this.proList = new List(); this.currentDate = Date.today(); - this.total = total; } - public GSOverallCalculationBatch(List businessUnits, Date currentDate, Boolean total) { + public GSOverallCalculationBatch(List businessUnits, Date currentDate, Double gsTotalOverall, Double gsTotalSatisfaction, Double gsTotalBudget) { this.businessUnits = businessUnits; if (this.businessUnits.size() > 0) { this.currentUnit = this.businessUnits.get(0); @@ -36,9 +42,11 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch this.gsOverall = 0; this.gsSatisfaction = 0; this.gsBudget = 0; + this.gsTotalOverall = gsTotalOverall; + this.gsTotalSatisfaction = gsTotalSatisfaction; + this.gsTotalBudget = gsTotalBudget; this.proList = new List(); this.currentDate = currentDate; - this.total = total; } public List start(Database.BatchableContext BC) { @@ -58,13 +66,14 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch endDate = thursDayListNextMonth[1]; } + this.cycleStartDate = startDate; + if (this.currentDate == endDate.addDays(-1)) { + this.total = true; // If last day we calculate total value + } + System.debug('Start date : ' + startDate); System.debug('End date : ' + endDate); - if (this.total == true) { - return [SELECT Id, Contract__c, Report_Out_Date__c, Contract_Snapshot__c, Contract_Snapshot__r.GS_Quality_Overall_Status__c, Contract_Snapshot__r.GS_Quality_Customer_Satisfaction__c, - Contract_Snapshot__r.Project_Quality_Overall_Status__c, Contract_Snapshot__r.Customer_Satisfaction_of_services__c, Contract_Snapshot__r.GS_Execution_Budget__c - FROM Project_Report_Out__c WHERE Contract_Snapshot__c != '' AND Report_Out_Date__c >=: startDate AND Report_Out_Date__c <: endDate]; - } + if (this.currentUnit == null) { return new List(); } @@ -101,20 +110,19 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch System.debug('this.gsOverall : ' + this.gsOverall); System.debug('this.gsSatisfaction : ' + this.gsSatisfaction); System.debug('this.gsBudget : ' + this.gsBudget); + this.gsTotalOverall += this.gsOverall; + this.gsTotalSatisfaction += this.gsSatisfaction; + this.gsTotalBudget += this.gsBudget; + this.gsOverall = 100 * (this.gsOverall / this.gsBudget); this.gsSatisfaction = 100 * (this.gsSatisfaction / this.gsBudget); List contractsToUpdate = new List(); for (Project_Report_Out__c pro : this.proList) { - if (this.total == false) { - toUpdate.add(new PRO_Contract_Snapshot__c(Id = pro.Contract_Snapshot__c, GS_Quality_Overall_Status__c = this.gsOverall, + toUpdate.add(new PRO_Contract_Snapshot__c(Id = pro.Contract_Snapshot__c, GS_Quality_Overall_Status__c = this.gsOverall, GS_Quality_Customer_Satisfaction__c = this.gsSatisfaction)); - contractsToUpdate.add(new DContract__c(Id = pro.Contract__c, GS_Quality_Overall_Status__c = this.gsOverall, + contractsToUpdate.add(new DContract__c(Id = pro.Contract__c, GS_Quality_Overall_Status__c = this.gsOverall, GS_Quality_Customer_Satisfaction__c = this.gsSatisfaction)); - } else { - contractsToUpdate.add(new DContract__c(Id = pro.Contract__c, GS_Quality_Total_Overall_Status__c = this.gsOverall, - GS_Quality_Total_Customer_Satisfaction__c = this.gsSatisfaction)); - } } if (contractsToUpdate.size() > 0) { @@ -147,12 +155,23 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch } } - if (this.total == false && this.businessUnits.size() > 0) { - Database.executeBatch(new GSOverallCalculationBatch(this.businessUnits, this.currentDate, this.total)); - } else if (this.total == false) { - Database.executeBatch(new GSOverallCalculationBatch(new List(), this.currentDate, true)); + if (this.businessUnits.size() > 0) { + GSOverallCalculationBatch overallBatch = new GSOverallCalculationBatch(this.businessUnits, this.currentDate, this.gsTotalOverall, + this.gsTotalSatisfaction, this.gsTotalBudget); + Database.executeBatch(overallBatch); } else { + if (this.total == true) { + this.gsTotalOverall = 100 * (this.gsTotalOverall / this.gsTotalBudget); + this.gsTotalSatisfaction = 100 * (this.gsTotalSatisfaction / this.gsTotalBudget); + Contract_Statistic__c cs = new Contract_Statistic__c(PRO_Cycle_Date__c = this.cycleStartDate, GS_Quality_Total_Overall_Status__c = this.gsTotalOverall, + GS_Quality_Total_Customer_Satisfaction__c = this.gsTotalSatisfaction); + insert cs; + } Database.executeBatch(new ForPeriodCalcSnapshotBatch(false), 50); } } + + public void setTotal(Boolean total) { + this.total = total; + } } \ No newline at end of file diff --git a/src/classes/GSRevenueCalculationBatch.cls b/src/classes/GSRevenueCalculationBatch.cls index 9a4059f5..09367aac 100644 --- a/src/classes/GSRevenueCalculationBatch.cls +++ b/src/classes/GSRevenueCalculationBatch.cls @@ -73,7 +73,7 @@ public without sharing class GSRevenueCalculationBatch implements Database.Batch if (this.businessUnits.size() > 0) { Database.executeBatch(new GSRevenueCalculationBatch(this.businessUnits)); } else { - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], false)); + Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c])); } } From 705a6af9f79e6562ce9983f2b418c0c8c6b91236 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Mon, 27 Feb 2017 14:45:53 +0100 Subject: [PATCH 13/15] Add TODO comments --- src/classes/SnapshotHelper.cls | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/classes/SnapshotHelper.cls b/src/classes/SnapshotHelper.cls index fcb6abae..fe1f311d 100644 --- a/src/classes/SnapshotHelper.cls +++ b/src/classes/SnapshotHelper.cls @@ -49,7 +49,7 @@ public class SnapshotHelper { proContractSnapshot.GS_Services_Completed__c = dcontract.GS_Services_Completed__c; proContractSnapshot.GS_Net_Income_to_Date__c = dcontract.GS_Net_Income_to_Date__c; proContractSnapshot.GS_Net_Income_to_Date_Handover__c = dcontract.GS_Net_Income_to_Date_Handover__c; - proContractSnapshot.GS_Revenue__c = dcontract.GS_Revenue__c; + // TODO FORMULA field proContractSnapshot.GS_Revenue__c = dcontract.GS_Revenue__c; if (oldSnapshot.Id != null) { proContractSnapshot.Work_Completed_for_Period__c = (dcontract.Percent_through_SOW_deliverables__c != null ? dcontract.Percent_through_SOW_deliverables__c : 0) - (oldSnapshot.Percent_Of_Work_Completed__c != null ? oldSnapshot.Percent_Of_Work_Completed__c : 0); @@ -61,7 +61,7 @@ public class SnapshotHelper { proContractSnapshot.GS_Services_Completed_for_Period__c = (dcontract.GS_Services_Completed__c != null ? dcontract.GS_Services_Completed__c : 0) - (oldSnapshot.GS_Services_Completed__c != null ? oldSnapshot.GS_Services_Completed__c : 0); proContractSnapshot.GS_Net_Income_for_Period__c = (dcontract.GS_Net_Income_to_Date__c != null ? dcontract.GS_Net_Income_to_Date__c : 0) - (oldSnapshot.GS_Net_Income_to_Date__c != null ? oldSnapshot.GS_Net_Income_to_Date__c : 0); proContractSnapshot.GS_Net_Income_for_Period_by_Handover__c = (dcontract.GS_Net_Income_to_Date_Handover__c != null ? dcontract.GS_Net_Income_to_Date_Handover__c : 0) - (oldSnapshot.GS_Net_Income_to_Date_Handover__c != null ? oldSnapshot.GS_Net_Income_to_Date_Handover__c : 0); - } else { + } else { //TODO proContractSnapshot.Work_Completed_for_Period__c = 0; proContractSnapshot.Services_Spend_for_Period__c = 0; proContractSnapshot.Expense_Travel_Spend_for_Period__c = 0; @@ -119,7 +119,7 @@ public class SnapshotHelper { sn.GS_Services_Handover_Completed_for_Per__c = (sn.GS_Services_Completed_Handover__c != null ? sn.GS_Services_Completed_Handover__c : 0) - (oldSn.GS_Services_Completed_Handover__c != null ? oldSn.GS_Services_Completed_Handover__c : 0); sn.GS_Net_Income_for_Period__c = (sn.GS_Net_Income_to_Date__c != null ? sn.GS_Net_Income_to_Date__c : 0) - (oldSn.GS_Net_Income_to_Date__c != null ? oldSn.GS_Net_Income_to_Date__c : 0); sn.GS_Net_Income_for_Period_by_Handover__c = (sn.GS_Net_Income_to_Date_Handover__c != null ? sn.GS_Net_Income_to_Date_Handover__c : 0) - (oldSn.GS_Net_Income_to_Date_Handover__c != null ? oldSn.GS_Net_Income_to_Date_Handover__c : 0); - } else { + } else { // TODO sn.Work_Completed_for_Period__c = 0; sn.Services_Spend_for_Period__c = 0; sn.Expense_Travel_Spend_for_Period__c = 0; From 34039cfa0cff7841baa009e6d3234433fd89384f Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Thu, 2 Mar 2017 09:18:02 +0100 Subject: [PATCH 14/15] Fixed Revenue calculations --- src/classes/ContractSnapshotTest.cls | 419 ++++-------------- src/classes/GSOverallCalculationBatch.cls | 205 ++++----- src/classes/GSOverallCalculationScheduler.cls | 2 +- src/classes/GSRevenueCalculationBatch.cls | 74 +--- src/classes/GSRevenueCalculationScheduler.cls | 2 +- src/classes/PROContractSnapshotBatch.cls | 8 +- src/classes/ProjectReportOutController.cls | 23 +- src/classes/SnapshotHelper.cls | 64 +-- 8 files changed, 205 insertions(+), 592 deletions(-) diff --git a/src/classes/ContractSnapshotTest.cls b/src/classes/ContractSnapshotTest.cls index a4ad6108..f65bc865 100644 --- a/src/classes/ContractSnapshotTest.cls +++ b/src/classes/ContractSnapshotTest.cls @@ -111,202 +111,23 @@ public class ContractSnapshotTest { insert dcontract3; insert dcontract4; - Test.startTest(); - Database.executeBatch(new GSRevenueCalculationBatch([SELECT Id FROM Business_Unit__c])); - Test.stopTest(); - - DContract__c conFromDB = [SELECT Id, GS_Revenue__c FROM DContract__c WHERE Id =: dcontract1.Id LIMIT 1]; - System.assertEquals(2910, conFromDB.GS_Revenue__c); - conFromDB = [SELECT Id, GS_Revenue__c FROM DContract__c WHERE Id =: dcontract2.Id LIMIT 1]; - System.assertEquals(2910, conFromDB.GS_Revenue__c); - conFromDB = [SELECT Id, GS_Revenue__c FROM DContract__c WHERE Id =: dcontract3.Id LIMIT 1]; - System.assertEquals(2910, conFromDB.GS_Revenue__c); - conFromDB = [SELECT Id, GS_Revenue__c FROM DContract__c WHERE Id =: dcontract4.Id LIMIT 1]; - System.assertEquals(2910, conFromDB.GS_Revenue__c); - } - - static testMethod void shouldCalculateGSQualitySituation1() { - Business_Unit__c bu = new Business_Unit__c(Name = 'Inc'); - insert bu; - DContract__c dcontract1 = new DContract__c(Name = 'Sample Test 1', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 10, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 1000); - DContract__c dcontract2 = new DContract__c(Name = 'Sample Test 2', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 20, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 2000); - DContract__c dcontract3 = new DContract__c(Name = 'Sample Test 3', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 30, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 3000); - DContract__c dcontract4 = new DContract__c(Name = 'Sample Test 4', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 40, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 4000); - insert dcontract1; - insert dcontract2; - insert dcontract3; - insert dcontract4; - - PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, - GS_Execution_Budget__c = 1367); - PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, - GS_Execution_Budget__c = 1333); - PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, - GS_Execution_Budget__c = 4567); - insert proS1; - insert proS2; - insert proS3; - insert proS4; - insert proS5; - insert proS6; - insert proS7; - insert proS8; - - Project_Report_Out__c pro1 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS1.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 9)); - Project_Report_Out__c pro2 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS2.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); - Project_Report_Out__c pro3 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS3.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); - - Project_Report_Out__c pro4 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS4.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 9)); - Project_Report_Out__c pro5 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS5.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); - - Project_Report_Out__c pro6 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS6.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); - Project_Report_Out__c pro7 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS7.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); - - Project_Report_Out__c pro8 = new Project_Report_Out__c(Contract__c = dcontract4.Id, Contract_Snapshot__c = proS8.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); - - insert pro1; - insert pro2; - insert pro3; - insert pro4; - insert pro5; - insert pro6; - insert pro7; - insert pro8; - - Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 8), 0, 0, 0)); - Test.stopTest(); - - PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; - Decimal value = proDromDB.GS_Quality_Overall_Status__c; - System.assertEquals(24.98, value.setScale(2)); - value = proDromDB.GS_Quality_Customer_Satisfaction__c; - System.assertEquals(25.99, value.setScale(2)); - // Expected Value = (400 + 444 + 678 + 543) / (1000 + 1367 + 1333 + 4567) = 2065 / 8267 = 24,97 - // Expected Value (Customer) = (250 + 223 + 900 + 776) / (1000 + 1367 + 1333 + 4567) = 2149 / 8267 = 25,99 - } - - static testMethod void shouldCalculateGSQualitySituation2() { - Business_Unit__c bu = new Business_Unit__c(Name = 'Inc'); - insert bu; - DContract__c dcontract1 = new DContract__c(Name = 'Sample Test 1', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 10, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 1000); - DContract__c dcontract2 = new DContract__c(Name = 'Sample Test 2', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 20, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 2000); - DContract__c dcontract3 = new DContract__c(Name = 'Sample Test 3', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 30, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 3000); - DContract__c dcontract4 = new DContract__c(Name = 'Sample Test 4', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 40, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 4000); - insert dcontract1; - insert dcontract2; - insert dcontract3; - insert dcontract4; - - PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, - GS_Execution_Budget__c = 1367); - PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, - GS_Execution_Budget__c = 1333); - PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, - GS_Execution_Budget__c = 4567); - insert proS1; - insert proS2; - insert proS3; - insert proS4; - insert proS5; - insert proS6; - insert proS7; - insert proS8; - - Project_Report_Out__c pro1 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS1.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); - Project_Report_Out__c pro2 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS2.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); - Project_Report_Out__c pro3 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS3.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); - - Project_Report_Out__c pro4 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS4.Id, Report_Out_Date__c = Date.newInstance(2017, 2, 23)); - Project_Report_Out__c pro5 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS5.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); - - Project_Report_Out__c pro6 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS6.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); - Project_Report_Out__c pro7 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS7.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); - - Project_Report_Out__c pro8 = new Project_Report_Out__c(Contract__c = dcontract4.Id, Contract_Snapshot__c = proS8.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); - - insert pro1; - insert pro2; - insert pro3; - insert pro4; - insert pro5; - insert pro6; - insert pro7; - insert pro8; - - Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 22), 0, 0, 0)); - Test.stopTest(); - - PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; - Decimal value = proDromDB.GS_Quality_Overall_Status__c; - System.assertEquals(24.98, value.setScale(2)); - value = proDromDB.GS_Quality_Customer_Satisfaction__c; - System.assertEquals(25.99, value.setScale(2)); - // Expected Value = (400 + 444 + 678 + 543) / (1000 + 1367 + 1333 + 4567) = 2065 / 8267 = 24,97 - // Expected Value (Customer) = (250 + 223 + 900 + 776) / (1000 + 1367 + 1333 + 4567) = 2149 / 8267 = 25,99 - } - - static testMethod void shouldCalculateGSQualitySituation3() { - Business_Unit__c bu = new Business_Unit__c(Name = 'Inc'); - insert bu; - DContract__c dcontract1 = new DContract__c(Name = 'Sample Test 1', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 10, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 1000); - DContract__c dcontract2 = new DContract__c(Name = 'Sample Test 2', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 20, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 2000); - DContract__c dcontract3 = new DContract__c(Name = 'Sample Test 3', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 30, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 3000); - DContract__c dcontract4 = new DContract__c(Name = 'Sample Test 4', Prime_Contracting_Business_Unit__c = bu.Id, Percent_through_SOW_deliverables__c = 40, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 4000); - insert dcontract1; - insert dcontract2; - insert dcontract3; - insert dcontract4; + PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, GS_Project_Services_Budget_Handoff_Cp__c = 1000, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + Work_Completed_for_Period__c = 5, GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 2, 9)); + PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, GS_Project_Services_Budget_Handoff_Cp__c = 1000, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + Work_Completed_for_Period__c = 5, GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 2, 23)); + PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, GS_Project_Services_Budget_Handoff_Cp__c = 1000, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + Work_Completed_for_Period__c = 5, GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 3, 9)); + PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Contract__c = dcontract2.Id, GS_Project_Services_Budget_Handoff_Cp__c = 1000, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + Work_Completed_for_Period__c = 5, GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 2, 9)); + PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Contract__c = dcontract2.Id, GS_Project_Services_Budget_Handoff_Cp__c = 1000, Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, + Work_Completed_for_Period__c = 3, GS_Execution_Budget__c = 1367, Snapshot_Date__c = Date.newInstance(2017, 2, 23)); + PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Contract__c = dcontract3.Id, GS_Project_Services_Budget_Handoff_Cp__c = 1000, Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, + Work_Completed_for_Period__c = 7, GS_Execution_Budget__c = 1333, Snapshot_Date__c = Date.newInstance(2017, 2, 23)); + PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Contract__c = dcontract3.Id, GS_Project_Services_Budget_Handoff_Cp__c = 1000, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + Work_Completed_for_Period__c = 7, GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 3, 9)); + PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Contract__c = dcontract4.Id, GS_Project_Services_Budget_Handoff_Cp__c = 1000, Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, + Work_Completed_for_Period__c = 15, GS_Execution_Budget__c = 4567, Snapshot_Date__c = Date.newInstance(2017, 2, 23)); - PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, - GS_Execution_Budget__c = 1367); - PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, - GS_Execution_Budget__c = 1333); - PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, - GS_Execution_Budget__c = 4567); insert proS1; insert proS2; insert proS3; @@ -316,93 +137,13 @@ public class ContractSnapshotTest { insert proS7; insert proS8; - Project_Report_Out__c pro1 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS1.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); - Project_Report_Out__c pro2 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS2.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); - Project_Report_Out__c pro3 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS3.Id, Report_Out_Date__c = Date.newInstance(2017, 4, 13)); - - Project_Report_Out__c pro4 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS4.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 9)); - Project_Report_Out__c pro5 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS5.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); - - Project_Report_Out__c pro6 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS6.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); - Project_Report_Out__c pro7 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS7.Id, Report_Out_Date__c = Date.newInstance(2017, 4, 13)); - - Project_Report_Out__c pro8 = new Project_Report_Out__c(Contract__c = dcontract4.Id, Contract_Snapshot__c = proS8.Id, Report_Out_Date__c = Date.newInstance(2017, 3, 23)); - - insert pro1; - insert pro2; - insert pro3; - insert pro4; - insert pro5; - insert pro6; - insert pro7; - insert pro8; - - Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 3, 27), 0, 0, 0)); - Test.stopTest(); - - PRO_Contract_Snapshot__c proDromDB = [SELECT Id, GS_Quality_Overall_Status__c, GS_Quality_Customer_Satisfaction__c FROM PRO_Contract_Snapshot__c WHERE Id =: proS8.Id]; - Decimal value = proDromDB.GS_Quality_Overall_Status__c; - System.assertEquals(24.98, value.setScale(2)); - value = proDromDB.GS_Quality_Customer_Satisfaction__c; - System.assertEquals(25.99, value.setScale(2)); - // Expected Value = (400 + 444 + 678 + 543) / (1000 + 1367 + 1333 + 4567) = 2065 / 8267 = 24,97 - // Expected Value (Customer) = (250 + 223 + 900 + 776) / (1000 + 1367 + 1333 + 4567) = 2149 / 8267 = 25,99 - } - - static testMethod void shouldCalculateGSQualityTotal() { - Business_Unit__c bu1 = new Business_Unit__c(Name = 'Inc'); - insert bu1; - Business_Unit__c bu2 = new Business_Unit__c(Name = 'Test BU'); - insert bu2; - DContract__c dcontract1 = new DContract__c(Name = 'Sample Test 1', Prime_Contracting_Business_Unit__c = bu1.Id, Percent_through_SOW_deliverables__c = 10, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 1000); - DContract__c dcontract2 = new DContract__c(Name = 'Sample Test 2', Prime_Contracting_Business_Unit__c = bu1.Id, Percent_through_SOW_deliverables__c = 20, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 2000); - DContract__c dcontract3 = new DContract__c(Name = 'Sample Test 3', Prime_Contracting_Business_Unit__c = bu2.Id, Percent_through_SOW_deliverables__c = 30, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 3000); - DContract__c dcontract4 = new DContract__c(Name = 'Sample Test 4', Prime_Contracting_Business_Unit__c = bu2.Id, Percent_through_SOW_deliverables__c = 40, Expected_Buffer__c = 0, - Contract_Expense_BUDGET__c = 0, of_Services_budget_for_GS__c = 100, Total_Amount_of_Contract__c = 4000); - insert dcontract1; - insert dcontract2; - insert dcontract3; - insert dcontract4; - - PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 200, Customer_Satisfaction_of_services__c = 100, - GS_Execution_Budget__c = 500); - PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 200, - GS_Execution_Budget__c = 900); - PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 600, Customer_Satisfaction_of_services__c = 300, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 800, Customer_Satisfaction_of_services__c = 400, - GS_Execution_Budget__c = 1500); - - insert proS1; - insert proS2; - insert proS3; - insert proS4; - - Project_Report_Out__c pro1 = new Project_Report_Out__c(Contract__c = dcontract1.Id, Contract_Snapshot__c = proS1.Id, Report_Out_Date__c = Date.newInstance(2017, 1, 26)); - Project_Report_Out__c pro2 = new Project_Report_Out__c(Contract__c = dcontract2.Id, Contract_Snapshot__c = proS2.Id, Report_Out_Date__c = Date.newInstance(2017, 1, 26)); - Project_Report_Out__c pro3 = new Project_Report_Out__c(Contract__c = dcontract3.Id, Contract_Snapshot__c = proS3.Id, Report_Out_Date__c = Date.newInstance(2017, 1, 26)); - Project_Report_Out__c pro4 = new Project_Report_Out__c(Contract__c = dcontract4.Id, Contract_Snapshot__c = proS4.Id, Report_Out_Date__c = Date.newInstance(2017, 1, 26)); - - insert pro1; - insert pro2; - insert pro3; - insert pro4; - Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c], Date.newInstance(2017, 2, 8), 0, 0, 0)); + Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 3, 8))); Test.stopTest(); - Contract_Statistic__c proDromDB = [SELECT Id, GS_Quality_Total_Overall_Status__c, GS_Quality_Total_Customer_Satisfaction__c FROM Contract_Statistic__c LIMIT 1]; - Decimal value = proDromDB.GS_Quality_Total_Overall_Status__c; - System.assertEquals(51.28, value.setScale(2)); - value = proDromDB.GS_Quality_Total_Customer_Satisfaction__c; - System.assertEquals(25.64, value.setScale(2)); - // Expected Value = (200 + 400 + 600 + 800) / (500 + 900 + 1000 + 1500) = 2000 / 3900 = 51,28 - // Expected Value (Customer) = (100 + 200 + 300 + 400) / (500 + 900 + 1000 + 1500) = 1000 / 3900 = 25,64 + Contract_Statistic__c proDromDB = [SELECT Id, Inc_GS_Revenue__c FROM Contract_Statistic__c LIMIT 1]; + Decimal value = proDromDB.Inc_GS_Revenue__c; + System.assertEquals(300.00, value.setScale(2)); } static testMethod void shouldCalculateGSQualitySituation1() { @@ -421,22 +162,22 @@ public class ContractSnapshotTest { insert dcontract3; insert dcontract4; - PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, - GS_Execution_Budget__c = 1367); - PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, - GS_Execution_Budget__c = 1333); - PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, - GS_Execution_Budget__c = 4567); + PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 2, 9)); + PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 2, 23)); + PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 3, 9)); + PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Contract__c = dcontract2.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 2, 9)); + PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Contract__c = dcontract2.Id, Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, + GS_Execution_Budget__c = 1367, Snapshot_Date__c = Date.newInstance(2017, 2, 23)); + PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Contract__c = dcontract3.Id, Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, + GS_Execution_Budget__c = 1333, Snapshot_Date__c = Date.newInstance(2017, 2, 23)); + PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Contract__c = dcontract3.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 3, 9)); + PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Contract__c = dcontract4.Id, Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, + GS_Execution_Budget__c = 4567, Snapshot_Date__c = Date.newInstance(2017, 2, 23)); insert proS1; insert proS2; insert proS3; @@ -468,7 +209,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id, Name FROM Business_Unit__c], new Contract_Statistic__c(), Date.newInstance(2017, 3, 8), 0, 0, 0)); + Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 3, 8))); Test.stopTest(); Contract_Statistic__c proDromDB = [SELECT Id, Inc_GS_Customer_Sat__c, Inc_GS_Overall_Status__c FROM Contract_Statistic__c LIMIT 1]; @@ -496,22 +237,22 @@ public class ContractSnapshotTest { insert dcontract3; insert dcontract4; - PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, - GS_Execution_Budget__c = 1367); - PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, - GS_Execution_Budget__c = 1333); - PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, - GS_Execution_Budget__c = 4567); + PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 2, 23)); + PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 3, 9)); + PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 3, 23)); + PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Contract__c = dcontract2.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 2, 23)); + PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Contract__c = dcontract2.Id, Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, + GS_Execution_Budget__c = 1367, Snapshot_Date__c = Date.newInstance(2017, 3, 9)); + PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Contract__c = dcontract3.Id, Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, + GS_Execution_Budget__c = 1333, Snapshot_Date__c = Date.newInstance(2017, 3, 9)); + PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Contract__c = dcontract3.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 3, 23)); + PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Contract__c = dcontract4.Id, Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, + GS_Execution_Budget__c = 4567, Snapshot_Date__c = Date.newInstance(2017, 3, 9)); insert proS1; insert proS2; insert proS3; @@ -543,7 +284,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id, Name FROM Business_Unit__c], new Contract_Statistic__c(), Date.newInstance(2017, 3, 22), 0, 0, 0)); + Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 3, 22))); Test.stopTest(); Contract_Statistic__c proDromDB = [SELECT Id, Inc_GS_Customer_Sat__c, Inc_GS_Overall_Status__c FROM Contract_Statistic__c LIMIT 1]; @@ -571,22 +312,22 @@ public class ContractSnapshotTest { insert dcontract3; insert dcontract4; - PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, - GS_Execution_Budget__c = 1367); - PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, - GS_Execution_Budget__c = 1333); - PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, - GS_Execution_Budget__c = 4567); + PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 3, 9)); + PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 3, 23)); + PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 4, 13)); + PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Contract__c = dcontract2.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 3, 9)); + PRO_Contract_Snapshot__c proS5 = new PRO_Contract_Snapshot__c(Contract__c = dcontract2.Id, Project_Quality_Overall_Status__c = 444, Customer_Satisfaction_of_services__c = 223, + GS_Execution_Budget__c = 1367, Snapshot_Date__c = Date.newInstance(2017, 3, 23)); + PRO_Contract_Snapshot__c proS6 = new PRO_Contract_Snapshot__c(Contract__c = dcontract3.Id, Project_Quality_Overall_Status__c = 678, Customer_Satisfaction_of_services__c = 900, + GS_Execution_Budget__c = 1333, Snapshot_Date__c = Date.newInstance(2017, 3, 23)); + PRO_Contract_Snapshot__c proS7 = new PRO_Contract_Snapshot__c(Contract__c = dcontract3.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 250, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 4, 13)); + PRO_Contract_Snapshot__c proS8 = new PRO_Contract_Snapshot__c(Contract__c = dcontract4.Id, Project_Quality_Overall_Status__c = 543, Customer_Satisfaction_of_services__c = 776, + GS_Execution_Budget__c = 4567, Snapshot_Date__c = Date.newInstance(2017, 3, 23)); insert proS1; insert proS2; insert proS3; @@ -618,7 +359,7 @@ public class ContractSnapshotTest { insert pro8; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id, Name FROM Business_Unit__c], new Contract_Statistic__c(), Date.newInstance(2017, 3, 27), 0, 0, 0)); + Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 3, 27))); Test.stopTest(); Contract_Statistic__c proDromDB = [SELECT Id, Inc_GS_Customer_Sat__c, Inc_GS_Overall_Status__c FROM Contract_Statistic__c LIMIT 1]; @@ -648,14 +389,14 @@ public class ContractSnapshotTest { insert dcontract3; insert dcontract4; - PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 200, Customer_Satisfaction_of_services__c = 100, - GS_Execution_Budget__c = 500); - PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 200, - GS_Execution_Budget__c = 900); - PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 600, Customer_Satisfaction_of_services__c = 300, - GS_Execution_Budget__c = 1000); - PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Project_Quality_Overall_Status__c = 800, Customer_Satisfaction_of_services__c = 400, - GS_Execution_Budget__c = 1500); + PRO_Contract_Snapshot__c proS1 = new PRO_Contract_Snapshot__c(Contract__c = dcontract1.Id, Project_Quality_Overall_Status__c = 200, Customer_Satisfaction_of_services__c = 100, + GS_Execution_Budget__c = 500, Snapshot_Date__c = Date.newInstance(2017, 1, 26)); + PRO_Contract_Snapshot__c proS2 = new PRO_Contract_Snapshot__c(Contract__c = dcontract2.Id, Project_Quality_Overall_Status__c = 400, Customer_Satisfaction_of_services__c = 200, + GS_Execution_Budget__c = 900, Snapshot_Date__c = Date.newInstance(2017, 1, 26)); + PRO_Contract_Snapshot__c proS3 = new PRO_Contract_Snapshot__c(Contract__c = dcontract3.Id, Project_Quality_Overall_Status__c = 600, Customer_Satisfaction_of_services__c = 300, + GS_Execution_Budget__c = 1000, Snapshot_Date__c = Date.newInstance(2017, 1, 26)); + PRO_Contract_Snapshot__c proS4 = new PRO_Contract_Snapshot__c(Contract__c = dcontract4.Id, Project_Quality_Overall_Status__c = 800, Customer_Satisfaction_of_services__c = 400, + GS_Execution_Budget__c = 1500, Snapshot_Date__c = Date.newInstance(2017, 1, 26)); insert proS1; insert proS2; @@ -673,7 +414,7 @@ public class ContractSnapshotTest { insert pro4; Test.startTest(); - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id, Name FROM Business_Unit__c], new Contract_Statistic__c(), Date.newInstance(2017, 2, 8), 0, 0, 0)); + Database.executeBatch(new GSOverallCalculationBatch(Date.newInstance(2017, 2, 8))); Test.stopTest(); Contract_Statistic__c proDromDB = [SELECT Id, GS_Quality_Total_Overall_Status__c, GS_Quality_Total_Customer_Satisfaction__c FROM Contract_Statistic__c LIMIT 1]; diff --git a/src/classes/GSOverallCalculationBatch.cls b/src/classes/GSOverallCalculationBatch.cls index 977dab04..b2e39087 100644 --- a/src/classes/GSOverallCalculationBatch.cls +++ b/src/classes/GSOverallCalculationBatch.cls @@ -1,145 +1,111 @@ /** * Calculates GS Quality for Overall Status and for Customer Satisfaction in PRO Snapshots. */ -public without sharing class GSOverallCalculationBatch implements Database.Batchable, Database.Stateful { +public without sharing class GSOverallCalculationBatch implements Database.Batchable, Database.Stateful { - private List businessUnits; - private Business_Unit__c currentUnit; - - private Double gsOverall; - private Double gsSatisfaction; - private Double gsBudget; + private Date startDate; + private Date endDate; + private Date currentDate; private Double gsTotalOverall; private Double gsTotalSatisfaction; private Double gsTotalBudget; - private Date currentDate; - private Date cycleStartDate; - private Contract_Statistic__c contractStats; - - public GSOverallCalculationBatch(List businessUnits) { - this.businessUnits = businessUnits; - if (this.businessUnits.size() > 0) { - this.currentUnit = this.businessUnits.get(0); - this.businessUnits.remove(0); - } - this.gsOverall = 0; - this.gsSatisfaction = 0; - this.gsBudget = 0; + + public GSOverallCalculationBatch() { + this.currentDate = Date.today(); this.gsTotalOverall = 0; this.gsTotalSatisfaction = 0; this.gsTotalBudget = 0; - this.currentDate = Date.today(); - this.contractStats = new Contract_Statistic__c(); } - public GSOverallCalculationBatch(List businessUnits, Contract_Statistic__c contractStats, Date currentDate, - Double gsTotalOverall, Double gsTotalSatisfaction, Double gsTotalBudget) { - this.businessUnits = businessUnits; - if (this.businessUnits.size() > 0) { - this.currentUnit = this.businessUnits.get(0); - this.businessUnits.remove(0); - } - this.gsOverall = 0; - this.gsSatisfaction = 0; - this.gsBudget = 0; - this.gsTotalOverall = gsTotalOverall; - this.gsTotalSatisfaction = gsTotalSatisfaction; - this.gsTotalBudget = gsTotalBudget; + public GSOverallCalculationBatch(Date currentDate) { this.currentDate = currentDate; - this.contractStats = contractStats; + this.gsTotalOverall = 0; + this.gsTotalSatisfaction = 0; + this.gsTotalBudget = 0; } - public List start(Database.BatchableContext BC) { + public List start(Database.BatchableContext BC) { List thursDayList = PROHelper.GetAllThursDayOfMonth(this.currentDate.year(), this.currentDate.month()); List thursDayListLastMonth = PROHelper.GetAllThursDayOfMonth(this.currentDate.addMonths(-1).year(), this.currentDate.addMonths(-1).month()); List thursDayListNextMonth = PROHelper.GetAllThursDayOfMonth(this.currentDate.addMonths(1).year(), this.currentDate.addMonths(1).month()); - Date startDate; - Date endDate; + if (this.currentDate < thursDayList[1]) { - startDate = thursDayListLastMonth[3]; - endDate = thursDayList[1]; + this.startDate = thursDayListLastMonth[3]; + this.endDate = thursDayList[1]; } else if (this.currentDate < thursDayList[3]) { - startDate = thursDayList[1]; - endDate = thursDayList[3]; + this.startDate = thursDayList[1]; + this.endDate = thursDayList[3]; } else { - startDate = thursDayList[3]; - endDate = thursDayListNextMonth[1]; + this.startDate = thursDayList[3]; + this.endDate = thursDayListNextMonth[1]; } - System.debug('Start date : ' + startDate); - System.debug('End date : ' + endDate); - - this.cycleStartDate = startDate; - if (this.currentUnit == null) { - return new List(); - } - return [SELECT Id, Contract__c, Report_Out_Date__c, Contract_Snapshot__c, Contract_Snapshot__r.Project_Quality_Overall_Status__c, - Contract_Snapshot__r.Customer_Satisfaction_of_services__c, Contract_Snapshot__r.GS_Execution_Budget__c - FROM Project_Report_Out__c WHERE Contract__r.Prime_Contracting_Business_Unit__c =: this.currentUnit.Id AND Contract_Snapshot__c != '' - AND Report_Out_Date__c >=: startDate AND Report_Out_Date__c <: endDate]; + return [SELECT Id, Name FROM Business_Unit__c]; } - public void execute(Database.BatchableContext BC, List scope) { + public void execute(Database.BatchableContext BC, List scope) { try { - System.debug('scope ; ' + scope.size()); - for (Project_Report_Out__c pro : scope) { - System.debug(pro.Report_Out_Date__c); - this.gsOverall += pro.Contract_Snapshot__r.Project_Quality_Overall_Status__c != null ? pro.Contract_Snapshot__r.Project_Quality_Overall_Status__c : 0; - this.gsSatisfaction += pro.Contract_Snapshot__r.Customer_Satisfaction_of_services__c != null ? pro.Contract_Snapshot__r.Customer_Satisfaction_of_services__c : 0; - this.gsBudget += pro.Contract_Snapshot__r.GS_Execution_Budget__c != null ? pro.Contract_Snapshot__r.GS_Execution_Budget__c : 0; + Map buMapping = new Map(); + for (Business_Unit__c bu : scope) { + buMapping.put(bu.Id, bu.Name); } - } catch (Exception ex) { - System.debug('Error : ' + ex); - if (!Test.isRunningTest()) { - BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); - EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'GS Quality calculation error', 'GS Quality calculation error', ex); - } - } - } - public void finish(Database.BatchableContext BC) { - try { - System.debug('this.gsOverall : ' + this.gsOverall); - System.debug('this.gsSatisfaction : ' + this.gsSatisfaction); - System.debug('this.gsBudget : ' + this.gsBudget); - this.gsTotalOverall += this.gsOverall; - this.gsTotalSatisfaction += this.gsSatisfaction; - this.gsTotalBudget += this.gsBudget; - - if (this.gsBudget == 0) { - this.gsOverall = 0; - this.gsSatisfaction = 0; - } else { - this.gsOverall = 100 * (this.gsOverall / this.gsBudget); - this.gsSatisfaction = 100 * (this.gsSatisfaction / this.gsBudget); - } + AggregateResult[] groupedResults = [SELECT Contract__r.Prime_Contracting_Business_Unit__c, SUM(Project_Quality_Overall_Status__c), SUM(Customer_Satisfaction_of_services__c), + SUM(GS_Execution_Budget__c), SUM(GS_Revenue__c) + FROM PRO_Contract_Snapshot__c WHERE Contract__c != null AND Snapshot_Date__c >=: this.startDate AND Snapshot_Date__c <: this.endDate + GROUP BY Contract__r.Prime_Contracting_Business_Unit__c]; - String qualityField = (this.currentUnit.Name + '_GS_Overall_Status__c').toLowerCase(); - String customerField = (this.currentUnit.Name + '_GS_Customer_Sat__c').toLowerCase(); - Sobject sobj = (Sobject) this.contractStats; - if (hasSObjectField(qualityField, sobj) && hasSObjectField(customerField, sobj)) { - sobj.put(qualityField, this.gsOverall); - sobj.put(customerField, this.gsSatisfaction); - } else { - System.debug('cannot find field with name : ' + qualityField + ' ' + customerField); - if (!Test.isRunningTest()) { - EmailHelper.sendEmail(BatchDefaultSettings__c.getOrgDefaults().Error_Emails__c.split(','), 'Cannot find sObject field for GS Quality', - 'Cannot find fields: ' + qualityField + ', ' + customerField); + Sobject sobj = (Sobject) new Contract_Statistic__c(PRO_Cycle_Date__c = this.startDate , DLAC_GS_Customer_Sat__c = 0, DLAC_GS_Overall_Status__c = 0, DSA_GS_Customer_Sat__c = 0, + DSA_GS_Overall_Status__c = 0, DSI_GS_Customer_Sat__c = 0, DSI_GS_Overall_Status__c = 0, DTZ_GS_Customer_Sat__c = 0, DTZ_GS_Overall_Status__c = 0, + DWA_GS_Customer_Sat__c = 0, DWA_GS_Overall_Status__c = 0, Inc_GS_Customer_Sat__c = 0, Inc_GS_Overall_Status__c = 0, Myanmar_GS_Customer_Sat__c = 0, + Myanmar_GS_Overall_Status__c = 0, GS_Quality_Total_Customer_Satisfaction__c = 0, GS_Quality_Total_Overall_Status__c = 0, DLAC_GS_Revenue__c = 0, + DSA_GS_Revenue__c = 0, DSI_GS_Revenue__c = 0, DTZ_GS_Revenue__c = 0, DWA_GS_Revenue__c = 0, Inc_GS_Revenue__c = 0, Myanmar_GS_Revenue__c = 0); + + System.debug('groupedResults'+ groupedResults); + for (AggregateResult result : groupedResults) { + Id buId = (Id) result.get('Prime_Contracting_Business_Unit__c'); + if (buId == null) { + continue; + } + + Double gsOverall = (Double) result.get('expr0'); + Double gsSatisfaction = (Double) result.get('expr1'); + Double gsBudget = (Double) result.get('expr2'); + Double gsRevenue = (Double) result.get('expr3'); + + this.gsTotalOverall += gsOverall != null ? gsOverall : 0; + this.gsTotalSatisfaction += gsSatisfaction != null ? gsSatisfaction : 0; + this.gsTotalBudget += gsBudget != null ? gsBudget : 0; + + if (gsBudget == null || gsBudget == 0 || gsOverall == null || gsSatisfaction == null) { + gsOverall = 0; + gsSatisfaction = 0; + } else { + gsOverall = 100 * (gsOverall / gsBudget); + gsSatisfaction = 100 * (gsSatisfaction / gsBudget); + } + + if (gsRevenue == null) { + gsRevenue = 0; + } + + String buName = buMapping.get(buId); + String qualityField = (buName + '_GS_Overall_Status__c').toLowerCase(); + String customerField = (buName + '_GS_Customer_Sat__c').toLowerCase(); + String revenueField = (buName + '_GS_Revenue__c').toLowerCase(); + if (hasSObjectField(qualityField, sobj) && hasSObjectField(customerField, sobj)) { + sobj.put(qualityField, gsOverall); + sobj.put(customerField, gsSatisfaction); + sobj.put(revenueField, gsRevenue); + } else { + System.debug('Cannot find field with name : ' + qualityField + ' ' + customerField); + if (!Test.isRunningTest()) { + EmailHelper.sendEmail(BatchDefaultSettings__c.getOrgDefaults().Error_Emails__c.split(','), 'Cannot find sObject field for GS Quality', + 'Cannot find fields: ' + qualityField + ', ' + customerField + ', ' + revenueField); + } } } - } catch (Exception ex) { - System.debug('Error : ' + ex); - if (!Test.isRunningTest()) { - BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); - EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'GS Quality calculation error', 'GS Quality calculation error', ex); - } - } - if (this.businessUnits.size() > 0) { - GSOverallCalculationBatch overallBatch = new GSOverallCalculationBatch(this.businessUnits, this.contractStats, this.currentDate, this.gsTotalOverall, - this.gsTotalSatisfaction, this.gsTotalBudget); - Database.executeBatch(overallBatch); - } else { if (this.gsTotalBudget != 0) { this.gsTotalSatisfaction = 100 * (this.gsTotalSatisfaction / this.gsTotalBudget); this.gsTotalOverall = 100 * (this.gsTotalOverall / this.gsTotalBudget); @@ -147,13 +113,24 @@ public without sharing class GSOverallCalculationBatch implements Database.Batch this.gsTotalSatisfaction = 0; this.gsTotalOverall = 0; } - this.contractStats.PRO_Cycle_Date__c = this.cycleStartDate; - this.contractStats.GS_Quality_Total_Overall_Status__c = this.gsTotalOverall; - this.contractStats.GS_Quality_Total_Customer_Satisfaction__c = this.gsTotalSatisfaction; - insert this.contractStats; + + Contract_Statistic__c contractStats = (Contract_Statistic__c) sobj; + contractStats.GS_Quality_Total_Overall_Status__c = this.gsTotalOverall; + contractStats.GS_Quality_Total_Customer_Satisfaction__c = this.gsTotalSatisfaction; + insert contractStats; + + } catch (Exception ex) { + System.debug('Error : ' + ex); + if (!Test.isRunningTest()) { + BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); + EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'GS Quality calculation error', 'GS Quality calculation error', ex); + } } } + public void finish(Database.BatchableContext BC) { + } + private Boolean hasSObjectField(String fieldName, SObject so) { return so.getSobjectType().getDescribe().fields.getMap().keySet().contains(fieldName); } diff --git a/src/classes/GSOverallCalculationScheduler.cls b/src/classes/GSOverallCalculationScheduler.cls index 867f8a51..922e4fdf 100644 --- a/src/classes/GSOverallCalculationScheduler.cls +++ b/src/classes/GSOverallCalculationScheduler.cls @@ -19,7 +19,7 @@ public without sharing class GSOverallCalculationScheduler implements Schedulabl } if (currentDate == endDate.addDays(-1)) { - GSOverallCalculationBatch overallBatch = new GSOverallCalculationBatch([SELECT Id, Name FROM Business_Unit__c]); + GSOverallCalculationBatch overallBatch = new GSOverallCalculationBatch(); Database.executeBatch(overallBatch); } } diff --git a/src/classes/GSRevenueCalculationBatch.cls b/src/classes/GSRevenueCalculationBatch.cls index 09367aac..0a65ebc3 100644 --- a/src/classes/GSRevenueCalculationBatch.cls +++ b/src/classes/GSRevenueCalculationBatch.cls @@ -3,95 +3,25 @@ */ public without sharing class GSRevenueCalculationBatch implements Database.Batchable, Database.Stateful{ - private List businessUnits; - private Business_Unit__c currentUnit; - private Double gsRevenue; - private List contracts; public GSRevenueCalculationBatch(List businessUnits) { - this.businessUnits = businessUnits; - if (this.businessUnits.size() > 0) { - this.currentUnit = this.businessUnits.get(0); - this.businessUnits.remove(0); - } - this.gsRevenue = 0; - this.contracts = new List(); + } public List start(Database.BatchableContext BC) { - if (this.currentUnit != null) { - return [SELECT Id, Name, GS_Execution_Budget__c, Percent_through_SOW_deliverables__c, GS_Revenue__c FROM DContract__c WHERE Prime_Contracting_Business_Unit__c =: this.currentUnit.Id]; - } return new List(); } public void execute(Database.BatchableContext BC, List scope) { - try { - Map workMap = getHistoryMap(scope); - for (DContract__c con : scope) { - Decimal val = workMap.get(con.Id); - if (val == null) { - val = con.Percent_through_SOW_deliverables__c; - } - this.gsRevenue += (val != null ? val / 100.00 : 0) * (con.GS_Execution_Budget__c != null ? con.GS_Execution_Budget__c : 0); - this.contracts.add(con); - } - } catch (Exception ex) { - System.debug('Error : ' + ex); - if (!Test.isRunningTest()) { - BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); - EmailHelper.sendEmailFromException(settings.Error_Emails__c.split(','), 'GS Revenue calculation error', 'GS Revenue calculation error', ex); - } - } + } public void finish(Database.BatchableContext BC) { - List toUpdate = new List(); - for (DContract__c con : this.contracts) { - if (this.gsRevenue != con.GS_Revenue__c) { - con.GS_Revenue__c = this.gsRevenue; - toUpdate.add(con); - } - } - List updateResults = Database.update(toUpdate, false); - String emailBody = ''; - for(Integer i = 0; i < updateResults.size(); i++){ - if (!updateResults.get(i).isSuccess()){ - Database.Error error = updateResults.get(i).getErrors().get(0); - System.debug('Error : Fields - ' + error.getFields() + ' Message - ' + error.getMessage()); - emailBody += '' + toUpdate.get(i).Id + '' + error.getFields()+ '' + error.getStatusCode()+ '' + error.getMessage() + ''; - } - } - - if (emailBody != '') { - BatchDefaultSettings__c settings = BatchDefaultSettings__c.getOrgDefaults(); - emailBody = 'td>Fieldstd>Code' + emailBody + '
IdError Message

'; - EmailHelper.sendEmail(settings.Error_Emails__c.split(','), 'GS Revenue calculation error', emailBody); - } - - if (this.businessUnits.size() > 0) { - Database.executeBatch(new GSRevenueCalculationBatch(this.businessUnits)); - } else { - Database.executeBatch(new GSOverallCalculationBatch([SELECT Id FROM Business_Unit__c])); - } } private Map getHistoryMap(List scope) { - if (Test.isRunningTest()) { - return getTestDate(scope); - } - - List historyList = [SELECT OldValue, parentId, CreatedDate FROM DContract__History WHERE parentId IN: scope AND Field = 'Percent_through_SOW_deliverables__c' - AND CreatedDate = THIS_MONTH ORDER BY CreatedDate ASC]; Map historyMap = new Map (); - - for (DContract__History hRec : historyList) { - if (!historyMap.containsKey(hRec.parentId)) { - historyMap.put(hRec.parentId, hRec.OldValue != null ? (Decimal) hRec.OldValue : 0); - } - } - return historyMap; } diff --git a/src/classes/GSRevenueCalculationScheduler.cls b/src/classes/GSRevenueCalculationScheduler.cls index eb8c05d8..7f1c4775 100644 --- a/src/classes/GSRevenueCalculationScheduler.cls +++ b/src/classes/GSRevenueCalculationScheduler.cls @@ -1,6 +1,6 @@ public without sharing class GSRevenueCalculationScheduler implements Schedulable { public void execute(SchedulableContext sc) { - Database.executeBatch(new GSRevenueCalculationBatch([SELECT Id FROM Business_Unit__c])); + } } \ No newline at end of file diff --git a/src/classes/PROContractSnapshotBatch.cls b/src/classes/PROContractSnapshotBatch.cls index 6ea3effb..07a90286 100644 --- a/src/classes/PROContractSnapshotBatch.cls +++ b/src/classes/PROContractSnapshotBatch.cls @@ -25,7 +25,7 @@ global class PROContractSnapshotBatch implements Database.Batchable GetContract() { - List contractList = [SELECT Id, GS_Revenue__c, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, + List contractList = [SELECT Id, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c, - GS_Net_Income_to_Date_Handover__c, GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Services_Completed_Handover__c - FROM DContract__c WHERE Status__c = 'Active' AND Requires_Report_Out__c = 'No']; + GS_Net_Income_to_Date_Handover__c, GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Services_Completed_Handover__c, + GS_Project_Services_Budget_Handoff__c FROM DContract__c WHERE Status__c = 'Active' AND Requires_Report_Out__c = 'No']; return contractList; } } \ No newline at end of file diff --git a/src/classes/ProjectReportOutController.cls b/src/classes/ProjectReportOutController.cls index dc0422e7..f3822690 100644 --- a/src/classes/ProjectReportOutController.cls +++ b/src/classes/ProjectReportOutController.cls @@ -231,7 +231,7 @@ public with sharing class ProjectReportOutController { DContract__c dcontract = [SELECT Id, Name, Last_Report_Out_Status__c, Percent_through_SOW_deliverables__c, Budget_Used__c, of_Services_budget_for_GS__c, Total_Costs__c, Services_Spend__c, Buffer_by_SOW_Completed__c, Execution_Efficiency__c, Expected_Buffer__c, Direct_Costs_Total_Expenses__c, Net_Income_based_on_SOW_Completed__c, Product_Spend__c, GS_Execution_Budget__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c, GS_Services_Completed_Handover__c, - GS_Revenue__c, Project_Quality_Overall_Status__c, Customer_Satisfaction_of_services__c FROM DContract__c WHERE Id =: reportContractId]; + Project_Quality_Overall_Status__c, Customer_Satisfaction_of_services__c, GS_Project_Services_Budget_Handoff__c FROM DContract__c WHERE Id =: reportContractId]; dcontract.Last_Report_Out_Status__c = status; dcontract.Percent_through_SOW_deliverables__c = proTabContractPercntgethroughSOW; @@ -358,26 +358,7 @@ public with sharing class ProjectReportOutController { proContractSnapshot.Id = currentSnapshot; } - return SnapshotHelper.fillPROSnapshot(proContractSnapshot, dcontract, SnapshotHelper.getLastPROSnapshot(dcontract)); - /** - proContractSnapshot.Budget_Used__c = dcontract.Budget_Used__c; - proContractSnapshot.Percent_Of_Work_Completed__c = dcontract.Percent_through_SOW_deliverables__c; - proContractSnapshot.Percent_Services_Budget_For_GS__c = dcontract.of_Services_budget_for_GS__c; - proContractSnapshot.Buffer_By_Deliverables_Complete__c = dcontract.Buffer_by_SOW_Completed__c; - proContractSnapshot.Execution_Efficiency__c = dcontract.Execution_Efficiency__c; - proContractSnapshot.Expected_Buffer__c = dcontract.Expected_Buffer__c; - proContractSnapshot.Expense_And_Travel_Spend__c = dcontract.Direct_Costs_Total_Expenses__c; - proContractSnapshot.Net_Income_By_Deliverables_Complete__c = dcontract.Net_Income_based_on_SOW_Completed__c; - proContractSnapshot.Product_Spend__c = dcontract.Product_Spend__c; - proContractSnapshot.Services_Spend__c = dcontract.Services_Spend__c; - proContractSnapshot.Total_Calculated_Costs__c = dcontract.Total_Costs__c; - proContractSnapshot.Contract__c = dcontract.Id; - proContractSnapshot.Name = dcontract.Name; - proContractSnapshot.Snapshot_Date__c = Date.today(); - proContractSnapshot.GS_Execution_Budget__c = dcontract.GS_Execution_Budget__c; - return proContractSnapshot; - */ - + return SnapshotHelper.fillPROSnapshot(pro.Report_Out_Date__c, proContractSnapshot, dcontract, SnapshotHelper.getLastPROSnapshot(dcontract, currentSnapshot)); } // Get a list of picklist values from an existing object field. diff --git a/src/classes/SnapshotHelper.cls b/src/classes/SnapshotHelper.cls index fe1f311d..c8648055 100644 --- a/src/classes/SnapshotHelper.cls +++ b/src/classes/SnapshotHelper.cls @@ -5,7 +5,7 @@ public class SnapshotHelper { public static Map getOldPROSnapshot(List contracts) { Map snapshotsMap = new Map(); - List snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c, + List snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c, Product_Spend2__c, Contract__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: Date.today() AND Contract__c IN: contracts ORDER BY Snapshot_Date__c DESC]; @@ -18,10 +18,18 @@ public class SnapshotHelper { return snapshotsMap; } - public static PRO_Contract_Snapshot__c getLastPROSnapshot(DContract__c dcontract) { - List snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c, + public static PRO_Contract_Snapshot__c getLastPROSnapshot(DContract__c dcontract, Id snapshotId) { + List snapshotsList; + if (snapshotId == null) { + snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c, Product_Spend2__c, Contract__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: Date.today() AND Contract__c =: dcontract.Id ORDER BY Snapshot_Date__c DESC LIMIT 1]; + } else { + snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c, Product_Spend2__c, + Contract__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: Date.today() + AND Contract__c =: dcontract.Id AND Id !=: snapshotId ORDER BY Snapshot_Date__c DESC LIMIT 1]; + } + if (snapshotsList.size() > 0) { return snapshotsList.get(0); } @@ -29,7 +37,7 @@ public class SnapshotHelper { return new PRO_Contract_Snapshot__c(); } - public static PRO_Contract_Snapshot__c fillPROSnapshot(PRO_Contract_Snapshot__c proContractSnapshot, DContract__c dcontract, PRO_Contract_Snapshot__c oldSnapshot) { + public static PRO_Contract_Snapshot__c fillPROSnapshot(Date proDate, PRO_Contract_Snapshot__c proContractSnapshot, DContract__c dcontract, PRO_Contract_Snapshot__c oldSnapshot) { proContractSnapshot.Budget_Used__c = dcontract.Budget_Used__c; proContractSnapshot.Percent_Of_Work_Completed__c = dcontract.Percent_through_SOW_deliverables__c; proContractSnapshot.Percent_Services_Budget_For_GS__c = dcontract.of_Services_budget_for_GS__c; @@ -38,40 +46,28 @@ public class SnapshotHelper { proContractSnapshot.Expected_Buffer__c = dcontract.Expected_Buffer__c; proContractSnapshot.Expense_And_Travel_Spend__c = dcontract.Direct_Costs_Total_Expenses__c; proContractSnapshot.Net_Income_By_Deliverables_Complete__c = dcontract.Net_Income_based_on_SOW_Completed__c; - proContractSnapshot.Product_Spend__c = dcontract.Product_Spend__c; + proContractSnapshot.Product_Spend2__c = dcontract.Product_Spend__c; proContractSnapshot.Services_Spend__c = dcontract.Services_Spend__c; proContractSnapshot.Total_Calculated_Costs__c = dcontract.Total_Costs__c; proContractSnapshot.Contract__c = dcontract.Id; proContractSnapshot.Name = dcontract.Name; - proContractSnapshot.Snapshot_Date__c = Date.today(); + proContractSnapshot.Snapshot_Date__c = proDate; proContractSnapshot.GS_Execution_Budget__c = dcontract.GS_Execution_Budget__c; proContractSnapshot.GS_Services_Completed_Handover__c = dcontract.GS_Services_Completed_Handover__c; proContractSnapshot.GS_Services_Completed__c = dcontract.GS_Services_Completed__c; proContractSnapshot.GS_Net_Income_to_Date__c = dcontract.GS_Net_Income_to_Date__c; proContractSnapshot.GS_Net_Income_to_Date_Handover__c = dcontract.GS_Net_Income_to_Date_Handover__c; - // TODO FORMULA field proContractSnapshot.GS_Revenue__c = dcontract.GS_Revenue__c; + proContractSnapshot.GS_Project_Services_Budget_Handoff_Cp__c = dcontract.GS_Project_Services_Budget_Handoff__c; - if (oldSnapshot.Id != null) { - proContractSnapshot.Work_Completed_for_Period__c = (dcontract.Percent_through_SOW_deliverables__c != null ? dcontract.Percent_through_SOW_deliverables__c : 0) - (oldSnapshot.Percent_Of_Work_Completed__c != null ? oldSnapshot.Percent_Of_Work_Completed__c : 0); - proContractSnapshot.Services_Spend_for_Period__c = (dcontract.Services_Spend__c != null ? dcontract.Services_Spend__c : 0) - (oldSnapshot.Services_Spend__c != null ? oldSnapshot.Services_Spend__c : 0); - proContractSnapshot.Expense_Travel_Spend_for_Period__c = (dcontract.Direct_Costs_Total_Expenses__c != null ? dcontract.Direct_Costs_Total_Expenses__c : 0) - (oldSnapshot.Expense_And_Travel_Spend__c != null ? oldSnapshot.Expense_And_Travel_Spend__c : 0); - proContractSnapshot.Product_Spend_for_Period__c = (dcontract.Product_Spend__c != null ? dcontract.Product_Spend__c : 0) - (oldSnapshot.Product_Spend__c != null ? oldSnapshot.Product_Spend__c : 0); - proContractSnapshot.Total_Calculated_Costs_for_Period__c = (dcontract.Total_Costs__c != null ? dcontract.Total_Costs__c : 0) - (oldSnapshot.Total_Calculated_Costs__c != null ? oldSnapshot.Total_Calculated_Costs__c : 0); - proContractSnapshot.GS_Services_Handover_Completed_for_Per__c = (dcontract.GS_Services_Completed_Handover__c != null ? dcontract.GS_Services_Completed_Handover__c : 0) - (oldSnapshot.GS_Services_Completed_Handover__c != null ? oldSnapshot.GS_Services_Completed_Handover__c : 0); - proContractSnapshot.GS_Services_Completed_for_Period__c = (dcontract.GS_Services_Completed__c != null ? dcontract.GS_Services_Completed__c : 0) - (oldSnapshot.GS_Services_Completed__c != null ? oldSnapshot.GS_Services_Completed__c : 0); - proContractSnapshot.GS_Net_Income_for_Period__c = (dcontract.GS_Net_Income_to_Date__c != null ? dcontract.GS_Net_Income_to_Date__c : 0) - (oldSnapshot.GS_Net_Income_to_Date__c != null ? oldSnapshot.GS_Net_Income_to_Date__c : 0); - proContractSnapshot.GS_Net_Income_for_Period_by_Handover__c = (dcontract.GS_Net_Income_to_Date_Handover__c != null ? dcontract.GS_Net_Income_to_Date_Handover__c : 0) - (oldSnapshot.GS_Net_Income_to_Date_Handover__c != null ? oldSnapshot.GS_Net_Income_to_Date_Handover__c : 0); - } else { //TODO - proContractSnapshot.Work_Completed_for_Period__c = 0; - proContractSnapshot.Services_Spend_for_Period__c = 0; - proContractSnapshot.Expense_Travel_Spend_for_Period__c = 0; - proContractSnapshot.Product_Spend_for_Period__c = 0; - proContractSnapshot.Total_Calculated_Costs_for_Period__c = 0; - proContractSnapshot.GS_Services_Handover_Completed_for_Per__c = 0; - proContractSnapshot.GS_Services_Completed_for_Period__c = 0; - proContractSnapshot.GS_Net_Income_for_Period__c = 0; - proContractSnapshot.GS_Net_Income_for_Period_by_Handover__c = 0; - } + proContractSnapshot.Work_Completed_for_Period__c = (dcontract.Percent_through_SOW_deliverables__c != null ? dcontract.Percent_through_SOW_deliverables__c : 0) - (oldSnapshot.Percent_Of_Work_Completed__c != null ? oldSnapshot.Percent_Of_Work_Completed__c : 0); + proContractSnapshot.Services_Spend_for_Period__c = (dcontract.Services_Spend__c != null ? dcontract.Services_Spend__c : 0) - (oldSnapshot.Services_Spend__c != null ? oldSnapshot.Services_Spend__c : 0); + proContractSnapshot.Expense_Travel_Spend_for_Period__c = (dcontract.Direct_Costs_Total_Expenses__c != null ? dcontract.Direct_Costs_Total_Expenses__c : 0) - (oldSnapshot.Expense_And_Travel_Spend__c != null ? oldSnapshot.Expense_And_Travel_Spend__c : 0); + proContractSnapshot.Product_Spend_for_Period__c = (dcontract.Product_Spend__c != null ? dcontract.Product_Spend__c : 0) - (oldSnapshot.Product_Spend2__c != null ? oldSnapshot.Product_Spend2__c : 0); + proContractSnapshot.Total_Calculated_Costs_for_Period__c = (dcontract.Total_Costs__c != null ? dcontract.Total_Costs__c : 0) - (oldSnapshot.Total_Calculated_Costs__c != null ? oldSnapshot.Total_Calculated_Costs__c : 0); + proContractSnapshot.GS_Services_Handover_Completed_for_Per__c = (dcontract.GS_Services_Completed_Handover__c != null ? dcontract.GS_Services_Completed_Handover__c : 0) - (oldSnapshot.GS_Services_Completed_Handover__c != null ? oldSnapshot.GS_Services_Completed_Handover__c : 0); + proContractSnapshot.GS_Services_Completed_for_Period__c = (dcontract.GS_Services_Completed__c != null ? dcontract.GS_Services_Completed__c : 0) - (oldSnapshot.GS_Services_Completed__c != null ? oldSnapshot.GS_Services_Completed__c : 0); + proContractSnapshot.GS_Net_Income_for_Period__c = (dcontract.GS_Net_Income_to_Date__c != null ? dcontract.GS_Net_Income_to_Date__c : 0) - (oldSnapshot.GS_Net_Income_to_Date__c != null ? oldSnapshot.GS_Net_Income_to_Date__c : 0); + proContractSnapshot.GS_Net_Income_for_Period_by_Handover__c = (dcontract.GS_Net_Income_to_Date_Handover__c != null ? dcontract.GS_Net_Income_to_Date_Handover__c : 0) - (oldSnapshot.GS_Net_Income_to_Date_Handover__c != null ? oldSnapshot.GS_Net_Income_to_Date_Handover__c : 0); return proContractSnapshot; } @@ -109,7 +105,6 @@ public class SnapshotHelper { } public static Pipeline_Snapshot__c calculateForPeriod(Pipeline_Snapshot__c sn, Pipeline_Snapshot__c oldSn) { - if (oldSn.Id != null) { sn.Work_Completed_for_Period__c = (sn.Work_Completed__c != null ? sn.Work_Completed__c : 0) - (oldSn.Work_Completed__c != null ? oldSn.Work_Completed__c : 0); sn.Services_Spend_for_Period__c = (sn.Services_Spend__c != null ? sn.Services_Spend__c : 0) - (oldSn.Services_Spend__c != null ? oldSn.Services_Spend__c : 0); sn.Expense_Travel_Spend_for_Period__c = (sn.Expense_Travel_Spend__c != null ? sn.Expense_Travel_Spend__c : 0) - (oldSn.Expense_Travel_Spend__c != null ? oldSn.Expense_Travel_Spend__c : 0); @@ -119,17 +114,6 @@ public class SnapshotHelper { sn.GS_Services_Handover_Completed_for_Per__c = (sn.GS_Services_Completed_Handover__c != null ? sn.GS_Services_Completed_Handover__c : 0) - (oldSn.GS_Services_Completed_Handover__c != null ? oldSn.GS_Services_Completed_Handover__c : 0); sn.GS_Net_Income_for_Period__c = (sn.GS_Net_Income_to_Date__c != null ? sn.GS_Net_Income_to_Date__c : 0) - (oldSn.GS_Net_Income_to_Date__c != null ? oldSn.GS_Net_Income_to_Date__c : 0); sn.GS_Net_Income_for_Period_by_Handover__c = (sn.GS_Net_Income_to_Date_Handover__c != null ? sn.GS_Net_Income_to_Date_Handover__c : 0) - (oldSn.GS_Net_Income_to_Date_Handover__c != null ? oldSn.GS_Net_Income_to_Date_Handover__c : 0); - } else { // TODO - sn.Work_Completed_for_Period__c = 0; - sn.Services_Spend_for_Period__c = 0; - sn.Expense_Travel_Spend_for_Period__c = 0; - sn.Product_Spend_for_Period__c = 0; - sn.Total_Calculated_Costs_for_Period__c = 0; - sn.GS_Services_Completed_for_Period__c = 0; - sn.GS_Services_Handover_Completed_for_Per__c = 0; - sn.GS_Net_Income_for_Period__c = 0; - sn.GS_Net_Income_for_Period_by_Handover__c = 0; - } return sn; } From 457afad59fe4bde09642772508722598c614ca09 Mon Sep 17 00:00:00 2001 From: Tomasz Stalka Date: Sat, 4 Mar 2017 15:45:31 +0100 Subject: [PATCH 15/15] Fix for production --- src/classes/ContractSnapshotTest.cls | 6 +++--- src/classes/GSOverallCalculationScheduler.cls | 1 + src/classes/PROContractSnapshotBatch.cls | 7 ++++--- src/classes/SnapshotHelper.cls | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/classes/ContractSnapshotTest.cls b/src/classes/ContractSnapshotTest.cls index f65bc865..3a1130f0 100644 --- a/src/classes/ContractSnapshotTest.cls +++ b/src/classes/ContractSnapshotTest.cls @@ -23,7 +23,7 @@ public class ContractSnapshotTest { projectReportOutController.save(); List snapshots = [SELECT Id, Name, Percent_Of_Work_Completed__c, Percent_Services_Budget_For_GS__c, Expected_Buffer__c, - Expense_And_Travel_Spend__c, Services_Spend__c, Total_Calculated_Costs__c, Product_Spend__c, Net_Income_By_Deliverables_Complete__c, + Expense_And_Travel_Spend__c, Services_Spend__c, Total_Calculated_Costs__c, Product_Spend2__c, Net_Income_By_Deliverables_Complete__c, Execution_Efficiency__c, Buffer_By_Deliverables_Complete__c, Budget_Used__c FROM PRO_Contract_Snapshot__c WHERE Contract__c =: dcontract.Id]; @@ -47,7 +47,7 @@ public class ContractSnapshotTest { List snapshots = [SELECT Id, Name, Percent_Of_Work_Completed__c, Percent_Services_Budget_For_GS__c, Expected_Buffer__c, - Expense_And_Travel_Spend__c, Services_Spend__c, Total_Calculated_Costs__c, Product_Spend__c, Net_Income_By_Deliverables_Complete__c, + Expense_And_Travel_Spend__c, Services_Spend__c, Total_Calculated_Costs__c, Product_Spend2__c, Net_Income_By_Deliverables_Complete__c, Execution_Efficiency__c, Buffer_By_Deliverables_Complete__c, Budget_Used__c, Work_Completed_for_Period__c FROM PRO_Contract_Snapshot__c WHERE Contract__c =: dcontract.Id AND Id !=: proSn1.Id AND Id !=: proSn2.Id]; @@ -435,7 +435,7 @@ public class ContractSnapshotTest { System.assertEquals(5000, snapshot.Expense_And_Travel_Spend__c); System.assertEquals(2500, snapshot.Services_Spend__c); System.assertEquals(6000, snapshot.Total_Calculated_Costs__c); - System.assertEquals(1000, snapshot.Product_Spend__c); + System.assertEquals(1000, snapshot.Product_Spend2__c); System.assertEquals(-2400, snapshot.Net_Income_By_Deliverables_Complete__c); System.assertEquals(60, snapshot.Execution_Efficiency__c); System.assertEquals(-28, snapshot.Buffer_By_Deliverables_Complete__c); diff --git a/src/classes/GSOverallCalculationScheduler.cls b/src/classes/GSOverallCalculationScheduler.cls index 922e4fdf..827aba64 100644 --- a/src/classes/GSOverallCalculationScheduler.cls +++ b/src/classes/GSOverallCalculationScheduler.cls @@ -1,6 +1,7 @@ public without sharing class GSOverallCalculationScheduler implements Schedulable { public void execute(SchedulableContext sc) { + Database.executeBatch(new ForPeriodCalcSnapshotBatch(false)); Date currentDate = Date.today(); List thursDayList = PROHelper.GetAllThursDayOfMonth(currentDate.year(), currentDate.month()); List thursDayListLastMonth = PROHelper.GetAllThursDayOfMonth(currentDate.addMonths(-1).year(), currentDate.addMonths(-1).month()); diff --git a/src/classes/PROContractSnapshotBatch.cls b/src/classes/PROContractSnapshotBatch.cls index 07a90286..90bfc43a 100644 --- a/src/classes/PROContractSnapshotBatch.cls +++ b/src/classes/PROContractSnapshotBatch.cls @@ -4,10 +4,11 @@ */ global class PROContractSnapshotBatch implements Database.Batchable, Database.Stateful { + private Date currentDate; private List failedReocrds = new List(); public PROContractSnapshotBatch(){ - + this.currentDate = Date.today(); } global List start(Database.BatchableContext context) { @@ -17,7 +18,7 @@ global class PROContractSnapshotBatch implements Database.Batchable contracts) { try { List insertList = new List(); - Map oldSnapshots = SnapshotHelper.getOldPROSnapshot(contracts); + Map oldSnapshots = SnapshotHelper.getOldPROSnapshot(contracts, this.currentDate); for (DContract__c dcontract : contracts) { PRO_Contract_Snapshot__c proContractSnapshot = new PRO_Contract_Snapshot__c(); @@ -25,7 +26,7 @@ global class PROContractSnapshotBatch implements Database.Batchable getOldPROSnapshot(List contracts) { + public static Map getOldPROSnapshot(List contracts, Date currentDate) { Map snapshotsMap = new Map(); List snapshotsList = [SELECT Id, Percent_Of_Work_Completed__c, Services_Spend__c, Expense_And_Travel_Spend__c, Product_Spend__c, Total_Calculated_Costs__c, GS_Services_Completed_Handover__c, Product_Spend2__c, - Contract__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: Date.today() + Contract__c, GS_Services_Completed__c, GS_Net_Income_to_Date__c, GS_Net_Income_to_Date_Handover__c FROM PRO_Contract_Snapshot__c WHERE Snapshot_Date__c <: currentDate AND Contract__c IN: contracts ORDER BY Snapshot_Date__c DESC]; for (PRO_Contract_Snapshot__c sn : snapshotsList) {