|
7 | 7 |
|
8 | 8 | import frappe |
9 | 9 | from frappe import _, bold, qb, throw |
| 10 | +from frappe.contacts.doctype.address.address import get_address_display |
10 | 11 | from frappe.model.workflow import get_workflow_name, is_transition_condition_satisfied |
11 | 12 | from frappe.query_builder import Criterion, DocType |
12 | 13 | from frappe.query_builder.custom import ConstantColumn |
@@ -4167,3 +4168,109 @@ def update_gl_dict_with_regional_fields(doc, gl_dict): |
4167 | 4168 | def update_gl_dict_with_app_based_fields(doc, gl_dict): |
4168 | 4169 | for method in frappe.get_hooks("update_gl_dict_with_app_based_fields", default=[]): |
4169 | 4170 | 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