1 24 25 29 30 package org.ofbiz.common.period; 31 32 import java.sql.Timestamp ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import org.ofbiz.base.util.UtilDateTime; 37 import org.ofbiz.base.util.UtilMisc; 38 import org.ofbiz.entity.GenericDelegator; 39 import org.ofbiz.entity.GenericEntityException; 40 import org.ofbiz.entity.GenericValue; 41 import org.ofbiz.entity.condition.EntityConditionList; 42 import org.ofbiz.entity.condition.EntityExpr; 43 import org.ofbiz.entity.condition.EntityOperator; 44 import org.ofbiz.service.DispatchContext; 45 import org.ofbiz.service.ServiceUtil; 46 47 public class PeriodServices { 48 public static String module = PeriodServices.class.getName(); 49 50 53 public static Map findLastClosedDate(DispatchContext dctx, Map context) { 54 GenericDelegator delegator = dctx.getDelegator(); 55 String organizationPartyId = (String ) context.get("organizationPartyId"); String periodTypeId = (String ) context.get("periodTypeId"); 57 Timestamp findDate = (Timestamp ) context.get("findDate"); 58 59 if (findDate == null) { 61 findDate = UtilDateTime.nowTimestamp(); 62 } 63 64 Timestamp lastClosedDate = null; GenericValue lastClosedTimePeriod = null; 66 Map result = ServiceUtil.returnSuccess(); 67 68 try { 69 List findClosedConditions = UtilMisc.toList(new EntityExpr("organizationPartyId", EntityOperator.EQUALS, organizationPartyId), 71 new EntityExpr("thruDate", EntityOperator.LESS_THAN_EQUAL_TO, findDate), 72 new EntityExpr("isClosed", EntityOperator.EQUALS, "Y")); 73 if ((periodTypeId != null) && !(periodTypeId.equals(""))) { 74 findClosedConditions.add(new EntityExpr("periodTypeId", EntityOperator.EQUALS, periodTypeId)); 76 } 77 List closedTimePeriods = delegator.findByCondition("CustomTimePeriod", new EntityConditionList(findClosedConditions, EntityOperator.AND), 78 UtilMisc.toList("customTimePeriodId", "periodTypeId", "isClosed", "fromDate", "thruDate"), 79 UtilMisc.toList("thruDate DESC")); 80 81 if ((closedTimePeriods != null) && (closedTimePeriods.size() > 0) && (((GenericValue) closedTimePeriods.get(0)).get("thruDate") != null)) { 82 lastClosedTimePeriod = (GenericValue) closedTimePeriods.get(0); 83 lastClosedDate = UtilDateTime.toTimestamp(lastClosedTimePeriod.getDate("thruDate")); 84 } else { 85 Map findParams = UtilMisc.toMap("organizationPartyId", organizationPartyId); 88 if ((periodTypeId != null) && !(periodTypeId.equals(""))) { 89 findParams.put("periodTypeId", periodTypeId); 90 } 91 List timePeriods = delegator.findByAnd("CustomTimePeriod", findParams, UtilMisc.toList("fromDate ASC")); 92 if ((timePeriods != null) && (timePeriods.size() > 0) && (((GenericValue) timePeriods.get(0)).get("fromDate") != null)) { 93 lastClosedDate = UtilDateTime.toTimestamp(((GenericValue) timePeriods.get(0)).getDate("fromDate")); 94 } else { 95 return ServiceUtil.returnError("Cannot get a starting date for net income"); 96 } 97 } 98 99 result.put("lastClosedTimePeriod", lastClosedTimePeriod); result.put("lastClosedDate", lastClosedDate); return result; 102 } catch (GenericEntityException ex) { 103 return(ServiceUtil.returnError(ex.getMessage())); 104 } 105 } 106 } | Popular Tags |