From ae450fc23b4728b287b0ea2c27c8a7e68a3aa986 Mon Sep 17 00:00:00 2001 From: pratu16x7 Date: Mon, 26 Jun 2017 15:31:46 +0530 Subject: [PATCH] async call to require, more validation --- erpnext/public/js/controllers/transaction.js | 35 ++++++++++--------- .../js/utils/serial_no_batch_selector.js | 33 ++++++++++++++--- .../stock/doctype/stock_entry/stock_entry.js | 12 ++++--- 3 files changed, 55 insertions(+), 25 deletions(-) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 1801f5a95a..03f9f122a9 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -1,8 +1,6 @@ // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors // License: GNU General Public License v3. See license.txt -frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js"); - erpnext.TransactionController = erpnext.taxes_and_totals.extend({ setup: function() { this._super(); @@ -272,8 +270,10 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ if(['Sales Invoice', 'Purchase Invoice'].includes(this.frm.doc.doctype)) { update_stock = cint(me.frm.doc.update_stock); show_batch_dialog = update_stock; - } else if(this.frm.doc.doctype === 'Delivery Note') { - show_batch_dialog = 1; + + } else if((this.frm.doc.doctype === 'Purchase Receipt' && me.frm.doc.is_return) || + this.frm.doc.doctype === 'Delivery Note') { + show_batch_dialog = 1; } // clear barcode if setting item (else barcode will take priority) @@ -323,19 +323,9 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ if(show_batch_dialog) { var d = locals[cdt][cdn]; $.each(r.message, function(k, v) { - if(!d[k]) { - d[k] = v; - } + if(!d[k]) d[k] = v; }); - let serial_no_batch_selector = new erpnext.SerialNoBatchSelector({ - frm: me.frm, - item: d, - warehouse_details: { - type: "Warehouse", - name: d.warehouse - }, - }); - refresh_field("items"); + erpnext.show_serial_batch_selector(me.frm, d); } } } @@ -1193,3 +1183,16 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ } }, }); + +erpnext.show_serial_batch_selector = function(frm, d) { + frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function() { + let serial_no_batch_selector = new erpnext.SerialNoBatchSelector({ + frm: frm, + item: d, + warehouse_details: { + type: "Warehouse", + name: d.warehouse + }, + }); + }); +} diff --git a/erpnext/public/js/utils/serial_no_batch_selector.js b/erpnext/public/js/utils/serial_no_batch_selector.js index 2b90e3eeab..ceca9acdaa 100644 --- a/erpnext/public/js/utils/serial_no_batch_selector.js +++ b/erpnext/public/js/utils/serial_no_batch_selector.js @@ -31,8 +31,14 @@ erpnext.SerialNoBatchSelector = Class.extend({ this.data = this.oldest ? this.oldest : []; let title = ""; let fields = [ - {fieldname: 'item_code', read_only: 1, fieldtype:'Link', options: 'Item', - label: __('Item Code'), 'default': me.item_code}, + { + fieldname: 'item_code', + read_only: 1, + fieldtype:'Link', + options: 'Item', + label: __('Item Code'), + default: me.item_code + }, {fieldtype:'Column Break'}, { fieldname: 'warehouse', @@ -51,7 +57,13 @@ erpnext.SerialNoBatchSelector = Class.extend({ } }, {fieldtype:'Column Break'}, - {fieldname: 'qty', fieldtype:'Float', label: __(me.has_batch ? 'Total Qty' : 'Qty'), 'default': me.qty}, + { + fieldname: 'qty', + fieldtype:'Float', + read_only: 1, + label: __(me.has_batch ? 'Total Qty' : 'Qty'), + default: me.qty + }, ]; if(this.has_batch) { @@ -226,11 +238,23 @@ erpnext.SerialNoBatchSelector = Class.extend({ return {filters: {item: me.item_code }}; }, onchange: function(e) { - if(this.get_value().length === 0) { + let val = this.get_value(); + if(val.length === 0) { this.grid_row.on_grid_fields_dict .available_qty.set_value(0); return; } + let selected_batches = this.grid.grid_rows.map((row) => { + if(row === this.grid_row) { + return ""; + } + return row.on_grid_fields_dict.batch_no.get_value(); + }); + if(selected_batches.includes(val)) { + this.set_value(""); + frappe.throw(__(`Batch ${val} already selected.`)); + return; + } if(me.warehouse_details.name) { frappe.call({ method: 'erpnext.stock.doctype.batch.batch.get_batch_qty', @@ -246,6 +270,7 @@ erpnext.SerialNoBatchSelector = Class.extend({ }); } else { + this.set_value(""); frappe.throw(__(`Please select a warehouse to get available quantities`)); } diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js index 141faebb5c..04cf5d7aba 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.js +++ b/erpnext/stock/doctype/stock_entry/stock_entry.js @@ -1,7 +1,6 @@ // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors // License: GNU General Public License v3. See license.txt frappe.provide("erpnext.stock"); -frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js"); frappe.ui.form.on('Stock Entry', { setup: function(frm) { @@ -569,9 +568,12 @@ erpnext.stock.select_batch_and_serial_no = (frm, item) => { return; } - let serial_no_batch_selector = new erpnext.SerialNoBatchSelector({ - frm: frm, - item: item, - warehouse_details: get_warehouse_type_and_name(item), + frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function() { + let serial_no_batch_selector = new erpnext.SerialNoBatchSelector({ + frm: frm, + item: item, + warehouse_details: get_warehouse_type_and_name(item), + }); }); + }