Skip to content

Commit

Permalink
fix: don't map items twice
Browse files Browse the repository at this point in the history
* don't explicitly map Delivery Note Item custom fields to Packing Slip Item, get auto-mapped while mapping the doc.
* call Packing List `set_missing_values` after mapping the doc.
* refactor `get_recommended_case_no`, use `frappe.db.get_value` instead of `frappe.db.sql`.

(cherry picked from commit 75fe9dd)
  • Loading branch information
s-aga-r authored and mergify[bot] committed May 25, 2023
1 parent c7628c9 commit 6f0c7cf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 68 deletions.
6 changes: 4 additions & 2 deletions erpnext/stock/doctype/delivery_note/delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,9 @@ def update_item(obj, target, source_parent):

@frappe.whitelist()
def make_packing_slip(source_name, target_doc=None):
def set_missing_values(source, target):
target.run_method("set_missing_values")

doclist = get_mapped_doc(
"Delivery Note",
source_name,
Expand All @@ -698,9 +701,7 @@ def make_packing_slip(source_name, target_doc=None):
"batch_no": "batch_no",
"description": "description",
"qty": "qty",
"total_weight": "net_weight",
"stock_uom": "stock_uom",
"weight_uom": "weight_uom",
"name": "dn_detail",
},
"condition": lambda doc: not frappe.db.exists(
Expand All @@ -720,6 +721,7 @@ def make_packing_slip(source_name, target_doc=None):
},
},
target_doc,
set_missing_values,
)

return doclist
Expand Down
18 changes: 0 additions & 18 deletions erpnext/stock/doctype/packing_slip/packing_slip.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,6 @@ frappe.ui.form.on("Packing Slip", {
frm.trigger("validate_calculate_item_details");
},

onload_post_render: (frm) => {
if(frm.doc.delivery_note && frm.doc.__islocal) {
frm.trigger("get_items");
}
},

get_items: (frm) => {
return frm.call({
doc: frm.doc,
method: "get_items",
callback: function(r) {
if(!r.exc) {
frm.refresh();
}
}
});
},

// To Case No. cannot be less than From Case No.
validate_case_nos: (frm) => {
doc = locals[frm.doc.doctype][frm.doc.name];
Expand Down
73 changes: 25 additions & 48 deletions erpnext/stock/doctype/packing_slip/packing_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def validate(self):
validate_uom_is_integer(self, "stock_uom", "qty")
validate_uom_is_integer(self, "weight_uom", "net_weight")

self.set_missing_values()

def validate_delivery_note(self):
"""
Validates if delivery note has status as draft
Expand Down Expand Up @@ -80,6 +82,20 @@ def validate_qty(self):
if new_packed_qty > flt(item["qty"]) and no_of_cases:
self.recommend_new_qty(item, ps_item_qty, no_of_cases)

def set_missing_values(self):
if not self.from_case_no:
self.from_case_no = self.get_recommended_case_no()

for item in self.items:
weight_per_unit, weight_uom = frappe.db.get_value(
"Item", item.item_code, ["weight_per_unit", "weight_uom"]
)

if weight_per_unit and not item.net_weight:
item.net_weight = weight_per_unit
if weight_uom and not item.weight_uom:
item.weight_uom = weight_uom

def get_details_for_packing(self):
"""
Returns
Expand Down Expand Up @@ -141,56 +157,17 @@ def recommend_new_qty(self, item, ps_item_qty, no_of_cases):
)
)

def update_item_details(self):
"""
Fill empty columns in Packing Slip Item
"""
if not self.from_case_no:
self.from_case_no = self.get_recommended_case_no()

for d in self.get("items"):
res = frappe.db.get_value("Item", d.item_code, ["weight_per_unit", "weight_uom"], as_dict=True)

if res and len(res) > 0:
d.net_weight = res["weight_per_unit"]
d.weight_uom = res["weight_uom"]

def get_recommended_case_no(self):
"""
Returns the next case no. for a new packing slip for a delivery
note
"""
recommended_case_no = frappe.db.sql(
"""SELECT MAX(to_case_no) FROM `tabPacking Slip`
WHERE delivery_note = %s AND docstatus=1""",
self.delivery_note,
)

return cint(recommended_case_no[0][0]) + 1
"""Returns the next case no. for a new packing slip for a delivery note"""

@frappe.whitelist()
def get_items(self):
self.set("items", [])

custom_fields = frappe.get_meta("Delivery Note Item").get_custom_fields()

dn_details = self.get_details_for_packing()[0]
for item in dn_details:
if flt(item.qty) > flt(item.packed_qty):
ch = self.append("items", {})
ch.item_code = item.item_code
ch.item_name = item.item_name
ch.stock_uom = item.stock_uom
ch.description = item.description
ch.batch_no = item.batch_no
ch.qty = flt(item.qty) - flt(item.packed_qty)

# copy custom fields
for d in custom_fields:
if item.get(d.fieldname):
ch.set(d.fieldname, item.get(d.fieldname))

self.update_item_details()
return (
cint(
frappe.db.get_value(
"Packing Slip", {"delivery_note": self.delivery_note, "docstatus": 1}, ["max(to_case_no)"]
)
)
+ 1
)


@frappe.whitelist()
Expand Down

0 comments on commit 6f0c7cf

Please sign in to comment.