refactor: generalize popup for multiple doctypes

This commit is contained in:
khushi8112
2025-11-30 18:30:47 +05:30
parent b808a51d8f
commit 5cfd7ec32a
2 changed files with 54 additions and 23 deletions

View File

@@ -4175,7 +4175,10 @@ def get_missing_company_details(doctype, docname):
from frappe.contacts.doctype.address.address import get_address_display_list
company = frappe.db.get_value(doctype, docname, "company")
company_address = frappe.db.get_value(doctype, docname, "company_address")
if doctype == "Purchase Order":
company_address = frappe.db.get_value(doctype, docname, "billing_address")
else:
company_address = frappe.db.get_value(doctype, docname, "company_address")
company_details = frappe.get_value(
"Company", company, ["company_logo", "website", "phone_no", "email"], as_dict=True
@@ -4195,7 +4198,7 @@ def get_missing_company_details(doctype, docname):
)
return
if not company_address and not frappe.has_permission("Sales Invoice", "write", throw=False):
if not company_address and not frappe.has_permission(doctype, "write", throw=False):
frappe.msgprint(
_(
"Company Address is missing. You don't have permission to update it. Please contact your System Manager."
@@ -4224,7 +4227,7 @@ def get_missing_company_details(doctype, docname):
@frappe.whitelist()
def update_company_master_and_address(name, company, details):
def update_company_master_and_address(current_doctype, name, company, details):
from frappe.utils import validate_email_address
if isinstance(details, str):
@@ -4259,18 +4262,36 @@ def update_company_master_and_address(name, company, details):
address_doc.insert()
company_address = address_doc.name
if company_address:
company_address_display = frappe.db.get_value("Sales Invoice", name, "company_address_display")
if not company_address_display or details.get("address_line1"):
from frappe.query_builder import DocType
update_doc_company_address(current_doctype, name, company_address, details)
SalesInvoice = DocType("Sales Invoice")
(
frappe.qb.update(SalesInvoice)
.set(SalesInvoice.company_address, company_address)
.set(SalesInvoice.company_address_display, get_address_display(company_address))
.where(SalesInvoice.name == name)
).run()
def update_doc_company_address(current_doctype, docname, company_address, details):
if not company_address:
return
return True
address_field_map = {
"Purchase Order": ("billing_address", "billing_address_display"),
"Sales Invoice": ("company_address", "company_address_display"),
"Delivery Note": ("company_address", "company_address_display"),
"POS Invoice": ("company_address", "company_address_display"),
}
address_field, display_field = address_field_map.get(
current_doctype, ("company_address", "company_address_display")
)
current_display = frappe.db.get_value(current_doctype, docname, display_field)
if current_display and not details.get("address_line1"):
return
from frappe.query_builder import DocType
DocType = DocType(current_doctype)
(
frappe.qb.update(DocType)
.set(getattr(DocType, address_field), company_address)
.set(getattr(DocType, display_field), get_address_display(company_address))
.where(DocType.name == docname)
).run()

View File

@@ -1,5 +1,14 @@
const doctype_list = ["Sales Invoice"];
const allowed_print_formats = ["Sales Invoice Standard", "Sales Invoice with Item Image"];
const doctype_list = ["Sales Invoice", "Delivery Note", "Purchase Order", "POS Invoice"];
const allowed_print_formats = [
"Sales Invoice Standard",
"Sales Invoice with Item Image",
"Delivery Note Standard",
"Delivery Note with Item Image",
"Purchase Order Standard",
"Purchase Order with Item Image",
"POS Invoice Standard",
"POS Invoice with Item Image",
];
const allowed_letterheads = ["Company Letterhead", "Company Letterhead - Grey"];
handle_route_event();
@@ -25,25 +34,25 @@ function should_fetch_company_details() {
return allowed_print_formats.includes(print_format) || allowed_letterheads.includes(letterhead);
}
function fetch_company_details(doctype, docname) {
function fetch_company_details(current_doctype, current_docname) {
frappe.call({
method: "erpnext.controllers.accounts_controller.get_missing_company_details",
args: { doctype, docname },
args: { doctype: current_doctype, docname: current_docname },
callback: function (r) {
if (r && r.message) {
open_company_details_dialog(r.message);
open_company_details_dialog(r.message, current_doctype);
}
},
});
}
function open_company_details_dialog(data) {
function open_company_details_dialog(data, current_doctype) {
const dialog = new frappe.ui.Dialog({
title: __("Enter Company Details"),
fields: build_dialog_fields(data),
primary_action_label: __("Save"),
primary_action(values) {
save_company_details(dialog, data, values);
save_company_details(dialog, data, values, current_doctype);
},
});
@@ -117,13 +126,14 @@ function make_field(label, fieldname, fieldtype, existing_value, required_if_emp
};
}
function save_company_details(dialog, data, values) {
function save_company_details(dialog, data, values, current_doctype) {
frappe.call({
method: "erpnext.controllers.accounts_controller.update_company_master_and_address",
args: {
name: data.name,
company: data.company,
details: values,
current_doctype: current_doctype,
},
callback() {
dialog.hide();