Search in sources :

Example 1 with PurchaseOrderResponse

use of org.kuali.kfs.module.purap.util.cxml.PurchaseOrderResponse in project cu-kfs by CU-CommunityApps.

the class CuB2BPurchaseOrderSciquestServiceImpl method sendPurchaseOrder.

// end KFSUPGRADE-583
public String sendPurchaseOrder(PurchaseOrderDocument purchaseOrder) {
    /*
         * IMPORTANT DESIGN NOTE: We need the contract manager's name, phone number, and e-mail address. B2B orders that don't
         * qualify to become APO's will have contract managers on the PO, and the ones that DO become APO's will not. We decided to
         * always get the contract manager from the B2B contract associated with the order, and for B2B orders to ignore the
         * contract manager field on the PO. We pull the name and phone number from the contract manager table and get the e-mail
         * address from the user data.
         */
    // CU change for contract manager
    // non-catalog POs might not have a vendor contract, so we need to get the contract manager from the PO which will always be there
    ContractManager contractManager = null;
    if (!PurapConstants.RequisitionSources.B2B.equals(purchaseOrder.getRequisitionSourceCode())) {
        contractManager = purchaseOrder.getContractManager();
    } else {
        contractManager = purchaseOrder.getVendorContract().getContractManager();
    }
    String contractManagerEmail = getContractManagerEmail(contractManager);
    String vendorDuns = purchaseOrder.getVendorDetail().getVendorDunsNumber();
    RequisitionDocument r = requisitionService.getRequisitionById(purchaseOrder.getRequisitionIdentifier());
    WorkflowDocument reqWorkflowDoc = r.getDocumentHeader().getWorkflowDocument();
    String requisitionInitiatorPrincipalId = getRequisitionInitiatorPrincipal(reqWorkflowDoc.getInitiatorPrincipalId());
    if (LOG.isDebugEnabled()) {
        LOG.debug("sendPurchaseOrder(): b2bPurchaseOrderURL is " + getB2bPurchaseOrderURL());
    }
    if (!PurapConstants.RequisitionSources.B2B.equals(purchaseOrder.getRequisitionSourceCode())) {
        prepareNonB2BPurchaseOrderForTransmission(purchaseOrder);
    }
    String validateErrors = verifyCxmlPOData(purchaseOrder, requisitionInitiatorPrincipalId, getB2bPurchaseOrderPassword(), contractManager, contractManagerEmail, vendorDuns);
    if (!StringUtils.isEmpty(validateErrors)) {
        return validateErrors;
    }
    StringBuffer transmitErrors = new StringBuffer();
    try {
        LOG.debug("sendPurchaseOrder() Generating cxml");
        String cxml = getCxml(purchaseOrder, requisitionInitiatorPrincipalId, getB2bPurchaseOrderPassword(), contractManager, contractManagerEmail, vendorDuns, true);
        LOG.debug("sendPurchaseOrder() Sending cxml\n" + cxml);
        LOG.debug("sendPurchaseOrder() Sending cxml\n" + cxml);
        String responseCxml = b2bDao.sendPunchOutRequest(cxml, getB2bPurchaseOrderURL());
        LOG.info("b2bPurchaseOrderURL " + getB2bPurchaseOrderURL());
        LOG.info("sendPurchaseOrder(): Response cXML for po #" + purchaseOrder.getPurapDocumentIdentifier() + ":\n" + responseCxml);
        // allow PO to use old form, then POA use new form for testing
        if (!responseCxml.contains("Success") && responseCxml.contains("No custom field found") && responseCxml.contains("document configuration (DeliveryEmail)")) {
            String cxml1 = cxml.substring(0, cxml.indexOf("<CustomFieldValueSet label=\"Delivery Phone")) + cxml.substring(cxml.indexOf("</POHeader>"));
            LOG.info("sendPurchaseOrder() re-Sending cxml\n" + cxml1);
            responseCxml = b2bDao.sendPunchOutRequest(cxml1, getB2bPurchaseOrderURL());
            cxml = getCxml(purchaseOrder, requisitionInitiatorPrincipalId, getB2bPurchaseOrderPassword(), contractManager, contractManagerEmail, vendorDuns, false);
            LOG.debug("sendPurchaseOrder() re-Sending cxml\n" + cxml);
            responseCxml = b2bDao.sendPunchOutRequest(cxml, getB2bPurchaseOrderURL());
            LOG.info("re-sendPurchaseOrder(): Response cXML for po number " + purchaseOrder.getPurapDocumentIdentifier() + ":" + responseCxml);
        }
        PurchaseOrderResponse poResponse = B2BParserHelper.getInstance().parsePurchaseOrderResponse(responseCxml);
        String statusText = poResponse.getStatusText();
        if (LOG.isDebugEnabled()) {
            LOG.debug("sendPurchaseOrder(): statusText is " + statusText);
        }
        if (ObjectUtils.isNull(statusText) || (!"success".equalsIgnoreCase(statusText.trim()))) {
            LOG.error("sendPurchaseOrder(): PO cXML for po number " + purchaseOrder.getPurapDocumentIdentifier() + " failed sending to SciQuest:\n" + statusText);
            transmitErrors.append("Unable to send Purchase Order: " + statusText);
            // find any additional error messages that might have been sent
            List errorMessages = poResponse.getPOResponseErrorMessages();
            if (ObjectUtils.isNotNull(errorMessages) && !errorMessages.isEmpty()) {
                for (Iterator iter = errorMessages.iterator(); iter.hasNext(); ) {
                    String errorMessage = (String) iter.next();
                    if (ObjectUtils.isNotNull(errorMessage)) {
                        LOG.error("sendPurchaseOrder(): SciQuest error message for po number " + purchaseOrder.getPurapDocumentIdentifier() + ": " + errorMessage);
                        transmitErrors.append("Error sending Purchase Order: " + errorMessage);
                    }
                }
            }
        }
    } catch (B2BConnectionException e) {
        LOG.error("sendPurchaseOrder() Error connecting to b2b", e);
        transmitErrors.append("Connection to vendor failed.");
    } catch (CxmlParseError e) {
        LOG.error("sendPurchaseOrder() Error Parsing", e);
        transmitErrors.append("Unable to read cxml returned from vendor.");
    } catch (Throwable e) {
        LOG.error("sendPurchaseOrder() Unknown Error", e);
        transmitErrors.append("Unexpected error occurred while attempting to transmit Purchase Order.");
    }
    return transmitErrors.toString();
}
Also used : RequisitionDocument(org.kuali.kfs.module.purap.document.RequisitionDocument) ContractManager(org.kuali.kfs.vnd.businessobject.ContractManager) WorkflowDocument(org.kuali.rice.kew.api.WorkflowDocument) PurchaseOrderResponse(org.kuali.kfs.module.purap.util.cxml.PurchaseOrderResponse) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) B2BConnectionException(org.kuali.kfs.module.purap.exception.B2BConnectionException) CxmlParseError(org.kuali.kfs.module.purap.exception.CxmlParseError)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 RequisitionDocument (org.kuali.kfs.module.purap.document.RequisitionDocument)1 B2BConnectionException (org.kuali.kfs.module.purap.exception.B2BConnectionException)1 CxmlParseError (org.kuali.kfs.module.purap.exception.CxmlParseError)1 PurchaseOrderResponse (org.kuali.kfs.module.purap.util.cxml.PurchaseOrderResponse)1 ContractManager (org.kuali.kfs.vnd.businessobject.ContractManager)1 WorkflowDocument (org.kuali.rice.kew.api.WorkflowDocument)1