001package org.kuali.ole.select.controller;
002
003import org.apache.commons.lang.StringUtils;
004import org.apache.commons.lang.time.DateUtils;
005import org.kuali.ole.OLEKeyConstants;
006import org.kuali.ole.batch.service.OLEClaimNoticeService;
007import org.kuali.ole.select.bo.OLEClaimingAddress;
008import org.kuali.ole.select.bo.OLEClaimingByTitle;
009import org.kuali.ole.select.bo.OLEClaimingByVendor;
010import org.kuali.ole.select.bo.OLEPOClaimHistory;
011import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
012import org.kuali.ole.select.businessobject.OleRequisitionItem;
013import org.kuali.ole.select.document.OlePurchaseOrderDocument;
014import org.kuali.ole.select.form.OLEAddTitlesToInvoiceForm;
015import org.kuali.ole.select.form.OLEClaimingForm;
016import org.kuali.ole.select.lookup.DocData;
017import org.kuali.ole.vnd.businessobject.VendorDetail;
018import org.kuali.rice.core.api.util.type.KualiInteger;
019import org.kuali.rice.krad.service.KRADServiceLocator;
020import org.kuali.rice.krad.util.GlobalVariables;
021import org.kuali.rice.krad.web.controller.UifControllerBase;
022import org.kuali.rice.krad.web.form.UifFormBase;
023import org.springframework.stereotype.Controller;
024import org.springframework.validation.BindingResult;
025import org.springframework.web.bind.annotation.ModelAttribute;
026import org.springframework.web.bind.annotation.RequestMapping;
027import org.springframework.web.servlet.ModelAndView;
028
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031import java.io.IOException;
032import java.sql.*;
033import java.sql.Date;
034import java.util.*;
035
036/**
037 * Created with IntelliJ IDEA.
038 * User: vivekb
039 * Date: 1/18/14
040 * Time: 7:11 PM
041 * To change oleClaimingByTitle template use File | Settings | File Templates.
042 */
043@Controller
044@RequestMapping(value = "/oleClaimingController")
045public class OLEClaimingController extends UifControllerBase {
046
047    /**
048     * oleClaimingByTitle method converts UifFormBase to OLEClaimingForm
049     *
050     * @param form
051     * @param result
052     * @param request
053     * @param response
054     * @return ModelAndView
055     */
056    @Override
057    @RequestMapping(params = "methodToCall=start")
058    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
059                              HttpServletRequest request, HttpServletResponse response) {
060        OLEClaimingForm oleClaimingForm = (OLEClaimingForm)form;
061        String poItemList = request.getParameter("poItemList");
062        String poItemListSelection = request.getParameter("poItemListSelection");
063        String titleListSelection = request.getParameter("titleListSelection");
064        String vendorListSelection = request.getParameter("vendorListSelection");
065        String[] poListArray = poItemList!=null ? poItemList.split("~") : new String[0];
066        String[] poListSelectionArray = poItemListSelection!=null ? poItemListSelection.split("~") : new String[0];
067        String[] titleListSelectionArray = titleListSelection!=null ? titleListSelection.split("~") : new String[0];
068        String[] vendorListSelectionArray = vendorListSelection!=null ? vendorListSelection.split("~") : new String[0];
069        HashMap<String,List<OLEClaimingByTitle>> hashMap = new HashMap<>();
070        List<OLEClaimingByTitle> claimTitles = null;
071        for (int i = 1; i < poListArray.length; i++) {
072            if (poListArray[i].equals("true")) {
073                if (hashMap.containsKey( vendorListSelectionArray[i])) {
074                    claimTitles = hashMap.get(vendorListSelectionArray[i]);
075                    OLEClaimingByTitle oleClaimingByTitle = new OLEClaimingByTitle();
076                    oleClaimingByTitle.setTitle(titleListSelectionArray[i]);
077                    oleClaimingByTitle.setPoItemId(poListSelectionArray[i]);
078                    claimTitles.add(oleClaimingByTitle);
079                    hashMap.put(vendorListSelectionArray[i],claimTitles);
080                }
081                else {
082                    claimTitles = new ArrayList<>();
083                    OLEClaimingByTitle oleClaimingByTitle = new OLEClaimingByTitle();
084                    oleClaimingByTitle.setTitle(titleListSelectionArray[i]);
085                    oleClaimingByTitle.setPoItemId(poListSelectionArray[i]);
086                    claimTitles.add(oleClaimingByTitle);
087                    hashMap.put(vendorListSelectionArray[i],claimTitles);
088                }
089            }
090        }
091        Iterator<Map.Entry<String,List<OLEClaimingByTitle>>> entries = hashMap.entrySet().iterator();
092        List<OLEClaimingByVendor> oleClaimingByVendors = new ArrayList<>();
093        while (entries.hasNext()) {
094            Map.Entry<String,List<OLEClaimingByTitle>> entry = entries.next();
095            List<OLEClaimingByTitle> oleClaimingByTitles = entry.getValue();
096            OLEClaimingByVendor oleClaimingByVendor = new OLEClaimingByVendor();
097            oleClaimingByVendor.setVendorName(entry.getKey());
098            oleClaimingByVendor.setOleClaimingByTitles(oleClaimingByTitles);
099            oleClaimingByVendors.add(oleClaimingByVendor);
100        }
101        oleClaimingForm.setOleClaimingByVendors(oleClaimingByVendors);
102        return super.start(oleClaimingForm, result, request, response);
103    }
104    @Override
105    protected UifFormBase createInitialForm(HttpServletRequest request) {
106        return new OLEClaimingForm();
107    }
108
109    @RequestMapping(params = "methodToCall=claim")
110    public ModelAndView claim(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws IOException {
111        OLEClaimingForm oleClaimingForm = (OLEClaimingForm) form;
112        OLEClaimNoticeService oleClaimNoticeService = new OLEClaimNoticeService();
113        List<OLEClaimingByVendor>  oleClaimingByVendors = oleClaimingForm.getOleClaimingByVendors();
114        for(OLEClaimingByVendor oleClaimingByVendor:  oleClaimingByVendors){
115            for(OLEClaimingByTitle oleClaimingByTitle : oleClaimingByVendor.getOleClaimingByTitles()){
116                Map map = new HashMap();
117                map.put("itemIdentifier", oleClaimingByTitle.getPoItemId());
118                OlePurchaseOrderItem olePurchaseOrderItem = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePurchaseOrderItem.class,map);
119                if(olePurchaseOrderItem.getClaimCount()!=null){
120                    Integer count = olePurchaseOrderItem.getClaimCount().intValue();
121                    count++;
122                    olePurchaseOrderItem.setClaimCount(new KualiInteger(count));
123                }else {
124                    olePurchaseOrderItem.setClaimCount(new KualiInteger(1));
125                }
126                // TODO olePurchaseOrderItem.setClaimDate();
127                String emailAddress="";
128                String claimInterval="";
129                OlePurchaseOrderDocument olePurchaseOrderDocument = olePurchaseOrderItem.getPurapDocument();
130                VendorDetail vendorDetail =olePurchaseOrderDocument.getVendorDetail();
131                java.util.Date actionDate=null;
132                if( vendorDetail.getVendorContacts()!=null &&  vendorDetail.getVendorContacts().size()>0){
133                    emailAddress = vendorDetail.getVendorContacts().get(0).getVendorContactEmailAddress();
134                    claimInterval = vendorDetail.getClaimInterval();
135                }
136                if((emailAddress==null || emailAddress.isEmpty() ) && vendorDetail.getVendorAddresses()!=null &&  vendorDetail.getVendorAddresses().size()>0){
137                    emailAddress = vendorDetail.getVendorAddresses().get(0).getVendorAddressEmailAddress();
138                }
139                if (StringUtils.isNotBlank(claimInterval)) {
140                    boolean actIntvlFlag = isNumber(claimInterval);
141                    if (actIntvlFlag) {
142                        Integer actIntvl = Integer.parseInt(claimInterval);
143                        actionDate = DateUtils.addDays(new java.util.Date(), actIntvl);
144                        olePurchaseOrderItem.setClaimDate(new Date(actionDate.getTime()));
145                    }
146                }  else{
147                    olePurchaseOrderItem.setClaimDate(null);
148                }
149                oleClaimNoticeService.updateOleClaimingByTitle(oleClaimingByTitle, olePurchaseOrderItem, olePurchaseOrderDocument);
150                OLEClaimingAddress toAddress = oleClaimNoticeService.updateClaimingToAddress(olePurchaseOrderDocument);
151                OLEClaimingAddress fromAddress = oleClaimNoticeService.updateClaimingFromAddress(olePurchaseOrderDocument);
152                oleClaimingByVendor.setToAddress(toAddress);
153                oleClaimingByVendor.setFromAddress(fromAddress);
154                oleClaimingByVendor.setVendorTransmissionFormatDetail(oleClaimingByTitle.getVendorTransmissionFormatDetail());
155                oleClaimingByVendor.setEmailAddress(emailAddress);
156                OLEPOClaimHistory claimHistory = new OLEPOClaimHistory();
157                claimHistory.setClaimCount(olePurchaseOrderItem.getClaimCount().intValue());
158                claimHistory.setClaimDate(new Date(System.currentTimeMillis()));
159                claimHistory.setReqItemId(olePurchaseOrderItem.getCopyList().get(0).getReqItemId());
160                claimHistory.setOperator(GlobalVariables.getUserSession().getPrincipalName());
161                olePurchaseOrderItem.getClaimHistories().add(claimHistory);
162                KRADServiceLocator.getBusinessObjectService().save(olePurchaseOrderItem);
163            }
164            try{
165                oleClaimNoticeService.generatePdf(oleClaimingByVendor);
166            }catch (Exception e){
167                e.printStackTrace();
168            }
169        }
170        //GlobalVariables.getMessageMap().putInfo(OLEKeyConstants.CLAIM_SUCCESS_MSG,OLEKeyConstants.CLAIM_SUCCESS_MSG);
171        oleClaimingForm.setSuccessMsg("Claimed Successfully");
172        return getUIFModelAndView(oleClaimingForm);
173    }
174
175    @RequestMapping(params = "methodToCall=cancelProcess")
176    public ModelAndView cancelProcess(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
177        OLEClaimingForm oleClaimingForm = (OLEClaimingForm) form;
178        oleClaimingForm.setCancelBox(true);
179        return getUIFModelAndView(oleClaimingForm);
180    }
181
182    private static boolean isNumber(String actionInterval) {
183        String actStr = actionInterval;
184        for (int i = 0; i < actStr.length(); i++) {
185            if (!Character.isDigit(actStr.charAt(i)))
186                return false;
187        }
188        return true;
189    }
190
191}