2022-08-25 20:35:08 +00:00
|
|
|
import click
|
2021-09-06 11:24:28 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.utils import flt
|
|
|
|
|
|
|
|
import erpnext
|
|
|
|
from erpnext.setup.utils import get_exchange_rate
|
|
|
|
|
|
|
|
|
|
|
|
def execute():
|
2022-03-28 12:23:54 +00:00
|
|
|
frappe.reload_doctype("Opportunity")
|
2021-09-06 11:24:28 +00:00
|
|
|
opportunities = frappe.db.get_list(
|
|
|
|
"Opportunity",
|
|
|
|
filters={"opportunity_amount": [">", 0]},
|
|
|
|
fields=["name", "company", "currency", "opportunity_amount"],
|
|
|
|
)
|
|
|
|
|
|
|
|
for opportunity in opportunities:
|
|
|
|
company_currency = erpnext.get_company_currency(opportunity.company)
|
|
|
|
|
2022-08-25 20:35:08 +00:00
|
|
|
if opportunity.currency is None or opportunity.currency == "":
|
2022-08-25 18:45:35 +00:00
|
|
|
opportunity.currency = company_currency
|
|
|
|
frappe.db.set_value(
|
|
|
|
"Opportunity",
|
|
|
|
opportunity.name,
|
|
|
|
{"currency": opportunity.currency},
|
|
|
|
update_modified=False,
|
|
|
|
)
|
|
|
|
click.secho(
|
2022-08-25 20:35:44 +00:00
|
|
|
f' Opportunity `{opportunity.name}` has no currency set. We force it to company currency : `{opportunity.currency}`"\n',
|
2022-08-25 18:45:35 +00:00
|
|
|
fg="yellow",
|
|
|
|
)
|
|
|
|
|
2021-09-06 11:24:28 +00:00
|
|
|
# base total and total will be 0 only since item table did not have amount field earlier
|
|
|
|
if opportunity.currency != company_currency:
|
|
|
|
conversion_rate = get_exchange_rate(opportunity.currency, company_currency)
|
|
|
|
base_opportunity_amount = flt(conversion_rate) * flt(opportunity.opportunity_amount)
|
|
|
|
else:
|
|
|
|
conversion_rate = 1
|
2022-02-13 13:54:10 +00:00
|
|
|
base_opportunity_amount = flt(opportunity.opportunity_amount)
|
2021-09-06 11:24:28 +00:00
|
|
|
|
|
|
|
frappe.db.set_value(
|
|
|
|
"Opportunity",
|
|
|
|
opportunity.name,
|
2022-02-13 13:54:10 +00:00
|
|
|
{"conversion_rate": conversion_rate, "base_opportunity_amount": base_opportunity_amount},
|
2021-09-06 11:24:28 +00:00
|
|
|
update_modified=False,
|
|
|
|
)
|