From f68dc69078cc51c5655d49ea56717bdfbf1a8a74 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 1 Mar 2018 10:54:24 +0530 Subject: [PATCH] Set auto created serial nos in incoming transactions in case of multi UOM (#13112) * Create user from Employee * Set auto created serial nos in incoming transactions in case of multi uom --- erpnext/hr/doctype/employee/employee.js | 2 +- erpnext/hr/doctype/employee/employee.py | 5 +- erpnext/patches.txt | 1 + ...t_auto_created_serial_no_in_stock_entry.py | 53 +++++++++++++++++++ erpnext/stock/doctype/serial_no/serial_no.py | 9 +++- 5 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 erpnext/patches/v10_0/set_auto_created_serial_no_in_stock_entry.py diff --git a/erpnext/hr/doctype/employee/employee.js b/erpnext/hr/doctype/employee/employee.js index c24a6bc30b..5e1013d6a2 100755 --- a/erpnext/hr/doctype/employee/employee.js +++ b/erpnext/hr/doctype/employee/employee.js @@ -81,7 +81,7 @@ frappe.ui.form.on('Employee',{ } frappe.call({ method: "erpnext.hr.doctype.employee.employee.create_user", - args: { employee: cur_frm.doc.name }, + args: { employee: frm.doc.name, email: frm.doc.prefered_email }, callback: function(r) { frm.set_value("user_id", r.message) diff --git a/erpnext/hr/doctype/employee/employee.py b/erpnext/hr/doctype/employee/employee.py index 9541b775a1..25d3ec4bc9 100755 --- a/erpnext/hr/doctype/employee/employee.py +++ b/erpnext/hr/doctype/employee/employee.py @@ -263,7 +263,7 @@ def deactivate_sales_person(status = None, employee = None): frappe.db.set_value("Sales Person", sales_person, "enabled", 0) @frappe.whitelist() -def create_user(employee, user = None): +def create_user(employee, user = None, email=None): emp = frappe.get_doc("Employee", employee) employee_name = emp.employee_name.split(" ") @@ -277,6 +277,9 @@ def create_user(employee, user = None): first_name = employee_name[0] + if email: + emp.prefered_email = email + user = frappe.new_doc("User") user.update({ "name": emp.employee_name, diff --git a/erpnext/patches.txt b/erpnext/patches.txt index df795fcc7a..705d2198aa 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -490,4 +490,5 @@ erpnext.patches.v10_0.set_default_payment_terms_based_on_company erpnext.patches.v10_0.update_sales_order_link_to_purchase_order erpnext.patches.v10_0.added_extra_gst_custom_field_in_gstr2 #2018-02-13 erpnext.patches.v10_0.set_b2c_limit +erpnext.patches.v10_0.set_auto_created_serial_no_in_stock_entry erpnext.patches.v10_0.update_territory_and_customer_group diff --git a/erpnext/patches/v10_0/set_auto_created_serial_no_in_stock_entry.py b/erpnext/patches/v10_0/set_auto_created_serial_no_in_stock_entry.py new file mode 100644 index 0000000000..afc49523f6 --- /dev/null +++ b/erpnext/patches/v10_0/set_auto_created_serial_no_in_stock_entry.py @@ -0,0 +1,53 @@ +# Copyright (c) 2017, Frappe and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import frappe + +def execute(): + serialised_items = [d.name for d in frappe.get_all("Item", filters={"has_serial_no": 1})] + + for dt in ["Stock Entry Detail", "Purchase Receipt Item", "Purchase Invoice Item"]: + cond = "" + if dt=="Purchase Invoice Item": + cond = """ and parent in (select name from `tabPurchase Invoice` + where `tabPurchase Invoice`.name = `tabPurchase Invoice Item`.parent and update_stock=1)""" + + item_rows = frappe.db.sql(""" + select name + from `tab{0}` + where conversion_factor != 1 + and docstatus = 1 + and ifnull(serial_no, '') = '' + and item_code in ({1}) + {2} + """.format(dt, ', '.join(['%s']*len(serialised_items)), cond), tuple(serialised_items)) + + if item_rows: + sle_serial_nos = dict(frappe.db.sql(""" + select voucher_detail_no, serial_no + from `tabStock Ledger Entry` + where ifnull(serial_no, '') != '' + and voucher_detail_no in (%s) + """.format(', '.join(['%s']*len(item_rows))), + tuple([d[0] for d in item_rows]))) + + batch_size = 100 + for i in range(0, len(item_rows), batch_size): + batch_item_rows = item_rows[i:i + batch_size] + when_then = [] + for item_row in batch_item_rows: + + when_then.append('WHEN `name` = "{row_name}" THEN "{value}"'.format( + row_name=item_row[0], + value=sle_serial_nos.get(item_row[0]))) + + frappe.db.sql(""" + update + `tab{doctype}` + set + serial_no = CASE {when_then_cond} ELSE `serial_no` END + """.format( + doctype = dt, + when_then_cond=" ".join(when_then) + )) \ No newline at end of file diff --git a/erpnext/stock/doctype/serial_no/serial_no.py b/erpnext/stock/doctype/serial_no/serial_no.py index 4c609df297..00a8a9305b 100644 --- a/erpnext/stock/doctype/serial_no/serial_no.py +++ b/erpnext/stock/doctype/serial_no/serial_no.py @@ -326,11 +326,16 @@ def update_serial_nos_after_submit(controller, parentfield): update_rejected_serial_nos = True if (controller.doctype in ("Purchase Receipt", "Purchase Invoice") and d.rejected_qty) else False accepted_serial_nos_updated = False - warehouse = d.t_warehouse if controller.doctype == "Stock Entry" else d.warehouse + if controller.doctype == "Stock Entry": + warehouse = d.t_warehouse + qty = d.transfer_qty + else: + warehouse = d.warehouse + qty = d.stock_qty for sle in stock_ledger_entries: if sle.voucher_detail_no==d.name: - if not accepted_serial_nos_updated and d.qty and abs(sle.actual_qty)==d.qty \ + if not accepted_serial_nos_updated and qty and abs(sle.actual_qty)==qty \ and sle.warehouse == warehouse and sle.serial_no != d.serial_no: d.serial_no = sle.serial_no frappe.db.set_value(d.doctype, d.name, "serial_no", sle.serial_no)