1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.controller;
17
18 import org.kuali.ole.OLEConstants;
19 import org.kuali.ole.select.document.OlePurchaseOrderDocument;
20 import org.kuali.ole.select.form.OLEAddTitlesToInvoiceForm;
21 import org.kuali.ole.select.service.OLEAddTitlesToInvoiceService;
22 import org.kuali.rice.kim.api.identity.Person;
23 import org.kuali.rice.krad.util.GlobalVariables;
24 import org.kuali.rice.krad.web.controller.UifControllerBase;
25 import org.kuali.rice.krad.web.form.UifFormBase;
26 import org.springframework.stereotype.Controller;
27 import org.springframework.validation.BindingResult;
28 import org.springframework.web.bind.annotation.ModelAttribute;
29 import org.springframework.web.bind.annotation.RequestMapping;
30 import org.springframework.web.servlet.ModelAndView;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import java.util.*;
35
36 @Controller
37 @RequestMapping(value = "/oleTitlesToInvoiceController")
38 public class OLEAddTitlesToInvoiceController extends UifControllerBase {
39
40
41 OLEAddTitlesToInvoiceService oleAddTitlesToInvoiceService;
42
43
44 public OLEAddTitlesToInvoiceService getOLEAddTitlesToInvoiceService() {
45
46 return this.oleAddTitlesToInvoiceService;
47 }
48
49 public OLEAddTitlesToInvoiceService getNewOleAddTitlesToInvoiceService() {
50 this.oleAddTitlesToInvoiceService = new OLEAddTitlesToInvoiceService();
51 return this.oleAddTitlesToInvoiceService;
52 }
53
54 @Override
55 protected UifFormBase createInitialForm(HttpServletRequest request) {
56 return new OLEAddTitlesToInvoiceForm();
57 }
58
59 @RequestMapping(params = "methodToCall=pay")
60 public ModelAndView pay(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
61 OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
62 oleAddTitlesToInvoiceForm.setErrorMsg("");
63 oleAddTitlesToInvoiceForm.setPayAndReceive(false);
64 double invoiceAmount = 0;
65
66 String poItemList = request.getParameter("poItemList");
67 String poItemListSelection = request.getParameter("poItemListSelection");
68 String invoicePriceList = request.getParameter("invoicePriceList");
69 List<String> poItemListResult = new ArrayList<String>();
70 List<String> invoicedPrice = new ArrayList<String>();
71 String[] poListArray = poItemList.split(":");
72 String[] poListSelectionArray = poItemListSelection.split(":");
73 String[] invoicePriceSectionArray = invoicePriceList.split(":");
74 for (int i = 1; i < poListArray.length; i++) {
75 boolean isNotNullValue=true;
76 if (poListArray[i].equals("true")) {
77 poItemListResult.add(poListSelectionArray[i]);
78 if (invoicePriceSectionArray[i] != null && (!invoicePriceSectionArray[i].trim().equalsIgnoreCase(""))) {
79 if(invoicePriceSectionArray[i].toString().equalsIgnoreCase("null")){
80 isNotNullValue=false;
81 }else {
82 invoiceAmount = invoiceAmount + Double.parseDouble(invoicePriceSectionArray[i].toString());
83 }
84 }
85 if(isNotNullValue){
86 invoicedPrice.add(String.valueOf(invoicePriceSectionArray[i]));
87 }else {
88 invoicedPrice.add(String.valueOf(""));
89 }
90 }
91
92 }
93 oleAddTitlesToInvoiceForm.setInvoiceAmount(invoiceAmount + "");
94 oleAddTitlesToInvoiceForm.setOlePurchaseOrderItems(getNewOleAddTitlesToInvoiceService().populateOlePurchaseOrderItemByPoItemList(poItemListResult,invoicedPrice));
95
96 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
97 }
98
99 @RequestMapping(params = "methodToCall=receiveAndPay")
100 public ModelAndView receiveAndPay(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
101 HttpServletRequest request, HttpServletResponse response) {
102 OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
103 oleAddTitlesToInvoiceForm.setErrorMsg("");
104 oleAddTitlesToInvoiceForm.setPayAndReceive(true);
105 double invoiceAmount = 0;
106
107 String poItemList = request.getParameter("poItemList");
108 String poItemListSelection = request.getParameter("poItemListSelection");
109 String invoicePriceList = request.getParameter("invoicePriceList");
110 List<String> poItemListResult = new ArrayList<String>();
111 List<String> invoicedPrice = new ArrayList<String>();
112 String[] poListArray = poItemList.split(":");
113 String[] poListSelectionArray = poItemListSelection.split(":");
114 String[] invoicePriceSectionArray = invoicePriceList.split(":");
115 for (int i = 1; i < poListArray.length; i++) {
116 boolean isNotNullValue=true;
117 if (poListArray[i].equals("true")) {
118 poItemListResult.add(poListSelectionArray[i]);
119 if (invoicePriceSectionArray[i] != null && (!invoicePriceSectionArray[i].trim().equalsIgnoreCase(""))) {
120 if(invoicePriceSectionArray[i].toString().equalsIgnoreCase("null")){
121 isNotNullValue=false;
122 }else {
123 invoiceAmount = invoiceAmount + Double.parseDouble(invoicePriceSectionArray[i].toString());
124 }
125 }
126 if(isNotNullValue){
127 invoicedPrice.add(String.valueOf(invoicePriceSectionArray[i]));
128 }else {
129 invoicedPrice.add(String.valueOf(""));
130 }
131 }
132
133 }
134 oleAddTitlesToInvoiceForm.setInvoiceAmount(invoiceAmount + "");
135 oleAddTitlesToInvoiceForm.setOlePurchaseOrderItems(getNewOleAddTitlesToInvoiceService().populateOlePurchaseOrderItemByPoItemList(poItemListResult,invoicedPrice));
136
137 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
138 }
139
140
141 @Override
142 @RequestMapping(params = "methodToCall=start")
143 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
144
145 OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
146 oleAddTitlesToInvoiceForm.setOlePurchaseOrderItems(getNewOleAddTitlesToInvoiceService().populateOlePurchaseOrderItem());
147 oleAddTitlesToInvoiceForm.setPayAndReceive(false);
148 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
149 }
150
151
152 @RequestMapping(params = "methodToCall=route")
153 public ModelAndView route(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
154
155 OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
156 oleAddTitlesToInvoiceForm.setErrorMsg("");
157 oleAddTitlesToInvoiceForm.setSuccessMsg("");
158 boolean isAllowedForInvoice = true;
159
160 if (!getNewOleAddTitlesToInvoiceService().isSelectedAtleatOneItem(oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems())) {
161 oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_SELECT_ATLEAST_ONE_TITLE);
162 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
163 }
164 List<OlePurchaseOrderDocument> olePurchaseOrderDocuments = getNewOleAddTitlesToInvoiceService().populatePurchaseOrderDocuments(oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems());
165 if (!getNewOleAddTitlesToInvoiceService().validateSelectedPurchaseOrderByVendor(oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems())) {
166 oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_SELECT_TITLE_SAME_VENDOR);
167 isAllowedForInvoice = false;
168 }
169 String poId = "";
170 boolean isClosedPosExist = false;
171 for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
172 if (!getNewOleAddTitlesToInvoiceService().validateStatusOfPurchaseOrderDocument(olePurchaseOrderDocument)) {
173 poId = poId + olePurchaseOrderDocument.getPurapDocumentIdentifier().toString() + ",";
174 isClosedPosExist = true;
175 }
176 }
177 if (isClosedPosExist) {
178
179 poId = poId.substring(0, poId.length() - 1);
180 oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_PO_CLOSED + poId);
181 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
182 }
183 Person currentUser = GlobalVariables.getUserSession().getPerson();
184 if (isAllowedForInvoice) {
185 if ((oleAddTitlesToInvoiceForm.getInvoiceNbr().equals("")) && (oleAddTitlesToInvoiceForm.getInvoiceNbr() != null)) {
186 if (oleAddTitlesToInvoiceForm.getInvoiceDate() == null || oleAddTitlesToInvoiceForm.getInvoiceDate().equals("")) {
187 oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_DATE);
188 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
189 }
190 if (oleAddTitlesToInvoiceForm.getInvoiceDate() == null || oleAddTitlesToInvoiceForm.getInvoiceAmount().equals("")) {
191 oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_VND_AMT);
192 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
193 }
194
195
196
197
198 if (oleAddTitlesToInvoiceForm.getPaymentMethod() == null || oleAddTitlesToInvoiceForm.getPaymentMethod().equals("")) {
199 oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_SELECT_PAYMENT_METHOD);
200 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
201 }
202 try {
203 if (!getNewOleAddTitlesToInvoiceService().createNewInvoiceDocument(olePurchaseOrderDocuments, oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems(), oleAddTitlesToInvoiceForm.getPaymentMethod(), oleAddTitlesToInvoiceForm.getInvoiceDate(), oleAddTitlesToInvoiceForm.getInvoiceNumber(), oleAddTitlesToInvoiceForm.getInvoiceAmount(), currentUser.getPrincipalId())) {
204 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
205 }
206 } catch (Exception e) {
207 e.printStackTrace();
208 throw new RuntimeException("Exception occured while creating invoice", e);
209 }
210 oleAddTitlesToInvoiceForm.setSuccessMsg(OLEConstants.OLEAddTitlesToInvoice.SUCCESS_CREATE_INVOICE + OLEConstants.OLEAddTitlesToInvoice.DOC_ID + createLinkForDocHandler(getOLEAddTitlesToInvoiceService().getOleInvoiceDocument().getDocumentNumber()));
211
212 } else {
213
214 if (!getNewOleAddTitlesToInvoiceService().validateInvoiceDocumentVendor(oleAddTitlesToInvoiceForm.getInvoiceNbr(), oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems())) {
215 oleAddTitlesToInvoiceForm.setErrorMsg(oleAddTitlesToInvoiceForm.getErrorMsg() == null ? "" : (oleAddTitlesToInvoiceForm.getErrorMsg() + "</br>") + OLEConstants.OLEAddTitlesToInvoice.ERROR_SELECT_POITEM_INVOICE_SAME_VND);
216 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
217 }
218 if (!getNewOleAddTitlesToInvoiceService().addOlePurchaseOrderItemsToInvoiceDocument(olePurchaseOrderDocuments, oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems(), oleAddTitlesToInvoiceForm.getInvoiceNbr(), currentUser.getPrincipalId(), oleAddTitlesToInvoiceForm.getInvoiceAmount())) {
219 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
220 }
221 try {
222
223 } catch (Exception e) {
224 e.printStackTrace();
225 throw new RuntimeException("Exception occured while updating invoice ", e);
226 }
227 oleAddTitlesToInvoiceForm.setSuccessMsg(OLEConstants.OLEAddTitlesToInvoice.UPDATE_INVOICE + OLEConstants.OLEAddTitlesToInvoice.DOC_ID + getOLEAddTitlesToInvoiceService().getOleInvoiceDocument().getDocumentNumber());
228
229 }
230 }
231 OLEAddTitlesToInvoiceService titlesToInvoiceService = getOLEAddTitlesToInvoiceService();
232 boolean isReceiveSuccess = false;
233 if (oleAddTitlesToInvoiceForm.isPayAndReceive()) {
234
235
236
237
238
239
240 try {
241
242 Set<String> hashSet = new HashSet<String>();
243 for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
244 if (!getNewOleAddTitlesToInvoiceService().validateReceivingForProcess(olePurchaseOrderDocument.getItems()) || getOLEAddTitlesToInvoiceService().isAllowedPoForReceiving()) {
245 if (titlesToInvoiceService.receiveAndPay(olePurchaseOrderDocument.getItems(), olePurchaseOrderDocument)) {
246 isReceiveSuccess = true;
247 }
248 } else {
249 hashSet.add(olePurchaseOrderDocument.getPurapDocumentIdentifier().toString());
250 }
251
252 }
253 if (hashSet.size() > 0) {
254 String failedPosForReceiving = "";
255 Iterator<String> itr = hashSet.iterator();
256 while (itr.hasNext()) {
257 if (failedPosForReceiving.equalsIgnoreCase("")) {
258 failedPosForReceiving = itr.next().toString();
259 } else {
260 failedPosForReceiving = itr.next().toString() + "," + failedPosForReceiving;
261 }
262 }
263 oleAddTitlesToInvoiceForm.setErrorMsg(oleAddTitlesToInvoiceForm.getErrorMsg() == null ? "" : (oleAddTitlesToInvoiceForm.getErrorMsg() + "</br>") + OLEConstants.OLEAddTitlesToInvoice.ERROR_CREATE_RCV_PO_ITM_EXIST + (failedPosForReceiving.equalsIgnoreCase("") ? "".toString() : "-" + failedPosForReceiving.toString()));
264 }
265 } catch (Exception e) {
266 e.printStackTrace();
267 throw new RuntimeException("Exception occured while creating receiving", e);
268 }
269 if (isReceiveSuccess) {
270 if (titlesToInvoiceService.getReceivingDocumentsList().size() > 0) {
271 String poStringDocNumForRCV = createLinkForDocHandler(titlesToInvoiceService.getReceivingDocumentsList());
272 oleAddTitlesToInvoiceForm.setSuccessMsg(oleAddTitlesToInvoiceForm.getSuccessMsg() + "<br/>" + OLEConstants.OLEAddTitlesToInvoice.SUCCESS_RECEIVE + (poStringDocNumForRCV.equalsIgnoreCase("") ? "".toString() : " DOC ID -" + poStringDocNumForRCV.toString()));
273 } else {
274 oleAddTitlesToInvoiceForm.setSuccessMsg(oleAddTitlesToInvoiceForm.getSuccessMsg());
275 }
276 }
277 }
278 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
279 }
280 public String createLinkForDocHandler(List<String> docIdList){
281 String docIdString="";
282 if(docIdList.size()>0){
283 for(String docId:docIdList){
284 String linkUrl=OLEConstants.OLEAddTitlesToInvoice.LINK_START_TAG_HREF+ "\" "+OLEConstants.OLEAddTitlesToInvoice.LINK_DOC_HANDLER+docId+"\" "+" target=\"_blank\" "+OLEConstants.OLEAddTitlesToInvoice.ANCHOR_END+docId+OLEConstants.OLEAddTitlesToInvoice.LINK_END_TAG;
285 if(docIdString.equalsIgnoreCase("")){
286 docIdString=linkUrl;
287 } else {
288 docIdString=linkUrl+","+docIdString;
289 }
290 }
291 }
292 return docIdString;
293 }
294 public String createLinkForDocHandler(String docId){
295
296 String linkUrl=OLEConstants.OLEAddTitlesToInvoice.LINK_START_TAG_HREF+"\" "+OLEConstants.OLEAddTitlesToInvoice.LINK_DOC_HANDLER+docId+"\" "+" target=\"_blank\" "+OLEConstants.OLEAddTitlesToInvoice.ANCHOR_END+docId+OLEConstants.OLEAddTitlesToInvoice.LINK_END_TAG;
297 return linkUrl;
298 }
299
300 @RequestMapping(params = "methodToCall=cancelProcess")
301 public ModelAndView cancelProcess(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
302 OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
303 oleAddTitlesToInvoiceForm.setCancelBox(true);
304 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
305 }
306
307 @RequestMapping(params = "methodToCall=redirectToRQ")
308 public ModelAndView redirectToReceivingQueue(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
309 OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
310 oleAddTitlesToInvoiceForm.setCancelBox(true);
311 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
312 }
313
314
315 }