Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use 'name' to identify trigger for free item #45290

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions erpnext/accounts/doctype/sales_invoice/sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ def set_indicator(self):
self.indicator_title = _("Paid")

def validate(self):
self.update_temp_key_to_permanent_key()
super().validate()
# update temp key with permanent key
self.validate_auto_set_posting_time()

if not (self.is_pos or self.is_debit_note):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
"col_break3",
"base_rate",
"base_amount",
"pricing_rules",
"stock_uom_rate",
"is_free_item",
"grant_commission",
"pricing_rule_section",
"pricing_rules",
"is_free_item",
"parent_item_row",
"section_break_21",
"net_rate",
"net_amount",
Expand Down Expand Up @@ -952,12 +954,23 @@
"no_copy": 1,
"print_hide": 1,
"read_only": 1
},
{
"fieldname": "pricing_rule_section",
"fieldtype": "Section Break",
"label": "Pricing Rule"
},
{
"fieldname": "parent_item_row",
"fieldtype": "Data",
"label": "Parent Row",
"read_only": 1
}
],
"idx": 1,
"istable": 1,
"links": [],
"modified": "2024-11-25 16:27:33.287341",
"modified": "2024-12-27 16:45:24.605739",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Sales Invoice Item",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class SalesInvoiceItem(Document):
net_rate: DF.Currency
page_break: DF.Check
parent: DF.Data
parent_item_row: DF.Data | None
parentfield: DF.Data
parenttype: DF.Data
price_list_rate: DF.Currency
Expand Down
23 changes: 10 additions & 13 deletions erpnext/public/js/controllers/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -1891,28 +1891,25 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
}

apply_product_discount(args) {
const items = this.frm.doc.items.filter(d => (d.is_free_item)) || [];

const exist_items = items.map(row => { return {item_code: row.item_code, pricing_rules: row.pricing_rules};});
// Note: somehow free items are removed even before this point
let existing_free_items_for_trigger = this.frm.doc.items.filter(d => { return d.is_free_item && d.parent_item_row == args.name}) || [];

args.free_item_data.forEach(pr_row => {
let row_to_modify = {};

// If there are no free items, or if the current free item doesn't exist in the table, add it
if (!items || !exist_items.filter(e_row => {
return e_row.item_code == pr_row.item_code && e_row.pricing_rules == pr_row.pricing_rules;
}).length) {
row_to_modify = frappe.model.add_child(this.frm.doc,
this.frm.doc.doctype + ' Item', 'items');

} else if(items) {
row_to_modify = items.filter(d => (d.item_code === pr_row.item_code
&& d.pricing_rules === pr_row.pricing_rules))[0];
let _match = existing_free_items_for_trigger.filter(d => {return d.item_code == pr_row.item_code && d.pricing_rules == pr_row.pricing_rules})
if(_match.length) {
row_to_modify = _match[0];
} else {
row_to_modify = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + ' Item', 'items');
}

console.log(row_to_modify);

for (let key in pr_row) {
row_to_modify[key] = pr_row[key];
}
row_to_modify["parent_item_row"] = args.name;
this.frm.script_manager.copy_from_first_row("items", row_to_modify, ["expense_account", "income_account"]);
});

Expand Down
32 changes: 31 additions & 1 deletion erpnext/utilities/transaction_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,35 @@ def fetch_item_details(self, item: dict) -> dict:
)
)

def update_temp_key_to_permanent_key(self):
def print_item(x):
print(
(
x.item_code,
("name", x.name),
("__temporary_name", x.get("__temporary_name")),
("parent_item_row", x.get("parent_item_row")),
("__is_local", x.get("__is_local")),
)
)

print("----------before----------")
for x in self.items:
print_item(x)

tk_to_pk = {}
for x in self.items:
if x.name and x.get("__temporary_name"):
tk_to_pk[x.get("__temporary_name")] = x.name

print(tk_to_pk)
for x in self.items:
if x.get("__islocal") and x.parent_item_row:
x.parent_item_row = tk_to_pk[x.parent_item_row]
print("----------after----------")
for x in self.items:
print_item(x)

@frappe.whitelist()
def process_item_selection(self, item_idx):
# Server side 'item' doc. Update this to reflect in UI
Expand Down Expand Up @@ -365,13 +394,14 @@ def add_free_item(self, item_obj: object, item_details: dict) -> None:
x
for x in existing_free_items
if x.item_code == free_item.get("item_code")
and x.pricing_rules == free_item.get("pricing_rules")
and x.get("parent_item_row") == item_obj.get("__temporary_name")
]
if _matches:
row_to_modify = _matches[0]
else:
row_to_modify = self.append("items")

setattr(row_to_modify, "parent_item_row", item_obj.get("__temporary_name"))
for k, _v in free_item.items():
setattr(row_to_modify, k, free_item.get(k))

Expand Down
Loading