001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.module.endow.document.service.impl;
017
018 import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine;
019 import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionSecurity;
020 import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionTaxLotLine;
021 import org.kuali.kfs.module.endow.businessobject.HoldingTaxLot;
022 import org.kuali.kfs.module.endow.businessobject.Security;
023 import org.kuali.kfs.module.endow.document.AssetDecreaseDocument;
024 import org.kuali.kfs.module.endow.document.AssetIncreaseDocument;
025 import org.kuali.kfs.module.endow.document.service.HoldingTaxLotService;
026 import org.kuali.kfs.module.endow.document.service.KEMService;
027 import org.kuali.kfs.module.endow.document.service.SecurityService;
028 import org.kuali.kfs.module.endow.document.service.UpdateAssetDecreaseDocumentTaxLotsService;
029 import org.kuali.rice.kns.util.KualiDecimal;
030 import org.kuali.rice.kns.util.ObjectUtils;
031
032 /**
033 * Provides an implementation for the transaction line related tax lots update for the AssetIncreaseDocument.
034 */
035 public class UpdateAssetDecreaseDocumentTaxLotsServiceImpl implements UpdateAssetDecreaseDocumentTaxLotsService {
036
037 private HoldingTaxLotService taxLotService;
038 private SecurityService securityService;
039 private KEMService kemService;
040
041 /**
042 *
043 * @see org.kuali.kfs.module.endow.document.service.UpdateAssetDecreaseDocumentTaxLotsService#updateTransactionLineTaxLots(org.kuali.kfs.module.endow.document.AssetDecreaseDocument, org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
044 */
045 public void updateTransactionLineTaxLots(AssetDecreaseDocument adDocument, EndowmentTransactionLine transLine) {
046 EndowmentTransactionTaxLotLine taxLotLine = null;
047 boolean newLine = false;
048 EndowmentTransactionSecurity endowmentTransactionSecurity = adDocument.getSourceTransactionSecurity();
049
050 // updating an existing tax lot
051 if (transLine.getTaxLotLines() != null && transLine.getTaxLotLines().size() > 0) {
052 // there is only one tax lot line per each transaction line
053 taxLotLine = transLine.getTaxLotLines().get(0);
054 }
055 // or adding a new one
056 else {
057 // create and set a new tax lot line
058 newLine = true;
059 taxLotLine = new EndowmentTransactionTaxLotLine();
060 taxLotLine.setDocumentNumber(adDocument.getDocumentNumber());
061 taxLotLine.setDocumentLineNumber(transLine.getTransactionLineNumber());
062 taxLotLine.setTransactionHoldingLotNumber(1);
063 taxLotLine.setKemid(transLine.getKemid());
064 taxLotLine.setSecurityID(endowmentTransactionSecurity.getSecurityID());
065 taxLotLine.setRegistrationCode(endowmentTransactionSecurity.getRegistrationCode());
066 taxLotLine.setIpIndicator(transLine.getTransactionIPIndicatorCode());
067 }
068
069 taxLotLine.setLotUnits(transLine.getTransactionUnits().bigDecimalValue().negate());
070 taxLotLine.setLotHoldingCost(transLine.getTransactionAmount().bigDecimalValue().negate());
071
072 // set the tax lot acquired date
073 setTaxLotAcquiredDate(taxLotLine, adDocument, transLine);
074
075 // set the new lot indicator
076 setNewLotIndicator(taxLotLine, adDocument);
077
078 if (newLine) {
079 transLine.getTaxLotLines().add(taxLotLine);
080 }
081
082 }
083
084 /**
085 * Sets the Acquired date for the given tax lot line. If the tax lot indicator for the security (END_TRAN_SEC_T:
086 * SEC_TAX_LOT_IND) is No then for the lot acquired date - LOT_AQ_DATE - Search the END_HLDG_TAX_LOT_T records by KEMID by
087 * SEC_ID by REGIS_CD by HLDG_IP_IND [where HLDG_IP_IND is equal to END_TRAN_LN_T: TRAN_IP_IND_CD] by HLDG_LOT_NBR where
088 * HLDG_LOT_NBR is equal to 1 and return the HLDG_ACQD_DT: - If a lot exists for the security in END_HLDG_TAX_LOT_T, but the
089 * HLDG_UNITS and HLDG_COST are zero, insert the current date (System or Process) in LOT_ACQD_DT. - IF no lot exists for the
090 * security, then insert the current date (System or Process) in LOT_ACQD_DT. If the tax lot indicator for the security
091 * (END_TRAN_SEC_T: SEC_TAX_LOT_IND) is Yes: - LOT_AQ_DATE - insert the current date (System or Process) in this field
092 *
093 * @param taxLotLine the tax lot line for which to set the acquired date
094 * @param adDocument the Asset Decrease Document the tax lot line belongs to
095 * @param transLine the transaction line the tax lot is related to
096 */
097 private void setTaxLotAcquiredDate(EndowmentTransactionTaxLotLine taxLotLine, AssetDecreaseDocument adDocument, EndowmentTransactionLine transLine) {
098 EndowmentTransactionSecurity endowmentTransactionSecurity = adDocument.getSourceTransactionSecurity();
099
100 Security security = securityService.getByPrimaryKey(endowmentTransactionSecurity.getSecurityID());
101
102 // if security tax lot indicator is 'No' and a tax lot exists for the kemid, security, registration code and income
103 // principal indicator - set the lot acquired date to be the tax lot holding acquired date if units and cost is not zero;
104 // otherwise set the date to be the current date
105 if (ObjectUtils.isNotNull(security) && !security.getClassCode().isTaxLotIndicator()) {
106 HoldingTaxLot holdingTaxLot = taxLotService.getByPrimaryKey(transLine.getKemid(), endowmentTransactionSecurity.getSecurityID(), endowmentTransactionSecurity.getRegistrationCode(), 1, transLine.getTransactionIPIndicatorCode());
107 if (ObjectUtils.isNotNull(holdingTaxLot)) {
108 if (holdingTaxLot.getUnits().equals(KualiDecimal.ZERO) && holdingTaxLot.getCost().equals(KualiDecimal.ZERO)) {
109 taxLotLine.setLotAcquiredDate(kemService.getCurrentDate());
110 }
111 else {
112 taxLotLine.setLotAcquiredDate(holdingTaxLot.getAcquiredDate());
113 }
114 }
115 else {
116 taxLotLine.setLotAcquiredDate(kemService.getCurrentDate());
117 }
118
119 }
120 // if security tax lot indicator is 'Yes' set the lot acquired date to be the current date
121 else {
122 taxLotLine.setLotAcquiredDate(kemService.getCurrentDate());
123 }
124 }
125
126 /**
127 * Sets the new lot indicator for the tax lot: -- if the security tax lot indicator is No then I think we should set the field
128 * to 'N'. When the batch process runs we might need to create a new entry on the holding tax lot table in case no entry is
129 * found for the given KEMID, security ID, registration code, holding ip indicator, and holding lot number = 1. In case there is
130 * an entry we will just update that one; -- if the security tax lot is Yes then the field should be set to 'Y'.We are always
131 * creating a new field with the lot number being the next sequential lot number.
132 *
133 * @param taxLotLine
134 * @param adDocument
135 */
136 private void setNewLotIndicator(EndowmentTransactionTaxLotLine taxLotLine, AssetDecreaseDocument adDocument) {
137 EndowmentTransactionSecurity endowmentTransactionSecurity = adDocument.getSourceTransactionSecurity();
138 Security security = securityService.getByPrimaryKey(endowmentTransactionSecurity.getSecurityID());
139
140 if (ObjectUtils.isNotNull(security)) {
141 // if the security tax lot indicator is No then I think we should set the field to 'N'. When the batch process runs we
142 // might need to create a new entry on the holding tax lot table in case no entry is found for the given KEMID, security
143 // ID, registration code, holding ip indicator, and holding lot number = 1. In case there is an entry we will just
144 // update that one
145 if (!security.getClassCode().isTaxLotIndicator()) {
146
147 taxLotLine.setNewLotIndicator(false);
148 }
149 // if the security tax lot is Yes then the field should be set to 'Y'.We are always creating a new field with the lot
150 // number being the next sequential lot number.
151 else {
152 taxLotLine.setNewLotIndicator(true);
153 }
154 }
155 }
156
157 /**
158 * Gets the taxLotService.
159 *
160 * @return taxLotService
161 */
162 protected HoldingTaxLotService getTaxLotService() {
163 return taxLotService;
164 }
165
166 /**
167 * Sets the taxLotService.
168 *
169 * @param taxLotService
170 */
171 public void setTaxLotService(HoldingTaxLotService taxLotService) {
172 this.taxLotService = taxLotService;
173 }
174
175 /**
176 * Gets the securityService.
177 *
178 * @return securityService
179 */
180 protected SecurityService getSecurityService() {
181 return securityService;
182 }
183
184 /**
185 * Sets the securityService.
186 *
187 * @param securityService
188 */
189 public void setSecurityService(SecurityService securityService) {
190 this.securityService = securityService;
191 }
192
193 /**
194 * Gets the kemService.
195 *
196 * @return kemService
197 */
198 protected KEMService getKemService() {
199 return kemService;
200 }
201
202 /**
203 * Sets the kemService.
204 *
205 * @param kemService
206 */
207 public void setKemService(KEMService kemService) {
208 this.kemService = kemService;
209 }
210 }