2013-11-20 07:29:58 +00:00
// Copyright (c) 2013, Web Notes 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
// Preset
// ------
// cur_frm.cscript.tname - Details table name
// cur_frm.cscript.fname - Details fieldname
2013-12-13 08:40:14 +00:00
// cur_frm.cscript.other_fname - fieldname
2011-06-08 09:07:15 +00:00
// cur_frm.cscript.sales_team_fname - Sales Team fieldname
2014-02-14 10:17:51 +00:00
frappe . provide ( "erpnext.selling" ) ;
frappe . require ( "assets/erpnext/js/transaction.js" ) ;
2014-01-02 06:17:23 +00:00
2013-12-13 08:40:14 +00:00
{ % include "public/js/controllers/accounts.js" % }
2013-05-15 15:45:57 +00:00
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 . toggle _rounded _total ( ) ;
this . setup _queries ( ) ;
2013-08-02 07:09:10 +00:00
this . toggle _editable _price _list _rate ( ) ;
2013-07-11 13:43:58 +00:00
} ,
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 : [
[ 'Sales Taxes and Charges Master' , 'company' , '=' , me . frm . doc . company ] ,
[ 'Sales Taxes and Charges Master' , 'docstatus' , '!=' , 2 ]
]
}
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
2013-07-04 11:43:53 +00:00
if ( ! this . fname ) {
return ;
}
2014-04-11 11:21:27 +00:00
2013-07-04 11:43:53 +00:00
if ( this . frm . fields _dict [ this . fname ] . grid . get _field ( 'item_code' ) ) {
this . frm . set _query ( "item_code" , this . fname , 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
2013-07-04 11:43:53 +00:00
if ( this . frm . fields _dict [ this . fname ] . grid . get _field ( 'batch_no' ) ) {
this . frm . set _query ( "batch_no" , this . fname , 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 ) ) ;
2013-08-01 12:49:51 +00:00
if ( this . frm . fields _dict . packing _details ) {
2014-03-26 12:54:30 +00:00
var packing _list _exists = ( this . frm . doc . packing _details || [ ] ) . length ;
2013-08-01 12:49:51 +00:00
this . frm . toggle _display ( "packing_list" , packing _list _exists ? true : false ) ;
}
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-01-28 12:13:10 +00:00
erpnext . utils . get _party _details ( this . frm ) ;
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
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 ( ) {
this . get _price _list _currency ( "Selling" ) ;
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
2014-02-10 12:56:49 +00:00
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 , [ "rate" , "price_list_rate" ] ) ;
2014-04-11 11:21:27 +00:00
2014-02-10 12:24:04 +00:00
if ( item . price _list _rate ) {
2014-02-10 12:56:49 +00:00
item . discount _percentage = flt ( ( 1 - item . rate / item . price _list _rate ) * 100.0 ,
2014-02-10 12:24:04 +00:00
precision ( "discount_percentage" , item ) ) ;
2013-05-21 14:05:06 +00:00
} else {
2014-02-10 12:24:04 +00:00
item . discount _percentage = 0.0 ;
2013-05-21 14:05:06 +00:00
}
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
this . calculate _taxes _and _totals ( ) ;
} ,
2013-12-23 10:19:08 +00:00
2014-01-03 12:45:07 +00:00
discount _amount : function ( ) {
2013-12-23 10:19:08 +00:00
this . calculate _taxes _and _totals ( ) ;
} ,
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 ( ) {
if ( this . frm . doc . net _total ) {
2014-02-14 10:17:51 +00:00
frappe . model . round _floats _in ( this . frm . doc , [ "net_total" , "total_commission" ] ) ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
if ( this . frm . doc . 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 ) ) + " > " +
2014-04-14 10:55:30 +00:00
_ _ ( frappe . meta . get _label ( this . frm . doc . doctype , "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" ,
2013-05-21 14:05:06 +00:00
flt ( this . frm . doc . total _commission * 100.0 / this . frm . doc . net _total ) ) ;
}
} ,
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 ) ) ;
sales _person . allocated _amount = flt ( this . frm . doc . 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 ) {
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 ( {
2013-12-12 13:42:19 +00:00
method : "erpnext.selling.utils.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-05-21 14:05:06 +00:00
toggle _rounded _total : function ( ) {
var me = this ;
2014-02-14 10:17:51 +00:00
if ( cint ( frappe . defaults . get _global _default ( "disable_rounded_total" ) ) ) {
2013-05-21 14:05:06 +00:00
$ . each ( [ "rounded_total" , "rounded_total_export" ] , function ( i , fieldname ) {
me . frm . set _df _property ( fieldname , "print_hide" , 1 ) ;
me . frm . toggle _display ( fieldname , false ) ;
} ) ;
}
} ,
2014-04-11 11:21:27 +00:00
2013-08-02 07:09:10 +00:00
toggle _editable _price _list _rate : function ( ) {
2014-02-14 10:17:51 +00:00
var df = frappe . meta . get _docfield ( this . tname , "price_list_rate" , this . frm . doc . name ) ;
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 _taxes _and _totals : function ( update _paid _amount ) {
2013-05-24 13:55:01 +00:00
this . _super ( ) ;
2014-04-16 09:51:46 +00:00
this . calculate _total _advance ( "Sales Invoice" , "advance_adjustment_details" , update _paid _amount ) ;
2013-05-21 14:05:06 +00:00
this . calculate _commission ( ) ;
this . calculate _contribution ( ) ;
2013-05-28 11:53:36 +00:00
// TODO check for custom_recalc in custom scripts of server
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
this . frm . refresh _fields ( ) ;
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 _item _values : function ( ) {
var me = this ;
2014-04-11 11:21:27 +00:00
2014-01-03 12:45:07 +00:00
if ( ! this . discount _amount _applied ) {
2013-12-23 10:19:08 +00:00
$ . each ( this . frm . item _doclist , function ( i , item ) {
2014-02-14 10:17:51 +00:00
frappe . model . round _floats _in ( item ) ;
2014-02-10 13:50:15 +00:00
item . amount = flt ( item . rate * item . qty , precision ( "amount" , item ) ) ;
2013-12-23 10:19:08 +00:00
2014-02-10 12:24:04 +00:00
me . _set _in _company _currency ( item , "price_list_rate" , "base_price_list_rate" ) ;
2014-02-10 12:56:49 +00:00
me . _set _in _company _currency ( item , "rate" , "base_rate" ) ;
2014-02-10 13:50:15 +00:00
me . _set _in _company _currency ( item , "amount" , "base_amount" ) ;
2013-12-23 10:19:08 +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
determine _exclusive _rate : function ( ) {
var me = this ;
$ . each ( me . frm . item _doclist , function ( n , item ) {
var item _tax _map = me . _load _item _tax _rate ( item . item _tax _rate ) ;
var cumulated _tax _fraction = 0.0 ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
$ . each ( me . frm . tax _doclist , function ( i , tax ) {
tax . tax _fraction _for _current _item = me . get _current _tax _fraction ( tax , item _tax _map ) ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
if ( i == 0 ) {
2013-05-23 13:55:08 +00:00
tax . grand _total _fraction _for _current _item = 1 + tax . tax _fraction _for _current _item ;
2013-05-21 14:05:06 +00:00
} else {
2014-04-11 11:21:27 +00:00
tax . grand _total _fraction _for _current _item =
2013-05-23 13:55:08 +00:00
me . frm . tax _doclist [ i - 1 ] . grand _total _fraction _for _current _item +
2013-05-21 14:05:06 +00:00
tax . tax _fraction _for _current _item ;
}
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
cumulated _tax _fraction += tax . tax _fraction _for _current _item ;
} ) ;
2014-04-11 11:21:27 +00:00
2014-01-03 12:45:07 +00:00
if ( cumulated _tax _fraction && ! me . discount _amount _applied ) {
2014-02-10 13:50:15 +00:00
item . base _amount = flt (
( item . amount * me . frm . doc . conversion _rate ) / ( 1 + cumulated _tax _fraction ) ,
precision ( "base_amount" , item ) ) ;
2013-12-25 14:16:20 +00:00
2014-02-10 13:50:15 +00:00
item . base _rate = flt ( item . base _amount / item . qty , precision ( "base_rate" , item ) ) ;
2014-04-11 11:21:27 +00:00
2014-02-10 12:24:04 +00:00
if ( item . discount _percentage == 100 ) {
2014-02-10 12:56:49 +00:00
item . base _price _list _rate = item . base _rate ;
item . base _rate = 0.0 ;
2013-05-21 14:05:06 +00:00
} else {
2014-02-10 12:56:49 +00:00
item . base _price _list _rate = flt ( item . base _rate / ( 1 - item . discount _percentage / 100.0 ) ,
2014-02-10 12:24:04 +00:00
precision ( "base_price_list_rate" , 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
get _current _tax _fraction : function ( tax , item _tax _map ) {
// Get tax fraction for calculating tax exclusive amount
// from tax inclusive amount
var current _tax _fraction = 0.0 ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
if ( cint ( tax . included _in _print _rate ) ) {
2013-05-23 13:55:08 +00:00
var tax _rate = this . _get _tax _rate ( tax , item _tax _map ) ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
if ( tax . charge _type == "On Net Total" ) {
current _tax _fraction = ( tax _rate / 100.0 ) ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
} else if ( tax . charge _type == "On Previous Row Amount" ) {
current _tax _fraction = ( tax _rate / 100.0 ) *
2013-05-23 13:55:08 +00:00
this . frm . tax _doclist [ cint ( tax . row _id ) - 1 ] . tax _fraction _for _current _item ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
} else if ( tax . charge _type == "On Previous Row Total" ) {
current _tax _fraction = ( tax _rate / 100.0 ) *
2013-05-23 13:55:08 +00:00
this . frm . tax _doclist [ cint ( tax . row _id ) - 1 ] . grand _total _fraction _for _current _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
return current _tax _fraction ;
} ,
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
calculate _net _total : function ( ) {
var me = this ;
this . frm . doc . net _total = this . frm . doc . net _total _export = 0.0 ;
2013-12-25 14:16:20 +00:00
2013-05-21 14:05:06 +00:00
$ . each ( this . frm . item _doclist , function ( i , item ) {
2014-02-10 13:50:15 +00:00
me . frm . doc . net _total += item . base _amount ;
me . frm . doc . net _total _export += item . amount ;
2013-05-21 14:05:06 +00:00
} ) ;
2013-12-25 14:16:20 +00:00
2014-02-14 10:17:51 +00:00
frappe . model . round _floats _in ( this . frm . doc , [ "net_total" , "net_total_export" ] ) ;
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 _totals : function ( ) {
2013-12-23 10:19:08 +00:00
var me = this ;
2013-05-21 14:05:06 +00:00
var tax _count = this . frm . tax _doclist . length ;
2013-12-23 10:19:08 +00:00
2013-05-21 14:05:06 +00:00
this . frm . doc . grand _total = flt (
tax _count ? this . frm . tax _doclist [ tax _count - 1 ] . total : this . frm . doc . net _total ,
precision ( "grand_total" ) ) ;
this . frm . doc . grand _total _export = flt ( this . frm . doc . grand _total / this . frm . doc . conversion _rate ,
precision ( "grand_total_export" ) ) ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
this . frm . doc . other _charges _total = flt ( this . frm . doc . grand _total - this . frm . doc . net _total ,
precision ( "other_charges_total" ) ) ;
2014-04-11 11:21:27 +00:00
this . frm . doc . other _charges _total _export = flt ( this . frm . doc . grand _total _export -
2014-01-03 12:45:07 +00:00
this . frm . doc . net _total _export + flt ( this . frm . doc . discount _amount ) ,
2013-05-21 14:05:06 +00:00
precision ( "other_charges_total_export" ) ) ;
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
this . frm . doc . rounded _total = Math . round ( this . frm . doc . grand _total ) ;
this . frm . doc . rounded _total _export = Math . round ( this . frm . doc . grand _total _export ) ;
} ,
2013-12-23 10:19:08 +00:00
2014-01-03 12:45:07 +00:00
apply _discount _amount : function ( ) {
2013-12-23 10:19:08 +00:00
var me = this ;
var distributed _amount = 0.0 ;
2014-01-03 12:45:07 +00:00
if ( this . frm . doc . discount _amount ) {
var grand _total _for _discount _amount = this . get _grand _total _for _discount _amount ( ) ;
// calculate item amount after Discount Amount
if ( grand _total _for _discount _amount ) {
$ . each ( this . frm . item _doclist , function ( i , item ) {
2014-02-10 13:50:15 +00:00
distributed _amount = flt ( me . frm . doc . discount _amount ) * item . base _amount / grand _total _for _discount _amount ;
item . base _amount = flt ( item . base _amount - distributed _amount , precision ( "base_amount" , item ) ) ;
2014-01-03 12:45:07 +00:00
} ) ;
2013-12-23 10:19:08 +00:00
2014-01-03 12:45:07 +00:00
this . discount _amount _applied = true ;
this . _calculate _taxes _and _totals ( ) ;
}
2013-12-23 10:19:08 +00:00
}
2013-05-21 14:05:06 +00:00
} ,
2013-12-25 14:16:20 +00:00
2014-01-03 12:45:07 +00:00
get _grand _total _for _discount _amount : function ( ) {
2013-12-25 14:16:20 +00:00
var me = this ;
var total _actual _tax = 0.0 ;
var actual _taxes _dict = { } ;
$ . each ( this . frm . tax _doclist , function ( i , tax ) {
if ( tax . charge _type == "Actual" )
actual _taxes _dict [ tax . idx ] = tax . tax _amount ;
else if ( actual _taxes _dict [ tax . row _id ] !== null ) {
actual _tax _amount = flt ( actual _taxes _dict [ tax . row _id ] ) * flt ( tax . rate ) / 100 ;
actual _taxes _dict [ tax . idx ] = actual _tax _amount ;
}
} ) ;
$ . each ( actual _taxes _dict , function ( key , value ) {
if ( value )
total _actual _tax += value ;
} ) ;
2014-04-11 11:21:27 +00:00
grand _total _for _discount _amount = flt ( this . frm . doc . grand _total - total _actual _tax ,
2013-12-25 14:16:20 +00:00
precision ( "grand_total" ) ) ;
2014-01-03 12:45:07 +00:00
return grand _total _for _discount _amount ;
2013-12-25 14:16:20 +00:00
} ,
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 ) {
2014-03-13 11:56:41 +00:00
frappe . model . round _floats _in ( this . frm . doc , [ "grand_total" , "total_advance" , "write_off_amount" ,
"paid_amount" ] ) ;
2014-04-11 11:21:27 +00:00
var total _amount _to _pay = this . frm . doc . 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 ) ;
}
2014-03-25 14:05:41 +00:00
} else {
this . frm . doc . paid _amount = 0
}
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
2013-05-28 11:53:36 +00:00
this . frm . doc . total _commission = flt ( this . frm . doc . net _total * this . frm . doc . commission _rate / 100.0 ,
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 (
me . frm . doc . net _total * sales _person . allocated _percentage / 100.0 ,
precision ( "allocated_amount" , sales _person ) ) ;
}
} ) ;
} ,
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
_cleanup : function ( ) {
2013-05-24 13:55:01 +00:00
this . _super ( ) ;
this . frm . doc . in _words = this . frm . doc . in _words _export = "" ;
2013-05-21 14:05:06 +00:00
} ,
2013-05-23 13:55:08 +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
2013-05-23 13:55:08 +00:00
set _dynamic _labels : function ( ) {
2013-06-21 12:25:31 +00:00
this . _super ( ) ;
2013-06-05 15:16:56 +00:00
set _sales _bom _help ( this . frm . doc ) ;
2013-05-23 13:55:08 +00:00
} ,
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
change _form _labels : function ( company _currency ) {
var me = this ;
var field _label _map = { } ;
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
var setup _field _label _map = function ( fields _list , currency ) {
$ . each ( fields _list , function ( i , fname ) {
2014-02-14 10:17:51 +00:00
var docfield = frappe . meta . docfield _map [ me . frm . doc . doctype ] [ fname ] ;
2013-05-23 13:55:08 +00:00
if ( docfield ) {
2014-04-14 10:55:30 +00:00
var label = _ _ ( docfield . label || "" ) . replace ( /\([^\)]*\)/g , "" ) ;
2013-05-23 13:55:08 +00:00
field _label _map [ fname ] = label . trim ( ) + " (" + currency + ")" ;
}
} ) ;
} ;
2014-04-11 11:21:27 +00:00
setup _field _label _map ( [ "net_total" , "other_charges_total" , "grand_total" ,
2013-05-23 13:55:08 +00:00
"rounded_total" , "in_words" ,
"outstanding_amount" , "total_advance" , "paid_amount" , "write_off_amount" ] ,
company _currency ) ;
2014-04-11 11:21:27 +00:00
setup _field _label _map ( [ "net_total_export" , "other_charges_total_export" , "grand_total_export" ,
2013-05-23 13:55:08 +00:00
"rounded_total_export" , "in_words_export" ] , this . frm . doc . currency ) ;
2014-04-11 11:21:27 +00:00
cur _frm . set _df _property ( "conversion_rate" , "description" , "1 " + this . frm . doc . currency
2013-11-02 09:17:11 +00:00
+ " = [?] " + company _currency )
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
if ( this . frm . doc . price _list _currency && this . frm . doc . price _list _currency != company _currency ) {
2014-04-11 11:21:27 +00:00
cur _frm . set _df _property ( "plc_conversion_rate" , "description" , "1 " + this . frm . doc . price _list _currency
2013-11-02 09:17:11 +00:00
+ " = [?] " + company _currency )
2013-05-23 13:55:08 +00:00
}
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
// toggle fields
2014-04-11 11:21:27 +00:00
this . frm . toggle _display ( [ "conversion_rate" , "net_total" , "other_charges_total" ,
2013-05-23 13:55:08 +00:00
"grand_total" , "rounded_total" , "in_words" ] ,
this . frm . doc . currency != company _currency ) ;
2014-04-11 11:21:27 +00:00
this . frm . toggle _display ( [ "plc_conversion_rate" , "price_list_currency" ] ,
2013-05-23 13:55:08 +00:00
this . frm . doc . price _list _currency != company _currency ) ;
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
// set labels
$ . each ( field _label _map , function ( fname , label ) {
me . frm . fields _dict [ fname ] . set _label ( label ) ;
} ) ;
} ,
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
change _grid _labels : function ( company _currency ) {
var me = this ;
var field _label _map = { } ;
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
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 10:17:51 +00:00
var docfield = frappe . meta . docfield _map [ grid _doctype ] [ fname ] ;
2013-05-23 13:55:08 +00:00
if ( docfield ) {
2014-04-14 10:55:30 +00:00
var label = _ _ ( docfield . label || "" ) . replace ( /\([^\)]*\)/g , "" ) ;
2014-04-11 11:21:27 +00:00
field _label _map [ grid _doctype + "-" + fname ] =
2013-05-23 13:55:08 +00:00
label . trim ( ) + " (" + currency + ")" ;
}
} ) ;
}
2014-04-11 11:21:27 +00:00
2014-02-10 13:50:15 +00:00
setup _field _label _map ( [ "base_rate" , "base_price_list_rate" , "base_amount" ] ,
2013-05-23 13:55:08 +00:00
company _currency , this . fname ) ;
2014-04-11 11:21:27 +00:00
2014-02-10 13:50:15 +00:00
setup _field _label _map ( [ "rate" , "price_list_rate" , "amount" ] ,
2013-05-23 13:55:08 +00:00
this . frm . doc . currency , this . fname ) ;
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
setup _field _label _map ( [ "tax_amount" , "total" ] , company _currency , "other_charges" ) ;
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
if ( this . frm . fields _dict [ "advance_allocation_details" ] ) {
setup _field _label _map ( [ "advance_amount" , "allocated_amount" ] , company _currency ,
"advance_allocation_details" ) ;
}
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
// toggle columns
var item _grid = this . frm . fields _dict [ this . fname ] . grid ;
2014-04-11 11:21:27 +00:00
var show = ( this . frm . doc . currency != company _currency ) ||
( ( cur _frm . doc . other _charges || [ ] ) . filter (
2014-03-26 12:54:30 +00:00
function ( d ) { return d . included _in _print _rate === 1 } ) . length ) ;
2014-04-11 11:21:27 +00:00
2014-02-10 13:50:15 +00:00
$ . each ( [ "base_rate" , "base_price_list_rate" , "base_amount" ] , function ( i , fname ) {
2014-02-14 10:17:51 +00:00
if ( frappe . meta . get _docfield ( item _grid . doctype , fname ) )
2013-05-23 13:55:08 +00:00
item _grid . set _column _disp ( fname , show ) ;
} ) ;
2014-04-11 11:21:27 +00:00
2013-05-23 13:55:08 +00:00
// set labels
var $wrapper = $ ( this . frm . wrapper ) ;
$ . each ( field _label _map , function ( fname , label ) {
2013-08-05 06:46:04 +00:00
fname = fname . split ( "-" ) ;
2014-02-14 10:17:51 +00:00
var df = frappe . meta . get _docfield ( fname [ 0 ] , fname [ 1 ] , me . frm . doc . name ) ;
2013-08-05 06:46:04 +00:00
if ( df ) df . label = label ;
2013-05-23 13:55:08 +00:00
} ) ;
} ,
2013-05-15 15:45:57 +00:00
} ) ;
2012-07-11 07:44:52 +00:00
// Help for Sales BOM items
var set _sales _bom _help = function ( doc ) {
2012-07-16 08:46:57 +00:00
if ( ! cur _frm . fields _dict . packing _list ) return ;
2014-03-27 08:47:33 +00:00
if ( ( doc . packing _details || [ ] ) . length ) {
2012-07-11 07:44:52 +00:00
$ ( cur _frm . fields _dict . packing _list . row . wrapper ) . toggle ( true ) ;
2014-04-11 11:21:27 +00:00
2012-07-11 07:44:52 +00:00
if ( inList ( [ 'Delivery Note' , 'Sales Invoice' ] , doc . doctype ) ) {
2013-09-27 20:05:17 +00:00
help _msg = "<div class='alert alert-warning'>" +
2014-04-15 05:20:36 +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." ) +
2013-09-27 20:05:17 +00:00
"</div>" ;
2014-02-14 10:17:51 +00:00
frappe . meta . get _docfield ( doc . doctype , 'sales_bom_help' , doc . name ) . options = help _msg ;
2014-04-11 11:21:27 +00:00
}
2012-07-11 07:44:52 +00:00
} else {
$ ( cur _frm . fields _dict . packing _list . row . wrapper ) . toggle ( false ) ;
if ( inList ( [ 'Delivery Note' , 'Sales Invoice' ] , doc . doctype ) ) {
2014-02-14 10:17:51 +00:00
frappe . meta . get _docfield ( doc . doctype , 'sales_bom_help' , doc . name ) . options = '' ;
2012-07-11 07:44:52 +00:00
}
}
refresh _field ( 'sales_bom_help' ) ;
}