blob: 490eec0dbdc33e5ec99ff0895bd47fb019859cf2 [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");
6frappe.require("assets/erpnext/js/transaction.js");
Rushabh Mehtab09d9da2014-01-02 11:47:23 +05307
Rushabh Mehta6de403f2013-12-13 14:10:14 +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 Doshi652bc072014-04-16 15:21:46 +0530236 calculate_taxes_and_totals: function(update_paid_amount) {
Anand Doshi3543f302013-05-24 19:25:01 +0530237 this._super();
Nabin Haite7d15362014-12-25 16:01:55 +0530238 this.calculate_total_advance("Sales Invoice", "advances", update_paid_amount);
Anand Doshif3096132013-05-21 19:35:06 +0530239 this.calculate_commission();
240 this.calculate_contribution();
Anand Doshi923d41d2013-05-28 17:23:36 +0530241
242 // TODO check for custom_recalc in custom scripts of server
Anand Doshibd67e872014-04-11 16:51:27 +0530243
Anand Doshi2168e392013-05-23 19:25:08 +0530244 this.frm.refresh_fields();
Anand Doshif3096132013-05-21 19:35:06 +0530245 },
Anand Doshibd67e872014-04-11 16:51:27 +0530246
Anand Doshif3096132013-05-21 19:35:06 +0530247 calculate_item_values: function() {
248 var me = this;
Anand Doshibd67e872014-04-11 16:51:27 +0530249
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530250 if (!this.discount_amount_applied) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530251 $.each(this.frm.item_doclist, function(i, item) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530252 frappe.model.round_floats_in(item);
Nabin Hait1eb56012014-02-10 19:20:15 +0530253 item.amount = flt(item.rate * item.qty, precision("amount", item));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530254
Nabin Haita7f757a2014-02-10 17:54:04 +0530255 me._set_in_company_currency(item, "price_list_rate", "base_price_list_rate");
Nabin Hait7979f7e2014-02-10 18:26:49 +0530256 me._set_in_company_currency(item, "rate", "base_rate");
Nabin Hait1eb56012014-02-10 19:20:15 +0530257 me._set_in_company_currency(item, "amount", "base_amount");
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530258 });
259 }
Anand Doshif3096132013-05-21 19:35:06 +0530260 },
Anand Doshibd67e872014-04-11 16:51:27 +0530261
Anand Doshif3096132013-05-21 19:35:06 +0530262 determine_exclusive_rate: function() {
263 var me = this;
264 $.each(me.frm.item_doclist, function(n, item) {
265 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
266 var cumulated_tax_fraction = 0.0;
Anand Doshibd67e872014-04-11 16:51:27 +0530267
Anand Doshif3096132013-05-21 19:35:06 +0530268 $.each(me.frm.tax_doclist, function(i, tax) {
269 tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map);
Anand Doshibd67e872014-04-11 16:51:27 +0530270
Anand Doshif3096132013-05-21 19:35:06 +0530271 if(i==0) {
Anand Doshi2168e392013-05-23 19:25:08 +0530272 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530273 } else {
Anand Doshibd67e872014-04-11 16:51:27 +0530274 tax.grand_total_fraction_for_current_item =
Anand Doshi2168e392013-05-23 19:25:08 +0530275 me.frm.tax_doclist[i-1].grand_total_fraction_for_current_item +
Anand Doshif3096132013-05-21 19:35:06 +0530276 tax.tax_fraction_for_current_item;
277 }
Anand Doshibd67e872014-04-11 16:51:27 +0530278
Anand Doshif3096132013-05-21 19:35:06 +0530279 cumulated_tax_fraction += tax.tax_fraction_for_current_item;
280 });
Anand Doshibd67e872014-04-11 16:51:27 +0530281
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530282 if(cumulated_tax_fraction && !me.discount_amount_applied) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530283 item.base_amount = flt(
284 (item.amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
285 precision("base_amount", item));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530286
Nabin Hait1eb56012014-02-10 19:20:15 +0530287 item.base_rate = flt(item.base_amount / item.qty, precision("base_rate", item));
Anand Doshibd67e872014-04-11 16:51:27 +0530288
Nabin Haita7f757a2014-02-10 17:54:04 +0530289 if(item.discount_percentage == 100) {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530290 item.base_price_list_rate = item.base_rate;
291 item.base_rate = 0.0;
Anand Doshif3096132013-05-21 19:35:06 +0530292 } else {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530293 item.base_price_list_rate = flt(item.base_rate / (1 - item.discount_percentage / 100.0),
Nabin Haita7f757a2014-02-10 17:54:04 +0530294 precision("base_price_list_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530295 }
296 }
297 });
298 },
Anand Doshibd67e872014-04-11 16:51:27 +0530299
Anand Doshif3096132013-05-21 19:35:06 +0530300 get_current_tax_fraction: function(tax, item_tax_map) {
301 // Get tax fraction for calculating tax exclusive amount
302 // from tax inclusive amount
303 var current_tax_fraction = 0.0;
Anand Doshibd67e872014-04-11 16:51:27 +0530304
Anand Doshif3096132013-05-21 19:35:06 +0530305 if(cint(tax.included_in_print_rate)) {
Anand Doshi2168e392013-05-23 19:25:08 +0530306 var tax_rate = this._get_tax_rate(tax, item_tax_map);
Anand Doshibd67e872014-04-11 16:51:27 +0530307
Anand Doshif3096132013-05-21 19:35:06 +0530308 if(tax.charge_type == "On Net Total") {
309 current_tax_fraction = (tax_rate / 100.0);
Anand Doshibd67e872014-04-11 16:51:27 +0530310
Anand Doshif3096132013-05-21 19:35:06 +0530311 } else if(tax.charge_type == "On Previous Row Amount") {
312 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530313 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item;
Anand Doshibd67e872014-04-11 16:51:27 +0530314
Anand Doshif3096132013-05-21 19:35:06 +0530315 } else if(tax.charge_type == "On Previous Row Total") {
316 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530317 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530318 }
319 }
Anand Doshibd67e872014-04-11 16:51:27 +0530320
Anand Doshif3096132013-05-21 19:35:06 +0530321 return current_tax_fraction;
322 },
Anand Doshibd67e872014-04-11 16:51:27 +0530323
Anand Doshif3096132013-05-21 19:35:06 +0530324 calculate_net_total: function() {
325 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530326 this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530327
Anand Doshif3096132013-05-21 19:35:06 +0530328 $.each(this.frm.item_doclist, function(i, item) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530329 me.frm.doc.net_total += item.base_amount;
330 me.frm.doc.net_total_export += item.amount;
Anand Doshif3096132013-05-21 19:35:06 +0530331 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530332
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530333 frappe.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
Anand Doshif3096132013-05-21 19:35:06 +0530334 },
Anand Doshibd67e872014-04-11 16:51:27 +0530335
Anand Doshif3096132013-05-21 19:35:06 +0530336 calculate_totals: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530337 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530338 var tax_count = this.frm.tax_doclist.length;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530339
Nabin Haitccd9fd32014-11-14 14:27:24 +0530340 this.frm.doc.grand_total = flt(tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total);
341 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate);
Anand Doshibd67e872014-04-11 16:51:27 +0530342
Anand Doshif3096132013-05-21 19:35:06 +0530343 this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
344 precision("other_charges_total"));
Anand Doshibd67e872014-04-11 16:51:27 +0530345 this.frm.doc.other_charges_total_export = flt(this.frm.doc.grand_total_export -
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530346 this.frm.doc.net_total_export + flt(this.frm.doc.discount_amount),
Anand Doshif3096132013-05-21 19:35:06 +0530347 precision("other_charges_total_export"));
Anand Doshibd67e872014-04-11 16:51:27 +0530348
Nabin Haitccd9fd32014-11-14 14:27:24 +0530349 this.frm.doc.grand_total = flt(this.frm.doc.grand_total, precision("grand_total"));
350 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total_export, precision("grand_total_export"));
351
Anand Doshif3096132013-05-21 19:35:06 +0530352 this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
353 this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export);
354 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530355
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530356 apply_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530357 var me = this;
358 var distributed_amount = 0.0;
359
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530360 if (this.frm.doc.discount_amount) {
361 var grand_total_for_discount_amount = this.get_grand_total_for_discount_amount();
362 // calculate item amount after Discount Amount
363 if (grand_total_for_discount_amount) {
364 $.each(this.frm.item_doclist, function(i, item) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530365 distributed_amount = flt(me.frm.doc.discount_amount) * item.base_amount / grand_total_for_discount_amount;
366 item.base_amount = flt(item.base_amount - distributed_amount, precision("base_amount", item));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530367 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530368
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530369 this.discount_amount_applied = true;
370 this._calculate_taxes_and_totals();
371 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530372 }
373 },
374
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530375 get_grand_total_for_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530376 var me = this;
377 var total_actual_tax = 0.0;
378 var actual_taxes_dict = {};
379
380 $.each(this.frm.tax_doclist, function(i, tax) {
381 if (tax.charge_type == "Actual")
382 actual_taxes_dict[tax.idx] = tax.tax_amount;
383 else if (actual_taxes_dict[tax.row_id] !== null) {
384 actual_tax_amount = flt(actual_taxes_dict[tax.row_id]) * flt(tax.rate) / 100;
385 actual_taxes_dict[tax.idx] = actual_tax_amount;
386 }
387 });
388
389 $.each(actual_taxes_dict, function(key, value) {
390 if (value)
391 total_actual_tax += value;
392 });
393
Anand Doshibd67e872014-04-11 16:51:27 +0530394 grand_total_for_discount_amount = flt(this.frm.doc.grand_total - total_actual_tax,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530395 precision("grand_total"));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530396 return grand_total_for_discount_amount;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530397 },
Anand Doshibd67e872014-04-11 16:51:27 +0530398
Anand Doshi652bc072014-04-16 15:21:46 +0530399 calculate_outstanding_amount: function(update_paid_amount) {
Anand Doshibd67e872014-04-11 16:51:27 +0530400 // NOTE:
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530401 // paid_amount and write_off_amount is only for POS Invoice
Anand Doshi923d41d2013-05-28 17:23:36 +0530402 // total_advance is only for non POS Invoice
Anand Doshi29ea5d02013-07-05 17:23:14 +0530403 if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) {
Nabin Hait54106802014-03-13 17:26:41 +0530404 frappe.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount",
405 "paid_amount"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530406 var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount
Nabin Hait54106802014-03-13 17:26:41 +0530407 - this.frm.doc.total_advance;
Nabin Hait0c9d4222014-03-25 19:35:41 +0530408 if(this.frm.doc.is_pos) {
Anand Doshi652bc072014-04-16 15:21:46 +0530409 if(!this.frm.doc.paid_amount || update_paid_amount===undefined || update_paid_amount) {
410 this.frm.doc.paid_amount = flt(total_amount_to_pay);
411 }
Nabin Hait0c9d4222014-03-25 19:35:41 +0530412 } else {
413 this.frm.doc.paid_amount = 0
414 }
Nabin Hait482ffac2014-03-13 16:32:43 +0530415
Anand Doshibd67e872014-04-11 16:51:27 +0530416 this.frm.set_value("outstanding_amount", flt(total_amount_to_pay
Nabin Hait54106802014-03-13 17:26:41 +0530417 - this.frm.doc.paid_amount, precision("outstanding_amount")));
Anand Doshifc777182013-05-27 19:29:07 +0530418 }
419 },
Anand Doshibd67e872014-04-11 16:51:27 +0530420
Anand Doshif3096132013-05-21 19:35:06 +0530421 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530422 if(this.frm.fields_dict.commission_rate) {
423 if(this.frm.doc.commission_rate > 100) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530424 var msg = __(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
425 " " + __("cannot be greater than 100");
Anand Doshi923d41d2013-05-28 17:23:36 +0530426 msgprint(msg);
427 throw msg;
428 }
Anand Doshibd67e872014-04-11 16:51:27 +0530429
Anand Doshi923d41d2013-05-28 17:23:36 +0530430 this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0,
431 precision("total_commission"));
432 }
Anand Doshif3096132013-05-21 19:35:06 +0530433 },
Anand Doshibd67e872014-04-11 16:51:27 +0530434
Anand Doshif3096132013-05-21 19:35:06 +0530435 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530436 var me = this;
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530437 $.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530438 frappe.model.round_floats_in(sales_person);
Anand Doshif3096132013-05-21 19:35:06 +0530439 if(sales_person.allocated_percentage) {
440 sales_person.allocated_amount = flt(
441 me.frm.doc.net_total * sales_person.allocated_percentage / 100.0,
442 precision("allocated_amount", sales_person));
443 }
444 });
445 },
Anand Doshibd67e872014-04-11 16:51:27 +0530446
Anand Doshif3096132013-05-21 19:35:06 +0530447 _cleanup: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530448 this._super();
449 this.frm.doc.in_words = this.frm.doc.in_words_export = "";
Anand Doshif3096132013-05-21 19:35:06 +0530450 },
Anand Doshi2168e392013-05-23 19:25:08 +0530451
Anand Doshicefccb92013-07-15 18:28:14 +0530452 shipping_rule: function() {
453 var me = this;
454 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530455 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530456 doc: this.frm.doc,
457 method: "apply_shipping_rule",
458 callback: function(r) {
459 if(!r.exc) {
460 me.calculate_taxes_and_totals();
461 }
462 }
463 })
464 }
465 },
Anand Doshibd67e872014-04-11 16:51:27 +0530466
Anand Doshi2168e392013-05-23 19:25:08 +0530467 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530468 this._super();
Nabin Hait0b157552014-06-24 17:02:45 +0530469 this.set_sales_bom_help(this.frm.doc);
470 },
471
472 set_sales_bom_help: function(doc) {
473 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haite7d15362014-12-25 16:01:55 +0530474 if ((doc.packed_items || []).length) {
Nabin Hait0b157552014-06-24 17:02:45 +0530475 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
476
477 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
478 help_msg = "<div class='alert alert-warning'>" +
Nabin Hait60c48fe2014-06-24 17:34:52 +0530479 __("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 +0530480 "</div>";
481 frappe.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg;
482 }
483 } else {
484 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
485 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
486 frappe.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = '';
487 }
488 }
489 refresh_field('sales_bom_help');
Anand Doshi2168e392013-05-23 19:25:08 +0530490 },
Anand Doshibd67e872014-04-11 16:51:27 +0530491
Anand Doshi2168e392013-05-23 19:25:08 +0530492 change_form_labels: function(company_currency) {
493 var me = this;
494 var field_label_map = {};
Anand Doshibd67e872014-04-11 16:51:27 +0530495
Anand Doshi2168e392013-05-23 19:25:08 +0530496 var setup_field_label_map = function(fields_list, currency) {
497 $.each(fields_list, function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530498 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530499 if(docfield) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530500 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530501 field_label_map[fname] = label.trim() + " (" + currency + ")";
502 }
503 });
504 };
Anand Doshibd67e872014-04-11 16:51:27 +0530505 setup_field_label_map(["net_total", "other_charges_total", "grand_total",
Anand Doshi2168e392013-05-23 19:25:08 +0530506 "rounded_total", "in_words",
507 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"],
508 company_currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530509
510 setup_field_label_map(["net_total_export", "other_charges_total_export", "grand_total_export",
Anand Doshi2168e392013-05-23 19:25:08 +0530511 "rounded_total_export", "in_words_export"], this.frm.doc.currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530512
513 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
Rushabh Mehtaef584552013-11-02 14:47:11 +0530514 + " = [?] " + company_currency)
Anand Doshibd67e872014-04-11 16:51:27 +0530515
Anand Doshi2168e392013-05-23 19:25:08 +0530516 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Anand Doshibd67e872014-04-11 16:51:27 +0530517 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
Rushabh Mehtaef584552013-11-02 14:47:11 +0530518 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530519 }
Anand Doshibd67e872014-04-11 16:51:27 +0530520
Anand Doshi2168e392013-05-23 19:25:08 +0530521 // toggle fields
Anand Doshibd67e872014-04-11 16:51:27 +0530522 this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total",
Anand Doshi2168e392013-05-23 19:25:08 +0530523 "grand_total", "rounded_total", "in_words"],
524 this.frm.doc.currency != company_currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530525
526 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
Anand Doshi2168e392013-05-23 19:25:08 +0530527 this.frm.doc.price_list_currency != company_currency);
Anand Doshibd67e872014-04-11 16:51:27 +0530528
Anand Doshi2168e392013-05-23 19:25:08 +0530529 // set labels
530 $.each(field_label_map, function(fname, label) {
531 me.frm.fields_dict[fname].set_label(label);
532 });
533 },
Anand Doshibd67e872014-04-11 16:51:27 +0530534
Anand Doshi2168e392013-05-23 19:25:08 +0530535 change_grid_labels: function(company_currency) {
536 var me = this;
537 var field_label_map = {};
Anand Doshibd67e872014-04-11 16:51:27 +0530538
Anand Doshi2168e392013-05-23 19:25:08 +0530539 var setup_field_label_map = function(fields_list, currency, parentfield) {
540 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
541 $.each(fields_list, function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530542 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530543 if(docfield) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530544 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshibd67e872014-04-11 16:51:27 +0530545 field_label_map[grid_doctype + "-" + fname] =
Anand Doshi2168e392013-05-23 19:25:08 +0530546 label.trim() + " (" + currency + ")";
547 }
548 });
549 }
Anand Doshibd67e872014-04-11 16:51:27 +0530550
Nabin Hait1eb56012014-02-10 19:20:15 +0530551 setup_field_label_map(["base_rate", "base_price_list_rate", "base_amount"],
Nabin Haitdd38a262014-12-26 13:15:21 +0530552 company_currency, "items");
Anand Doshibd67e872014-04-11 16:51:27 +0530553
Nabin Hait1eb56012014-02-10 19:20:15 +0530554 setup_field_label_map(["rate", "price_list_rate", "amount"],
Nabin Haitdd38a262014-12-26 13:15:21 +0530555 this.frm.doc.currency, "items");
Anand Doshibd67e872014-04-11 16:51:27 +0530556
Nabin Haite7d15362014-12-25 16:01:55 +0530557 setup_field_label_map(["tax_amount", "total"], company_currency, "taxes");
Anand Doshibd67e872014-04-11 16:51:27 +0530558
Nabin Haite7d15362014-12-25 16:01:55 +0530559 if(this.frm.fields_dict["advances"]) {
Anand Doshi2168e392013-05-23 19:25:08 +0530560 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency,
Nabin Haite7d15362014-12-25 16:01:55 +0530561 "advances");
Anand Doshi2168e392013-05-23 19:25:08 +0530562 }
Anand Doshibd67e872014-04-11 16:51:27 +0530563
Anand Doshi2168e392013-05-23 19:25:08 +0530564 // toggle columns
Nabin Haitdd38a262014-12-26 13:15:21 +0530565 var item_grid = this.frm.fields_dict["items"].grid;
Anand Doshibd67e872014-04-11 16:51:27 +0530566 var show = (this.frm.doc.currency != company_currency) ||
Nabin Haite7d15362014-12-25 16:01:55 +0530567 ((cur_frm.doc.taxes || []).filter(
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530568 function(d) { return d.included_in_print_rate===1}).length);
Anand Doshibd67e872014-04-11 16:51:27 +0530569
Nabin Hait1eb56012014-02-10 19:20:15 +0530570 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530571 if(frappe.meta.get_docfield(item_grid.doctype, fname))
Anand Doshi2168e392013-05-23 19:25:08 +0530572 item_grid.set_column_disp(fname, show);
573 });
Anand Doshibd67e872014-04-11 16:51:27 +0530574
Anand Doshi2168e392013-05-23 19:25:08 +0530575 // set labels
576 var $wrapper = $(this.frm.wrapper);
577 $.each(field_label_map, function(fname, label) {
Anand Doshi5013dcb2013-08-05 12:16:04 +0530578 fname = fname.split("-");
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530579 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530580 if(df) df.label = label;
Anand Doshi2168e392013-05-23 19:25:08 +0530581 });
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530582 }
Nabin Hait0b157552014-06-24 17:02:45 +0530583});
Anand Doshi43f087c2014-08-26 14:25:53 +0530584
585frappe.ui.form.on(cur_frm.doctype,"project_name", function(frm) {
Nabin Hait10b155a2014-10-23 13:18:59 +0530586 if(in_list(["Delivery Note", "Sales Invoice"], frm.doc.doctype)) {
587 frappe.call({
588 method:'erpnext.projects.doctype.project.project.get_cost_center_name' ,
589 args: { project_name: frm.doc.project_name },
590 callback: function(r, rt) {
591 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530592 $.each(frm.doc["items"] || [], function(i, row) {
Neil Trini Lasrado6fc28212014-10-28 14:36:26 +0530593 frappe.model.set_value(row.doctype, row.name, "cost_center", r.message);
594 msgprint(__("Cost Center For Item with Item Code '"+row.item_name+"' has been Changed to "+ r.message));
Nabin Hait10b155a2014-10-23 13:18:59 +0530595 })
596 }
Anand Doshi43f087c2014-08-26 14:25:53 +0530597 }
Nabin Hait10b155a2014-10-23 13:18:59 +0530598 })
599 }
Anand Doshi43f087c2014-08-26 14:25:53 +0530600})