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 = class SellingController extends erpnext . TransactionController {
2016-05-19 12:52:49 +00:00
setup ( ) {
super . setup ( ) ;
}
2013-07-11 13:43:58 +00:00
onload ( ) {
super . onload ( ) ;
this . setup _queries ( ) ;
2017-11-17 09:01:09 +00:00
this . frm . set _query ( 'shipping_rule' , function ( ) {
return {
filters : {
"shipping_rule_type" : "Selling"
}
} ;
} ) ;
2021-07-13 09:28:17 +00:00
}
2014-04-11 11:21:27 +00:00
2013-07-11 13:43:58 +00:00
setup _queries ( ) {
2013-07-04 11:43:53 +00:00
var me = this ;
2014-04-11 11:21:27 +00:00
2017-01-13 13:23:11 +00:00
$ . each ( [ [ "customer" , "customer" ] ,
2014-04-11 11:21:27 +00:00
[ "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
2017-01-13 13:23:11 +00:00
me . frm . set _query ( 'contact_person' , erpnext . queries . contact _query ) ;
me . frm . set _query ( 'customer_address' , erpnext . queries . address _query ) ;
me . frm . set _query ( 'shipping_address_name' , erpnext . queries . address _query ) ;
2021-12-21 07:05:44 +00:00
me . frm . set _query ( 'dispatch_address_name' , erpnext . queries . dispatch _address _query ) ;
2017-01-13 13:23:11 +00:00
2022-06-02 08:27:54 +00:00
erpnext . accounts . dimensions . setup _dimension _filters ( me . frm , me . frm . doctype ) ;
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
2019-07-04 17:16:16 +00:00
if ( this . frm . fields _dict . tc _name ) {
this . frm . set _query ( "tc_name" , function ( ) {
return { filters : { selling : 1 } } ;
} ) ;
}
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" ,
2022-05-16 06:44:07 +00:00
filters : { 'is_sales_item' : 1 , 'customer' : cur _frm . doc . customer , 'has_variants' : 0 }
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 )
} ) ;
}
2020-01-06 10:04:15 +00:00
if ( this . frm . fields _dict [ "items" ] . grid . get _field ( 'item_code' ) ) {
this . frm . set _query ( "item_tax_template" , "items" , function ( doc , cdt , cdn ) {
return me . set _query _for _item _tax _template ( doc , cdt , cdn )
} ) ;
}
2016-11-18 06:46:22 +00:00
}
2013-08-01 12:49:51 +00:00
refresh ( ) {
super . refresh ( ) ;
2021-08-19 08:11:10 +00:00
2017-01-16 07:36:07 +00:00
frappe . dynamic _link = { doc : this . frm . doc , fieldname : 'customer' , doctype : 'Customer' }
2017-01-13 13:23:11 +00:00
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 ) ) ;
2021-09-02 11:14:59 +00:00
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 ( ) {
2014-05-28 07:19:20 +00:00
var me = this ;
2018-08-01 12:14:34 +00:00
erpnext . utils . get _party _details ( this . frm , null , null , function ( ) {
me . apply _price _list ( ) ;
} ) ;
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 ( ) {
2014-01-02 06:17:23 +00:00
erpnext . utils . get _address _display ( this . frm , "customer_address" ) ;
2018-12-28 20:49:11 +00:00
erpnext . utils . set _taxes _from _address ( this . frm , "customer_address" , "customer_address" , "shipping_address_name" ) ;
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 ( ) {
erpnext . utils . get _address _display ( this . frm , "shipping_address_name" , "shipping_address" ) ;
2018-12-28 20:49:11 +00:00
erpnext . utils . set _taxes _from _address ( this . frm , "shipping_address_name" , "customer_address" , "shipping_address_name" ) ;
2014-02-19 12:13:24 +00:00
}
2014-04-11 11:21:27 +00:00
2021-07-13 09:28:17 +00:00
dispatch _address _name ( ) {
erpnext . utils . get _address _display ( this . frm , "dispatch_address_name" , "dispatch_address" ) ;
}
2014-05-28 07:19:20 +00:00
sales _partner ( ) {
this . apply _pricing _rule ( ) ;
}
campaign ( ) {
this . apply _pricing _rule ( ) ;
}
2013-08-09 12:41:35 +00:00
selling _price _list ( ) {
2014-07-01 12:15:15 +00:00
this . apply _price _list ( ) ;
2018-02-13 11:41:06 +00:00
this . set _dynamic _labels ( ) ;
2013-05-21 14:05:06 +00:00
}
2014-04-11 11:21:27 +00:00
2014-02-10 12:24:04 +00:00
discount _percentage ( doc , cdt , cdn ) {
2019-03-18 09:04:19 +00:00
var item = frappe . get _doc ( cdt , cdn ) ;
item . discount _amount = 0.0 ;
this . apply _discount _on _item ( doc , cdt , cdn , 'discount_percentage' ) ;
}
discount _amount ( doc , cdt , cdn ) {
2019-07-04 10:26:34 +00:00
if ( doc . name === cdn ) {
return ;
}
2019-04-05 14:05:02 +00:00
var item = frappe . get _doc ( cdt , cdn ) ;
item . discount _percentage = 0.0 ;
2019-03-18 09:04:19 +00:00
this . apply _discount _on _item ( doc , cdt , cdn , 'discount_amount' ) ;
}
apply _discount _on _item ( doc , cdt , cdn , field ) {
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 ) {
2019-03-18 09:04:19 +00:00
item [ field ] = 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 ( ) {
this . calculate _commission ( ) ;
}
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
total _commission ( ) {
2021-11-30 12:24:18 +00:00
frappe . model . round _floats _in ( this . frm . doc , [ "amount_eligible_for_commission" , "total_commission" ] ) ;
2014-04-11 11:21:27 +00:00
2021-11-30 12:24:18 +00:00
const { amount _eligible _for _commission } = this . frm . doc ;
if ( ! amount _eligible _for _commission ) return ;
this . frm . set _value (
"commission_rate" , flt (
this . frm . doc . total _commission * 100.0 / amount _eligible _for _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
allocated _percentage ( doc , cdt , cdn ) {
2014-03-27 08:47:33 +00:00
var sales _person = frappe . get _doc ( cdt , cdn ) ;
2013-05-21 14:05:06 +00:00
if ( sales _person . allocated _percentage ) {
2018-09-25 13:38:16 +00:00
2013-05-21 14:05:06 +00:00
sales _person . allocated _percentage = flt ( sales _person . allocated _percentage ,
precision ( "allocated_percentage" , sales _person ) ) ;
2018-09-25 13:38:16 +00:00
2021-11-30 12:24:18 +00:00
sales _person . allocated _amount = flt ( this . frm . doc . amount _eligible _for _commission *
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 ) ) ;
2018-09-25 13:38:16 +00:00
refresh _field ( [ "allocated_amount" ] , sales _person ) ;
2013-05-21 14:05:06 +00:00
2018-09-25 13:38:16 +00:00
this . calculate _incentive ( sales _person ) ;
refresh _field ( [ "allocated_percentage" , "allocated_amount" , "commission_rate" , "incentives" ] , sales _person . name ,
2013-05-21 14:05:06 +00:00
sales _person . parentfield ) ;
2018-12-29 14:03:44 +00:00
}
2018-09-25 13:38:16 +00:00
}
sales _person ( doc , cdt , cdn ) {
var row = frappe . get _doc ( cdt , cdn ) ;
this . calculate _incentive ( row ) ;
refresh _field ( "incentives" , row . name , row . parentfield ) ;
2013-05-21 14:05:06 +00:00
}
2014-04-11 11:21:27 +00:00
2013-08-02 07:09:10 +00:00
toggle _editable _price _list _rate ( ) {
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 ) {
2021-09-13 10:20:20 +00:00
const parent _field = frappe . meta . get _parentfield ( this . frm . doc . doctype , this . frm . doc . doctype + " Item" ) ;
if ( ! this . frm . fields _dict [ parent _field ] ) return ;
this . frm . fields _dict [ parent _field ] . grid . update _docfield _property (
'price_list_rate' , 'read_only' , 0
) ;
2013-08-02 07:09:10 +00:00
}
}
2014-04-11 11:21:27 +00:00
2013-05-21 14:05:06 +00:00
calculate _commission ( ) {
2023-03-07 06:14:09 +00:00
if ( ! this . frm . fields _dict . commission _rate || this . frm . doc . docstatus === 1 ) return ;
2014-04-11 11:21:27 +00:00
2021-11-30 12:24:18 +00:00
if ( this . frm . doc . commission _rate > 100 ) {
this . frm . set _value ( "commission_rate" , 100 ) ;
frappe . throw ( ` ${ _ _ ( 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
}
2021-11-30 12:24:18 +00:00
this . frm . doc . amount _eligible _for _commission = this . frm . doc . items . reduce (
( sum , item ) => item . grant _commission ? sum + item . base _net _amount : sum , 0
)
this . frm . doc . total _commission = flt (
this . frm . doc . amount _eligible _for _commission * this . frm . doc . commission _rate / 100.0 ,
precision ( "total_commission" )
) ;
refresh _field ( [ "amount_eligible_for_commission" , "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 ( ) {
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 ) {
2017-05-30 07:24:42 +00:00
frappe . model . round _floats _in ( sales _person ) ;
2021-11-30 12:24:18 +00:00
if ( ! sales _person . allocated _percentage ) return ;
sales _person . allocated _amount = flt (
me . frm . doc . amount _eligible _for _commission
* sales _person . allocated _percentage
/ 1 0 0 . 0 ,
precision ( "allocated_amount" , sales _person )
) ;
2017-05-30 07:24:42 +00:00
} ) ;
2013-05-21 14:05:06 +00:00
}
2014-04-11 11:21:27 +00:00
2018-09-25 13:38:16 +00:00
calculate _incentive ( row ) {
if ( row . allocated _amount )
{
row . incentives = flt (
row . allocated _amount * row . commission _rate / 100.0 ,
2018-09-26 10:25:43 +00:00
precision ( "incentives" , row ) ) ;
2018-09-25 13:38:16 +00:00
}
}
2013-05-23 13:55:08 +00:00
set _dynamic _labels ( ) {
2013-06-21 12:25:31 +00:00
super . set _dynamic _labels ( ) ;
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 ( 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 ) ;
2017-05-30 07:24:42 +00:00
if ( in _list ( [ 'Delivery Note' , 'Sales Invoice' ] , doc . doctype ) ) {
var 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 ) ;
2017-05-30 07:24:42 +00:00
if ( in _list ( [ '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
2017-08-21 02:58:55 +00:00
company _address ( ) {
var me = this ;
if ( this . frm . doc . company _address ) {
frappe . call ( {
method : "frappe.contacts.doctype.address.address.get_address_display" ,
args : { "address_dict" : this . frm . doc . company _address } ,
callback : function ( r ) {
if ( r . message ) {
me . frm . set _value ( "company_address_display" , r . message )
}
}
} )
} else {
this . frm . set _value ( "company_address_display" , "" ) ;
}
2017-11-23 07:35:15 +00:00
}
conversion _factor ( doc , cdt , cdn , dont _fetch _price _list _rate ) {
super . conversion _factor ( doc , cdt , cdn , dont _fetch _price _list _rate ) ;
}
qty ( doc , cdt , cdn ) {
2018-04-13 06:33:42 +00:00
super . qty ( doc , cdt , cdn ) ;
2017-11-23 07:35:15 +00:00
}
2022-12-05 09:18:18 +00:00
pick _serial _and _batch ( doc , cdt , cdn ) {
let item = locals [ cdt ] [ cdn ] ;
let me = this ;
let path = "assets/erpnext/js/utils/serial_no_batch_selector.js" ;
frappe . db . get _value ( "Item" , item . item _code , [ "has_batch_no" , "has_serial_no" ] )
. then ( ( r ) => {
if ( r . message && ( r . message . has _batch _no || r . message . has _serial _no ) ) {
item . has _serial _no = r . message . has _serial _no ;
item . has _batch _no = r . message . has _batch _no ;
2023-03-13 09:21:43 +00:00
item . type _of _transaction = item . qty > 0 ? "Outward" : "Inward" ;
2022-12-05 09:18:18 +00:00
item . title = item . has _serial _no ?
_ _ ( "Select Serial No" ) : _ _ ( "Select Batch No" ) ;
if ( item . has _serial _no && item . has _batch _no ) {
item . title = _ _ ( "Select Serial and Batch" ) ;
}
frappe . require ( path , function ( ) {
2023-03-21 05:24:41 +00:00
new erpnext . SerialBatchPackageSelector (
2022-12-05 09:18:18 +00:00
me . frm , item , ( r ) => {
if ( r ) {
2023-03-24 04:44:09 +00:00
frappe . model . set _value ( item . doctype , item . name , {
"serial_and_batch_bundle" : r . name ,
"qty" : Math . abs ( r . total _qty )
} ) ;
2022-12-05 09:18:18 +00:00
}
}
) ;
} ) ;
}
} ) ;
}
2018-06-28 08:57:58 +00:00
update _auto _repeat _reference ( doc ) {
if ( doc . auto _repeat ) {
frappe . call ( {
2019-07-17 08:20:32 +00:00
method : "frappe.automation.doctype.auto_repeat.auto_repeat.update_reference" ,
2018-12-29 14:03:44 +00:00
args : {
2018-06-28 08:57:58 +00:00
docname : doc . auto _repeat ,
reference : doc . name
} ,
callback : function ( r ) {
if ( r . message == "success" ) {
frappe . show _alert ( { message : _ _ ( "Auto repeat document updated" ) , indicator : 'green' } ) ;
} else {
frappe . show _alert ( { message : _ _ ( "An error occurred during the update process" ) , indicator : 'red' } ) ;
}
}
} )
}
}
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 ) ;
2020-11-25 10:06:41 +00:00
frappe . msgprint ( _ _ ( "Cost Center For Item with Item Code {0} has been Changed to {1}" , [ row . item _name , r . message ] ) ) ;
2016-05-19 12:52:49 +00:00
}
} )
}
2014-10-23 07:48:59 +00:00
}
2016-05-19 12:52:49 +00:00
} )
}
2014-10-23 07:48:59 +00:00
}
2018-08-01 12:14:34 +00:00
} )
2018-12-29 14:03:44 +00:00
frappe . ui . form . on ( cur _frm . doctype , {
set _as _lost _dialog : function ( frm ) {
var dialog = new frappe . ui . Dialog ( {
title : _ _ ( "Set as Lost" ) ,
fields : [
2020-08-13 03:50:36 +00:00
{
"fieldtype" : "Table MultiSelect" ,
"label" : _ _ ( "Lost Reasons" ) ,
"fieldname" : "lost_reason" ,
"options" : frm . doctype === 'Opportunity' ? 'Opportunity Lost Reason Detail' : 'Quotation Lost Reason Detail' ,
"reqd" : 1
} ,
2021-11-09 14:06:27 +00:00
{
"fieldtype" : "Table MultiSelect" ,
"label" : _ _ ( "Competitors" ) ,
"fieldname" : "competitors" ,
"options" : "Competitor Detail"
} ,
2020-08-13 03:50:36 +00:00
{
2022-01-29 17:02:03 +00:00
"fieldtype" : "Small Text" ,
2020-08-13 03:50:36 +00:00
"label" : _ _ ( "Detailed Reason" ) ,
"fieldname" : "detailed_reason"
} ,
2018-12-29 14:03:44 +00:00
] ,
primary _action : function ( ) {
2021-11-09 14:06:27 +00:00
let values = dialog . get _values ( ) ;
2018-12-29 14:03:44 +00:00
frm . call ( {
doc : frm . doc ,
method : 'declare_enquiry_lost' ,
args : {
2021-11-09 14:06:27 +00:00
'lost_reasons_list' : values . lost _reason ,
2022-01-31 06:39:54 +00:00
'competitors' : values . competitors ? values . competitors : [ ] ,
2021-11-09 14:06:27 +00:00
'detailed_reason' : values . detailed _reason
2018-12-29 14:03:44 +00:00
} ,
callback : function ( r ) {
dialog . hide ( ) ;
frm . reload _doc ( ) ;
} ,
} ) ;
} ,
primary _action _label : _ _ ( 'Declare Lost' )
} ) ;
dialog . show ( ) ;
}
2022-06-10 08:22:17 +00:00
} )