Skip to content

Commit

Permalink
fix: Handle Multiselect field mapping separately
Browse files Browse the repository at this point in the history
- Map Multiselect child table to Website Item (copy rows)
  • Loading branch information
marination committed Apr 20, 2022
1 parent e76220e commit dc2f694
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion erpnext/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,4 @@ erpnext.patches.v13_0.enable_ksa_vat_docs #1
erpnext.patches.v13_0.create_gst_custom_fields_in_quotation
erpnext.patches.v13_0.update_expense_claim_status_for_paid_advances
erpnext.patches.v13_0.set_return_against_in_pos_invoice_references
erpnext.patches.v13_0.copy_custom_field_filters_to_website_item
erpnext.patches.v13_0.copy_custom_field_filters_to_website_item
56 changes: 48 additions & 8 deletions erpnext/patches/v13_0/copy_custom_field_filters_to_website_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,43 @@

def execute():
"Add Field Filters, that are not standard fields in Website Item, as Custom Fields."

def move_table_multiselect_data(docfield):
"Copy child table data (Table Multiselect) from Item to Website Item for a docfield."
table_multiselect_data = get_table_multiselect_data(docfield)
field = docfield.fieldname

for row in table_multiselect_data:
# add copied multiselect data rows in Website Item
web_item = frappe.db.get_value("Website Item", {"item_code": row.parent})
web_item_doc = frappe.get_doc("Website Item", web_item)

child_doc = frappe.new_doc(docfield.options, web_item_doc, field)

for field in ["name", "creation", "modified", "idx"]:
row[field] = None

child_doc.update(row)

child_doc.parenttype = "Website Item"
child_doc.parent = web_item

child_doc.insert()

def get_table_multiselect_data(docfield):
child_table = frappe.qb.DocType(docfield.options)
item = frappe.qb.DocType("Item")

table_multiselect_data = ( # query table data for field
frappe.qb.from_(child_table)
.join(item)
.on(item.item_code == child_table.parent)
.select(child_table.star)
.where((child_table.parentfield == docfield.fieldname) & (item.published_in_website == 1))
).run(as_dict=True)

return table_multiselect_data

settings = frappe.get_doc("E Commerce Settings")

if not (settings.enable_field_filters or settings.filter_fields):
Expand Down Expand Up @@ -43,12 +80,15 @@ def execute():
)

# map field values
frappe.db.sql(
"""
UPDATE `tabWebsite Item` wi, `tabItem` i
SET wi.{0} = i.{0}
WHERE wi.item_code = i.item_code
""".format(
row.fieldname
if df.fieldtype == "Table MultiSelect":
move_table_multiselect_data(df)
else:
frappe.db.sql( # nosemgrep
"""
UPDATE `tabWebsite Item` wi, `tabItem` i
SET wi.{0} = i.{0}
WHERE wi.item_code = i.item_code
""".format(
row.fieldname
)
)
)

0 comments on commit dc2f694

Please sign in to comment.