blob: 50fadc8c8cd27fd97cc5054446f439da96ec11c4 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301// Copyright (c) 2013, Web Notes 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
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305frappe.provide("erpnext.selling");
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05306frappe.require("assets/erpnext/js/controllers/transaction.js");
Rushabh Mehtab09d9da2014-01-02 11:47:23 +05307
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05308{% include "public/js/controllers/accounts.js" %};
Anand Doshi1dde46a2013-05-15 21:15:57 +05309
Rushabh Mehtae10cf022014-09-12 13:09:41 +053010cur_frm.email_field = "contact_email";
11
Anand Doshi3543f302013-05-24 19:25:01 +053012erpnext.selling.SellingController = erpnext.TransactionController.extend({
Anand Doshi9b496142013-07-11 19:13:58 +053013 onload: function() {
14 this._super();
Anand Doshi9b496142013-07-11 19:13:58 +053015 this.setup_queries();
Anand Doshi33fe8672013-08-02 12:39:10 +053016 this.toggle_editable_price_list_rate();
Anand Doshi9b496142013-07-11 19:13:58 +053017 },
Anand Doshibd67e872014-04-11 16:51:27 +053018
Rushabh Mehtab6843f22014-06-04 13:10:41 +053019 onload_post_render: function() {
Nabin Haitdd38a262014-12-26 13:15:21 +053020 cur_frm.get_field("items").grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +053021 },
22
Anand Doshi9b496142013-07-11 19:13:58 +053023 setup_queries: function() {
Anand Doshi99100a42013-07-04 17:13:53 +053024 var me = this;
Anand Doshibd67e872014-04-11 16:51:27 +053025
Anand Doshif3096132013-05-21 19:35:06 +053026 this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate");
Anand Doshibd67e872014-04-11 16:51:27 +053027
28 $.each([["customer_address", "customer_filter"],
Anand Doshid5d39ac2013-07-29 13:28:37 +053029 ["shipping_address_name", "customer_filter"],
Anand Doshibd67e872014-04-11 16:51:27 +053030 ["contact_person", "customer_filter"],
31 ["customer", "customer"],
32 ["lead", "lead"]],
Anand Doshid5d39ac2013-07-29 13:28:37 +053033 function(i, opts) {
Anand Doshibd67e872014-04-11 16:51:27 +053034 if(me.frm.fields_dict[opts[0]])
Anand Doshid5d39ac2013-07-29 13:28:37 +053035 me.frm.set_query(opts[0], erpnext.queries[opts[1]]);
36 });
Anand Doshibd67e872014-04-11 16:51:27 +053037
Akhilesh Darjee4f721562014-01-29 16:31:38 +053038 if(this.frm.fields_dict.taxes_and_charges) {
39 this.frm.set_query("taxes_and_charges", function() {
Saurabhf52dc072013-07-10 13:07:49 +053040 return {
41 filters: [
42 ['Sales Taxes and Charges Master', 'company', '=', me.frm.doc.company],
Saurabhf52dc072013-07-10 13:07:49 +053043 ['Sales Taxes and Charges Master', 'docstatus', '!=', 2]
44 ]
45 }
Anand Doshi99100a42013-07-04 17:13:53 +053046 });
47 }
Anand Doshi99100a42013-07-04 17:13:53 +053048
Rushabh Mehta4a404e92013-08-09 18:11:35 +053049 if(this.frm.fields_dict.selling_price_list) {
50 this.frm.set_query("selling_price_list", function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +053051 return { filters: { selling: 1 } };
Anand Doshi720a01a2013-07-26 11:32:02 +053052 });
Anand Doshi720a01a2013-07-26 11:32:02 +053053 }
Anand Doshibd67e872014-04-11 16:51:27 +053054
Nabin Haitdd38a262014-12-26 13:15:21 +053055 if(!this.frm.fields_dict["items"]) {
Anand Doshi99100a42013-07-04 17:13:53 +053056 return;
57 }
Anand Doshibd67e872014-04-11 16:51:27 +053058
Nabin Haitdd38a262014-12-26 13:15:21 +053059 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
60 this.frm.set_query("item_code", "items", function() {
Anand Doshi9b496142013-07-11 19:13:58 +053061 return {
Rushabh Mehta6de403f2013-12-13 14:10:14 +053062 query: "erpnext.controllers.queries.item_query",
Anand Doshi9b496142013-07-11 19:13:58 +053063 filters: (me.frm.doc.order_type === "Maintenance" ?
64 {'is_service_item': 'Yes'}:
65 {'is_sales_item': 'Yes' })
66 }
Anand Doshi99100a42013-07-04 17:13:53 +053067 });
68 }
Anand Doshibd67e872014-04-11 16:51:27 +053069
Nabin Haitdd38a262014-12-26 13:15:21 +053070 if(this.frm.fields_dict["items"].grid.get_field('batch_no')) {
71 this.frm.set_query("batch_no", "items", function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +053072 var item = frappe.get_doc(cdt, cdn);
Anand Doshi99100a42013-07-04 17:13:53 +053073 if(!item.item_code) {
Pratik Vyasb52618c2014-04-14 16:25:30 +053074 frappe.throw(__("Please enter Item Code to get batch no"));
Anand Doshi99100a42013-07-04 17:13:53 +053075 } else {
Nabin Haitd1fd1e22013-10-18 12:29:11 +053076 filters = {
77 'item_code': item.item_code,
78 'posting_date': me.frm.doc.posting_date,
79 }
80 if(item.warehouse) filters["warehouse"] = item.warehouse
Anand Doshibd67e872014-04-11 16:51:27 +053081
Nabin Haitd1fd1e22013-10-18 12:29:11 +053082 return {
Akhilesh Darjee4f721562014-01-29 16:31:38 +053083 query : "erpnext.controllers.queries.get_batch_no",
Nabin Haitd1fd1e22013-10-18 12:29:11 +053084 filters: filters
Anand Doshi99100a42013-07-04 17:13:53 +053085 }
86 }
87 });
88 }
Anand Doshibd67e872014-04-11 16:51:27 +053089
Anand Doshied698922013-07-23 15:16:50 +053090 if(this.frm.fields_dict.sales_team && this.frm.fields_dict.sales_team.grid.get_field("sales_person")) {
Anand Doshid5d39ac2013-07-29 13:28:37 +053091 this.frm.set_query("sales_person", "sales_team", erpnext.queries.not_a_group_filter);
Anand Doshied698922013-07-23 15:16:50 +053092 }
Anand Doshi1dde46a2013-05-15 21:15:57 +053093 },
Anand Doshibd67e872014-04-11 16:51:27 +053094
Anand Doshic4a54fe2013-08-01 18:19:51 +053095 refresh: function() {
96 this._super();
Anand Doshibd67e872014-04-11 16:51:27 +053097 this.frm.toggle_display("customer_name",
Akhilesh Darjee5ce1b8b2013-12-09 16:29:04 +053098 (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
Nabin Haite7d15362014-12-25 16:01:55 +053099 if(this.frm.fields_dict.packed_items) {
100 var packing_list_exists = (this.frm.doc.packed_items || []).length;
Anand Doshic4a54fe2013-08-01 18:19:51 +0530101 this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
102 }
Rushabh Mehta8aded132013-07-04 12:50:52 +0530103 },
Anand Doshibd67e872014-04-11 16:51:27 +0530104
Anand Doshi3543f302013-05-24 19:25:01 +0530105 customer: function() {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530106 var me = this;
107 erpnext.utils.get_party_details(this.frm, null, null, function(){me.apply_pricing_rule()});
Anand Doshi3543f302013-05-24 19:25:01 +0530108 },
Anand Doshibd67e872014-04-11 16:51:27 +0530109
Nabin Haita279d782013-07-15 13:04:33 +0530110 customer_address: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530111 erpnext.utils.get_address_display(this.frm, "customer_address");
Nabin Haita279d782013-07-15 13:04:33 +0530112 },
Anand Doshibd67e872014-04-11 16:51:27 +0530113
Nabin Hait9d1f0772014-02-19 17:43:24 +0530114 shipping_address_name: function() {
115 erpnext.utils.get_address_display(this.frm, "shipping_address_name", "shipping_address");
116 },
Anand Doshibd67e872014-04-11 16:51:27 +0530117
Nabin Haita279d782013-07-15 13:04:33 +0530118 contact_person: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530119 erpnext.utils.get_contact_details(this.frm);
Nabin Haita279d782013-07-15 13:04:33 +0530120 },
Anand Doshibd67e872014-04-11 16:51:27 +0530121
Nabin Haita3dd72a2014-05-28 12:49:20 +0530122 sales_partner: function() {
123 this.apply_pricing_rule();
124 },
125
126 campaign: function() {
127 this.apply_pricing_rule();
128 },
129
Anand Doshif3096132013-05-21 19:35:06 +0530130 barcode: function(doc, cdt, cdn) {
131 this.item_code(doc, cdt, cdn);
Anand Doshi1dde46a2013-05-15 21:15:57 +0530132 },
Anand Doshibd67e872014-04-11 16:51:27 +0530133
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530134 selling_price_list: function() {
Anand Doshidffec8f2014-07-01 17:45:15 +0530135 this.apply_price_list();
Anand Doshif3096132013-05-21 19:35:06 +0530136 },
Anand Doshibd67e872014-04-11 16:51:27 +0530137
Nabin Haita7f757a2014-02-10 17:54:04 +0530138 price_list_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530139 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530140 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530141
Nabin Hait7979f7e2014-02-10 18:26:49 +0530142 item.rate = flt(item.price_list_rate * (1 - item.discount_percentage / 100.0),
143 precision("rate", item));
Anand Doshibd67e872014-04-11 16:51:27 +0530144
Anand Doshif3096132013-05-21 19:35:06 +0530145 this.calculate_taxes_and_totals();
146 },
Anand Doshibd67e872014-04-11 16:51:27 +0530147
Nabin Haita7f757a2014-02-10 17:54:04 +0530148 discount_percentage: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530149 var item = frappe.get_doc(cdt, cdn);
Nabin Haita7f757a2014-02-10 17:54:04 +0530150 if(!item.price_list_rate) {
151 item.discount_percentage = 0.0;
Anand Doshi923d41d2013-05-28 17:23:36 +0530152 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530153 this.price_list_rate(doc, cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530154 }
Anand Doshif3096132013-05-21 19:35:06 +0530155 },
Anand Doshibd67e872014-04-11 16:51:27 +0530156
Nabin Hait7979f7e2014-02-10 18:26:49 +0530157 rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530158 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530159 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530160
Nabin Haita7f757a2014-02-10 17:54:04 +0530161 if(item.price_list_rate) {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530162 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0,
Nabin Haita7f757a2014-02-10 17:54:04 +0530163 precision("discount_percentage", item));
Anand Doshif3096132013-05-21 19:35:06 +0530164 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530165 item.discount_percentage = 0.0;
Anand Doshif3096132013-05-21 19:35:06 +0530166 }
Anand Doshibd67e872014-04-11 16:51:27 +0530167
Anand Doshif3096132013-05-21 19:35:06 +0530168 this.calculate_taxes_and_totals();
169 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530170
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530171 discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530172 this.calculate_taxes_and_totals();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530173 },
Anand Doshibd67e872014-04-11 16:51:27 +0530174
Anand Doshif3096132013-05-21 19:35:06 +0530175 commission_rate: function() {
176 this.calculate_commission();
177 refresh_field("total_commission");
178 },
Anand Doshibd67e872014-04-11 16:51:27 +0530179
Anand Doshif3096132013-05-21 19:35:06 +0530180 total_commission: function() {
181 if(this.frm.doc.net_total) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530182 frappe.model.round_floats_in(this.frm.doc, ["net_total", "total_commission"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530183
Anand Doshif3096132013-05-21 19:35:06 +0530184 if(this.frm.doc.net_total < this.frm.doc.total_commission) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530185 var msg = (__("[Error]") + " " +
186 __(frappe.meta.get_label(this.frm.doc.doctype, "total_commission",
Anand Doshibd67e872014-04-11 16:51:27 +0530187 this.frm.doc.name)) + " > " +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530188 __(frappe.meta.get_label(this.frm.doc.doctype, "net_total", this.frm.doc.name)));
Anand Doshif3096132013-05-21 19:35:06 +0530189 msgprint(msg);
190 throw msg;
191 }
Anand Doshibd67e872014-04-11 16:51:27 +0530192
193 this.frm.set_value("commission_rate",
Anand Doshif3096132013-05-21 19:35:06 +0530194 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.net_total));
195 }
196 },
Anand Doshibd67e872014-04-11 16:51:27 +0530197
Anand Doshif3096132013-05-21 19:35:06 +0530198 allocated_percentage: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530199 var sales_person = frappe.get_doc(cdt, cdn);
Anand Doshibd67e872014-04-11 16:51:27 +0530200
Anand Doshif3096132013-05-21 19:35:06 +0530201 if(sales_person.allocated_percentage) {
202 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
203 precision("allocated_percentage", sales_person));
204 sales_person.allocated_amount = flt(this.frm.doc.net_total *
Anand Doshibd67e872014-04-11 16:51:27 +0530205 sales_person.allocated_percentage / 100.0,
Anand Doshif3096132013-05-21 19:35:06 +0530206 precision("allocated_amount", sales_person));
207
208 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
209 sales_person.parentfield);
210 }
211 },
Anand Doshibd67e872014-04-11 16:51:27 +0530212
Anand Doshifc777182013-05-27 19:29:07 +0530213 warehouse: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530214 var item = frappe.get_doc(cdt, cdn);
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530215 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530216 return this.frm.call({
Nabin Hait0984f222014-05-30 19:10:12 +0530217 method: "erpnext.stock.get_item_details.get_available_qty",
Anand Doshifc777182013-05-27 19:29:07 +0530218 child: item,
219 args: {
220 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530221 warehouse: item.warehouse,
Anand Doshifc777182013-05-27 19:29:07 +0530222 },
223 });
224 }
225 },
Anand Doshibd67e872014-04-11 16:51:27 +0530226
Anand Doshi33fe8672013-08-02 12:39:10 +0530227 toggle_editable_price_list_rate: function() {
Nabin Haitdd38a262014-12-26 13:15:21 +0530228 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 +0530229 var editable_price_list_rate = cint(frappe.defaults.get_default("editable_price_list_rate"));
Anand Doshibd67e872014-04-11 16:51:27 +0530230
Anand Doshi33fe8672013-08-02 12:39:10 +0530231 if(df && editable_price_list_rate) {
232 df.read_only = 0;
233 }
234 },
Anand Doshibd67e872014-04-11 16:51:27 +0530235
Anand Doshif3096132013-05-21 19:35:06 +0530236 calculate_net_total: function() {
237 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530238 this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530239
Nabin Hait96339342015-01-21 17:22:11 +0530240 $.each(this.frm.doc["items"] || [], function(i, item) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530241 me.frm.doc.net_total += item.base_amount;
242 me.frm.doc.net_total_export += item.amount;
Anand Doshif3096132013-05-21 19:35:06 +0530243 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530244
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530245 frappe.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
Anand Doshif3096132013-05-21 19:35:06 +0530246 },
Anand Doshibd67e872014-04-11 16:51:27 +0530247
Anand Doshif3096132013-05-21 19:35:06 +0530248 calculate_totals: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530249 var me = this;
Nabin Hait96339342015-01-21 17:22:11 +0530250 var tax_count = this.frm.doc["taxes"] ? this.frm.doc["taxes"].length: 0;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530251
Nabin Hait239c2c32015-01-15 14:21:41 +0530252 this.frm.doc.grand_total = flt(tax_count ? this.frm.doc["taxes"][tax_count - 1].total : this.frm.doc.net_total);
Anand Doshibd67e872014-04-11 16:51:27 +0530253
Anand Doshif3096132013-05-21 19:35:06 +0530254 this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
255 precision("other_charges_total"));
Nabin Hait5e13e0c2015-02-05 17:18:03 +0530256
257 this.frm.doc.grand_total_export = (this.frm.doc.other_charges_total || this.frm.doc.discount_amount) ?
258 flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate) : this.frm.doc.net_total_export;
259
Anand Doshibd67e872014-04-11 16:51:27 +0530260 this.frm.doc.other_charges_total_export = flt(this.frm.doc.grand_total_export -
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530261 this.frm.doc.net_total_export + flt(this.frm.doc.discount_amount),
Anand Doshif3096132013-05-21 19:35:06 +0530262 precision("other_charges_total_export"));
Anand Doshibd67e872014-04-11 16:51:27 +0530263
Nabin Haitccd9fd32014-11-14 14:27:24 +0530264 this.frm.doc.grand_total = flt(this.frm.doc.grand_total, precision("grand_total"));
265 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total_export, precision("grand_total_export"));
266
Anand Doshif3096132013-05-21 19:35:06 +0530267 this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
268 this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export);
269 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530270
Anand Doshi652bc072014-04-16 15:21:46 +0530271 calculate_outstanding_amount: function(update_paid_amount) {
Anand Doshibd67e872014-04-11 16:51:27 +0530272 // NOTE:
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530273 // paid_amount and write_off_amount is only for POS Invoice
Anand Doshi923d41d2013-05-28 17:23:36 +0530274 // total_advance is only for non POS Invoice
Anand Doshi29ea5d02013-07-05 17:23:14 +0530275 if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) {
Nabin Hait54106802014-03-13 17:26:41 +0530276 frappe.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount",
277 "paid_amount"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530278 var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount
Nabin Hait54106802014-03-13 17:26:41 +0530279 - this.frm.doc.total_advance;
Nabin Hait0c9d4222014-03-25 19:35:41 +0530280 if(this.frm.doc.is_pos) {
Anand Doshi652bc072014-04-16 15:21:46 +0530281 if(!this.frm.doc.paid_amount || update_paid_amount===undefined || update_paid_amount) {
282 this.frm.doc.paid_amount = flt(total_amount_to_pay);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530283 this.frm.refresh_field("paid_amount");
Anand Doshi652bc072014-04-16 15:21:46 +0530284 }
Nabin Hait0c9d4222014-03-25 19:35:41 +0530285 } else {
286 this.frm.doc.paid_amount = 0
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530287 this.frm.refresh_field("paid_amount");
Nabin Hait0c9d4222014-03-25 19:35:41 +0530288 }
Nabin Hait482ffac2014-03-13 16:32:43 +0530289
Anand Doshibd67e872014-04-11 16:51:27 +0530290 this.frm.set_value("outstanding_amount", flt(total_amount_to_pay
Nabin Hait54106802014-03-13 17:26:41 +0530291 - this.frm.doc.paid_amount, precision("outstanding_amount")));
Anand Doshifc777182013-05-27 19:29:07 +0530292 }
293 },
Anand Doshibd67e872014-04-11 16:51:27 +0530294
Anand Doshif3096132013-05-21 19:35:06 +0530295 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530296 if(this.frm.fields_dict.commission_rate) {
297 if(this.frm.doc.commission_rate > 100) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530298 var msg = __(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
299 " " + __("cannot be greater than 100");
Anand Doshi923d41d2013-05-28 17:23:36 +0530300 msgprint(msg);
301 throw msg;
302 }
Anand Doshibd67e872014-04-11 16:51:27 +0530303
Anand Doshi923d41d2013-05-28 17:23:36 +0530304 this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0,
305 precision("total_commission"));
306 }
Anand Doshif3096132013-05-21 19:35:06 +0530307 },
Anand Doshibd67e872014-04-11 16:51:27 +0530308
Anand Doshif3096132013-05-21 19:35:06 +0530309 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530310 var me = this;
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530311 $.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530312 frappe.model.round_floats_in(sales_person);
Anand Doshif3096132013-05-21 19:35:06 +0530313 if(sales_person.allocated_percentage) {
314 sales_person.allocated_amount = flt(
315 me.frm.doc.net_total * sales_person.allocated_percentage / 100.0,
316 precision("allocated_amount", sales_person));
317 }
318 });
319 },
Anand Doshibd67e872014-04-11 16:51:27 +0530320
Anand Doshicefccb92013-07-15 18:28:14 +0530321 shipping_rule: function() {
322 var me = this;
323 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530324 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530325 doc: this.frm.doc,
326 method: "apply_shipping_rule",
327 callback: function(r) {
328 if(!r.exc) {
329 me.calculate_taxes_and_totals();
330 }
331 }
332 })
333 }
334 },
Anand Doshibd67e872014-04-11 16:51:27 +0530335
Anand Doshi2168e392013-05-23 19:25:08 +0530336 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530337 this._super();
Nabin Hait0b157552014-06-24 17:02:45 +0530338 this.set_sales_bom_help(this.frm.doc);
339 },
340
341 set_sales_bom_help: function(doc) {
342 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haite7d15362014-12-25 16:01:55 +0530343 if ((doc.packed_items || []).length) {
Nabin Hait0b157552014-06-24 17:02:45 +0530344 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
345
346 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
347 help_msg = "<div class='alert alert-warning'>" +
Nabin Hait60c48fe2014-06-24 17:34:52 +0530348 __("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.")+
Nabin Hait0b157552014-06-24 17:02:45 +0530349 "</div>";
350 frappe.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg;
351 }
352 } else {
353 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
354 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
355 frappe.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = '';
356 }
357 }
358 refresh_field('sales_bom_help');
Anand Doshi2168e392013-05-23 19:25:08 +0530359 },
Anand Doshibd67e872014-04-11 16:51:27 +0530360
Anand Doshi2168e392013-05-23 19:25:08 +0530361 change_form_labels: function(company_currency) {
362 var me = this;
363 var field_label_map = {};
Anand Doshibd67e872014-04-11 16:51:27 +0530364
Anand Doshi2168e392013-05-23 19:25:08 +0530365 var setup_field_label_map = function(fields_list, currency) {
366 $.each(fields_list, function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530367 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530368 if(docfield) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530369 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530370 field_label_map[fname] = label.trim() + " (" + currency + ")";
371 }
372 });
373 };
Nabin Hait2244ac42015-01-12 17:35:14 +0530374 setup_field_label_map(["net_total", "other_charges_total", "base_discount_amount", "grand_total",
Anand Doshi2168e392013-05-23 19:25:08 +0530375 "rounded_total", "in_words",
376 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"],
377 company_currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530378
Nabin Hait2244ac42015-01-12 17:35:14 +0530379 setup_field_label_map(["net_total_export", "other_charges_total_export", "discount_amount", "grand_total_export",
Anand Doshi2168e392013-05-23 19:25:08 +0530380 "rounded_total_export", "in_words_export"], this.frm.doc.currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530381
382 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
Rushabh Mehtaef584552013-11-02 14:47:11 +0530383 + " = [?] " + company_currency)
Anand Doshibd67e872014-04-11 16:51:27 +0530384
Anand Doshi2168e392013-05-23 19:25:08 +0530385 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Anand Doshibd67e872014-04-11 16:51:27 +0530386 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
Rushabh Mehtaef584552013-11-02 14:47:11 +0530387 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530388 }
Anand Doshibd67e872014-04-11 16:51:27 +0530389
Anand Doshi2168e392013-05-23 19:25:08 +0530390 // toggle fields
Anand Doshibd67e872014-04-11 16:51:27 +0530391 this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total",
Nabin Hait2244ac42015-01-12 17:35:14 +0530392 "grand_total", "rounded_total", "in_words", "base_discount_amount"],
Anand Doshi2168e392013-05-23 19:25:08 +0530393 this.frm.doc.currency != company_currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530394
395 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
Anand Doshi2168e392013-05-23 19:25:08 +0530396 this.frm.doc.price_list_currency != company_currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530397
Anand Doshi2168e392013-05-23 19:25:08 +0530398 // set labels
399 $.each(field_label_map, function(fname, label) {
400 me.frm.fields_dict[fname].set_label(label);
401 });
402 },
Anand Doshibd67e872014-04-11 16:51:27 +0530403
Anand Doshi2168e392013-05-23 19:25:08 +0530404 change_grid_labels: function(company_currency) {
405 var me = this;
406 var field_label_map = {};
Anand Doshibd67e872014-04-11 16:51:27 +0530407
Anand Doshi2168e392013-05-23 19:25:08 +0530408 var setup_field_label_map = function(fields_list, currency, parentfield) {
409 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
410 $.each(fields_list, function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530411 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530412 if(docfield) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530413 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshibd67e872014-04-11 16:51:27 +0530414 field_label_map[grid_doctype + "-" + fname] =
Anand Doshi2168e392013-05-23 19:25:08 +0530415 label.trim() + " (" + currency + ")";
416 }
417 });
418 }
Anand Doshibd67e872014-04-11 16:51:27 +0530419
Nabin Hait1eb56012014-02-10 19:20:15 +0530420 setup_field_label_map(["base_rate", "base_price_list_rate", "base_amount"],
Nabin Haitdd38a262014-12-26 13:15:21 +0530421 company_currency, "items");
Anand Doshibd67e872014-04-11 16:51:27 +0530422
Nabin Hait1eb56012014-02-10 19:20:15 +0530423 setup_field_label_map(["rate", "price_list_rate", "amount"],
Nabin Haitdd38a262014-12-26 13:15:21 +0530424 this.frm.doc.currency, "items");
Anand Doshibd67e872014-04-11 16:51:27 +0530425
Nabin Haite7d15362014-12-25 16:01:55 +0530426 setup_field_label_map(["tax_amount", "total"], company_currency, "taxes");
Anand Doshibd67e872014-04-11 16:51:27 +0530427
Nabin Haite7d15362014-12-25 16:01:55 +0530428 if(this.frm.fields_dict["advances"]) {
Anand Doshi2168e392013-05-23 19:25:08 +0530429 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency,
Nabin Haite7d15362014-12-25 16:01:55 +0530430 "advances");
Anand Doshi2168e392013-05-23 19:25:08 +0530431 }
Anand Doshibd67e872014-04-11 16:51:27 +0530432
Anand Doshi2168e392013-05-23 19:25:08 +0530433 // toggle columns
Nabin Haitdd38a262014-12-26 13:15:21 +0530434 var item_grid = this.frm.fields_dict["items"].grid;
Anand Doshibd67e872014-04-11 16:51:27 +0530435 var show = (this.frm.doc.currency != company_currency) ||
Nabin Haite7d15362014-12-25 16:01:55 +0530436 ((cur_frm.doc.taxes || []).filter(
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530437 function(d) { return d.included_in_print_rate===1}).length);
Anand Doshibd67e872014-04-11 16:51:27 +0530438
Nabin Hait1eb56012014-02-10 19:20:15 +0530439 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530440 if(frappe.meta.get_docfield(item_grid.doctype, fname))
Anand Doshi2168e392013-05-23 19:25:08 +0530441 item_grid.set_column_disp(fname, show);
442 });
Anand Doshibd67e872014-04-11 16:51:27 +0530443
Anand Doshi2168e392013-05-23 19:25:08 +0530444 // set labels
445 var $wrapper = $(this.frm.wrapper);
446 $.each(field_label_map, function(fname, label) {
Anand Doshi5013dcb2013-08-05 12:16:04 +0530447 fname = fname.split("-");
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530448 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530449 if(df) df.label = label;
Anand Doshi2168e392013-05-23 19:25:08 +0530450 });
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530451 }
Nabin Hait0b157552014-06-24 17:02:45 +0530452});
Anand Doshi43f087c2014-08-26 14:25:53 +0530453
454frappe.ui.form.on(cur_frm.doctype,"project_name", function(frm) {
Nabin Hait10b155a2014-10-23 13:18:59 +0530455 if(in_list(["Delivery Note", "Sales Invoice"], frm.doc.doctype)) {
456 frappe.call({
457 method:'erpnext.projects.doctype.project.project.get_cost_center_name' ,
458 args: { project_name: frm.doc.project_name },
459 callback: function(r, rt) {
460 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530461 $.each(frm.doc["items"] || [], function(i, row) {
Neil Trini Lasrado6fc28212014-10-28 14:36:26 +0530462 frappe.model.set_value(row.doctype, row.name, "cost_center", r.message);
463 msgprint(__("Cost Center For Item with Item Code '"+row.item_name+"' has been Changed to "+ r.message));
Nabin Hait10b155a2014-10-23 13:18:59 +0530464 })
465 }
Anand Doshi43f087c2014-08-26 14:25:53 +0530466 }
Nabin Hait10b155a2014-10-23 13:18:59 +0530467 })
468 }
Anand Doshi43f087c2014-08-26 14:25:53 +0530469})