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