Created
November 25, 2015 13:30
-
-
Save ahassen/52c65c863e544fffd8f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.apposit.terra.connect.report | |
| import com.apposit.terra.connect.model.* | |
| import org.springframework.beans.factory.InitializingBean | |
| /** | |
| * Created by asuraphel on 11/24/15. | |
| */ | |
| enum ReportAccessorUserLevel { | |
| REGION(0), | |
| ZONE(1), | |
| UNION(2) | |
| private final int indentation | |
| private ReportAccessorUserLevel(int value) { | |
| this.indentation = value | |
| } | |
| int getIndentation() { indentation } | |
| } | |
| class AtaAllocatedVsDeliveredReportService implements InitializingBean { | |
| def grailsApplication | |
| public static final List LIST_OF_SUPPORTED_FERTILIZER_TYPES = ['UREA', 'NPS', 'DAP' ] | |
| public static final int INDENTATION_OF_ZONE_FOR_REGIONAL_USER = 1 | |
| private static db = null | |
| public void afterPropertiesSet() throws Exception { | |
| db = grailsApplication.mainContext.mongo.getDB(grailsApplication.config.grails.mongo.databaseName) | |
| } | |
| private List getAllocatedVsDeliveredRowsForZone( Zone zone, Date from, Date to, ReportAccessorUserLevel reportAccessorUserLevel ) { | |
| Map allocationAndDeliverySumForThisZone = [ | |
| DAP: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ], | |
| NPS: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ], | |
| UREA: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ] | |
| ] | |
| List allocs = db.zonalAllocation.aggregate( [ | |
| [ '$match' : [ zone: zone.id, | |
| dateCreated: ['$gte': from, '$lt': to], | |
| fertilizerType: [ '$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES ] ] ], | |
| [ | |
| '$group' : [ | |
| '_id': "\$fertilizerType", | |
| 'sum': [ '$sum': "\$allocatedAmount" ] | |
| ] | |
| ] | |
| ]).results()*.values() | |
| for( alloc in allocs) { | |
| allocationAndDeliverySumForThisZone[alloc[0]].allocated = alloc[1] | |
| } | |
| List delivFromInputStockIssue = db.inputStockIssueCollection.aggregate( [ | |
| [ | |
| '$match' : [ | |
| fertilizerType: [ '$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES ], | |
| dateCreated: ['$gte': from, '$lt': to], | |
| "embeddedReceivingOrganization.location.zone": zone.id | |
| ] | |
| ], | |
| [ | |
| '$group' : [ | |
| '_id': "\$fertilizerType", | |
| 'sum': [ '$sum': "\$totalWeight" ] | |
| ] | |
| ] | |
| ]).results()*.values() | |
| for( delivered in delivFromInputStockIssue) { | |
| allocationAndDeliverySumForThisZone[delivered[0]].delivered += delivered[1] | |
| } | |
| double dapPercentage = allocationAndDeliverySumForThisZone.DAP.delivered.toDouble() / (allocationAndDeliverySumForThisZone.DAP.allocated) | |
| double ureaPercentage = allocationAndDeliverySumForThisZone.UREA.delivered.toDouble() / (allocationAndDeliverySumForThisZone.UREA.allocated) | |
| double npsPercentage = allocationAndDeliverySumForThisZone.NPS.delivered.toDouble() / (allocationAndDeliverySumForThisZone.NPS.allocated) | |
| List indentationList = [''] * ( INDENTATION_OF_ZONE_FOR_REGIONAL_USER - reportAccessorUserLevel.indentation) | |
| return [ | |
| indentationList + [zone.name, "", "", allocationAndDeliverySumForThisZone.DAP.allocated, "" , allocationAndDeliverySumForThisZone.UREA.allocated, "" , allocationAndDeliverySumForThisZone.NPS.allocated, ""], | |
| indentationList + ["", "", "", allocationAndDeliverySumForThisZone.DAP.delivered, dapPercentage.round(2).toString() + "%" , allocationAndDeliverySumForThisZone.UREA.delivered, ureaPercentage.round(2).toString() + "%" , allocationAndDeliverySumForThisZone.NPS.delivered, npsPercentage.round(2).toString() + "%"] | |
| ] | |
| } | |
| private List getAllocatedVsDeliveredRowsForRegion( Region region, Date from, Date to) { | |
| Map allocationAndDeliverySumForThisRegion = [ | |
| DAP: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ], | |
| NPS: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ], | |
| UREA: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ] | |
| ] | |
| List allocs = db.regionalAllocation.aggregate( [ | |
| [ '$match' : [ region: region.id, | |
| dateCreated: ['$gte': from, '$lt': to], | |
| fertilizerType: [ '$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES ] ] ], | |
| [ | |
| '$group' : [ | |
| '_id': "\$fertilizerType", | |
| 'sum': [ '$sum': "\$allocatedAmount" ] | |
| ] | |
| ] | |
| ]).results()*.values() | |
| for( alloc in allocs) { | |
| allocationAndDeliverySumForThisRegion[alloc[0]].allocated = alloc[1] | |
| } | |
| List delivViaWID = db.WIDGoodsReceived.aggregate( [ | |
| [ '$match' : [ "embeddedToOrganization.location.region": region.id, | |
| dateCreated: ['$gte': from, '$lt': to], | |
| "fertilizerType": [ '$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES ] ] ], | |
| [ | |
| '$group' : [ | |
| '_id': "\$fertilizerType", | |
| 'sum': [ '$sum': "\$totalWeight" ] | |
| ] | |
| ] | |
| ]).results()*.values() | |
| for( delWID in delivViaWID) { | |
| allocationAndDeliverySumForThisRegion[delWID[0]].delivered = delWID[1] | |
| } | |
| List delivFromInputStockIssue = db.inputStockIssueCollection.aggregate( [ | |
| [ '$match' : [ "embeddedToOrganization.location.region": region.id, | |
| dateCreated: ['$gte': from, '$lt': to], | |
| "fertilizerType": [ '$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES ] ] ], | |
| [ | |
| '$group' : [ | |
| '_id': "\$fertilizerType", | |
| 'sum': [ '$sum': "\$totalWeight" ] | |
| ] | |
| ] | |
| ]).results()*.values() | |
| for( delivered in delivFromInputStockIssue) { | |
| allocationAndDeliverySumForThisRegion[delivered[0]].delivered += delivered[1] | |
| } | |
| double dapPercentage = allocationAndDeliverySumForThisRegion.DAP.delivered.toDouble() / (allocationAndDeliverySumForThisRegion.DAP.allocated) | |
| double ureaPercentage = allocationAndDeliverySumForThisRegion.UREA.delivered.toDouble() / (allocationAndDeliverySumForThisRegion.UREA.allocated) | |
| double npsPercentage = allocationAndDeliverySumForThisRegion.NPS.delivered.toDouble() / (allocationAndDeliverySumForThisRegion.NPS.allocated) | |
| return [ | |
| [region.name, "", "", "", allocationAndDeliverySumForThisRegion.DAP.allocated, "" , allocationAndDeliverySumForThisRegion.UREA.allocated, "" , allocationAndDeliverySumForThisRegion.NPS.allocated, ""], | |
| ["", "", "", "", allocationAndDeliverySumForThisRegion.DAP.delivered, dapPercentage.round(2).toString() + "%" , allocationAndDeliverySumForThisRegion.UREA.delivered, ureaPercentage.round(2).toString() + "%" , allocationAndDeliverySumForThisRegion.NPS.delivered, npsPercentage.round(2).toString() + "%"] | |
| ] | |
| } | |
| private List getAllocatedVsDeliveredRowsForUnion( Organization union, Date from, Date to ) { | |
| Map unionData = [ | |
| DAP: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ], | |
| NPS: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ], | |
| UREA: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ] | |
| ] | |
| List allocs = db.unionAllocation.aggregate( [ | |
| [ '$match' : [ union: union.id, | |
| dateCreated: ['$gte': from, '$lt': to], | |
| fertilizerType: [ '$in': FERTILIZER_TYPE ] ] ], | |
| [ | |
| '$group' : [ | |
| '_id': "\$fertilizerType", | |
| 'sum': [ '$sum': "\$allocatedAmount" ] | |
| ] | |
| ] | |
| ]).results()*.values() | |
| for( alloc in allocs) [ | |
| unionData[alloc[0]].allocated = alloc[1] | |
| ] | |
| List delivViaInputStockCollection = db.inputStockIssueCollection.aggregate( [ | |
| [ | |
| '$match' : [ | |
| fertilizerType: [ '$in': FERTILIZER_TYPE ], | |
| dateCreated: ['$gte': from, '$lt': to], | |
| "embeddedReceivingOrganization.organizationId": union.id | |
| ] | |
| ], | |
| [ | |
| '$group' : [ | |
| '_id': "\$fertilizerType", | |
| 'sum': [ '$sum': "\$totalWeight" ] | |
| ] | |
| ] | |
| ]).results()*.values() | |
| for( delWID in delivViaInputStockCollection) { | |
| unionData[delWID[0]].delivered = delWID[1] | |
| } | |
| double dapPercentage = unionData.DAP.delivered.toDouble() / unionData.DAP.allocated.toDouble() | |
| double ureaPercentage = unionData.UREA.delivered.toDouble() / unionData.UREA.allocated.toDouble() | |
| double npsPercentage = unionData.NPS.delivered.toDouble() / unionData.NPS.allocated.toDouble() | |
| return [ | |
| [union.name, "", "", "", unionData.DAP.allocated, "" , unionData.UREA.allocated, "" , unionData.NPS.allocated, ""], | |
| ["", "", "", "", unionData.DAP.delivered, dapPercentage.round(2).toString() + "%" , unionData.UREA.delivered, ureaPercentage.round(2).toString() + "%" , | |
| unionData.NPS.delivered, npsPercentage.round(2).toString() + "%"] | |
| ] | |
| } | |
| private List getaAllocatedVsDeliveredRowsForWoreda(Woreda woreda, Date from, Date to){ | |
| Map woredaAllocatedAndDelivered = [ | |
| DAP: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ], | |
| NPS: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ], | |
| UREA: [ | |
| allocated: 0, | |
| delivered: 0 | |
| ] | |
| ] | |
| List allocatedForWoreda = db.cooperativeAllocation.aggregate([ | |
| ['$match': ["embeddedCooperative.location.woreda": woreda?.id, | |
| dateCreated: ['$gte': from, '$lte': to], | |
| fertilizerType: ['$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES] | |
| ] | |
| ], | |
| ['$group': ['_id': "\$fertilizerType", | |
| 'sum': ['$sum': "\$allocatedAmount"] | |
| ] | |
| ] | |
| ]).results()*.values() | |
| for(allocation in allocatedForWoreda){ | |
| woredaAllocatedAndDelivered[allocation[0]].allocated = allocation[1] | |
| } | |
| List deliveredForWoreda = db.WIDGoodsReceived.aggregate([ | |
| ['$match': ["embeddedToOrganization.location.woreda": woreda?.id, | |
| dateCreated: ['$gte': from, '$lte': to], | |
| fertilizerType: ['$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES] | |
| ] | |
| ], | |
| ['$group': ['_id': "\$fertilizerType", | |
| 'sum': ['$sum': ['$divide': ["\$totalWeight", 100]]] | |
| ] | |
| ] | |
| ]).results()*.values() | |
| for(delivered in deliveredForWoreda){ | |
| woredaAllocatedAndDelivered[delivered[0]].delivered = delivered[1] | |
| } | |
| double dapPercentage = (woredaAllocatedAndDelivered.DAP.allocated != 0) ? (woredaAllocatedAndDelivered.DAP.delivered.toDouble() / woredaAllocatedAndDelivered.DAP.allocated.toDouble()) : 0.0 | |
| double ureaPercentage = (woredaAllocatedAndDelivered.UREA.allocated != 0) ? (woredaAllocatedAndDelivered.UREA.delivered.toDouble() / woredaAllocatedAndDelivered.UREA.allocated.toDouble()) : 0.0 | |
| double npsPercentage = (woredaAllocatedAndDelivered.NPS.allocated != 0) ? (woredaAllocatedAndDelivered.NPS.delivered.toDouble() / woredaAllocatedAndDelivered.NPS.allocated.toDouble()) : 0.0 | |
| return [ | |
| [woreda.name, "", "", "", woredaAllocatedAndDelivered.DAP.allocated, "" , woredaAllocatedAndDelivered.UREA.allocated, "" , woredaAllocatedAndDelivered.NPS.allocated, ""], | |
| ["", "", "", "", woredaAllocatedAndDelivered.DAP.delivered, dapPercentage.round(2).toString() + "%" , woredaAllocatedAndDelivered.UREA.delivered, ureaPercentage.round(2).toString() + "%" , | |
| woredaAllocatedAndDelivered.NPS.delivered, npsPercentage.round(2).toString() + "%"] | |
| ] | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment