Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.09 KB | None | 0 0
  1. package com.apposit.terra.connect.report
  2.  
  3. import com.apposit.terra.connect.model.*
  4. import org.springframework.beans.factory.InitializingBean
  5.  
  6.  
  7. /**
  8. * Created by asuraphel on 11/24/15.
  9. */
  10. enum ReportAccessorUserLevel {
  11. REGION(0),
  12. ZONE(1),
  13. UNION(2)
  14.  
  15. private final int indentation
  16.  
  17. private ReportAccessorUserLevel(int value) {
  18. this.indentation = value
  19. }
  20.  
  21. int getIndentation() { indentation }
  22. }
  23.  
  24.  
  25. class AtaAllocatedVsDeliveredReportService implements InitializingBean {
  26.  
  27. def grailsApplication
  28.  
  29. public static final List LIST_OF_SUPPORTED_FERTILIZER_TYPES = ['UREA', 'NPS', 'DAP' ]
  30. public static final int INDENTATION_OF_ZONE_FOR_REGIONAL_USER = 1
  31.  
  32. private static db = null
  33.  
  34.  
  35. public void afterPropertiesSet() throws Exception {
  36. db = grailsApplication.mainContext.mongo.getDB(grailsApplication.config.grails.mongo.databaseName)
  37. }
  38.  
  39. private List getAllocatedVsDeliveredRowsForZone( Zone zone, Date from, Date to, ReportAccessorUserLevel reportAccessorUserLevel ) {
  40.  
  41. Map allocationAndDeliverySumForThisZone = [
  42. DAP: [
  43. allocated: 0,
  44. delivered: 0
  45. ],
  46.  
  47. NPS: [
  48. allocated: 0,
  49. delivered: 0
  50. ],
  51.  
  52. UREA: [
  53. allocated: 0,
  54. delivered: 0
  55. ]
  56. ]
  57.  
  58. List allocs = db.zonalAllocation.aggregate( [
  59.  
  60. [ '$match' : [ zone: zone.id,
  61. dateCreated: ['$gte': from, '$lt': to],
  62. fertilizerType: [ '$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES ] ] ],
  63.  
  64. [
  65. '$group' : [
  66. '_id': "\$fertilizerType",
  67. 'sum': [ '$sum': "\$allocatedAmount" ]
  68. ]
  69. ]
  70. ]).results()*.values()
  71.  
  72. for( alloc in allocs) {
  73.  
  74. allocationAndDeliverySumForThisZone[alloc[0]].allocated = alloc[1]
  75. }
  76.  
  77. List delivFromInputStockIssue = db.inputStockIssueCollection.aggregate( [
  78.  
  79. [
  80. '$match' : [
  81. fertilizerType: [ '$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES ],
  82. dateCreated: ['$gte': from, '$lt': to],
  83. "embeddedReceivingOrganization.location.zone": zone.id
  84. ]
  85. ],
  86.  
  87. [
  88. '$group' : [
  89. '_id': "\$fertilizerType",
  90. 'sum': [ '$sum': "\$totalWeight" ]
  91. ]
  92. ]
  93. ]).results()*.values()
  94.  
  95.  
  96. for( delivered in delivFromInputStockIssue) {
  97.  
  98. allocationAndDeliverySumForThisZone[delivered[0]].delivered += delivered[1]
  99. }
  100.  
  101. double dapPercentage = allocationAndDeliverySumForThisZone.DAP.delivered.toDouble() / (allocationAndDeliverySumForThisZone.DAP.allocated)
  102. double ureaPercentage = allocationAndDeliverySumForThisZone.UREA.delivered.toDouble() / (allocationAndDeliverySumForThisZone.UREA.allocated)
  103. double npsPercentage = allocationAndDeliverySumForThisZone.NPS.delivered.toDouble() / (allocationAndDeliverySumForThisZone.NPS.allocated)
  104.  
  105. List indentationList = [''] * ( INDENTATION_OF_ZONE_FOR_REGIONAL_USER - reportAccessorUserLevel.indentation)
  106.  
  107. return [
  108. indentationList + [zone.name, "", "", allocationAndDeliverySumForThisZone.DAP.allocated, "" , allocationAndDeliverySumForThisZone.UREA.allocated, "" , allocationAndDeliverySumForThisZone.NPS.allocated, ""],
  109. indentationList + ["", "", "", allocationAndDeliverySumForThisZone.DAP.delivered, dapPercentage.round(2).toString() + "%" , allocationAndDeliverySumForThisZone.UREA.delivered, ureaPercentage.round(2).toString() + "%" , allocationAndDeliverySumForThisZone.NPS.delivered, npsPercentage.round(2).toString() + "%"]
  110. ]
  111.  
  112. }
  113.  
  114.  
  115. private List getAllocatedVsDeliveredRowsForRegion( Region region, Date from, Date to) {
  116.  
  117. Map allocationAndDeliverySumForThisRegion = [
  118. DAP: [
  119. allocated: 0,
  120. delivered: 0
  121. ],
  122.  
  123. NPS: [
  124. allocated: 0,
  125. delivered: 0
  126. ],
  127.  
  128. UREA: [
  129. allocated: 0,
  130. delivered: 0
  131. ]
  132. ]
  133.  
  134. List allocs = db.regionalAllocation.aggregate( [
  135.  
  136. [ '$match' : [ region: region.id,
  137. dateCreated: ['$gte': from, '$lt': to],
  138. fertilizerType: [ '$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES ] ] ],
  139.  
  140. [
  141. '$group' : [
  142. '_id': "\$fertilizerType",
  143. 'sum': [ '$sum': "\$allocatedAmount" ]
  144. ]
  145. ]
  146. ]).results()*.values()
  147.  
  148. for( alloc in allocs) {
  149.  
  150. allocationAndDeliverySumForThisRegion[alloc[0]].allocated = alloc[1]
  151. }
  152.  
  153.  
  154. List delivViaWID = db.WIDGoodsReceived.aggregate( [
  155.  
  156. [ '$match' : [ "embeddedToOrganization.location.region": region.id,
  157. dateCreated: ['$gte': from, '$lt': to],
  158. "fertilizerType": [ '$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES ] ] ],
  159.  
  160. [
  161. '$group' : [
  162. '_id': "\$fertilizerType",
  163. 'sum': [ '$sum': "\$totalWeight" ]
  164. ]
  165. ]
  166. ]).results()*.values()
  167.  
  168. for( delWID in delivViaWID) {
  169.  
  170. allocationAndDeliverySumForThisRegion[delWID[0]].delivered = delWID[1]
  171. }
  172.  
  173.  
  174. List delivFromInputStockIssue = db.inputStockIssueCollection.aggregate( [
  175.  
  176. [ '$match' : [ "embeddedToOrganization.location.region": region.id,
  177. dateCreated: ['$gte': from, '$lt': to],
  178. "fertilizerType": [ '$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES ] ] ],
  179.  
  180. [
  181. '$group' : [
  182. '_id': "\$fertilizerType",
  183. 'sum': [ '$sum': "\$totalWeight" ]
  184. ]
  185. ]
  186. ]).results()*.values()
  187.  
  188.  
  189. for( delivered in delivFromInputStockIssue) {
  190.  
  191. allocationAndDeliverySumForThisRegion[delivered[0]].delivered += delivered[1]
  192. }
  193.  
  194. double dapPercentage = allocationAndDeliverySumForThisRegion.DAP.delivered.toDouble() / (allocationAndDeliverySumForThisRegion.DAP.allocated)
  195. double ureaPercentage = allocationAndDeliverySumForThisRegion.UREA.delivered.toDouble() / (allocationAndDeliverySumForThisRegion.UREA.allocated)
  196. double npsPercentage = allocationAndDeliverySumForThisRegion.NPS.delivered.toDouble() / (allocationAndDeliverySumForThisRegion.NPS.allocated)
  197.  
  198.  
  199. return [
  200. [region.name, "", "", "", allocationAndDeliverySumForThisRegion.DAP.allocated, "" , allocationAndDeliverySumForThisRegion.UREA.allocated, "" , allocationAndDeliverySumForThisRegion.NPS.allocated, ""],
  201. ["", "", "", "", allocationAndDeliverySumForThisRegion.DAP.delivered, dapPercentage.round(2).toString() + "%" , allocationAndDeliverySumForThisRegion.UREA.delivered, ureaPercentage.round(2).toString() + "%" , allocationAndDeliverySumForThisRegion.NPS.delivered, npsPercentage.round(2).toString() + "%"]
  202. ]
  203. }
  204.  
  205. private List getAllocatedVsDeliveredRowsForUnion( Organization union, Date from, Date to ) {
  206.  
  207. Map unionData = [
  208. DAP: [
  209. allocated: 0,
  210. delivered: 0
  211. ],
  212.  
  213. NPS: [
  214. allocated: 0,
  215. delivered: 0
  216. ],
  217.  
  218. UREA: [
  219. allocated: 0,
  220. delivered: 0
  221. ]
  222. ]
  223.  
  224. List allocs = db.unionAllocation.aggregate( [
  225.  
  226. [ '$match' : [ union: union.id,
  227. dateCreated: ['$gte': from, '$lt': to],
  228. fertilizerType: [ '$in': FERTILIZER_TYPE ] ] ],
  229.  
  230. [
  231. '$group' : [
  232. '_id': "\$fertilizerType",
  233. 'sum': [ '$sum': "\$allocatedAmount" ]
  234. ]
  235. ]
  236. ]).results()*.values()
  237.  
  238. for( alloc in allocs) [
  239.  
  240. unionData[alloc[0]].allocated = alloc[1]
  241. ]
  242.  
  243.  
  244. List delivViaInputStockCollection = db.inputStockIssueCollection.aggregate( [
  245.  
  246. [
  247. '$match' : [
  248. fertilizerType: [ '$in': FERTILIZER_TYPE ],
  249. dateCreated: ['$gte': from, '$lt': to],
  250. "embeddedReceivingOrganization.organizationId": union.id
  251. ]
  252. ],
  253.  
  254. [
  255. '$group' : [
  256. '_id': "\$fertilizerType",
  257. 'sum': [ '$sum': "\$totalWeight" ]
  258. ]
  259. ]
  260. ]).results()*.values()
  261.  
  262. for( delWID in delivViaInputStockCollection) {
  263.  
  264. unionData[delWID[0]].delivered = delWID[1]
  265. }
  266.  
  267. double dapPercentage = unionData.DAP.delivered.toDouble() / unionData.DAP.allocated.toDouble()
  268. double ureaPercentage = unionData.UREA.delivered.toDouble() / unionData.UREA.allocated.toDouble()
  269. double npsPercentage = unionData.NPS.delivered.toDouble() / unionData.NPS.allocated.toDouble()
  270.  
  271.  
  272. return [
  273. [union.name, "", "", "", unionData.DAP.allocated, "" , unionData.UREA.allocated, "" , unionData.NPS.allocated, ""],
  274. ["", "", "", "", unionData.DAP.delivered, dapPercentage.round(2).toString() + "%" , unionData.UREA.delivered, ureaPercentage.round(2).toString() + "%" ,
  275. unionData.NPS.delivered, npsPercentage.round(2).toString() + "%"]
  276. ]
  277. }
  278.  
  279. private List getaAllocatedVsDeliveredRowsForWoreda(Woreda woreda, Date from, Date to){
  280.  
  281. Map woredaAllocatedAndDelivered = [
  282. DAP: [
  283. allocated: 0,
  284. delivered: 0
  285. ],
  286. NPS: [
  287. allocated: 0,
  288. delivered: 0
  289. ],
  290. UREA: [
  291. allocated: 0,
  292. delivered: 0
  293. ]
  294. ]
  295.  
  296. List allocatedForWoreda = db.cooperativeAllocation.aggregate([
  297.  
  298. ['$match': ["embeddedCooperative.location.woreda": woreda?.id,
  299. dateCreated: ['$gte': from, '$lte': to],
  300. fertilizerType: ['$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES]
  301. ]
  302. ],
  303. ['$group': ['_id': "\$fertilizerType",
  304. 'sum': ['$sum': "\$allocatedAmount"]
  305. ]
  306. ]
  307. ]).results()*.values()
  308.  
  309. for(allocation in allocatedForWoreda){
  310. woredaAllocatedAndDelivered[allocation[0]].allocated = allocation[1]
  311. }
  312.  
  313. List deliveredForWoreda = db.WIDGoodsReceived.aggregate([
  314.  
  315. ['$match': ["embeddedToOrganization.location.woreda": woreda?.id,
  316. dateCreated: ['$gte': from, '$lte': to],
  317. fertilizerType: ['$in': LIST_OF_SUPPORTED_FERTILIZER_TYPES]
  318. ]
  319. ],
  320. ['$group': ['_id': "\$fertilizerType",
  321. 'sum': ['$sum': ['$divide': ["\$totalWeight", 100]]]
  322. ]
  323. ]
  324. ]).results()*.values()
  325.  
  326. for(delivered in deliveredForWoreda){
  327. woredaAllocatedAndDelivered[delivered[0]].delivered = delivered[1]
  328. }
  329.  
  330. double dapPercentage = (woredaAllocatedAndDelivered.DAP.allocated != 0) ? (woredaAllocatedAndDelivered.DAP.delivered.toDouble() / woredaAllocatedAndDelivered.DAP.allocated.toDouble()) : 0.0
  331. double ureaPercentage = (woredaAllocatedAndDelivered.UREA.allocated != 0) ? (woredaAllocatedAndDelivered.UREA.delivered.toDouble() / woredaAllocatedAndDelivered.UREA.allocated.toDouble()) : 0.0
  332. double npsPercentage = (woredaAllocatedAndDelivered.NPS.allocated != 0) ? (woredaAllocatedAndDelivered.NPS.delivered.toDouble() / woredaAllocatedAndDelivered.NPS.allocated.toDouble()) : 0.0
  333.  
  334.  
  335. return [
  336. [woreda.name, "", "", "", woredaAllocatedAndDelivered.DAP.allocated, "" , woredaAllocatedAndDelivered.UREA.allocated, "" , woredaAllocatedAndDelivered.NPS.allocated, ""],
  337. ["", "", "", "", woredaAllocatedAndDelivered.DAP.delivered, dapPercentage.round(2).toString() + "%" , woredaAllocatedAndDelivered.UREA.delivered, ureaPercentage.round(2).toString() + "%" ,
  338. woredaAllocatedAndDelivered.NPS.delivered, npsPercentage.round(2).toString() + "%"]
  339. ]
  340. }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement