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" ;
2015-05-06 17:29:00 +00:00
{ % include 'accounts/doctype/sales_taxes_and_charges_template/sales_taxes_and_charges_template.js' % }
2015-02-23 06:28:15 +00:00
2014-02-14 10:17:51 +00:00
frappe . provide ( "erpnext.selling" ) ;
2015-01-22 11:43:13 +00:00
frappe . require ( "assets/erpnext/js/controllers/transaction.js" ) ;
2014-01-02 06:17:23 +00:00
2014-09-12 07:39:41 +00:00
cur _frm . email _field = "contact_email" ;
2013-05-24 13:55:01 +00:00
erpnext . selling . SellingController = erpnext . TransactionController . extend ( {
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 ( ) {
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-06 13:15:47 +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 {
2013-12-13 08:40:14 +00:00
query : "erpnext.controllers.queries.item_query" ,
2013-07-11 13:43:58 +00:00
filters : ( me . frm . doc . order _type === "Maintenance" ?
{ 'is_service_item' : 'Yes' } :
{ 'is_sales_item' : 'Yes' } )
}
2013-07-04 11:43:53 +00:00
} ) ;
}
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 ( 'batch_no' ) ) {
this . frm . set _query ( "batch_no" , "items" , function ( doc , cdt , cdn ) {
2014-03-27 08:47:33 +00:00
var item = frappe . get _doc ( cdt , cdn ) ;
2013-07-04 11:43:53 +00:00
if ( ! item . item _code ) {
2014-04-14 10:55:30 +00:00
frappe . throw ( _ _ ( "Please enter Item Code to get batch no" ) ) ;
2013-07-04 11:43:53 +00:00
} else {
2013-10-18 06:59:11 +00:00
filters = {
'item_code' : item . item _code ,
'posting_date' : me . frm . doc . posting _date ,
}
if ( item . warehouse ) filters [ "warehouse" ] = item . warehouse
2014-04-11 11:21:27 +00:00
2013-10-18 06:59:11 +00:00
return {
2014-01-29 11:01:38 +00:00
query : "erpnext.controllers.queries.get_batch_no" ,
2013-10-18 06:59:11 +00:00
filters : filters
2013-07-04 11:43:53 +00:00
}
}
} ) ;
}
2014-04-11 11:21:27 +00:00
2013-07-23 09:46:50 +00:00
if ( this . frm . fields _dict . sales _team && this . frm . fields _dict . sales _team . grid . get _field ( "sales_person" ) ) {
2013-07-29 07:58:37 +00:00
this . frm . set _query ( "sales_person" , "sales_team" , erpnext . queries . not _a _group _filter ) ;
2013-07-23 09:46:50 +00:00
}
2013-05-15 15:45:57 +00:00
} ,
2014-04-11 11:21:27 +00:00
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
2013-07-15 07:34:33 +00:00
contact _person : function ( ) {
2014-01-02 06:17:23 +00:00
erpnext . utils . get _contact _details ( this . frm ) ;
2013-07-15 07:34:33 +00:00
} ,
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-05-21 14:05:06 +00:00
barcode : function ( doc , cdt , cdn ) {
this . item _code ( doc , cdt , cdn ) ;
2013-05-15 15:45:57 +00:00
} ,
2014-04-11 11:21:27 +00:00
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
2014-02-10 12:56:49 +00:00
item . rate = flt ( item . price _list _rate * ( 1 - item . discount _percentage / 100.0 ) ,
precision ( "rate" , item ) ) ;
2014-04-11 11:21:27 +00:00
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
}
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 ;
this . batch _no ( doc , cdt , cdn ) ;
2014-03-27 08:47:33 +00:00
var item = frappe . get _doc ( cdt , cdn ) ;
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 ( {
2014-05-30 13:40:12 +00:00
method : "erpnext.stock.get_item_details.get_available_qty" ,
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
} ,
} ) ;
}
} ,
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
2014-04-16 09:51:46 +00:00
calculate _outstanding _amount : function ( update _paid _amount ) {
2014-04-11 11:21:27 +00:00
// NOTE:
2013-09-05 07:29:33 +00:00
// paid_amount and write_off_amount is only for POS Invoice
2013-05-28 11:53:36 +00:00
// total_advance is only for non POS Invoice
2013-07-05 11:53:14 +00:00
if ( this . frm . doc . doctype == "Sales Invoice" && this . frm . doc . docstatus == 0 ) {
2015-02-12 10:39:11 +00:00
frappe . model . round _floats _in ( this . frm . doc , [ "base_grand_total" , "total_advance" , "write_off_amount" ,
2014-03-13 11:56:41 +00:00
"paid_amount" ] ) ;
2015-02-12 10:39:11 +00:00
var total _amount _to _pay = this . frm . doc . base _grand _total - this . frm . doc . write _off _amount
2014-03-13 11:56:41 +00:00
- this . frm . doc . total _advance ;
2014-03-25 14:05:41 +00:00
if ( this . frm . doc . is _pos ) {
2014-04-16 09:51:46 +00:00
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 11:44:13 +00:00
this . frm . refresh _field ( "paid_amount" ) ;
2014-04-16 09:51:46 +00:00
}
2014-03-25 14:05:41 +00:00
} else {
this . frm . doc . paid _amount = 0
2015-01-06 11:44:13 +00:00
this . frm . refresh _field ( "paid_amount" ) ;
2014-03-25 14:05:41 +00:00
}
2014-03-13 11:02:43 +00:00
2014-04-11 11:21:27 +00:00
this . frm . set _value ( "outstanding_amount" , flt ( total _amount _to _pay
2014-03-13 11:56:41 +00:00
- this . frm . doc . paid _amount , precision ( "outstanding_amount" ) ) ) ;
2013-05-27 13:59:07 +00:00
}
} ,
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 ( ) ;
2014-06-24 11:32:45 +00:00
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 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'>" +
2014-06-24 12:04:52 +00:00
_ _ ( "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 11:32:45 +00:00
"</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' ) ;
2012-07-11 07:44:52 +00:00
}
2014-06-24 11:32:45 +00:00
} ) ;
2014-08-26 08:55:53 +00:00
frappe . ui . form . on ( cur _frm . doctype , "project_name" , function ( frm ) {
2014-10-23 07:48:59 +00:00
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 07:45:21 +00:00
$ . each ( frm . doc [ "items" ] || [ ] , function ( i , row ) {
2014-10-28 09:06:26 +00:00
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
} )
}
2014-08-26 08:55:53 +00:00
}
2014-10-23 07:48:59 +00:00
} )
}
2014-08-26 08:55:53 +00:00
} )