Hi,

I’ve tried to duplicate “Gross Profit” report to calculate the percentage by costing [Markup]. I just edited the code to these but loading the report is giving me this error. Selecting few invoice is showing the report perfectly, but most of the invoice and other filters are showing this error.

```
# calculate gross profit
row.gross_profit = flt(row.base_amount - row.buying_amount, self.currency_precision)
if row.base_amount:
row.gross_profit_percent = flt((row.gross_profit / row.**[changes base_amount to]**buying_amount) * 100.0, self.currency_precision)
else:
row.gross_profit_percent = 0.0
**In the "def get_average_rate_based_on_group_by(self):" section**
def set_average_rate(self, new_row):
new_row.gross_profit = flt(new_row.base_amount - new_row.buying_amount, self.currency_precision)
new_row.gross_profit_percent = flt(((new_row.gross_profit / new_row.**[changes base_amount to]**buying_amount) * 100.0), self.currency_precision)
```

I know I need to make other changes in the report code. Can anyone help me with it?

Thanks in advance.

Hi,

I suppose the division field ’ new_row.**[changes base_amount to]**buying_amount’ somewhere must be returning zero value that’s why the error. Before the division operator, you should check for zero value in the field.

1 Like

Oh my god! Yes! You’re right. Some of the items are purchased at zero value, making the valuation/buying rate zero which will obviously mess up the calculation! Thank You!

Now, if I want to avoid this error, I guess I can have a code to calculate the percentage based on sale price when the buying price is zero; thus instead returning error it will calculate the next calculation. So I tried this code, still getting this error! I’m sure I need to have some other condition here.

# calculate gross profit

```
row.gross_profit = flt(row.base_amount - row.buying_amount, self.currency_precision)
if row.buying_amount !=0:
row.gross_profit_percent = flt((row.gross_profit / row.base_amount) * 100.0, self.currency_precision)
else if:
row.gross_profit_percent = flt((row.gross_profit / row.buying_amount) * 100.0, self.currency_precision)
else:
row.gross_profit_percent = 0.0
```

I need to have some condition after first one that would follow the the second condition. I’ve tried that, getting syntax error.

The syntax of else if is wrong it should be elif:

Also, if condition should contain the division field for checking non-zero

```
row.gross_profit = flt(row.base_amount - row.buying_amount, self.currency_precision)
if row.base_amount_amount !=0:
row.gross_profit_percent = flt((row.gross_profit / row.base_amount) * 100.0, self.currency_precision)
elif row.buying_amount != 0:
row.gross_profit_percent = flt((row.gross_profit / row.buying_amount) * 100.0, self.currency_precision)
else:
row.gross_profit_percent = 0.0
```

1 Like

I guess, I will need to rearrange the code as I the calculation by buying rate first as I want the calculation by costing first. I tried this but getting the error!

```
# calculate gross profit
row.gross_profit = flt(row.base_amount - row.buying_amount, self.currency_precision)
if row.buying_amount_amount !=0:
row.gross_profit_percent = flt((row.gross_profit / row.buying_amount) * 100.0, self.currency_precision)
elif row.base_amount != 0:
row.gross_profit_percent = flt((row.gross_profit / row.base_amount) * 100.0, self.currency_precision)
else:
row.gross_profit_percent = 0.0
```

I’m sure I need to make changes here too. But I’m not sure what changes I need to make!

```
def set_average_rate(self, new_row):
new_row.gross_profit = flt(new_row.base_amount - new_row.buying_amount, self.currency_precision)
new_row.gross_profit_percent = flt(((new_row.gross_profit / new_row.buying_amount) * 100.0), self.currency_precision) \
```