| 1 | |
package edu.sampleu.bookstore.document.web; |
| 2 | |
|
| 3 | |
import edu.sampleu.bookstore.bo.Book; |
| 4 | |
import edu.sampleu.bookstore.bo.BookOrder; |
| 5 | |
import edu.sampleu.bookstore.document.BookOrderDocument; |
| 6 | |
import org.apache.struts.action.ActionForm; |
| 7 | |
import org.apache.struts.action.ActionForward; |
| 8 | |
import org.apache.struts.action.ActionMapping; |
| 9 | |
import org.kuali.rice.core.api.util.type.KualiDecimal; |
| 10 | |
import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase; |
| 11 | |
import org.kuali.rice.kns.web.struts.form.KualiForm; |
| 12 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
| 13 | |
|
| 14 | |
import javax.servlet.http.HttpServletRequest; |
| 15 | |
import javax.servlet.http.HttpServletResponse; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | 0 | public class BookOrderAction extends KualiTransactionalDocumentActionBase { |
| 23 | |
|
| 24 | |
public ActionForward addBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { |
| 25 | 0 | BookOrderForm form = (BookOrderForm) actionForm; |
| 26 | 0 | BookOrderDocument document = form.getBookOrderDocument(); |
| 27 | |
|
| 28 | 0 | BookOrder newBookEntry = form.getNewBookOrder(); |
| 29 | 0 | document.addBookOrder(newBookEntry); |
| 30 | |
|
| 31 | 0 | for (BookOrder entry : document.getBookOrders()) { |
| 32 | 0 | if (entry.getBookId() != null) { |
| 33 | 0 | Book book = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId()); |
| 34 | |
|
| 35 | 0 | entry.setUnitPrice(book.getPrice()); |
| 36 | 0 | Double totalPrice = 0.0d; |
| 37 | 0 | if (book.getPrice() != null && entry.getQuantity() != null) { |
| 38 | 0 | totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue(); |
| 39 | 0 | if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) { |
| 40 | 0 | totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100); |
| 41 | |
} |
| 42 | |
} |
| 43 | 0 | entry.setTotalPrice(new KualiDecimal(totalPrice)); |
| 44 | 0 | } |
| 45 | |
} |
| 46 | |
|
| 47 | |
|
| 48 | 0 | form.setNewBookOrder(new BookOrder()); |
| 49 | |
|
| 50 | 0 | return mapping.findForward("basic"); |
| 51 | |
} |
| 52 | |
|
| 53 | |
public ActionForward deleteBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { |
| 54 | 0 | BookOrderForm form = (BookOrderForm) actionForm; |
| 55 | 0 | BookOrderDocument document = form.getBookOrderDocument(); |
| 56 | |
|
| 57 | 0 | int deleteIndex = getLineToDelete(request); |
| 58 | 0 | document.removeBookOrder(deleteIndex); |
| 59 | |
|
| 60 | 0 | return mapping.findForward("basic"); |
| 61 | |
} |
| 62 | |
|
| 63 | |
@Override |
| 64 | |
protected void doProcessingAfterPost(KualiForm actionForm, HttpServletRequest request) { |
| 65 | 0 | super.doProcessingAfterPost(actionForm, request); |
| 66 | 0 | BookOrderForm form = (BookOrderForm) actionForm; |
| 67 | 0 | BookOrderDocument document = form.getBookOrderDocument(); |
| 68 | 0 | for (BookOrder entry : document.getBookOrders()) { |
| 69 | 0 | if(entry.getBookId() != null){ |
| 70 | 0 | Book book = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId()); |
| 71 | 0 | entry.setUnitPrice(book.getPrice()); |
| 72 | 0 | Double totalPrice = 0.0d; |
| 73 | 0 | if (book.getPrice() != null && entry.getQuantity() != null) { |
| 74 | 0 | totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue(); |
| 75 | 0 | if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) { |
| 76 | 0 | totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100); |
| 77 | |
} |
| 78 | |
} |
| 79 | 0 | entry.setTotalPrice(new KualiDecimal(totalPrice)); |
| 80 | 0 | entry.setBook(book); |
| 81 | 0 | } |
| 82 | |
} |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
} |