Skip to content

Instantly share code, notes, and snippets.

@GreyMark7650
Created April 25, 2013 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GreyMark7650/5459251 to your computer and use it in GitHub Desktop.
Save GreyMark7650/5459251 to your computer and use it in GitHub Desktop.
A script for erpnext to generate the next item ID in the specified product group.
cur_frm.cscript.next_id = function(doc)
   {
   doc.item_code = "";
   var pattern = "";
pattern = doc.item_group.substring(0,4)+'%';
var query = "SELECT `tabItem`.name FROM `tabItem` WHERE `tabItem`.name LIKE '" + pattern + "' ORDER BY name desc limit 1";
wn.call({
   method: "webnotes.widgets.query_builder.runquery",
   args: {
       "query": query
       },
   callback: function(r)
{
// returned last value
var last_id = "";
if (typeof r != 'undefined' && typeof r.values != 'undefined' && typeof r.values[0] != 'undefined')
{
last_id = r.values[0][0];
}
else
{
last_id = pattern.substring(0,pattern.length-1) + "001000";
}
var last_number = parseInt(last_id.substring(4))+1;
var new_prefix = last_id.substring(0,4);
var new_suffix = last_number.toString();
var new_id = new_prefix;
for (var i=0;i<(6-new_suffix.length);i++)
{
new_id += '0';
}
new_id += new_suffix;
    cur_frm.set_value("item_code", new_id);
    }
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment