Flexible runtime calculation of net / tax / gross

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1806
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Flexible runtime calculation of net / tax / gross

Post by jsetzer » 2018-08-26 11:36

Hi there,

for a small project I needed some net/gross price calculation. Due to customer's requirements, especially due to rounding issues, there are two different calculation methods:

Method A: Net to Gross

gross = net + taxes

...in other words...

gross = net * ( 1 + taxrate )


Method B: Gross to net

net = gross - taxes

...in other words ...

net = gross / ( 1 + taxrate )


Example

For example users enter net price first, see the calculated gross price and then adjust the gross price by rounding up or so. My aim was to find a simple and user friendly solution so that the details view just does the calculation without being too complex. And this is the result:
sample_2.gif
sample_2.gif (37.54 KiB) Viewed 1959 times


----

There are four relevant fields im my prices table.:
AppGini_2018-08-26_12-24-50.png
AppGini_2018-08-26_12-24-50.png (4.86 KiB) Viewed 1959 times
  • netPrice
  • grossPrice
  • taxrate_id (Lookup)
  • taxrate_rate (Auto-fill from taxrates table)

Method A

When user starts input in netPrice field, the details view switches to calculation method (A) and grossPrice field will be disabled. After user has finished input, the grossPrice is being calculated using Method (A) and enabled afterwards.
net-to-gross.gif
net-to-gross.gif (19.51 KiB) Viewed 1959 times
To indicate the calculation method, I'm using bootstrap's 'has-success' class which renders a nice green border around an input.


---


Method B

When user starts input in grossPrice field, the details view switches to calculation method (B) and netPrice field will be disabled. After user has finished input, the netPrice is being calculated using Method (B) and enabled afterwards.
gross-to-net.gif
gross-to-net.gif (16.21 KiB) Viewed 1959 times




As a result, users don't have to care. They can just enter the price in net field OR gross field. The other price will be calculated automatically, even if they change the tax rate:
taxrate.gif
taxrate.gif (55.36 KiB) Viewed 1959 times



All that is written in Javascript + JQuery in hooks\cm_prices-dv.js. Detecting input and changes can be done like this:

Code: Select all

$j('#netPrice').on('input', function () {
    calcMethod('net2gross');
});

$j('#grossPrice').on('input', function () {
    calcMethod('gross2net');
});

$j('#netPrice,#grossPrice').on('change', function () {
    recalc();
});

Calculation method looks something like this:

Code: Select all

function recalc() {

    // get rate from autofill field taxrate_rate
    var rate = getRate(); 

    var gross2net = (_calcMethod == 'gross2net');

    if (gross2net) {
        var g = gross(); // get gross from grossPrice field
        var n = (g / (1 + rate)).toFixed(2); // calc net
        net(n); // put result in netPrice field
    } else {
        var n = net(); // get net from netPrice field
        var g = (n * (1 + rate)).toFixed(2); // calc gross
        gross(g); // put result in grossPrice field
    }

    // enable input fields
    // ...
}

Hope you like it and my idea and code gives someone a good starting point.

Regards,
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
a.gneady
Site Admin
Posts: 1280
Joined: 2012-09-27 14:46
Contact:

Re: Flexible runtime calculation of net / tax / gross

Post by a.gneady » 2018-08-28 14:15

Awesome! Thanks for sharing :)
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

Post Reply