brotherton-erpnext/erpnext/portal/utils.py
Sabu Siyad f900a78995
refactor!: drop ecommerce in favor of webshop (#33265)
* refactor!: remove ecommerce item group field check

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: remove `e_commerce` directory

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: remove `get_context` from `item_group`

https://frappeframework.com/docs/v14/user/en/guides/portal-development/context

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: remove related `./templates`

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(navbar): remove wishlist (ecommerce)

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(js): remove js from scripts

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: remove `www/all-products`

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: remove pages and js

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: remove js/customer_reviews

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(portal utils): remove shopping cart debtor account

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: remove e_commerce events from hooks

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(web): remove e_commerce js from bundle

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(setup): remove shopping cart setup

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: remove pages

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor(item): remove website item button

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(payment request): remove `on_payment_authorized`

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: @staticmethod `get_gateway_details`

to avoid monkey patching, in custom apps
https://discuss.erpnext.com/t/how-to-override-method-in-frappe/28786/36

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(pages): remove product page

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(homepage): do not setup website items

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor(workspace): remove link to ecommerce settings

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(www): remove shop-by-category

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(homepage): remove featured product

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor: remove products in homepage

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor(homepage): remove explore button

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor: remove products fields from homepage

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* Revert "refactor!: @staticmethod `get_gateway_details`"

This reverts commit 561bcd96680a930bb92627869502d9346b10611b.

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: remove payment gateway e_commerce import

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* chore: pre-commit

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!: pass `party` into `get_price`

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor: move `get_item_codes_by_attributes` to `utilities/product`

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* refactor!(quotation): input customer group

Signed-off-by: Sabu Siyad <hello@ssiyad.com>

* chore: pre-commit

* refactor: remove custom `navbar_items.html`

* refactor!(item): remove `published_in_website`

* refactor: move `validate_duplicate_website_item` before rename

* test: remove `test_shopping_cart_without_website_item`

* chore: add doctype drop patch

* refactor: removed website item related code

* refactor: removed shopping_cart code

* refactor: removed e-commerce related patches

* refactor: removed website related fields from item group

* fix: patch create_asset_depreciation_schedules_from_assets, KeyError: '0K BU64 AUY'

---------

Signed-off-by: Sabu Siyad <hello@ssiyad.com>
Co-authored-by: Rohit Waghchaure <rohitw1991@gmail.com>
2023-10-17 17:05:44 +05:30

94 lines
2.7 KiB
Python

import frappe
def set_default_role(doc, method):
"""Set customer, supplier, student, guardian based on email"""
if frappe.flags.setting_role or frappe.flags.in_migrate:
return
roles = frappe.get_roles(doc.name)
contact_name = frappe.get_value("Contact", dict(email_id=doc.email))
if contact_name:
contact = frappe.get_doc("Contact", contact_name)
for link in contact.links:
frappe.flags.setting_role = True
if link.link_doctype == "Customer" and "Customer" not in roles:
doc.add_roles("Customer")
elif link.link_doctype == "Supplier" and "Supplier" not in roles:
doc.add_roles("Supplier")
def create_customer_or_supplier():
"""Based on the default Role (Customer, Supplier), create a Customer / Supplier.
Called on_session_creation hook.
"""
user = frappe.session.user
if frappe.db.get_value("User", user, "user_type") != "Website User":
return
user_roles = frappe.get_roles()
portal_settings = frappe.get_single("Portal Settings")
default_role = portal_settings.default_role
if default_role not in ["Customer", "Supplier"]:
return
# create customer / supplier if the user has that role
if portal_settings.default_role and portal_settings.default_role in user_roles:
doctype = portal_settings.default_role
else:
doctype = None
if not doctype:
return
if party_exists(doctype, user):
return
party = frappe.new_doc(doctype)
fullname = frappe.utils.get_fullname(user)
if not doctype == "Customer":
party.update(
{
"supplier_name": fullname,
"supplier_group": "All Supplier Groups",
"supplier_type": "Individual",
}
)
party.flags.ignore_mandatory = True
party.insert(ignore_permissions=True)
alternate_doctype = "Customer" if doctype == "Supplier" else "Supplier"
if party_exists(alternate_doctype, user):
# if user is both customer and supplier, alter fullname to avoid contact name duplication
fullname += "-" + doctype
create_party_contact(doctype, fullname, user, party.name)
return party
def create_party_contact(doctype, fullname, user, party_name):
contact = frappe.new_doc("Contact")
contact.update({"first_name": fullname, "email_id": user})
contact.append("links", dict(link_doctype=doctype, link_name=party_name))
contact.append("email_ids", dict(email_id=user, is_primary=True))
contact.flags.ignore_mandatory = True
contact.insert(ignore_permissions=True)
def party_exists(doctype, user):
# check if contact exists against party and if it is linked to the doctype
contact_name = frappe.db.get_value("Contact", {"email_id": user})
if contact_name:
contact = frappe.get_doc("Contact", contact_name)
doctypes = [d.link_doctype for d in contact.links]
return doctype in doctypes
return False