def unlink_pr(doc_name): doc = frappe.get_doc('CD GSTR 2B Entry', doc_name) user = frappe.session.user is_enabled = frappe.db.get_value('CD GSTR 2B Settings', None, 'enable_account_freezing') acc_settings = frappe.db.get_values( 'Accounts Settings', None, ['acc_frozen_upto', 'frozen_accounts_modifier']) if doc.cf_status == 'Accepted' and is_enabled: if not user in get_users_with_role( acc_settings[0][1]) and user != 'Administrator': if getdate(doc.cf_document_date) <= getdate(acc_settings[0][0]): frappe.throw( _("You are not authorized to update entries before {0}"). format(formatdate(acc_settings[0][0]))) else: frappe.throw( _("You do not have enough permission to do this action.")) return if doc.cf_status == 'Pending' and is_enabled: if getdate(doc.cf_document_date) <= getdate(acc_settings[0][0]) and \ not user in get_users_with_role(acc_settings[0][1])and user!='Administrator': frappe.throw( _("You are not authorized to update entries before {0}"). format(formatdate(acc_settings[0][0]))) return doc.cf_match_status = 'Missing in PR' doc.cf_reason = None doc.cf_status = 'Pending' doc.cf_purchase_invoice = None doc.save(ignore_permissions=True) doc.reload()
def notify_error_to_stock_managers(doc, traceback): recipients = get_users_with_role("Stock Manager") if not recipients: get_users_with_role("System Manager") subject = _("Error while reposting item valuation") message = (_("Hi,") + "<br>" + _( "An error has been appeared while reposting item valuation via {0}" ).format(get_link_to_form(doc.doctype, doc.name)) + "<br>" + _( "Please check the error message and take necessary actions to fix the error and then restart the reposting again." )) frappe.sendmail(recipients=recipients, subject=subject, message=message)
def validate_delivery_window(doc, method): from erpnext.stock.doctype.delivery_trip.delivery_trip import get_delivery_window if not frappe.db.get_single_value("Delivery Settings", "send_delivery_window_warning"): return if not (doc.get("delivery_start_time") and doc.get("delivery_end_time")): return if not doc.get("customer"): return delivery_window = get_delivery_window(customer=doc.get("customer")) delivery_start_time = delivery_window.delivery_start_time delivery_end_time = delivery_window.delivery_end_time if not (delivery_start_time and delivery_end_time): return if to_timedelta(doc.delivery_start_time) < to_timedelta(delivery_start_time) \ or to_timedelta(doc.delivery_end_time) > to_timedelta(delivery_end_time): if method == "validate": frappe.msgprint(_("The delivery window is set outside the customer's default timings")) elif method == "on_submit": # send an email notifying users that the document is outside the customer's delivery window role_profiles = ["Fulfillment Manager"] role_profile_users = frappe.get_all("User", filters={"role_profile_name": ["IN", role_profiles]}, fields=["email"]) role_profile_users = [user.email for user in role_profile_users] accounts_managers = get_users_with_role("Accounts Manager") system_managers = get_users_with_role("System Manager") recipients = list(set(role_profile_users + accounts_managers) - set(system_managers)) if not recipients: return # form the email subject = _("[Info] An order may be delivered outside a customer's preferred delivery window") message = _("""An order ({name}) has the following delivery window: {doc_start} - {doc_end}<br><br> While the default delivery window is {customer_start} - {customer_end}""".format( name=frappe.utils.get_link_to_form(doc.doctype, doc.name), doc_start=get_time(doc.delivery_start_time).strftime("%I:%M %p"), doc_end=get_time(doc.delivery_end_time).strftime("%I:%M %p"), customer_start=get_time(delivery_start_time).strftime("%I:%M %p"), customer_end=get_time(delivery_end_time).strftime("%I:%M %p"), )) frappe.sendmail(recipients=recipients, subject=subject, message=message)
def check_credit_limit(customer, company, ignore_outstanding_sales_order=False, extra_amount=0): credit_limit = get_credit_limit(customer, company) if not credit_limit: return customer_outstanding = get_customer_outstanding( customer, company, ignore_outstanding_sales_order) if extra_amount > 0: customer_outstanding += flt(extra_amount) if credit_limit > 0 and flt(customer_outstanding) > credit_limit: msgprint( _("Credit limit has been crossed for customer {0} ({1}/{2})"). format(customer, customer_outstanding, credit_limit)) # If not authorized person raise exception credit_controller_role = frappe.db.get_single_value( "Accounts Settings", "credit_controller") if not credit_controller_role or credit_controller_role not in frappe.get_roles( ): # form a list of emails for the credit controller users credit_controller_users = get_users_with_role( credit_controller_role or "Sales Master Manager") # form a list of emails and names to show to the user credit_controller_users_formatted = [ get_formatted_email(user).replace("<", "(").replace(">", ")") for user in credit_controller_users ] if not credit_controller_users_formatted: frappe.throw( _("Please contact your administrator to extend the credit limits for {0}." ).format(customer)) message = """Please contact any of the following users to extend the credit limits for {0}: <br><br><ul><li>{1}</li></ul>""".format( customer, "<li>".join(credit_controller_users_formatted)) # if the current user does not have permissions to override credit limit, # prompt them to send out an email to the controller users frappe.msgprint( message, title="Notify", raise_exception=1, primary_action={ "label": "Send Email", "server_action": "erpnext.selling.doctype.customer.customer.send_emails", "args": { "customer": customer, "customer_outstanding": customer_outstanding, "credit_limit": credit_limit, "credit_controller_users_list": credit_controller_users, }, }, )
def get_users_next_action_data(transitions, doc): user_data_map = {} for transition in transitions: users = get_users_with_role(transition.allowed) filtered_users = filter_allowed_users(users, doc, transition) for user in filtered_users: if not user_data_map.get(user): user_data_map[user] = frappe._dict({ 'possible_actions': [], 'email': frappe.db.get_value('User', user, 'email'), }) user_data_map[user].get('possible_actions').append(frappe._dict({ 'action_name': transition.action, 'action_link': get_workflow_action_url(transition.action, doc, user) })) return user_data_map
def check_credit_limit(customer, company, reference_doctype, reference_document, ignore_outstanding_sales_order=False, extra_amount=0): customer_outstanding = get_customer_outstanding(customer, company, ignore_outstanding_sales_order) if extra_amount > 0: customer_outstanding += flt(extra_amount) credit_limit = get_credit_limit(customer, company) if credit_limit > 0 and flt(customer_outstanding) > credit_limit: msgprint(_("Credit limit has been crossed for customer {0} ({1}/{2})") .format(customer, customer_outstanding, credit_limit)) # If not authorized person raise exception credit_controller_role = frappe.db.get_single_value('Accounts Settings', 'credit_controller') if not credit_controller_role or credit_controller_role not in frappe.get_roles(): # form a list of emails for the credit controller users credit_controller_users = get_users_with_role(credit_controller_role or "Sales Master Manager") credit_controller_users = [user[0] for user in credit_controller_users] # form a list of emails and names to show to the user credit_controller_users_list = [user for user in credit_controller_users if frappe.db.exists("Employee", {"prefered_email": user})] credit_controller_users = [get_formatted_email(user).replace("<", "(").replace(">", ")") for user in credit_controller_users_list] if not credit_controller_users: frappe.throw(_("Please contact your administrator to extend the credit limits for {0}.".format(customer))) message = """Please contact any of the following users to extend the credit limits for {0}: <br><br><ul><li>{1}</li></ul>""".format(customer, '<li>'.join(credit_controller_users)) # if the current user does not have permissions to override credit limit, # prompt them to send out an email to the controller users frappe.msgprint(message, title="Notify", raise_exception=1, primary_action={ 'label': 'Send Email', 'server_action': 'erpnext.selling.doctype.customer.customer.send_emails', 'hide_on_success': 1, 'args': { 'customer': customer, 'customer_outstanding': customer_outstanding, 'credit_limit': credit_limit, 'credit_controller_users_list': credit_controller_users_list, 'document_link': frappe.utils.get_link_to_form(reference_doctype, reference_document) } } )
def get_users_next_action_data(transitions, doc): user_data_map = {} for transition in transitions: users = get_users_with_role(transition.allowed) filtered_users = filter_allowed_users(users, doc, transition) for user in filtered_users: if not user_data_map.get(user): user_data_map[user] = frappe._dict({ "possible_actions": [], "email": frappe.db.get_value("User", user, "email"), }) user_data_map[user].get("possible_actions").append( frappe._dict({ "action_name": transition.action, "action_link": get_workflow_action_url(transition.action, doc, user), })) return user_data_map
def update_status(data, status): if isinstance(data, string_types): data = json.loads(data) forbidden_doc_list = [] ineligible_doc_list = [] allow_user = True user = frappe.session.user is_enabled = frappe.db.get_value('CD GSTR 2B Settings', None, 'enable_account_freezing') acc_settings = frappe.db.get_values('Accounts Settings', None, ['acc_frozen_upto', 'frozen_accounts_modifier']) if not user in get_users_with_role(acc_settings[0][1]) and user != 'Administrator' and is_enabled: allow_user = False for row in data: if row and row['gstr_2b']: doc = frappe.get_doc('CD GSTR 2B Entry', row['gstr_2b']) if doc.cf_status == 'Accepted' and not allow_user and is_enabled: if getdate(doc.cf_document_date) <= getdate(acc_settings[0][0]): forbidden_doc_list.append(comma_and("""<a href="#Form/CD GSTR 2B Entry/{0}">{1}</a>""".format(row['gstr_2b'], row['gstr_2b']))) else: forbidden_doc_list.append(comma_and("""<a href="#Form/CD GSTR 2B Entry/{0}">{1}</a>""".format(row['gstr_2b'], row['gstr_2b']))) continue if doc.cf_status == 'Pending' and not allow_user and is_enabled: if getdate(doc.cf_document_date) <= getdate(acc_settings[0][0]): forbidden_doc_list.append(comma_and("""<a href="#Form/CD GSTR 2B Entry/{0}">{1}</a>""".format(row['gstr_2b'], row['gstr_2b']))) continue if status == 'Accepted' and doc.cf_purchase_invoice and \ round(abs(doc.cf_tax_amount - get_tax_details(doc.cf_purchase_invoice)['total_tax_amount']), 2) > 10: ineligible_doc_list.append(comma_and("""<a href="#Form/CD GSTR 2B Entry/{0}">{1}</a>""".format(row['gstr_2b'], row['gstr_2b']))) continue doc.cf_status = status doc.save(ignore_permissions = True) doc.reload() frappe.db.commit() if forbidden_doc_list: forbidden_docs = ','.join(forbidden_doc_list) frappe.throw(_(f"You are not authorized to update entries {forbidden_docs}")) if ineligible_doc_list: ineligible_docs = ','.join(ineligible_doc_list) frappe.throw(_(f"Tax difference exceeded Rs.10. Unable to accept these documents {ineligible_docs}."))