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