2015-03-03 09:25:30 +00:00
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2013-08-05 09:29:54 +00:00
// License: GNU General Public License v3. See license.txt
2012-02-23 07:05:32 +00:00
2011-06-08 09:07:15 +00:00
2015-02-23 06:28:15 +00:00
cur _frm . cscript . tax _table = "Sales Taxes and Charges" ;
2016-04-21 13:30:34 +00:00
{ % include 'erpnext/accounts/doctype/sales_taxes_and_charges_template/sales_taxes_and_charges_template.js' % }
2015-02-23 06:28:15 +00:00
2014-01-02 06:17:23 +00:00
2014-09-12 07:39:41 +00:00
cur _frm . email _field = "contact_email" ;
2016-04-29 11:52:42 +00:00
frappe . provide ( "erpnext.selling" ) ;
2013-05-24 13:55:01 +00:00
erpnext . selling . SellingController = erpnext . TransactionController . extend ( {
2016-05-19 12:52:49 +00:00
setup : function ( ) {
this . _super ( ) ;
} ,
2013-07-11 13:43:58 +00:00
onload : function ( ) {
this . _super ( ) ;
this . setup _queries ( ) ;
} ,
2014-04-11 11:21:27 +00:00
2013-07-11 13:43:58 +00:00
setup _queries : function ( ) {
2016-05-19 12:52:49 +00:00
2013-07-04 11:43:53 +00:00
var me = this ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
this . frm . add _fetch ( "sales_partner" , "commission_rate" , "commission_rate" ) ;
2014-04-11 11:21:27 +00:00
$ . each ( [ [ "customer_address" , "customer_filter" ] ,
2013-07-29 07:58:37 +00:00
[ "shipping_address_name" , "customer_filter" ] ,
2014-04-11 11:21:27 +00:00
[ "contact_person" , "customer_filter" ] ,
[ "customer" , "customer" ] ,
[ "lead" , "lead" ] ] ,
2013-07-29 07:58:37 +00:00
function ( i , opts ) {
2014-04-11 11:21:27 +00:00
if ( me . frm . fields _dict [ opts [ 0 ] ] )
2013-07-29 07:58:37 +00:00
me . frm . set _query ( opts [ 0 ] , erpnext . queries [ opts [ 1 ] ] ) ;
} ) ;
2014-04-11 11:21:27 +00:00
2014-01-29 11:01:38 +00:00
if ( this . frm . fields _dict . taxes _and _charges ) {
this . frm . set _query ( "taxes_and_charges" , function ( ) {
2013-07-10 07:37:49 +00:00
return {
filters : [
2015-05-08 05:38:48 +00:00
[ 'Sales Taxes and Charges Template' , 'company' , '=' , me . frm . doc . company ] ,
[ 'Sales Taxes and Charges Template' , 'docstatus' , '!=' , 2 ]
2013-07-10 07:37:49 +00:00
]
}
2013-07-04 11:43:53 +00:00
} ) ;
}
2013-08-09 12:41:35 +00:00
if ( this . frm . fields _dict . selling _price _list ) {
this . frm . set _query ( "selling_price_list" , function ( ) {
2014-01-07 13:07:38 +00:00
return { filters : { selling : 1 } } ;
2013-07-26 06:02:02 +00:00
} ) ;
}
2014-04-11 11:21:27 +00:00
2014-12-26 07:45:21 +00:00
if ( ! this . frm . fields _dict [ "items" ] ) {
2013-07-04 11:43:53 +00:00
return ;
}
2014-04-11 11:21:27 +00:00
2014-12-26 07:45:21 +00:00
if ( this . frm . fields _dict [ "items" ] . grid . get _field ( 'item_code' ) ) {
this . frm . set _query ( "item_code" , "items" , function ( ) {
2013-07-11 13:43:58 +00:00
return {
2016-08-16 06:02:08 +00:00
query : "erpnext.controllers.queries.item_query" ,
filters : { 'is_sales_item' : 1 }
2013-07-11 13:43:58 +00:00
}
2013-07-04 11:43:53 +00:00
} ) ;
}
2014-04-11 11:21:27 +00:00
2016-11-22 17:46:40 +00:00
if ( this . frm . fields _dict [ "packed_items" ] &&
this . frm . fields _dict [ "packed_items" ] . grid . get _field ( 'batch_no' ) ) {
2016-11-18 06:46:22 +00:00
this . frm . set _query ( "batch_no" , "packed_items" , function ( doc , cdt , cdn ) {
return me . set _query _for _batch ( doc , cdt , cdn )
} ) ;
}
2014-12-26 07:45:21 +00:00
if ( this . frm . fields _dict [ "items" ] . grid . get _field ( 'batch_no' ) ) {
this . frm . set _query ( "batch_no" , "items" , function ( doc , cdt , cdn ) {
2016-11-18 06:46:22 +00:00
return me . set _query _for _batch ( doc , cdt , cdn )
2013-07-04 11:43:53 +00:00
} ) ;
}
2013-05-15 15:45:57 +00:00
} ,
2014-04-11 11:21:27 +00:00
2016-11-18 06:46:22 +00:00
set _query _for _batch : function ( doc , cdt , cdn ) {
// Show item's batches in the dropdown of batch no
var me = this ;
var item = frappe . get _doc ( cdt , cdn ) ;
if ( ! item . item _code ) {
frappe . throw ( _ _ ( "Please enter Item Code to get batch no" ) ) ;
} else {
filters = {
'item_code' : item . item _code ,
'posting_date' : me . frm . doc . posting _date || frappe . datetime . nowdate ( ) ,
}
if ( item . warehouse ) filters [ "warehouse" ] = item . warehouse
return {
query : "erpnext.controllers.queries.get_batch_no" ,
filters : filters
}
}
} ,
2013-08-01 12:49:51 +00:00
refresh : function ( ) {
this . _super ( ) ;
2014-04-11 11:21:27 +00:00
this . frm . toggle _display ( "customer_name" ,
2013-12-09 10:59:04 +00:00
( this . frm . doc . customer _name && this . frm . doc . customer _name !== this . frm . doc . customer ) ) ;
2014-12-25 10:31:55 +00:00
if ( this . frm . fields _dict . packed _items ) {
var packing _list _exists = ( this . frm . doc . packed _items || [ ] ) . length ;
2013-08-01 12:49:51 +00:00
this . frm . toggle _display ( "packing_list" , packing _list _exists ? true : false ) ;
}
2015-04-16 12:25:35 +00:00
this . toggle _editable _price _list _rate ( ) ;
2013-07-04 07:20:52 +00:00
} ,
2014-04-11 11:21:27 +00:00
2013-05-24 13:55:01 +00:00
customer : function ( ) {
2014-05-28 07:19:20 +00:00
var me = this ;
erpnext . utils . get _party _details ( this . frm , null , null , function ( ) { me . apply _pricing _rule ( ) } ) ;
2013-05-24 13:55:01 +00:00
} ,
2014-04-11 11:21:27 +00:00
2013-07-15 07:34:33 +00:00
customer _address : function ( ) {
2014-01-02 06:17:23 +00:00
erpnext . utils . get _address _display ( this . frm , "customer_address" ) ;
2013-07-15 07:34:33 +00:00
} ,
2014-04-11 11:21:27 +00:00
2014-02-19 12:13:24 +00:00
shipping _address _name : function ( ) {
erpnext . utils . get _address _display ( this . frm , "shipping_address_name" , "shipping_address" ) ;
} ,
2014-04-11 11:21:27 +00:00
2014-05-28 07:19:20 +00:00
sales _partner : function ( ) {
this . apply _pricing _rule ( ) ;
} ,
campaign : function ( ) {
this . apply _pricing _rule ( ) ;
} ,
2013-08-09 12:41:35 +00:00
selling _price _list : function ( ) {
2014-07-01 12:15:15 +00:00
this . apply _price _list ( ) ;
2013-05-21 14:05:06 +00:00
} ,
2014-04-11 11:21:27 +00:00
2014-02-10 12:24:04 +00:00
price _list _rate : function ( doc , cdt , cdn ) {
2014-03-27 08:47:33 +00:00
var item = frappe . get _doc ( cdt , cdn ) ;
2014-02-14 10:17:51 +00:00
frappe . model . round _floats _in ( item , [ "price_list_rate" , "discount_percentage" ] ) ;
2014-04-11 11:21:27 +00:00
2016-03-05 09:40:25 +00:00
// check if child doctype is Sales Order Item/Qutation Item and calculate the rate
if ( in _list ( [ "Quotation Item" , "Sales Order Item" , "Delivery Note Item" , "Sales Invoice Item" ] ) , cdt )
2016-04-09 09:01:09 +00:00
this . apply _pricing _rule _on _item ( item ) ;
2016-03-05 09:40:25 +00:00
else
item . rate = flt ( item . price _list _rate * ( 1 - item . discount _percentage / 100.0 ) ,
precision ( "rate" , item ) ) ;
2013-05-21 14:05:06 +00:00
this . calculate _taxes _and _totals ( ) ;
} ,
2014-04-11 11:21:27 +00:00
2014-02-10 12:24:04 +00:00
discount _percentage : function ( doc , cdt , cdn ) {
2014-03-27 08:47:33 +00:00
var item = frappe . get _doc ( cdt , cdn ) ;
2014-02-10 12:24:04 +00:00
if ( ! item . price _list _rate ) {
item . discount _percentage = 0.0 ;
2013-05-28 11:53:36 +00:00
} else {
2014-02-10 12:24:04 +00:00
this . price _list _rate ( doc , cdt , cdn ) ;
2013-05-28 11:53:36 +00:00
}
2016-02-26 12:32:55 +00:00
this . set _gross _profit ( item ) ;
2013-05-21 14:05:06 +00:00
} ,
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
commission _rate : function ( ) {
this . calculate _commission ( ) ;
refresh _field ( "total_commission" ) ;
} ,
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
total _commission : function ( ) {
2015-02-12 10:39:11 +00:00
if ( this . frm . doc . base _net _total ) {
frappe . model . round _floats _in ( this . frm . doc , [ "base_net_total" , "total_commission" ] ) ;
2014-04-11 11:21:27 +00:00
2015-02-12 10:39:11 +00:00
if ( this . frm . doc . base _net _total < this . frm . doc . total _commission ) {
2014-04-14 10:55:30 +00:00
var msg = ( _ _ ( "[Error]" ) + " " +
_ _ ( frappe . meta . get _label ( this . frm . doc . doctype , "total_commission" ,
2014-04-11 11:21:27 +00:00
this . frm . doc . name ) ) + " > " +
2015-02-12 10:39:11 +00:00
_ _ ( frappe . meta . get _label ( this . frm . doc . doctype , "base_net_total" , this . frm . doc . name ) ) ) ;
2013-05-21 14:05:06 +00:00
msgprint ( msg ) ;
throw msg ;
}
2014-04-11 11:21:27 +00:00
this . frm . set _value ( "commission_rate" ,
2015-02-12 10:39:11 +00:00
flt ( this . frm . doc . total _commission * 100.0 / this . frm . doc . base _net _total ) ) ;
2013-05-21 14:05:06 +00:00
}
} ,
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
allocated _percentage : function ( doc , cdt , cdn ) {
2014-03-27 08:47:33 +00:00
var sales _person = frappe . get _doc ( cdt , cdn ) ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
if ( sales _person . allocated _percentage ) {
sales _person . allocated _percentage = flt ( sales _person . allocated _percentage ,
precision ( "allocated_percentage" , sales _person ) ) ;
2015-02-12 10:39:11 +00:00
sales _person . allocated _amount = flt ( this . frm . doc . base _net _total *
2014-04-11 11:21:27 +00:00
sales _person . allocated _percentage / 100.0 ,
2013-05-21 14:05:06 +00:00
precision ( "allocated_amount" , sales _person ) ) ;
refresh _field ( [ "allocated_percentage" , "allocated_amount" ] , sales _person . name ,
sales _person . parentfield ) ;
}
} ,
2014-04-11 11:21:27 +00:00
2013-05-27 13:59:07 +00:00
warehouse : function ( doc , cdt , cdn ) {
2015-03-10 09:34:28 +00:00
var me = this ;
2014-03-27 08:47:33 +00:00
var item = frappe . get _doc ( cdt , cdn ) ;
2016-04-29 11:52:42 +00:00
2013-07-23 08:06:38 +00:00
if ( item . item _code && item . warehouse ) {
2013-07-29 14:00:39 +00:00
return this . frm . call ( {
2016-02-25 13:29:20 +00:00
method : "erpnext.stock.get_item_details.get_bin_details" ,
2013-05-27 13:59:07 +00:00
child : item ,
args : {
item _code : item . item _code ,
2013-07-23 08:06:38 +00:00
warehouse : item . warehouse ,
2013-05-27 13:59:07 +00:00
} ,
2016-02-25 13:29:20 +00:00
callback : function ( r ) {
if ( inList ( [ 'Delivery Note' , 'Sales Invoice' ] , doc . doctype ) ) {
me . batch _no ( doc , cdt , cdn ) ;
}
}
2013-05-27 13:59:07 +00:00
} ) ;
}
} ,
2014-04-11 11:21:27 +00:00
2013-08-02 07:09:10 +00:00
toggle _editable _price _list _rate : function ( ) {
2014-12-26 07:45:21 +00:00
var df = frappe . meta . get _docfield ( this . frm . doc . doctype + " Item" , "price_list_rate" , this . frm . doc . name ) ;
2014-02-14 10:17:51 +00:00
var editable _price _list _rate = cint ( frappe . defaults . get _default ( "editable_price_list_rate" ) ) ;
2014-04-11 11:21:27 +00:00
2013-08-02 07:09:10 +00:00
if ( df && editable _price _list _rate ) {
df . read _only = 0 ;
}
} ,
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
calculate _commission : function ( ) {
2013-05-28 11:53:36 +00:00
if ( this . frm . fields _dict . commission _rate ) {
if ( this . frm . doc . commission _rate > 100 ) {
2014-04-14 10:55:30 +00:00
var msg = _ _ ( frappe . meta . get _label ( this . frm . doc . doctype , "commission_rate" , this . frm . doc . name ) ) +
" " + _ _ ( "cannot be greater than 100" ) ;
2013-05-28 11:53:36 +00:00
msgprint ( msg ) ;
throw msg ;
}
2014-04-11 11:21:27 +00:00
2015-02-12 10:39:11 +00:00
this . frm . doc . total _commission = flt ( this . frm . doc . base _net _total * this . frm . doc . commission _rate / 100.0 ,
2013-05-28 11:53:36 +00:00
precision ( "total_commission" ) ) ;
}
2013-05-21 14:05:06 +00:00
} ,
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
calculate _contribution : function ( ) {
2013-05-23 13:55:08 +00:00
var me = this ;
2014-03-26 12:54:30 +00:00
$ . each ( this . frm . doc . doctype . sales _team || [ ] , function ( i , sales _person ) {
2014-02-14 10:17:51 +00:00
frappe . model . round _floats _in ( sales _person ) ;
2013-05-21 14:05:06 +00:00
if ( sales _person . allocated _percentage ) {
sales _person . allocated _amount = flt (
2015-02-12 10:39:11 +00:00
me . frm . doc . base _net _total * sales _person . allocated _percentage / 100.0 ,
2013-05-21 14:05:06 +00:00
precision ( "allocated_amount" , sales _person ) ) ;
}
} ) ;
} ,
2014-04-11 11:21:27 +00:00
2013-07-15 12:58:14 +00:00
shipping _rule : function ( ) {
var me = this ;
if ( this . frm . doc . shipping _rule ) {
2013-07-29 14:00:39 +00:00
return this . frm . call ( {
2013-07-15 12:58:14 +00:00
doc : this . frm . doc ,
method : "apply_shipping_rule" ,
callback : function ( r ) {
if ( ! r . exc ) {
me . calculate _taxes _and _totals ( ) ;
}
}
} )
}
} ,
2014-04-11 11:21:27 +00:00
2015-03-10 09:34:28 +00:00
batch _no : function ( doc , cdt , cdn ) {
var me = this ;
var item = frappe . get _doc ( cdt , cdn ) ;
2015-03-20 09:36:30 +00:00
2015-03-30 10:34:55 +00:00
if ( item . warehouse && item . item _code && item . batch _no ) {
2015-03-11 06:20:28 +00:00
return this . frm . call ( {
method : "erpnext.stock.get_item_details.get_batch_qty" ,
child : item ,
args : {
"batch_no" : item . batch _no ,
"warehouse" : item . warehouse ,
"item_code" : item . item _code
} ,
"fieldname" : "actual_batch_qty"
} ) ;
}
2015-03-10 09:34:28 +00:00
} ,
2013-05-23 13:55:08 +00:00
set _dynamic _labels : function ( ) {
2013-06-21 12:25:31 +00:00
this . _super ( ) ;
2015-07-07 08:29:23 +00:00
this . set _product _bundle _help ( this . frm . doc ) ;
2014-06-24 11:32:45 +00:00
} ,
2015-07-07 08:29:23 +00:00
set _product _bundle _help : function ( doc ) {
2014-06-24 11:32:45 +00:00
if ( ! cur _frm . fields _dict . packing _list ) return ;
2014-12-25 10:31:55 +00:00
if ( ( doc . packed _items || [ ] ) . length ) {
2014-06-24 11:32:45 +00:00
$ ( 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'>" +
2015-07-07 08:29:23 +00:00
_ _ ( "For 'Product Bundle' 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 'Product Bundle' item, those values can be entered in the main Item table, values will be copied to 'Packing List' table." ) +
2014-06-24 11:32:45 +00:00
"</div>" ;
2015-07-07 08:29:23 +00:00
frappe . meta . get _docfield ( doc . doctype , 'product_bundle_help' , doc . name ) . options = help _msg ;
2014-06-24 11:32:45 +00:00
}
} else {
$ ( cur _frm . fields _dict . packing _list . row . wrapper ) . toggle ( false ) ;
if ( inList ( [ 'Delivery Note' , 'Sales Invoice' ] , doc . doctype ) ) {
2015-07-07 08:29:23 +00:00
frappe . meta . get _docfield ( doc . doctype , 'product_bundle_help' , doc . name ) . options = '' ;
2014-06-24 11:32:45 +00:00
}
}
2015-07-07 08:29:23 +00:00
refresh _field ( 'product_bundle_help' ) ;
2015-12-16 04:27:59 +00:00
} ,
2016-01-26 10:52:50 +00:00
2015-12-16 04:27:59 +00:00
make _payment _request : function ( ) {
frappe . call ( {
method : "erpnext.accounts.doctype.payment_request.payment_request.make_payment_request" ,
args : {
"dt" : cur _frm . doc . doctype ,
"dn" : cur _frm . doc . name ,
2015-12-16 15:18:40 +00:00
"recipient_id" : cur _frm . doc . contact _email
2015-12-16 04:27:59 +00:00
} ,
callback : function ( r ) {
2015-12-17 04:18:44 +00:00
if ( ! r . exc ) {
2015-12-21 12:54:49 +00:00
var doc = frappe . model . sync ( r . message ) ;
frappe . set _route ( "Form" , r . message . doctype , r . message . name ) ;
2015-12-17 04:18:44 +00:00
}
2015-12-16 04:27:59 +00:00
}
} )
2016-01-18 10:51:25 +00:00
} ,
2016-01-19 14:22:22 +00:00
rate : function ( doc , cdt , cdn ) {
// if user changes the rate then set margin Rate or amount to 0
item = locals [ cdt ] [ cdn ] ;
2016-03-05 09:40:25 +00:00
item . margin _type = "" ;
item . margin _rate _or _amount = 0.0 ;
2016-01-19 14:22:22 +00:00
cur _frm . refresh _fields ( ) ;
} ,
2016-03-05 09:40:25 +00:00
margin _rate _or _amount : function ( doc , cdt , cdn ) {
2016-01-18 10:51:25 +00:00
// calculated the revised total margin and rate on margin rate changes
item = locals [ cdt ] [ cdn ] ;
2016-04-09 09:01:09 +00:00
this . apply _pricing _rule _on _item ( item )
2016-03-05 09:40:25 +00:00
this . calculate _taxes _and _totals ( ) ;
2016-01-18 10:51:25 +00:00
cur _frm . refresh _fields ( ) ;
} ,
2016-01-19 14:22:22 +00:00
2016-03-05 09:40:25 +00:00
margin _type : function ( doc , cdt , cdn ) {
2016-01-18 10:51:25 +00:00
// calculate the revised total margin and rate on margin type changes
item = locals [ cdt ] [ cdn ] ;
2016-04-09 09:01:09 +00:00
this . apply _pricing _rule _on _item ( item , doc , cdt , cdn )
2016-03-05 09:40:25 +00:00
this . calculate _taxes _and _totals ( ) ;
2016-01-19 14:22:22 +00:00
cur _frm . refresh _fields ( ) ;
2012-07-11 07:44:52 +00:00
}
2014-06-24 11:32:45 +00:00
} ) ;
2014-08-26 08:55:53 +00:00
2016-03-09 11:32:59 +00:00
frappe . ui . form . on ( cur _frm . doctype , "project" , function ( frm ) {
2014-10-23 07:48:59 +00:00
if ( in _list ( [ "Delivery Note" , "Sales Invoice" ] , frm . doc . doctype ) ) {
2016-05-19 12:52:49 +00:00
if ( frm . doc . project ) {
frappe . call ( {
method : 'erpnext.projects.doctype.project.project.get_cost_center_name' ,
args : { project : frm . doc . project } ,
callback : function ( r , rt ) {
if ( ! r . exc ) {
$ . each ( frm . doc [ "items" ] || [ ] , function ( i , row ) {
if ( r . message ) {
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 07:48:59 +00:00
}
2016-05-19 12:52:49 +00:00
} )
}
2014-10-23 07:48:59 +00:00
}
2014-08-26 08:55:53 +00:00
} )