2017-11-14 09:57:28 +00:00
frappe . provide ( 'frappe.ui.form' ) ;
frappe . ui . form . ItemQuickEntryForm = class ItemQuickEntryForm extends frappe . ui . form . QuickEntryForm {
constructor ( doctype , after _insert ) {
super ( doctype , after _insert ) ;
}
render _dialog ( ) {
this . mandatory = this . get _variant _fields ( ) . concat ( this . mandatory ) ;
this . mandatory = this . mandatory . concat ( this . get _attributes _fields ( ) ) ;
2020-03-28 09:26:45 +00:00
this . check _naming _series _based _on ( ) ;
2017-11-14 09:57:28 +00:00
super . render _dialog ( ) ;
this . init _post _render _dialog _operations ( ) ;
this . preset _fields _for _template ( ) ;
this . dialog . $wrapper . find ( '.edit-full' ) . text ( _ _ ( 'Edit in full page for more options like assets, serial nos, batches etc.' ) )
}
2020-03-28 09:26:45 +00:00
check _naming _series _based _on ( ) {
if ( frappe . defaults . get _default ( "item_naming_by" ) === "Naming Series" ) {
this . mandatory = this . mandatory . filter ( d => d . fieldname !== "item_code" ) ;
}
}
2017-11-14 09:57:28 +00:00
init _post _render _dialog _operations ( ) {
this . dialog . fields _dict . attribute _html . $wrapper . append ( frappe . render _template ( "item_quick_entry" ) ) ;
this . init _for _create _variant _trigger ( ) ;
this . init _for _item _template _trigger ( ) ;
// explicitly hide manufacturing fields as hidden not working.
this . toggle _manufacturer _fields ( ) ;
this . dialog . get _field ( "item_template" ) . df . hidden = 1 ;
this . dialog . get _field ( "item_template" ) . refresh ( ) ;
}
register _primary _action ( ) {
var me = this ;
this . dialog . set _primary _action ( _ _ ( 'Save' ) , function ( ) {
if ( me . dialog . working ) return ;
var data = me . dialog . get _values ( ) ;
var variant _values = { } ;
if ( me . dialog . fields _dict . create _variant . $input . prop ( "checked" ) ) {
variant _values = me . get _variant _doc ( ) ;
if ( ! Object . keys ( variant _values ) . length ) {
data = null ;
}
variant _values . stock _uom = me . template _doc . stock _uom ;
variant _values . item _group = me . template _doc . item _group ;
}
if ( data ) {
me . dialog . working = true ;
var values = me . update _doc ( ) ;
//patch for manufacturer type variants as extend is overwriting it.
if ( variant _values [ 'variant_based_on' ] == "Manufacturer" ) {
values [ 'variant_based_on' ] = "Manufacturer" ;
}
$ . extend ( variant _values , values ) ;
me . insert ( variant _values ) ;
}
} ) ;
}
insert ( variant _values ) {
let me = this ;
return new Promise ( resolve => {
frappe . call ( {
method : "frappe.client.insert" ,
args : {
doc : variant _values
} ,
callback : function ( r ) {
me . dialog . hide ( ) ;
// delete the old doc
frappe . model . clear _doc ( me . dialog . doc . doctype , me . dialog . doc . name ) ;
me . dialog . doc = r . message ;
if ( frappe . _from _link ) {
frappe . ui . form . update _calling _link ( me . dialog . doc ) ;
} else {
if ( me . after _insert ) {
me . after _insert ( me . dialog . doc ) ;
} else {
2018-08-16 03:52:33 +00:00
me . open _form _if _not _list ( ) ;
2017-11-14 09:57:28 +00:00
}
}
} ,
error : function ( ) {
me . open _doc ( ) ;
} ,
always : function ( ) {
me . dialog . working = false ;
resolve ( me . dialog . doc ) ;
} ,
freeze : true
} ) ;
} ) ;
}
open _doc ( ) {
this . dialog . hide ( ) ;
this . update _doc ( ) ;
if ( this . dialog . fields _dict . create _variant . $input . prop ( "checked" ) ) {
var template = this . dialog . fields _dict . item _template . input . value ;
if ( template )
frappe . set _route ( "Form" , this . doctype , template ) ;
} else {
frappe . set _route ( 'Form' , this . doctype , this . doc . name ) ;
}
}
get _variant _fields ( ) {
var variant _fields = [ {
fieldname : "create_variant" ,
fieldtype : "Check" ,
label : _ _ ( "Create Variant" )
} ,
{
fieldname : 'item_template' ,
label : _ _ ( 'Item Template' ) ,
reqd : 0 ,
fieldtype : 'Link' ,
options : "Item" ,
get _query : function ( ) {
return {
filters : {
"has_variants" : 1
}
} ;
}
} ] ;
return variant _fields ;
}
get _manufacturing _fields ( ) {
this . manufacturer _fields = [ {
fieldtype : 'Link' ,
options : 'Manufacturer' ,
label : 'Manufacturer' ,
fieldname : "manufacturer" ,
hidden : 1 ,
reqd : 0
} , {
fieldtype : 'Data' ,
label : 'Manufacturer Part Number' ,
fieldname : 'manufacturer_part_no' ,
hidden : 1 ,
reqd : 0
} ] ;
return this . manufacturer _fields ;
}
get _attributes _fields ( ) {
var attribute _fields = [ {
fieldname : 'attribute_html' ,
fieldtype : 'HTML'
} ]
attribute _fields = attribute _fields . concat ( this . get _manufacturing _fields ( ) ) ;
return attribute _fields ;
}
init _for _create _variant _trigger ( ) {
var me = this ;
this . dialog . fields _dict . create _variant . $input . on ( "click" , function ( ) {
me . preset _fields _for _template ( ) ;
me . init _post _template _trigger _operations ( false , [ ] , true ) ;
} ) ;
}
preset _fields _for _template ( ) {
var for _variant = this . dialog . get _value ( 'create_variant' ) ;
// setup template field, seen and mandatory if variant
let template _field = this . dialog . get _field ( "item_template" ) ;
template _field . df . reqd = for _variant ;
template _field . set _value ( '' ) ;
template _field . df . hidden = ! for _variant ;
template _field . refresh ( ) ;
// hide properties for variant
[ 'item_code' , 'item_name' , 'item_group' , 'stock_uom' ] . forEach ( ( d ) => {
let f = this . dialog . get _field ( d ) ;
f . df . hidden = for _variant ;
f . refresh ( ) ;
} ) ;
this . dialog . get _field ( 'attribute_html' ) . toggle ( false ) ;
// non mandatory for variants
[ 'item_code' , 'stock_uom' , 'item_group' ] . forEach ( ( d ) => {
let f = this . dialog . get _field ( d ) ;
f . df . reqd = ! for _variant ;
f . refresh ( ) ;
} ) ;
}
init _for _item _template _trigger ( ) {
var me = this ;
me . dialog . fields _dict [ "item_template" ] . df . onchange = ( ) => {
var template = me . dialog . fields _dict . item _template . input . value ;
me . template _doc = null ;
if ( template ) {
frappe . call ( {
method : "frappe.client.get" ,
args : {
doctype : "Item" ,
name : template
} ,
callback : function ( r ) {
me . template _doc = r . message ;
me . is _manufacturer = false ;
if ( me . template _doc . variant _based _on === "Manufacturer" ) {
me . init _post _template _trigger _operations ( true , [ ] , true ) ;
} else {
me . init _post _template _trigger _operations ( false , me . template _doc . attributes , false ) ;
me . render _attributes ( me . template _doc . attributes ) ;
}
}
} ) ;
} else {
me . dialog . get _field ( 'attribute_html' ) . toggle ( false ) ;
me . init _post _template _trigger _operations ( false , [ ] , true ) ;
}
}
}
init _post _template _trigger _operations ( is _manufacturer , attributes , attributes _flag ) {
this . attributes = attributes ;
this . attribute _values = { } ;
this . attributes _count = attributes . length ;
this . dialog . fields _dict . attribute _html . $wrapper . find ( ".attributes" ) . empty ( ) ;
this . is _manufacturer = is _manufacturer ;
this . toggle _manufacturer _fields ( ) ;
this . dialog . fields _dict . attribute _html . $wrapper . find ( ".attributes" ) . toggleClass ( "hide-control" , attributes _flag ) ;
this . dialog . fields _dict . attribute _html . $wrapper . find ( ".attributes-header" ) . toggleClass ( "hide-control" , attributes _flag ) ;
}
toggle _manufacturer _fields ( ) {
var me = this ;
$ . each ( this . manufacturer _fields , function ( i , dialog _field ) {
me . dialog . get _field ( dialog _field . fieldname ) . df . hidden = ! me . is _manufacturer ;
me . dialog . get _field ( dialog _field . fieldname ) . df . reqd = dialog _field . fieldname == 'manufacturer' ? me . is _manufacturer : false ;
me . dialog . get _field ( dialog _field . fieldname ) . refresh ( ) ;
} ) ;
}
initiate _render _attributes ( ) {
this . dialog . fields _dict . attribute _html . $wrapper . find ( ".attributes" ) . empty ( ) ;
this . render _attributes ( this . attributes ) ;
}
render _attributes ( attributes ) {
var me = this ;
this . dialog . get _field ( 'attribute_html' ) . toggle ( true ) ;
$ . each ( attributes , function ( index , row ) {
var desc = "" ;
var fieldtype = "Data" ;
if ( row . numeric _values ) {
fieldtype = "Float" ;
desc = "Min Value: " + row . from _range + " , Max Value: " + row . to _range + ", in Increments of: " + row . increment ;
}
me . init _make _control ( fieldtype , row ) ;
me [ row . attribute ] . set _value ( me . attribute _values [ row . attribute ] || "" ) ;
me [ row . attribute ] . $wrapper . toggleClass ( "has-error" , me . attribute _values [ row . attribute ] ? false : true ) ;
// Set Label explicitly as make_control is not displaying label
$ ( me [ row . attribute ] . label _area ) . text ( _ _ ( row . attribute ) ) ;
if ( desc ) {
$ ( repl ( ` <p class="help-box small text-muted hidden-xs">%(desc)s</p> ` , {
"desc" : desc
} ) ) . insertAfter ( me [ row . attribute ] . input _area ) ;
}
if ( ! row . numeric _values ) {
me . init _awesomplete _for _attribute ( row ) ;
} else {
me [ row . attribute ] . $input . on ( "change" , function ( ) {
me . attribute _values [ row . attribute ] = $ ( this ) . val ( ) ;
$ ( this ) . closest ( ".frappe-control" ) . toggleClass ( "has-error" , $ ( this ) . val ( ) ? false : true ) ;
} ) ;
}
} ) ;
}
init _make _control ( fieldtype , row ) {
this [ row . attribute ] = frappe . ui . form . make _control ( {
df : {
"fieldtype" : fieldtype ,
"label" : row . attribute ,
"fieldname" : row . attribute ,
"options" : row . options || ""
} ,
parent : $ ( this . dialog . fields _dict . attribute _html . wrapper ) . find ( ".attributes" ) ,
only _input : false
} ) ;
this [ row . attribute ] . make _input ( ) ;
}
init _awesomplete _for _attribute ( row ) {
var me = this ;
this [ row . attribute ] . input . awesomplete = new Awesomplete ( this [ row . attribute ] . input , {
minChars : 0 ,
maxItems : 99 ,
autoFirst : true ,
list : [ ] ,
} ) ;
this [ row . attribute ] . $input . on ( 'input' , function ( e ) {
frappe . call ( {
method : "frappe.client.get_list" ,
args : {
doctype : "Item Attribute Value" ,
filters : [
[ "parent" , "=" , $ ( e . target ) . attr ( "data-fieldname" ) ] ,
[ "attribute_value" , "like" , e . target . value + "%" ]
] ,
2018-03-29 07:15:15 +00:00
fields : [ "attribute_value" ] ,
2018-12-04 13:13:52 +00:00
parent : "Item Attribute"
2017-11-14 09:57:28 +00:00
} ,
callback : function ( r ) {
if ( r . message ) {
e . target . awesomplete . list = r . message . map ( function ( d ) {
return d . attribute _value ;
} ) ;
}
}
} ) ;
} ) . on ( 'focus' , function ( e ) {
$ ( e . target ) . val ( '' ) . trigger ( 'input' ) ;
} ) . on ( "awesomplete-close" , function ( e ) {
me . attribute _values [ $ ( e . target ) . attr ( "data-fieldname" ) ] = e . target . value ;
$ ( e . target ) . closest ( ".frappe-control" ) . toggleClass ( "has-error" , e . target . value ? false : true ) ;
} ) ;
}
get _variant _doc ( ) {
var me = this ;
var variant _doc = { } ;
var attribute = this . validate _mandatory _attributes ( ) ;
if ( Object . keys ( attribute ) . length ) {
frappe . call ( {
method : "erpnext.controllers.item_variant.create_variant_doc_for_quick_entry" ,
args : {
"template" : me . dialog . fields _dict . item _template . $input . val ( ) ,
args : attribute
} ,
async : false ,
callback : function ( r ) {
if ( Object . prototype . toString . call ( r . message ) == "[object Object]" ) {
variant _doc = r . message ;
} else {
var msgprint _dialog = frappe . msgprint ( _ _ ( "Item Variant {0} already exists with same attributes" , [ repl ( ' < a class = "strong variant-click" data - item - code = "%(item)s" \
> % ( item ) s < / a > ' , {
item : r . message
} ) ] ) ) ;
msgprint _dialog . $wrapper . find ( ".variant-click" ) . on ( "click" , function ( ) {
msgprint _dialog . hide ( ) ;
me . dialog . hide ( ) ;
if ( frappe . _from _link ) {
frappe . _from _link . set _value ( $ ( this ) . attr ( "data-item-code" ) ) ;
} else {
frappe . set _route ( 'Form' , "Item" , $ ( this ) . attr ( "data-item-code" ) ) ;
}
} ) ;
}
}
} )
}
return variant _doc ;
}
validate _mandatory _attributes ( ) {
var me = this ;
var attribute = { } ;
var mandatory = [ ] ;
$ . each ( this . attributes , function ( index , attr ) {
var value = me . attribute _values [ attr . attribute ] || "" ;
if ( value ) {
attribute [ attr . attribute ] = attr . numeric _values ? flt ( value ) : value ;
} else {
mandatory . push ( attr . attribute ) ;
}
} )
if ( this . is _manufacturer ) {
$ . each ( this . manufacturer _fields , function ( index , field ) {
attribute [ field . fieldname ] = me . dialog . fields _dict [ field . fieldname ] . input . value ;
} ) ;
}
return attribute ;
}
} ;