Set up an Invoice XML Generation Event Script

Use this activity to set up an invoice XML generation event script to customise the invoice XSD structure.

The XML generation event script is used to customise or extend the original XML document generated by MineMarket. If extra nodes are required, the XSD that the original XML document is based on must be modified to include the extra nodes. After the XSD has been edited, the Crystal Report RPT file data source must be refreshed in the design environment to display the new nodes.

The names of the default data items included in the invoice XML output can be determined from the InvoiceDataSet.xsd file.

Example: Standard Invoice XML Generation Script

Copy
using System;
using System.IO;
using System.Data;
using System.Xml;
using Mincom.MineMarket.DAL;
using Mincom.MineMarket;
using Mincom.MineMarket.Server;
using System.Collections;
using System.Drawing;
using System.Collections.Generic;
/// <summary>
/// Allows invoice report data to be extended and/or overridden.
/// </summary>
public class InvoiceDataXMLScriptEvent : IInvoiceDataXMLScript
{
    public InvoiceDataXMLScriptEvent()
    {
    }
    #region IInvoiceDataXMLScript Members
    // <summary>
    // Enables to update the Core generated XML document that represents the invoice.
    // </summary>
    public XmlDocument UpdateDataSet(Invoice inv, XmlDocument originalDocument, ArrayList parameters)
    {

        String extraError = String.Empty;

        #region Constant Declaration
        const string invoiceSignatureDocument = "INVOICE SIGNATURE";
        const string dateFormat = "dd MMM yyyy";

        #endregion
        // Example of adding new nodes. Nodes must be added to the Xsd.
        #region Extra data for Invoice Analytes
        XmlNodeList nodeListInvoiceAnalyte = originalDocument.GetElementsByTagName("InvoiceAnalytes");
        foreach (XmlNode nodeInvoiceAnalyte in nodeListInvoiceAnalyte)
        {
            XmlElement elemExtraDescription = originalDocument.CreateElement("ExtraDescription");
            nodeInvoiceAnalyte.AppendChild(elemExtraDescription);
            XmlElement elemExtraBasis = originalDocument.CreateElement("ExtraBasis");
            nodeInvoiceAnalyte.AppendChild(elemExtraBasis);
            XmlElement elemExtraValue = originalDocument.CreateElement("ExtraValue");
            nodeInvoiceAnalyte.AppendChild(elemExtraValue);
            XmlElement elemExtraOrder = originalDocument.CreateElement("ExtraOrder");
            nodeInvoiceAnalyte.AppendChild(elemExtraOrder);
            elemExtraOrder.InnerText = "0";
        }
        #endregion
        #region Add elements to Invoice Data
        XmlNodeList nodeListInvoiceData = originalDocument.GetElementsByTagName("InvoiceData");
        foreach (XmlNode nodeInvoiceData in nodeListInvoiceData)
        {
            #region Get the Contract
            XmlNode nodeContractName = nodeInvoiceData.SelectSingleNode("ContractName");
            string invoiceContractName = String.Empty;
            BaseContract invoiceContract;
            invoiceContractName = nodeContractName.InnerText;
            Criteria contractCriteria = new Criteria(typeof(BaseContract), "Name", QueryOperator.Equal, invoiceContractName);
            ArrayList result = BaseContract.GetFactory().GetByCriteria(contractCriteria);
            invoiceContract = (BaseContract)result[0];
            #endregion
            #region Get Seller and Buyer
            Organisation buyer = null;
            Organisation seller = null;
            if (invoiceContract.GetType().Name.ToUpper() == "PURCHASECONTRACT")
            {
                seller = invoiceContract.Customer;
                buyer = invoiceContract.Company;
            }
            else
            {
                seller = invoiceContract.Company;
                buyer = invoiceContract.Customer;
            }
            #endregion
            #region Get the seller's signature
            MemoryStream streamSignature = new MemoryStream();
            String signatureFile = String.Empty;
            foreach (DocumentLink documentLink in seller.Documents)
            {
                if (documentLink.Name.ToUpper() == invoiceSignatureDocument)
                {
                    signatureFile = documentLink.DocumentLinkText.Text;
                }
            }
            #endregion
            #region Get the seller's logo
            MemoryStream streamLogo = new MemoryStream();
            if (seller.Logo != null)
            {
                seller.Logo.LargeImage.Save(streamLogo, System.Drawing.Imaging.ImageFormat.Bmp);
            }
            else
            {
                // Add error handling if required
            }
            #endregion

            #region Add elements to Despatch Order
            XmlNodeList nodeListDespatchOrder = nodeInvoiceData.SelectNodes("DespatchOrders");
            foreach (XmlNode nodeDespatchOrder in nodeListDespatchOrder)
            {
                #region Get the Despatch Order object
                int invoiceDespatchOrderID = 0;
                DespatchOrder invoiceDespatchOrder;
                invoiceDespatchOrderID = int.Parse(nodeDespatchOrder.SelectSingleNode("DespatchOrderID").InnerText);
                Criteria despatchOrderCriteria = new Criteria(typeof(DespatchOrder), "ID", QueryOperator.Equal, invoiceDespatchOrderID);
                result = DespatchOrder.GetFactory().GetByCriteria(despatchOrderCriteria);
                invoiceDespatchOrder = (DespatchOrder)result[0];
                ContractTerm contractTerm = invoiceDespatchOrder.GetContractTerm();
                #endregion
                // Override despatch order bill of lading date format to match rest of report.
                DateTime billOfLading;
                if (!string.IsNullOrEmpty(nodeDespatchOrder["BillOfLading"].InnerText) 
                    && DateTime.TryParse(nodeDespatchOrder["BillOfLading"].InnerText, out billOfLading))
                    nodeDespatchOrder["BillOfLading"].InnerText = billOfLading.ToString(dateFormat).ToUpper();
                #region Get Invoice Type
                String invoiceType = String.Empty;
                if (buyer.OrganisationCategory != null)
                {
                    if (buyer.OrganisationCategory.Name.ToUpper() == "DOMESTIC")
                    {
                        invoiceType = "DOMESTIC";
                    }
                    else
                    {
                        if (buyer.OrganisationCategory.Name.ToUpper() == "EXPORT")
                        {
                            invoiceType = "COMMERCIAL";
                        }
                        else
                        {
                            // Error handling if required
                        }
                    }
                }
                else
                {
                    invoiceType = "JOINT VENTURE";
                }
                #endregion
                #region Get the Payment Term
                String paymentTerm = String.Empty;
                String paymentTermComment = String.Empty;
                if (contractTerm != null)
                {
                    foreach (PaymentTerm paymentTermItem in contractTerm.PaymentTerms)
                    {
                        if (inv.GetInvoiceType().Name.ToUpper() == paymentTermItem.InvoiceType.Name.ToUpper())
                        {
                            paymentTerm = paymentTermItem.InvoiceType.Name.ToUpper();
                            if (paymentTermItem.Comments != null && paymentTermItem.Comments != String.Empty)
                            {
                                paymentTermComment = paymentTermItem.Comments;
                            }
                            break;
                        }
                    }
                }
                #endregion
                #region Get the Invoice Number
                String invoiceNumber = String.Empty;
                invoiceNumber = inv.Name;
                #endregion
                #region Get the Invoice Quantity
                String invoiceQuantity = String.Empty;
                String invoiceQuantityUOM = String.Empty;
                XmlNode quantityDecimals = nodeDespatchOrder.SelectSingleNode("QuantityDecimals");
                string noOfQuantityDecimals = string.Empty;
                int noOfDecimals = 0;

                if (contractTerm != null && quantityDecimals != null && !string.IsNullOrEmpty(quantityDecimals.InnerText) && int.TryParse(quantityDecimals.InnerText, out noOfDecimals))
                {
                    if (noOfDecimals == 0 && !contractTerm.QuantityDecimals.IsNull)
                        noOfDecimals = contractTerm.QuantityDecimals.Value;
                    for (int i = 0; i < noOfDecimals; i++)
                        noOfQuantityDecimals += "0";
                }
                if (noOfQuantityDecimals == "0") noOfQuantityDecimals = "00";
                invoiceQuantity = Convert.ToDouble(nodeDespatchOrder.SelectSingleNode("FinalWetMass").InnerText).ToString(string.Format("#,##0.{0}", noOfQuantityDecimals));
                invoiceQuantityUOM = invoiceDespatchOrder.QuantityUOM.Symbol;
                #endregion
                #region Getting the log data for line items
                XmlNodeList nodeListLineItems = nodeDespatchOrder.SelectNodes("LineItems");

                foreach (XmlNode nodeLineItem in nodeListLineItems)
                {
                    XmlNode nodeContractChargeType = nodeLineItem.SelectSingleNode("ContractChargeType");
                    if (nodeContractChargeType.InnerText != null && (nodeContractChargeType.InnerText.ToUpper().IndexOf("PENALTY") >= 0 || nodeContractChargeType.InnerText.ToUpper().IndexOf("BONUS") >= 0 || nodeContractChargeType.InnerText.ToUpper().IndexOf("MASS ADJUSTMENT") >= 0))
                    {
                        #region Get Log Data
                        XmlNode lineItemID = nodeLineItem.SelectSingleNode("LineItemID");
                        Criteria lineItemIDCriteria = new Criteria(typeof(InvoiceItem), "ID", QueryOperator.Equal, int.Parse(lineItemID.InnerText));
                        result = InvoiceItem.GetFactory().GetByCriteria(lineItemIDCriteria);
                        InvoiceItem invoiceItem = null;
                        if (result.Count > 0)
                        {
                            invoiceItem = (InvoiceItem)result[0];
                        }
                        XmlDocument logData = new XmlDocument();
                        logData.LoadXml((invoiceItem == null ? "<Header></Header>" : (invoiceItem.EvaluationData == null ? "<Header></Header>" : (invoiceItem.EvaluationData == String.Empty ? "<Header></Header>" : invoiceItem.EvaluationData))));
                        #endregion
                    }
                }
                #endregion
            }
            #endregion
        }
        #endregion
        #region Save document to file
        //originalDocument.Save(@"C:\TEMP\InvoiceData.xml");
        #endregion
        return originalDocument;
    }
    // <summary>
    // Will indicate to the system if the script must be used or not
    // </summary>
    public bool UseScript(Invoice inv)
    {
        return true;
    }
    #endregion
}

Security Note: You need the Allow script maintenance user group security right in the Scripting user group security rights group for this activity.

Activity Steps

  1. Open the Invoice Data XML Script (script editor).

    The Scripting Ribbon Tab displays.

  2. Edit the C# script.
  3. Set the UseScript method to set return true.
    Copy
    // <summary>
    // Will indicate to the system if the script must be used or not
    // </summary>
    public bool UseScript(Invoice inv)
    {
        return true;
    }
  4. Ensure that script references are selected:
    1. Note the DLLs that are referenced in the script. The DLLs are listed at the top of the script, after the word "using".
    2. Select the References tab.
    3. Select the required DLLs.
  5. Click the Build icon on the Scripting ribbon tab to compile the script.
  6. Click the Save icon on the Scripting ribbon tab.