1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.service.impl;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.kuali.ole.sys.OLEConstants;
25 import org.kuali.ole.sys.businessobject.TaxRegion;
26 import org.kuali.ole.sys.businessobject.TaxRegionCounty;
27 import org.kuali.ole.sys.businessobject.TaxRegionPostalCode;
28 import org.kuali.ole.sys.businessobject.TaxRegionState;
29 import org.kuali.ole.sys.service.TaxRegionService;
30 import org.kuali.rice.krad.service.BusinessObjectService;
31 import org.kuali.rice.krad.util.ObjectUtils;
32 import org.kuali.rice.location.api.postalcode.PostalCode;
33 import org.kuali.rice.location.api.postalcode.PostalCodeService;
34 import org.springframework.transaction.annotation.Transactional;
35
36 @Transactional
37 public class TaxRegionServiceImpl implements TaxRegionService {
38
39 protected BusinessObjectService businessObjectService;
40 protected PostalCodeService postalCodeService;
41
42
43
44
45 @Override
46 public List<TaxRegion> getSalesTaxRegions(String postalCode) {
47
48 List<TaxRegion> salesTaxRegions = new ArrayList<TaxRegion>();
49 if ( StringUtils.isNotBlank(postalCode) ) {
50 PostalCode postalCodeObj = postalCodeService.getPostalCode( OLEConstants.COUNTRY_CODE_UNITED_STATES, postalCode );
51 if(ObjectUtils.isNotNull(postalCodeObj)) {
52 salesTaxRegions.addAll(getPostalCodeTaxRegions(postalCodeObj.getCode(), postalCodeObj.getCountryCode(), false));
53 salesTaxRegions.addAll(getStateTaxRegions(postalCodeObj.getStateCode(), postalCodeObj.getCountryCode(), false));
54 salesTaxRegions.addAll(getCountyTaxRegions(postalCodeObj.getCountyCode(), postalCodeObj.getStateCode(), postalCodeObj.getCountryCode(), false));
55 }
56 }
57
58 return salesTaxRegions;
59 }
60
61
62
63
64 @Override
65 public List<TaxRegion> getUseTaxRegions(String postalCode) {
66
67 List<TaxRegion> useTaxRegions = new ArrayList<TaxRegion>();
68
69 if ( StringUtils.isNotBlank(postalCode) ) {
70 PostalCode postalCodeObj = postalCodeService.getPostalCode( OLEConstants.COUNTRY_CODE_UNITED_STATES, postalCode );
71 useTaxRegions.addAll(getPostalCodeTaxRegions(postalCodeObj.getCode(), postalCodeObj.getCountryCode(), true));
72 useTaxRegions.addAll(getStateTaxRegions(postalCodeObj.getStateCode(), postalCodeObj.getCountryCode(), true));
73 useTaxRegions.addAll(getCountyTaxRegions(postalCodeObj.getCountyCode(), postalCodeObj.getStateCode(), postalCodeObj.getCountryCode(), true));
74 }
75 return useTaxRegions;
76 }
77
78
79
80
81
82
83
84
85
86 protected List<TaxRegion> getPostalCodeTaxRegions(String postalCode, String postalCountryCode, boolean useTaxOnly) {
87
88 List<TaxRegion> postalCodeTaxRegions = new ArrayList<TaxRegion>();
89
90 if (StringUtils.isNotEmpty(postalCode)) {
91 Map<String, Object> criteria = new HashMap<String, Object>();
92 criteria.put("postalCode", postalCode);
93 criteria.put("postalCountryCode", postalCountryCode);
94 criteria.put("active", true);
95 if (useTaxOnly) {
96 criteria.put("taxRegion.taxRegionUseTaxIndicator", useTaxOnly);
97 }
98
99 List<TaxRegionPostalCode> taxRegionPostalCodes = (List<TaxRegionPostalCode>) businessObjectService.findMatching(TaxRegionPostalCode.class, criteria);
100 for (TaxRegionPostalCode taxRegionPostalCode : taxRegionPostalCodes) {
101 postalCodeTaxRegions.add(taxRegionPostalCode.getTaxRegion());
102 }
103 }
104 return postalCodeTaxRegions;
105 }
106
107
108
109
110
111
112
113
114
115 protected List<TaxRegion> getStateTaxRegions(String stateCode, String postalCountryCode, boolean useTaxOnly) {
116
117 List<TaxRegion> stateTaxRegions = new ArrayList<TaxRegion>();
118
119 if (StringUtils.isNotEmpty(stateCode)) {
120
121 Map<String, Object> criteria = new HashMap<String, Object>();
122 criteria.put("stateCode", stateCode);
123 criteria.put("postalCountryCode", postalCountryCode);
124 criteria.put("active", true);
125 if (useTaxOnly) {
126 criteria.put("taxRegion.taxRegionUseTaxIndicator", useTaxOnly);
127 }
128
129 List<TaxRegionState> taxRegionStates = (List<TaxRegionState>) businessObjectService.findMatching(TaxRegionState.class, criteria);
130 for (TaxRegionState taxRegionState : taxRegionStates) {
131 stateTaxRegions.add(taxRegionState.getTaxRegion());
132 }
133 }
134 return stateTaxRegions;
135 }
136
137
138
139
140
141
142
143
144
145 protected List<TaxRegion> getCountyTaxRegions(String countyCode, String stateCode, String postalCountryCode, boolean useTaxOnly) {
146
147 List<TaxRegion> countyTaxRegions = new ArrayList<TaxRegion>();
148 if (StringUtils.isNotEmpty(countyCode)) {
149 Map<String, Object> criteria = new HashMap<String, Object>();
150 criteria.put("countyCode", countyCode);
151 criteria.put("stateCode", stateCode);
152 criteria.put("postalCountryCode", postalCountryCode);
153 criteria.put("active", true);
154 if (useTaxOnly) {
155 criteria.put("taxRegion.taxRegionUseTaxIndicator", useTaxOnly);
156 }
157
158 List<TaxRegionCounty> taxRegionCounties = (List<TaxRegionCounty>) businessObjectService.findMatching(TaxRegionCounty.class, criteria);
159 for (TaxRegionCounty taxRegionCounty : taxRegionCounties) {
160 countyTaxRegions.add(taxRegionCounty.getTaxRegion());
161 }
162 }
163 return countyTaxRegions;
164 }
165
166 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
167 this.businessObjectService = businessObjectService;
168 }
169
170 public void setPostalCodeService(PostalCodeService postalCodeService) {
171 this.postalCodeService = postalCodeService;
172 }
173 }