Skip to content

Commit fc182a8

Browse files
committed
refactor: Replace use publish_realtime with msgprint for cleaner flow
1 parent 58e217b commit fc182a8

File tree

3 files changed

+237
-254
lines changed

3 files changed

+237
-254
lines changed

erpnext/accounts/doctype/sales_invoice/sales_invoice.py

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -282,59 +282,6 @@ def set_indicator(self):
282282
self.indicator_color = "green"
283283
self.indicator_title = _("Paid")
284284

285-
def before_print(self, settings=None):
286-
from frappe.contacts.doctype.address.address import get_address_display_list
287-
288-
super().before_print(settings)
289-
290-
company_details = frappe.get_value(
291-
"Company", self.company, ["company_logo", "website", "phone_no", "email"], as_dict=True
292-
)
293-
294-
required_fields = [
295-
company_details.get("company_logo"),
296-
company_details.get("phone_no"),
297-
company_details.get("email"),
298-
]
299-
300-
if not all(required_fields) and not frappe.has_permission("Company", "write", throw=False):
301-
frappe.msgprint(
302-
_(
303-
"Some required Company details are missing. You don't have permission to update them. Please contact your System Manager."
304-
)
305-
)
306-
return
307-
308-
if not self.company_address and not frappe.has_permission("Sales Invoice", "write", throw=False):
309-
frappe.msgprint(
310-
_(
311-
"Company Address is missing. You don't have permission to update it. Please contact your System Manager."
312-
)
313-
)
314-
return
315-
316-
address_display_list = get_address_display_list("Company", self.company)
317-
address_line = address_display_list[0].get("address_line1") if address_display_list else ""
318-
319-
required_fields.append(self.company_address)
320-
required_fields.append(address_line)
321-
322-
if not all(required_fields):
323-
frappe.publish_realtime(
324-
"sales_invoice_before_print",
325-
{
326-
"company_logo": company_details.get("company_logo"),
327-
"website": company_details.get("website"),
328-
"phone_no": company_details.get("phone_no"),
329-
"email": company_details.get("email"),
330-
"address_line": address_line,
331-
"company": self.company,
332-
"company_address": self.company_address,
333-
"name": self.name,
334-
},
335-
user=frappe.session.user,
336-
)
337-
338285
def validate(self):
339286
self.validate_auto_set_posting_time()
340287
super().validate()
@@ -2948,59 +2895,6 @@ def get_loyalty_programs(customer):
29482895
return lp_details
29492896

29502897

2951-
@frappe.whitelist()
2952-
def save_company_master_details(name, company, details):
2953-
from frappe.utils import validate_email_address
2954-
2955-
if isinstance(details, str):
2956-
details = frappe.parse_json(details)
2957-
2958-
if details.get("email"):
2959-
validate_email_address(details.get("email"), throw=True)
2960-
2961-
company_fields = ["company_logo", "website", "phone_no", "email"]
2962-
company_fields_to_update = {field: details.get(field) for field in company_fields if details.get(field)}
2963-
2964-
if company_fields_to_update:
2965-
frappe.db.set_value("Company", company, company_fields_to_update)
2966-
2967-
company_address = details.get("company_address")
2968-
if details.get("address_line1"):
2969-
address_doc = frappe.get_doc(
2970-
{
2971-
"doctype": "Address",
2972-
"address_title": details.get("address_title"),
2973-
"address_type": details.get("address_type"),
2974-
"address_line1": details.get("address_line1"),
2975-
"address_line2": details.get("address_line2"),
2976-
"city": details.get("city"),
2977-
"state": details.get("state"),
2978-
"pincode": details.get("pincode"),
2979-
"country": details.get("country"),
2980-
"is_your_company_address": 1,
2981-
"links": [{"link_doctype": "Company", "link_name": company}],
2982-
}
2983-
)
2984-
address_doc.insert()
2985-
company_address = address_doc.name
2986-
2987-
if company_address:
2988-
company_address_display = frappe.db.get_value("Sales Invoice", name, "company_address_display")
2989-
if not company_address_display or details.get("address_line1"):
2990-
from frappe.query_builder import DocType
2991-
2992-
SalesInvoice = DocType("Sales Invoice")
2993-
2994-
(
2995-
frappe.qb.update(SalesInvoice)
2996-
.set(SalesInvoice.company_address, company_address)
2997-
.set(SalesInvoice.company_address_display, get_address_display(company_address))
2998-
.where(SalesInvoice.name == name)
2999-
).run()
3000-
3001-
return True
3002-
3003-
30042898
@frappe.whitelist()
30052899
def create_invoice_discounting(source_name, target_doc=None):
30062900
invoice = frappe.get_doc("Sales Invoice", source_name)

erpnext/controllers/accounts_controller.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import frappe
99
from frappe import _, bold, qb, throw
10+
from frappe.contacts.doctype.address.address import get_address_display
1011
from frappe.model.workflow import get_workflow_name, is_transition_condition_satisfied
1112
from frappe.query_builder import Criterion, DocType
1213
from frappe.query_builder.custom import ConstantColumn
@@ -4167,3 +4168,109 @@ def update_gl_dict_with_regional_fields(doc, gl_dict):
41674168
def update_gl_dict_with_app_based_fields(doc, gl_dict):
41684169
for method in frappe.get_hooks("update_gl_dict_with_app_based_fields", default=[]):
41694170
frappe.get_attr(method)(doc, gl_dict)
4171+
4172+
4173+
@frappe.whitelist()
4174+
def get_missing_company_details(doctype, docname):
4175+
from frappe.contacts.doctype.address.address import get_address_display_list
4176+
4177+
company = frappe.db.get_value(doctype, docname, "company")
4178+
company_address = frappe.db.get_value(doctype, docname, "company_address")
4179+
4180+
company_details = frappe.get_value(
4181+
"Company", company, ["company_logo", "website", "phone_no", "email"], as_dict=True
4182+
)
4183+
4184+
required_fields = [
4185+
company_details.get("company_logo"),
4186+
company_details.get("phone_no"),
4187+
company_details.get("email"),
4188+
]
4189+
4190+
if not all(required_fields) and not frappe.has_permission("Company", "write", throw=False):
4191+
frappe.msgprint(
4192+
_(
4193+
"Some required Company details are missing. You don't have permission to update them. Please contact your System Manager."
4194+
)
4195+
)
4196+
return
4197+
4198+
if not company_address and not frappe.has_permission("Sales Invoice", "write", throw=False):
4199+
frappe.msgprint(
4200+
_(
4201+
"Company Address is missing. You don't have permission to update it. Please contact your System Manager."
4202+
)
4203+
)
4204+
return
4205+
4206+
address_display_list = get_address_display_list("Company", company)
4207+
address_line = address_display_list[0].get("address_line1") if address_display_list else ""
4208+
4209+
required_fields.append(company_address)
4210+
required_fields.append(address_line)
4211+
4212+
if all(required_fields):
4213+
return False
4214+
return {
4215+
"company_logo": company_details.get("company_logo"),
4216+
"website": company_details.get("website"),
4217+
"phone_no": company_details.get("phone_no"),
4218+
"email": company_details.get("email"),
4219+
"address_line": address_line,
4220+
"company": company,
4221+
"company_address": company_address,
4222+
"name": docname,
4223+
}
4224+
4225+
4226+
@frappe.whitelist()
4227+
def update_company_master_and_address(name, company, details):
4228+
from frappe.utils import validate_email_address
4229+
4230+
if isinstance(details, str):
4231+
details = frappe.parse_json(details)
4232+
4233+
if details.get("email"):
4234+
validate_email_address(details.get("email"), throw=True)
4235+
4236+
company_fields = ["company_logo", "website", "phone_no", "email"]
4237+
company_fields_to_update = {field: details.get(field) for field in company_fields if details.get(field)}
4238+
4239+
if company_fields_to_update:
4240+
frappe.db.set_value("Company", company, company_fields_to_update)
4241+
4242+
company_address = details.get("company_address")
4243+
if details.get("address_line1"):
4244+
address_doc = frappe.get_doc(
4245+
{
4246+
"doctype": "Address",
4247+
"address_title": details.get("address_title"),
4248+
"address_type": details.get("address_type"),
4249+
"address_line1": details.get("address_line1"),
4250+
"address_line2": details.get("address_line2"),
4251+
"city": details.get("city"),
4252+
"state": details.get("state"),
4253+
"pincode": details.get("pincode"),
4254+
"country": details.get("country"),
4255+
"is_your_company_address": 1,
4256+
"links": [{"link_doctype": "Company", "link_name": company}],
4257+
}
4258+
)
4259+
address_doc.insert()
4260+
company_address = address_doc.name
4261+
4262+
if company_address:
4263+
company_address_display = frappe.db.get_value("Sales Invoice", name, "company_address_display")
4264+
if not company_address_display or details.get("address_line1"):
4265+
from frappe.query_builder import DocType
4266+
4267+
SalesInvoice = DocType("Sales Invoice")
4268+
4269+
(
4270+
frappe.qb.update(SalesInvoice)
4271+
.set(SalesInvoice.company_address, company_address)
4272+
.set(SalesInvoice.company_address_display, get_address_display(company_address))
4273+
.where(SalesInvoice.name == name)
4274+
).run()
4275+
4276+
return True

0 commit comments

Comments
 (0)