Search in sources :

Example 1 with PurchaseOrderService

use of org.kuali.kfs.module.purap.document.service.PurchaseOrderService in project cu-kfs by CU-CommunityApps.

the class CuElectronicInvoiceTestAction method generate.

public ActionForward generate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    checkAuthorization(form, "");
    ElectronicInvoiceTestForm testForm = (ElectronicInvoiceTestForm) form;
    String poDocNumber = testForm.getPoDocNumber();
    LOG.info("Generating Electronic Invoice XML file for Purchase Order Document " + poDocNumber);
    PurchaseOrderService poService = SpringContext.getBean(PurchaseOrderService.class);
    PurchaseOrderDocument po = null;
    if (StringUtils.isBlank(poDocNumber)) {
        GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_DOCUMENT_NUMBER, PurapKeyConstants.ERROR_ELECTRONIC_INVOICE_GENERATION_PURCHASE_ORDER_NUMBER_EMPTY, new String[] { poDocNumber });
        return mapping.findForward(RiceConstants.MAPPING_BASIC);
    }
    if (!getDocumentService().documentExists(poDocNumber)) {
        GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_DOCUMENT_NUMBER, PurapKeyConstants.ERROR_ELECTRONIC_INVOICE_GENERATION_PURCHASE_ORDER_DOES_NOT_EXIST, poDocNumber);
        return mapping.findForward(RiceConstants.MAPPING_BASIC);
    }
    try {
        po = poService.getPurchaseOrderByDocumentNumber(poDocNumber);
    } catch (Exception e) {
        throw e;
    }
    response.setHeader("Cache-Control", "max-age=30");
    response.setContentType("application/xml");
    StringBuffer sbContentDispValue = new StringBuffer();
    String useJavascript = request.getParameter("useJavascript");
    if (useJavascript == null || useJavascript.equalsIgnoreCase("false")) {
        sbContentDispValue.append("attachment");
    } else {
        sbContentDispValue.append("inline");
    }
    StringBuffer sbFilename = new StringBuffer();
    sbFilename.append("PO_");
    sbFilename.append(poDocNumber);
    sbFilename.append(".xml");
    sbContentDispValue.append("; filename=");
    sbContentDispValue.append(sbFilename);
    response.setHeader("Content-disposition", sbContentDispValue.toString());
    // lookup the PO and fill in the XML with valid data
    if (po != null) {
        String duns = "";
        if (po.getVendorDetail() != null) {
            duns = StringUtils.defaultString(po.getVendorDetail().getVendorDunsNumber());
        }
        DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
        // getting date in kfs format
        String currDate = ElectronicInvoiceUtils.getDateDisplayText(dateTimeService.getCurrentDate());
        String vendorNumber = po.getVendorDetail().getVendorNumber();
        String eInvoiceFile = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "\n<!-- ******Testing tool generated XML****** Version 1.2." + "\n\n  Generated On " + currDate + " for PO " + po.getPurapDocumentIdentifier() + " (Doc# " + poDocNumber + ") -->\n\n" + "<!-- All the cXML attributes are junk values -->\n" + "<cXML payloadID=\"200807260401062080.964@eai002\"\n" + "    timestamp=\"2008-07-26T04:01:06-08:00\"\n" + "    version=\"1.2.014\" xml:lang=\"en\" \n" + "    xmlns=\"http://www.kuali.org/kfs/purap/electronicInvoice\" \n" + "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + "  <Header>\n" + "      <From>\n" + "          <Credential domain=\"DUNS\">\n" + "              <Identity>" + duns + "</Identity> <!-- DUNS number from PO Vendor " + vendorNumber + "-->\n" + "          </Credential>\n" + "      </From>\n" + "      <To>\n" + "          <Credential domain=\"NetworkId\">\n" + "              <Identity>" + "IU" + "</Identity> <!-- Hardcoded --> \n" + "          </Credential>\n" + "      </To>\n" + "      <Sender>\n" + "          <Credential domain=\"DUNS\">\n" + "              <Identity>" + duns + "</Identity> <!-- DUNS number from PO Vendor " + vendorNumber + "-->\n" + "          </Credential>\n" + "          <UserAgent/>\n" + "      </Sender>\n" + "  </Header>\n" + "  <Request deploymentMode=\"production\">\n" + "      <InvoiceDetailRequest>\n" + "          <InvoiceDetailRequestHeader\n" + "              invoiceDate=\"" + currDate + "\" invoiceID=\"" + RandomUtils.nextInt() + "\" operation=\"new\" purpose=\"standard\"> <!-- invoiceID=Random unique Id, invoiceDate=Curr date -->\n" + "              <InvoiceDetailHeaderIndicator/>\n" + "              <InvoiceDetailLineIndicator/>\n" + "              <InvoicePartner>\n" + getContactXMLChunk("billTo", po) + "              </InvoicePartner>\n" + "              <InvoicePartner>\n" + "                  <Contact addressID=\"" + RandomUtils.nextInt() + "\" role=\"remitTo\"> <!-- Vendor address -->\n" + "                      <Name xml:lang=\"en\">\n" + "                          " + po.getVendorName() + "\n" + "                      </Name>\n" + "                      <PostalAddress>\n" + "                          <Street>" + StringUtils.defaultString(po.getVendorLine1Address()) + "</Street>\n" + "                          <Street>" + StringUtils.defaultString(po.getVendorLine2Address()) + "</Street>\n" + "                          <City>" + StringUtils.defaultString(po.getVendorCityName()) + "</City>\n" + "                          <State>" + StringUtils.defaultString(po.getVendorStateCode()) + "</State>\n" + "                          <PostalCode>" + StringUtils.defaultString(po.getVendorPostalCode()) + "</PostalCode>\n" + "                          <Country isoCountryCode=\"" + StringUtils.defaultString(po.getVendorCountryCode()) + "\">\n" + "                              " + StringUtils.defaultString(po.getVendorCountry().getName()) + "\n" + "                          </Country>\n" + "                      </PostalAddress>\n" + "                  </Contact>\n" + "              </InvoicePartner>\n" + getDeliveryAddressXMLChunk("shipTo", po) + getPaymentTermXML(po) + "          </InvoiceDetailRequestHeader>\n" + "          <InvoiceDetailOrder>\n" + "              <InvoiceDetailOrderInfo>\n" + "                  <OrderReference\n" + "                      orderDate=\"" + ElectronicInvoiceUtils.getDateDisplayText(dateTimeService.getCurrentDate()) + "\" orderID=\"" + po.getPurapDocumentIdentifier() + "\"> <!--orderDate=Curr date,orderID=PO#-->\n" + "                      <DocumentReference payloadID=\"NA\" /> <!--HardCoded-->\n" + "                  </OrderReference>\n" + "              </InvoiceDetailOrderInfo>\n" + "              <!-- No junk values in Items-->\n";
        for (int i = 0; i < po.getItems().size(); i++) {
            List items = po.getItems();
            PurchaseOrderItem item = (PurchaseOrderItem) items.get(i);
            if (!item.getItemType().isAdditionalChargeIndicator()) {
                eInvoiceFile = eInvoiceFile + getPOItemXMLChunk(item);
            }
        }
        KualiDecimal totalDollarAmt = po.getTotalDollarAmount() == null ? KualiDecimal.ZERO : po.getTotalDollarAmount();
        eInvoiceFile = eInvoiceFile + "          </InvoiceDetailOrder>\n" + "          <InvoiceDetailSummary>\n" + "              <SubtotalAmount>\n" + "                  <Money currency=\"USD\">" + po.getTotalPreTaxDollarAmount() + "</Money>\n" + "              </SubtotalAmount>\n" + "              <Tax>\n" + "                  <Money currency=\"USD\">" + po.getTotalTaxAmount() + "</Money>\n" + "                  <Description xml:lang=\"en\">Total Tax</Description>\n" + "              </Tax>\n" + "              <SpecialHandlingAmount>\n" + "                  <Money currency=\"USD\">0.00</Money>\n" + "              </SpecialHandlingAmount>\n" + "              <ShippingAmount>\n" + "                  <Money currency=\"USD\">0.00</Money>\n" + "              </ShippingAmount>\n" + "              <GrossAmount>\n" + "                  <Money currency=\"USD\">" + totalDollarAmt + "</Money>\n" + "              </GrossAmount>\n" + "              <InvoiceDetailDiscount>\n" + "                  <Money currency=\"USD\">0.00</Money>\n" + "                  </InvoiceDetailDiscount>\n" + "              <NetAmount>\n" + "                  <Money currency=\"USD\">" + totalDollarAmt + "</Money>\n" + "              </NetAmount>\n" + "              <DepositAmount>\n" + "                  <Money currency=\"USD\">0.00</Money>\n" + "              </DepositAmount>\n" + "              <DueAmount>\n" + "                  <Money currency=\"USD\">" + totalDollarAmt + "</Money>\n" + "              </DueAmount>\n" + "          </InvoiceDetailSummary>\n" + "      </InvoiceDetailRequest>\n" + "  </Request>\n" + "</cXML>";
        ServletOutputStream sos;
        sos = response.getOutputStream();
        ByteArrayOutputStream baOutStream = new ByteArrayOutputStream();
        StringBufferInputStream inStream = new StringBufferInputStream(eInvoiceFile);
        convert(baOutStream, inStream);
        response.setContentLength(baOutStream.size());
        baOutStream.writeTo(sos);
        sos.flush();
    }
    return mapping.findForward(KFSConstants.MAPPING_BASIC);
}
Also used : PurchaseOrderService(org.kuali.kfs.module.purap.document.service.PurchaseOrderService) ServletOutputStream(javax.servlet.ServletOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PurchaseOrderItem(org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem) StringBufferInputStream(java.io.StringBufferInputStream) ElectronicInvoiceTestForm(org.kuali.kfs.module.purap.document.web.struts.ElectronicInvoiceTestForm) PurchaseOrderDocument(org.kuali.kfs.module.purap.document.PurchaseOrderDocument) KualiDecimal(org.kuali.rice.core.api.util.type.KualiDecimal) List(java.util.List) DateTimeService(org.kuali.rice.core.api.datetime.DateTimeService)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringBufferInputStream (java.io.StringBufferInputStream)1 List (java.util.List)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 PurchaseOrderItem (org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem)1 PurchaseOrderDocument (org.kuali.kfs.module.purap.document.PurchaseOrderDocument)1 PurchaseOrderService (org.kuali.kfs.module.purap.document.service.PurchaseOrderService)1 ElectronicInvoiceTestForm (org.kuali.kfs.module.purap.document.web.struts.ElectronicInvoiceTestForm)1 DateTimeService (org.kuali.rice.core.api.datetime.DateTimeService)1 KualiDecimal (org.kuali.rice.core.api.util.type.KualiDecimal)1