Maximo

 View Only
  • 1.  Creating a New Invoice Line via Automation Script

    Posted Mon July 25, 2022 11:20 AM
    Any clue how to create a new Invoice Line for an invoice via automation script. Trying to create a new invoice line for a newly created invoice that takes in necessary attributes from a matrectrans object.

    ------------------------------
    Brad Wright
    ------------------------------



    #Maximo
    #AssetandFacilitiesManagement
    #MaximoIntegrationandScripting


  • 2.  RE: Creating a New Invoice Line via Automation Script

    Posted Thu September 15, 2022 10:14 AM
    Hi Brad,

    Can you please post your script code and launch point here?

    ------------------------------
    Subhransu Sekhar Sahoo
    ------------------------------



  • 3.  RE: Creating a New Invoice Line via Automation Script

    IBM Champion
    Posted Fri September 16, 2022 02:36 AM
    Based on your question, I am not sure what is your trigger point to create invoice lines from Invoice. 
    If a user is creating Invoices for a PO then creating invoicelines is few more clicks aways. I need to understand how you are planning to call this automation script.
    Anyways, Lets assume that you have a invoice with PONUM poulated and you need to copy matrectrans lines into invoice lines ( maximo does this using copy lines form po in the invoice lines).
    1. Create an Action script which you can use in Escalation or Applicaiton action/button
    2. Script will call the following method
    getAllUninvoicedMatReceipts(java.lang.String poNum, java.lang.String poLineNum, java.lang.String siteID, boolean isNegativeLine, java.lang.String receiptID)

    This is a methos in the invoice mbo accessible from automation script mbo variable. As suggested in the method you need to pass the required parameters.
    3. Get the invoice line mboset from invoice mbo
    4. write a loop on the vector set return by the method in step2. This loop will add new invoice lines and populated the new invoice line with detailed from the matrectrans vector mbo.
    5. finally save the invoice mbo using mbo.getThisMboSet().save().

    ------------------------------
    Biplab Choudhury
    IBM Champion 2022
    Senior Consultant
    BPD Zenith
    Melbourne
    ------------------------------



  • 4.  RE: Creating a New Invoice Line via Automation Script

    Posted Mon September 19, 2022 09:50 AM
    Thanks Biplab for your reply. Brad was a summer intern we had working for us on this project. He has returned to school so I am going to be taking over the project. We are trying to take some of the load off of our AP department by allowing an invoice to be created on Blanket Releases when they are received. The script will be launched from a button on the receipt screen. Brad had got to the point that he could create an invoice by running the script but we didn't know how to create the invoice lines.


    Three fields will be added to the "Receiving" screen to capture the following:
    o Vendor Invoice #
    o Total Invoice Amount
    o Invoice Date
    • All three fields must be populated before clicking the "Create Invoice" button
    • The Receipt lines must be associated with a PO of type "Release"; otherwise, stop the script and notify the user
    • There will be a checkbox on each receipt line that will include the line on the invoice if it is checked.
    • If the Invoice amount is greater than a variance (> 0 and < $500), notify the user to contact the AP Dept with the release number. Then automatically generate an invoice with a status of "WAVAR", which is a synonym of WAPPR.
    • If the invoice amount is greater than or equal to a variance ($500), halt the invoice creation process and notify the user that they should contact the AP Dept.
    • If no variance exists, create the invoice with a status of "WAUTO", which is also a synonym of WAPPR.

    I have inserted the script below that he was working on when he left.

    Thanks for any guidance you can give us on this.

    Thanks again,
    Randy


    load("nashorn:mozilla_compat.js");
    MboConstants = Java.type("psdi.mbo.MboConstants");
    importClass(Packages.psdi.server.MXServer); // used to get date from web server

    main();

    function main() {
    // Make sure there is a Mbo associated with the launch point. This is largely unnecessary but is defensive.
    if (mbo.getThisMboSet().count() > 0) {
    poLineSet = mbo.getThisMboSet();
    poToInvoice(poLineSet);

    }
    }

    function poToInvoice(newPOLineSet) {
    var newInvoice = createNewInvoice(newPOLineSet);

    // createInvoiceForReceipt(MboRemote receipt)
    // where POLINE's attribute "INVOICELINE" is true ("Invoice Line?" is checked) and PONUM equals original POLINE PONUM
    // poLinesToInvoice contains all POLINEs that have the above values
    // var poLinesToInvoice = mbo.getMboSet('$POLINE','POLINE',"invoiceline = 1 and ponum = '" + newPOLineSet.getString("PONUM") + "'");
    var poLinesToInvoice = mbo.getMboSet('$POLINE','POLINE',"ponum = '150654'");

    poLinesToInvoice = poLinesToInvoice.getMbo(0);

    // using these conditional statements for testing one poline (lines 28-36)
    if(poLinesToInvoice.getString("LINETYPE") === "MATERIAL") {
    // use matrectrans to retrieve data for invoice line
    newInvoice = fillOutInvoiceLineFromMatRec(poLinesToInvoice, newInvoice);
    }

    if(poLinesToInvoice.getString("LINETYPE") === "SERVICE") {
    // use servrectrans to retrieve data for invoice line
    fillOutInvoiceLineFromServRec(poLinesToInvoice, newInvoice);
    }



    // below code is a for loop that goes through each po line that "Invoice Line?" is checked in PO (should be the correct structure to use)
    /*
    for(var i = 0; i < poLinesToInvoice.count(); i++) {
    var poLineSelected = poLinesToInvoice.getMbo(i);

    // add a condition to look for different vendor invoice number of each line. if different create new invoice

    // error handling through code
    if(// check if recepits are complete (RECEIPTSCOMPLETE is a YORN, yes or no, type) //)
    {

    }

    if(poLinesToInvoice.getString("LINETYPE") === "MATERIAL")
    {
    // use matrectrans to retrieve data for invoice line
    }

    if(poLinesToInvoice.getString("LINETYPE") === "SERVICE")
    {
    // use servrectrans to retrieve data for invoice line
    }

    }
    */

    // set status to whatever is decided
    newInvoice.changeStatus("WAPPR", MXServer.getMXServer().getDate(), "Brad WAPPR")
    newInvoice.save();

    // send invoice to AP
    }

    // creates and returns new Invoice object
    function createNewInvoice(newPOLineSet) {
    var currentDate = new Date().toLocaleDateString();
    // PO associated with PO Line
    var poLinePO = mbo.getMboSet("$po", "PO", "PONUM = '" + newPOLineSet.getString("PONUM") + "'");
    poLinePO = poLinePO.getMbo(0);

    // Creates new Invoice
    invoiceSet = mbo.getMboSet("$ADDINVOICE", "INVOICE", "1=2");
    invoiceMbo = invoiceSet.add();

    // Set value of "Company" field
    // invoiceMbo.setValue("VENDOR", poLinePO.getString("VENDOR")); // set vendor value for invoice to the same vendor as the PO
    invoiceMbo.setValue("VENDOR", "1777");
    invoiceMbo.setValue("VENDORINVOICENUM", "872A-22"); // either grabbed externally or taken from new required attribute
    invoiceMbo.setValue("INVOICEDATE", currentDate); // either grabbed externally or taken from new required attribute
    // invoiceMbo.setValue("PONUM", "150654");

    // return new invoice object
    return invoiceMbo;
    }


    function fillOutInvoiceLineFromMatRec(newPOLineSet, newInvoice) {
    // Material Receipt for current PO Line (Using PO 150654 as a test)

    // get matrectrans object associated with the POLINE
    //var receiptForLine = mbo.getMboSet("$MATRECTRANS", "MATRECTRANS", "PONUM = '" + newPOLineSet.getString("PONUM") + "' and POLINENUM = '" + newPOLineSet.getString("POLINENUM") + "'");

    // manual example of above line
    var receiptForLine = mbo.getMboSet("$MATRECTRANS", "MATRECTRANS", "PONUM = '150654' and POLINENUM = '1'");
    newInvoice.copyReceiptToInvoiceLine(receiptForLine); // causes no error, but does not copy receipt to invoice line

    // new Invoice Line for selected Invoice (not sure if invoicelines works for creating new invoice line)
    // var invoiceLines = mbo.getMboSet("$INVOICELINE", "INVOICELINE", "INVOICENUM = '" + newInvoice.getString("INVOICENUM") + "'"); // causing null err

    // below line causes error
    // invoiceLines.createInvoiceLine(receiptForLine, receiptForLine.getDouble("QUANTITY"), receiptForLine.getDouble("LINECOST"));

    /* Below code will only work once you figure out how to create a new invoiceline object
    using the same method as creating a new invoice object will not work.

    invoiceLineMbo.setValue("COMMODITYGROUP", receiptForLine.getString("COMMODITY"));
    invoiceLineMbo.setValue("CONDITIONCODE", receiptForLine.getString("CONDITIONCODE"));
    invoiceLineMbo.setValue("CONVERSION", receiptForLine.getString("CONVERSION"));
    invoiceLineMbo.setValue("DESCRIPTION", receiptForLine.getString("DESCRIPTION"));
    invoiceLineMbo.setValue("INVOICEQTY", receiptForLine.getString("QUANTITY"));
    invoiceLineMbo.setValue("INVOICEUNIT", receiptForLine.getString("ISSUEUNIT"));
    invoiceLineMbo.setValue("ITEMNUM", receiptForLine.getString("ITEMNUM"));
    invoiceLineMbo.setValue("ITEMSETID", receiptForLine.getString("ITEMSETID"));
    invoiceLineMbo.setValue("LANGCODE", receiptForLine.getString("LANGCODE"));
    invoiceLineMbo.setValue("LINECOST", receiptForLine.getString("LINECOST"));
    invoiceLineMbo.setValue("LINECOST2", receiptForLine.getString("LINECOST2"));
    invoiceLineMbo.setValue("LINETYPE", receiptForLine.getString("LINETYPE"));
    invoiceLineMbo.setValue("ORGID", receiptForLine.getString("ORGID"));
    invoiceLineMbo.setValue("POLINENUM", receiptForLine.getString("POLINENUM"));
    invoiceLineMbo.setValue("PONUM", receiptForLine.getString("PONUM"));
    invoiceLineMbo.setValue("POREVISIONNUM", receiptForLine.getString("POREVISIONNUM"));
    invoiceLineMbo.setValue("SITEID", receiptForLine.getString("SITEID"));
    invoiceLineMbo.setValue("UNITCOST", receiptForLine.getString("UNITCOST"));
    */
    return newInvoice;
    }

    // copy and paste of above func, just replace MATRECTRANS with SERVRECTRANS and setValue for different fields
    // look at ReceiptInvoiceFields.xlsx (located on btwright Z: drive) to see what field values from recepit go to invoiceline
    function fillOutInvoiceLineFromServRec(newPOLineSet,InvoiceSet) {
    // Service Receipt for current PO Line
    var receiptForLine = mbo.getMboSet("$SERVRECTRANS", "SERVRECTRANS", "PONUM = '" +
    newPOLineSet.getString("PONUM") +
    " and POLINENUM = " +
    newPOLineSet.getString("POLINENUM") + "'");

    // new Invoice Line for selected Invoice (not working)
    var invoiceLines = mbo.getMboSet("$INVOICELINE", "INVOICELINE", "INVOICENUM = " + InvoiceSet.getString("INVOICENUM") + "'");
    invoiceLineMbo = invoiceLines.add();
    // below code sets values of invoiceline to the values from SERVRECTRANS
    invoiceLineMbo.setValue("COMMODITY", receiptForLine.getString("COMMODITY"));
    invoiceLineMbo.setValue("COMMODITYGROUP", receiptForLine.getString("COMMODITYGROUP"));
    invoiceLineMbo.setValue("COSTOVERRECEIVED", receiptForLine.getString("COSTOVERRECEIVED"));
    invoiceLineMbo.setValue("DESCRIPTION", receiptForLine.getString("DESCRIPTION"));
    invoiceLineMbo.setValue("INVOICEQTY", receiptForLine.getString("QUANTITY"));
    invoiceLineMbo.setValue("ITEMNUM", receiptForLine.getString("ITEMNUM"));
    invoiceLineMbo.setValue("ITEMSETID", receiptForLine.getString("ITEMSETID"));
    invoiceLineMbo.setValue("LANGCODE", receiptForLine.getString("LANGCODE"));
    invoiceLineMbo.setValue("LINECOST", receiptForLine.getString("LINECOST"));
    invoiceLineMbo.setValue("LINECOST2", receiptForLine.getString("LINECOST2"));
    invoiceLineMbo.setValue("LINETYPE", receiptForLine.getString("LINETYPE"));
    invoiceLineMbo.setValue("LOADEDCOST", receiptForLine.getString("LOADEDCOST"));
    invoiceLineMbo.setValue("ORGID", receiptForLine.getString("ORGID"));
    invoiceLineMbo.setValue("POLINENUM", receiptForLine.getString("POLINENUM"));
    invoiceLineMbo.setValue("PONUM", receiptForLine.getString("PONUM"));
    invoiceLineMbo.setValue("POREVISIONNUM", receiptForLine.getString("POREVISIONNUM"));
    invoiceLineMbo.setValue("POSITEID", receiptForLine.getString("POSITEID"));
    invoiceLineMbo.setValue("PRORATECOST", receiptForLine.getString("PRORATECOST"));
    invoiceLineMbo.setValue("PRORATED", receiptForLine.getString("PRORATED"));
    invoiceLineMbo.setValue("QTYOVERRECEIVED", receiptForLine.getString("QTYOVERRECEIVED"));
    invoiceLineMbo.setValue("SITEID", receiptForLine.getString("SITEID"));
    invoiceLineMbo.setValue("TAX1", receiptForLine.getString("TAX1"));
    invoiceLineMbo.setValue("TAX1CODE", receiptForLine.getString("TAX1CODE"));
    invoiceLineMbo.setValue("TAX2", receiptForLine.getString("TAX2"));
    invoiceLineMbo.setValue("TAX2CODE", receiptForLine.getString("TAX2CODE"));
    invoiceLineMbo.setValue("TAX3", receiptForLine.getString("TAX3"));
    invoiceLineMbo.setValue("TAX3CODE", receiptForLine.getString("TAX3CODE"));
    invoiceLineMbo.setValue("TAX4", receiptForLine.getString("TAX4"));
    invoiceLineMbo.setValue("TAX4CODE", receiptForLine.getString("TAX4CODE"));
    invoiceLineMbo.setValue("TAX5", receiptForLine.getString("TAX5"));
    invoiceLineMbo.setValue("TAX5CODE", receiptForLine.getString("TAX5CODE"));
    invoiceLineMbo.setValue("UNITCOST", receiptForLine.getString("UNITCOST"));
    }

    // every script has this at bottom for the "Maximo Development Tools" extension.
    var scriptConfig={
    "autoscript": "INVOICESELECTED",
    "description": "Create invoice from selected BLANKET PO",
    "version": "",
    "active": true,
    "logLevel": "ERROR",
    "scriptLaunchPoints": [
    {
    "launchPointName": "INVOICESELECTED",
    "launchPointType": "OBJECT",
    "active": true,
    "description": "Create invoice from selected BLANKET PO",
    "objectName": "POLINE",
    "save": true,
    "add": true,
    "update": true,
    "delete": false,
    "beforeSave": true
    }
    ]
    };

    ------------------------------
    Randy Prosser
    ------------------------------