blob: bfefb9fa5b1a15511474c0ae1f468c64f961bb30 [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 Mehta009378e2015-05-06 22:59:00 +05306{% include 'accounts/doctype/sales_taxes_and_charges_template/sales_taxes_and_charges_template.js' %}
Nabin Hait613d0812015-02-23 11:58:15 +05307
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05308frappe.provide("erpnext.selling");
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05309frappe.require("assets/erpnext/js/controllers/transaction.js");
Rushabh Mehtab09d9da2014-01-02 11:47:23 +053010
Rushabh Mehtae10cf022014-09-12 13:09:41 +053011cur_frm.email_field = "contact_email";
12
Anand Doshi3543f302013-05-24 19:25:01 +053013erpnext.selling.SellingController = erpnext.TransactionController.extend({
Anand Doshi9b496142013-07-11 19:13:58 +053014 onload: function() {
15 this._super();
Anand Doshi9b496142013-07-11 19:13:58 +053016 this.setup_queries();
17 },
Anand Doshibd67e872014-04-11 16:51:27 +053018
Anand Doshi9b496142013-07-11 19:13:58 +053019 setup_queries: function() {
Anand Doshi99100a42013-07-04 17:13:53 +053020 var me = this;
Anand Doshibd67e872014-04-11 16:51:27 +053021
Anand Doshif3096132013-05-21 19:35:06 +053022 this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate");
Anand Doshibd67e872014-04-11 16:51:27 +053023
24 $.each([["customer_address", "customer_filter"],
Anand Doshid5d39ac2013-07-29 13:28:37 +053025 ["shipping_address_name", "customer_filter"],
Anand Doshibd67e872014-04-11 16:51:27 +053026 ["contact_person", "customer_filter"],
27 ["customer", "customer"],
28 ["lead", "lead"]],
Anand Doshid5d39ac2013-07-29 13:28:37 +053029 function(i, opts) {
Anand Doshibd67e872014-04-11 16:51:27 +053030 if(me.frm.fields_dict[opts[0]])
Anand Doshid5d39ac2013-07-29 13:28:37 +053031 me.frm.set_query(opts[0], erpnext.queries[opts[1]]);
32 });
Anand Doshibd67e872014-04-11 16:51:27 +053033
Akhilesh Darjee4f721562014-01-29 16:31:38 +053034 if(this.frm.fields_dict.taxes_and_charges) {
35 this.frm.set_query("taxes_and_charges", function() {
Saurabhf52dc072013-07-10 13:07:49 +053036 return {
37 filters: [
Rushabh Mehtaab1ed012015-05-08 11:08:48 +053038 ['Sales Taxes and Charges Template', 'company', '=', me.frm.doc.company],
39 ['Sales Taxes and Charges Template', 'docstatus', '!=', 2]
Saurabhf52dc072013-07-10 13:07:49 +053040 ]
41 }
Anand Doshi99100a42013-07-04 17:13:53 +053042 });
43 }
Anand Doshi99100a42013-07-04 17:13:53 +053044
Rushabh Mehta4a404e92013-08-09 18:11:35 +053045 if(this.frm.fields_dict.selling_price_list) {
46 this.frm.set_query("selling_price_list", function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +053047 return { filters: { selling: 1 } };
Anand Doshi720a01a2013-07-26 11:32:02 +053048 });
Anand Doshi720a01a2013-07-26 11:32:02 +053049 }
Anand Doshibd67e872014-04-11 16:51:27 +053050
Nabin Haitdd38a262014-12-26 13:15:21 +053051 if(!this.frm.fields_dict["items"]) {
Anand Doshi99100a42013-07-04 17:13:53 +053052 return;
53 }
Anand Doshibd67e872014-04-11 16:51:27 +053054
Nabin Haitdd38a262014-12-26 13:15:21 +053055 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
56 this.frm.set_query("item_code", "items", function() {
Anand Doshi9b496142013-07-11 19:13:58 +053057 return {
Rushabh Mehta6de403f2013-12-13 14:10:14 +053058 query: "erpnext.controllers.queries.item_query",
Anand Doshi01de9452016-01-26 16:22:50 +053059 filters: {'is_sales_item': 1}
Anand Doshi9b496142013-07-11 19:13:58 +053060 }
Anand Doshi99100a42013-07-04 17:13:53 +053061 });
62 }
Anand Doshibd67e872014-04-11 16:51:27 +053063
Nabin Haitdd38a262014-12-26 13:15:21 +053064 if(this.frm.fields_dict["items"].grid.get_field('batch_no')) {
65 this.frm.set_query("batch_no", "items", function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +053066 var item = frappe.get_doc(cdt, cdn);
Anand Doshi99100a42013-07-04 17:13:53 +053067 if(!item.item_code) {
Pratik Vyasb52618c2014-04-14 16:25:30 +053068 frappe.throw(__("Please enter Item Code to get batch no"));
Anand Doshi99100a42013-07-04 17:13:53 +053069 } else {
Nabin Haitd1fd1e22013-10-18 12:29:11 +053070 filters = {
71 'item_code': item.item_code,
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +053072 'posting_date': me.frm.doc.posting_date || nowdate(),
Nabin Haitd1fd1e22013-10-18 12:29:11 +053073 }
74 if(item.warehouse) filters["warehouse"] = item.warehouse
Anand Doshibd67e872014-04-11 16:51:27 +053075
Nabin Haitd1fd1e22013-10-18 12:29:11 +053076 return {
Akhilesh Darjee4f721562014-01-29 16:31:38 +053077 query : "erpnext.controllers.queries.get_batch_no",
Nabin Haitd1fd1e22013-10-18 12:29:11 +053078 filters: filters
Anand Doshi99100a42013-07-04 17:13:53 +053079 }
80 }
81 });
82 }
Anand Doshi1dde46a2013-05-15 21:15:57 +053083 },
Anand Doshibd67e872014-04-11 16:51:27 +053084
Anand Doshic4a54fe2013-08-01 18:19:51 +053085 refresh: function() {
86 this._super();
Anand Doshibd67e872014-04-11 16:51:27 +053087 this.frm.toggle_display("customer_name",
Akhilesh Darjee5ce1b8b2013-12-09 16:29:04 +053088 (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
Nabin Haite7d15362014-12-25 16:01:55 +053089 if(this.frm.fields_dict.packed_items) {
90 var packing_list_exists = (this.frm.doc.packed_items || []).length;
Anand Doshic4a54fe2013-08-01 18:19:51 +053091 this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
92 }
Nabin Hait2029ea92015-04-16 17:55:35 +053093 this.toggle_editable_price_list_rate();
Rushabh Mehta8aded132013-07-04 12:50:52 +053094 },
Anand Doshibd67e872014-04-11 16:51:27 +053095
Anand Doshi3543f302013-05-24 19:25:01 +053096 customer: function() {
Nabin Haita3dd72a2014-05-28 12:49:20 +053097 var me = this;
98 erpnext.utils.get_party_details(this.frm, null, null, function(){me.apply_pricing_rule()});
Anand Doshi3543f302013-05-24 19:25:01 +053099 },
Anand Doshibd67e872014-04-11 16:51:27 +0530100
Nabin Haita279d782013-07-15 13:04:33 +0530101 customer_address: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530102 erpnext.utils.get_address_display(this.frm, "customer_address");
Nabin Haita279d782013-07-15 13:04:33 +0530103 },
Anand Doshibd67e872014-04-11 16:51:27 +0530104
Nabin Hait9d1f0772014-02-19 17:43:24 +0530105 shipping_address_name: function() {
106 erpnext.utils.get_address_display(this.frm, "shipping_address_name", "shipping_address");
107 },
Anand Doshibd67e872014-04-11 16:51:27 +0530108
Nabin Haita3dd72a2014-05-28 12:49:20 +0530109 sales_partner: function() {
110 this.apply_pricing_rule();
111 },
112
113 campaign: function() {
114 this.apply_pricing_rule();
115 },
116
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530117 selling_price_list: function() {
Anand Doshidffec8f2014-07-01 17:45:15 +0530118 this.apply_price_list();
Anand Doshif3096132013-05-21 19:35:06 +0530119 },
Anand Doshibd67e872014-04-11 16:51:27 +0530120
Nabin Haita7f757a2014-02-10 17:54:04 +0530121 price_list_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530122 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530123 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530124
mbauskara52472c2016-03-05 15:10:25 +0530125 // check if child doctype is Sales Order Item/Qutation Item and calculate the rate
126 if(in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), cdt)
127 this.calculate_revised_margin_and_rate(item, doc,cdt, cdn);
128 else
129 item.rate = flt(item.price_list_rate * (1 - item.discount_percentage / 100.0),
130 precision("rate", item));
131
Anand Doshif3096132013-05-21 19:35:06 +0530132 this.calculate_taxes_and_totals();
133 },
Anand Doshibd67e872014-04-11 16:51:27 +0530134
Nabin Haita7f757a2014-02-10 17:54:04 +0530135 discount_percentage: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530136 var item = frappe.get_doc(cdt, cdn);
Nabin Haita7f757a2014-02-10 17:54:04 +0530137 if(!item.price_list_rate) {
138 item.discount_percentage = 0.0;
Anand Doshi923d41d2013-05-28 17:23:36 +0530139 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530140 this.price_list_rate(doc, cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530141 }
Saurabh5ada14b2016-02-26 18:02:55 +0530142 this.set_gross_profit(item);
Anand Doshif3096132013-05-21 19:35:06 +0530143 },
Anand Doshibd67e872014-04-11 16:51:27 +0530144
Anand Doshif3096132013-05-21 19:35:06 +0530145 commission_rate: function() {
146 this.calculate_commission();
147 refresh_field("total_commission");
148 },
Anand Doshibd67e872014-04-11 16:51:27 +0530149
Anand Doshif3096132013-05-21 19:35:06 +0530150 total_commission: function() {
Nabin Hait5690be12015-02-12 16:09:11 +0530151 if(this.frm.doc.base_net_total) {
152 frappe.model.round_floats_in(this.frm.doc, ["base_net_total", "total_commission"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530153
Nabin Hait5690be12015-02-12 16:09:11 +0530154 if(this.frm.doc.base_net_total < this.frm.doc.total_commission) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530155 var msg = (__("[Error]") + " " +
156 __(frappe.meta.get_label(this.frm.doc.doctype, "total_commission",
Anand Doshibd67e872014-04-11 16:51:27 +0530157 this.frm.doc.name)) + " > " +
Nabin Hait5690be12015-02-12 16:09:11 +0530158 __(frappe.meta.get_label(this.frm.doc.doctype, "base_net_total", this.frm.doc.name)));
Anand Doshif3096132013-05-21 19:35:06 +0530159 msgprint(msg);
160 throw msg;
161 }
Anand Doshibd67e872014-04-11 16:51:27 +0530162
163 this.frm.set_value("commission_rate",
Nabin Hait5690be12015-02-12 16:09:11 +0530164 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.base_net_total));
Anand Doshif3096132013-05-21 19:35:06 +0530165 }
166 },
Anand Doshibd67e872014-04-11 16:51:27 +0530167
Anand Doshif3096132013-05-21 19:35:06 +0530168 allocated_percentage: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530169 var sales_person = frappe.get_doc(cdt, cdn);
Anand Doshibd67e872014-04-11 16:51:27 +0530170
Anand Doshif3096132013-05-21 19:35:06 +0530171 if(sales_person.allocated_percentage) {
172 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
173 precision("allocated_percentage", sales_person));
Nabin Hait5690be12015-02-12 16:09:11 +0530174 sales_person.allocated_amount = flt(this.frm.doc.base_net_total *
Anand Doshibd67e872014-04-11 16:51:27 +0530175 sales_person.allocated_percentage / 100.0,
Anand Doshif3096132013-05-21 19:35:06 +0530176 precision("allocated_amount", sales_person));
177
178 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
179 sales_person.parentfield);
180 }
181 },
Anand Doshibd67e872014-04-11 16:51:27 +0530182
Anand Doshifc777182013-05-27 19:29:07 +0530183 warehouse: function(doc, cdt, cdn) {
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530184 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530185 var item = frappe.get_doc(cdt, cdn);
Saurabhd3135532016-02-25 18:59:20 +0530186
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530187 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530188 return this.frm.call({
Saurabhd3135532016-02-25 18:59:20 +0530189 method: "erpnext.stock.get_item_details.get_bin_details",
Anand Doshifc777182013-05-27 19:29:07 +0530190 child: item,
191 args: {
192 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530193 warehouse: item.warehouse,
Anand Doshifc777182013-05-27 19:29:07 +0530194 },
Saurabhd3135532016-02-25 18:59:20 +0530195 callback:function(r){
196 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
197 me.batch_no(doc, cdt, cdn);
198 }
199 }
Anand Doshifc777182013-05-27 19:29:07 +0530200 });
201 }
202 },
Anand Doshibd67e872014-04-11 16:51:27 +0530203
Anand Doshi33fe8672013-08-02 12:39:10 +0530204 toggle_editable_price_list_rate: function() {
Nabin Haitdd38a262014-12-26 13:15:21 +0530205 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 +0530206 var editable_price_list_rate = cint(frappe.defaults.get_default("editable_price_list_rate"));
Anand Doshibd67e872014-04-11 16:51:27 +0530207
Anand Doshi33fe8672013-08-02 12:39:10 +0530208 if(df && editable_price_list_rate) {
209 df.read_only = 0;
210 }
211 },
Anand Doshibd67e872014-04-11 16:51:27 +0530212
Anand Doshif3096132013-05-21 19:35:06 +0530213 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530214 if(this.frm.fields_dict.commission_rate) {
215 if(this.frm.doc.commission_rate > 100) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530216 var msg = __(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
217 " " + __("cannot be greater than 100");
Anand Doshi923d41d2013-05-28 17:23:36 +0530218 msgprint(msg);
219 throw msg;
220 }
Anand Doshibd67e872014-04-11 16:51:27 +0530221
Nabin Hait5690be12015-02-12 16:09:11 +0530222 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 +0530223 precision("total_commission"));
224 }
Anand Doshif3096132013-05-21 19:35:06 +0530225 },
Anand Doshibd67e872014-04-11 16:51:27 +0530226
Anand Doshif3096132013-05-21 19:35:06 +0530227 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530228 var me = this;
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530229 $.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530230 frappe.model.round_floats_in(sales_person);
Anand Doshif3096132013-05-21 19:35:06 +0530231 if(sales_person.allocated_percentage) {
232 sales_person.allocated_amount = flt(
Nabin Hait5690be12015-02-12 16:09:11 +0530233 me.frm.doc.base_net_total * sales_person.allocated_percentage / 100.0,
Anand Doshif3096132013-05-21 19:35:06 +0530234 precision("allocated_amount", sales_person));
235 }
236 });
237 },
Anand Doshibd67e872014-04-11 16:51:27 +0530238
Anand Doshicefccb92013-07-15 18:28:14 +0530239 shipping_rule: function() {
240 var me = this;
241 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530242 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530243 doc: this.frm.doc,
244 method: "apply_shipping_rule",
245 callback: function(r) {
246 if(!r.exc) {
247 me.calculate_taxes_and_totals();
248 }
249 }
250 })
251 }
252 },
Anand Doshibd67e872014-04-11 16:51:27 +0530253
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530254 batch_no: function(doc, cdt, cdn) {
255 var me = this;
256 var item = frappe.get_doc(cdt, cdn);
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530257
Sambhaji Kolatea4fa96c2015-03-30 16:04:55 +0530258 if(item.warehouse && item.item_code && item.batch_no) {
Sambhaji Kolatefeff40a2015-03-11 11:50:28 +0530259 return this.frm.call({
260 method: "erpnext.stock.get_item_details.get_batch_qty",
261 child: item,
262 args: {
263 "batch_no": item.batch_no,
264 "warehouse": item.warehouse,
265 "item_code": item.item_code
266 },
267 "fieldname": "actual_batch_qty"
268 });
269 }
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530270 },
271
Anand Doshi2168e392013-05-23 19:25:08 +0530272 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530273 this._super();
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530274 this.set_product_bundle_help(this.frm.doc);
Nabin Hait0b157552014-06-24 17:02:45 +0530275 },
276
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530277 set_product_bundle_help: function(doc) {
Nabin Hait0b157552014-06-24 17:02:45 +0530278 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haite7d15362014-12-25 16:01:55 +0530279 if ((doc.packed_items || []).length) {
Nabin Hait0b157552014-06-24 17:02:45 +0530280 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
281
282 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
283 help_msg = "<div class='alert alert-warning'>" +
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530284 __("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 +0530285 "</div>";
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530286 frappe.meta.get_docfield(doc.doctype, 'product_bundle_help', doc.name).options = help_msg;
Nabin Hait0b157552014-06-24 17:02:45 +0530287 }
288 } else {
289 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
290 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530291 frappe.meta.get_docfield(doc.doctype, 'product_bundle_help', doc.name).options = '';
Nabin Hait0b157552014-06-24 17:02:45 +0530292 }
293 }
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530294 refresh_field('product_bundle_help');
Saurabheba7aa42015-12-16 09:57:59 +0530295 },
Anand Doshi01de9452016-01-26 16:22:50 +0530296
Saurabheba7aa42015-12-16 09:57:59 +0530297 make_payment_request: function() {
Saurabheba7aa42015-12-16 09:57:59 +0530298 frappe.call({
299 method:"erpnext.accounts.doctype.payment_request.payment_request.make_payment_request",
300 args: {
301 "dt": cur_frm.doc.doctype,
302 "dn": cur_frm.doc.name,
Saurabhb109ee92015-12-16 20:48:40 +0530303 "recipient_id": cur_frm.doc.contact_email
Saurabheba7aa42015-12-16 09:57:59 +0530304 },
305 callback: function(r) {
Saurabhcb98d9e2015-12-17 09:48:44 +0530306 if(!r.exc){
Saurabh3ba22672015-12-21 18:24:49 +0530307 var doc = frappe.model.sync(r.message);
Saurabh3ba22672015-12-21 18:24:49 +0530308 frappe.set_route("Form", r.message.doctype, r.message.name);
Saurabhcb98d9e2015-12-17 09:48:44 +0530309 }
Saurabheba7aa42015-12-16 09:57:59 +0530310 }
311 })
mbauskar1e9c1642016-01-18 16:21:25 +0530312 },
mbauskar1e428ae2016-01-19 19:52:22 +0530313
314 rate: function(doc, cdt, cdn){
315 // if user changes the rate then set margin Rate or amount to 0
316 item = locals[cdt][cdn];
mbauskara52472c2016-03-05 15:10:25 +0530317 item.margin_type = "";
318 item.margin_rate_or_amount = 0.0;
mbauskar1e428ae2016-01-19 19:52:22 +0530319 cur_frm.refresh_fields();
320 },
321
mbauskara52472c2016-03-05 15:10:25 +0530322 margin_rate_or_amount: function(doc, cdt, cdn) {
mbauskar1e9c1642016-01-18 16:21:25 +0530323 // calculated the revised total margin and rate on margin rate changes
324 item = locals[cdt][cdn];
mbauskar1e428ae2016-01-19 19:52:22 +0530325 this.calculate_revised_margin_and_rate(item)
mbauskara52472c2016-03-05 15:10:25 +0530326 this.calculate_taxes_and_totals();
mbauskar1e9c1642016-01-18 16:21:25 +0530327 cur_frm.refresh_fields();
328 },
mbauskar1e428ae2016-01-19 19:52:22 +0530329
mbauskara52472c2016-03-05 15:10:25 +0530330 margin_type: function(doc, cdt, cdn){
mbauskar1e9c1642016-01-18 16:21:25 +0530331 // calculate the revised total margin and rate on margin type changes
332 item = locals[cdt][cdn];
mbauskar1e428ae2016-01-19 19:52:22 +0530333 this.calculate_revised_margin_and_rate(item, doc,cdt, cdn)
mbauskara52472c2016-03-05 15:10:25 +0530334 this.calculate_taxes_and_totals();
mbauskar1e428ae2016-01-19 19:52:22 +0530335 cur_frm.refresh_fields();
336 },
337
338 calculate_revised_margin_and_rate: function(item){
mbauskara52472c2016-03-05 15:10:25 +0530339 if(in_list(["Percentage", "Amount"], item.margin_type)){
340 if(item.margin_type == "Percentage")
341 item.total_margin = item.price_list_rate + item.price_list_rate * ( item.margin_rate_or_amount / 100);
mbauskar1e428ae2016-01-19 19:52:22 +0530342 else
mbauskara52472c2016-03-05 15:10:25 +0530343 item.total_margin = item.price_list_rate + item.margin_rate_or_amount;
mbauskar1e428ae2016-01-19 19:52:22 +0530344 item.rate = flt(item.total_margin * (1 - item.discount_percentage / 100.0),
345 precision("rate", item));
mbauskar1e428ae2016-01-19 19:52:22 +0530346 }
347 else{
348 item.rate_or_amount = 0.0;
349 item.total_margin = 0.0;
350 item.rate = flt(item.price_list_rate * (1 - item.discount_percentage / 100.0),
351 precision("rate", item));
352 }
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530353 }
Nabin Hait0b157552014-06-24 17:02:45 +0530354});
Anand Doshi43f087c2014-08-26 14:25:53 +0530355
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530356frappe.ui.form.on(cur_frm.doctype,"project", function(frm) {
Nabin Hait10b155a2014-10-23 13:18:59 +0530357 if(in_list(["Delivery Note", "Sales Invoice"], frm.doc.doctype)) {
358 frappe.call({
359 method:'erpnext.projects.doctype.project.project.get_cost_center_name' ,
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530360 args: { project: frm.doc.project },
Nabin Hait10b155a2014-10-23 13:18:59 +0530361 callback: function(r, rt) {
362 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530363 $.each(frm.doc["items"] || [], function(i, row) {
Neil Trini Lasrado6fc28212014-10-28 14:36:26 +0530364 frappe.model.set_value(row.doctype, row.name, "cost_center", r.message);
365 msgprint(__("Cost Center For Item with Item Code '"+row.item_name+"' has been Changed to "+ r.message));
Nabin Hait10b155a2014-10-23 13:18:59 +0530366 })
367 }
Anand Doshi43f087c2014-08-26 14:25:53 +0530368 }
Nabin Hait10b155a2014-10-23 13:18:59 +0530369 })
370 }
Anand Doshi43f087c2014-08-26 14:25:53 +0530371})