2013-11-20 12:59:58 +05:30
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
2013-08-05 14:59:54 +05:30
// License: GNU General Public License v3. See license.txt
2012-02-23 12:35:32 +05:30
2011-06-08 14:37:15 +05:30
2014-02-14 15:47:51 +05:30
frappe . provide ( "erpnext.selling" ) ;
2015-01-22 17:13:13 +05:30
frappe . require ( "assets/erpnext/js/controllers/transaction.js" ) ;
2014-01-02 11:47:23 +05:30
2015-01-22 17:13:13 +05:30
{ % include "public/js/controllers/accounts.js" % } ;
2013-05-15 21:15:57 +05:30
2014-09-12 13:09:41 +05:30
cur _frm . email _field = "contact_email" ;
2013-05-24 19:25:01 +05:30
erpnext . selling . SellingController = erpnext . TransactionController . extend ( {
2013-07-11 19:13:58 +05:30
onload : function ( ) {
this . _super ( ) ;
this . setup _queries ( ) ;
2013-08-02 12:39:10 +05:30
this . toggle _editable _price _list _rate ( ) ;
2013-07-11 19:13:58 +05:30
} ,
2014-04-11 16:51:27 +05:30
2014-06-04 13:10:41 +05:30
onload _post _render : function ( ) {
2014-12-26 13:15:21 +05:30
cur _frm . get _field ( "items" ) . grid . set _multiple _add ( "item_code" , "qty" ) ;
2014-06-04 13:10:41 +05:30
} ,
2013-07-11 19:13:58 +05:30
setup _queries : function ( ) {
2013-07-04 17:13:53 +05:30
var me = this ;
2014-04-11 16:51:27 +05:30
2013-05-21 19:35:06 +05:30
this . frm . add _fetch ( "sales_partner" , "commission_rate" , "commission_rate" ) ;
2014-04-11 16:51:27 +05:30
$ . each ( [ [ "customer_address" , "customer_filter" ] ,
2013-07-29 13:28:37 +05:30
[ "shipping_address_name" , "customer_filter" ] ,
2014-04-11 16:51:27 +05:30
[ "contact_person" , "customer_filter" ] ,
[ "customer" , "customer" ] ,
[ "lead" , "lead" ] ] ,
2013-07-29 13:28:37 +05:30
function ( i , opts ) {
2014-04-11 16:51:27 +05:30
if ( me . frm . fields _dict [ opts [ 0 ] ] )
2013-07-29 13:28:37 +05:30
me . frm . set _query ( opts [ 0 ] , erpnext . queries [ opts [ 1 ] ] ) ;
} ) ;
2014-04-11 16:51:27 +05:30
2014-01-29 16:31:38 +05:30
if ( this . frm . fields _dict . taxes _and _charges ) {
this . frm . set _query ( "taxes_and_charges" , function ( ) {
2013-07-10 13:07:49 +05:30
return {
filters : [
[ 'Sales Taxes and Charges Master' , 'company' , '=' , me . frm . doc . company ] ,
[ 'Sales Taxes and Charges Master' , 'docstatus' , '!=' , 2 ]
]
}
2013-07-04 17:13:53 +05:30
} ) ;
}
2013-08-09 18:11:35 +05:30
if ( this . frm . fields _dict . selling _price _list ) {
this . frm . set _query ( "selling_price_list" , function ( ) {
2014-01-07 18:37:38 +05:30
return { filters : { selling : 1 } } ;
2013-07-26 11:32:02 +05:30
} ) ;
}
2014-04-11 16:51:27 +05:30
2014-12-26 13:15:21 +05:30
if ( ! this . frm . fields _dict [ "items" ] ) {
2013-07-04 17:13:53 +05:30
return ;
}
2014-04-11 16:51:27 +05:30
2014-12-26 13:15:21 +05:30
if ( this . frm . fields _dict [ "items" ] . grid . get _field ( 'item_code' ) ) {
this . frm . set _query ( "item_code" , "items" , function ( ) {
2013-07-11 19:13:58 +05:30
return {
2013-12-13 14:10:14 +05:30
query : "erpnext.controllers.queries.item_query" ,
2013-07-11 19:13:58 +05:30
filters : ( me . frm . doc . order _type === "Maintenance" ?
{ 'is_service_item' : 'Yes' } :
{ 'is_sales_item' : 'Yes' } )
}
2013-07-04 17:13:53 +05:30
} ) ;
}
2014-04-11 16:51:27 +05:30
2014-12-26 13:15:21 +05:30
if ( this . frm . fields _dict [ "items" ] . grid . get _field ( 'batch_no' ) ) {
this . frm . set _query ( "batch_no" , "items" , function ( doc , cdt , cdn ) {
2014-03-27 14:17:33 +05:30
var item = frappe . get _doc ( cdt , cdn ) ;
2013-07-04 17:13:53 +05:30
if ( ! item . item _code ) {
2014-04-14 16:25:30 +05:30
frappe . throw ( _ _ ( "Please enter Item Code to get batch no" ) ) ;
2013-07-04 17:13:53 +05:30
} else {
2013-10-18 12:29:11 +05:30
filters = {
'item_code' : item . item _code ,
'posting_date' : me . frm . doc . posting _date ,
}
if ( item . warehouse ) filters [ "warehouse" ] = item . warehouse
2014-04-11 16:51:27 +05:30
2013-10-18 12:29:11 +05:30
return {
2014-01-29 16:31:38 +05:30
query : "erpnext.controllers.queries.get_batch_no" ,
2013-10-18 12:29:11 +05:30
filters : filters
2013-07-04 17:13:53 +05:30
}
}
} ) ;
}
2014-04-11 16:51:27 +05:30
2013-07-23 15:16:50 +05:30
if ( this . frm . fields _dict . sales _team && this . frm . fields _dict . sales _team . grid . get _field ( "sales_person" ) ) {
2013-07-29 13:28:37 +05:30
this . frm . set _query ( "sales_person" , "sales_team" , erpnext . queries . not _a _group _filter ) ;
2013-07-23 15:16:50 +05:30
}
2013-05-15 21:15:57 +05:30
} ,
2014-04-11 16:51:27 +05:30
2013-08-01 18:19:51 +05:30
refresh : function ( ) {
this . _super ( ) ;
2014-04-11 16:51:27 +05:30
this . frm . toggle _display ( "customer_name" ,
2013-12-09 16:29:04 +05:30
( this . frm . doc . customer _name && this . frm . doc . customer _name !== this . frm . doc . customer ) ) ;
2014-12-25 16:01:55 +05:30
if ( this . frm . fields _dict . packed _items ) {
var packing _list _exists = ( this . frm . doc . packed _items || [ ] ) . length ;
2013-08-01 18:19:51 +05:30
this . frm . toggle _display ( "packing_list" , packing _list _exists ? true : false ) ;
}
2013-07-04 12:50:52 +05:30
} ,
2014-04-11 16:51:27 +05:30
2013-05-24 19:25:01 +05:30
customer : function ( ) {
2014-05-28 12:49:20 +05:30
var me = this ;
erpnext . utils . get _party _details ( this . frm , null , null , function ( ) { me . apply _pricing _rule ( ) } ) ;
2013-05-24 19:25:01 +05:30
} ,
2014-04-11 16:51:27 +05:30
2013-07-15 13:04:33 +05:30
customer _address : function ( ) {
2014-01-02 11:47:23 +05:30
erpnext . utils . get _address _display ( this . frm , "customer_address" ) ;
2013-07-15 13:04:33 +05:30
} ,
2014-04-11 16:51:27 +05:30
2014-02-19 17:43:24 +05:30
shipping _address _name : function ( ) {
erpnext . utils . get _address _display ( this . frm , "shipping_address_name" , "shipping_address" ) ;
} ,
2014-04-11 16:51:27 +05:30
2013-07-15 13:04:33 +05:30
contact _person : function ( ) {
2014-01-02 11:47:23 +05:30
erpnext . utils . get _contact _details ( this . frm ) ;
2013-07-15 13:04:33 +05:30
} ,
2014-04-11 16:51:27 +05:30
2014-05-28 12:49:20 +05:30
sales _partner : function ( ) {
this . apply _pricing _rule ( ) ;
} ,
campaign : function ( ) {
this . apply _pricing _rule ( ) ;
} ,
2013-05-21 19:35:06 +05:30
barcode : function ( doc , cdt , cdn ) {
this . item _code ( doc , cdt , cdn ) ;
2013-05-15 21:15:57 +05:30
} ,
2014-04-11 16:51:27 +05:30
2013-08-09 18:11:35 +05:30
selling _price _list : function ( ) {
2014-07-01 17:45:15 +05:30
this . apply _price _list ( ) ;
2013-05-21 19:35:06 +05:30
} ,
2014-04-11 16:51:27 +05:30
2014-02-10 17:54:04 +05:30
price _list _rate : function ( doc , cdt , cdn ) {
2014-03-27 14:17:33 +05:30
var item = frappe . get _doc ( cdt , cdn ) ;
2014-02-14 15:47:51 +05:30
frappe . model . round _floats _in ( item , [ "price_list_rate" , "discount_percentage" ] ) ;
2014-04-11 16:51:27 +05:30
2014-02-10 18:26:49 +05:30
item . rate = flt ( item . price _list _rate * ( 1 - item . discount _percentage / 100.0 ) ,
precision ( "rate" , item ) ) ;
2014-04-11 16:51:27 +05:30
2013-05-21 19:35:06 +05:30
this . calculate _taxes _and _totals ( ) ;
} ,
2014-04-11 16:51:27 +05:30
2014-02-10 17:54:04 +05:30
discount _percentage : function ( doc , cdt , cdn ) {
2014-03-27 14:17:33 +05:30
var item = frappe . get _doc ( cdt , cdn ) ;
2014-02-10 17:54:04 +05:30
if ( ! item . price _list _rate ) {
item . discount _percentage = 0.0 ;
2013-05-28 17:23:36 +05:30
} else {
2014-02-10 17:54:04 +05:30
this . price _list _rate ( doc , cdt , cdn ) ;
2013-05-28 17:23:36 +05:30
}
2013-05-21 19:35:06 +05:30
} ,
2014-04-11 16:51:27 +05:30
2014-02-10 18:26:49 +05:30
rate : function ( doc , cdt , cdn ) {
2014-03-27 14:17:33 +05:30
var item = frappe . get _doc ( cdt , cdn ) ;
2014-02-14 15:47:51 +05:30
frappe . model . round _floats _in ( item , [ "rate" , "price_list_rate" ] ) ;
2014-04-11 16:51:27 +05:30
2014-02-10 17:54:04 +05:30
if ( item . price _list _rate ) {
2014-02-10 18:26:49 +05:30
item . discount _percentage = flt ( ( 1 - item . rate / item . price _list _rate ) * 100.0 ,
2014-02-10 17:54:04 +05:30
precision ( "discount_percentage" , item ) ) ;
2013-05-21 19:35:06 +05:30
} else {
2014-02-10 17:54:04 +05:30
item . discount _percentage = 0.0 ;
2013-05-21 19:35:06 +05:30
}
2014-04-11 16:51:27 +05:30
2013-05-21 19:35:06 +05:30
this . calculate _taxes _and _totals ( ) ;
} ,
2013-12-23 15:49:08 +05:30
2014-01-03 18:15:07 +05:30
discount _amount : function ( ) {
2013-12-23 15:49:08 +05:30
this . calculate _taxes _and _totals ( ) ;
} ,
2014-04-11 16:51:27 +05:30
2013-05-21 19:35:06 +05:30
commission _rate : function ( ) {
this . calculate _commission ( ) ;
refresh _field ( "total_commission" ) ;
} ,
2014-04-11 16:51:27 +05:30
2013-05-21 19:35:06 +05:30
total _commission : function ( ) {
2015-02-12 16:09:11 +05:30
if ( this . frm . doc . base _net _total ) {
frappe . model . round _floats _in ( this . frm . doc , [ "base_net_total" , "total_commission" ] ) ;
2014-04-11 16:51:27 +05:30
2015-02-12 16:09:11 +05:30
if ( this . frm . doc . base _net _total < this . frm . doc . total _commission ) {
2014-04-14 16:25:30 +05:30
var msg = ( _ _ ( "[Error]" ) + " " +
_ _ ( frappe . meta . get _label ( this . frm . doc . doctype , "total_commission" ,
2014-04-11 16:51:27 +05:30
this . frm . doc . name ) ) + " > " +
2015-02-12 16:09:11 +05:30
_ _ ( frappe . meta . get _label ( this . frm . doc . doctype , "base_net_total" , this . frm . doc . name ) ) ) ;
2013-05-21 19:35:06 +05:30
msgprint ( msg ) ;
throw msg ;
}
2014-04-11 16:51:27 +05:30
this . frm . set _value ( "commission_rate" ,
2015-02-12 16:09:11 +05:30
flt ( this . frm . doc . total _commission * 100.0 / this . frm . doc . base _net _total ) ) ;
2013-05-21 19:35:06 +05:30
}
} ,
2014-04-11 16:51:27 +05:30
2013-05-21 19:35:06 +05:30
allocated _percentage : function ( doc , cdt , cdn ) {
2014-03-27 14:17:33 +05:30
var sales _person = frappe . get _doc ( cdt , cdn ) ;
2014-04-11 16:51:27 +05:30
2013-05-21 19:35:06 +05:30
if ( sales _person . allocated _percentage ) {
sales _person . allocated _percentage = flt ( sales _person . allocated _percentage ,
precision ( "allocated_percentage" , sales _person ) ) ;
2015-02-12 16:09:11 +05:30
sales _person . allocated _amount = flt ( this . frm . doc . base _net _total *
2014-04-11 16:51:27 +05:30
sales _person . allocated _percentage / 100.0 ,
2013-05-21 19:35:06 +05:30
precision ( "allocated_amount" , sales _person ) ) ;
refresh _field ( [ "allocated_percentage" , "allocated_amount" ] , sales _person . name ,
sales _person . parentfield ) ;
}
} ,
2014-04-11 16:51:27 +05:30
2013-05-27 19:29:07 +05:30
warehouse : function ( doc , cdt , cdn ) {
2014-03-27 14:17:33 +05:30
var item = frappe . get _doc ( cdt , cdn ) ;
2013-07-23 13:36:38 +05:30
if ( item . item _code && item . warehouse ) {
2013-07-29 19:30:39 +05:30
return this . frm . call ( {
2014-05-30 19:10:12 +05:30
method : "erpnext.stock.get_item_details.get_available_qty" ,
2013-05-27 19:29:07 +05:30
child : item ,
args : {
item _code : item . item _code ,
2013-07-23 13:36:38 +05:30
warehouse : item . warehouse ,
2013-05-27 19:29:07 +05:30
} ,
} ) ;
}
} ,
2014-04-11 16:51:27 +05:30
2013-08-02 12:39:10 +05:30
toggle _editable _price _list _rate : function ( ) {
2014-12-26 13:15:21 +05:30
var df = frappe . meta . get _docfield ( this . frm . doc . doctype + " Item" , "price_list_rate" , this . frm . doc . name ) ;
2014-02-14 15:47:51 +05:30
var editable _price _list _rate = cint ( frappe . defaults . get _default ( "editable_price_list_rate" ) ) ;
2014-04-11 16:51:27 +05:30
2013-08-02 12:39:10 +05:30
if ( df && editable _price _list _rate ) {
df . read _only = 0 ;
}
} ,
2014-04-11 16:51:27 +05:30
2014-04-16 15:21:46 +05:30
calculate _outstanding _amount : function ( update _paid _amount ) {
2014-04-11 16:51:27 +05:30
// NOTE:
2013-09-05 12:59:33 +05:30
// paid_amount and write_off_amount is only for POS Invoice
2013-05-28 17:23:36 +05:30
// total_advance is only for non POS Invoice
2013-07-05 17:23:14 +05:30
if ( this . frm . doc . doctype == "Sales Invoice" && this . frm . doc . docstatus == 0 ) {
2015-02-12 16:09:11 +05:30
frappe . model . round _floats _in ( this . frm . doc , [ "base_grand_total" , "total_advance" , "write_off_amount" ,
2014-03-13 17:26:41 +05:30
"paid_amount" ] ) ;
2015-02-12 16:09:11 +05:30
var total _amount _to _pay = this . frm . doc . base _grand _total - this . frm . doc . write _off _amount
2014-03-13 17:26:41 +05:30
- this . frm . doc . total _advance ;
2014-03-25 19:35:41 +05:30
if ( this . frm . doc . is _pos ) {
2014-04-16 15:21:46 +05:30
if ( ! this . frm . doc . paid _amount || update _paid _amount === undefined || update _paid _amount ) {
this . frm . doc . paid _amount = flt ( total _amount _to _pay ) ;
2015-01-06 17:14:13 +05:30
this . frm . refresh _field ( "paid_amount" ) ;
2014-04-16 15:21:46 +05:30
}
2014-03-25 19:35:41 +05:30
} else {
this . frm . doc . paid _amount = 0
2015-01-06 17:14:13 +05:30
this . frm . refresh _field ( "paid_amount" ) ;
2014-03-25 19:35:41 +05:30
}
2014-03-13 16:32:43 +05:30
2014-04-11 16:51:27 +05:30
this . frm . set _value ( "outstanding_amount" , flt ( total _amount _to _pay
2014-03-13 17:26:41 +05:30
- this . frm . doc . paid _amount , precision ( "outstanding_amount" ) ) ) ;
2013-05-27 19:29:07 +05:30
}
} ,
2014-04-11 16:51:27 +05:30
2013-05-21 19:35:06 +05:30
calculate _commission : function ( ) {
2013-05-28 17:23:36 +05:30
if ( this . frm . fields _dict . commission _rate ) {
if ( this . frm . doc . commission _rate > 100 ) {
2014-04-14 16:25:30 +05:30
var msg = _ _ ( frappe . meta . get _label ( this . frm . doc . doctype , "commission_rate" , this . frm . doc . name ) ) +
" " + _ _ ( "cannot be greater than 100" ) ;
2013-05-28 17:23:36 +05:30
msgprint ( msg ) ;
throw msg ;
}
2014-04-11 16:51:27 +05:30
2015-02-12 16:09:11 +05:30
this . frm . doc . total _commission = flt ( this . frm . doc . base _net _total * this . frm . doc . commission _rate / 100.0 ,
2013-05-28 17:23:36 +05:30
precision ( "total_commission" ) ) ;
}
2013-05-21 19:35:06 +05:30
} ,
2014-04-11 16:51:27 +05:30
2013-05-21 19:35:06 +05:30
calculate _contribution : function ( ) {
2013-05-23 19:25:08 +05:30
var me = this ;
2014-03-26 18:24:30 +05:30
$ . each ( this . frm . doc . doctype . sales _team || [ ] , function ( i , sales _person ) {
2014-02-14 15:47:51 +05:30
frappe . model . round _floats _in ( sales _person ) ;
2013-05-21 19:35:06 +05:30
if ( sales _person . allocated _percentage ) {
sales _person . allocated _amount = flt (
2015-02-12 16:09:11 +05:30
me . frm . doc . base _net _total * sales _person . allocated _percentage / 100.0 ,
2013-05-21 19:35:06 +05:30
precision ( "allocated_amount" , sales _person ) ) ;
}
} ) ;
} ,
2014-04-11 16:51:27 +05:30
2013-07-15 18:28:14 +05:30
shipping _rule : function ( ) {
var me = this ;
if ( this . frm . doc . shipping _rule ) {
2013-07-29 19:30:39 +05:30
return this . frm . call ( {
2013-07-15 18:28:14 +05:30
doc : this . frm . doc ,
method : "apply_shipping_rule" ,
callback : function ( r ) {
if ( ! r . exc ) {
me . calculate _taxes _and _totals ( ) ;
}
}
} )
}
} ,
2014-04-11 16:51:27 +05:30
2013-05-23 19:25:08 +05:30
set _dynamic _labels : function ( ) {
2013-06-21 17:55:31 +05:30
this . _super ( ) ;
2014-06-24 17:02:45 +05:30
this . set _sales _bom _help ( this . frm . doc ) ;
} ,
set _sales _bom _help : function ( doc ) {
if ( ! cur _frm . fields _dict . packing _list ) return ;
2014-12-25 16:01:55 +05:30
if ( ( doc . packed _items || [ ] ) . length ) {
2014-06-24 17:02:45 +05:30
$ ( cur _frm . fields _dict . packing _list . row . wrapper ) . toggle ( true ) ;
if ( inList ( [ 'Delivery Note' , 'Sales Invoice' ] , doc . doctype ) ) {
help _msg = "<div class='alert alert-warning'>" +
2014-06-24 17:34:52 +05:30
_ _ ( "For 'Sales BOM' items, Warehouse, Serial No and Batch No will be considered from the 'Packing List' table. If Warehouse and Batch No are same for all packing items for any 'Sales BOM' item, those values can be entered in the main Item table, values will be copied to 'Packing List' table." ) +
2014-06-24 17:02:45 +05:30
"</div>" ;
frappe . meta . get _docfield ( doc . doctype , 'sales_bom_help' , doc . name ) . options = help _msg ;
}
} else {
$ ( cur _frm . fields _dict . packing _list . row . wrapper ) . toggle ( false ) ;
if ( inList ( [ 'Delivery Note' , 'Sales Invoice' ] , doc . doctype ) ) {
frappe . meta . get _docfield ( doc . doctype , 'sales_bom_help' , doc . name ) . options = '' ;
}
}
refresh _field ( 'sales_bom_help' ) ;
2013-05-23 19:25:08 +05:30
} ,
2014-04-11 16:51:27 +05:30
2013-05-23 19:25:08 +05:30
change _form _labels : function ( company _currency ) {
var me = this ;
var field _label _map = { } ;
2014-04-11 16:51:27 +05:30
2013-05-23 19:25:08 +05:30
var setup _field _label _map = function ( fields _list , currency ) {
$ . each ( fields _list , function ( i , fname ) {
2014-02-14 15:47:51 +05:30
var docfield = frappe . meta . docfield _map [ me . frm . doc . doctype ] [ fname ] ;
2013-05-23 19:25:08 +05:30
if ( docfield ) {
2014-04-14 16:25:30 +05:30
var label = _ _ ( docfield . label || "" ) . replace ( /\([^\)]*\)/g , "" ) ;
2013-05-23 19:25:08 +05:30
field _label _map [ fname ] = label . trim ( ) + " (" + currency + ")" ;
}
} ) ;
} ;
2015-02-12 16:09:11 +05:30
setup _field _label _map ( [ "base_net_total" , "base_total_taxes_and_charges" , "base_discount_amount" , "base_grand_total" ,
"base_rounded_total" , "base_in_words" ,
2013-05-23 19:25:08 +05:30
"outstanding_amount" , "total_advance" , "paid_amount" , "write_off_amount" ] ,
company _currency ) ;
2014-04-11 16:51:27 +05:30
2015-02-12 16:09:11 +05:30
setup _field _label _map ( [ "net_total" , "total_taxes_and_charges" , "discount_amount" , "grand_total" ,
"rounded_total" , "in_words" ] , this . frm . doc . currency ) ;
2014-04-11 16:51:27 +05:30
cur _frm . set _df _property ( "conversion_rate" , "description" , "1 " + this . frm . doc . currency
2013-11-02 14:47:11 +05:30
+ " = [?] " + company _currency )
2014-04-11 16:51:27 +05:30
2013-05-23 19:25:08 +05:30
if ( this . frm . doc . price _list _currency && this . frm . doc . price _list _currency != company _currency ) {
2014-04-11 16:51:27 +05:30
cur _frm . set _df _property ( "plc_conversion_rate" , "description" , "1 " + this . frm . doc . price _list _currency
2013-11-02 14:47:11 +05:30
+ " = [?] " + company _currency )
2013-05-23 19:25:08 +05:30
}
2014-04-11 16:51:27 +05:30
2013-05-23 19:25:08 +05:30
// toggle fields
2015-02-12 16:09:11 +05:30
this . frm . toggle _display ( [ "conversion_rate" , "base_net_total" , "base_total_taxes_and_charges" ,
"base_grand_total" , "base_rounded_total" , "base_in_words" , "base_discount_amount" ] ,
2013-05-23 19:25:08 +05:30
this . frm . doc . currency != company _currency ) ;
2014-04-11 16:51:27 +05:30
this . frm . toggle _display ( [ "plc_conversion_rate" , "price_list_currency" ] ,
2013-05-23 19:25:08 +05:30
this . frm . doc . price _list _currency != company _currency ) ;
2014-04-11 16:51:27 +05:30
2013-05-23 19:25:08 +05:30
// set labels
$ . each ( field _label _map , function ( fname , label ) {
me . frm . fields _dict [ fname ] . set _label ( label ) ;
} ) ;
} ,
2014-04-11 16:51:27 +05:30
2013-05-23 19:25:08 +05:30
change _grid _labels : function ( company _currency ) {
var me = this ;
var field _label _map = { } ;
2014-04-11 16:51:27 +05:30
2013-05-23 19:25:08 +05:30
var setup _field _label _map = function ( fields _list , currency , parentfield ) {
var grid _doctype = me . frm . fields _dict [ parentfield ] . grid . doctype ;
$ . each ( fields _list , function ( i , fname ) {
2014-02-14 15:47:51 +05:30
var docfield = frappe . meta . docfield _map [ grid _doctype ] [ fname ] ;
2013-05-23 19:25:08 +05:30
if ( docfield ) {
2014-04-14 16:25:30 +05:30
var label = _ _ ( docfield . label || "" ) . replace ( /\([^\)]*\)/g , "" ) ;
2014-04-11 16:51:27 +05:30
field _label _map [ grid _doctype + "-" + fname ] =
2013-05-23 19:25:08 +05:30
label . trim ( ) + " (" + currency + ")" ;
}
} ) ;
}
2014-04-11 16:51:27 +05:30
2014-02-10 19:20:15 +05:30
setup _field _label _map ( [ "base_rate" , "base_price_list_rate" , "base_amount" ] ,
2014-12-26 13:15:21 +05:30
company _currency , "items" ) ;
2014-04-11 16:51:27 +05:30
2014-02-10 19:20:15 +05:30
setup _field _label _map ( [ "rate" , "price_list_rate" , "amount" ] ,
2014-12-26 13:15:21 +05:30
this . frm . doc . currency , "items" ) ;
2014-04-11 16:51:27 +05:30
2014-12-25 16:01:55 +05:30
setup _field _label _map ( [ "tax_amount" , "total" ] , company _currency , "taxes" ) ;
2014-04-11 16:51:27 +05:30
2014-12-25 16:01:55 +05:30
if ( this . frm . fields _dict [ "advances" ] ) {
2013-05-23 19:25:08 +05:30
setup _field _label _map ( [ "advance_amount" , "allocated_amount" ] , company _currency ,
2014-12-25 16:01:55 +05:30
"advances" ) ;
2013-05-23 19:25:08 +05:30
}
2014-04-11 16:51:27 +05:30
2013-05-23 19:25:08 +05:30
// toggle columns
2014-12-26 13:15:21 +05:30
var item _grid = this . frm . fields _dict [ "items" ] . grid ;
2014-04-11 16:51:27 +05:30
var show = ( this . frm . doc . currency != company _currency ) ||
2014-12-25 16:01:55 +05:30
( ( cur _frm . doc . taxes || [ ] ) . filter (
2014-03-26 18:24:30 +05:30
function ( d ) { return d . included _in _print _rate === 1 } ) . length ) ;
2014-04-11 16:51:27 +05:30
2014-02-10 19:20:15 +05:30
$ . each ( [ "base_rate" , "base_price_list_rate" , "base_amount" ] , function ( i , fname ) {
2014-02-14 15:47:51 +05:30
if ( frappe . meta . get _docfield ( item _grid . doctype , fname ) )
2013-05-23 19:25:08 +05:30
item _grid . set _column _disp ( fname , show ) ;
} ) ;
2014-04-11 16:51:27 +05:30
2013-05-23 19:25:08 +05:30
// set labels
var $wrapper = $ ( this . frm . wrapper ) ;
$ . each ( field _label _map , function ( fname , label ) {
2013-08-05 12:16:04 +05:30
fname = fname . split ( "-" ) ;
2014-02-14 15:47:51 +05:30
var df = frappe . meta . get _docfield ( fname [ 0 ] , fname [ 1 ] , me . frm . doc . name ) ;
2013-08-05 12:16:04 +05:30
if ( df ) df . label = label ;
2013-05-23 19:25:08 +05:30
} ) ;
2012-07-11 13:14:52 +05:30
}
2014-06-24 17:02:45 +05:30
} ) ;
2014-08-26 14:25:53 +05:30
frappe . ui . form . on ( cur _frm . doctype , "project_name" , function ( frm ) {
2014-10-23 13:18:59 +05:30
if ( in _list ( [ "Delivery Note" , "Sales Invoice" ] , frm . doc . doctype ) ) {
frappe . call ( {
method : 'erpnext.projects.doctype.project.project.get_cost_center_name' ,
args : { project _name : frm . doc . project _name } ,
callback : function ( r , rt ) {
if ( ! r . exc ) {
2014-12-26 13:15:21 +05:30
$ . each ( frm . doc [ "items" ] || [ ] , function ( i , row ) {
2014-10-28 14:36:26 +05:30
frappe . model . set _value ( row . doctype , row . name , "cost_center" , r . message ) ;
msgprint ( _ _ ( "Cost Center For Item with Item Code '" + row . item _name + "' has been Changed to " + r . message ) ) ;
2014-10-23 13:18:59 +05:30
} )
}
2014-08-26 14:25:53 +05:30
}
2014-10-23 13:18:59 +05:30
} )
}
2014-08-26 14:25:53 +05:30
} )