Try ERPNext Buy Support Partners Foundation

Convert Currency Field to Time Field

Hi, I currently have three fields:

  1. remaining_funds
  2. hourly_rate
  3. remaining_time

Field 1 and 2 are currency fields. Now the logic behind this is I want to add remaining funds and the hourly rate. This should then divide remaining funds by the hourly rate and calculate the remaining time. The problem is if i try to write a server script with the following syntax:

doc.time_remaining = doc.funds_remaining / doc.hourly_rate

it gives me a parser error. Any help is appreciated.

Also, I tried working around this with the following mindset. I changed all three Fields to float values. Then implemented a while loop to convert remaining funds to remaining hours.

The syntax for which is:
// remaining funds = a
// hourly rate = b
the rest of the variables below I also created as fields and made as hidden and read only.

doc.c = doc.a / doc.b;
doc.i = 0.0;

while(doc.i < doc.c){
doc.i = doc.i + 1.0;
}

doc.j = doc.c - doc.i;
doc.k = doc.j * 60;

print doc.i; // for hours
print doc.k // for minutes

However when I use a while loop or attempt to manipulate the fields (float type) as normal python variables, it gives me a syntax error. Am i missing something here? Also I am writing all this as a server script

to get to the issue, need the following:

  1. share your full script in a code block.
  2. post a screenshot of your doctype and field details.
  3. post a screenshot of the syntax error you get.

also a question: why did you do it in the server-side instead of the client-side if you are not getting anything from the server-side functions?

Hi there,

First a general request: if you want help with something, don’t just say you’re getting a syntax error. Tell us exactly what syntax error you’re getting. Otherwise, we have to do a lot of guessing.

There are several different problems with this code. For starters, // isn’t a comment marker in Python. It’s an operator for floor division (division with rounding down). That operator, though incorrectly used here, would actually be extremely helpful for what you’re trying to do. It would let you, for example, avoid the completely unnecessary while loop.

My advice would be to test all this out in the console before trying to run it as a server script. The console gives immediate feedback with no debugging needed, which makes learning python a lot easier. Good luck!