2019-03-18 09:04:19 +00:00
|
|
|
# Copyright (c) 2017, Frappe and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2019-03-18 09:04:19 +00:00
|
|
|
import frappe
|
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
parentfield = {"item_code": "items", "item_group": "item_groups", "brand": "brands"}
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-03-18 09:04:19 +00:00
|
|
|
|
|
|
|
def execute():
|
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
if not frappe.get_all("Pricing Rule", limit=1):
|
|
|
|
return
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
frappe.reload_doc("accounts", "doctype", "pricing_rule_detail")
|
|
|
|
doctypes = {
|
|
|
|
"Supplier Quotation": "buying",
|
|
|
|
"Purchase Order": "buying",
|
|
|
|
"Purchase Invoice": "accounts",
|
|
|
|
"Purchase Receipt": "stock",
|
|
|
|
"Quotation": "selling",
|
|
|
|
"Sales Order": "selling",
|
|
|
|
"Sales Invoice": "accounts",
|
|
|
|
"Delivery Note": "stock",
|
|
|
|
}
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
for doctype, module in doctypes.items():
|
|
|
|
frappe.reload_doc(module, "doctype", frappe.scrub(doctype))
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
child_doc = frappe.scrub(doctype) + "_item"
|
|
|
|
frappe.reload_doc(module, "doctype", child_doc, force=True)
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
child_doctype = doctype + " Item"
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
frappe.db.sql(
|
|
|
|
""" UPDATE `tab{child_doctype}` SET pricing_rules = pricing_rule
|
|
|
|
WHERE docstatus < 2 and pricing_rule is not null and pricing_rule != ''
|
|
|
|
""".format(
|
|
|
|
child_doctype=child_doctype
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
data = frappe.db.sql(
|
|
|
|
""" SELECT pricing_rule, name, parent,
|
|
|
|
parenttype, creation, modified, docstatus, modified_by, owner, name
|
|
|
|
FROM `tab{child_doc}` where docstatus < 2 and pricing_rule is not null
|
|
|
|
and pricing_rule != ''""".format(
|
|
|
|
child_doc=child_doctype
|
|
|
|
),
|
|
|
|
as_dict=1,
|
|
|
|
)
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
values = []
|
|
|
|
for d in data:
|
|
|
|
values.append(
|
2022-03-28 13:22:46 +00:00
|
|
|
(
|
2019-08-12 06:19:14 +00:00
|
|
|
d.pricing_rule,
|
|
|
|
d.name,
|
|
|
|
d.parent,
|
|
|
|
"pricing_rules",
|
|
|
|
d.parenttype,
|
|
|
|
d.creation,
|
|
|
|
d.modified,
|
|
|
|
d.docstatus,
|
|
|
|
d.modified_by,
|
|
|
|
d.owner,
|
|
|
|
frappe.generate_hash("", 10),
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
if values:
|
|
|
|
frappe.db.sql(
|
|
|
|
""" INSERT INTO
|
|
|
|
`tabPricing Rule Detail` (`pricing_rule`, `child_docname`, `parent`, `parentfield`, `parenttype`,
|
|
|
|
`creation`, `modified`, `docstatus`, `modified_by`, `owner`, `name`)
|
|
|
|
VALUES {values} """.format(
|
|
|
|
values=", ".join(["%s"] * len(values))
|
2022-03-28 13:22:46 +00:00
|
|
|
),
|
2019-08-12 06:19:14 +00:00
|
|
|
tuple(values),
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
frappe.reload_doc("accounts", "doctype", "pricing_rule")
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
for doctype, apply_on in {
|
|
|
|
"Pricing Rule Item Code": "Item Code",
|
|
|
|
"Pricing Rule Item Group": "Item Group",
|
|
|
|
"Pricing Rule Brand": "Brand",
|
|
|
|
}.items():
|
|
|
|
frappe.reload_doc("accounts", "doctype", frappe.scrub(doctype))
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
field = frappe.scrub(apply_on)
|
|
|
|
data = frappe.get_all(
|
|
|
|
"Pricing Rule",
|
|
|
|
fields=[field, "name", "creation", "modified", "owner", "modified_by"],
|
|
|
|
filters={"apply_on": apply_on},
|
|
|
|
)
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
values = []
|
|
|
|
for d in data:
|
|
|
|
values.append(
|
2022-03-28 13:22:46 +00:00
|
|
|
(
|
2019-08-12 06:19:14 +00:00
|
|
|
d.get(field),
|
|
|
|
d.name,
|
|
|
|
parentfield.get(field),
|
|
|
|
"Pricing Rule",
|
|
|
|
d.creation,
|
|
|
|
d.modified,
|
|
|
|
d.owner,
|
|
|
|
d.modified_by,
|
|
|
|
frappe.generate_hash("", 10),
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-08-12 06:19:14 +00:00
|
|
|
if values:
|
|
|
|
frappe.db.sql(
|
|
|
|
""" INSERT INTO
|
|
|
|
`tab{doctype}` ({field}, parent, parentfield, parenttype, creation, modified,
|
|
|
|
owner, modified_by, name)
|
|
|
|
VALUES {values} """.format(
|
|
|
|
doctype=doctype, field=field, values=", ".join(["%s"] * len(values))
|
2022-03-28 13:22:46 +00:00
|
|
|
),
|
2019-08-12 06:19:14 +00:00
|
|
|
tuple(values),
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|