View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.controller;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.OLEConstants;
20  import org.kuali.ole.select.document.OlePurchaseOrderDocument;
21  import org.kuali.ole.select.form.OLEAddTitlesToInvoiceForm;
22  import org.kuali.ole.select.service.OLEAddTitlesToInvoiceService;
23  import org.kuali.rice.kim.api.identity.Person;
24  import org.kuali.rice.krad.util.GlobalVariables;
25  import org.kuali.rice.krad.web.controller.UifControllerBase;
26  import org.kuali.rice.krad.web.form.UifFormBase;
27  import org.springframework.stereotype.Controller;
28  import org.springframework.validation.BindingResult;
29  import org.springframework.web.bind.annotation.ModelAttribute;
30  import org.springframework.web.bind.annotation.RequestMapping;
31  import org.springframework.web.servlet.ModelAndView;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import java.io.IOException;
36  import java.util.*;
37  
38  @Controller
39  @RequestMapping(value = "/oleTitlesToInvoiceController")
40  public class OLEAddTitlesToInvoiceController extends UifControllerBase {
41  
42  
43      OLEAddTitlesToInvoiceService oleAddTitlesToInvoiceService;
44  
45  
46      public OLEAddTitlesToInvoiceService getOLEAddTitlesToInvoiceService() {
47  
48          return this.oleAddTitlesToInvoiceService;
49      }
50  
51      public OLEAddTitlesToInvoiceService getNewOleAddTitlesToInvoiceService() {
52          this.oleAddTitlesToInvoiceService = new OLEAddTitlesToInvoiceService();
53          return this.oleAddTitlesToInvoiceService;
54      }
55  
56      @Override
57      protected UifFormBase createInitialForm(HttpServletRequest request) {
58          return new OLEAddTitlesToInvoiceForm();
59      }
60  
61      @RequestMapping(params = "methodToCall=pay")
62      public ModelAndView pay(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws IOException {
63          OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
64          oleAddTitlesToInvoiceForm.setErrorMsg("");
65          oleAddTitlesToInvoiceForm.setPayAndReceive(false);
66          double invoiceAmount = 0;
67          //preparing po item list from view page
68          String poItemList = request.getParameter("poItemList");
69          String poItemListSelection = request.getParameter("poItemListSelection");
70          String invoicePriceList = request.getParameter("invoicePriceList");
71          if((!StringUtils.isNotBlank(poItemList)) || (!StringUtils.isNotBlank(poItemListSelection)) || (!(StringUtils.isNotBlank(invoicePriceList)))){
72              return getUIFModelAndView(oleAddTitlesToInvoiceForm)  ;
73          }
74          List<String> poItemListResult = new ArrayList<String>();
75          List<String> invoicedPrice = new ArrayList<String>();
76          String[] poListArray = poItemList.split(":");
77          String[] poListSelectionArray = poItemListSelection.split(":");
78          String[] invoicePriceSectionArray = invoicePriceList.split(":");
79          for (int i = 1; i < poListArray.length; i++) {
80              boolean isNotNullValue=true;
81              if (poListArray[i].equals("true")) {
82                  poItemListResult.add(poListSelectionArray[i]);
83                  if (invoicePriceSectionArray[i] != null && (!invoicePriceSectionArray[i].trim().equalsIgnoreCase(""))) {
84                      if(invoicePriceSectionArray[i].toString().equalsIgnoreCase("null")){
85                          isNotNullValue=false;
86                      }else {
87                         invoiceAmount = invoiceAmount + Double.parseDouble(invoicePriceSectionArray[i].toString());
88                      }
89                  }
90                  if(isNotNullValue){
91                    invoicedPrice.add(String.valueOf(invoicePriceSectionArray[i]));
92                  }else {
93                      invoicedPrice.add(String.valueOf(""));
94                  }
95              }
96  
97          }
98          oleAddTitlesToInvoiceForm.setInvoiceAmount(invoiceAmount + "");
99          oleAddTitlesToInvoiceForm.setOlePurchaseOrderItems(getNewOleAddTitlesToInvoiceService().populateOlePurchaseOrderItemByPoItemList(poItemListResult,invoicedPrice));
100 
101         return getUIFModelAndView(oleAddTitlesToInvoiceForm);
102     }
103 
104     @RequestMapping(params = "methodToCall=receiveAndPay")
105     public ModelAndView receiveAndPay(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
106                                       HttpServletRequest request, HttpServletResponse response) {
107         OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
108         oleAddTitlesToInvoiceForm.setErrorMsg("");
109         oleAddTitlesToInvoiceForm.setPayAndReceive(true);
110         double invoiceAmount = 0;
111         //preparing po item list from view page
112         String poItemList = request.getParameter("poItemList");
113         String poItemListSelection = request.getParameter("poItemListSelection");
114         String invoicePriceList = request.getParameter("invoicePriceList");
115         if((!StringUtils.isNotBlank(poItemList)) || (!StringUtils.isNotBlank(poItemListSelection)) || (!(StringUtils.isNotBlank(invoicePriceList)))){
116             return getUIFModelAndView(oleAddTitlesToInvoiceForm)  ;
117         }
118         List<String> poItemListResult = new ArrayList<String>();
119         List<String> invoicedPrice = new ArrayList<String>();
120         String[] poListArray = poItemList.split(":");
121         String[] poListSelectionArray = poItemListSelection.split(":");
122         String[] invoicePriceSectionArray = invoicePriceList.split(":");
123         for (int i = 1; i < poListArray.length; i++) {
124             boolean isNotNullValue=true;
125             if (poListArray[i].equals("true")) {
126                 poItemListResult.add(poListSelectionArray[i]);
127                 if (invoicePriceSectionArray[i] != null && (!invoicePriceSectionArray[i].trim().equalsIgnoreCase(""))) {
128                     if(invoicePriceSectionArray[i].toString().equalsIgnoreCase("null")){
129                         isNotNullValue=false;
130                     }else {
131                         invoiceAmount = invoiceAmount + Double.parseDouble(invoicePriceSectionArray[i].toString());
132                     }
133                 }
134                 if(isNotNullValue){
135                     invoicedPrice.add(String.valueOf(invoicePriceSectionArray[i]));
136                 }else {
137                     invoicedPrice.add(String.valueOf(""));
138                 }
139             }
140 
141         }
142         oleAddTitlesToInvoiceForm.setInvoiceAmount(invoiceAmount + "");
143         oleAddTitlesToInvoiceForm.setOlePurchaseOrderItems(getNewOleAddTitlesToInvoiceService().populateOlePurchaseOrderItemByPoItemList(poItemListResult,invoicedPrice));
144 
145         return getUIFModelAndView(oleAddTitlesToInvoiceForm);
146     }
147 
148 
149     @Override
150     @RequestMapping(params = "methodToCall=start")
151     public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
152 
153         OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
154         oleAddTitlesToInvoiceForm.setOlePurchaseOrderItems(getNewOleAddTitlesToInvoiceService().populateOlePurchaseOrderItem());
155         oleAddTitlesToInvoiceForm.setPayAndReceive(false);
156         return getUIFModelAndView(oleAddTitlesToInvoiceForm);
157     }
158 
159 
160     @RequestMapping(params = "methodToCall=route")
161     public ModelAndView route(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
162 
163         OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
164         oleAddTitlesToInvoiceForm.setErrorMsg("");
165         oleAddTitlesToInvoiceForm.setSuccessMsg("");
166         boolean isAllowedForInvoice = true;
167         /*List<OlePurchaseOrderItem> olePurchaseOrderItems=getOLEAddTitlesToInvoiceService().getSelectedItems(oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems());*/
168         if(oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems()==null){
169             oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_PO_ITEM_NULL);
170             return getUIFModelAndView(oleAddTitlesToInvoiceForm);
171         }
172         if ((oleAddTitlesToInvoiceForm.getDocumentNumber().equals("")) && (oleAddTitlesToInvoiceForm.getDocumentNumber() != null)) {
173             if (oleAddTitlesToInvoiceForm.getInvoiceDate() == null || oleAddTitlesToInvoiceForm.getInvoiceDate().equals("")) {
174                 oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_DATE);
175                 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
176             }
177             if (oleAddTitlesToInvoiceForm.getInvoiceDate() == null || oleAddTitlesToInvoiceForm.getInvoiceAmount().equals("")) {
178                 oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_VND_AMT);
179                 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
180             }
181             if (oleAddTitlesToInvoiceForm.getPaymentMethod() == null || oleAddTitlesToInvoiceForm.getPaymentMethod().equals("")) {
182                 oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_SELECT_PAYMENT_METHOD);
183                 return getUIFModelAndView(oleAddTitlesToInvoiceForm);
184             }
185         }
186         if (!oleAddTitlesToInvoiceForm.isSkipValidation()) {
187             if (getNewOleAddTitlesToInvoiceService().isPoAlreadyPaid(oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems())) {
188                 oleAddTitlesToInvoiceForm.setContinueReceiveAndPay(true);
189                 oleAddTitlesToInvoiceForm.setSkipValidation(true);
190                 return getUIFModelAndView(form);
191             }
192         }
193         if(!oleAddTitlesToInvoiceForm.isSkipValidation()){
194             oleAddTitlesToInvoiceForm.setContinueReceiveAndPay(false);
195         }
196         if (!getNewOleAddTitlesToInvoiceService().isSelectedAtleatOneItem(oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems())) {
197             oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_SELECT_ATLEAST_ONE_TITLE);
198             return getUIFModelAndView(oleAddTitlesToInvoiceForm);
199         }
200         List<OlePurchaseOrderDocument> olePurchaseOrderDocuments = getNewOleAddTitlesToInvoiceService().populatePurchaseOrderDocuments(oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems());
201         if (!getNewOleAddTitlesToInvoiceService().validateSelectedPurchaseOrderByVendor(oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems())) {
202             oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_SELECT_TITLE_SAME_VENDOR);
203             isAllowedForInvoice = false;
204         }
205         String poId = "";
206         boolean isClosedPosExist = false;
207         for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
208             if (!getNewOleAddTitlesToInvoiceService().validateStatusOfPurchaseOrderDocument(olePurchaseOrderDocument)) {
209                 poId = poId + olePurchaseOrderDocument.getPurapDocumentIdentifier().toString() + ",";
210                 isClosedPosExist = true;
211             }
212         }
213         if (isClosedPosExist) {
214 
215             poId = poId.substring(0, poId.length() - 1);
216             oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_PO_CLOSED + poId);
217             return getUIFModelAndView(oleAddTitlesToInvoiceForm);
218         }
219         Person currentUser = GlobalVariables.getUserSession().getPerson();
220         if (isAllowedForInvoice) {
221             if ((oleAddTitlesToInvoiceForm.getDocumentNumber().equals("")) && (oleAddTitlesToInvoiceForm.getDocumentNumber() != null)) {
222                 try {
223                     if (!getNewOleAddTitlesToInvoiceService().createNewInvoiceDocument(olePurchaseOrderDocuments, oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems(), oleAddTitlesToInvoiceForm.getPaymentMethod(), oleAddTitlesToInvoiceForm.getInvoiceDate(), oleAddTitlesToInvoiceForm.getInvoiceNumber(), oleAddTitlesToInvoiceForm.getInvoiceAmount(), currentUser.getPrincipalId())) {
224                         return getUIFModelAndView(oleAddTitlesToInvoiceForm);
225                     }
226                 } catch (Exception e) {
227                     e.printStackTrace();
228                     throw new RuntimeException("Exception occured while creating invoice", e);
229                 }
230                 oleAddTitlesToInvoiceForm.setSuccessMsg(OLEConstants.OLEAddTitlesToInvoice.SUCCESS_CREATE_INVOICE + OLEConstants.OLEAddTitlesToInvoice.DOC_ID + createLinkForDocHandler(getOLEAddTitlesToInvoiceService().getOleInvoiceDocument().getDocumentNumber()));
231 
232             } else {
233                 if(!getNewOleAddTitlesToInvoiceService().validateNumber(oleAddTitlesToInvoiceForm.getDocumentNumber())){
234                     oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.INVALID_FORMAT_DOC);
235                     return getUIFModelAndView(oleAddTitlesToInvoiceForm);
236                 }
237                 if(!getNewOleAddTitlesToInvoiceService().validateInvoiceDocumentNumber(oleAddTitlesToInvoiceForm.getDocumentNumber())){
238                     oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.INV_OR_NOT_EXIST_DOC_NUM);
239                     return getUIFModelAndView(oleAddTitlesToInvoiceForm);
240                 }
241                 if (!getNewOleAddTitlesToInvoiceService().validateInvoiceDocumentVendor(oleAddTitlesToInvoiceForm.getDocumentNumber(), oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems())) {
242                     oleAddTitlesToInvoiceForm.setErrorMsg(oleAddTitlesToInvoiceForm.getErrorMsg() == null ? "" : (oleAddTitlesToInvoiceForm.getErrorMsg() + "</br>") + OLEConstants.OLEAddTitlesToInvoice.ERROR_SELECT_POITEM_INVOICE_SAME_VND);
243                     return getUIFModelAndView(oleAddTitlesToInvoiceForm);
244                 }
245                 if (!getNewOleAddTitlesToInvoiceService().addOlePurchaseOrderItemsToInvoiceDocument(olePurchaseOrderDocuments, oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems(), oleAddTitlesToInvoiceForm.getDocumentNumber(), currentUser.getPrincipalId(), oleAddTitlesToInvoiceForm.getInvoiceAmount())) {
246                     return getUIFModelAndView(oleAddTitlesToInvoiceForm);
247                 }
248                 try {
249 
250                 } catch (Exception e) {
251                     e.printStackTrace();
252                     throw new RuntimeException("Exception occured while updating invoice ", e);
253                 }
254                 oleAddTitlesToInvoiceForm.setSuccessMsg(OLEConstants.OLEAddTitlesToInvoice.UPDATE_INVOICE + OLEConstants.OLEAddTitlesToInvoice.DOC_ID + createLinkForDocHandler(getOLEAddTitlesToInvoiceService().getOleInvoiceDocument().getDocumentNumber()));
255 
256             }
257         }
258         OLEAddTitlesToInvoiceService titlesToInvoiceService = getOLEAddTitlesToInvoiceService();
259         boolean isReceiveSuccess = false;
260         if (oleAddTitlesToInvoiceForm.isPayAndReceive()) {
261             /*if (oleAddTitlesToInvoiceForm.isPayAndReceive()) {
262                 if (!getNewOleAddTitlesToInvoiceService().validateForReceiving(oleAddTitlesToInvoiceForm.getOlePurchaseOrderItems())) {
263                     oleAddTitlesToInvoiceForm.setErrorMsg(OLEConstants.OLEAddTitlesToInvoice.ERROR_SELECT_RECEIVE_SAME_PO);
264                     return getUIFModelAndView(oleAddTitlesToInvoiceForm);
265                 }
266             }*/
267             try {
268 
269                 Set<String> hashSet = new HashSet<String>();
270                 for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
271                     if (!getNewOleAddTitlesToInvoiceService().validateReceivingForProcess(olePurchaseOrderDocument.getItems()) || getOLEAddTitlesToInvoiceService().isAllowedPoForReceiving()) {
272                         if (titlesToInvoiceService.receiveAndPay(olePurchaseOrderDocument.getItems(), olePurchaseOrderDocument)) {
273                             isReceiveSuccess = true;
274                         }
275                     } else {
276                         hashSet.add(olePurchaseOrderDocument.getPurapDocumentIdentifier().toString());
277                     }
278 
279                 }
280                 if (hashSet.size() > 0) {
281                     String failedPosForReceiving = "";
282                     Iterator<String> itr = hashSet.iterator();
283                     while (itr.hasNext()) {
284                         if (failedPosForReceiving.equalsIgnoreCase("")) {
285                             failedPosForReceiving = itr.next().toString();
286                         } else {
287                             failedPosForReceiving = itr.next().toString() + "," + failedPosForReceiving;
288                         }
289                     }
290                     oleAddTitlesToInvoiceForm.setErrorMsg(oleAddTitlesToInvoiceForm.getErrorMsg() == null ? "" : (oleAddTitlesToInvoiceForm.getErrorMsg() + "</br>") + OLEConstants.OLEAddTitlesToInvoice.ERROR_CREATE_RCV_PO_ITM_EXIST + (failedPosForReceiving.equalsIgnoreCase("") ? "".toString() : "-" + failedPosForReceiving.toString()));
291                 }
292             } catch (Exception e) {
293                 e.printStackTrace();
294                 throw new RuntimeException("Exception occured while creating receiving", e);
295             }
296             if (isReceiveSuccess) {
297                 if (titlesToInvoiceService.getReceivingDocumentsList().size() > 0) {
298                     String poStringDocNumForRCV = createLinkForDocHandler(titlesToInvoiceService.getReceivingDocumentsList());
299                     oleAddTitlesToInvoiceForm.setSuccessMsg(oleAddTitlesToInvoiceForm.getSuccessMsg() + "<br/>" + OLEConstants.OLEAddTitlesToInvoice.SUCCESS_RECEIVE + (poStringDocNumForRCV.equalsIgnoreCase("") ? "".toString() : " DOC ID -" + poStringDocNumForRCV.toString()));
300                 } else {
301                     oleAddTitlesToInvoiceForm.setSuccessMsg(oleAddTitlesToInvoiceForm.getSuccessMsg());
302                 }
303             }
304         }
305         oleAddTitlesToInvoiceForm.setSkipValidation(false);
306         return getUIFModelAndView(oleAddTitlesToInvoiceForm);
307     }
308     public String createLinkForDocHandler(List<String> docIdList){
309         String docIdString="";
310         if(docIdList.size()>0){
311             for(String docId:docIdList){
312                 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;
313                 if(docIdString.equalsIgnoreCase("")){
314                     docIdString=linkUrl;
315                 }   else {
316                     docIdString=linkUrl+","+docIdString;
317                 }
318             }
319         }
320         return docIdString;
321     }
322     public String createLinkForDocHandler(String docId){
323 
324         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;
325        return linkUrl;
326     }
327 
328     @RequestMapping(params = "methodToCall=cancelProcess")
329     public ModelAndView cancelProcess(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
330         OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
331         oleAddTitlesToInvoiceForm.setCancelBox(true);
332         return getUIFModelAndView(oleAddTitlesToInvoiceForm);
333     }
334 
335     @RequestMapping(params = "methodToCall=redirectToRQ")
336     public ModelAndView redirectToReceivingQueue(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
337         OLEAddTitlesToInvoiceForm oleAddTitlesToInvoiceForm = (OLEAddTitlesToInvoiceForm) form;
338         oleAddTitlesToInvoiceForm.setCancelBox(true);
339         return getUIFModelAndView(oleAddTitlesToInvoiceForm);
340     }
341 
342 
343 }