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 java.math.BigDecimal;
019    
020    import org.kuali.kfs.module.endow.EndowConstants;
021    import org.kuali.kfs.module.endow.EndowKeyConstants;
022    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine;
023    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionSecurity;
024    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionTaxLotLine;
025    import org.kuali.kfs.module.endow.businessobject.HoldingTaxLot;
026    import org.kuali.kfs.module.endow.document.EndowmentTaxLotLinesDocumentBase;
027    import org.kuali.kfs.module.endow.document.service.HoldingTaxLotService;
028    import org.kuali.kfs.module.endow.document.service.KEMService;
029    import org.kuali.kfs.module.endow.document.service.LiabilityDocumentService;
030    import org.kuali.kfs.module.endow.document.service.SecurityService;
031    import org.kuali.rice.kns.util.GlobalVariables;
032    import org.kuali.rice.kns.util.KualiDecimal;
033    import org.kuali.rice.kns.util.ObjectUtils;
034    
035    /**
036     * This class...
037     */
038    public class LiabilityDocumentServiceImpl extends EndowmentTransactionLinesDocumentServiceImpl implements LiabilityDocumentService {
039    
040        private HoldingTaxLotService taxLotService;
041        private SecurityService securityService;
042        private KEMService kemService;
043    
044        /**
045         * @see org.kuali.kfs.module.endow.document.service.UpdateAssetIncreaseDocumentTaxLotsService#updateTransactionLineTaxLots(boolean,
046         *      org.kuali.kfs.module.endow.document.AssetIncreaseDocument,
047         *      org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
048         */
049        public void updateLiabilityIncreaseTransactionLineTaxLots(boolean isSource, EndowmentTaxLotLinesDocumentBase document, EndowmentTransactionLine transLine) {
050            EndowmentTransactionSecurity endowmentTransactionSecurity = null;
051            String securityID = null;
052            String registrationCode = null;
053    
054            if (isSource) {
055                endowmentTransactionSecurity = document.getSourceTransactionSecurity();
056                securityID = document.getSourceTransactionSecurity().getSecurityID();
057                registrationCode = document.getSourceTransactionSecurity().getRegistrationCode();
058            }
059            else {
060                endowmentTransactionSecurity = document.getTargetTransactionSecurity();
061                securityID = document.getTargetTransactionSecurity().getSecurityID();
062                registrationCode = document.getTargetTransactionSecurity().getRegistrationCode();
063            }
064    
065            EndowmentTransactionTaxLotLine taxLotLine = obtainTaxLotLine(transLine, endowmentTransactionSecurity);
066    
067            // Update the Cost to -ve
068            taxLotLine.setLotHoldingCost(taxLotLine.getLotHoldingCost().negate());
069    
070    
071            HoldingTaxLot holdingTaxLot = taxLotService.getByPrimaryKey(transLine.getKemid(), securityID, registrationCode, 1, transLine.getTransactionIPIndicatorCode());
072    
073            if (ObjectUtils.isNotNull(holdingTaxLot)) {
074                if (holdingTaxLot.getUnits().equals(KualiDecimal.ZERO) && holdingTaxLot.getCost().equals(KualiDecimal.ZERO)) {
075                    taxLotLine.setLotAcquiredDate(kemService.getCurrentDate());
076                }
077                else {
078                    taxLotLine.setLotAcquiredDate(holdingTaxLot.getAcquiredDate());
079                }
080            }
081            else {
082                taxLotLine.setLotAcquiredDate(kemService.getCurrentDate());
083            }
084        }
085    
086        /**
087         * @see org.kuali.kfs.module.endow.document.service.UpdateAssetIncreaseDocumentTaxLotsService#updateTransactionLineTaxLots(boolean,
088         *      org.kuali.kfs.module.endow.document.AssetIncreaseDocument,
089         *      org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
090         */
091        public void updateLiabilityDecreaseTransactionLineTaxLots(boolean isSource, EndowmentTaxLotLinesDocumentBase document, EndowmentTransactionLine transLine) {
092            EndowmentTransactionSecurity endowmentTransactionSecurity = null;
093            String securityID = null;
094            String registrationCode = null;
095    
096            if (isSource) {
097                endowmentTransactionSecurity = document.getSourceTransactionSecurity();
098                securityID = document.getSourceTransactionSecurity().getSecurityID();
099                registrationCode = document.getSourceTransactionSecurity().getRegistrationCode();
100            }
101            else {
102                endowmentTransactionSecurity = document.getTargetTransactionSecurity();
103                securityID = document.getTargetTransactionSecurity().getSecurityID();
104                registrationCode = document.getTargetTransactionSecurity().getRegistrationCode();
105            }
106    
107            EndowmentTransactionTaxLotLine taxLotLine = obtainTaxLotLine(transLine, endowmentTransactionSecurity);
108    
109            BigDecimal postiveUnitValue = taxLotLine.getLotUnits();
110            // Negate the Units for Liability
111            taxLotLine.setLotUnits(taxLotLine.getLotUnits().negate());
112    
113            HoldingTaxLot holdingTaxLot = taxLotService.getByPrimaryKey(transLine.getKemid(), securityID, registrationCode, 1, transLine.getTransactionIPIndicatorCode());
114            if (ObjectUtils.isNotNull(holdingTaxLot)) {
115                if (holdingTaxLot.getUnits().compareTo(postiveUnitValue) < 0) {
116                    GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(EndowConstants.TRANSACTION_LINE_ERRORS, EndowKeyConstants.EndowmentTransactionDocumentConstants.ERROR_ASSET_DECREASE_INSUFFICIENT_UNITS);
117                    // Empty out Tax lot lines.
118                    transLine.getTaxLotLines().remove(0);
119                }
120            }
121            else {
122                // Object must exist
123                GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(EndowConstants.TRANSACTION_LINE_ERRORS, EndowKeyConstants.EndowmentTransactionDocumentConstants.ERROR_TRANSACTION_LINE_TAXLOT_INVALID, "Liability");
124                // Empty out Tax lot lines.
125                transLine.getTaxLotLines().remove(0);
126    
127            }
128        }
129    
130        private EndowmentTransactionTaxLotLine obtainTaxLotLine(EndowmentTransactionLine transLine, EndowmentTransactionSecurity endowmentTransactionSecurity) {
131            EndowmentTransactionTaxLotLine taxLotLine = null;
132    
133            if (transLine.getTaxLotLines() != null && transLine.getTaxLotLines().size() > 0) {
134                // there is only one tax lot line per each transaction line
135                taxLotLine = transLine.getTaxLotLines().get(0);
136            }
137            else {
138                // Create and set a new tax lot line
139                taxLotLine = new EndowmentTransactionTaxLotLine();
140                // taxLotLine.setDocumentNumber(aiDocument.getDocumentNumber());
141                // taxLotLine.setDocumentLineNumber(transLine.getTransactionLineNumber());
142                taxLotLine.setTransactionHoldingLotNumber(1);
143    
144                // Adding Taxlot line
145                transLine.getTaxLotLines().add(0, taxLotLine);
146    
147            }
148    
149            // Updating data in case of refresh
150            taxLotLine.setLotUnits(transLine.getTransactionUnits().bigDecimalValue());
151            taxLotLine.setLotHoldingCost(transLine.getTransactionAmount().bigDecimalValue());
152    
153            // set kemid, security, regostration code, ip indicator
154            taxLotLine.setKemid(transLine.getKemid());
155            taxLotLine.setSecurityID(endowmentTransactionSecurity.getSecurityID());
156            taxLotLine.setRegistrationCode(endowmentTransactionSecurity.getRegistrationCode());
157            taxLotLine.setIpIndicator(transLine.getTransactionIPIndicatorCode());
158    
159            // set the new lot indicator
160            taxLotLine.setNewLotIndicator(false);
161    
162            return taxLotLine;
163        }
164    
165        /**
166         * Gets the taxLotService.
167         * 
168         * @return taxLotService
169         */
170        public HoldingTaxLotService getTaxLotService() {
171            return taxLotService;
172        }
173    
174        /**
175         * Sets the taxLotService.
176         * 
177         * @param taxLotService
178         */
179        public void setTaxLotService(HoldingTaxLotService taxLotService) {
180            this.taxLotService = taxLotService;
181        }
182    
183        /**
184         * Gets the securityService.
185         * 
186         * @return securityService
187         */
188        public SecurityService getSecurityService() {
189            return securityService;
190        }
191    
192        /**
193         * Sets the securityService.
194         * 
195         * @param securityService
196         */
197        public void setSecurityService(SecurityService securityService) {
198            this.securityService = securityService;
199        }
200    
201        public KEMService getKemService() {
202            return kemService;
203        }
204    
205        public void setKemService(KEMService kemService) {
206            this.kemService = kemService;
207        }
208    
209    }