001package org.kuali.ole.select.document.service.impl;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.select.businessobject.OlePaymentRequestItem;
005import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
006import org.kuali.ole.select.businessobject.OlePurchaseOrderLineForInvoice;
007import org.kuali.ole.select.businessobject.OlePurchaseOrderTotal;
008import org.kuali.ole.select.document.OlePurchaseOrderDocument;
009import org.kuali.ole.select.document.service.OleInvoiceItemService;
010import org.kuali.rice.core.api.util.type.KualiDecimal;
011import org.kuali.rice.krad.service.BusinessObjectService;
012import org.kuali.rice.krad.service.KRADServiceLocator;
013
014import java.math.BigDecimal;
015import java.util.ArrayList;
016import java.util.HashMap;
017import java.util.List;
018import java.util.Map;
019
020/**
021 * Created with IntelliJ IDEA.
022 * User: gopalp
023 * Date: 7/23/13
024 * Time: 5:17 PM
025 * To change this template use File | Settings | File Templates.
026 */
027public class OleInvoiceItemServiceImpl implements OleInvoiceItemService {
028
029
030    private BusinessObjectService businessObjectService;
031
032    public BusinessObjectService getBusinessObjectService() {
033        if (null == businessObjectService) {
034            businessObjectService = KRADServiceLocator.getBusinessObjectService();
035        }
036        return businessObjectService;
037    }
038
039    public List<OlePurchaseOrderLineForInvoice> getOlePurchaseOrderLineForInvoice(OlePurchaseOrderDocument oleInvoiceDocument) {
040        List<OlePurchaseOrderLineForInvoice> olePurchaseOrderLineForInvoiceList = new ArrayList<OlePurchaseOrderLineForInvoice>();
041        OlePurchaseOrderLineForInvoice olePurchaseOrderLineForInvoice;
042        for (OlePurchaseOrderItem item : (List<OlePurchaseOrderItem>) oleInvoiceDocument.getItems()) {
043            if (item.getItemType().getItemTypeCode().equals(OLEConstants.ITM_TYP_CODE)) {
044                if (item.isItemForInvoice()) {
045                    olePurchaseOrderLineForInvoice = new OlePurchaseOrderLineForInvoice();
046                    olePurchaseOrderLineForInvoice.setItemId(item.getItemTitleId());
047                    olePurchaseOrderLineForInvoice.setItemTitle(item.getItemDescription() != null ? item.getItemDescription() : "");
048                    KualiDecimal noOfCopiesReceived = new KualiDecimal(0.0);
049                    if (item.getNoOfCopiesInvoiced() != null) {
050                        noOfCopiesReceived = item.getNoOfCopiesInvoiced().kualiDecimalValue();
051                    }
052                    olePurchaseOrderLineForInvoice.setAmount((item.getItemListPrice().multiply(noOfCopiesReceived)).toString());
053                    olePurchaseOrderLineForInvoiceList.add(olePurchaseOrderLineForInvoice);
054                }
055            }
056        }
057        return olePurchaseOrderLineForInvoiceList;
058    }
059
060
061    public List<OlePurchaseOrderTotal> getOlePurchaseOrderTotal(OlePurchaseOrderDocument oleInvoiceDocument) {
062
063        OlePurchaseOrderTotal olePurchaseOrderTotal;
064        BigDecimal encumAmt = new BigDecimal(0.0);
065        BigDecimal encumbranceTotalAmount = new BigDecimal(0.0);
066        List<OlePurchaseOrderTotal> olePurchaseOrderTotalList = new ArrayList<OlePurchaseOrderTotal>();
067        for (OlePurchaseOrderItem item : (List<OlePurchaseOrderItem>) oleInvoiceDocument.getItems()) {
068            olePurchaseOrderTotal = new OlePurchaseOrderTotal();
069            String poId = oleInvoiceDocument.getPurapDocumentIdentifier().toString();
070            olePurchaseOrderTotal.setPoNumber(poId);
071            if (item.getItemType().getItemTypeCode().equals(OLEConstants.ITM_TYP_CODE)) {
072                KualiDecimal noOfCopiesRcvd = new KualiDecimal(0.0);
073                if (item.getNoOfCopiesInvoiced() != null) {
074                    noOfCopiesRcvd = item.getNoOfCopiesInvoiced().kualiDecimalValue();
075                }
076                encumAmt = encumAmt.add((item.getItemUnitPrice().multiply(noOfCopiesRcvd.bigDecimalValue())));
077                olePurchaseOrderTotal.setEncumbranceAmount(encumAmt.toString());
078                BigDecimal paidAmount = getTotalPaidItem(item.getItemIdentifier());
079                if (paidAmount.intValue() > 0) {
080                    if (noOfCopiesRcvd != null && item.getItemListPrice() != null) {
081                        encumbranceTotalAmount = ((noOfCopiesRcvd.bigDecimalValue()).multiply(item.getItemListPrice().bigDecimalValue()));
082                        olePurchaseOrderTotal.setEncumbranceTotalAmount(encumbranceTotalAmount);
083                    }
084                }
085                olePurchaseOrderTotal.setPaidAmount(paidAmount);
086            }
087            olePurchaseOrderTotalList.add(olePurchaseOrderTotal);
088        }
089        return olePurchaseOrderTotalList;
090    }
091
092
093    public BigDecimal getTotalPaidItem(Integer poItemId) {
094        Map preqMap = new HashMap();
095        preqMap.put("poItemIdentifier", poItemId);
096        preqMap.put("itemTypeCode", OLEConstants.ITM_TYP_CODE);
097        List<OlePaymentRequestItem> olePaymentRequestItem = (List<OlePaymentRequestItem>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePaymentRequestItem.class, preqMap);
098        BigDecimal paidAmt = new BigDecimal(0.0);
099        if (olePaymentRequestItem.size() > 0) {
100            BigDecimal itmQty = new BigDecimal(olePaymentRequestItem.get(0).getItemQuantity().longValue());
101            paidAmt = olePaymentRequestItem.get(0).getItemUnitPrice().multiply(itmQty);
102        }
103        return paidAmt;
104    }
105
106
107    public List<OlePurchaseOrderLineForInvoice> getOlePurchaseOrderLineForInvoiceForAddItem(OlePurchaseOrderDocument olePurchaseOrderDocument) {
108
109        List<OlePurchaseOrderLineForInvoice> olePurchaseOrderLineForInvoiceList = new ArrayList<OlePurchaseOrderLineForInvoice>();
110        OlePurchaseOrderLineForInvoice olePurchaseOrderLineForInvoice;
111        for (OlePurchaseOrderItem item : (List<OlePurchaseOrderItem>) olePurchaseOrderDocument.getItems()) {
112            if (item.getItemType().getItemTypeCode().equals(OLEConstants.ITM_TYP_CODE)) {
113                olePurchaseOrderLineForInvoice = new OlePurchaseOrderLineForInvoice();
114                olePurchaseOrderLineForInvoice.setItemId(item.getItemTitleId());
115                olePurchaseOrderLineForInvoice.setItemTitle(item.getItemDescription() != null ? item.getItemDescription() : "");
116                KualiDecimal noOfCopiesReceived = item.getItemQuantity();
117                olePurchaseOrderLineForInvoice.setAmount((item.getItemListPrice().multiply(noOfCopiesReceived)).toString());
118                olePurchaseOrderLineForInvoiceList.add(olePurchaseOrderLineForInvoice);
119            }
120        }
121        return olePurchaseOrderLineForInvoiceList;
122
123    }
124
125
126    public List<OlePurchaseOrderTotal> getOlePurchaseOrderTotalForAddItem(OlePurchaseOrderDocument olePurchaseOrderDocument) {
127
128        OlePurchaseOrderTotal olePurchaseOrderTotal;
129        List<OlePurchaseOrderTotal> olePurchaseOrderTotalList = new ArrayList<OlePurchaseOrderTotal>();
130        BigDecimal encumAmt = new BigDecimal(0.0);
131        BigDecimal encumbranceTotalAmount = new BigDecimal(0.0);
132        olePurchaseOrderTotal = new OlePurchaseOrderTotal();
133        String poId = olePurchaseOrderDocument.getPurapDocumentIdentifier().toString();
134        olePurchaseOrderTotal.setPoNumber(poId);
135        for (OlePurchaseOrderItem item : (List<OlePurchaseOrderItem>) olePurchaseOrderDocument.getItems()) {
136            if (item.getItemType().getItemTypeCode().equals(OLEConstants.ITM_TYP_CODE)) {
137                KualiDecimal noOfCopiesRcvd = item.getItemQuantity();
138                encumAmt = encumAmt.add((item.getItemUnitPrice().multiply(noOfCopiesRcvd.bigDecimalValue())));
139                olePurchaseOrderTotal.setEncumbranceAmount(encumAmt.toString());
140                BigDecimal paidAmount = getTotalPaidItem(item.getItemIdentifier());
141                olePurchaseOrderTotal.setPaidAmount(paidAmount);
142                if (paidAmount.intValue() > 0) {
143                    if (noOfCopiesRcvd != null && item.getItemListPrice() != null) {
144                        encumbranceTotalAmount = ((noOfCopiesRcvd.bigDecimalValue()).multiply(item.getItemListPrice().bigDecimalValue()));
145                        olePurchaseOrderTotal.setEncumbranceTotalAmount(encumbranceTotalAmount);
146                    }
147                }
148            }
149        }
150        olePurchaseOrderTotalList.add(olePurchaseOrderTotal);
151
152        return olePurchaseOrderTotalList;
153    }
154
155
156}