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