Calculations within data tables

Good day,

I am creating an invoicing system and as you can imagine there will be a couple calculations which would need to be done, namely;

  • Cost of items (cost of one unit + number of units), Cost of labour (cost per hour + number of hours).
  • Total (Cost of Items), Total (Cost of Labour).
  • VAT added ((Total items + Total Labour) x 15%).
  • Grand Total (Total items + Total Labour + VAT)

I thought the best way to break it up would be to have an “Invoice” database with a relation to a “Items” & “Labour” databases. In the “Items” & “Labour” data base there is a column for “Cost per unit” (DataType: Money), a column for “Number of Units” (DataType: Int), also a column called “TotalCost” (DataType: Money).

  1. How do I get the “Cost per unit” + “Number of Units” to automaticly calculate in “TotalCost” column?

In my Front end I would like to make it as user friendly as posible, so I have two input areas where the User can add “Items” & “Labour” sepretly and it be shown in two seperate data tables.

  1. How can I add the “Total Cost” of all the rows in the table, and show the Total amount in a text field below the table?

  2. I would then like to add the two Totals of the “Items” & “Labour” together and show in a text field, I would imagine this would be the same logic as Point 2?

  3. I would like to then save the total of Point 3 to the “Invoice” database, which i will then use to calculate VAT and the Grand Total (Total + VAT), would this be the best way to calcualte this or is there a simplre way?

  4. How can you set a currency in the money in the DataType: Money?

Kind Regards
Donovan

Good Day,

So I figured out Point 1 using Functions and Examples of Generated Columns | Backendless documentation.

But now I am struggling with Point 2, I tried the same solution as Point 1 but it does not work as it does not recognise the child column.

Kind Regards
Donovan

Hello @Donovan_Hardwick

I am glad that you have found the solution for Point 1.

For Point 2 you can use the aggregation function count COUNT - Backendless REST API Documentation but it will be the other call to db.

For Point 4 will you calculate it by scheduler like once a day, if yes then you can use backendless timer What is a Codeless Timer - Backendless Codeless Development Guide

For point 5 you can not set up currency for type Money, but you can create columns with the name of the currency, for example, invoiceUSD or invoiceEUR

@sergey.kuk Thank you for your reply.

I am still struggling with point 2, I am using codeless logic and still cannot find a way to add items in a dynamic row to show a total. The user will add units and cost of units in input field, this will automaticly give a total (Units + Cost of units), the user will then click on an “Add” button (where the logic is stored) the Units, Cost of Units and Total will all be added to a table (Dynamic list in frontend).

When the user clicks on the “add” button I would like for the Grand total to be worked out by adding each row total. Is there codeless logic which can do this?

Kind Regards

I think yes. You could get Dynamic List, iterate over them, and calculate the sum of any property you want. If you want to get help with that, you could create a page with a minimum of logic, and we will try to help you with that issue(don’t forget to provide container and page name, and app id)

Regards, Dima.

@Dima @sergey.kuk

I would really appreciate the assistance.

    1. App ID: FAB3842F-AA5A-3932-FF0C-652D56409B00
    1. Container: default
    1. Page Name: testPage

So I have added two input fields which contain logic, they add up and equal to “RowTotal”. The two buttons add and delete items in the dynamic list “RowAddedUnits”. I have not added any other logic. I have added the calculations below which I am struggling with.

  1. “TotalOfColumn” = Sum of all “RowTotal”
  2. “GrandTotal” = “TotalOfColumn” (In my application I will have two “TotalOfColumn” added up to equal “GrandTotal”).
  3. “VAT(15%)” = “GrandTotal” + 15%
  4. “GrandTotalWithVAT” = “GrandTotal” + “VAT(15%)”

Kind Regards
Donovan

Already add logic for Total Of Column. You could check it.

@Dima

Thank you it works, the only problem is if a row is deleted then the “TotalOfColumn” does not decrease.

i have also tries working out the VAT and Grand Total but keep getting a return of NaN.

Kind Regards
Donovan

Thank you it works, the only problem is if a row is deleted then the “TotalOfColumn” does not decrease.

Check if dynamic list was reassigned.

i have also tries working out the VAT and Grand Total but keep getting a return of NaN.

NaN(Not a Number) usually appears when you try to make math operations with another type of data than Number.

Hello! I’m struggling with the similar problem.
I have a dynamic list of items. I want to calculate the sum of one property of all items in the list. For this I made value logic as at picture below. But whatever I do it always shows zero. Could you please help?

Hi Dmitriy,

Try the following:

  1. add a print block inside of the for loop to print out the value of j
  2. add a print block inside of the for loop to print out the value of the cuts property of j
  3. Instead of the change block, use the set block to set the value of the sum variable.

Mark

Thank you, Mark!

The set block worked, but I got some strange behaviour.
In my UI the result of sum never changes once I add, delete or change objects from my dynamic list.

I made very strange thing to achieve my goal to see the value once I manipulate with dynamic list.

To every component of my dynamic list I added On Change Event which just put something to the page data. And now if I change something inside my list, I right away see how the sum is changing.

But it seems that this is not how it should be done:)
Can you correct me please?

This is what I did for every component in the dynamic list:

This is my final logic for summarizing my list:

Just in case this is my UI:

Could we clarify, the end result of actions that we did?

We should have a list of inputs, and the sum of all inputs below them?

Everything you do is correct, except one under-hood detail. And I couldn’t explain that in a simple way.

On Change event triggers changing the Dynamic list, but Content Logic is not directly related for this Dynamic List, and recalculating(in my attempt to investigate this problem) not triggered while we do not change App / Page Data. I think that is normal behavior if you understand JS in deep, but for no-code developers that might be a little strange and maybe hard. So we should discuss that with a team if we can simplify or rework that behavior.

For you, I’ve could recommend trigger changing App / Page Data manually, just by adding a logic like below:

image

(only row with Date Now)

Please, try that and tell me if it helps you. If you still have troubles with that, create please an empty page with example, and provide app id / container name / page name. And I would help you directly in your app with this example.

Regards, Dima.

Yeah, as I said everything works fine when I trigger changing Page Data on any change events related to my list.

I just thought I’m doing something wrong, but appeared that I did everything right:)

And you are right, it’s not easy to understand for no coder like me. But I’m fine as long my app is working correct.

Your platform is great tho, for people with my experience it’s a masterpiece.