Skip to content

Commit

Permalink
refactor: provide UI button to recalculate when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ruthra-kumar committed Nov 24, 2023
1 parent aa17110 commit 7b24b80
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
20 changes: 20 additions & 0 deletions erpnext/projects/doctype/project/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ frappe.ui.form.on("Project", {
frm.events.create_duplicate(frm);
}, __("Actions"));

frm.add_custom_button(__('Update Total Purchase Cost'), () => {
frm.events.update_total_purchase_cost(frm);
}, __("Actions"));

frm.trigger("set_project_status_button");


Expand All @@ -92,6 +96,22 @@ frappe.ui.form.on("Project", {

},

update_total_purchase_cost: function(frm) {
frappe.call({
method: "erpnext.projects.doctype.project.project.recalculate_project_total_purchase_cost",
args: {project: frm.doc.name},
freeze: true,
freeze_message: __('Recalculating Purchase Cost against this Project...'),
callback: function(r) {
if (r && r.message) {
frappe.msgprint(__('Total Purchase Cost has been updated'));
frm.refresh();
}
}

});
},

set_project_status_button: function(frm) {
frm.add_custom_button(__('Set Project Status'), () => {
let d = new frappe.ui.Dialog({
Expand Down
37 changes: 29 additions & 8 deletions erpnext/projects/doctype/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import frappe
from email_reply_parser import EmailReplyParser
from frappe import _
from frappe import _, qb
from frappe.desk.reportview import get_match_cond
from frappe.model.document import Document
from frappe.query_builder import Interval
from frappe.query_builder.functions import Count, CurDate, Date, UnixTimestamp
from frappe.query_builder.functions import Count, CurDate, Date, Sum, UnixTimestamp
from frappe.utils import add_days, flt, get_datetime, get_time, get_url, nowtime, today
from frappe.utils.user import is_website_user

Expand Down Expand Up @@ -249,12 +249,7 @@ def calculate_gross_margin(self):
self.per_gross_margin = (self.gross_margin / flt(self.total_billed_amount)) * 100

def update_purchase_costing(self):
total_purchase_cost = frappe.db.sql(
"""select sum(base_net_amount)
from `tabPurchase Invoice Item` where project = %s and docstatus=1""",
self.name,
)

total_purchase_cost = calculate_total_purchase_cost(self.name)
self.total_purchase_cost = total_purchase_cost and total_purchase_cost[0][0] or 0

def update_sales_amount(self):
Expand Down Expand Up @@ -695,3 +690,29 @@ def get_holiday_list(company=None):

def get_users_email(doc):
return [d.email for d in doc.users if frappe.db.get_value("User", d.user, "enabled")]


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))
.where((pitem.project == project) & (pitem.docstatus == 1))
.run(as_list=True)
)
return total_purchase_cost
return None


@frappe.whitelist()
def recalculate_project_total_purchase_cost(project: str | None = None):
if project:
total_purchase_cost = calculate_total_purchase_cost(project)
frappe.db.set_value(
"Project",
project,
"total_purchase_cost",
(total_purchase_cost and total_purchase_cost[0][0] or 0),
)

0 comments on commit 7b24b80

Please sign in to comment.