Overriding calculation-handler

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robin
    Junior Member
    • Aug 2025
    • 4

    #1

    Overriding calculation-handler

    Hello,

    I am trying to write my own calculation logic for the items in an invoice. I've found this official Espo documentation: https://docs.espocrm.com/development...-calculations/
    which I've followed. The implementation of the serverside calculation according to the documentation works fine. However without the client side calculation, the user would have to save the edited items everytime to see an update in the amount.

    A bit of background:
    for an invoice there are two different types of items: a normal item and a maintenance-item. for an invoice item ive created a custom field called "isMaintenance". if this custom boolean field is ticket, the item does not count towards the total amount, but towards the "shipping" amount (since we dont need the shipping field, I've repurposed it to hold the total amount of all the maintenance items.)
    the grand total amount then is the total amount + maintenance (shipping)

    now everytime I change a price/quantity of an item, the "normal" amount gets recalculated on client side. however the shipping/maintenance amount does not get calculated, probably because it was not meant to be based off of items.
    Thats what i was trying to fix with a custom calculaion-handler. I've wrote a handler, referenced it in the ...clientDef/Invoice.json file but only get the following error in my browser console:
    "Uncaught (in promise) Error: Could not fetch asset 'client/custom/modules/sales/lib/transpiled/src/invoice-calculation-handler.js?r=1756810231'."


    It seems that the calculation-handler does not get transpiled. However I already wrote 2 dynamic-custom-handler which are in the same directory and they are working fine, so I assume the transpilation for them must've worked.

    Any help towards that problem would be appreciated
  • robin
    Junior Member
    • Aug 2025
    • 4

    #2
    So Ive managed to find a solution. As far as I can tell, the problem was in my calculation-handler code. I've copied this from the mentioned documentation page
    Code:
    define(['sales:quote-calculation-handler'], (Dep) => {
    
    return class extends Dep {
    
    // Define custom calculations here.
    // Use client/modules/sales/quote-calculation-handler.js as an example.
    
    /**
    * Calculate.
    *
    * @param {import('model').default} model An order.
    */
    calculate(model) {
    super.calculate(model);
    }
    
    /**
    * Calculate item.
    *
    * @param {import('model').default} model An item.
    * @param {string} [field] A field that was changed.
    */
    calculateItem(model, field) {
    super.calculateItem(model, field);
    }
    
    /**
    * Select product.
    *
    * @param {import('model').default} model
    * @param {import('model').default} product
    */
    selectProduct(model, product) {
    super.selectProduct(model, product);
    }
    
    /**
    * Get select product attributes.
    *
    * @param {import('model').default} model
    * @return {string[]}
    */
    getSelectProductAttributeList(model) {
    super.getSelectProductAttributeList(model);
    }
    }
    });​
    Since I wanted to adjust the invoice-calculation, ive replaced "define(['sales:quote-calculation-handler']" with "define(['sales:invoice-calculation-handler']" thinking if there is a quote-calculation-handler, surely there is a invoice-calculation-handler as well which I can extend, since Invoices obviously also have the whole calculation logic.
    However that lead to the error I mentioned in my previous post.
    If i instead extended the quote-calculation-handler and then use that for the invoice calculation, everything works fine.
    I suppose it does make sense to only have one calculation-handler for invoice and quote, since the logic behind it is the same.

    If someone from the EspoCRM Team sees this, maybe it would be worth extending the documentation page (https://docs.espocrm.com/development...-calculations/) with this.
    (If the docdumentation gets updated, please also consider that the commeent in the client-side code example says "// Use client/modules/sales/quote-calculation-handler.js as an example.​" However this file does not seem to exist anymore. Might be worth updating this reference.)

    Comment

    Working...