Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | // License: GNU General Public License v3. See license.txt |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 3 | |
Nabin Hait | b4a3dfa | 2015-01-22 17:13:13 +0530 | [diff] [blame] | 4 | erpnext.TransactionController = erpnext.taxes_and_totals.extend({ |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 5 | setup: function() { |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 6 | this._super(); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 7 | frappe.ui.form.on(this.frm.doctype + " Item", "rate", function(frm, cdt, cdn) { |
| 8 | var item = frappe.get_doc(cdt, cdn); |
Rushabh Mehta | f69ffeb | 2017-05-17 19:40:40 +0530 | [diff] [blame] | 9 | var has_margin_field = frappe.meta.has_field(cdt, 'margin_type'); |
| 10 | |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 11 | frappe.model.round_floats_in(item, ["rate", "price_list_rate"]); |
| 12 | |
| 13 | if(item.price_list_rate) { |
Rushabh Mehta | f69ffeb | 2017-05-17 19:40:40 +0530 | [diff] [blame] | 14 | if(item.rate > item.price_list_rate && has_margin_field) { |
| 15 | // if rate is greater than price_list_rate, set margin |
| 16 | // or set discount |
| 17 | item.discount_percentage = 0; |
Nabin Hait | e72c98d | 2017-05-24 09:01:44 +0530 | [diff] [blame] | 18 | item.margin_type = 'Amount'; |
| 19 | item.margin_rate_or_amount = flt(item.rate - item.price_list_rate, |
| 20 | precision("margin_rate_or_amount", item)); |
| 21 | item.rate_with_margin = item.rate; |
Rushabh Mehta | f69ffeb | 2017-05-17 19:40:40 +0530 | [diff] [blame] | 22 | } else { |
| 23 | item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, |
| 24 | precision("discount_percentage", item)); |
| 25 | item.margin_type = ''; |
| 26 | item.margin_rate_or_amount = 0; |
| 27 | item.rate_with_margin = 0; |
| 28 | } |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 29 | } else { |
| 30 | item.discount_percentage = 0.0; |
Rushabh Mehta | f69ffeb | 2017-05-17 19:40:40 +0530 | [diff] [blame] | 31 | item.margin_type = ''; |
| 32 | item.margin_rate_or_amount = 0; |
| 33 | item.rate_with_margin = 0; |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | cur_frm.cscript.set_gross_profit(item); |
| 37 | cur_frm.cscript.calculate_taxes_and_totals(); |
| 38 | }) |
| 39 | |
| 40 | frappe.ui.form.on(this.frm.cscript.tax_table, "rate", function(frm, cdt, cdn) { |
| 41 | cur_frm.cscript.calculate_taxes_and_totals(); |
Rushabh Mehta | 764aa92 | 2016-05-02 13:28:46 +0530 | [diff] [blame] | 42 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 43 | |
| 44 | frappe.ui.form.on(this.frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) { |
| 45 | cur_frm.cscript.calculate_taxes_and_totals(); |
Rushabh Mehta | 764aa92 | 2016-05-02 13:28:46 +0530 | [diff] [blame] | 46 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 47 | |
| 48 | frappe.ui.form.on(this.frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) { |
| 49 | cur_frm.cscript.calculate_taxes_and_totals(); |
Rushabh Mehta | 764aa92 | 2016-05-02 13:28:46 +0530 | [diff] [blame] | 50 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 51 | |
| 52 | frappe.ui.form.on(this.frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) { |
| 53 | cur_frm.cscript.set_dynamic_labels(); |
| 54 | cur_frm.cscript.calculate_taxes_and_totals(); |
Rushabh Mehta | 764aa92 | 2016-05-02 13:28:46 +0530 | [diff] [blame] | 55 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 56 | |
| 57 | frappe.ui.form.on(this.frm.doctype, "apply_discount_on", function(frm) { |
| 58 | if(frm.doc.additional_discount_percentage) { |
| 59 | frm.trigger("additional_discount_percentage"); |
| 60 | } else { |
| 61 | cur_frm.cscript.calculate_taxes_and_totals(); |
| 62 | } |
Rushabh Mehta | 764aa92 | 2016-05-02 13:28:46 +0530 | [diff] [blame] | 63 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 64 | |
| 65 | frappe.ui.form.on(this.frm.doctype, "additional_discount_percentage", function(frm) { |
| 66 | if (frm.via_discount_amount) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if(!frm.doc.apply_discount_on) { |
| 71 | frappe.msgprint(__("Please set 'Apply Additional Discount On'")); |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | frm.via_discount_percentage = true; |
| 76 | |
| 77 | if(frm.doc.additional_discount_percentage && frm.doc.discount_amount) { |
| 78 | // Reset discount amount and net / grand total |
| 79 | frm.set_value("discount_amount", 0); |
| 80 | } |
| 81 | |
| 82 | var total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]); |
| 83 | var discount_amount = flt(total*flt(frm.doc.additional_discount_percentage) / 100, |
| 84 | precision("discount_amount")); |
| 85 | |
| 86 | frm.set_value("discount_amount", discount_amount); |
| 87 | delete frm.via_discount_percentage; |
| 88 | }); |
| 89 | |
| 90 | frappe.ui.form.on(this.frm.doctype, "discount_amount", function(frm) { |
| 91 | frm.cscript.set_dynamic_labels(); |
| 92 | |
| 93 | if (!frm.via_discount_percentage) { |
| 94 | frm.via_discount_amount = true; |
| 95 | frm.set_value("additional_discount_percentage", 0); |
| 96 | delete frm.via_discount_amount; |
| 97 | } |
| 98 | |
| 99 | frm.cscript.calculate_taxes_and_totals(); |
| 100 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 101 | }, |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 102 | onload: function() { |
Anand Doshi | 1e4fb68 | 2013-08-23 12:56:33 +0530 | [diff] [blame] | 103 | var me = this; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 104 | if(this.frm.doc.__islocal) { |
Anand Doshi | 1e4fb68 | 2013-08-23 12:56:33 +0530 | [diff] [blame] | 105 | var today = get_today(), |
Nabin Hait | 73083dc | 2014-03-14 15:10:42 +0530 | [diff] [blame] | 106 | currency = frappe.defaults.get_user_default("currency"); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 107 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 108 | $.each({ |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 109 | currency: currency, |
| 110 | price_list_currency: currency, |
| 111 | status: "Draft", |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 112 | is_subcontracted: "No", |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 113 | }, function(fieldname, value) { |
| 114 | if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname]) |
| 115 | me.frm.set_value(fieldname, value); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 116 | }); |
nabinhait | bc408d8 | 2014-07-07 16:51:52 +0530 | [diff] [blame] | 117 | |
nabinhait | b739264 | 2014-07-28 14:55:08 +0530 | [diff] [blame] | 118 | if(this.frm.doc.company && !this.frm.doc.amended_from) { |
Rushabh Mehta | b66edd1 | 2017-03-30 21:21:56 +0530 | [diff] [blame] | 119 | this.frm.trigger("company"); |
nabinhait | bc408d8 | 2014-07-07 16:51:52 +0530 | [diff] [blame] | 120 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 121 | } |
Akhilesh Darjee | 564c621 | 2013-10-17 11:31:11 +0530 | [diff] [blame] | 122 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 123 | if(this.frm.fields_dict["taxes"]) { |
| 124 | this["taxes_remove"] = this.calculate_taxes_and_totals; |
Anand Doshi | a91c95f | 2013-08-23 13:00:47 +0530 | [diff] [blame] | 125 | } |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 126 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 127 | if(this.frm.fields_dict["items"]) { |
| 128 | this["items_remove"] = this.calculate_taxes_and_totals; |
Anand Doshi | 1e4fb68 | 2013-08-23 12:56:33 +0530 | [diff] [blame] | 129 | } |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 130 | |
Neil Trini Lasrado | e2d8dd0 | 2015-06-22 19:06:15 +0530 | [diff] [blame] | 131 | if(this.frm.fields_dict["recurring_print_format"]) { |
| 132 | this.frm.set_query("recurring_print_format", function(doc) { |
| 133 | return{ |
| 134 | filters: [ |
| 135 | ['Print Format', 'doc_type', '=', cur_frm.doctype], |
| 136 | ] |
| 137 | } |
| 138 | }); |
| 139 | } |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 140 | |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 141 | if(this.frm.fields_dict["return_against"]) { |
| 142 | this.frm.set_query("return_against", function(doc) { |
| 143 | var filters = { |
| 144 | "docstatus": 1, |
| 145 | "is_return": 0, |
| 146 | "company": doc.company |
| 147 | }; |
| 148 | if (me.frm.fields_dict["customer"] && doc.customer) filters["customer"] = doc.customer; |
| 149 | if (me.frm.fields_dict["supplier"] && doc.supplier) filters["supplier"] = doc.supplier; |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 150 | |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 151 | return { |
| 152 | filters: filters |
| 153 | } |
| 154 | }); |
| 155 | } |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 156 | |
Nabin Hait | 8af429d | 2016-11-16 17:21:59 +0530 | [diff] [blame] | 157 | this.setup_quality_inspection(); |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 158 | }, |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 159 | |
Nabin Hait | 8af429d | 2016-11-16 17:21:59 +0530 | [diff] [blame] | 160 | setup_quality_inspection: function() { |
| 161 | if(!in_list(["Delivery Note", "Sales Invoice", "Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype)) { |
| 162 | return; |
| 163 | } |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 164 | var me = this; |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 165 | var inspection_type = in_list(["Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype) |
Nabin Hait | 8af429d | 2016-11-16 17:21:59 +0530 | [diff] [blame] | 166 | ? "Incoming" : "Outgoing"; |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 167 | |
Manas Solanki | a8d1bc7 | 2016-11-15 14:48:23 +0530 | [diff] [blame] | 168 | var quality_inspection_field = this.frm.get_docfield("items", "quality_inspection"); |
Nabin Hait | 8af429d | 2016-11-16 17:21:59 +0530 | [diff] [blame] | 169 | quality_inspection_field.get_route_options_for_new_doc = function(row) { |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 170 | if(me.frm.is_new()) return; |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 171 | return { |
| 172 | "inspection_type": inspection_type, |
Manas Solanki | 34feab1 | 2016-11-16 14:36:28 +0530 | [diff] [blame] | 173 | "reference_type": me.frm.doc.doctype, |
Manas Solanki | dbc2511 | 2016-11-16 15:06:08 +0530 | [diff] [blame] | 174 | "reference_name": me.frm.doc.name, |
Nabin Hait | 8af429d | 2016-11-16 17:21:59 +0530 | [diff] [blame] | 175 | "item_code": row.doc.item_code, |
| 176 | "description": row.doc.description, |
| 177 | "item_serial_no": row.doc.serial_no ? row.doc.serial_no.split("\n")[0] : null, |
| 178 | "batch_no": row.doc.batch_no |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 179 | } |
| 180 | } |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 181 | |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 182 | this.frm.set_query("quality_inspection", "items", function(doc, cdt, cdn) { |
| 183 | var d = locals[cdt][cdn]; |
| 184 | return { |
| 185 | filters: { |
| 186 | docstatus: 1, |
| 187 | inspection_type: inspection_type, |
| 188 | item_code: d.item_code |
| 189 | } |
| 190 | } |
| 191 | }); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 192 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 193 | |
Anand Doshi | 5e4e5d7 | 2013-08-06 17:23:44 +0530 | [diff] [blame] | 194 | onload_post_render: function() { |
Akhilesh Darjee | 61b5c59 | 2013-10-15 20:24:34 +0530 | [diff] [blame] | 195 | var me = this; |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 196 | if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length |
| 197 | && !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) { |
| 198 | this.apply_default_taxes(); |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 199 | } else if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"] |
Nabin Hait | bdfb070 | 2015-10-28 15:38:08 +0530 | [diff] [blame] | 200 | && !this.frm.doc.is_pos) { |
| 201 | me.calculate_taxes_and_totals(); |
Anand Doshi | 5e4e5d7 | 2013-08-06 17:23:44 +0530 | [diff] [blame] | 202 | } |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 203 | if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) { |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 204 | this.setup_item_selector(); |
Rushabh Mehta | b6843f2 | 2014-06-04 13:10:41 +0530 | [diff] [blame] | 205 | } |
Anand Doshi | 5e4e5d7 | 2013-08-06 17:23:44 +0530 | [diff] [blame] | 206 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 207 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 208 | refresh: function() { |
Anand Doshi | 8bde7f9 | 2014-04-24 18:11:49 +0530 | [diff] [blame] | 209 | erpnext.toggle_naming_series(); |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 210 | erpnext.hide_company(); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 211 | this.show_item_wise_taxes(); |
Nabin Hait | 1f996c3 | 2013-07-29 19:00:53 +0530 | [diff] [blame] | 212 | this.set_dynamic_labels(); |
Rushabh Mehta | 614e7ab | 2015-02-18 19:51:48 +0530 | [diff] [blame] | 213 | this.setup_sms(); |
| 214 | }, |
Rushabh Mehta | 4a3c805 | 2015-05-19 11:10:12 +0530 | [diff] [blame] | 215 | |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 216 | apply_default_taxes: function() { |
| 217 | var me = this; |
Rushabh Mehta | 4984bcc | 2015-05-21 12:51:07 +0530 | [diff] [blame] | 218 | var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges", |
| 219 | me.frm.doc.name); |
| 220 | |
| 221 | if(taxes_and_charges_field) { |
Nabin Hait | bdfb070 | 2015-10-28 15:38:08 +0530 | [diff] [blame] | 222 | return frappe.call({ |
Rushabh Mehta | 4984bcc | 2015-05-21 12:51:07 +0530 | [diff] [blame] | 223 | method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges", |
| 224 | args: { |
| 225 | "master_doctype": taxes_and_charges_field.options |
| 226 | }, |
| 227 | callback: function(r) { |
| 228 | if(!r.exc) { |
| 229 | me.frm.set_value("taxes", r.message); |
Nabin Hait | bdfb070 | 2015-10-28 15:38:08 +0530 | [diff] [blame] | 230 | me.calculate_taxes_and_totals(); |
Rushabh Mehta | 4984bcc | 2015-05-21 12:51:07 +0530 | [diff] [blame] | 231 | } |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 232 | } |
Rushabh Mehta | 4984bcc | 2015-05-21 12:51:07 +0530 | [diff] [blame] | 233 | }); |
| 234 | } |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 235 | }, |
Rushabh Mehta | 614e7ab | 2015-02-18 19:51:48 +0530 | [diff] [blame] | 236 | |
| 237 | setup_sms: function() { |
| 238 | var me = this; |
Nabin Hait | 7132bd3 | 2016-04-13 15:19:37 +0530 | [diff] [blame] | 239 | if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped", "Closed"], this.frm.doc.status) |
Rushabh Mehta | 4a3c805 | 2015-05-19 11:10:12 +0530 | [diff] [blame] | 240 | && this.frm.doctype != "Purchase Invoice") { |
Rushabh Mehta | 614e7ab | 2015-02-18 19:51:48 +0530 | [diff] [blame] | 241 | this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); }); |
| 242 | } |
| 243 | }, |
| 244 | |
| 245 | send_sms: function() { |
Rushabh Mehta | 4a3c805 | 2015-05-19 11:10:12 +0530 | [diff] [blame] | 246 | var sms_man = new SMSManager(this.frm.doc); |
Nabin Hait | 239c2c3 | 2015-01-15 14:21:41 +0530 | [diff] [blame] | 247 | }, |
Anand Doshi | 50d7e8c | 2015-01-06 17:14:13 +0530 | [diff] [blame] | 248 | |
Neil Trini Lasrado | d015195 | 2015-06-09 15:21:50 +0530 | [diff] [blame] | 249 | barcode: function(doc, cdt, cdn) { |
| 250 | var d = locals[cdt][cdn]; |
| 251 | if(d.barcode=="" || d.barcode==null) { |
| 252 | // barcode cleared, remove item |
| 253 | d.item_code = ""; |
| 254 | } |
Rushabh Mehta | 675f9c6 | 2016-06-02 16:34:00 +0530 | [diff] [blame] | 255 | this.item_code(doc, cdt, cdn, true); |
Neil Trini Lasrado | d015195 | 2015-06-09 15:21:50 +0530 | [diff] [blame] | 256 | }, |
| 257 | |
Rushabh Mehta | 675f9c6 | 2016-06-02 16:34:00 +0530 | [diff] [blame] | 258 | item_code: function(doc, cdt, cdn, from_barcode) { |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 259 | var me = this; |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 260 | var item = frappe.get_doc(cdt, cdn); |
Rushabh Mehta | 675f9c6 | 2016-06-02 16:34:00 +0530 | [diff] [blame] | 261 | |
| 262 | // clear barcode if setting item (else barcode will take priority) |
| 263 | if(!from_barcode) { |
| 264 | item.barcode = null; |
| 265 | } |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 266 | if(item.item_code || item.barcode || item.serial_no) { |
| 267 | if(!this.validate_company_and_party()) { |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 268 | cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove(); |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 269 | } else { |
| 270 | return this.frm.call({ |
| 271 | method: "erpnext.stock.get_item_details.get_item_details", |
| 272 | child: item, |
| 273 | args: { |
| 274 | args: { |
| 275 | item_code: item.item_code, |
| 276 | barcode: item.barcode, |
| 277 | serial_no: item.serial_no, |
| 278 | warehouse: item.warehouse, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 279 | customer: me.frm.doc.customer, |
| 280 | supplier: me.frm.doc.supplier, |
| 281 | currency: me.frm.doc.currency, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 282 | update_stock: in_list(['Sales Invoice', 'Purchase Invoice'], me.frm.doc.doctype) ? cint(me.frm.doc.update_stock) : 0, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 283 | conversion_rate: me.frm.doc.conversion_rate, |
| 284 | price_list: me.frm.doc.selling_price_list || |
| 285 | me.frm.doc.buying_price_list, |
| 286 | price_list_currency: me.frm.doc.price_list_currency, |
| 287 | plc_conversion_rate: me.frm.doc.plc_conversion_rate, |
| 288 | company: me.frm.doc.company, |
| 289 | order_type: me.frm.doc.order_type, |
| 290 | is_pos: cint(me.frm.doc.is_pos), |
| 291 | is_subcontracted: me.frm.doc.is_subcontracted, |
Nabin Hait | a1bf43b | 2015-03-17 10:50:47 +0530 | [diff] [blame] | 292 | transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date, |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 293 | ignore_pricing_rule: me.frm.doc.ignore_pricing_rule, |
Anand Doshi | afe85bd | 2016-01-21 21:04:16 +0530 | [diff] [blame] | 294 | doctype: me.frm.doc.doctype, |
| 295 | name: me.frm.doc.name, |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 296 | project: item.project || me.frm.doc.project, |
mbauskar | ba41242 | 2017-04-19 12:35:51 +0530 | [diff] [blame] | 297 | qty: item.qty || 1, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 298 | stock_qty: item.stock_qty, |
| 299 | conversion_factor: item.conversion_factor |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 300 | } |
| 301 | }, |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 302 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 303 | callback: function(r) { |
| 304 | if(!r.exc) { |
| 305 | me.frm.script_manager.trigger("price_list_rate", cdt, cdn); |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 306 | me.toggle_conversion_factor(item); |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | }); |
| 310 | } |
| 311 | } |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 312 | }, |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 313 | |
| 314 | serial_no: function(doc, cdt, cdn) { |
| 315 | var me = this; |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 316 | var item = frappe.get_doc(cdt, cdn); |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 317 | |
| 318 | if (item.serial_no) { |
| 319 | if (!item.item_code) { |
Rushabh Mehta | b66edd1 | 2017-03-30 21:21:56 +0530 | [diff] [blame] | 320 | this.frm.trigger("item_code", cdt, cdn); |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 321 | } |
| 322 | else { |
| 323 | var sr_no = []; |
| 324 | |
| 325 | // Replacing all occurences of comma with carriage return |
| 326 | var serial_nos = item.serial_no.trim().replace(/,/g, '\n'); |
| 327 | |
| 328 | serial_nos = serial_nos.trim().split('\n'); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 329 | |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 330 | // Trim each string and push unique string to new list |
| 331 | for (var x=0; x<=serial_nos.length - 1; x++) { |
| 332 | if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) { |
| 333 | sr_no.push(serial_nos[x].trim()); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // Add the new list to the serial no. field in grid with each in new line |
| 338 | item.serial_no = ""; |
| 339 | for (var x=0; x<=sr_no.length - 1; x++) |
| 340 | item.serial_no += sr_no[x] + '\n'; |
| 341 | |
| 342 | refresh_field("serial_no", item.name, item.parentfield); |
Nabin Hait | b7c0c55 | 2015-08-04 12:18:12 +0530 | [diff] [blame] | 343 | if(!doc.is_return) { |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 344 | frappe.model.set_value(item.doctype, item.name, |
Nabin Hait | e68f32c | 2017-03-03 15:00:41 +0530 | [diff] [blame] | 345 | "qty", sr_no.length / item.conversion_factor); |
| 346 | frappe.model.set_value(item.doctype, item.name, "stock_qty", sr_no.length); |
Nabin Hait | b7c0c55 | 2015-08-04 12:18:12 +0530 | [diff] [blame] | 347 | } |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 351 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 352 | validate: function() { |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 353 | this.calculate_taxes_and_totals(false); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 354 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 355 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 356 | company: function() { |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 357 | var me = this; |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 358 | var set_pricing = function() { |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 359 | if(me.frm.doc.company && me.frm.fields_dict.currency) { |
| 360 | var company_currency = me.get_company_currency(); |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 361 | var company_doc = frappe.get_doc(":Company", me.frm.doc.company); |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 362 | if (!me.frm.doc.currency) { |
| 363 | me.frm.set_value("currency", company_currency); |
| 364 | } |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 365 | |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 366 | if (me.frm.doc.currency == company_currency) { |
| 367 | me.frm.set_value("conversion_rate", 1.0); |
| 368 | } |
| 369 | if (me.frm.doc.price_list_currency == company_currency) { |
| 370 | me.frm.set_value('plc_conversion_rate', 1.0); |
| 371 | } |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 372 | if (company_doc.default_letter_head) { |
Rushabh Mehta | c185734 | 2015-05-27 12:29:57 +0530 | [diff] [blame] | 373 | if(me.frm.fields_dict.letter_head) { |
| 374 | me.frm.set_value("letter_head", company_doc.default_letter_head); |
| 375 | } |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 376 | } |
Rohit Waghchaure | 585448b | 2016-12-30 15:41:55 +0530 | [diff] [blame] | 377 | if (company_doc.default_terms && me.frm.doc.doctype != "Purchase Invoice" && frappe.meta.has_field(me.frm.doc.doctype, "tc_name")) { |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 378 | me.frm.set_value("tc_name", company_doc.default_terms); |
| 379 | } |
Akhilesh Darjee | 1797cfe | 2013-10-11 13:34:10 +0530 | [diff] [blame] | 380 | |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 381 | me.frm.script_manager.trigger("currency"); |
| 382 | me.apply_pricing_rule(); |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 383 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 384 | } |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 385 | |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 386 | var set_party_account = function(set_pricing) { |
| 387 | if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) { |
| 388 | if(me.frm.doc.doctype=="Sales Invoice") { |
| 389 | var party_type = "Customer"; |
| 390 | var party_account_field = 'debit_to'; |
| 391 | } else { |
| 392 | var party_type = "Supplier"; |
| 393 | var party_account_field = 'credit_to'; |
| 394 | } |
Anand Doshi | 39a2891 | 2016-02-15 11:42:18 +0530 | [diff] [blame] | 395 | |
Nabin Hait | ef86440 | 2016-02-10 14:47:25 +0530 | [diff] [blame] | 396 | var party = me.frm.doc[frappe.model.scrub(party_type)]; |
| 397 | if(party) { |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 398 | return frappe.call({ |
| 399 | method: "erpnext.accounts.party.get_party_account", |
| 400 | args: { |
| 401 | company: me.frm.doc.company, |
| 402 | party_type: party_type, |
Nabin Hait | ef86440 | 2016-02-10 14:47:25 +0530 | [diff] [blame] | 403 | party: party |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 404 | }, |
| 405 | callback: function(r) { |
| 406 | if(!r.exc && r.message) { |
| 407 | me.frm.set_value(party_account_field, r.message); |
| 408 | set_pricing(); |
| 409 | } |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 410 | } |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 411 | }); |
Nabin Hait | ef86440 | 2016-02-10 14:47:25 +0530 | [diff] [blame] | 412 | } else { |
| 413 | set_pricing(); |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 414 | } |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 415 | } else { |
| 416 | set_pricing(); |
| 417 | } |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 418 | |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 419 | } |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 420 | |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 421 | if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date; |
| 422 | else var date = this.frm.doc.transaction_date; |
Rushabh Mehta | 5dfe20c | 2016-04-26 12:59:08 +0530 | [diff] [blame] | 423 | |
| 424 | if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") && |
Rohit Waghchaure | b2e7a1f | 2016-04-11 12:10:20 +0530 | [diff] [blame] | 425 | in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)){ |
| 426 | erpnext.utils.get_shipping_address(this.frm, function(){ |
| 427 | set_party_account(set_pricing); |
| 428 | }) |
| 429 | }else{ |
| 430 | set_party_account(set_pricing); |
| 431 | } |
Rushabh Mehta | 45ca8a4 | 2015-04-28 16:02:09 +0530 | [diff] [blame] | 432 | |
| 433 | if(this.frm.doc.company) { |
| 434 | erpnext.last_selected_company = this.frm.doc.company; |
| 435 | } |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 436 | }, |
| 437 | |
| 438 | transaction_date: function() { |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 439 | if (this.frm.doc.transaction_date) { |
| 440 | this.frm.transaction_date = this.frm.doc.transaction_date; |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 441 | frappe.ui.form.trigger(this.frm.doc.doctype, "currency"); |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 442 | } |
Neil Trini Lasrado | 78fa695 | 2014-10-03 17:43:02 +0530 | [diff] [blame] | 443 | }, |
| 444 | |
| 445 | posting_date: function() { |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 446 | var me = this; |
| 447 | if (this.frm.doc.posting_date) { |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 448 | this.frm.posting_date = this.frm.doc.posting_date; |
| 449 | |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 450 | if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) || |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 451 | (this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) { |
| 452 | return frappe.call({ |
| 453 | method: "erpnext.accounts.party.get_due_date", |
| 454 | args: { |
| 455 | "posting_date": me.frm.doc.posting_date, |
| 456 | "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier", |
| 457 | "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier, |
| 458 | "company": me.frm.doc.company |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 459 | }, |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 460 | callback: function(r, rt) { |
| 461 | if(r.message) { |
| 462 | me.frm.set_value("due_date", r.message); |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 463 | frappe.ui.form.trigger(me.frm.doc.doctype, "currency"); |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 464 | } |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 465 | } |
| 466 | }) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 467 | } else { |
| 468 | frappe.ui.form.trigger(me.frm.doc.doctype, "currency"); |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 469 | } |
| 470 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 471 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 472 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 473 | get_company_currency: function() { |
| 474 | return erpnext.get_currency(this.frm.doc.company); |
| 475 | }, |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 476 | |
Neil Trini Lasrado | 09a66c4 | 2015-07-07 16:41:37 +0530 | [diff] [blame] | 477 | contact_person: function() { |
| 478 | erpnext.utils.get_contact_details(this.frm); |
| 479 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 480 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 481 | currency: function() { |
Chude Osiegbu | 256ec17 | 2016-09-19 03:53:37 +0100 | [diff] [blame] | 482 | /* manqala 19/09/2016: let the translation date be whichever of the transaction_date or posting_date is available */ |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 483 | var transaction_date = this.frm.doc.transaction_date || this.frm.doc.posting_date; |
Chude Osiegbu | 256ec17 | 2016-09-19 03:53:37 +0100 | [diff] [blame] | 484 | /* end manqala */ |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 485 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 486 | var me = this; |
| 487 | this.set_dynamic_labels(); |
Akhilesh Darjee | 7837827 | 2013-10-11 19:06:30 +0530 | [diff] [blame] | 488 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 489 | var company_currency = this.get_company_currency(); |
Nabin Hait | 69cdf59 | 2015-05-02 18:46:10 +0530 | [diff] [blame] | 490 | // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc |
Rushabh Mehta | 76b1341 | 2016-07-18 14:30:18 +0530 | [diff] [blame] | 491 | if(this.frm.doc.currency && this.frm.doc.currency !== company_currency |
Nabin Hait | 3950507 | 2016-07-07 11:11:32 +0530 | [diff] [blame] | 492 | && !this.frm.doc.ignore_pricing_rule) { |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 493 | this.get_exchange_rate(transaction_date, this.frm.doc.currency, company_currency, |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 494 | function(exchange_rate) { |
Akhilesh Darjee | 7837827 | 2013-10-11 19:06:30 +0530 | [diff] [blame] | 495 | me.frm.set_value("conversion_rate", exchange_rate); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 496 | }); |
| 497 | } else { |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 498 | this.conversion_rate(); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 499 | } |
| 500 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 501 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 502 | conversion_rate: function() { |
Anand Doshi | 535d833 | 2013-07-19 13:51:07 +0530 | [diff] [blame] | 503 | if(this.frm.doc.currency === this.get_company_currency()) { |
| 504 | this.frm.set_value("conversion_rate", 1.0); |
Akhilesh Darjee | acf65a7 | 2013-10-17 13:04:26 +0530 | [diff] [blame] | 505 | } |
| 506 | if(this.frm.doc.currency === this.frm.doc.price_list_currency && |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 507 | this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) { |
| 508 | this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate); |
| 509 | } |
Rushabh Mehta | 4a3c805 | 2015-05-19 11:10:12 +0530 | [diff] [blame] | 510 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 511 | if(flt(this.frm.doc.conversion_rate)>0.0) { |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 512 | if(this.frm.doc.ignore_pricing_rule) { |
| 513 | this.calculate_taxes_and_totals(); |
Anand Doshi | 51f722d | 2014-07-04 15:44:26 +0530 | [diff] [blame] | 514 | } else if (!this.in_apply_price_list){ |
| 515 | this.apply_price_list(); |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 516 | } |
| 517 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 518 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 519 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 520 | |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 521 | get_exchange_rate: function(transaction_date, from_currency, to_currency, callback) { |
Nabin Hait | 17179ee | 2017-05-06 13:23:12 +0530 | [diff] [blame] | 522 | if (!transaction_date || !from_currency || !to_currency) return; |
Nabin Hait | bdfb070 | 2015-10-28 15:38:08 +0530 | [diff] [blame] | 523 | return frappe.call({ |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 524 | method: "erpnext.setup.utils.get_exchange_rate", |
| 525 | args: { |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 526 | transaction_date: transaction_date, |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 527 | from_currency: from_currency, |
| 528 | to_currency: to_currency |
| 529 | }, |
| 530 | callback: function(r) { |
| 531 | callback(flt(r.message)); |
| 532 | } |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 533 | }); |
| 534 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 535 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 536 | price_list_currency: function() { |
Akhilesh Darjee | db59ffb | 2013-09-11 13:05:24 +0530 | [diff] [blame] | 537 | var me=this; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 538 | this.set_dynamic_labels(); |
Nabin Hait | ec361ea | 2015-05-11 18:14:45 +0530 | [diff] [blame] | 539 | |
| 540 | var company_currency = this.get_company_currency(); |
| 541 | // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc |
| 542 | if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) { |
Chude Osiegbu | fbf27b5 | 2016-09-07 03:06:08 +0100 | [diff] [blame] | 543 | this.get_exchange_rate(this.frm.doc.posting_date, this.frm.doc.price_list_currency, company_currency, |
Nabin Hait | ec361ea | 2015-05-11 18:14:45 +0530 | [diff] [blame] | 544 | function(exchange_rate) { |
| 545 | me.frm.set_value("plc_conversion_rate", exchange_rate); |
| 546 | }); |
| 547 | } else { |
| 548 | this.plc_conversion_rate(); |
| 549 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 550 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 551 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 552 | plc_conversion_rate: function() { |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 553 | if(this.frm.doc.price_list_currency === this.get_company_currency()) { |
| 554 | this.frm.set_value("plc_conversion_rate", 1.0); |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 555 | } else if(this.frm.doc.price_list_currency === this.frm.doc.currency |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 556 | && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 && |
Nabin Hait | ec361ea | 2015-05-11 18:14:45 +0530 | [diff] [blame] | 557 | cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) { |
| 558 | this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate); |
Akhilesh Darjee | 564c621 | 2013-10-17 11:31:11 +0530 | [diff] [blame] | 559 | } |
Rushabh Mehta | 4a3c805 | 2015-05-19 11:10:12 +0530 | [diff] [blame] | 560 | |
Nabin Hait | ec361ea | 2015-05-11 18:14:45 +0530 | [diff] [blame] | 561 | if(!this.in_apply_price_list) { |
| 562 | this.apply_price_list(); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 563 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 564 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 565 | |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 566 | uom: function(doc, cdt, cdn) { |
| 567 | var me = this; |
| 568 | var item = frappe.get_doc(cdt, cdn); |
| 569 | if(item.item_code && item.uom) { |
| 570 | return this.frm.call({ |
| 571 | method: "erpnext.stock.get_item_details.get_conversion_factor", |
| 572 | child: item, |
| 573 | args: { |
| 574 | item_code: item.item_code, |
| 575 | uom: item.uom |
| 576 | }, |
| 577 | callback: function(r) { |
| 578 | if(!r.exc) { |
| 579 | me.conversion_factor(me.frm.doc, cdt, cdn); |
| 580 | } |
| 581 | } |
| 582 | }); |
| 583 | } |
| 584 | }, |
| 585 | |
Nabin Hait | 3257aee | 2017-05-06 12:47:14 +0530 | [diff] [blame] | 586 | conversion_factor: function(doc, cdt, cdn, dont_fetch_price_list_rate) { |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 587 | if(frappe.meta.get_docfield(cdt, "stock_qty", cdn)) { |
| 588 | var item = frappe.get_doc(cdt, cdn); |
| 589 | frappe.model.round_floats_in(item, ["qty", "conversion_factor"]); |
| 590 | item.stock_qty = flt(item.qty * item.conversion_factor, precision("stock_qty", item)); |
| 591 | refresh_field("stock_qty", item.name, item.parentfield); |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 592 | this.toggle_conversion_factor(item); |
Nabin Hait | 3257aee | 2017-05-06 12:47:14 +0530 | [diff] [blame] | 593 | if(!dont_fetch_price_list_rate) this.apply_price_list(item, true); |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 594 | } |
| 595 | }, |
| 596 | |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 597 | toggle_conversion_factor: function(item) { |
| 598 | // toggle read only property for conversion factor field if the uom and stock uom are same |
| 599 | this.frm.fields_dict.items.grid.toggle_enable("conversion_factor", |
| 600 | (item.uom != item.stock_uom)? true: false) |
| 601 | }, |
| 602 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 603 | qty: function(doc, cdt, cdn) { |
Nabin Hait | 3257aee | 2017-05-06 12:47:14 +0530 | [diff] [blame] | 604 | this.conversion_factor(doc, cdt, cdn, true); |
| 605 | this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 606 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 607 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 608 | set_dynamic_labels: function() { |
| 609 | // What TODO? should we make price list system non-mandatory? |
| 610 | this.frm.toggle_reqd("plc_conversion_rate", |
| 611 | !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency)); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 612 | |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 613 | if(this.frm.doc_currency!==this.frm.doc.currency) { |
| 614 | // reset names only when the currency is different |
| 615 | |
| 616 | var company_currency = this.get_company_currency(); |
| 617 | this.change_form_labels(company_currency); |
| 618 | this.change_grid_labels(company_currency); |
| 619 | this.frm.refresh_fields(); |
| 620 | this.frm.doc_currency = this.frm.doc.currency; |
| 621 | } |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 622 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 623 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 624 | change_form_labels: function(company_currency) { |
| 625 | var me = this; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 626 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 627 | this.frm.set_currency_labels(["base_total", "base_net_total", "base_total_taxes_and_charges", |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 628 | "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words", |
| 629 | "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay", |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 630 | "base_paid_amount", "base_write_off_amount", "base_change_amount", "base_operating_cost", |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 631 | "base_raw_material_cost", "base_total_cost", "base_scrap_material_cost" |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 632 | ], company_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 633 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 634 | this.frm.set_currency_labels(["total", "net_total", "total_taxes_and_charges", "discount_amount", |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 635 | "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted", |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 636 | "rounded_total", "in_words", "paid_amount", "write_off_amount", "operating_cost", "scrap_material_cost", |
| 637 | "raw_material_cost", "total_cost"], this.frm.doc.currency); |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 638 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 639 | this.frm.set_currency_labels(["outstanding_amount", "total_advance"], this.frm.doc.party_account_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 640 | |
| 641 | cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency |
| 642 | + " = [?] " + company_currency) |
| 643 | |
| 644 | if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) { |
| 645 | cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency |
| 646 | + " = [?] " + company_currency) |
| 647 | } |
| 648 | |
| 649 | // toggle fields |
| 650 | this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges", |
| 651 | "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 652 | "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount", |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 653 | "base_paid_amount", "base_write_off_amount", "base_operating_cost", |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 654 | "base_raw_material_cost", "base_total_cost", "base_scrap_material_cost"], |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 655 | this.frm.doc.currency != company_currency); |
| 656 | |
| 657 | this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"], |
| 658 | this.frm.doc.price_list_currency != company_currency); |
| 659 | |
Nabin Hait | 903c42a | 2015-02-24 15:24:49 +0530 | [diff] [blame] | 660 | var show =cint(cur_frm.doc.discount_amount) || |
| 661 | ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length); |
| 662 | |
| 663 | if(frappe.meta.get_docfield(cur_frm.doctype, "net_total")) |
| 664 | cur_frm.toggle_display("net_total", show); |
| 665 | |
| 666 | if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total")) |
| 667 | cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency))); |
| 668 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 669 | }, |
| 670 | |
| 671 | change_grid_labels: function(company_currency) { |
| 672 | var me = this; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 673 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 674 | this.frm.set_currency_labels(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"], |
| 675 | company_currency, "items"); |
| 676 | |
| 677 | this.frm.set_currency_labels(["rate", "net_rate", "price_list_rate", "amount", "net_amount"], |
| 678 | this.frm.doc.currency, "items"); |
| 679 | |
| 680 | if(this.frm.fields_dict["operations"]) { |
| 681 | this.frm.set_currency_labels(["operating_cost", "hour_rate"], this.frm.doc.currency, "operations"); |
| 682 | this.frm.set_currency_labels(["base_operating_cost", "base_hour_rate"], company_currency, "operations"); |
| 683 | |
| 684 | var item_grid = this.frm.fields_dict["operations"].grid; |
| 685 | $.each(["base_operating_cost", "base_hour_rate"], function(i, fname) { |
| 686 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 687 | item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 688 | }); |
| 689 | } |
| 690 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 691 | if(this.frm.fields_dict["scrap_items"]) { |
| 692 | this.frm.set_currency_labels(["rate", "amount"], this.frm.doc.currency, "scrap_items"); |
| 693 | this.frm.set_currency_labels(["base_rate", "base_amount"], company_currency, "scrap_items"); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 694 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 695 | var item_grid = this.frm.fields_dict["scrap_items"].grid; |
| 696 | $.each(["base_rate", "base_amount"], function(i, fname) { |
| 697 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 698 | item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); |
| 699 | }); |
| 700 | } |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 701 | |
| 702 | if(this.frm.fields_dict["taxes"]) { |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 703 | this.frm.set_currency_labels(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes"); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 704 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 705 | this.frm.set_currency_labels(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes"); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | if(this.frm.fields_dict["advances"]) { |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 709 | this.frm.set_currency_labels(["advance_amount", "allocated_amount"], |
Nabin Hait | aa01590 | 2015-10-16 13:08:33 +0530 | [diff] [blame] | 710 | this.frm.doc.party_account_currency, "advances"); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | // toggle columns |
| 714 | var item_grid = this.frm.fields_dict["items"].grid; |
| 715 | $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) { |
| 716 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 717 | item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); |
| 718 | }); |
| 719 | |
| 720 | var show = (cint(cur_frm.doc.discount_amount)) || |
| 721 | ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length); |
| 722 | |
| 723 | $.each(["net_rate", "net_amount"], function(i, fname) { |
| 724 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 725 | item_grid.set_column_disp(fname, show); |
| 726 | }); |
| 727 | |
| 728 | $.each(["base_net_rate", "base_net_amount"], function(i, fname) { |
| 729 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 730 | item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency))); |
| 731 | }); |
| 732 | |
| 733 | // set labels |
| 734 | var $wrapper = $(this.frm.wrapper); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 735 | }, |
| 736 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 737 | recalculate: function() { |
| 738 | this.calculate_taxes_and_totals(); |
| 739 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 740 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 741 | recalculate_values: function() { |
| 742 | this.calculate_taxes_and_totals(); |
| 743 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 744 | |
Anand Doshi | d0b0072 | 2013-05-28 18:54:48 +0530 | [diff] [blame] | 745 | calculate_charges: function() { |
| 746 | this.calculate_taxes_and_totals(); |
| 747 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 748 | |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 749 | ignore_pricing_rule: function() { |
Nabin Hait | 8c9fb76 | 2017-04-28 14:23:03 +0530 | [diff] [blame] | 750 | if(this.frm.doc.ignore_pricing_rule) { |
| 751 | var me = this; |
| 752 | var item_list = []; |
| 753 | |
| 754 | $.each(this.frm.doc["items"] || [], function(i, d) { |
| 755 | if (d.item_code) { |
| 756 | item_list.push({ |
| 757 | "doctype": d.doctype, |
| 758 | "name": d.name, |
| 759 | "pricing_rule": d.pricing_rule |
| 760 | }) |
| 761 | } |
| 762 | }); |
| 763 | return this.frm.call({ |
| 764 | method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.remove_pricing_rules", |
| 765 | args: { item_list: item_list }, |
| 766 | callback: function(r) { |
| 767 | if (!r.exc && r.message) { |
| 768 | me._set_values_for_item_list(r.message); |
| 769 | me.calculate_taxes_and_totals(); |
| 770 | if(me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on") |
| 771 | } |
| 772 | } |
| 773 | }); |
| 774 | } else { |
| 775 | this.apply_pricing_rule(); |
| 776 | } |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 777 | }, |
| 778 | |
| 779 | apply_pricing_rule: function(item, calculate_taxes_and_totals) { |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 780 | var me = this; |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 781 | var args = this._get_args(item); |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 782 | if (!(args.items && args.items.length)) { |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 783 | if(calculate_taxes_and_totals) me.calculate_taxes_and_totals(); |
| 784 | return; |
| 785 | } |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 786 | return this.frm.call({ |
| 787 | method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule", |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 788 | args: { args: args }, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 789 | callback: function(r) { |
Anand Doshi | f214bfc | 2014-07-16 17:52:08 +0530 | [diff] [blame] | 790 | if (!r.exc && r.message) { |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 791 | me._set_values_for_item_list(r.message); |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 792 | if(item) me.set_gross_profit(item); |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 793 | if(calculate_taxes_and_totals) me.calculate_taxes_and_totals(); |
Rohit Waghchaure | deaee7c | 2016-09-22 14:02:53 +0530 | [diff] [blame] | 794 | if(me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on") |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | }); |
| 798 | }, |
| 799 | |
| 800 | _get_args: function(item) { |
| 801 | var me = this; |
| 802 | return { |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 803 | "items": this._get_item_list(item), |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 804 | "customer": me.frm.doc.customer, |
| 805 | "customer_group": me.frm.doc.customer_group, |
| 806 | "territory": me.frm.doc.territory, |
| 807 | "supplier": me.frm.doc.supplier, |
| 808 | "supplier_type": me.frm.doc.supplier_type, |
| 809 | "currency": me.frm.doc.currency, |
| 810 | "conversion_rate": me.frm.doc.conversion_rate, |
| 811 | "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list, |
Nabin Hait | dd82bf2 | 2015-05-11 16:49:17 +0530 | [diff] [blame] | 812 | "price_list_currency": me.frm.doc.price_list_currency, |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 813 | "plc_conversion_rate": me.frm.doc.plc_conversion_rate, |
| 814 | "company": me.frm.doc.company, |
| 815 | "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date, |
| 816 | "campaign": me.frm.doc.campaign, |
| 817 | "sales_partner": me.frm.doc.sales_partner, |
| 818 | "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 819 | "doctype": me.frm.doc.doctype, |
Rohit Waghchaure | 5ecbaa4 | 2017-03-28 12:09:07 +0530 | [diff] [blame] | 820 | "name": me.frm.doc.name, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 821 | "is_return": cint(me.frm.doc.is_return), |
| 822 | "update_stock": in_list(['Sales Invoice', 'Purchase Invoice'], me.frm.doc.doctype) ? cint(me.frm.doc.update_stock) : 0, |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 823 | "conversion_factor": me.frm.doc.conversion_factor |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 824 | }; |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 825 | }, |
| 826 | |
| 827 | _get_item_list: function(item) { |
| 828 | var item_list = []; |
| 829 | var append_item = function(d) { |
Anand Doshi | 39edcbc | 2014-07-28 16:00:19 +0530 | [diff] [blame] | 830 | if (d.item_code) { |
| 831 | item_list.push({ |
| 832 | "doctype": d.doctype, |
| 833 | "name": d.name, |
| 834 | "item_code": d.item_code, |
| 835 | "item_group": d.item_group, |
| 836 | "brand": d.brand, |
Anand Doshi | 39a2891 | 2016-02-15 11:42:18 +0530 | [diff] [blame] | 837 | "qty": d.qty, |
| 838 | "parenttype": d.parenttype, |
Rohit Waghchaure | 43b5080 | 2016-10-24 14:04:46 +0530 | [diff] [blame] | 839 | "parent": d.parent, |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 840 | "pricing_rule": d.pricing_rule, |
| 841 | "warehouse": d.warehouse, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 842 | "serial_no": d.serial_no, |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 843 | "conversion_factor": d.conversion_factor || 1.0 |
Anand Doshi | 39edcbc | 2014-07-28 16:00:19 +0530 | [diff] [blame] | 844 | }); |
mbauskar | c79415e | 2016-01-18 16:32:24 +0530 | [diff] [blame] | 845 | |
| 846 | // if doctype is Quotation Item / Sales Order Iten then add Margin Type and rate in item_list |
mbauskar | a52472c | 2016-03-05 15:10:25 +0530 | [diff] [blame] | 847 | if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){ |
| 848 | item_list[0]["margin_type"] = d.margin_type |
| 849 | item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 850 | } |
Anand Doshi | 39edcbc | 2014-07-28 16:00:19 +0530 | [diff] [blame] | 851 | } |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 852 | }; |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 853 | |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 854 | if (item) { |
| 855 | append_item(item); |
Nabin Hait | c1367d9 | 2014-05-30 11:43:00 +0530 | [diff] [blame] | 856 | } else { |
Nabin Hait | 9633934 | 2015-01-21 17:22:11 +0530 | [diff] [blame] | 857 | $.each(this.frm.doc["items"] || [], function(i, d) { |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 858 | append_item(d); |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 859 | }); |
| 860 | } |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 861 | return item_list; |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 862 | }, |
| 863 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 864 | _set_values_for_item_list: function(children) { |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 865 | var me = this; |
Nabin Hait | 7d8fa80 | 2014-12-30 15:35:45 +0530 | [diff] [blame] | 866 | var price_list_rate_changed = false; |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 867 | for(var i=0, l=children.length; i<l; i++) { |
| 868 | var d = children[i]; |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 869 | var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule"); |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 870 | for(var k in d) { |
| 871 | var v = d[k]; |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 872 | if (["doctype", "name"].indexOf(k)===-1) { |
Nabin Hait | 7d8fa80 | 2014-12-30 15:35:45 +0530 | [diff] [blame] | 873 | if(k=="price_list_rate") { |
| 874 | if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true; |
| 875 | } |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 876 | frappe.model.set_value(d.doctype, d.name, k, v); |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 877 | } |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 878 | } |
| 879 | |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 880 | // if pricing rule set as blank from an existing value, apply price_list |
Nabin Hait | cfe3c54 | 2014-12-22 10:42:48 +0530 | [diff] [blame] | 881 | if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) { |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 882 | me.apply_price_list(frappe.get_doc(d.doctype, d.name)); |
| 883 | } |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 884 | } |
Nabin Hait | 7d8fa80 | 2014-12-30 15:35:45 +0530 | [diff] [blame] | 885 | |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 886 | if(!price_list_rate_changed) me.calculate_taxes_and_totals(); |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 887 | }, |
| 888 | |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 889 | apply_price_list: function(item) { |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 890 | var me = this; |
Rushabh Mehta | 052bd62 | 2015-01-30 16:57:24 +0530 | [diff] [blame] | 891 | var args = this._get_args(item); |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 892 | if (!((args.items && args.items.length) || args.price_list)) { |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 893 | return; |
| 894 | } |
Nabin Hait | 37b047d | 2015-02-23 16:01:33 +0530 | [diff] [blame] | 895 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 896 | return this.frm.call({ |
| 897 | method: "erpnext.stock.get_item_details.apply_price_list", |
Rushabh Mehta | 052bd62 | 2015-01-30 16:57:24 +0530 | [diff] [blame] | 898 | args: { args: args }, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 899 | callback: function(r) { |
| 900 | if (!r.exc) { |
Anand Doshi | 51f722d | 2014-07-04 15:44:26 +0530 | [diff] [blame] | 901 | me.in_apply_price_list = true; |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 902 | me.frm.set_value("price_list_currency", r.message.parent.price_list_currency); |
| 903 | me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate); |
Anand Doshi | 51f722d | 2014-07-04 15:44:26 +0530 | [diff] [blame] | 904 | me.in_apply_price_list = false; |
Nabin Hait | 37b047d | 2015-02-23 16:01:33 +0530 | [diff] [blame] | 905 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 906 | if(args.items.length) { |
Nabin Hait | 37b047d | 2015-02-23 16:01:33 +0530 | [diff] [blame] | 907 | me._set_values_for_item_list(r.message.children); |
| 908 | } |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | }); |
| 912 | }, |
| 913 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 914 | get_item_wise_taxes_html: function() { |
| 915 | var item_tax = {}; |
| 916 | var tax_accounts = []; |
| 917 | var company_currency = this.get_company_currency(); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 918 | |
Nabin Hait | 9633934 | 2015-01-21 17:22:11 +0530 | [diff] [blame] | 919 | $.each(this.frm.doc["taxes"] || [], function(i, tax) { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 920 | var tax_amount_precision = precision("tax_amount", tax); |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 921 | var tax_rate_precision = precision("rate", tax); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 922 | $.each(JSON.parse(tax.item_wise_tax_detail || '{}'), |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 923 | function(item_code, tax_data) { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 924 | if(!item_tax[item_code]) item_tax[item_code] = {}; |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 925 | if($.isArray(tax_data)) { |
Anand Doshi | 1e4fb68 | 2013-08-23 12:56:33 +0530 | [diff] [blame] | 926 | var tax_rate = ""; |
| 927 | if(tax_data[0] != null) { |
| 928 | tax_rate = (tax.charge_type === "Actual") ? |
| 929 | format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) : |
| 930 | (flt(tax_data[0], tax_rate_precision) + "%"); |
| 931 | } |
| 932 | var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency, |
| 933 | tax_amount_precision); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 934 | |
Anand Doshi | aa71b5b | 2013-08-05 13:00:53 +0530 | [diff] [blame] | 935 | item_tax[item_code][tax.name] = [tax_rate, tax_amount]; |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 936 | } else { |
Anand Doshi | aa71b5b | 2013-08-05 13:00:53 +0530 | [diff] [blame] | 937 | item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""]; |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 938 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 939 | }); |
Anand Doshi | aa71b5b | 2013-08-05 13:00:53 +0530 | [diff] [blame] | 940 | tax_accounts.push([tax.name, tax.account_head]); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 941 | }); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 942 | |
| 943 | var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })), |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 944 | function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n"); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 945 | |
Anand Doshi | 179e377 | 2013-09-05 16:53:57 +0530 | [diff] [blame] | 946 | var distinct_item_names = []; |
| 947 | var distinct_items = []; |
Nabin Hait | 9633934 | 2015-01-21 17:22:11 +0530 | [diff] [blame] | 948 | $.each(this.frm.doc["items"] || [], function(i, item) { |
Anand Doshi | 179e377 | 2013-09-05 16:53:57 +0530 | [diff] [blame] | 949 | if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) { |
| 950 | distinct_item_names.push(item.item_code || item.item_name); |
| 951 | distinct_items.push(item); |
| 952 | } |
| 953 | }); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 954 | |
Anand Doshi | 179e377 | 2013-09-05 16:53:57 +0530 | [diff] [blame] | 955 | var rows = $.map(distinct_items, function(item) { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 956 | var item_tax_record = item_tax[item.item_code || item.item_name]; |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 957 | if(!item_tax_record) { return null; } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 958 | return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", { |
| 959 | item_name: item.item_name, |
| 960 | taxes: $.map(tax_accounts, function(head) { |
Anand Doshi | aa71b5b | 2013-08-05 13:00:53 +0530 | [diff] [blame] | 961 | return item_tax_record[head[0]] ? |
| 962 | "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" : |
Anand Doshi | 64f316b | 2013-07-17 15:12:22 +0530 | [diff] [blame] | 963 | "<td></td>"; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 964 | }).join("\n") |
| 965 | }); |
| 966 | }).join("\n"); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 967 | |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 968 | if(!rows) return ""; |
Rushabh Mehta | f24f604 | 2015-08-20 18:01:40 +0530 | [diff] [blame] | 969 | return '<p><a class="h6 text-muted" href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">' |
Rushabh Mehta | f340e19 | 2017-03-15 15:12:43 +0530 | [diff] [blame] | 970 | + __("Show tax break-up") + '</a></p>\ |
Rushabh Mehta | 7d8b189 | 2013-11-08 16:27:18 +0530 | [diff] [blame] | 971 | <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\ |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 972 | <thead><tr>' + headings + '</tr></thead> \ |
| 973 | <tbody>' + rows + '</tbody> \ |
| 974 | </table></div>'; |
| 975 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 976 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 977 | validate_company_and_party: function() { |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 978 | var me = this; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 979 | var valid = true; |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 980 | |
Nabin Hait | 0f23106 | 2014-02-20 11:27:47 +0530 | [diff] [blame] | 981 | $.each(["company", "customer"], function(i, fieldname) { |
Saurabh | 8a8ef85 | 2015-10-21 19:28:53 +0530 | [diff] [blame] | 982 | if(frappe.meta.has_field(me.frm.doc.doctype, fieldname && me.frm.doc.doctype != "Purchase Order")) { |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 983 | if (!me.frm.doc[fieldname]) { |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 984 | msgprint(__("Please specify") + ": " + |
| 985 | frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) + |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 986 | ". " + __("It is needed to fetch Item Details.")); |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 987 | valid = false; |
| 988 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 989 | } |
| 990 | }); |
| 991 | return valid; |
| 992 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 993 | |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 994 | get_terms: function() { |
| 995 | var me = this; |
| 996 | if(this.frm.doc.tc_name) { |
Kanchan Chauhan | b642625 | 2016-12-12 17:22:27 +0530 | [diff] [blame] | 997 | return frappe.call({ |
| 998 | method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions', |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 999 | args: { |
Kanchan Chauhan | b642625 | 2016-12-12 17:22:27 +0530 | [diff] [blame] | 1000 | template_name: this.frm.doc.tc_name, |
| 1001 | doc: this.frm.doc |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 1002 | }, |
Kanchan Chauhan | b642625 | 2016-12-12 17:22:27 +0530 | [diff] [blame] | 1003 | callback: function(r) { |
| 1004 | if(!r.exc) { |
| 1005 | me.frm.set_value("terms", r.message); |
| 1006 | } |
| 1007 | } |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 1008 | }); |
| 1009 | } |
| 1010 | }, |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1011 | |
| 1012 | taxes_and_charges: function() { |
| 1013 | var me = this; |
| 1014 | if(this.frm.doc.taxes_and_charges) { |
| 1015 | return this.frm.call({ |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 1016 | method: "erpnext.controllers.accounts_controller.get_taxes_and_charges", |
| 1017 | args: { |
Nabin Hait | 03f3a8a | 2014-05-13 16:38:31 +0530 | [diff] [blame] | 1018 | "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges", |
| 1019 | this.frm.doc.name).options, |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 1020 | "master_name": this.frm.doc.taxes_and_charges, |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 1021 | }, |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1022 | callback: function(r) { |
| 1023 | if(!r.exc) { |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 1024 | me.frm.set_value("taxes", r.message); |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1025 | me.calculate_taxes_and_totals(); |
| 1026 | } |
| 1027 | } |
| 1028 | }); |
Nabin Hait | bc83b9c | 2014-05-02 17:03:18 +0530 | [diff] [blame] | 1029 | } |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1030 | }, |
| 1031 | |
| 1032 | show_item_wise_taxes: function() { |
| 1033 | if(this.frm.fields_dict.other_charges_calculation) { |
Anand Doshi | dd942bb | 2015-02-04 18:52:35 +0530 | [diff] [blame] | 1034 | var html = this.get_item_wise_taxes_html(); |
| 1035 | if (html) { |
| 1036 | this.frm.toggle_display("other_charges_calculation", true); |
| 1037 | $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html); |
| 1038 | } else { |
| 1039 | this.frm.toggle_display("other_charges_calculation", false); |
| 1040 | } |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1041 | } |
| 1042 | }, |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 1043 | |
| 1044 | is_recurring: function() { |
| 1045 | // set default values for recurring documents |
Rushabh Mehta | 756e518 | 2016-03-03 13:00:17 +0530 | [diff] [blame] | 1046 | if(this.frm.doc.is_recurring && this.frm.doc.__islocal) { |
| 1047 | frappe.msgprint(__("Please set recurring after saving")); |
| 1048 | this.frm.set_value('is_recurring', 0); |
| 1049 | return; |
| 1050 | } |
| 1051 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 1052 | if(this.frm.doc.is_recurring) { |
Nabin Hait | 68a628e | 2016-03-21 12:03:16 +0530 | [diff] [blame] | 1053 | if(!this.frm.doc.recurring_id) { |
| 1054 | this.frm.set_value('recurring_id', this.frm.doc.name); |
| 1055 | } |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 1056 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 1057 | var owner_email = this.frm.doc.owner=="Administrator" |
| 1058 | ? frappe.user_info("Administrator").email |
| 1059 | : this.frm.doc.owner; |
| 1060 | |
| 1061 | this.frm.doc.notification_email_address = $.map([cstr(owner_email), |
| 1062 | cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", "); |
| 1063 | this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate(); |
| 1064 | } |
| 1065 | |
| 1066 | refresh_many(["notification_email_address", "repeat_on_day_of_month"]); |
| 1067 | }, |
| 1068 | |
| 1069 | from_date: function() { |
| 1070 | // set to_date |
| 1071 | if(this.frm.doc.from_date) { |
| 1072 | var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6, |
| 1073 | 'Yearly': 12}; |
| 1074 | |
| 1075 | var months = recurring_type_map[this.frm.doc.recurring_type]; |
| 1076 | if(months) { |
| 1077 | var to_date = frappe.datetime.add_months(this.frm.doc.from_date, |
| 1078 | months); |
| 1079 | this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1); |
| 1080 | refresh_field('to_date'); |
| 1081 | } |
| 1082 | } |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 1083 | }, |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 1084 | |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 1085 | set_gross_profit: function(item) { |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 1086 | if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) { |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 1087 | rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1); |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 1088 | item.gross_profit = flt(((rate - item.valuation_rate) * item.stock_qty), precision("amount", item)); |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 1089 | } |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 1090 | }, |
| 1091 | |
| 1092 | setup_item_selector: function() { |
Rushabh Mehta | bc92824 | 2016-05-29 23:08:22 +0530 | [diff] [blame] | 1093 | // TODO: remove item selector |
| 1094 | |
| 1095 | return; |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 1096 | if(!this.item_selector) { |
| 1097 | this.item_selector = new erpnext.ItemSelector({frm: this.frm}); |
| 1098 | } |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 1099 | }, |
Rushabh Mehta | 76b1341 | 2016-07-18 14:30:18 +0530 | [diff] [blame] | 1100 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 1101 | get_advances: function() { |
| 1102 | if(!this.frm.is_return) { |
| 1103 | return this.frm.call({ |
| 1104 | method: "set_advances", |
| 1105 | doc: this.frm.doc, |
| 1106 | callback: function(r, rt) { |
| 1107 | refresh_field("advances"); |
| 1108 | } |
| 1109 | }) |
| 1110 | } |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1111 | }, |
Rushabh Mehta | 76b1341 | 2016-07-18 14:30:18 +0530 | [diff] [blame] | 1112 | |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1113 | make_payment_entry: function() { |
| 1114 | return frappe.call({ |
Rohit Waghchaure | b7a5502 | 2016-10-05 16:24:41 +0530 | [diff] [blame] | 1115 | method: cur_frm.cscript.get_method_for_payment(), |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1116 | args: { |
| 1117 | "dt": cur_frm.doc.doctype, |
| 1118 | "dn": cur_frm.doc.name |
| 1119 | }, |
| 1120 | callback: function(r) { |
| 1121 | var doclist = frappe.model.sync(r.message); |
| 1122 | frappe.set_route("Form", doclist[0].doctype, doclist[0].name); |
Nabin Hait | 56548cb | 2016-07-01 15:58:39 +0530 | [diff] [blame] | 1123 | // cur_frm.refresh_fields() |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1124 | } |
| 1125 | }); |
Rohit Waghchaure | b7a5502 | 2016-10-05 16:24:41 +0530 | [diff] [blame] | 1126 | }, |
| 1127 | |
| 1128 | get_method_for_payment: function(){ |
| 1129 | method = "erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry" |
Rohit Waghchaure | 7d52989 | 2016-10-06 14:35:04 +0530 | [diff] [blame] | 1130 | if(cur_frm.doc.__onload && cur_frm.doc.__onload.make_payment_via_journal_entry){ |
Rohit Waghchaure | b7a5502 | 2016-10-05 16:24:41 +0530 | [diff] [blame] | 1131 | if(in_list(['Sales Invoice', 'Purchase Invoice'], cur_frm.doc.doctype)){ |
| 1132 | method = "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_invoice" |
| 1133 | }else { |
| 1134 | method= "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_order" |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | return method |
Manas Solanki | cc90241 | 2016-11-10 19:15:11 +0530 | [diff] [blame] | 1139 | }, |
Nabin Hait | 6e74079 | 2017-05-06 12:45:16 +0530 | [diff] [blame] | 1140 | }); |