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 | b66edd1 | 2017-03-30 21:21:56 +0530 | [diff] [blame] | 100 | this.frm.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 | } |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 137 | |
Nabin Hait | 8af429d | 2016-11-16 17:21:59 +0530 | [diff] [blame] | 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; |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 146 | 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] | 147 | ? "Incoming" : "Outgoing"; |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 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 | } |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +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, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 263 | 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] | 264 | conversion_rate: me.frm.doc.conversion_rate, |
| 265 | price_list: me.frm.doc.selling_price_list || |
| 266 | me.frm.doc.buying_price_list, |
| 267 | price_list_currency: me.frm.doc.price_list_currency, |
| 268 | plc_conversion_rate: me.frm.doc.plc_conversion_rate, |
| 269 | company: me.frm.doc.company, |
| 270 | order_type: me.frm.doc.order_type, |
| 271 | is_pos: cint(me.frm.doc.is_pos), |
| 272 | is_subcontracted: me.frm.doc.is_subcontracted, |
Nabin Hait | a1bf43b | 2015-03-17 10:50:47 +0530 | [diff] [blame] | 273 | transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date, |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 274 | ignore_pricing_rule: me.frm.doc.ignore_pricing_rule, |
Anand Doshi | afe85bd | 2016-01-21 21:04:16 +0530 | [diff] [blame] | 275 | doctype: me.frm.doc.doctype, |
| 276 | name: me.frm.doc.name, |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 277 | project: item.project || me.frm.doc.project, |
mbauskar | ba41242 | 2017-04-19 12:35:51 +0530 | [diff] [blame] | 278 | qty: item.qty || 1, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 279 | stock_qty: item.stock_qty, |
| 280 | conversion_factor: item.conversion_factor |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 281 | } |
| 282 | }, |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 283 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 284 | callback: function(r) { |
| 285 | if(!r.exc) { |
| 286 | me.frm.script_manager.trigger("price_list_rate", cdt, cdn); |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 287 | me.toggle_conversion_factor(item); |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | }); |
| 291 | } |
| 292 | } |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 293 | }, |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 294 | |
| 295 | serial_no: function(doc, cdt, cdn) { |
| 296 | var me = this; |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 297 | var item = frappe.get_doc(cdt, cdn); |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 298 | |
| 299 | if (item.serial_no) { |
| 300 | if (!item.item_code) { |
Rushabh Mehta | b66edd1 | 2017-03-30 21:21:56 +0530 | [diff] [blame] | 301 | this.frm.trigger("item_code", cdt, cdn); |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 302 | } |
| 303 | else { |
| 304 | var sr_no = []; |
| 305 | |
| 306 | // Replacing all occurences of comma with carriage return |
| 307 | var serial_nos = item.serial_no.trim().replace(/,/g, '\n'); |
| 308 | |
| 309 | serial_nos = serial_nos.trim().split('\n'); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 310 | |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 311 | // Trim each string and push unique string to new list |
| 312 | for (var x=0; x<=serial_nos.length - 1; x++) { |
| 313 | if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) { |
| 314 | sr_no.push(serial_nos[x].trim()); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // Add the new list to the serial no. field in grid with each in new line |
| 319 | item.serial_no = ""; |
| 320 | for (var x=0; x<=sr_no.length - 1; x++) |
| 321 | item.serial_no += sr_no[x] + '\n'; |
| 322 | |
| 323 | refresh_field("serial_no", item.name, item.parentfield); |
Nabin Hait | b7c0c55 | 2015-08-04 12:18:12 +0530 | [diff] [blame] | 324 | if(!doc.is_return) { |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 325 | frappe.model.set_value(item.doctype, item.name, |
Nabin Hait | e68f32c | 2017-03-03 15:00:41 +0530 | [diff] [blame] | 326 | "qty", sr_no.length / item.conversion_factor); |
| 327 | 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] | 328 | } |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 332 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 333 | validate: function() { |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 334 | this.calculate_taxes_and_totals(false); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 335 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 336 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 337 | company: function() { |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 338 | var me = this; |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 339 | var set_pricing = function() { |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 340 | if(me.frm.doc.company && me.frm.fields_dict.currency) { |
| 341 | var company_currency = me.get_company_currency(); |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 342 | var company_doc = frappe.get_doc(":Company", me.frm.doc.company); |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 343 | if (!me.frm.doc.currency) { |
| 344 | me.frm.set_value("currency", company_currency); |
| 345 | } |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 346 | |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 347 | if (me.frm.doc.currency == company_currency) { |
| 348 | me.frm.set_value("conversion_rate", 1.0); |
| 349 | } |
| 350 | if (me.frm.doc.price_list_currency == company_currency) { |
| 351 | me.frm.set_value('plc_conversion_rate', 1.0); |
| 352 | } |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 353 | if (company_doc.default_letter_head) { |
Rushabh Mehta | c185734 | 2015-05-27 12:29:57 +0530 | [diff] [blame] | 354 | if(me.frm.fields_dict.letter_head) { |
| 355 | me.frm.set_value("letter_head", company_doc.default_letter_head); |
| 356 | } |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 357 | } |
Rohit Waghchaure | 585448b | 2016-12-30 15:41:55 +0530 | [diff] [blame] | 358 | 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] | 359 | me.frm.set_value("tc_name", company_doc.default_terms); |
| 360 | } |
Akhilesh Darjee | 1797cfe | 2013-10-11 13:34:10 +0530 | [diff] [blame] | 361 | |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 362 | me.frm.script_manager.trigger("currency"); |
| 363 | me.apply_pricing_rule(); |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 364 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 365 | } |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 366 | |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 367 | var set_party_account = function(set_pricing) { |
| 368 | if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) { |
| 369 | if(me.frm.doc.doctype=="Sales Invoice") { |
| 370 | var party_type = "Customer"; |
| 371 | var party_account_field = 'debit_to'; |
| 372 | } else { |
| 373 | var party_type = "Supplier"; |
| 374 | var party_account_field = 'credit_to'; |
| 375 | } |
Anand Doshi | 39a2891 | 2016-02-15 11:42:18 +0530 | [diff] [blame] | 376 | |
Nabin Hait | ef86440 | 2016-02-10 14:47:25 +0530 | [diff] [blame] | 377 | var party = me.frm.doc[frappe.model.scrub(party_type)]; |
| 378 | if(party) { |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 379 | return frappe.call({ |
| 380 | method: "erpnext.accounts.party.get_party_account", |
| 381 | args: { |
| 382 | company: me.frm.doc.company, |
| 383 | party_type: party_type, |
Nabin Hait | ef86440 | 2016-02-10 14:47:25 +0530 | [diff] [blame] | 384 | party: party |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 385 | }, |
| 386 | callback: function(r) { |
| 387 | if(!r.exc && r.message) { |
| 388 | me.frm.set_value(party_account_field, r.message); |
| 389 | set_pricing(); |
| 390 | } |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 391 | } |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 392 | }); |
Nabin Hait | ef86440 | 2016-02-10 14:47:25 +0530 | [diff] [blame] | 393 | } else { |
| 394 | set_pricing(); |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 395 | } |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 396 | } else { |
| 397 | set_pricing(); |
| 398 | } |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 399 | |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 400 | } |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 401 | |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 402 | if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date; |
| 403 | else var date = this.frm.doc.transaction_date; |
Rushabh Mehta | 5dfe20c | 2016-04-26 12:59:08 +0530 | [diff] [blame] | 404 | |
| 405 | if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") && |
Rohit Waghchaure | b2e7a1f | 2016-04-11 12:10:20 +0530 | [diff] [blame] | 406 | in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)){ |
| 407 | erpnext.utils.get_shipping_address(this.frm, function(){ |
| 408 | set_party_account(set_pricing); |
| 409 | }) |
| 410 | }else{ |
| 411 | set_party_account(set_pricing); |
| 412 | } |
Rushabh Mehta | 45ca8a4 | 2015-04-28 16:02:09 +0530 | [diff] [blame] | 413 | |
| 414 | if(this.frm.doc.company) { |
| 415 | erpnext.last_selected_company = this.frm.doc.company; |
| 416 | } |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 417 | }, |
| 418 | |
| 419 | transaction_date: function() { |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 420 | if (this.frm.doc.transaction_date) { |
| 421 | this.frm.transaction_date = this.frm.doc.transaction_date; |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 422 | frappe.ui.form.trigger(this.frm.doc.doctype, "currency"); |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 423 | } |
Neil Trini Lasrado | 78fa695 | 2014-10-03 17:43:02 +0530 | [diff] [blame] | 424 | }, |
| 425 | |
| 426 | posting_date: function() { |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 427 | var me = this; |
| 428 | if (this.frm.doc.posting_date) { |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 429 | this.frm.posting_date = this.frm.doc.posting_date; |
| 430 | |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 431 | if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) || |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 432 | (this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) { |
| 433 | return frappe.call({ |
| 434 | method: "erpnext.accounts.party.get_due_date", |
| 435 | args: { |
| 436 | "posting_date": me.frm.doc.posting_date, |
| 437 | "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier", |
| 438 | "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier, |
| 439 | "company": me.frm.doc.company |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 440 | }, |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 441 | callback: function(r, rt) { |
| 442 | if(r.message) { |
| 443 | me.frm.set_value("due_date", r.message); |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 444 | frappe.ui.form.trigger(me.frm.doc.doctype, "currency"); |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 445 | } |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 446 | } |
| 447 | }) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 448 | } else { |
| 449 | frappe.ui.form.trigger(me.frm.doc.doctype, "currency"); |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 450 | } |
| 451 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 452 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 453 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 454 | get_company_currency: function() { |
| 455 | return erpnext.get_currency(this.frm.doc.company); |
| 456 | }, |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 457 | |
Neil Trini Lasrado | 09a66c4 | 2015-07-07 16:41:37 +0530 | [diff] [blame] | 458 | contact_person: function() { |
| 459 | erpnext.utils.get_contact_details(this.frm); |
| 460 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 461 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 462 | currency: function() { |
Chude Osiegbu | 256ec17 | 2016-09-19 03:53:37 +0100 | [diff] [blame] | 463 | /* 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] | 464 | 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] | 465 | /* end manqala */ |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 466 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 467 | var me = this; |
| 468 | this.set_dynamic_labels(); |
Akhilesh Darjee | 7837827 | 2013-10-11 19:06:30 +0530 | [diff] [blame] | 469 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 470 | var company_currency = this.get_company_currency(); |
Nabin Hait | 69cdf59 | 2015-05-02 18:46:10 +0530 | [diff] [blame] | 471 | // 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] | 472 | if(this.frm.doc.currency && this.frm.doc.currency !== company_currency |
Nabin Hait | 3950507 | 2016-07-07 11:11:32 +0530 | [diff] [blame] | 473 | && !this.frm.doc.ignore_pricing_rule) { |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 474 | this.get_exchange_rate(transaction_date, this.frm.doc.currency, company_currency, |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 475 | function(exchange_rate) { |
Akhilesh Darjee | 7837827 | 2013-10-11 19:06:30 +0530 | [diff] [blame] | 476 | me.frm.set_value("conversion_rate", exchange_rate); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 477 | }); |
| 478 | } else { |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 479 | this.conversion_rate(); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 480 | } |
| 481 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 482 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 483 | conversion_rate: function() { |
Anand Doshi | 535d833 | 2013-07-19 13:51:07 +0530 | [diff] [blame] | 484 | if(this.frm.doc.currency === this.get_company_currency()) { |
| 485 | this.frm.set_value("conversion_rate", 1.0); |
Akhilesh Darjee | acf65a7 | 2013-10-17 13:04:26 +0530 | [diff] [blame] | 486 | } |
| 487 | if(this.frm.doc.currency === this.frm.doc.price_list_currency && |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 488 | this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) { |
| 489 | this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate); |
| 490 | } |
Rushabh Mehta | 4a3c805 | 2015-05-19 11:10:12 +0530 | [diff] [blame] | 491 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 492 | if(flt(this.frm.doc.conversion_rate)>0.0) { |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 493 | if(this.frm.doc.ignore_pricing_rule) { |
| 494 | this.calculate_taxes_and_totals(); |
Anand Doshi | 51f722d | 2014-07-04 15:44:26 +0530 | [diff] [blame] | 495 | } else if (!this.in_apply_price_list){ |
| 496 | this.apply_price_list(); |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 497 | } |
| 498 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 499 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 500 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 501 | |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 502 | get_exchange_rate: function(transaction_date, from_currency, to_currency, callback) { |
Nabin Hait | 17179ee | 2017-05-06 13:23:12 +0530 | [diff] [blame] | 503 | if (!transaction_date || !from_currency || !to_currency) return; |
Nabin Hait | bdfb070 | 2015-10-28 15:38:08 +0530 | [diff] [blame] | 504 | return frappe.call({ |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 505 | method: "erpnext.setup.utils.get_exchange_rate", |
| 506 | args: { |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 507 | transaction_date: transaction_date, |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 508 | from_currency: from_currency, |
| 509 | to_currency: to_currency |
| 510 | }, |
| 511 | callback: function(r) { |
| 512 | callback(flt(r.message)); |
| 513 | } |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 514 | }); |
| 515 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 516 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 517 | price_list_currency: function() { |
Akhilesh Darjee | db59ffb | 2013-09-11 13:05:24 +0530 | [diff] [blame] | 518 | var me=this; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 519 | this.set_dynamic_labels(); |
Nabin Hait | ec361ea | 2015-05-11 18:14:45 +0530 | [diff] [blame] | 520 | |
| 521 | var company_currency = this.get_company_currency(); |
| 522 | // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc |
| 523 | 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] | 524 | 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] | 525 | function(exchange_rate) { |
| 526 | me.frm.set_value("plc_conversion_rate", exchange_rate); |
| 527 | }); |
| 528 | } else { |
| 529 | this.plc_conversion_rate(); |
| 530 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 531 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 532 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 533 | plc_conversion_rate: function() { |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 534 | if(this.frm.doc.price_list_currency === this.get_company_currency()) { |
| 535 | this.frm.set_value("plc_conversion_rate", 1.0); |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 536 | } else if(this.frm.doc.price_list_currency === this.frm.doc.currency |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 537 | && 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] | 538 | cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) { |
| 539 | this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate); |
Akhilesh Darjee | 564c621 | 2013-10-17 11:31:11 +0530 | [diff] [blame] | 540 | } |
Rushabh Mehta | 4a3c805 | 2015-05-19 11:10:12 +0530 | [diff] [blame] | 541 | |
Nabin Hait | ec361ea | 2015-05-11 18:14:45 +0530 | [diff] [blame] | 542 | if(!this.in_apply_price_list) { |
| 543 | this.apply_price_list(); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 544 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 545 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 546 | |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 547 | uom: function(doc, cdt, cdn) { |
| 548 | var me = this; |
| 549 | var item = frappe.get_doc(cdt, cdn); |
| 550 | if(item.item_code && item.uom) { |
| 551 | return this.frm.call({ |
| 552 | method: "erpnext.stock.get_item_details.get_conversion_factor", |
| 553 | child: item, |
| 554 | args: { |
| 555 | item_code: item.item_code, |
| 556 | uom: item.uom |
| 557 | }, |
| 558 | callback: function(r) { |
| 559 | if(!r.exc) { |
| 560 | me.conversion_factor(me.frm.doc, cdt, cdn); |
| 561 | } |
| 562 | } |
| 563 | }); |
| 564 | } |
| 565 | }, |
| 566 | |
Nabin Hait | 3257aee | 2017-05-06 12:47:14 +0530 | [diff] [blame] | 567 | conversion_factor: function(doc, cdt, cdn, dont_fetch_price_list_rate) { |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 568 | if(frappe.meta.get_docfield(cdt, "stock_qty", cdn)) { |
| 569 | var item = frappe.get_doc(cdt, cdn); |
| 570 | frappe.model.round_floats_in(item, ["qty", "conversion_factor"]); |
| 571 | item.stock_qty = flt(item.qty * item.conversion_factor, precision("stock_qty", item)); |
| 572 | refresh_field("stock_qty", item.name, item.parentfield); |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 573 | this.toggle_conversion_factor(item); |
Nabin Hait | 3257aee | 2017-05-06 12:47:14 +0530 | [diff] [blame] | 574 | if(!dont_fetch_price_list_rate) this.apply_price_list(item, true); |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 575 | } |
| 576 | }, |
| 577 | |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 578 | toggle_conversion_factor: function(item) { |
| 579 | // toggle read only property for conversion factor field if the uom and stock uom are same |
| 580 | this.frm.fields_dict.items.grid.toggle_enable("conversion_factor", |
| 581 | (item.uom != item.stock_uom)? true: false) |
| 582 | }, |
| 583 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 584 | qty: function(doc, cdt, cdn) { |
Nabin Hait | 3257aee | 2017-05-06 12:47:14 +0530 | [diff] [blame] | 585 | this.conversion_factor(doc, cdt, cdn, true); |
| 586 | this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 587 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 588 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 589 | set_dynamic_labels: function() { |
| 590 | // What TODO? should we make price list system non-mandatory? |
| 591 | this.frm.toggle_reqd("plc_conversion_rate", |
| 592 | !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency)); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 593 | |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 594 | if(this.frm.doc_currency!==this.frm.doc.currency) { |
| 595 | // reset names only when the currency is different |
| 596 | |
| 597 | var company_currency = this.get_company_currency(); |
| 598 | this.change_form_labels(company_currency); |
| 599 | this.change_grid_labels(company_currency); |
| 600 | this.frm.refresh_fields(); |
| 601 | this.frm.doc_currency = this.frm.doc.currency; |
| 602 | } |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 603 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 604 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 605 | change_form_labels: function(company_currency) { |
| 606 | var me = this; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 607 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 608 | 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] | 609 | "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words", |
| 610 | "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] | 611 | "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] | 612 | "base_raw_material_cost", "base_total_cost", "base_scrap_material_cost" |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 613 | ], company_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 614 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 615 | 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] | 616 | "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted", |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 617 | "rounded_total", "in_words", "paid_amount", "write_off_amount", "operating_cost", "scrap_material_cost", |
| 618 | "raw_material_cost", "total_cost"], this.frm.doc.currency); |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 619 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 620 | 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] | 621 | |
| 622 | cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency |
| 623 | + " = [?] " + company_currency) |
| 624 | |
| 625 | if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) { |
| 626 | cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency |
| 627 | + " = [?] " + company_currency) |
| 628 | } |
| 629 | |
| 630 | // toggle fields |
| 631 | this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges", |
| 632 | "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 633 | "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount", |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 634 | "base_paid_amount", "base_write_off_amount", "base_operating_cost", |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 635 | "base_raw_material_cost", "base_total_cost", "base_scrap_material_cost"], |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 636 | this.frm.doc.currency != company_currency); |
| 637 | |
| 638 | this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"], |
| 639 | this.frm.doc.price_list_currency != company_currency); |
| 640 | |
Nabin Hait | 903c42a | 2015-02-24 15:24:49 +0530 | [diff] [blame] | 641 | var show =cint(cur_frm.doc.discount_amount) || |
| 642 | ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length); |
| 643 | |
| 644 | if(frappe.meta.get_docfield(cur_frm.doctype, "net_total")) |
| 645 | cur_frm.toggle_display("net_total", show); |
| 646 | |
| 647 | if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total")) |
| 648 | cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency))); |
| 649 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 650 | }, |
| 651 | |
| 652 | change_grid_labels: function(company_currency) { |
| 653 | var me = this; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 654 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 655 | this.frm.set_currency_labels(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"], |
| 656 | company_currency, "items"); |
| 657 | |
| 658 | this.frm.set_currency_labels(["rate", "net_rate", "price_list_rate", "amount", "net_amount"], |
| 659 | this.frm.doc.currency, "items"); |
| 660 | |
| 661 | if(this.frm.fields_dict["operations"]) { |
| 662 | this.frm.set_currency_labels(["operating_cost", "hour_rate"], this.frm.doc.currency, "operations"); |
| 663 | this.frm.set_currency_labels(["base_operating_cost", "base_hour_rate"], company_currency, "operations"); |
| 664 | |
| 665 | var item_grid = this.frm.fields_dict["operations"].grid; |
| 666 | $.each(["base_operating_cost", "base_hour_rate"], function(i, fname) { |
| 667 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 668 | item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 669 | }); |
| 670 | } |
| 671 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 672 | if(this.frm.fields_dict["scrap_items"]) { |
| 673 | this.frm.set_currency_labels(["rate", "amount"], this.frm.doc.currency, "scrap_items"); |
| 674 | 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] | 675 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 676 | var item_grid = this.frm.fields_dict["scrap_items"].grid; |
| 677 | $.each(["base_rate", "base_amount"], function(i, fname) { |
| 678 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 679 | item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); |
| 680 | }); |
| 681 | } |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 682 | |
| 683 | if(this.frm.fields_dict["taxes"]) { |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 684 | 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] | 685 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 686 | 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] | 687 | } |
| 688 | |
| 689 | if(this.frm.fields_dict["advances"]) { |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 690 | this.frm.set_currency_labels(["advance_amount", "allocated_amount"], |
Nabin Hait | aa01590 | 2015-10-16 13:08:33 +0530 | [diff] [blame] | 691 | this.frm.doc.party_account_currency, "advances"); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // toggle columns |
| 695 | var item_grid = this.frm.fields_dict["items"].grid; |
| 696 | $.each(["base_rate", "base_price_list_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 | |
| 701 | var show = (cint(cur_frm.doc.discount_amount)) || |
| 702 | ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length); |
| 703 | |
| 704 | $.each(["net_rate", "net_amount"], function(i, fname) { |
| 705 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 706 | item_grid.set_column_disp(fname, show); |
| 707 | }); |
| 708 | |
| 709 | $.each(["base_net_rate", "base_net_amount"], function(i, fname) { |
| 710 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 711 | item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency))); |
| 712 | }); |
| 713 | |
| 714 | // set labels |
| 715 | var $wrapper = $(this.frm.wrapper); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 716 | }, |
| 717 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 718 | recalculate: function() { |
| 719 | this.calculate_taxes_and_totals(); |
| 720 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 721 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 722 | recalculate_values: function() { |
| 723 | this.calculate_taxes_and_totals(); |
| 724 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 725 | |
Anand Doshi | d0b0072 | 2013-05-28 18:54:48 +0530 | [diff] [blame] | 726 | calculate_charges: function() { |
| 727 | this.calculate_taxes_and_totals(); |
| 728 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 729 | |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 730 | ignore_pricing_rule: function() { |
Nabin Hait | 8c9fb76 | 2017-04-28 14:23:03 +0530 | [diff] [blame] | 731 | if(this.frm.doc.ignore_pricing_rule) { |
| 732 | var me = this; |
| 733 | var item_list = []; |
| 734 | |
| 735 | $.each(this.frm.doc["items"] || [], function(i, d) { |
| 736 | if (d.item_code) { |
| 737 | item_list.push({ |
| 738 | "doctype": d.doctype, |
| 739 | "name": d.name, |
| 740 | "pricing_rule": d.pricing_rule |
| 741 | }) |
| 742 | } |
| 743 | }); |
| 744 | return this.frm.call({ |
| 745 | method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.remove_pricing_rules", |
| 746 | args: { item_list: item_list }, |
| 747 | callback: function(r) { |
| 748 | if (!r.exc && r.message) { |
| 749 | me._set_values_for_item_list(r.message); |
| 750 | me.calculate_taxes_and_totals(); |
| 751 | if(me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on") |
| 752 | } |
| 753 | } |
| 754 | }); |
| 755 | } else { |
| 756 | this.apply_pricing_rule(); |
| 757 | } |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 758 | }, |
| 759 | |
| 760 | apply_pricing_rule: function(item, calculate_taxes_and_totals) { |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 761 | var me = this; |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 762 | var args = this._get_args(item); |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 763 | if (!(args.items && args.items.length)) { |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 764 | if(calculate_taxes_and_totals) me.calculate_taxes_and_totals(); |
| 765 | return; |
| 766 | } |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 767 | return this.frm.call({ |
| 768 | method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule", |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 769 | args: { args: args }, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 770 | callback: function(r) { |
Anand Doshi | f214bfc | 2014-07-16 17:52:08 +0530 | [diff] [blame] | 771 | if (!r.exc && r.message) { |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 772 | me._set_values_for_item_list(r.message); |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 773 | if(item) me.set_gross_profit(item); |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 774 | if(calculate_taxes_and_totals) me.calculate_taxes_and_totals(); |
Rohit Waghchaure | deaee7c | 2016-09-22 14:02:53 +0530 | [diff] [blame] | 775 | 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] | 776 | } |
| 777 | } |
| 778 | }); |
| 779 | }, |
| 780 | |
| 781 | _get_args: function(item) { |
| 782 | var me = this; |
| 783 | return { |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 784 | "items": this._get_item_list(item), |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 785 | "customer": me.frm.doc.customer, |
| 786 | "customer_group": me.frm.doc.customer_group, |
| 787 | "territory": me.frm.doc.territory, |
| 788 | "supplier": me.frm.doc.supplier, |
| 789 | "supplier_type": me.frm.doc.supplier_type, |
| 790 | "currency": me.frm.doc.currency, |
| 791 | "conversion_rate": me.frm.doc.conversion_rate, |
| 792 | "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] | 793 | "price_list_currency": me.frm.doc.price_list_currency, |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 794 | "plc_conversion_rate": me.frm.doc.plc_conversion_rate, |
| 795 | "company": me.frm.doc.company, |
| 796 | "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date, |
| 797 | "campaign": me.frm.doc.campaign, |
| 798 | "sales_partner": me.frm.doc.sales_partner, |
| 799 | "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 800 | "doctype": me.frm.doc.doctype, |
Rohit Waghchaure | 5ecbaa4 | 2017-03-28 12:09:07 +0530 | [diff] [blame] | 801 | "name": me.frm.doc.name, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 802 | "is_return": cint(me.frm.doc.is_return), |
| 803 | "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] | 804 | "conversion_factor": me.frm.doc.conversion_factor |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 805 | }; |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 806 | }, |
| 807 | |
| 808 | _get_item_list: function(item) { |
| 809 | var item_list = []; |
| 810 | var append_item = function(d) { |
Anand Doshi | 39edcbc | 2014-07-28 16:00:19 +0530 | [diff] [blame] | 811 | if (d.item_code) { |
| 812 | item_list.push({ |
| 813 | "doctype": d.doctype, |
| 814 | "name": d.name, |
| 815 | "item_code": d.item_code, |
| 816 | "item_group": d.item_group, |
| 817 | "brand": d.brand, |
Anand Doshi | 39a2891 | 2016-02-15 11:42:18 +0530 | [diff] [blame] | 818 | "qty": d.qty, |
| 819 | "parenttype": d.parenttype, |
Rohit Waghchaure | 43b5080 | 2016-10-24 14:04:46 +0530 | [diff] [blame] | 820 | "parent": d.parent, |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 821 | "pricing_rule": d.pricing_rule, |
| 822 | "warehouse": d.warehouse, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 823 | "serial_no": d.serial_no, |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 824 | "conversion_factor": d.conversion_factor || 1.0 |
Anand Doshi | 39edcbc | 2014-07-28 16:00:19 +0530 | [diff] [blame] | 825 | }); |
mbauskar | c79415e | 2016-01-18 16:32:24 +0530 | [diff] [blame] | 826 | |
| 827 | // 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] | 828 | if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){ |
| 829 | item_list[0]["margin_type"] = d.margin_type |
| 830 | item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 831 | } |
Anand Doshi | 39edcbc | 2014-07-28 16:00:19 +0530 | [diff] [blame] | 832 | } |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 833 | }; |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 834 | |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 835 | if (item) { |
| 836 | append_item(item); |
Nabin Hait | c1367d9 | 2014-05-30 11:43:00 +0530 | [diff] [blame] | 837 | } else { |
Nabin Hait | 9633934 | 2015-01-21 17:22:11 +0530 | [diff] [blame] | 838 | $.each(this.frm.doc["items"] || [], function(i, d) { |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 839 | append_item(d); |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 840 | }); |
| 841 | } |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 842 | return item_list; |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 843 | }, |
| 844 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 845 | _set_values_for_item_list: function(children) { |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 846 | var me = this; |
Nabin Hait | 7d8fa80 | 2014-12-30 15:35:45 +0530 | [diff] [blame] | 847 | var price_list_rate_changed = false; |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 848 | for(var i=0, l=children.length; i<l; i++) { |
| 849 | var d = children[i]; |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 850 | 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] | 851 | for(var k in d) { |
| 852 | var v = d[k]; |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 853 | if (["doctype", "name"].indexOf(k)===-1) { |
Nabin Hait | 7d8fa80 | 2014-12-30 15:35:45 +0530 | [diff] [blame] | 854 | if(k=="price_list_rate") { |
| 855 | if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true; |
| 856 | } |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 857 | frappe.model.set_value(d.doctype, d.name, k, v); |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 858 | } |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 859 | } |
| 860 | |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 861 | // 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] | 862 | 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] | 863 | me.apply_price_list(frappe.get_doc(d.doctype, d.name)); |
| 864 | } |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 865 | } |
Nabin Hait | 7d8fa80 | 2014-12-30 15:35:45 +0530 | [diff] [blame] | 866 | |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 867 | if(!price_list_rate_changed) me.calculate_taxes_and_totals(); |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 868 | }, |
| 869 | |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 870 | apply_price_list: function(item) { |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 871 | var me = this; |
Rushabh Mehta | 052bd62 | 2015-01-30 16:57:24 +0530 | [diff] [blame] | 872 | var args = this._get_args(item); |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 873 | if (!((args.items && args.items.length) || args.price_list)) { |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 874 | return; |
| 875 | } |
Nabin Hait | 37b047d | 2015-02-23 16:01:33 +0530 | [diff] [blame] | 876 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 877 | return this.frm.call({ |
| 878 | method: "erpnext.stock.get_item_details.apply_price_list", |
Rushabh Mehta | 052bd62 | 2015-01-30 16:57:24 +0530 | [diff] [blame] | 879 | args: { args: args }, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 880 | callback: function(r) { |
| 881 | if (!r.exc) { |
Anand Doshi | 51f722d | 2014-07-04 15:44:26 +0530 | [diff] [blame] | 882 | me.in_apply_price_list = true; |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 883 | me.frm.set_value("price_list_currency", r.message.parent.price_list_currency); |
| 884 | 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] | 885 | me.in_apply_price_list = false; |
Nabin Hait | 37b047d | 2015-02-23 16:01:33 +0530 | [diff] [blame] | 886 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 887 | if(args.items.length) { |
Nabin Hait | 37b047d | 2015-02-23 16:01:33 +0530 | [diff] [blame] | 888 | me._set_values_for_item_list(r.message.children); |
| 889 | } |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | }); |
| 893 | }, |
| 894 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 895 | get_item_wise_taxes_html: function() { |
| 896 | var item_tax = {}; |
| 897 | var tax_accounts = []; |
| 898 | var company_currency = this.get_company_currency(); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 899 | |
Nabin Hait | 9633934 | 2015-01-21 17:22:11 +0530 | [diff] [blame] | 900 | $.each(this.frm.doc["taxes"] || [], function(i, tax) { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 901 | var tax_amount_precision = precision("tax_amount", tax); |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 902 | var tax_rate_precision = precision("rate", tax); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 903 | $.each(JSON.parse(tax.item_wise_tax_detail || '{}'), |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 904 | function(item_code, tax_data) { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 905 | if(!item_tax[item_code]) item_tax[item_code] = {}; |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 906 | if($.isArray(tax_data)) { |
Anand Doshi | 1e4fb68 | 2013-08-23 12:56:33 +0530 | [diff] [blame] | 907 | var tax_rate = ""; |
| 908 | if(tax_data[0] != null) { |
| 909 | tax_rate = (tax.charge_type === "Actual") ? |
| 910 | format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) : |
| 911 | (flt(tax_data[0], tax_rate_precision) + "%"); |
| 912 | } |
| 913 | var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency, |
| 914 | tax_amount_precision); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 915 | |
Anand Doshi | aa71b5b | 2013-08-05 13:00:53 +0530 | [diff] [blame] | 916 | item_tax[item_code][tax.name] = [tax_rate, tax_amount]; |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 917 | } else { |
Anand Doshi | aa71b5b | 2013-08-05 13:00:53 +0530 | [diff] [blame] | 918 | item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""]; |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 919 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 920 | }); |
Anand Doshi | aa71b5b | 2013-08-05 13:00:53 +0530 | [diff] [blame] | 921 | tax_accounts.push([tax.name, tax.account_head]); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 922 | }); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 923 | |
| 924 | 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] | 925 | function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n"); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 926 | |
Anand Doshi | 179e377 | 2013-09-05 16:53:57 +0530 | [diff] [blame] | 927 | var distinct_item_names = []; |
| 928 | var distinct_items = []; |
Nabin Hait | 9633934 | 2015-01-21 17:22:11 +0530 | [diff] [blame] | 929 | $.each(this.frm.doc["items"] || [], function(i, item) { |
Anand Doshi | 179e377 | 2013-09-05 16:53:57 +0530 | [diff] [blame] | 930 | if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) { |
| 931 | distinct_item_names.push(item.item_code || item.item_name); |
| 932 | distinct_items.push(item); |
| 933 | } |
| 934 | }); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 935 | |
Anand Doshi | 179e377 | 2013-09-05 16:53:57 +0530 | [diff] [blame] | 936 | var rows = $.map(distinct_items, function(item) { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 937 | var item_tax_record = item_tax[item.item_code || item.item_name]; |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 938 | if(!item_tax_record) { return null; } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 939 | return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", { |
| 940 | item_name: item.item_name, |
| 941 | taxes: $.map(tax_accounts, function(head) { |
Anand Doshi | aa71b5b | 2013-08-05 13:00:53 +0530 | [diff] [blame] | 942 | return item_tax_record[head[0]] ? |
| 943 | "<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] | 944 | "<td></td>"; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 945 | }).join("\n") |
| 946 | }); |
| 947 | }).join("\n"); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 948 | |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 949 | if(!rows) return ""; |
Rushabh Mehta | f24f604 | 2015-08-20 18:01:40 +0530 | [diff] [blame] | 950 | 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] | 951 | + __("Show tax break-up") + '</a></p>\ |
Rushabh Mehta | 7d8b189 | 2013-11-08 16:27:18 +0530 | [diff] [blame] | 952 | <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] | 953 | <thead><tr>' + headings + '</tr></thead> \ |
| 954 | <tbody>' + rows + '</tbody> \ |
| 955 | </table></div>'; |
| 956 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 957 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 958 | validate_company_and_party: function() { |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 959 | var me = this; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 960 | var valid = true; |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 961 | |
Nabin Hait | 0f23106 | 2014-02-20 11:27:47 +0530 | [diff] [blame] | 962 | $.each(["company", "customer"], function(i, fieldname) { |
Saurabh | 8a8ef85 | 2015-10-21 19:28:53 +0530 | [diff] [blame] | 963 | 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] | 964 | if (!me.frm.doc[fieldname]) { |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 965 | msgprint(__("Please specify") + ": " + |
| 966 | 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] | 967 | ". " + __("It is needed to fetch Item Details.")); |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 968 | valid = false; |
| 969 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 970 | } |
| 971 | }); |
| 972 | return valid; |
| 973 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 974 | |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 975 | get_terms: function() { |
| 976 | var me = this; |
| 977 | if(this.frm.doc.tc_name) { |
Kanchan Chauhan | b642625 | 2016-12-12 17:22:27 +0530 | [diff] [blame] | 978 | return frappe.call({ |
| 979 | 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] | 980 | args: { |
Kanchan Chauhan | b642625 | 2016-12-12 17:22:27 +0530 | [diff] [blame] | 981 | template_name: this.frm.doc.tc_name, |
| 982 | doc: this.frm.doc |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 983 | }, |
Kanchan Chauhan | b642625 | 2016-12-12 17:22:27 +0530 | [diff] [blame] | 984 | callback: function(r) { |
| 985 | if(!r.exc) { |
| 986 | me.frm.set_value("terms", r.message); |
| 987 | } |
| 988 | } |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 989 | }); |
| 990 | } |
| 991 | }, |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 992 | |
| 993 | taxes_and_charges: function() { |
| 994 | var me = this; |
| 995 | if(this.frm.doc.taxes_and_charges) { |
| 996 | return this.frm.call({ |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 997 | method: "erpnext.controllers.accounts_controller.get_taxes_and_charges", |
| 998 | args: { |
Nabin Hait | 03f3a8a | 2014-05-13 16:38:31 +0530 | [diff] [blame] | 999 | "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges", |
| 1000 | this.frm.doc.name).options, |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 1001 | "master_name": this.frm.doc.taxes_and_charges, |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 1002 | }, |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1003 | callback: function(r) { |
| 1004 | if(!r.exc) { |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 1005 | me.frm.set_value("taxes", r.message); |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1006 | me.calculate_taxes_and_totals(); |
| 1007 | } |
| 1008 | } |
| 1009 | }); |
Nabin Hait | bc83b9c | 2014-05-02 17:03:18 +0530 | [diff] [blame] | 1010 | } |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1011 | }, |
| 1012 | |
| 1013 | show_item_wise_taxes: function() { |
| 1014 | if(this.frm.fields_dict.other_charges_calculation) { |
Anand Doshi | dd942bb | 2015-02-04 18:52:35 +0530 | [diff] [blame] | 1015 | var html = this.get_item_wise_taxes_html(); |
| 1016 | if (html) { |
| 1017 | this.frm.toggle_display("other_charges_calculation", true); |
| 1018 | $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html); |
| 1019 | } else { |
| 1020 | this.frm.toggle_display("other_charges_calculation", false); |
| 1021 | } |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1022 | } |
| 1023 | }, |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 1024 | |
| 1025 | is_recurring: function() { |
| 1026 | // set default values for recurring documents |
Rushabh Mehta | 756e518 | 2016-03-03 13:00:17 +0530 | [diff] [blame] | 1027 | if(this.frm.doc.is_recurring && this.frm.doc.__islocal) { |
| 1028 | frappe.msgprint(__("Please set recurring after saving")); |
| 1029 | this.frm.set_value('is_recurring', 0); |
| 1030 | return; |
| 1031 | } |
| 1032 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 1033 | if(this.frm.doc.is_recurring) { |
Nabin Hait | 68a628e | 2016-03-21 12:03:16 +0530 | [diff] [blame] | 1034 | if(!this.frm.doc.recurring_id) { |
| 1035 | this.frm.set_value('recurring_id', this.frm.doc.name); |
| 1036 | } |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 1037 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 1038 | var owner_email = this.frm.doc.owner=="Administrator" |
| 1039 | ? frappe.user_info("Administrator").email |
| 1040 | : this.frm.doc.owner; |
| 1041 | |
| 1042 | this.frm.doc.notification_email_address = $.map([cstr(owner_email), |
| 1043 | cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", "); |
| 1044 | this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate(); |
| 1045 | } |
| 1046 | |
| 1047 | refresh_many(["notification_email_address", "repeat_on_day_of_month"]); |
| 1048 | }, |
| 1049 | |
| 1050 | from_date: function() { |
| 1051 | // set to_date |
| 1052 | if(this.frm.doc.from_date) { |
| 1053 | var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6, |
| 1054 | 'Yearly': 12}; |
| 1055 | |
| 1056 | var months = recurring_type_map[this.frm.doc.recurring_type]; |
| 1057 | if(months) { |
| 1058 | var to_date = frappe.datetime.add_months(this.frm.doc.from_date, |
| 1059 | months); |
| 1060 | this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1); |
| 1061 | refresh_field('to_date'); |
| 1062 | } |
| 1063 | } |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 1064 | }, |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 1065 | |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 1066 | set_gross_profit: function(item) { |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 1067 | if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) { |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 1068 | rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1); |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 1069 | 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] | 1070 | } |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 1071 | }, |
| 1072 | |
| 1073 | setup_item_selector: function() { |
Rushabh Mehta | bc92824 | 2016-05-29 23:08:22 +0530 | [diff] [blame] | 1074 | // TODO: remove item selector |
| 1075 | |
| 1076 | return; |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 1077 | if(!this.item_selector) { |
| 1078 | this.item_selector = new erpnext.ItemSelector({frm: this.frm}); |
| 1079 | } |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 1080 | }, |
Rushabh Mehta | 76b1341 | 2016-07-18 14:30:18 +0530 | [diff] [blame] | 1081 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 1082 | get_advances: function() { |
| 1083 | if(!this.frm.is_return) { |
| 1084 | return this.frm.call({ |
| 1085 | method: "set_advances", |
| 1086 | doc: this.frm.doc, |
| 1087 | callback: function(r, rt) { |
| 1088 | refresh_field("advances"); |
| 1089 | } |
| 1090 | }) |
| 1091 | } |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1092 | }, |
Rushabh Mehta | 76b1341 | 2016-07-18 14:30:18 +0530 | [diff] [blame] | 1093 | |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1094 | make_payment_entry: function() { |
| 1095 | return frappe.call({ |
Rohit Waghchaure | b7a5502 | 2016-10-05 16:24:41 +0530 | [diff] [blame] | 1096 | method: cur_frm.cscript.get_method_for_payment(), |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1097 | args: { |
| 1098 | "dt": cur_frm.doc.doctype, |
| 1099 | "dn": cur_frm.doc.name |
| 1100 | }, |
| 1101 | callback: function(r) { |
| 1102 | var doclist = frappe.model.sync(r.message); |
| 1103 | frappe.set_route("Form", doclist[0].doctype, doclist[0].name); |
Nabin Hait | 56548cb | 2016-07-01 15:58:39 +0530 | [diff] [blame] | 1104 | // cur_frm.refresh_fields() |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1105 | } |
| 1106 | }); |
Rohit Waghchaure | b7a5502 | 2016-10-05 16:24:41 +0530 | [diff] [blame] | 1107 | }, |
| 1108 | |
| 1109 | get_method_for_payment: function(){ |
| 1110 | method = "erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry" |
Rohit Waghchaure | 7d52989 | 2016-10-06 14:35:04 +0530 | [diff] [blame] | 1111 | 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] | 1112 | if(in_list(['Sales Invoice', 'Purchase Invoice'], cur_frm.doc.doctype)){ |
| 1113 | method = "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_invoice" |
| 1114 | }else { |
| 1115 | method= "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_order" |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | return method |
Manas Solanki | cc90241 | 2016-11-10 19:15:11 +0530 | [diff] [blame] | 1120 | }, |
Nabin Hait | 6e74079 | 2017-05-06 12:45:16 +0530 | [diff] [blame] | 1121 | }); |