mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-13 10:11:20 +00:00
fix: Calculate gross margin on update of project costing from invoices (#43876)
* fix: Calculate gross margin on update of project costing from invoices * chore: linter issues
This commit is contained in:
@@ -93,14 +93,14 @@ class Project(Document):
|
||||
|
||||
def validate(self):
|
||||
if not self.is_new():
|
||||
self.copy_from_template()
|
||||
self.copy_from_template() # nosemgrep
|
||||
self.send_welcome_email()
|
||||
self.update_costing()
|
||||
self.update_percent_complete()
|
||||
self.validate_from_to_dates("expected_start_date", "expected_end_date")
|
||||
self.validate_from_to_dates("actual_start_date", "actual_end_date")
|
||||
|
||||
def copy_from_template(self):
|
||||
def copy_from_template(self): # nosemgrep
|
||||
"""
|
||||
Copy tasks from template
|
||||
"""
|
||||
@@ -205,7 +205,7 @@ class Project(Document):
|
||||
self.db_update()
|
||||
|
||||
def after_insert(self):
|
||||
self.copy_from_template()
|
||||
self.copy_from_template() # nosemgrep
|
||||
if self.sales_order:
|
||||
frappe.db.set_value("Sales Order", self.sales_order, "project", self.name)
|
||||
|
||||
@@ -324,9 +324,13 @@ class Project(Document):
|
||||
self.total_sales_amount = total_sales_amount and total_sales_amount[0][0] or 0
|
||||
|
||||
def update_billed_amount(self):
|
||||
# nosemgrep
|
||||
total_billed_amount = frappe.db.sql(
|
||||
"""select sum(base_net_total)
|
||||
from `tabSales Invoice` where project = %s and docstatus=1""",
|
||||
"""select sum(base_net_amount)
|
||||
from `tabSales Invoice Item` si_item, `tabSales Invoice` si
|
||||
where si_item.parent = si.name
|
||||
and if(si_item.project, si_item.project, si.project) = %s
|
||||
and si.docstatus=1""",
|
||||
self.name,
|
||||
)
|
||||
|
||||
@@ -676,31 +680,8 @@ def update_project_sales_billing():
|
||||
return
|
||||
|
||||
# Else simply fallback to Daily
|
||||
exists_query = "(SELECT 1 from `tab{doctype}` where docstatus = 1 and project = `tabProject`.name)"
|
||||
project_map = {}
|
||||
for project_details in frappe.db.sql(
|
||||
"""
|
||||
SELECT name, 1 as order_exists, null as invoice_exists from `tabProject` where
|
||||
exists {order_exists}
|
||||
union
|
||||
SELECT name, null as order_exists, 1 as invoice_exists from `tabProject` where
|
||||
exists {invoice_exists}
|
||||
""".format(
|
||||
order_exists=exists_query.format(doctype="Sales Order"),
|
||||
invoice_exists=exists_query.format(doctype="Sales Invoice"),
|
||||
),
|
||||
as_dict=True,
|
||||
):
|
||||
project = project_map.setdefault(
|
||||
project_details.name, frappe.get_doc("Project", project_details.name)
|
||||
)
|
||||
if project_details.order_exists:
|
||||
project.update_sales_amount()
|
||||
if project_details.invoice_exists:
|
||||
project.update_billed_amount()
|
||||
|
||||
for project in project_map.values():
|
||||
project.save()
|
||||
for project in frappe.get_all("Project", filters={"status": ["!=", "Cancelled"]}):
|
||||
frappe.get_doc("Project", project.name).save()
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
@@ -751,7 +732,6 @@ def get_users_email(doc):
|
||||
def calculate_total_purchase_cost(project: str | None = None):
|
||||
if project:
|
||||
pitem = qb.DocType("Purchase Invoice Item")
|
||||
frappe.qb.DocType("Purchase Invoice Item")
|
||||
total_purchase_cost = (
|
||||
qb.from_(pitem)
|
||||
.select(Sum(pitem.base_net_amount))
|
||||
|
||||
Reference in New Issue
Block a user