blob: 6fdfeae876205b6f04ffaff092ceb22d88f59da6 [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// Preset
5// ------
6// cur_frm.cscript.tname - Details table name
7// cur_frm.cscript.fname - Details fieldname
Rushabh Mehta6de403f2013-12-13 14:10:14 +05308// cur_frm.cscript.other_fname - fieldname
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05309// cur_frm.cscript.sales_team_fname - Sales Team fieldname
10
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053011frappe.provide("erpnext.selling");
12frappe.require("assets/erpnext/js/transaction.js");
Rushabh Mehtab09d9da2014-01-02 11:47:23 +053013
Rushabh Mehta6de403f2013-12-13 14:10:14 +053014{% include "public/js/controllers/accounts.js" %}
Anand Doshi1dde46a2013-05-15 21:15:57 +053015
Anand Doshi3543f302013-05-24 19:25:01 +053016erpnext.selling.SellingController = erpnext.TransactionController.extend({
Anand Doshi9b496142013-07-11 19:13:58 +053017 onload: function() {
18 this._super();
Anand Doshi9b496142013-07-11 19:13:58 +053019 this.setup_queries();
Anand Doshi33fe8672013-08-02 12:39:10 +053020 this.toggle_editable_price_list_rate();
Anand Doshi9b496142013-07-11 19:13:58 +053021 },
Anand Doshibd67e872014-04-11 16:51:27 +053022
Rushabh Mehtab6843f22014-06-04 13:10:41 +053023 onload_post_render: function() {
Rushabh Mehta880b3ef2014-06-06 11:45:11 +053024 cur_frm.get_field(this.fname).grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +053025 },
26
Anand Doshi9b496142013-07-11 19:13:58 +053027 setup_queries: function() {
Anand Doshi99100a42013-07-04 17:13:53 +053028 var me = this;
Anand Doshibd67e872014-04-11 16:51:27 +053029
Anand Doshif3096132013-05-21 19:35:06 +053030 this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate");
Anand Doshibd67e872014-04-11 16:51:27 +053031
32 $.each([["customer_address", "customer_filter"],
Anand Doshid5d39ac2013-07-29 13:28:37 +053033 ["shipping_address_name", "customer_filter"],
Anand Doshibd67e872014-04-11 16:51:27 +053034 ["contact_person", "customer_filter"],
35 ["customer", "customer"],
36 ["lead", "lead"]],
Anand Doshid5d39ac2013-07-29 13:28:37 +053037 function(i, opts) {
Anand Doshibd67e872014-04-11 16:51:27 +053038 if(me.frm.fields_dict[opts[0]])
Anand Doshid5d39ac2013-07-29 13:28:37 +053039 me.frm.set_query(opts[0], erpnext.queries[opts[1]]);
40 });
Anand Doshibd67e872014-04-11 16:51:27 +053041
Akhilesh Darjee4f721562014-01-29 16:31:38 +053042 if(this.frm.fields_dict.taxes_and_charges) {
43 this.frm.set_query("taxes_and_charges", function() {
Saurabhf52dc072013-07-10 13:07:49 +053044 return {
45 filters: [
46 ['Sales Taxes and Charges Master', 'company', '=', me.frm.doc.company],
Saurabhf52dc072013-07-10 13:07:49 +053047 ['Sales Taxes and Charges Master', 'docstatus', '!=', 2]
48 ]
49 }
Anand Doshi99100a42013-07-04 17:13:53 +053050 });
51 }
Anand Doshi99100a42013-07-04 17:13:53 +053052
Rushabh Mehta4a404e92013-08-09 18:11:35 +053053 if(this.frm.fields_dict.selling_price_list) {
54 this.frm.set_query("selling_price_list", function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +053055 return { filters: { selling: 1 } };
Anand Doshi720a01a2013-07-26 11:32:02 +053056 });
Anand Doshi720a01a2013-07-26 11:32:02 +053057 }
Anand Doshibd67e872014-04-11 16:51:27 +053058
Anand Doshi99100a42013-07-04 17:13:53 +053059 if(!this.fname) {
60 return;
61 }
Anand Doshibd67e872014-04-11 16:51:27 +053062
Anand Doshi99100a42013-07-04 17:13:53 +053063 if(this.frm.fields_dict[this.fname].grid.get_field('item_code')) {
64 this.frm.set_query("item_code", this.fname, function() {
Anand Doshi9b496142013-07-11 19:13:58 +053065 return {
Rushabh Mehta6de403f2013-12-13 14:10:14 +053066 query: "erpnext.controllers.queries.item_query",
Anand Doshi9b496142013-07-11 19:13:58 +053067 filters: (me.frm.doc.order_type === "Maintenance" ?
68 {'is_service_item': 'Yes'}:
69 {'is_sales_item': 'Yes' })
70 }
Anand Doshi99100a42013-07-04 17:13:53 +053071 });
72 }
Anand Doshibd67e872014-04-11 16:51:27 +053073
Anand Doshi99100a42013-07-04 17:13:53 +053074 if(this.frm.fields_dict[this.fname].grid.get_field('batch_no')) {
75 this.frm.set_query("batch_no", this.fname, function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +053076 var item = frappe.get_doc(cdt, cdn);
Anand Doshi99100a42013-07-04 17:13:53 +053077 if(!item.item_code) {
Pratik Vyasb52618c2014-04-14 16:25:30 +053078 frappe.throw(__("Please enter Item Code to get batch no"));
Anand Doshi99100a42013-07-04 17:13:53 +053079 } else {
Nabin Haitd1fd1e22013-10-18 12:29:11 +053080 filters = {
81 'item_code': item.item_code,
82 'posting_date': me.frm.doc.posting_date,
83 }
84 if(item.warehouse) filters["warehouse"] = item.warehouse
Anand Doshibd67e872014-04-11 16:51:27 +053085
Nabin Haitd1fd1e22013-10-18 12:29:11 +053086 return {
Akhilesh Darjee4f721562014-01-29 16:31:38 +053087 query : "erpnext.controllers.queries.get_batch_no",
Nabin Haitd1fd1e22013-10-18 12:29:11 +053088 filters: filters
Anand Doshi99100a42013-07-04 17:13:53 +053089 }
90 }
91 });
92 }
Anand Doshibd67e872014-04-11 16:51:27 +053093
Anand Doshied698922013-07-23 15:16:50 +053094 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 +053095 this.frm.set_query("sales_person", "sales_team", erpnext.queries.not_a_group_filter);
Anand Doshied698922013-07-23 15:16:50 +053096 }
Anand Doshi1dde46a2013-05-15 21:15:57 +053097 },
Anand Doshibd67e872014-04-11 16:51:27 +053098
Anand Doshic4a54fe2013-08-01 18:19:51 +053099 refresh: function() {
100 this._super();
Anand Doshibd67e872014-04-11 16:51:27 +0530101 this.frm.toggle_display("customer_name",
Akhilesh Darjee5ce1b8b2013-12-09 16:29:04 +0530102 (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
Anand Doshic4a54fe2013-08-01 18:19:51 +0530103 if(this.frm.fields_dict.packing_details) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530104 var packing_list_exists = (this.frm.doc.packing_details || []).length;
Anand Doshic4a54fe2013-08-01 18:19:51 +0530105 this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
106 }
Rushabh Mehta8aded132013-07-04 12:50:52 +0530107 },
Anand Doshibd67e872014-04-11 16:51:27 +0530108
Anand Doshi3543f302013-05-24 19:25:01 +0530109 customer: function() {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530110 var me = this;
111 erpnext.utils.get_party_details(this.frm, null, null, function(){me.apply_pricing_rule()});
Anand Doshi3543f302013-05-24 19:25:01 +0530112 },
Anand Doshibd67e872014-04-11 16:51:27 +0530113
Nabin Haita279d782013-07-15 13:04:33 +0530114 customer_address: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530115 erpnext.utils.get_address_display(this.frm, "customer_address");
Nabin Haita279d782013-07-15 13:04:33 +0530116 },
Anand Doshibd67e872014-04-11 16:51:27 +0530117
Nabin Hait9d1f0772014-02-19 17:43:24 +0530118 shipping_address_name: function() {
119 erpnext.utils.get_address_display(this.frm, "shipping_address_name", "shipping_address");
120 },
Anand Doshibd67e872014-04-11 16:51:27 +0530121
Nabin Haita279d782013-07-15 13:04:33 +0530122 contact_person: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530123 erpnext.utils.get_contact_details(this.frm);
Nabin Haita279d782013-07-15 13:04:33 +0530124 },
Anand Doshibd67e872014-04-11 16:51:27 +0530125
Nabin Haita3dd72a2014-05-28 12:49:20 +0530126 sales_partner: function() {
127 this.apply_pricing_rule();
128 },
129
130 campaign: function() {
131 this.apply_pricing_rule();
132 },
133
Anand Doshif3096132013-05-21 19:35:06 +0530134 barcode: function(doc, cdt, cdn) {
135 this.item_code(doc, cdt, cdn);
Anand Doshi1dde46a2013-05-15 21:15:57 +0530136 },
Anand Doshibd67e872014-04-11 16:51:27 +0530137
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530138 selling_price_list: function() {
Anand Doshidffec8f2014-07-01 17:45:15 +0530139 this.apply_price_list();
Anand Doshif3096132013-05-21 19:35:06 +0530140 },
Anand Doshibd67e872014-04-11 16:51:27 +0530141
Nabin Haita7f757a2014-02-10 17:54:04 +0530142 price_list_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530143 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530144 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530145
Nabin Hait7979f7e2014-02-10 18:26:49 +0530146 item.rate = flt(item.price_list_rate * (1 - item.discount_percentage / 100.0),
147 precision("rate", item));
Anand Doshibd67e872014-04-11 16:51:27 +0530148
Anand Doshif3096132013-05-21 19:35:06 +0530149 this.calculate_taxes_and_totals();
150 },
Anand Doshibd67e872014-04-11 16:51:27 +0530151
Nabin Haita7f757a2014-02-10 17:54:04 +0530152 discount_percentage: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530153 var item = frappe.get_doc(cdt, cdn);
Nabin Haita7f757a2014-02-10 17:54:04 +0530154 if(!item.price_list_rate) {
155 item.discount_percentage = 0.0;
Anand Doshi923d41d2013-05-28 17:23:36 +0530156 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530157 this.price_list_rate(doc, cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530158 }
Anand Doshif3096132013-05-21 19:35:06 +0530159 },
Anand Doshibd67e872014-04-11 16:51:27 +0530160
Nabin Hait7979f7e2014-02-10 18:26:49 +0530161 rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530162 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530163 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530164
Nabin Haita7f757a2014-02-10 17:54:04 +0530165 if(item.price_list_rate) {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530166 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0,
Nabin Haita7f757a2014-02-10 17:54:04 +0530167 precision("discount_percentage", item));
Anand Doshif3096132013-05-21 19:35:06 +0530168 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530169 item.discount_percentage = 0.0;
Anand Doshif3096132013-05-21 19:35:06 +0530170 }
Anand Doshibd67e872014-04-11 16:51:27 +0530171
Anand Doshif3096132013-05-21 19:35:06 +0530172 this.calculate_taxes_and_totals();
173 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530174
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530175 discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530176 this.calculate_taxes_and_totals();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530177 },
Anand Doshibd67e872014-04-11 16:51:27 +0530178
Anand Doshif3096132013-05-21 19:35:06 +0530179 commission_rate: function() {
180 this.calculate_commission();
181 refresh_field("total_commission");
182 },
Anand Doshibd67e872014-04-11 16:51:27 +0530183
Anand Doshif3096132013-05-21 19:35:06 +0530184 total_commission: function() {
185 if(this.frm.doc.net_total) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530186 frappe.model.round_floats_in(this.frm.doc, ["net_total", "total_commission"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530187
Anand Doshif3096132013-05-21 19:35:06 +0530188 if(this.frm.doc.net_total < this.frm.doc.total_commission) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530189 var msg = (__("[Error]") + " " +
190 __(frappe.meta.get_label(this.frm.doc.doctype, "total_commission",
Anand Doshibd67e872014-04-11 16:51:27 +0530191 this.frm.doc.name)) + " > " +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530192 __(frappe.meta.get_label(this.frm.doc.doctype, "net_total", this.frm.doc.name)));
Anand Doshif3096132013-05-21 19:35:06 +0530193 msgprint(msg);
194 throw msg;
195 }
Anand Doshibd67e872014-04-11 16:51:27 +0530196
197 this.frm.set_value("commission_rate",
Anand Doshif3096132013-05-21 19:35:06 +0530198 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.net_total));
199 }
200 },
Anand Doshibd67e872014-04-11 16:51:27 +0530201
Anand Doshif3096132013-05-21 19:35:06 +0530202 allocated_percentage: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530203 var sales_person = frappe.get_doc(cdt, cdn);
Anand Doshibd67e872014-04-11 16:51:27 +0530204
Anand Doshif3096132013-05-21 19:35:06 +0530205 if(sales_person.allocated_percentage) {
206 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
207 precision("allocated_percentage", sales_person));
208 sales_person.allocated_amount = flt(this.frm.doc.net_total *
Anand Doshibd67e872014-04-11 16:51:27 +0530209 sales_person.allocated_percentage / 100.0,
Anand Doshif3096132013-05-21 19:35:06 +0530210 precision("allocated_amount", sales_person));
211
212 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
213 sales_person.parentfield);
214 }
215 },
Anand Doshibd67e872014-04-11 16:51:27 +0530216
Anand Doshifc777182013-05-27 19:29:07 +0530217 warehouse: function(doc, cdt, cdn) {
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530218 var me = this;
219 this.batch_no(doc, cdt, cdn);
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530220 var item = frappe.get_doc(cdt, cdn);
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530221 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530222 return this.frm.call({
Nabin Hait0984f222014-05-30 19:10:12 +0530223 method: "erpnext.stock.get_item_details.get_available_qty",
Anand Doshifc777182013-05-27 19:29:07 +0530224 child: item,
225 args: {
226 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530227 warehouse: item.warehouse,
Anand Doshifc777182013-05-27 19:29:07 +0530228 },
229 });
230 }
231 },
Anand Doshibd67e872014-04-11 16:51:27 +0530232
Anand Doshi33fe8672013-08-02 12:39:10 +0530233 toggle_editable_price_list_rate: function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530234 var df = frappe.meta.get_docfield(this.tname, "price_list_rate", this.frm.doc.name);
235 var editable_price_list_rate = cint(frappe.defaults.get_default("editable_price_list_rate"));
Anand Doshibd67e872014-04-11 16:51:27 +0530236
Anand Doshi33fe8672013-08-02 12:39:10 +0530237 if(df && editable_price_list_rate) {
238 df.read_only = 0;
239 }
240 },
Anand Doshibd67e872014-04-11 16:51:27 +0530241
Anand Doshi652bc072014-04-16 15:21:46 +0530242 calculate_taxes_and_totals: function(update_paid_amount) {
Anand Doshi3543f302013-05-24 19:25:01 +0530243 this._super();
Anand Doshi652bc072014-04-16 15:21:46 +0530244 this.calculate_total_advance("Sales Invoice", "advance_adjustment_details", update_paid_amount);
Anand Doshif3096132013-05-21 19:35:06 +0530245 this.calculate_commission();
246 this.calculate_contribution();
Anand Doshi923d41d2013-05-28 17:23:36 +0530247
248 // TODO check for custom_recalc in custom scripts of server
Anand Doshibd67e872014-04-11 16:51:27 +0530249
Anand Doshi2168e392013-05-23 19:25:08 +0530250 this.frm.refresh_fields();
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_item_values: function() {
254 var me = this;
Anand Doshibd67e872014-04-11 16:51:27 +0530255
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530256 if (!this.discount_amount_applied) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530257 $.each(this.frm.item_doclist, function(i, item) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530258 frappe.model.round_floats_in(item);
Nabin Hait1eb56012014-02-10 19:20:15 +0530259 item.amount = flt(item.rate * item.qty, precision("amount", item));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530260
Nabin Haita7f757a2014-02-10 17:54:04 +0530261 me._set_in_company_currency(item, "price_list_rate", "base_price_list_rate");
Nabin Hait7979f7e2014-02-10 18:26:49 +0530262 me._set_in_company_currency(item, "rate", "base_rate");
Nabin Hait1eb56012014-02-10 19:20:15 +0530263 me._set_in_company_currency(item, "amount", "base_amount");
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530264 });
265 }
Anand Doshif3096132013-05-21 19:35:06 +0530266 },
Anand Doshibd67e872014-04-11 16:51:27 +0530267
Anand Doshif3096132013-05-21 19:35:06 +0530268 determine_exclusive_rate: function() {
269 var me = this;
270 $.each(me.frm.item_doclist, function(n, item) {
271 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
272 var cumulated_tax_fraction = 0.0;
Anand Doshibd67e872014-04-11 16:51:27 +0530273
Anand Doshif3096132013-05-21 19:35:06 +0530274 $.each(me.frm.tax_doclist, function(i, tax) {
275 tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map);
Anand Doshibd67e872014-04-11 16:51:27 +0530276
Anand Doshif3096132013-05-21 19:35:06 +0530277 if(i==0) {
Anand Doshi2168e392013-05-23 19:25:08 +0530278 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530279 } else {
Anand Doshibd67e872014-04-11 16:51:27 +0530280 tax.grand_total_fraction_for_current_item =
Anand Doshi2168e392013-05-23 19:25:08 +0530281 me.frm.tax_doclist[i-1].grand_total_fraction_for_current_item +
Anand Doshif3096132013-05-21 19:35:06 +0530282 tax.tax_fraction_for_current_item;
283 }
Anand Doshibd67e872014-04-11 16:51:27 +0530284
Anand Doshif3096132013-05-21 19:35:06 +0530285 cumulated_tax_fraction += tax.tax_fraction_for_current_item;
286 });
Anand Doshibd67e872014-04-11 16:51:27 +0530287
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530288 if(cumulated_tax_fraction && !me.discount_amount_applied) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530289 item.base_amount = flt(
290 (item.amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
291 precision("base_amount", item));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530292
Nabin Hait1eb56012014-02-10 19:20:15 +0530293 item.base_rate = flt(item.base_amount / item.qty, precision("base_rate", item));
Anand Doshibd67e872014-04-11 16:51:27 +0530294
Nabin Haita7f757a2014-02-10 17:54:04 +0530295 if(item.discount_percentage == 100) {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530296 item.base_price_list_rate = item.base_rate;
297 item.base_rate = 0.0;
Anand Doshif3096132013-05-21 19:35:06 +0530298 } else {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530299 item.base_price_list_rate = flt(item.base_rate / (1 - item.discount_percentage / 100.0),
Nabin Haita7f757a2014-02-10 17:54:04 +0530300 precision("base_price_list_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530301 }
302 }
303 });
304 },
Anand Doshibd67e872014-04-11 16:51:27 +0530305
Anand Doshif3096132013-05-21 19:35:06 +0530306 get_current_tax_fraction: function(tax, item_tax_map) {
307 // Get tax fraction for calculating tax exclusive amount
308 // from tax inclusive amount
309 var current_tax_fraction = 0.0;
Anand Doshibd67e872014-04-11 16:51:27 +0530310
Anand Doshif3096132013-05-21 19:35:06 +0530311 if(cint(tax.included_in_print_rate)) {
Anand Doshi2168e392013-05-23 19:25:08 +0530312 var tax_rate = this._get_tax_rate(tax, item_tax_map);
Anand Doshibd67e872014-04-11 16:51:27 +0530313
Anand Doshif3096132013-05-21 19:35:06 +0530314 if(tax.charge_type == "On Net Total") {
315 current_tax_fraction = (tax_rate / 100.0);
Anand Doshibd67e872014-04-11 16:51:27 +0530316
Anand Doshif3096132013-05-21 19:35:06 +0530317 } else if(tax.charge_type == "On Previous Row Amount") {
318 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530319 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item;
Anand Doshibd67e872014-04-11 16:51:27 +0530320
Anand Doshif3096132013-05-21 19:35:06 +0530321 } else if(tax.charge_type == "On Previous Row Total") {
322 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530323 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530324 }
325 }
Anand Doshibd67e872014-04-11 16:51:27 +0530326
Anand Doshif3096132013-05-21 19:35:06 +0530327 return current_tax_fraction;
328 },
Anand Doshibd67e872014-04-11 16:51:27 +0530329
Anand Doshif3096132013-05-21 19:35:06 +0530330 calculate_net_total: function() {
331 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530332 this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530333
Anand Doshif3096132013-05-21 19:35:06 +0530334 $.each(this.frm.item_doclist, function(i, item) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530335 me.frm.doc.net_total += item.base_amount;
336 me.frm.doc.net_total_export += item.amount;
Anand Doshif3096132013-05-21 19:35:06 +0530337 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530338
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530339 frappe.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
Anand Doshif3096132013-05-21 19:35:06 +0530340 },
Anand Doshibd67e872014-04-11 16:51:27 +0530341
Anand Doshif3096132013-05-21 19:35:06 +0530342 calculate_totals: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530343 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530344 var tax_count = this.frm.tax_doclist.length;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530345
Nabin Haitccd9fd32014-11-14 14:27:24 +0530346 this.frm.doc.grand_total = flt(tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total);
Anand Doshibd67e872014-04-11 16:51:27 +0530347
Anand Doshif3096132013-05-21 19:35:06 +0530348 this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
349 precision("other_charges_total"));
Nabin Hait5e13e0c2015-02-05 17:18:03 +0530350
351 this.frm.doc.grand_total_export = (this.frm.doc.other_charges_total || this.frm.doc.discount_amount) ?
352 flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate) : this.frm.doc.net_total_export;
353
Anand Doshibd67e872014-04-11 16:51:27 +0530354 this.frm.doc.other_charges_total_export = flt(this.frm.doc.grand_total_export -
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530355 this.frm.doc.net_total_export + flt(this.frm.doc.discount_amount),
Anand Doshif3096132013-05-21 19:35:06 +0530356 precision("other_charges_total_export"));
Anand Doshibd67e872014-04-11 16:51:27 +0530357
Nabin Haitccd9fd32014-11-14 14:27:24 +0530358 this.frm.doc.grand_total = flt(this.frm.doc.grand_total, precision("grand_total"));
359 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total_export, precision("grand_total_export"));
360
Anand Doshif3096132013-05-21 19:35:06 +0530361 this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
362 this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export);
363 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530364
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530365 apply_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530366 var me = this;
367 var distributed_amount = 0.0;
368
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530369 if (this.frm.doc.discount_amount) {
Nabin Hait2244ac42015-01-12 17:35:14 +0530370 this.frm.set_value("base_discount_amount",
371 flt(this.frm.doc.discount_amount * this.frm.doc.conversion_rate, precision("base_discount_amount")))
372
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530373 var grand_total_for_discount_amount = this.get_grand_total_for_discount_amount();
374 // calculate item amount after Discount Amount
375 if (grand_total_for_discount_amount) {
376 $.each(this.frm.item_doclist, function(i, item) {
Nabin Hait2244ac42015-01-12 17:35:14 +0530377 distributed_amount = flt(me.frm.doc.base_discount_amount) * item.base_amount / grand_total_for_discount_amount;
Nabin Hait1eb56012014-02-10 19:20:15 +0530378 item.base_amount = flt(item.base_amount - distributed_amount, precision("base_amount", item));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530379 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530380
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530381 this.discount_amount_applied = true;
382 this._calculate_taxes_and_totals();
383 }
Nabin Hait2244ac42015-01-12 17:35:14 +0530384 } else {
385 this.frm.set_value("base_discount_amount", 0);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530386 }
387 },
388
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530389 get_grand_total_for_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530390 var me = this;
391 var total_actual_tax = 0.0;
392 var actual_taxes_dict = {};
393
394 $.each(this.frm.tax_doclist, function(i, tax) {
395 if (tax.charge_type == "Actual")
396 actual_taxes_dict[tax.idx] = tax.tax_amount;
397 else if (actual_taxes_dict[tax.row_id] !== null) {
398 actual_tax_amount = flt(actual_taxes_dict[tax.row_id]) * flt(tax.rate) / 100;
399 actual_taxes_dict[tax.idx] = actual_tax_amount;
400 }
401 });
402
403 $.each(actual_taxes_dict, function(key, value) {
404 if (value)
405 total_actual_tax += value;
406 });
407
Anand Doshibd67e872014-04-11 16:51:27 +0530408 grand_total_for_discount_amount = flt(this.frm.doc.grand_total - total_actual_tax,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530409 precision("grand_total"));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530410 return grand_total_for_discount_amount;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530411 },
Anand Doshibd67e872014-04-11 16:51:27 +0530412
Anand Doshi652bc072014-04-16 15:21:46 +0530413 calculate_outstanding_amount: function(update_paid_amount) {
Anand Doshibd67e872014-04-11 16:51:27 +0530414 // NOTE:
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530415 // paid_amount and write_off_amount is only for POS Invoice
Anand Doshi923d41d2013-05-28 17:23:36 +0530416 // total_advance is only for non POS Invoice
Anand Doshi29ea5d02013-07-05 17:23:14 +0530417 if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) {
Nabin Hait54106802014-03-13 17:26:41 +0530418 frappe.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount",
419 "paid_amount"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530420 var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount
Nabin Hait54106802014-03-13 17:26:41 +0530421 - this.frm.doc.total_advance;
Nabin Hait0c9d4222014-03-25 19:35:41 +0530422 if(this.frm.doc.is_pos) {
Anand Doshi652bc072014-04-16 15:21:46 +0530423 if(!this.frm.doc.paid_amount || update_paid_amount===undefined || update_paid_amount) {
424 this.frm.doc.paid_amount = flt(total_amount_to_pay);
425 }
Nabin Hait0c9d4222014-03-25 19:35:41 +0530426 } else {
427 this.frm.doc.paid_amount = 0
428 }
Nabin Hait482ffac2014-03-13 16:32:43 +0530429
Anand Doshibd67e872014-04-11 16:51:27 +0530430 this.frm.set_value("outstanding_amount", flt(total_amount_to_pay
Nabin Hait54106802014-03-13 17:26:41 +0530431 - this.frm.doc.paid_amount, precision("outstanding_amount")));
Anand Doshifc777182013-05-27 19:29:07 +0530432 }
433 },
Anand Doshibd67e872014-04-11 16:51:27 +0530434
Anand Doshif3096132013-05-21 19:35:06 +0530435 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530436 if(this.frm.fields_dict.commission_rate) {
437 if(this.frm.doc.commission_rate > 100) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530438 var msg = __(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
439 " " + __("cannot be greater than 100");
Anand Doshi923d41d2013-05-28 17:23:36 +0530440 msgprint(msg);
441 throw msg;
442 }
Anand Doshibd67e872014-04-11 16:51:27 +0530443
Anand Doshi923d41d2013-05-28 17:23:36 +0530444 this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0,
445 precision("total_commission"));
446 }
Anand Doshif3096132013-05-21 19:35:06 +0530447 },
Anand Doshibd67e872014-04-11 16:51:27 +0530448
Anand Doshif3096132013-05-21 19:35:06 +0530449 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530450 var me = this;
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530451 $.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530452 frappe.model.round_floats_in(sales_person);
Anand Doshif3096132013-05-21 19:35:06 +0530453 if(sales_person.allocated_percentage) {
454 sales_person.allocated_amount = flt(
455 me.frm.doc.net_total * sales_person.allocated_percentage / 100.0,
456 precision("allocated_amount", sales_person));
457 }
458 });
459 },
Anand Doshibd67e872014-04-11 16:51:27 +0530460
Anand Doshid6822dd2015-03-13 18:23:10 +0530461 manipulate_grand_total_for_inclusive_tax: function() {
462 // if fully inclusive taxes and diff
463 if (this.frm.tax_doclist.length) {
464 var all_inclusive = frappe.utils.all(this.frm.tax_doclist.map(function(d) {
465 return cint(d.included_in_print_rate);
466 }));
467
468 if (all_inclusive) {
469 var last_tax = this.frm.tax_doclist.slice(-1)[0];
470
471 var diff = this.frm.doc.net_total_export
472 - flt(last_tax.total / this.frm.doc.conversion_rate, precision("grand_total_export"));
473
474 if (diff) {
475 last_tax.total += flt(diff * this.frm.doc.conversion_rate, precision("total", last_tax));
476 }
477 }
478 }
479 },
480
Anand Doshif3096132013-05-21 19:35:06 +0530481 _cleanup: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530482 this._super();
483 this.frm.doc.in_words = this.frm.doc.in_words_export = "";
Anand Doshif3096132013-05-21 19:35:06 +0530484 },
Anand Doshi2168e392013-05-23 19:25:08 +0530485
Anand Doshicefccb92013-07-15 18:28:14 +0530486 shipping_rule: function() {
487 var me = this;
488 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530489 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530490 doc: this.frm.doc,
491 method: "apply_shipping_rule",
492 callback: function(r) {
493 if(!r.exc) {
494 me.calculate_taxes_and_totals();
495 }
496 }
497 })
498 }
499 },
Anand Doshibd67e872014-04-11 16:51:27 +0530500
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530501 batch_no: function(doc, cdt, cdn) {
502 var me = this;
503 var item = frappe.get_doc(cdt, cdn);
504 return this.frm.call({
505 method: "erpnext.stock.get_item_details.get_batch_qty",
506 child: item,
507 args: {
508 "batch_no": item.batch_no,
509 "warehouse": item.warehouse,
510 "item_code": item.item_code
511 },
512 "fieldname": "actual_batch_qty"
513 });
514 },
515
Anand Doshi2168e392013-05-23 19:25:08 +0530516 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530517 this._super();
Nabin Hait0b157552014-06-24 17:02:45 +0530518 this.set_sales_bom_help(this.frm.doc);
519 },
520
521 set_sales_bom_help: function(doc) {
522 if(!cur_frm.fields_dict.packing_list) return;
523 if ((doc.packing_details || []).length) {
524 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
525
526 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
527 help_msg = "<div class='alert alert-warning'>" +
Nabin Hait60c48fe2014-06-24 17:34:52 +0530528 __("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 +0530529 "</div>";
530 frappe.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg;
531 }
532 } else {
533 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
534 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
535 frappe.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = '';
536 }
537 }
538 refresh_field('sales_bom_help');
Anand Doshi2168e392013-05-23 19:25:08 +0530539 },
Anand Doshibd67e872014-04-11 16:51:27 +0530540
Anand Doshi2168e392013-05-23 19:25:08 +0530541 change_form_labels: function(company_currency) {
542 var me = this;
543 var field_label_map = {};
Anand Doshibd67e872014-04-11 16:51:27 +0530544
Anand Doshi2168e392013-05-23 19:25:08 +0530545 var setup_field_label_map = function(fields_list, currency) {
546 $.each(fields_list, function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530547 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530548 if(docfield) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530549 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530550 field_label_map[fname] = label.trim() + " (" + currency + ")";
551 }
552 });
553 };
Nabin Hait2244ac42015-01-12 17:35:14 +0530554 setup_field_label_map(["net_total", "other_charges_total", "base_discount_amount", "grand_total",
Anand Doshi2168e392013-05-23 19:25:08 +0530555 "rounded_total", "in_words",
556 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"],
557 company_currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530558
Nabin Hait2244ac42015-01-12 17:35:14 +0530559 setup_field_label_map(["net_total_export", "other_charges_total_export", "discount_amount", "grand_total_export",
Anand Doshi2168e392013-05-23 19:25:08 +0530560 "rounded_total_export", "in_words_export"], this.frm.doc.currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530561
562 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
Rushabh Mehtaef584552013-11-02 14:47:11 +0530563 + " = [?] " + company_currency)
Anand Doshibd67e872014-04-11 16:51:27 +0530564
Anand Doshi2168e392013-05-23 19:25:08 +0530565 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Anand Doshibd67e872014-04-11 16:51:27 +0530566 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
Rushabh Mehtaef584552013-11-02 14:47:11 +0530567 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530568 }
Anand Doshibd67e872014-04-11 16:51:27 +0530569
Anand Doshi2168e392013-05-23 19:25:08 +0530570 // toggle fields
Anand Doshibd67e872014-04-11 16:51:27 +0530571 this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total",
Nabin Hait2244ac42015-01-12 17:35:14 +0530572 "grand_total", "rounded_total", "in_words", "base_discount_amount"],
Anand Doshi2168e392013-05-23 19:25:08 +0530573 this.frm.doc.currency != company_currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530574
575 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
Anand Doshi2168e392013-05-23 19:25:08 +0530576 this.frm.doc.price_list_currency != company_currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530577
Anand Doshi2168e392013-05-23 19:25:08 +0530578 // set labels
579 $.each(field_label_map, function(fname, label) {
580 me.frm.fields_dict[fname].set_label(label);
581 });
582 },
Anand Doshibd67e872014-04-11 16:51:27 +0530583
Anand Doshi2168e392013-05-23 19:25:08 +0530584 change_grid_labels: function(company_currency) {
585 var me = this;
586 var field_label_map = {};
Anand Doshibd67e872014-04-11 16:51:27 +0530587
Anand Doshi2168e392013-05-23 19:25:08 +0530588 var setup_field_label_map = function(fields_list, currency, parentfield) {
589 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
590 $.each(fields_list, function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530591 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530592 if(docfield) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530593 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshibd67e872014-04-11 16:51:27 +0530594 field_label_map[grid_doctype + "-" + fname] =
Anand Doshi2168e392013-05-23 19:25:08 +0530595 label.trim() + " (" + currency + ")";
596 }
597 });
598 }
Anand Doshibd67e872014-04-11 16:51:27 +0530599
Nabin Hait1eb56012014-02-10 19:20:15 +0530600 setup_field_label_map(["base_rate", "base_price_list_rate", "base_amount"],
Anand Doshi2168e392013-05-23 19:25:08 +0530601 company_currency, this.fname);
Anand Doshibd67e872014-04-11 16:51:27 +0530602
Nabin Hait1eb56012014-02-10 19:20:15 +0530603 setup_field_label_map(["rate", "price_list_rate", "amount"],
Anand Doshi2168e392013-05-23 19:25:08 +0530604 this.frm.doc.currency, this.fname);
Anand Doshibd67e872014-04-11 16:51:27 +0530605
Anand Doshi2168e392013-05-23 19:25:08 +0530606 setup_field_label_map(["tax_amount", "total"], company_currency, "other_charges");
Anand Doshibd67e872014-04-11 16:51:27 +0530607
Anand Doshi2168e392013-05-23 19:25:08 +0530608 if(this.frm.fields_dict["advance_allocation_details"]) {
609 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency,
610 "advance_allocation_details");
611 }
Anand Doshibd67e872014-04-11 16:51:27 +0530612
Anand Doshi2168e392013-05-23 19:25:08 +0530613 // toggle columns
614 var item_grid = this.frm.fields_dict[this.fname].grid;
Anand Doshibd67e872014-04-11 16:51:27 +0530615 var show = (this.frm.doc.currency != company_currency) ||
616 ((cur_frm.doc.other_charges || []).filter(
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530617 function(d) { return d.included_in_print_rate===1}).length);
Anand Doshibd67e872014-04-11 16:51:27 +0530618
Nabin Hait1eb56012014-02-10 19:20:15 +0530619 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530620 if(frappe.meta.get_docfield(item_grid.doctype, fname))
Anand Doshi2168e392013-05-23 19:25:08 +0530621 item_grid.set_column_disp(fname, show);
622 });
Anand Doshibd67e872014-04-11 16:51:27 +0530623
Anand Doshi2168e392013-05-23 19:25:08 +0530624 // set labels
625 var $wrapper = $(this.frm.wrapper);
626 $.each(field_label_map, function(fname, label) {
Anand Doshi5013dcb2013-08-05 12:16:04 +0530627 fname = fname.split("-");
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530628 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530629 if(df) df.label = label;
Anand Doshi2168e392013-05-23 19:25:08 +0530630 });
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530631 }
Nabin Hait0b157552014-06-24 17:02:45 +0530632});
Anand Doshi43f087c2014-08-26 14:25:53 +0530633
634frappe.ui.form.on(cur_frm.doctype,"project_name", function(frm) {
Nabin Hait10b155a2014-10-23 13:18:59 +0530635 if(in_list(["Delivery Note", "Sales Invoice"], frm.doc.doctype)) {
636 frappe.call({
637 method:'erpnext.projects.doctype.project.project.get_cost_center_name' ,
638 args: { project_name: frm.doc.project_name },
639 callback: function(r, rt) {
640 if(!r.exc) {
641 $.each(frm.doc[cur_frm.cscript.fname] || [], function(i, row) {
642 frappe.model.set_value(row.doctype, row.name, "cost_center", r.message);
643 msgprint(__("Cost Center For Item with Item Code '"+row.item_name+"' has been Changed to "+ r.message));
644 })
645 }
Anand Doshi43f087c2014-08-26 14:25:53 +0530646 }
Nabin Hait10b155a2014-10-23 13:18:59 +0530647 })
648 }
Anand Doshi43f087c2014-08-26 14:25:53 +0530649})