001/**
002 * Copyright 2005-2015 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 */
016package edu.sampleu.bookstore.document.web;
017
018import edu.sampleu.bookstore.bo.Book;
019import edu.sampleu.bookstore.bo.BookOrder;
020import edu.sampleu.bookstore.document.BookOrderDocument;
021import org.apache.struts.action.ActionForm;
022import org.apache.struts.action.ActionForward;
023import org.apache.struts.action.ActionMapping;
024import org.kuali.rice.core.api.util.type.KualiDecimal;
025import org.kuali.rice.kns.service.KNSServiceLocator;
026import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
027import org.kuali.rice.kns.web.struts.form.KualiForm;
028import org.kuali.rice.krad.service.KRADServiceLocator;
029
030import javax.servlet.http.HttpServletRequest;
031import javax.servlet.http.HttpServletResponse;
032
033/*
034 * BookOrderAction class file for BookOrder maintenance Object
035 * Actions prior to submit and post-Submit processes are handled.  
036 */
037
038public class BookOrderAction extends KualiTransactionalDocumentActionBase {
039
040        public ActionForward addBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
041                BookOrderForm form = (BookOrderForm) actionForm;
042        BookOrderDocument document = form.getBookOrderDocument();
043
044        BookOrder newBookEntry = form.getNewBookOrder();       
045        document.addBookOrder(newBookEntry);
046        
047                for (BookOrder entry : document.getBookOrders()) {
048                        if (entry.getBookId() != null) {
049                                Book book = KNSServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId());
050
051                                entry.setUnitPrice(book.getPrice());
052                                Double totalPrice = 0.0d;
053                                if (book.getPrice() != null && entry.getQuantity() != null) {
054                                        totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue();
055                                        if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) {
056                                                totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100);
057                                        }
058                                }
059                                entry.setTotalPrice(new KualiDecimal(totalPrice));
060                        }
061                }
062
063        // clear the used book order entry
064        form.setNewBookOrder(new BookOrder());
065
066        return mapping.findForward("basic");
067    }
068        
069        public ActionForward deleteBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
070        BookOrderForm form = (BookOrderForm) actionForm;
071        BookOrderDocument document = form.getBookOrderDocument();
072        
073        int deleteIndex = getLineToDelete(request);
074        document.removeBookOrder(deleteIndex);
075
076        return mapping.findForward("basic");        
077    }
078
079        @Override
080        protected void doProcessingAfterPost(KualiForm actionForm, HttpServletRequest request) {
081                super.doProcessingAfterPost(actionForm, request);
082                BookOrderForm form = (BookOrderForm) actionForm;
083        BookOrderDocument document = form.getBookOrderDocument();
084        for (BookOrder entry : document.getBookOrders()) {
085                if(entry.getBookId() != null){
086                Book book = KNSServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId());
087                entry.setUnitPrice(book.getPrice());
088                        Double totalPrice = 0.0d;
089                        if (book.getPrice() != null && entry.getQuantity() != null) {
090                                totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue();
091                                if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) {
092                                        totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100);
093                                }
094                        }
095                        entry.setTotalPrice(new KualiDecimal(totalPrice));
096                entry.setBook(book);
097                }
098        }
099        }
100
101        
102    
103    
104        
105}