blob: 1fcf334717b3d13d1ac74510bc01b8ba86737a41 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05303
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05304
Nabin Hait613d0812015-02-23 11:58:15 +05305cur_frm.cscript.tax_table = "Sales Taxes and Charges";
Rushabh Mehta621283c2016-04-21 19:00:34 +05306{% include 'erpnext/accounts/doctype/sales_taxes_and_charges_template/sales_taxes_and_charges_template.js' %}
Nabin Hait613d0812015-02-23 11:58:15 +05307
Rushabh Mehtab09d9da2014-01-02 11:47:23 +05308
Rushabh Mehtae10cf022014-09-12 13:09:41 +05309cur_frm.email_field = "contact_email";
10
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053011frappe.provide("erpnext.selling");
Anand Doshi3543f302013-05-24 19:25:01 +053012erpnext.selling.SellingController = erpnext.TransactionController.extend({
Rushabh Mehta532eedf2016-05-19 18:22:49 +053013 setup: function() {
14 this._super();
Rushabh Mehta532eedf2016-05-19 18:22:49 +053015 },
16
Anand Doshi9b496142013-07-11 19:13:58 +053017 onload: function() {
18 this._super();
Anand Doshi9b496142013-07-11 19:13:58 +053019 this.setup_queries();
20 },
Anand Doshibd67e872014-04-11 16:51:27 +053021
Anand Doshi9b496142013-07-11 19:13:58 +053022 setup_queries: function() {
Rushabh Mehta532eedf2016-05-19 18:22:49 +053023
24
Anand Doshi99100a42013-07-04 17:13:53 +053025 var me = this;
Anand Doshibd67e872014-04-11 16:51:27 +053026
Anand Doshif3096132013-05-21 19:35:06 +053027 this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate");
Anand Doshibd67e872014-04-11 16:51:27 +053028
Rushabh Mehtab92087c2017-01-13 18:53:11 +053029 $.each([["customer", "customer"],
Anand Doshibd67e872014-04-11 16:51:27 +053030 ["lead", "lead"]],
Anand Doshid5d39ac2013-07-29 13:28:37 +053031 function(i, opts) {
Anand Doshibd67e872014-04-11 16:51:27 +053032 if(me.frm.fields_dict[opts[0]])
Anand Doshid5d39ac2013-07-29 13:28:37 +053033 me.frm.set_query(opts[0], erpnext.queries[opts[1]]);
34 });
Anand Doshibd67e872014-04-11 16:51:27 +053035
Rushabh Mehtab92087c2017-01-13 18:53:11 +053036 me.frm.set_query('contact_person', erpnext.queries.contact_query);
37 me.frm.set_query('customer_address', erpnext.queries.address_query);
38 me.frm.set_query('shipping_address_name', erpnext.queries.address_query);
39
Akhilesh Darjee4f721562014-01-29 16:31:38 +053040 if(this.frm.fields_dict.taxes_and_charges) {
41 this.frm.set_query("taxes_and_charges", function() {
Saurabhf52dc072013-07-10 13:07:49 +053042 return {
43 filters: [
Rushabh Mehtaab1ed012015-05-08 11:08:48 +053044 ['Sales Taxes and Charges Template', 'company', '=', me.frm.doc.company],
45 ['Sales Taxes and Charges Template', 'docstatus', '!=', 2]
Saurabhf52dc072013-07-10 13:07:49 +053046 ]
47 }
Anand Doshi99100a42013-07-04 17:13:53 +053048 });
49 }
Anand Doshi99100a42013-07-04 17:13:53 +053050
Rushabh Mehta4a404e92013-08-09 18:11:35 +053051 if(this.frm.fields_dict.selling_price_list) {
52 this.frm.set_query("selling_price_list", function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +053053 return { filters: { selling: 1 } };
Anand Doshi720a01a2013-07-26 11:32:02 +053054 });
Anand Doshi720a01a2013-07-26 11:32:02 +053055 }
Anand Doshibd67e872014-04-11 16:51:27 +053056
Nabin Haitdd38a262014-12-26 13:15:21 +053057 if(!this.frm.fields_dict["items"]) {
Anand Doshi99100a42013-07-04 17:13:53 +053058 return;
59 }
Anand Doshibd67e872014-04-11 16:51:27 +053060
Nabin Haitdd38a262014-12-26 13:15:21 +053061 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
62 this.frm.set_query("item_code", "items", function() {
Anand Doshi9b496142013-07-11 19:13:58 +053063 return {
Rushabh Mehtad973c162016-08-16 11:32:08 +053064 query: "erpnext.controllers.queries.item_query",
65 filters: {'is_sales_item': 1}
Anand Doshi9b496142013-07-11 19:13:58 +053066 }
Anand Doshi99100a42013-07-04 17:13:53 +053067 });
68 }
Anand Doshibd67e872014-04-11 16:51:27 +053069
Rushabh Mehta698c0402016-11-22 23:16:40 +053070 if(this.frm.fields_dict["packed_items"] &&
71 this.frm.fields_dict["packed_items"].grid.get_field('batch_no')) {
Rohit Waghchauree887e922016-11-18 12:16:22 +053072 this.frm.set_query("batch_no", "packed_items", function(doc, cdt, cdn) {
73 return me.set_query_for_batch(doc, cdt, cdn)
74 });
75 }
76
Nabin Haitdd38a262014-12-26 13:15:21 +053077 if(this.frm.fields_dict["items"].grid.get_field('batch_no')) {
78 this.frm.set_query("batch_no", "items", function(doc, cdt, cdn) {
Rohit Waghchauree887e922016-11-18 12:16:22 +053079 return me.set_query_for_batch(doc, cdt, cdn)
Anand Doshi99100a42013-07-04 17:13:53 +053080 });
81 }
Anand Doshi1dde46a2013-05-15 21:15:57 +053082 },
Anand Doshibd67e872014-04-11 16:51:27 +053083
Rohit Waghchauree887e922016-11-18 12:16:22 +053084 set_query_for_batch: function(doc, cdt, cdn) {
85 // Show item's batches in the dropdown of batch no
86
87 var me = this;
88 var item = frappe.get_doc(cdt, cdn);
89
90 if(!item.item_code) {
91 frappe.throw(__("Please enter Item Code to get batch no"));
92 } else {
93 filters = {
94 'item_code': item.item_code,
95 'posting_date': me.frm.doc.posting_date || frappe.datetime.nowdate(),
96 }
97 if(item.warehouse) filters["warehouse"] = item.warehouse
98
99 return {
100 query : "erpnext.controllers.queries.get_batch_no",
101 filters: filters
102 }
103 }
104 },
105
Anand Doshic4a54fe2013-08-01 18:19:51 +0530106 refresh: function() {
107 this._super();
Rushabh Mehtab92087c2017-01-13 18:53:11 +0530108
Rushabh Mehta8d39fd92017-01-16 13:06:07 +0530109 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
Rushabh Mehtab92087c2017-01-13 18:53:11 +0530110
Anand Doshibd67e872014-04-11 16:51:27 +0530111 this.frm.toggle_display("customer_name",
Akhilesh Darjee5ce1b8b2013-12-09 16:29:04 +0530112 (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
Nabin Haite7d15362014-12-25 16:01:55 +0530113 if(this.frm.fields_dict.packed_items) {
114 var packing_list_exists = (this.frm.doc.packed_items || []).length;
Anand Doshic4a54fe2013-08-01 18:19:51 +0530115 this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
116 }
Nabin Hait2029ea92015-04-16 17:55:35 +0530117 this.toggle_editable_price_list_rate();
Rushabh Mehta8aded132013-07-04 12:50:52 +0530118 },
Anand Doshibd67e872014-04-11 16:51:27 +0530119
Anand Doshi3543f302013-05-24 19:25:01 +0530120 customer: function() {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530121 var me = this;
122 erpnext.utils.get_party_details(this.frm, null, null, function(){me.apply_pricing_rule()});
Anand Doshi3543f302013-05-24 19:25:01 +0530123 },
Anand Doshibd67e872014-04-11 16:51:27 +0530124
Nabin Haita279d782013-07-15 13:04:33 +0530125 customer_address: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530126 erpnext.utils.get_address_display(this.frm, "customer_address");
Nabin Haita279d782013-07-15 13:04:33 +0530127 },
Anand Doshibd67e872014-04-11 16:51:27 +0530128
Nabin Hait9d1f0772014-02-19 17:43:24 +0530129 shipping_address_name: function() {
130 erpnext.utils.get_address_display(this.frm, "shipping_address_name", "shipping_address");
131 },
Anand Doshibd67e872014-04-11 16:51:27 +0530132
Nabin Haita3dd72a2014-05-28 12:49:20 +0530133 sales_partner: function() {
134 this.apply_pricing_rule();
135 },
136
137 campaign: function() {
138 this.apply_pricing_rule();
139 },
140
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530141 selling_price_list: function() {
Anand Doshidffec8f2014-07-01 17:45:15 +0530142 this.apply_price_list();
Anand Doshif3096132013-05-21 19:35:06 +0530143 },
Anand Doshibd67e872014-04-11 16:51:27 +0530144
Nabin Haita7f757a2014-02-10 17:54:04 +0530145 price_list_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530146 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530147 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530148
mbauskara52472c2016-03-05 15:10:25 +0530149 // check if child doctype is Sales Order Item/Qutation Item and calculate the rate
150 if(in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), cdt)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530151 this.apply_pricing_rule_on_item(item);
mbauskara52472c2016-03-05 15:10:25 +0530152 else
153 item.rate = flt(item.price_list_rate * (1 - item.discount_percentage / 100.0),
154 precision("rate", item));
155
Anand Doshif3096132013-05-21 19:35:06 +0530156 this.calculate_taxes_and_totals();
157 },
Anand Doshibd67e872014-04-11 16:51:27 +0530158
Nabin Haita7f757a2014-02-10 17:54:04 +0530159 discount_percentage: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530160 var item = frappe.get_doc(cdt, cdn);
Nabin Haita7f757a2014-02-10 17:54:04 +0530161 if(!item.price_list_rate) {
162 item.discount_percentage = 0.0;
Anand Doshi923d41d2013-05-28 17:23:36 +0530163 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530164 this.price_list_rate(doc, cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530165 }
Saurabh5ada14b2016-02-26 18:02:55 +0530166 this.set_gross_profit(item);
Anand Doshif3096132013-05-21 19:35:06 +0530167 },
Anand Doshibd67e872014-04-11 16:51:27 +0530168
Anand Doshif3096132013-05-21 19:35:06 +0530169 commission_rate: function() {
170 this.calculate_commission();
171 refresh_field("total_commission");
172 },
Anand Doshibd67e872014-04-11 16:51:27 +0530173
Anand Doshif3096132013-05-21 19:35:06 +0530174 total_commission: function() {
Nabin Hait5690be12015-02-12 16:09:11 +0530175 if(this.frm.doc.base_net_total) {
176 frappe.model.round_floats_in(this.frm.doc, ["base_net_total", "total_commission"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530177
Nabin Hait5690be12015-02-12 16:09:11 +0530178 if(this.frm.doc.base_net_total < this.frm.doc.total_commission) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530179 var msg = (__("[Error]") + " " +
180 __(frappe.meta.get_label(this.frm.doc.doctype, "total_commission",
Anand Doshibd67e872014-04-11 16:51:27 +0530181 this.frm.doc.name)) + " > " +
Nabin Hait5690be12015-02-12 16:09:11 +0530182 __(frappe.meta.get_label(this.frm.doc.doctype, "base_net_total", this.frm.doc.name)));
Anand Doshif3096132013-05-21 19:35:06 +0530183 msgprint(msg);
184 throw msg;
185 }
Anand Doshibd67e872014-04-11 16:51:27 +0530186
187 this.frm.set_value("commission_rate",
Nabin Hait5690be12015-02-12 16:09:11 +0530188 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.base_net_total));
Anand Doshif3096132013-05-21 19:35:06 +0530189 }
190 },
Anand Doshibd67e872014-04-11 16:51:27 +0530191
Anand Doshif3096132013-05-21 19:35:06 +0530192 allocated_percentage: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530193 var sales_person = frappe.get_doc(cdt, cdn);
Anand Doshibd67e872014-04-11 16:51:27 +0530194
Anand Doshif3096132013-05-21 19:35:06 +0530195 if(sales_person.allocated_percentage) {
196 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
197 precision("allocated_percentage", sales_person));
Nabin Hait5690be12015-02-12 16:09:11 +0530198 sales_person.allocated_amount = flt(this.frm.doc.base_net_total *
Anand Doshibd67e872014-04-11 16:51:27 +0530199 sales_person.allocated_percentage / 100.0,
Anand Doshif3096132013-05-21 19:35:06 +0530200 precision("allocated_amount", sales_person));
201
202 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
203 sales_person.parentfield);
204 }
205 },
Anand Doshibd67e872014-04-11 16:51:27 +0530206
Anand Doshifc777182013-05-27 19:29:07 +0530207 warehouse: function(doc, cdt, cdn) {
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530208 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530209 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehtabe2ee182016-04-29 17:22:42 +0530210
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530211 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530212 return this.frm.call({
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530213 method: "erpnext.stock.get_item_details.get_bin_details_and_serial_nos",
Anand Doshifc777182013-05-27 19:29:07 +0530214 child: item,
215 args: {
216 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530217 warehouse: item.warehouse,
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530218 stock_qty: item.stock_qty,
Nabin Hait1d90b412016-12-24 10:42:23 +0530219 serial_no: item.serial_no || ""
Anand Doshifc777182013-05-27 19:29:07 +0530220 },
Saurabhd3135532016-02-25 18:59:20 +0530221 callback:function(r){
222 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
223 me.batch_no(doc, cdt, cdn);
224 }
225 }
Anand Doshifc777182013-05-27 19:29:07 +0530226 });
227 }
228 },
Anand Doshibd67e872014-04-11 16:51:27 +0530229
Anand Doshi33fe8672013-08-02 12:39:10 +0530230 toggle_editable_price_list_rate: function() {
Nabin Haitdd38a262014-12-26 13:15:21 +0530231 var df = frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "price_list_rate", this.frm.doc.name);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530232 var editable_price_list_rate = cint(frappe.defaults.get_default("editable_price_list_rate"));
Anand Doshibd67e872014-04-11 16:51:27 +0530233
Anand Doshi33fe8672013-08-02 12:39:10 +0530234 if(df && editable_price_list_rate) {
235 df.read_only = 0;
236 }
237 },
Anand Doshibd67e872014-04-11 16:51:27 +0530238
Anand Doshif3096132013-05-21 19:35:06 +0530239 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530240 if(this.frm.fields_dict.commission_rate) {
241 if(this.frm.doc.commission_rate > 100) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530242 var msg = __(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
243 " " + __("cannot be greater than 100");
Anand Doshi923d41d2013-05-28 17:23:36 +0530244 msgprint(msg);
245 throw msg;
246 }
Anand Doshibd67e872014-04-11 16:51:27 +0530247
Nabin Hait5690be12015-02-12 16:09:11 +0530248 this.frm.doc.total_commission = flt(this.frm.doc.base_net_total * this.frm.doc.commission_rate / 100.0,
Anand Doshi923d41d2013-05-28 17:23:36 +0530249 precision("total_commission"));
250 }
Anand Doshif3096132013-05-21 19:35:06 +0530251 },
Anand Doshibd67e872014-04-11 16:51:27 +0530252
Anand Doshif3096132013-05-21 19:35:06 +0530253 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530254 var me = this;
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530255 $.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530256 frappe.model.round_floats_in(sales_person);
Anand Doshif3096132013-05-21 19:35:06 +0530257 if(sales_person.allocated_percentage) {
258 sales_person.allocated_amount = flt(
Nabin Hait5690be12015-02-12 16:09:11 +0530259 me.frm.doc.base_net_total * sales_person.allocated_percentage / 100.0,
Anand Doshif3096132013-05-21 19:35:06 +0530260 precision("allocated_amount", sales_person));
261 }
262 });
263 },
Anand Doshibd67e872014-04-11 16:51:27 +0530264
Anand Doshicefccb92013-07-15 18:28:14 +0530265 shipping_rule: function() {
266 var me = this;
267 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530268 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530269 doc: this.frm.doc,
270 method: "apply_shipping_rule",
271 callback: function(r) {
272 if(!r.exc) {
273 me.calculate_taxes_and_totals();
274 }
275 }
276 })
277 }
278 },
Anand Doshibd67e872014-04-11 16:51:27 +0530279
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530280 batch_no: function(doc, cdt, cdn) {
281 var me = this;
282 var item = frappe.get_doc(cdt, cdn);
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530283
Sambhaji Kolatea4fa96c2015-03-30 16:04:55 +0530284 if(item.warehouse && item.item_code && item.batch_no) {
Sambhaji Kolatefeff40a2015-03-11 11:50:28 +0530285 return this.frm.call({
286 method: "erpnext.stock.get_item_details.get_batch_qty",
287 child: item,
288 args: {
289 "batch_no": item.batch_no,
290 "warehouse": item.warehouse,
291 "item_code": item.item_code
292 },
293 "fieldname": "actual_batch_qty"
294 });
295 }
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530296 },
297
Anand Doshi2168e392013-05-23 19:25:08 +0530298 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530299 this._super();
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530300 this.set_product_bundle_help(this.frm.doc);
Nabin Hait0b157552014-06-24 17:02:45 +0530301 },
302
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530303 set_product_bundle_help: function(doc) {
Nabin Hait0b157552014-06-24 17:02:45 +0530304 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haite7d15362014-12-25 16:01:55 +0530305 if ((doc.packed_items || []).length) {
Nabin Hait0b157552014-06-24 17:02:45 +0530306 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
307
308 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
309 help_msg = "<div class='alert alert-warning'>" +
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530310 __("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.")+
Nabin Hait0b157552014-06-24 17:02:45 +0530311 "</div>";
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530312 frappe.meta.get_docfield(doc.doctype, 'product_bundle_help', doc.name).options = help_msg;
Nabin Hait0b157552014-06-24 17:02:45 +0530313 }
314 } else {
315 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
316 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530317 frappe.meta.get_docfield(doc.doctype, 'product_bundle_help', doc.name).options = '';
Nabin Hait0b157552014-06-24 17:02:45 +0530318 }
319 }
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530320 refresh_field('product_bundle_help');
Saurabheba7aa42015-12-16 09:57:59 +0530321 },
Anand Doshi01de9452016-01-26 16:22:50 +0530322
Saurabheba7aa42015-12-16 09:57:59 +0530323 make_payment_request: function() {
Saurabheba7aa42015-12-16 09:57:59 +0530324 frappe.call({
325 method:"erpnext.accounts.doctype.payment_request.payment_request.make_payment_request",
326 args: {
327 "dt": cur_frm.doc.doctype,
328 "dn": cur_frm.doc.name,
Saurabhb109ee92015-12-16 20:48:40 +0530329 "recipient_id": cur_frm.doc.contact_email
Saurabheba7aa42015-12-16 09:57:59 +0530330 },
331 callback: function(r) {
Saurabhcb98d9e2015-12-17 09:48:44 +0530332 if(!r.exc){
Saurabh3ba22672015-12-21 18:24:49 +0530333 var doc = frappe.model.sync(r.message);
Saurabh3ba22672015-12-21 18:24:49 +0530334 frappe.set_route("Form", r.message.doctype, r.message.name);
Saurabhcb98d9e2015-12-17 09:48:44 +0530335 }
Saurabheba7aa42015-12-16 09:57:59 +0530336 }
337 })
mbauskar1e9c1642016-01-18 16:21:25 +0530338 },
mbauskar1e428ae2016-01-19 19:52:22 +0530339
mbauskara52472c2016-03-05 15:10:25 +0530340 margin_rate_or_amount: function(doc, cdt, cdn) {
mbauskar1e9c1642016-01-18 16:21:25 +0530341 // calculated the revised total margin and rate on margin rate changes
342 item = locals[cdt][cdn];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530343 this.apply_pricing_rule_on_item(item)
mbauskara52472c2016-03-05 15:10:25 +0530344 this.calculate_taxes_and_totals();
mbauskar1e9c1642016-01-18 16:21:25 +0530345 cur_frm.refresh_fields();
346 },
mbauskar1e428ae2016-01-19 19:52:22 +0530347
mbauskara52472c2016-03-05 15:10:25 +0530348 margin_type: function(doc, cdt, cdn){
mbauskar1e9c1642016-01-18 16:21:25 +0530349 // calculate the revised total margin and rate on margin type changes
350 item = locals[cdt][cdn];
Nabin Haite72c98d2017-05-24 09:01:44 +0530351 if(!item.margin_type) {
352 frappe.model.set_value(cdt, cdn, "margin_rate_or_amount", 0);
353 } else {
354 this.apply_pricing_rule_on_item(item, doc,cdt, cdn)
355 this.calculate_taxes_and_totals();
356 cur_frm.refresh_fields();
357 }
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530358 }
Nabin Hait0b157552014-06-24 17:02:45 +0530359});
Anand Doshi43f087c2014-08-26 14:25:53 +0530360
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530361frappe.ui.form.on(cur_frm.doctype,"project", function(frm) {
Nabin Hait10b155a2014-10-23 13:18:59 +0530362 if(in_list(["Delivery Note", "Sales Invoice"], frm.doc.doctype)) {
Rushabh Mehta532eedf2016-05-19 18:22:49 +0530363 if(frm.doc.project) {
364 frappe.call({
365 method:'erpnext.projects.doctype.project.project.get_cost_center_name' ,
366 args: { project: frm.doc.project },
367 callback: function(r, rt) {
368 if(!r.exc) {
369 $.each(frm.doc["items"] || [], function(i, row) {
370 if(r.message) {
371 frappe.model.set_value(row.doctype, row.name, "cost_center", r.message);
372 msgprint(__("Cost Center For Item with Item Code '"+row.item_name+"' has been Changed to "+ r.message));
373 }
374 })
375 }
Nabin Hait10b155a2014-10-23 13:18:59 +0530376 }
Rushabh Mehta532eedf2016-05-19 18:22:49 +0530377 })
378 }
Nabin Hait10b155a2014-10-23 13:18:59 +0530379 }
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530380})