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(); |
Rohit Waghchaure | 6ab6303 | 2017-09-01 15:28:44 +0530 | [diff] [blame] | 7 | frappe.flags.hide_serial_batch_dialog = false; |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 8 | frappe.ui.form.on(this.frm.doctype + " Item", "rate", function(frm, cdt, cdn) { |
| 9 | var item = frappe.get_doc(cdt, cdn); |
Rushabh Mehta | f69ffeb | 2017-05-17 19:40:40 +0530 | [diff] [blame] | 10 | var has_margin_field = frappe.meta.has_field(cdt, 'margin_type'); |
| 11 | |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 12 | frappe.model.round_floats_in(item, ["rate", "price_list_rate"]); |
| 13 | |
| 14 | if(item.price_list_rate) { |
Rushabh Mehta | f69ffeb | 2017-05-17 19:40:40 +0530 | [diff] [blame] | 15 | if(item.rate > item.price_list_rate && has_margin_field) { |
| 16 | // if rate is greater than price_list_rate, set margin |
| 17 | // or set discount |
| 18 | item.discount_percentage = 0; |
Nabin Hait | e72c98d | 2017-05-24 09:01:44 +0530 | [diff] [blame] | 19 | item.margin_type = 'Amount'; |
Rushabh Mehta | 3631102 | 2017-06-20 09:30:53 +0530 | [diff] [blame] | 20 | item.margin_rate_or_amount = flt(item.rate - item.price_list_rate, |
Nabin Hait | e72c98d | 2017-05-24 09:01:44 +0530 | [diff] [blame] | 21 | precision("margin_rate_or_amount", item)); |
| 22 | item.rate_with_margin = item.rate; |
Rushabh Mehta | f69ffeb | 2017-05-17 19:40:40 +0530 | [diff] [blame] | 23 | } else { |
Rushabh Mehta | 3631102 | 2017-06-20 09:30:53 +0530 | [diff] [blame] | 24 | item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, |
Rushabh Mehta | f69ffeb | 2017-05-17 19:40:40 +0530 | [diff] [blame] | 25 | precision("discount_percentage", item)); |
| 26 | item.margin_type = ''; |
| 27 | item.margin_rate_or_amount = 0; |
| 28 | item.rate_with_margin = 0; |
| 29 | } |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 30 | } else { |
| 31 | item.discount_percentage = 0.0; |
Rushabh Mehta | f69ffeb | 2017-05-17 19:40:40 +0530 | [diff] [blame] | 32 | item.margin_type = ''; |
| 33 | item.margin_rate_or_amount = 0; |
| 34 | item.rate_with_margin = 0; |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 35 | } |
Saif Ur Rehman | 147fa73 | 2018-11-26 18:14:06 +0500 | [diff] [blame] | 36 | item.base_rate_with_margin = item.rate_with_margin * flt(frm.doc.conversion_rate); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 37 | |
| 38 | cur_frm.cscript.set_gross_profit(item); |
| 39 | cur_frm.cscript.calculate_taxes_and_totals(); |
Vishal Dhayagude | d42242d | 2017-11-29 16:09:59 +0530 | [diff] [blame] | 40 | |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 41 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 42 | |
Vishal Dhayagude | d42242d | 2017-11-29 16:09:59 +0530 | [diff] [blame] | 43 | |
| 44 | |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 45 | frappe.ui.form.on(this.frm.cscript.tax_table, "rate", function(frm, cdt, cdn) { |
| 46 | cur_frm.cscript.calculate_taxes_and_totals(); |
Rushabh Mehta | 764aa92 | 2016-05-02 13:28:46 +0530 | [diff] [blame] | 47 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 48 | |
| 49 | frappe.ui.form.on(this.frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) { |
| 50 | cur_frm.cscript.calculate_taxes_and_totals(); |
Rushabh Mehta | 764aa92 | 2016-05-02 13:28:46 +0530 | [diff] [blame] | 51 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 52 | |
| 53 | frappe.ui.form.on(this.frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) { |
| 54 | cur_frm.cscript.calculate_taxes_and_totals(); |
Rushabh Mehta | 764aa92 | 2016-05-02 13:28:46 +0530 | [diff] [blame] | 55 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 56 | |
| 57 | frappe.ui.form.on(this.frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) { |
| 58 | cur_frm.cscript.set_dynamic_labels(); |
| 59 | cur_frm.cscript.calculate_taxes_and_totals(); |
Rushabh Mehta | 764aa92 | 2016-05-02 13:28:46 +0530 | [diff] [blame] | 60 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 61 | |
| 62 | frappe.ui.form.on(this.frm.doctype, "apply_discount_on", function(frm) { |
| 63 | if(frm.doc.additional_discount_percentage) { |
| 64 | frm.trigger("additional_discount_percentage"); |
| 65 | } else { |
| 66 | cur_frm.cscript.calculate_taxes_and_totals(); |
| 67 | } |
Rushabh Mehta | 764aa92 | 2016-05-02 13:28:46 +0530 | [diff] [blame] | 68 | }); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 69 | |
| 70 | frappe.ui.form.on(this.frm.doctype, "additional_discount_percentage", function(frm) { |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 71 | if(!frm.doc.apply_discount_on) { |
| 72 | frappe.msgprint(__("Please set 'Apply Additional Discount On'")); |
Rushabh Mehta | 233a19a | 2017-07-07 13:05:11 +0530 | [diff] [blame] | 73 | return; |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | frm.via_discount_percentage = true; |
| 77 | |
| 78 | if(frm.doc.additional_discount_percentage && frm.doc.discount_amount) { |
| 79 | // Reset discount amount and net / grand total |
Rushabh Mehta | 6705ab3 | 2017-07-07 13:14:08 +0530 | [diff] [blame] | 80 | frm.doc.discount_amount = 0; |
| 81 | frm.cscript.calculate_taxes_and_totals(); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | var total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]); |
| 85 | var discount_amount = flt(total*flt(frm.doc.additional_discount_percentage) / 100, |
| 86 | precision("discount_amount")); |
| 87 | |
Rushabh Mehta | 233a19a | 2017-07-07 13:05:11 +0530 | [diff] [blame] | 88 | frm.set_value("discount_amount", discount_amount) |
| 89 | .then(() => delete frm.via_discount_percentage); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 90 | }); |
| 91 | |
| 92 | frappe.ui.form.on(this.frm.doctype, "discount_amount", function(frm) { |
| 93 | frm.cscript.set_dynamic_labels(); |
| 94 | |
| 95 | if (!frm.via_discount_percentage) { |
Rushabh Mehta | 233a19a | 2017-07-07 13:05:11 +0530 | [diff] [blame] | 96 | frm.doc.additional_discount_percentage = 0; |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | frm.cscript.calculate_taxes_and_totals(); |
| 100 | }); |
Makarand Bauskar | c196d74 | 2017-06-02 17:28:40 +0530 | [diff] [blame] | 101 | |
Saif | 60ec88b | 2018-11-13 12:07:53 +0500 | [diff] [blame] | 102 | frappe.ui.form.on(this.frm.doctype + " Item", { |
| 103 | items_add: function(frm, cdt, cdn) { |
| 104 | var item = frappe.get_doc(cdt, cdn); |
| 105 | if(!item.warehouse && frm.doc.set_warehouse) { |
| 106 | item.warehouse = frm.doc.set_warehouse; |
| 107 | } |
| 108 | } |
| 109 | }); |
| 110 | |
Makarand Bauskar | c196d74 | 2017-06-02 17:28:40 +0530 | [diff] [blame] | 111 | var me = this; |
| 112 | if(this.frm.fields_dict["items"].grid.get_field('batch_no')) { |
| 113 | this.frm.set_query("batch_no", "items", function(doc, cdt, cdn) { |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 114 | return me.set_query_for_batch(doc, cdt, cdn); |
Makarand Bauskar | c196d74 | 2017-06-02 17:28:40 +0530 | [diff] [blame] | 115 | }); |
| 116 | } |
Akhilesh Darjee | 564c621 | 2013-10-17 11:31:11 +0530 | [diff] [blame] | 117 | |
tundebabzy | 590bd0a | 2017-12-25 07:10:19 +0100 | [diff] [blame] | 118 | if( |
Faris Ansari | 404f39d | 2018-05-14 15:46:45 +0530 | [diff] [blame] | 119 | this.frm.docstatus < 2 |
Manas Solanki | a7f5589 | 2018-04-02 10:32:00 +0530 | [diff] [blame] | 120 | && this.frm.fields_dict["payment_terms_template"] |
tundebabzy | 590bd0a | 2017-12-25 07:10:19 +0100 | [diff] [blame] | 121 | && this.frm.fields_dict["payment_schedule"] |
| 122 | && this.frm.doc.payment_terms_template |
| 123 | && !this.frm.doc.payment_schedule.length |
| 124 | ){ |
tunde | df3a175 | 2017-09-11 11:02:57 +0100 | [diff] [blame] | 125 | this.frm.trigger("payment_terms_template"); |
| 126 | } |
| 127 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 128 | if(this.frm.fields_dict["taxes"]) { |
| 129 | this["taxes_remove"] = this.calculate_taxes_and_totals; |
Anand Doshi | a91c95f | 2013-08-23 13:00:47 +0530 | [diff] [blame] | 130 | } |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 131 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 132 | if(this.frm.fields_dict["items"]) { |
Vishal Dhayagude | d377018 | 2017-12-14 18:06:02 +0530 | [diff] [blame] | 133 | this["items_remove"] = this.calculate_net_weight; |
Anand Doshi | 1e4fb68 | 2013-08-23 12:56:33 +0530 | [diff] [blame] | 134 | } |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 135 | |
Neil Trini Lasrado | e2d8dd0 | 2015-06-22 19:06:15 +0530 | [diff] [blame] | 136 | if(this.frm.fields_dict["recurring_print_format"]) { |
| 137 | this.frm.set_query("recurring_print_format", function(doc) { |
| 138 | return{ |
| 139 | filters: [ |
| 140 | ['Print Format', 'doc_type', '=', cur_frm.doctype], |
| 141 | ] |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 142 | }; |
Neil Trini Lasrado | e2d8dd0 | 2015-06-22 19:06:15 +0530 | [diff] [blame] | 143 | }); |
| 144 | } |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 145 | |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 146 | if(this.frm.fields_dict["return_against"]) { |
| 147 | this.frm.set_query("return_against", function(doc) { |
| 148 | var filters = { |
| 149 | "docstatus": 1, |
| 150 | "is_return": 0, |
| 151 | "company": doc.company |
| 152 | }; |
| 153 | if (me.frm.fields_dict["customer"] && doc.customer) filters["customer"] = doc.customer; |
| 154 | if (me.frm.fields_dict["supplier"] && doc.supplier) filters["supplier"] = doc.supplier; |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 155 | |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 156 | return { |
| 157 | filters: filters |
Nabin Hait | 7b6eaee | 2017-10-03 01:09:46 +0530 | [diff] [blame] | 158 | }; |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 159 | }); |
| 160 | } |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 161 | |
| 162 | if(frappe.meta.get_docfield(this.frm.doc.doctype, "pricing_rules")) { |
Rohit Waghchaure | 70b996a | 2019-03-31 20:33:15 +0530 | [diff] [blame] | 163 | this.frm.set_indicator_formatter('pricing_rule', function(doc) { |
| 164 | return (doc.rule_applied) ? "green" : "red"; |
| 165 | }); |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 166 | } |
Nabin Hait | 7b6eaee | 2017-10-03 01:09:46 +0530 | [diff] [blame] | 167 | }, |
| 168 | onload: function() { |
| 169 | var me = this; |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 170 | |
Nabin Hait | 7b6eaee | 2017-10-03 01:09:46 +0530 | [diff] [blame] | 171 | if(this.frm.doc.__islocal) { |
| 172 | var currency = frappe.defaults.get_user_default("currency"); |
| 173 | |
| 174 | let set_value = (fieldname, value) => { |
| 175 | if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname]) { |
| 176 | return me.frm.set_value(fieldname, value); |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | return frappe.run_serially([ |
| 181 | () => set_value('currency', currency), |
| 182 | () => set_value('price_list_currency', currency), |
| 183 | () => set_value('status', 'Draft'), |
| 184 | () => set_value('is_subcontracted', 'No'), |
| 185 | () => { |
| 186 | if(this.frm.doc.company && !this.frm.doc.amended_from) { |
| 187 | this.frm.trigger("company"); |
| 188 | } |
| 189 | } |
| 190 | ]); |
| 191 | } |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 192 | }, |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 193 | |
Rohit Waghchaure | b4a5993 | 2018-11-26 20:02:01 +0530 | [diff] [blame] | 194 | is_return: function() { |
| 195 | if(!this.frm.doc.is_return && this.frm.doc.return_against) { |
| 196 | this.frm.set_value('return_against', ''); |
| 197 | } |
| 198 | }, |
| 199 | |
Nabin Hait | 8af429d | 2016-11-16 17:21:59 +0530 | [diff] [blame] | 200 | setup_quality_inspection: function() { |
| 201 | if(!in_list(["Delivery Note", "Sales Invoice", "Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype)) { |
| 202 | return; |
| 203 | } |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 204 | var me = this; |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 205 | 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] | 206 | ? "Incoming" : "Outgoing"; |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 207 | |
Manas Solanki | a8d1bc7 | 2016-11-15 14:48:23 +0530 | [diff] [blame] | 208 | var quality_inspection_field = this.frm.get_docfield("items", "quality_inspection"); |
Nabin Hait | 8af429d | 2016-11-16 17:21:59 +0530 | [diff] [blame] | 209 | quality_inspection_field.get_route_options_for_new_doc = function(row) { |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 210 | if(me.frm.is_new()) return; |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 211 | return { |
| 212 | "inspection_type": inspection_type, |
Manas Solanki | 34feab1 | 2016-11-16 14:36:28 +0530 | [diff] [blame] | 213 | "reference_type": me.frm.doc.doctype, |
Manas Solanki | dbc2511 | 2016-11-16 15:06:08 +0530 | [diff] [blame] | 214 | "reference_name": me.frm.doc.name, |
Nabin Hait | 8af429d | 2016-11-16 17:21:59 +0530 | [diff] [blame] | 215 | "item_code": row.doc.item_code, |
| 216 | "description": row.doc.description, |
| 217 | "item_serial_no": row.doc.serial_no ? row.doc.serial_no.split("\n")[0] : null, |
| 218 | "batch_no": row.doc.batch_no |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 219 | } |
| 220 | } |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 221 | |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 222 | this.frm.set_query("quality_inspection", "items", function(doc, cdt, cdn) { |
| 223 | var d = locals[cdt][cdn]; |
| 224 | return { |
| 225 | filters: { |
| 226 | docstatus: 1, |
| 227 | inspection_type: inspection_type, |
rohitwaghchaure | 4e8fdf7 | 2018-02-22 11:03:48 +0530 | [diff] [blame] | 228 | reference_name: doc.name, |
Manas Solanki | c2cd3fd | 2016-11-14 21:57:05 +0530 | [diff] [blame] | 229 | item_code: d.item_code |
| 230 | } |
| 231 | } |
| 232 | }); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 233 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 234 | |
rohitwaghchaure | 713cfc7 | 2018-09-11 17:40:37 +0530 | [diff] [blame] | 235 | make_payment_request: function() { |
Rohit Waghchaure | 210f498 | 2018-10-03 15:55:51 +0530 | [diff] [blame] | 236 | var me = this; |
| 237 | const payment_request_type = (in_list(['Sales Order', 'Sales Invoice'], this.frm.doc.doctype)) |
rohitwaghchaure | 713cfc7 | 2018-09-11 17:40:37 +0530 | [diff] [blame] | 238 | ? "Inward" : "Outward"; |
| 239 | |
| 240 | frappe.call({ |
| 241 | method:"erpnext.accounts.doctype.payment_request.payment_request.make_payment_request", |
| 242 | args: { |
| 243 | dt: me.frm.doc.doctype, |
| 244 | dn: me.frm.doc.name, |
| 245 | recipient_id: me.frm.doc.contact_email, |
| 246 | payment_request_type: payment_request_type, |
| 247 | party_type: payment_request_type == 'Outward' ? "Supplier" : "Customer", |
| 248 | party: payment_request_type == 'Outward' ? me.frm.doc.supplier : me.frm.doc.customer |
| 249 | }, |
| 250 | callback: function(r) { |
| 251 | if(!r.exc){ |
| 252 | var doc = frappe.model.sync(r.message); |
| 253 | frappe.set_route("Form", r.message.doctype, r.message.name); |
| 254 | } |
| 255 | } |
| 256 | }) |
| 257 | }, |
| 258 | |
Anand Doshi | 5e4e5d7 | 2013-08-06 17:23:44 +0530 | [diff] [blame] | 259 | onload_post_render: function() { |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 260 | if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length |
| 261 | && !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) { |
Nabin Hait | 7b6eaee | 2017-10-03 01:09:46 +0530 | [diff] [blame] | 262 | frappe.after_ajax(() => this.apply_default_taxes()); |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 263 | } 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] | 264 | && !this.frm.doc.is_pos) { |
Nabin Hait | 7b6eaee | 2017-10-03 01:09:46 +0530 | [diff] [blame] | 265 | frappe.after_ajax(() => this.calculate_taxes_and_totals()); |
Anand Doshi | 5e4e5d7 | 2013-08-06 17:23:44 +0530 | [diff] [blame] | 266 | } |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 267 | if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) { |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 268 | this.setup_item_selector(); |
Rushabh Mehta | b6843f2 | 2014-06-04 13:10:41 +0530 | [diff] [blame] | 269 | } |
Anand Doshi | 5e4e5d7 | 2013-08-06 17:23:44 +0530 | [diff] [blame] | 270 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 271 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 272 | refresh: function() { |
Anand Doshi | 8bde7f9 | 2014-04-24 18:11:49 +0530 | [diff] [blame] | 273 | erpnext.toggle_naming_series(); |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 274 | erpnext.hide_company(); |
Nabin Hait | 1f996c3 | 2013-07-29 19:00:53 +0530 | [diff] [blame] | 275 | this.set_dynamic_labels(); |
Rushabh Mehta | 614e7ab | 2015-02-18 19:51:48 +0530 | [diff] [blame] | 276 | this.setup_sms(); |
Rohit Waghchaure | f270567 | 2018-06-12 12:20:52 +0530 | [diff] [blame] | 277 | this.setup_quality_inspection(); |
Zarrar | 6922415 | 2018-11-13 11:11:32 +0530 | [diff] [blame] | 278 | this.frm.fields_dict["scan_barcode"] && this.frm.fields_dict["scan_barcode"].set_value(""); |
| 279 | this.frm.fields_dict["scan_barcode"] && this.frm.fields_dict["scan_barcode"].set_new_description(""); |
| 280 | }, |
| 281 | |
| 282 | scan_barcode: function() { |
| 283 | let scan_barcode_field = this.frm.fields_dict["scan_barcode"]; |
| 284 | |
Faris Ansari | 5631ffc | 2019-02-12 17:12:48 +0530 | [diff] [blame] | 285 | let show_description = function(idx, exist = null) { |
| 286 | if (exist) { |
| 287 | scan_barcode_field.set_new_description(__('Row #{0}: Qty increased by 1', [idx])); |
Zarrar | 6922415 | 2018-11-13 11:11:32 +0530 | [diff] [blame] | 288 | } else { |
Faris Ansari | 5631ffc | 2019-02-12 17:12:48 +0530 | [diff] [blame] | 289 | scan_barcode_field.set_new_description(__('Row #{0}: Item added', [idx])); |
Zarrar | 6922415 | 2018-11-13 11:11:32 +0530 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | if(this.frm.doc.scan_barcode) { |
| 294 | frappe.call({ |
| 295 | method: "erpnext.selling.page.point_of_sale.point_of_sale.search_serial_or_batch_or_barcode_number", |
| 296 | args: { search_value: this.frm.doc.scan_barcode } |
| 297 | }).then(r => { |
Faris Ansari | 5631ffc | 2019-02-12 17:12:48 +0530 | [diff] [blame] | 298 | const data = r && r.message; |
| 299 | if (!data) { |
| 300 | scan_barcode_field.set_new_description(__('Cannot find Item with this barcode')); |
| 301 | return; |
Zarrar | 6922415 | 2018-11-13 11:11:32 +0530 | [diff] [blame] | 302 | } |
Faris Ansari | 5631ffc | 2019-02-12 17:12:48 +0530 | [diff] [blame] | 303 | |
| 304 | let cur_grid = this.frm.fields_dict.items.grid; |
| 305 | |
| 306 | let row_to_modify = null; |
| 307 | const existing_item_row = this.frm.doc.items.find(d => d.item_code === data.item_code); |
| 308 | const blank_item_row = this.frm.doc.items.find(d => !d.item_code); |
| 309 | |
| 310 | if (existing_item_row) { |
| 311 | row_to_modify = existing_item_row; |
| 312 | } else if (blank_item_row) { |
| 313 | row_to_modify = blank_item_row; |
Zarrar | 6922415 | 2018-11-13 11:11:32 +0530 | [diff] [blame] | 314 | } |
Faris Ansari | 5631ffc | 2019-02-12 17:12:48 +0530 | [diff] [blame] | 315 | |
| 316 | if (!row_to_modify) { |
| 317 | // add new row |
| 318 | row_to_modify = frappe.model.add_child(this.frm.doc, cur_grid.doctype, 'items'); |
| 319 | } |
| 320 | |
| 321 | show_description(row_to_modify.idx, row_to_modify.item_code); |
| 322 | |
Rohit Waghchaure | ab4ff98 | 2019-02-28 13:21:28 +0530 | [diff] [blame] | 323 | this.frm.from_barcode = true; |
Faris Ansari | 5631ffc | 2019-02-12 17:12:48 +0530 | [diff] [blame] | 324 | frappe.model.set_value(row_to_modify.doctype, row_to_modify.name, { |
| 325 | item_code: data.item_code, |
| 326 | qty: (row_to_modify.qty || 0) + 1 |
| 327 | }); |
| 328 | |
Rohit Waghchaure | ab4ff98 | 2019-02-28 13:21:28 +0530 | [diff] [blame] | 329 | ['serial_no', 'batch_no', 'barcode'].forEach(field => { |
| 330 | if (data[field] && frappe.meta.has_field(row_to_modify.doctype, field)) { |
| 331 | frappe.model.set_value(row_to_modify.doctype, |
| 332 | row_to_modify.name, field, data[field]); |
| 333 | } |
| 334 | }); |
| 335 | |
| 336 | scan_barcode_field.set_value(''); |
Zarrar | 6922415 | 2018-11-13 11:11:32 +0530 | [diff] [blame] | 337 | }); |
Zarrar | 6922415 | 2018-11-13 11:11:32 +0530 | [diff] [blame] | 338 | } |
| 339 | return false; |
Rushabh Mehta | 614e7ab | 2015-02-18 19:51:48 +0530 | [diff] [blame] | 340 | }, |
Rushabh Mehta | 4a3c805 | 2015-05-19 11:10:12 +0530 | [diff] [blame] | 341 | |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 342 | apply_default_taxes: function() { |
| 343 | var me = this; |
Rushabh Mehta | 4984bcc | 2015-05-21 12:51:07 +0530 | [diff] [blame] | 344 | var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges", |
| 345 | me.frm.doc.name); |
| 346 | |
rohitwaghchaure | 1f39feb | 2018-01-05 12:25:45 +0530 | [diff] [blame] | 347 | if (!this.frm.doc.taxes_and_charges && this.frm.doc.taxes) { |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | if (taxes_and_charges_field) { |
Nabin Hait | bdfb070 | 2015-10-28 15:38:08 +0530 | [diff] [blame] | 352 | return frappe.call({ |
Rushabh Mehta | 4984bcc | 2015-05-21 12:51:07 +0530 | [diff] [blame] | 353 | method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges", |
| 354 | args: { |
rohitwaghchaure | 505661b | 2017-12-11 14:52:28 +0530 | [diff] [blame] | 355 | "master_doctype": taxes_and_charges_field.options, |
Nabin Hait | a2426fc | 2018-01-15 17:45:46 +0530 | [diff] [blame] | 356 | "tax_template": me.frm.doc.taxes_and_charges, |
rohitwaghchaure | 505661b | 2017-12-11 14:52:28 +0530 | [diff] [blame] | 357 | "company": me.frm.doc.company |
Rushabh Mehta | 4984bcc | 2015-05-21 12:51:07 +0530 | [diff] [blame] | 358 | }, |
| 359 | callback: function(r) { |
Nabin Hait | a2426fc | 2018-01-15 17:45:46 +0530 | [diff] [blame] | 360 | if(!r.exc && r.message) { |
Rushabh Mehta | 98aa581 | 2017-11-14 09:32:32 +0530 | [diff] [blame] | 361 | frappe.run_serially([ |
| 362 | () => { |
| 363 | // directly set in doc, so as not to call triggers |
rohitwaghchaure | 1bc47fa | 2017-12-16 10:49:58 +0530 | [diff] [blame] | 364 | if(r.message.taxes_and_charges) { |
| 365 | me.frm.doc.taxes_and_charges = r.message.taxes_and_charges; |
| 366 | } |
Rushabh Mehta | 98aa581 | 2017-11-14 09:32:32 +0530 | [diff] [blame] | 367 | |
| 368 | // set taxes table |
rohitwaghchaure | 1bc47fa | 2017-12-16 10:49:58 +0530 | [diff] [blame] | 369 | if(r.message.taxes) { |
| 370 | me.frm.set_value("taxes", r.message.taxes); |
| 371 | } |
Rushabh Mehta | 98aa581 | 2017-11-14 09:32:32 +0530 | [diff] [blame] | 372 | }, |
Saif | 3c9155e | 2018-10-18 17:27:56 +0500 | [diff] [blame] | 373 | () => me.set_dynamic_labels(), |
Rushabh Mehta | 98aa581 | 2017-11-14 09:32:32 +0530 | [diff] [blame] | 374 | () => me.calculate_taxes_and_totals() |
| 375 | ]); |
Rushabh Mehta | 4984bcc | 2015-05-21 12:51:07 +0530 | [diff] [blame] | 376 | } |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 377 | } |
Rushabh Mehta | 4984bcc | 2015-05-21 12:51:07 +0530 | [diff] [blame] | 378 | }); |
| 379 | } |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 380 | }, |
Rushabh Mehta | 614e7ab | 2015-02-18 19:51:48 +0530 | [diff] [blame] | 381 | |
| 382 | setup_sms: function() { |
| 383 | var me = this; |
Nabin Hait | 7132bd3 | 2016-04-13 15:19:37 +0530 | [diff] [blame] | 384 | 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] | 385 | && this.frm.doctype != "Purchase Invoice") { |
Rushabh Mehta | 614e7ab | 2015-02-18 19:51:48 +0530 | [diff] [blame] | 386 | this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); }); |
| 387 | } |
| 388 | }, |
| 389 | |
| 390 | send_sms: function() { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 391 | var sms_man = new erpnext.SMSManager(this.frm.doc); |
Nabin Hait | 239c2c3 | 2015-01-15 14:21:41 +0530 | [diff] [blame] | 392 | }, |
Anand Doshi | 50d7e8c | 2015-01-06 17:14:13 +0530 | [diff] [blame] | 393 | |
Neil Trini Lasrado | d015195 | 2015-06-09 15:21:50 +0530 | [diff] [blame] | 394 | barcode: function(doc, cdt, cdn) { |
| 395 | var d = locals[cdt][cdn]; |
| 396 | if(d.barcode=="" || d.barcode==null) { |
| 397 | // barcode cleared, remove item |
| 398 | d.item_code = ""; |
| 399 | } |
Rohit Waghchaure | ab4ff98 | 2019-02-28 13:21:28 +0530 | [diff] [blame] | 400 | |
| 401 | this.frm.from_barcode = true; |
| 402 | this.item_code(doc, cdt, cdn); |
Neil Trini Lasrado | d015195 | 2015-06-09 15:21:50 +0530 | [diff] [blame] | 403 | }, |
| 404 | |
Rohit Waghchaure | ab4ff98 | 2019-02-28 13:21:28 +0530 | [diff] [blame] | 405 | item_code: function(doc, cdt, cdn) { |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 406 | var me = this; |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 407 | var item = frappe.get_doc(cdt, cdn); |
pratu16x7 | 5f389c9 | 2017-06-26 13:38:24 +0530 | [diff] [blame] | 408 | var update_stock = 0, show_batch_dialog = 0; |
rohitwaghchaure | 5999ade | 2017-08-09 17:24:13 +0530 | [diff] [blame] | 409 | if(['Sales Invoice'].includes(this.frm.doc.doctype)) { |
pratu16x7 | 577e4c4 | 2017-06-23 11:39:03 +0530 | [diff] [blame] | 410 | update_stock = cint(me.frm.doc.update_stock); |
| 411 | show_batch_dialog = update_stock; |
pratu16x7 | ae450fc | 2017-06-26 15:31:46 +0530 | [diff] [blame] | 412 | |
| 413 | } else if((this.frm.doc.doctype === 'Purchase Receipt' && me.frm.doc.is_return) || |
| 414 | this.frm.doc.doctype === 'Delivery Note') { |
pratu16x7 | 89d001c | 2017-06-26 17:45:49 +0530 | [diff] [blame] | 415 | show_batch_dialog = 1; |
pratu16x7 | 577e4c4 | 2017-06-23 11:39:03 +0530 | [diff] [blame] | 416 | } |
Rushabh Mehta | 675f9c6 | 2016-06-02 16:34:00 +0530 | [diff] [blame] | 417 | // clear barcode if setting item (else barcode will take priority) |
Rohit Waghchaure | ab4ff98 | 2019-02-28 13:21:28 +0530 | [diff] [blame] | 418 | if(!this.frm.from_barcode) { |
Rushabh Mehta | 675f9c6 | 2016-06-02 16:34:00 +0530 | [diff] [blame] | 419 | item.barcode = null; |
| 420 | } |
Rohit Waghchaure | ab4ff98 | 2019-02-28 13:21:28 +0530 | [diff] [blame] | 421 | |
| 422 | this.frm.from_barcode = false; |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 423 | if(item.item_code || item.barcode || item.serial_no) { |
| 424 | if(!this.validate_company_and_party()) { |
pratu16x7 | 577e4c4 | 2017-06-23 11:39:03 +0530 | [diff] [blame] | 425 | this.frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove(); |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 426 | } else { |
| 427 | return this.frm.call({ |
| 428 | method: "erpnext.stock.get_item_details.get_item_details", |
| 429 | child: item, |
| 430 | args: { |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 431 | doc: me.frm.doc, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 432 | args: { |
| 433 | item_code: item.item_code, |
| 434 | barcode: item.barcode, |
| 435 | serial_no: item.serial_no, |
Saif Ur Rehman | 2689dea | 2019-01-09 13:43:06 +0500 | [diff] [blame] | 436 | set_warehouse: me.frm.doc.set_warehouse, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 437 | warehouse: item.warehouse, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 438 | customer: me.frm.doc.customer, |
| 439 | supplier: me.frm.doc.supplier, |
| 440 | currency: me.frm.doc.currency, |
pratu16x7 | 577e4c4 | 2017-06-23 11:39:03 +0530 | [diff] [blame] | 441 | update_stock: update_stock, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 442 | conversion_rate: me.frm.doc.conversion_rate, |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 443 | price_list: me.frm.doc.selling_price_list || me.frm.doc.buying_price_list, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 444 | price_list_currency: me.frm.doc.price_list_currency, |
| 445 | plc_conversion_rate: me.frm.doc.plc_conversion_rate, |
| 446 | company: me.frm.doc.company, |
| 447 | order_type: me.frm.doc.order_type, |
| 448 | is_pos: cint(me.frm.doc.is_pos), |
| 449 | is_subcontracted: me.frm.doc.is_subcontracted, |
Nabin Hait | a1bf43b | 2015-03-17 10:50:47 +0530 | [diff] [blame] | 450 | transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date, |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 451 | ignore_pricing_rule: me.frm.doc.ignore_pricing_rule, |
Anand Doshi | afe85bd | 2016-01-21 21:04:16 +0530 | [diff] [blame] | 452 | doctype: me.frm.doc.doctype, |
| 453 | name: me.frm.doc.name, |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 454 | project: item.project || me.frm.doc.project, |
mbauskar | ba41242 | 2017-04-19 12:35:51 +0530 | [diff] [blame] | 455 | qty: item.qty || 1, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 456 | stock_qty: item.stock_qty, |
rohitwaghchaure | c037dc7 | 2017-11-28 16:11:15 +0530 | [diff] [blame] | 457 | conversion_factor: item.conversion_factor, |
Vishal Dhayagude | d42242d | 2017-11-29 16:09:59 +0530 | [diff] [blame] | 458 | weight_per_unit: item.weight_per_unit, |
| 459 | weight_uom: item.weight_uom, |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 460 | uom : item.uom, |
Nabin Hait | 158e7dc | 2019-01-04 11:04:37 +0530 | [diff] [blame] | 461 | stock_uom: item.stock_uom, |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 462 | pos_profile: me.frm.doc.doctype == 'Sales Invoice' ? me.frm.doc.pos_profile : '', |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 463 | cost_center: item.cost_center, |
| 464 | tax_category: me.frm.doc.tax_category, |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 465 | item_tax_template: item.item_tax_template, |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 466 | child_docname: item.name, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 467 | } |
| 468 | }, |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 469 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 470 | callback: function(r) { |
| 471 | if(!r.exc) { |
Vishal Dhayagude | d377018 | 2017-12-14 18:06:02 +0530 | [diff] [blame] | 472 | frappe.run_serially([ |
Saif Ur Rehman | aca374e | 2018-12-28 04:53:25 +0500 | [diff] [blame] | 473 | () => { |
| 474 | var d = locals[cdt][cdn]; |
Saif Ur Rehman | fd531a6 | 2018-12-29 01:49:11 +0500 | [diff] [blame] | 475 | me.add_taxes_from_item_tax_template(d.item_tax_rate); |
Saif Ur Rehman | aca374e | 2018-12-28 04:53:25 +0500 | [diff] [blame] | 476 | }, |
Vishal Dhayagude | d377018 | 2017-12-14 18:06:02 +0530 | [diff] [blame] | 477 | () => me.frm.script_manager.trigger("price_list_rate", cdt, cdn), |
| 478 | () => me.toggle_conversion_factor(item), |
| 479 | () => { |
| 480 | if(show_batch_dialog && !frappe.flags.hide_serial_batch_dialog) { |
| 481 | var d = locals[cdt][cdn]; |
| 482 | $.each(r.message, function(k, v) { |
| 483 | if(!d[k]) d[k] = v; |
| 484 | }); |
Rohit Waghchaure | 6ab6303 | 2017-09-01 15:28:44 +0530 | [diff] [blame] | 485 | |
Vishal Dhayagude | d377018 | 2017-12-14 18:06:02 +0530 | [diff] [blame] | 486 | erpnext.show_serial_batch_selector(me.frm, d, (item) => { |
| 487 | me.frm.script_manager.trigger('qty', item.doctype, item.name); |
| 488 | }); |
| 489 | } |
| 490 | }, |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 491 | () => me.conversion_factor(doc, cdt, cdn, true), |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 492 | () => me.validate_pricing_rule(item) |
Vishal Dhayagude | d377018 | 2017-12-14 18:06:02 +0530 | [diff] [blame] | 493 | ]); |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | }); |
| 497 | } |
| 498 | } |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 499 | }, |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 500 | |
Saif Ur Rehman | aca374e | 2018-12-28 04:53:25 +0500 | [diff] [blame] | 501 | add_taxes_from_item_tax_template: function(item_tax_map) { |
| 502 | let me = this; |
| 503 | |
Saif Ur Rehman | fd531a6 | 2018-12-29 01:49:11 +0500 | [diff] [blame] | 504 | if(item_tax_map && cint(frappe.defaults.get_default("add_taxes_from_item_tax_template"))) { |
Saif Ur Rehman | aca374e | 2018-12-28 04:53:25 +0500 | [diff] [blame] | 505 | if(typeof (item_tax_map) == "string") { |
| 506 | item_tax_map = JSON.parse(item_tax_map); |
| 507 | } |
| 508 | |
| 509 | $.each(item_tax_map, function(tax, rate) { |
| 510 | let found = (me.frm.doc.taxes || []).find(d => d.account_head === tax); |
| 511 | if(!found) { |
| 512 | let child = frappe.model.add_child(me.frm.doc, "taxes"); |
| 513 | child.charge_type = "On Net Total"; |
| 514 | child.account_head = tax; |
| 515 | child.rate = 0; |
| 516 | } |
| 517 | }); |
| 518 | } |
| 519 | }, |
| 520 | |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 521 | serial_no: function(doc, cdt, cdn) { |
| 522 | var me = this; |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 523 | var item = frappe.get_doc(cdt, cdn); |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 524 | |
deepeshgarg007 | 49cca7a | 2019-01-25 16:41:01 +0530 | [diff] [blame] | 525 | if (item && item.serial_no) { |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 526 | if (!item.item_code) { |
Rushabh Mehta | b66edd1 | 2017-03-30 21:21:56 +0530 | [diff] [blame] | 527 | this.frm.trigger("item_code", cdt, cdn); |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 528 | } |
| 529 | else { |
Nabin Hait | 79c2191 | 2017-07-12 14:10:43 +0530 | [diff] [blame] | 530 | var valid_serial_nos = []; |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 531 | |
| 532 | // Replacing all occurences of comma with carriage return |
| 533 | var serial_nos = item.serial_no.trim().replace(/,/g, '\n'); |
| 534 | |
| 535 | serial_nos = serial_nos.trim().split('\n'); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 536 | |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 537 | // Trim each string and push unique string to new list |
| 538 | for (var x=0; x<=serial_nos.length - 1; x++) { |
Nabin Hait | 79c2191 | 2017-07-12 14:10:43 +0530 | [diff] [blame] | 539 | if (serial_nos[x].trim() != "" && valid_serial_nos.indexOf(serial_nos[x].trim()) == -1) { |
| 540 | valid_serial_nos.push(serial_nos[x].trim()); |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | // Add the new list to the serial no. field in grid with each in new line |
Nabin Hait | 79c2191 | 2017-07-12 14:10:43 +0530 | [diff] [blame] | 545 | item.serial_no = valid_serial_nos.join('\n'); |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 546 | |
| 547 | refresh_field("serial_no", item.name, item.parentfield); |
Nabin Hait | ed376ca | 2018-05-03 16:24:52 +0530 | [diff] [blame] | 548 | if(!doc.is_return && cint(user_defaults.set_qty_in_transactions_based_on_serial_no_input)) { |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 549 | frappe.model.set_value(item.doctype, item.name, |
Nabin Hait | 79c2191 | 2017-07-12 14:10:43 +0530 | [diff] [blame] | 550 | "qty", valid_serial_nos.length / item.conversion_factor); |
| 551 | frappe.model.set_value(item.doctype, item.name, "stock_qty", valid_serial_nos.length); |
Nabin Hait | b7c0c55 | 2015-08-04 12:18:12 +0530 | [diff] [blame] | 552 | } |
Akhilesh Darjee | fcd70d0 | 2013-10-18 14:24:21 +0530 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 556 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 557 | validate: function() { |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 558 | this.calculate_taxes_and_totals(false); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 559 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 560 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 561 | company: function() { |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 562 | var me = this; |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 563 | var set_pricing = function() { |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 564 | if(me.frm.doc.company && me.frm.fields_dict.currency) { |
| 565 | var company_currency = me.get_company_currency(); |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 566 | var company_doc = frappe.get_doc(":Company", me.frm.doc.company); |
tundebabzy | 5510d07 | 2017-09-29 10:51:59 +0100 | [diff] [blame] | 567 | |
Nabin Hait | 1f10d69 | 2017-10-02 13:20:51 +0530 | [diff] [blame] | 568 | if (!me.frm.doc.currency) { |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 569 | me.frm.set_value("currency", company_currency); |
| 570 | } |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 571 | |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 572 | if (me.frm.doc.currency == company_currency) { |
| 573 | me.frm.set_value("conversion_rate", 1.0); |
| 574 | } |
| 575 | if (me.frm.doc.price_list_currency == company_currency) { |
| 576 | me.frm.set_value('plc_conversion_rate', 1.0); |
| 577 | } |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 578 | if (company_doc.default_letter_head) { |
Rushabh Mehta | c185734 | 2015-05-27 12:29:57 +0530 | [diff] [blame] | 579 | if(me.frm.fields_dict.letter_head) { |
| 580 | me.frm.set_value("letter_head", company_doc.default_letter_head); |
| 581 | } |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 582 | } |
Rohit Waghchaure | 585448b | 2016-12-30 15:41:55 +0530 | [diff] [blame] | 583 | 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] | 584 | me.frm.set_value("tc_name", company_doc.default_terms); |
| 585 | } |
Akhilesh Darjee | 1797cfe | 2013-10-11 13:34:10 +0530 | [diff] [blame] | 586 | |
rohitwaghchaure | 505661b | 2017-12-11 14:52:28 +0530 | [diff] [blame] | 587 | frappe.run_serially([ |
| 588 | () => me.frm.script_manager.trigger("currency"), |
Saif Ur Rehman | eeead1d | 2018-12-29 02:25:00 +0500 | [diff] [blame] | 589 | () => me.update_item_tax_map(), |
rohitwaghchaure | 505661b | 2017-12-11 14:52:28 +0530 | [diff] [blame] | 590 | () => me.apply_default_taxes(), |
| 591 | () => me.apply_pricing_rule() |
| 592 | ]); |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 593 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 594 | } |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 595 | |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 596 | var set_party_account = function(set_pricing) { |
| 597 | if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) { |
| 598 | if(me.frm.doc.doctype=="Sales Invoice") { |
| 599 | var party_type = "Customer"; |
| 600 | var party_account_field = 'debit_to'; |
| 601 | } else { |
| 602 | var party_type = "Supplier"; |
| 603 | var party_account_field = 'credit_to'; |
| 604 | } |
Anand Doshi | 39a2891 | 2016-02-15 11:42:18 +0530 | [diff] [blame] | 605 | |
Nabin Hait | ef86440 | 2016-02-10 14:47:25 +0530 | [diff] [blame] | 606 | var party = me.frm.doc[frappe.model.scrub(party_type)]; |
Faris Ansari | f7717b9 | 2018-02-08 11:09:54 +0530 | [diff] [blame] | 607 | if(party && me.frm.doc.company) { |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 608 | return frappe.call({ |
| 609 | method: "erpnext.accounts.party.get_party_account", |
| 610 | args: { |
| 611 | company: me.frm.doc.company, |
| 612 | party_type: party_type, |
Nabin Hait | ef86440 | 2016-02-10 14:47:25 +0530 | [diff] [blame] | 613 | party: party |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 614 | }, |
| 615 | callback: function(r) { |
| 616 | if(!r.exc && r.message) { |
| 617 | me.frm.set_value(party_account_field, r.message); |
| 618 | set_pricing(); |
| 619 | } |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 620 | } |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 621 | }); |
Nabin Hait | ef86440 | 2016-02-10 14:47:25 +0530 | [diff] [blame] | 622 | } else { |
| 623 | set_pricing(); |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 624 | } |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 625 | } else { |
| 626 | set_pricing(); |
| 627 | } |
Rushabh Mehta | 8d1c7a2 | 2016-02-10 13:08:42 +0530 | [diff] [blame] | 628 | |
Nabin Hait | a731ad4 | 2016-02-04 17:21:28 +0530 | [diff] [blame] | 629 | } |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 630 | |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 631 | if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date; |
| 632 | else var date = this.frm.doc.transaction_date; |
Rushabh Mehta | 5dfe20c | 2016-04-26 12:59:08 +0530 | [diff] [blame] | 633 | |
| 634 | if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") && |
Rohit Waghchaure | b2e7a1f | 2016-04-11 12:10:20 +0530 | [diff] [blame] | 635 | in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)){ |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 636 | erpnext.utils.get_shipping_address(this.frm, function(){ |
| 637 | set_party_account(set_pricing); |
| 638 | }) |
| 639 | } else { |
Rohit Waghchaure | b2e7a1f | 2016-04-11 12:10:20 +0530 | [diff] [blame] | 640 | set_party_account(set_pricing); |
| 641 | } |
Rushabh Mehta | 45ca8a4 | 2015-04-28 16:02:09 +0530 | [diff] [blame] | 642 | |
| 643 | if(this.frm.doc.company) { |
| 644 | erpnext.last_selected_company = this.frm.doc.company; |
| 645 | } |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 646 | }, |
| 647 | |
| 648 | transaction_date: function() { |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 649 | if (this.frm.doc.transaction_date) { |
| 650 | this.frm.transaction_date = this.frm.doc.transaction_date; |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 651 | frappe.ui.form.trigger(this.frm.doc.doctype, "currency"); |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 652 | } |
Neil Trini Lasrado | 78fa695 | 2014-10-03 17:43:02 +0530 | [diff] [blame] | 653 | }, |
| 654 | |
| 655 | posting_date: function() { |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 656 | var me = this; |
| 657 | if (this.frm.doc.posting_date) { |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 658 | this.frm.posting_date = this.frm.doc.posting_date; |
| 659 | |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 660 | if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) || |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 661 | (this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) { |
| 662 | return frappe.call({ |
| 663 | method: "erpnext.accounts.party.get_due_date", |
| 664 | args: { |
| 665 | "posting_date": me.frm.doc.posting_date, |
| 666 | "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier", |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 667 | "bill_date": me.frm.doc.bill_date, |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 668 | "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier, |
| 669 | "company": me.frm.doc.company |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 670 | }, |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 671 | callback: function(r, rt) { |
| 672 | if(r.message) { |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 673 | me.frm.doc.due_date = r.message; |
| 674 | refresh_field("due_date"); |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 675 | frappe.ui.form.trigger(me.frm.doc.doctype, "currency"); |
tundebabzy | 650a1ee | 2018-01-02 07:32:30 +0100 | [diff] [blame] | 676 | me.recalculate_terms(); |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 677 | } |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 678 | } |
| 679 | }) |
Nabin Hait | 87d7027 | 2016-12-08 14:43:11 +0530 | [diff] [blame] | 680 | } else { |
| 681 | frappe.ui.form.trigger(me.frm.doc.doctype, "currency"); |
Nabin Hait | 40e92b6 | 2015-07-09 17:12:23 +0530 | [diff] [blame] | 682 | } |
| 683 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 684 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 685 | |
tundebabzy | da22167 | 2018-01-09 06:28:44 +0100 | [diff] [blame] | 686 | due_date: function() { |
| 687 | // due_date is to be changed, payment terms template and/or payment schedule must |
| 688 | // be removed as due_date is automatically changed based on payment terms |
rohitwaghchaure | 52beb77 | 2018-01-29 17:40:44 +0530 | [diff] [blame] | 689 | if (this.frm.doc.due_date && !this.frm.updating_party_details && !this.frm.doc.is_pos) { |
Saurabh | 3959aeb | 2018-01-23 15:40:29 +0530 | [diff] [blame] | 690 | if (this.frm.doc.payment_terms_template || |
| 691 | (this.frm.doc.payment_schedule && this.frm.doc.payment_schedule.length)) { |
tundebabzy | da22167 | 2018-01-09 06:28:44 +0100 | [diff] [blame] | 692 | var message1 = ""; |
| 693 | var message2 = ""; |
| 694 | var final_message = "Please clear the "; |
| 695 | |
| 696 | if (this.frm.doc.payment_terms_template) { |
| 697 | message1 = "selected Payment Terms Template"; |
| 698 | final_message = final_message + message1; |
| 699 | } |
| 700 | |
Nabin Hait | 2bfa180 | 2018-01-17 14:39:05 +0530 | [diff] [blame] | 701 | if ((this.frm.doc.payment_schedule || []).length) { |
tundebabzy | da22167 | 2018-01-09 06:28:44 +0100 | [diff] [blame] | 702 | message2 = "Payment Schedule Table"; |
| 703 | if (message1.length !== 0) message2 = " and " + message2; |
| 704 | final_message = final_message + message2; |
| 705 | } |
tundebabzy | da22167 | 2018-01-09 06:28:44 +0100 | [diff] [blame] | 706 | frappe.msgprint(final_message); |
| 707 | } |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 708 | } |
tundebabzy | da22167 | 2018-01-09 06:28:44 +0100 | [diff] [blame] | 709 | }, |
| 710 | |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 711 | bill_date: function() { |
| 712 | this.posting_date(); |
| 713 | }, |
| 714 | |
tundebabzy | 650a1ee | 2018-01-02 07:32:30 +0100 | [diff] [blame] | 715 | recalculate_terms: function() { |
| 716 | const doc = this.frm.doc; |
tundebabzy | 650a1ee | 2018-01-02 07:32:30 +0100 | [diff] [blame] | 717 | if (doc.payment_terms_template) { |
| 718 | this.payment_terms_template(); |
| 719 | } else if (doc.payment_schedule) { |
| 720 | const me = this; |
tundebabzy | 650a1ee | 2018-01-02 07:32:30 +0100 | [diff] [blame] | 721 | doc.payment_schedule.forEach( |
| 722 | function(term) { |
| 723 | if (term.payment_term) { |
| 724 | me.payment_term(doc, term.doctype, term.name); |
| 725 | } else { |
| 726 | frappe.model.set_value( |
| 727 | term.doctype, term.name, 'due_date', |
| 728 | doc.posting_date || doc.transaction_date |
| 729 | ); |
| 730 | } |
| 731 | } |
| 732 | ); |
| 733 | } |
| 734 | }, |
| 735 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 736 | get_company_currency: function() { |
| 737 | return erpnext.get_currency(this.frm.doc.company); |
| 738 | }, |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 739 | |
Neil Trini Lasrado | 09a66c4 | 2015-07-07 16:41:37 +0530 | [diff] [blame] | 740 | contact_person: function() { |
| 741 | erpnext.utils.get_contact_details(this.frm); |
| 742 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 743 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 744 | currency: function() { |
Chude Osiegbu | 256ec17 | 2016-09-19 03:53:37 +0100 | [diff] [blame] | 745 | /* 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] | 746 | 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] | 747 | /* end manqala */ |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 748 | var me = this; |
| 749 | this.set_dynamic_labels(); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 750 | var company_currency = this.get_company_currency(); |
Nabin Hait | 69cdf59 | 2015-05-02 18:46:10 +0530 | [diff] [blame] | 751 | // 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] | 752 | if(this.frm.doc.currency && this.frm.doc.currency !== company_currency |
Nabin Hait | 3950507 | 2016-07-07 11:11:32 +0530 | [diff] [blame] | 753 | && !this.frm.doc.ignore_pricing_rule) { |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 754 | |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 755 | this.get_exchange_rate(transaction_date, this.frm.doc.currency, company_currency, |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 756 | function(exchange_rate) { |
Akhilesh Darjee | 7837827 | 2013-10-11 19:06:30 +0530 | [diff] [blame] | 757 | me.frm.set_value("conversion_rate", exchange_rate); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 758 | }); |
| 759 | } else { |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 760 | this.conversion_rate(); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 761 | } |
| 762 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 763 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 764 | conversion_rate: function() { |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 765 | const me = this.frm; |
Anand Doshi | 535d833 | 2013-07-19 13:51:07 +0530 | [diff] [blame] | 766 | if(this.frm.doc.currency === this.get_company_currency()) { |
| 767 | this.frm.set_value("conversion_rate", 1.0); |
Akhilesh Darjee | acf65a7 | 2013-10-17 13:04:26 +0530 | [diff] [blame] | 768 | } |
| 769 | if(this.frm.doc.currency === this.frm.doc.price_list_currency && |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 770 | this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 771 | this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 772 | } |
Rushabh Mehta | 4a3c805 | 2015-05-19 11:10:12 +0530 | [diff] [blame] | 773 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 774 | if(flt(this.frm.doc.conversion_rate)>0.0) { |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 775 | if(this.frm.doc.ignore_pricing_rule) { |
| 776 | this.calculate_taxes_and_totals(); |
Anand Doshi | 51f722d | 2014-07-04 15:44:26 +0530 | [diff] [blame] | 777 | } else if (!this.in_apply_price_list){ |
Nabin Hait | a9c9b69 | 2017-08-31 19:40:09 +0530 | [diff] [blame] | 778 | this.set_actual_charges_based_on_currency(); |
Anand Doshi | 51f722d | 2014-07-04 15:44:26 +0530 | [diff] [blame] | 779 | this.apply_price_list(); |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 780 | } |
| 781 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 782 | } |
tundebabzy | ab5b030 | 2017-09-21 10:20:39 +0100 | [diff] [blame] | 783 | // Make read only if Accounts Settings doesn't allow stale rates |
tundebabzy | bc7c387 | 2017-11-28 18:10:05 +0100 | [diff] [blame] | 784 | this.frm.set_df_property("conversion_rate", "read_only", erpnext.stale_rate_allowed() ? 0 : 1); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 785 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 786 | |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 787 | shipping_rule: function() { |
| 788 | var me = this; |
| 789 | if(this.frm.doc.shipping_rule) { |
| 790 | return this.frm.call({ |
| 791 | doc: this.frm.doc, |
| 792 | method: "apply_shipping_rule", |
| 793 | callback: function(r) { |
| 794 | if(!r.exc) { |
| 795 | me.calculate_taxes_and_totals(); |
| 796 | } |
| 797 | } |
| 798 | }).fail(() => this.frm.set_value('shipping_rule', '')); |
| 799 | } |
Shreya | 39adc8f | 2018-01-22 13:08:05 +0530 | [diff] [blame] | 800 | else { |
| 801 | me.calculate_taxes_and_totals(); |
| 802 | } |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 803 | }, |
| 804 | |
Nabin Hait | a9c9b69 | 2017-08-31 19:40:09 +0530 | [diff] [blame] | 805 | set_actual_charges_based_on_currency: function() { |
| 806 | var me = this; |
| 807 | $.each(this.frm.doc.taxes || [], function(i, d) { |
| 808 | if(d.charge_type == "Actual") { |
| 809 | frappe.model.set_value(d.doctype, d.name, "tax_amount", |
| 810 | flt(d.tax_amount) / flt(me.frm.doc.conversion_rate)); |
| 811 | } |
| 812 | }); |
| 813 | }, |
| 814 | |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 815 | get_exchange_rate: function(transaction_date, from_currency, to_currency, callback) { |
Shreya | 3f77852 | 2018-05-15 16:59:20 +0530 | [diff] [blame] | 816 | var args; |
| 817 | if (["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"].includes(this.frm.doctype)) { |
| 818 | args = "for_selling"; |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 819 | } |
Shreya | 3f77852 | 2018-05-15 16:59:20 +0530 | [diff] [blame] | 820 | else if (["Purchase Order", "Purchase Receipt", "Purchase Invoice"].includes(this.frm.doctype)) { |
| 821 | args = "for_buying"; |
| 822 | } |
| 823 | |
Nabin Hait | 17179ee | 2017-05-06 13:23:12 +0530 | [diff] [blame] | 824 | if (!transaction_date || !from_currency || !to_currency) return; |
Nabin Hait | bdfb070 | 2015-10-28 15:38:08 +0530 | [diff] [blame] | 825 | return frappe.call({ |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 826 | method: "erpnext.setup.utils.get_exchange_rate", |
| 827 | args: { |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 828 | transaction_date: transaction_date, |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 829 | from_currency: from_currency, |
Shreya | 0db8506 | 2018-05-15 11:25:46 +0530 | [diff] [blame] | 830 | to_currency: to_currency, |
| 831 | args: args |
Rushabh Mehta | 66fa1ff | 2015-05-07 12:25:33 +0530 | [diff] [blame] | 832 | }, |
| 833 | callback: function(r) { |
| 834 | callback(flt(r.message)); |
| 835 | } |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 836 | }); |
| 837 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 838 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 839 | price_list_currency: function() { |
Akhilesh Darjee | db59ffb | 2013-09-11 13:05:24 +0530 | [diff] [blame] | 840 | var me=this; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 841 | this.set_dynamic_labels(); |
Nabin Hait | ec361ea | 2015-05-11 18:14:45 +0530 | [diff] [blame] | 842 | |
| 843 | var company_currency = this.get_company_currency(); |
| 844 | // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc |
| 845 | 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] | 846 | 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] | 847 | function(exchange_rate) { |
| 848 | me.frm.set_value("plc_conversion_rate", exchange_rate); |
| 849 | }); |
| 850 | } else { |
| 851 | this.plc_conversion_rate(); |
| 852 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 853 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 854 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 855 | plc_conversion_rate: function() { |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 856 | if(this.frm.doc.price_list_currency === this.get_company_currency()) { |
| 857 | this.frm.set_value("plc_conversion_rate", 1.0); |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 858 | } else if(this.frm.doc.price_list_currency === this.frm.doc.currency |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 859 | && 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] | 860 | cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 861 | this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate); |
Akhilesh Darjee | 564c621 | 2013-10-17 11:31:11 +0530 | [diff] [blame] | 862 | } |
Rushabh Mehta | 4a3c805 | 2015-05-19 11:10:12 +0530 | [diff] [blame] | 863 | |
Nabin Hait | ec361ea | 2015-05-11 18:14:45 +0530 | [diff] [blame] | 864 | if(!this.in_apply_price_list) { |
tundebabzy | 6e90f49 | 2018-02-12 10:48:57 +0100 | [diff] [blame] | 865 | this.apply_price_list(null, true); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 866 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 867 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 868 | |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 869 | uom: function(doc, cdt, cdn) { |
| 870 | var me = this; |
| 871 | var item = frappe.get_doc(cdt, cdn); |
| 872 | if(item.item_code && item.uom) { |
| 873 | return this.frm.call({ |
| 874 | method: "erpnext.stock.get_item_details.get_conversion_factor", |
| 875 | child: item, |
| 876 | args: { |
| 877 | item_code: item.item_code, |
| 878 | uom: item.uom |
| 879 | }, |
| 880 | callback: function(r) { |
| 881 | if(!r.exc) { |
| 882 | me.conversion_factor(me.frm.doc, cdt, cdn); |
| 883 | } |
| 884 | } |
| 885 | }); |
| 886 | } |
| 887 | }, |
| 888 | |
Nabin Hait | 3257aee | 2017-05-06 12:47:14 +0530 | [diff] [blame] | 889 | conversion_factor: function(doc, cdt, cdn, dont_fetch_price_list_rate) { |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 890 | if(doc.doctype != 'Material Request' && frappe.meta.get_docfield(cdt, "stock_qty", cdn)) { |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 891 | var item = frappe.get_doc(cdt, cdn); |
| 892 | frappe.model.round_floats_in(item, ["qty", "conversion_factor"]); |
| 893 | item.stock_qty = flt(item.qty * item.conversion_factor, precision("stock_qty", item)); |
Vishal Dhayagude | d377018 | 2017-12-14 18:06:02 +0530 | [diff] [blame] | 894 | item.total_weight = flt(item.stock_qty * item.weight_per_unit); |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 895 | refresh_field("stock_qty", item.name, item.parentfield); |
Vishal Dhayagude | d377018 | 2017-12-14 18:06:02 +0530 | [diff] [blame] | 896 | refresh_field("total_weight", item.name, item.parentfield); |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 897 | this.toggle_conversion_factor(item); |
Vishal Dhayagude | d377018 | 2017-12-14 18:06:02 +0530 | [diff] [blame] | 898 | this.calculate_net_weight(); |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 899 | if (!dont_fetch_price_list_rate && |
| 900 | frappe.meta.has_field(doc.doctype, "price_list_currency")) { |
| 901 | this.apply_price_list(item, true); |
| 902 | } |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 903 | } |
| 904 | }, |
| 905 | |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 906 | toggle_conversion_factor: function(item) { |
| 907 | // toggle read only property for conversion factor field if the uom and stock uom are same |
Rushabh Mehta | 3631102 | 2017-06-20 09:30:53 +0530 | [diff] [blame] | 908 | if(this.frm.get_field('items').grid.fields_map.conversion_factor) { |
| 909 | this.frm.fields_dict.items.grid.toggle_enable("conversion_factor", |
Javier Wong | 2469095 | 2017-12-29 16:36:47 +0800 | [diff] [blame] | 910 | ((item.uom != item.stock_uom) && !frappe.meta.get_docfield(cur_frm.fields_dict.items.grid.doctype, "conversion_factor").read_only)? true: false); |
Rushabh Mehta | 3631102 | 2017-06-20 09:30:53 +0530 | [diff] [blame] | 911 | } |
Vishal Dhayagude | d377018 | 2017-12-14 18:06:02 +0530 | [diff] [blame] | 912 | |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 913 | }, |
| 914 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 915 | qty: function(doc, cdt, cdn) { |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 916 | let item = frappe.get_doc(cdt, cdn); |
Nabin Hait | 3257aee | 2017-05-06 12:47:14 +0530 | [diff] [blame] | 917 | this.conversion_factor(doc, cdt, cdn, true); |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 918 | this.apply_pricing_rule(item, true); |
Vishal Dhayagude | d42242d | 2017-11-29 16:09:59 +0530 | [diff] [blame] | 919 | }, |
| 920 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 921 | service_stop_date: function(frm, cdt, cdn) { |
| 922 | var child = locals[cdt][cdn]; |
| 923 | |
| 924 | if(child.service_stop_date) { |
| 925 | let start_date = Date.parse(child.service_start_date); |
| 926 | let end_date = Date.parse(child.service_end_date); |
| 927 | let stop_date = Date.parse(child.service_stop_date); |
| 928 | |
| 929 | if(stop_date < start_date) { |
| 930 | frappe.model.set_value(cdt, cdn, "service_stop_date", ""); |
| 931 | frappe.throw(__("Service Stop Date cannot be before Service Start Date")); |
| 932 | } else if (stop_date > end_date) { |
| 933 | frappe.model.set_value(cdt, cdn, "service_stop_date", ""); |
| 934 | frappe.throw(__("Service Stop Date cannot be after Service End Date")); |
| 935 | } |
| 936 | } |
| 937 | }, |
| 938 | |
| 939 | service_start_date: function(frm, cdt, cdn) { |
| 940 | var child = locals[cdt][cdn]; |
| 941 | |
| 942 | if(child.service_start_date) { |
| 943 | frappe.call({ |
| 944 | "method": "erpnext.stock.get_item_details.calculate_service_end_date", |
| 945 | args: {"args": child}, |
| 946 | callback: function(r) { |
| 947 | frappe.model.set_value(cdt, cdn, "service_end_date", r.message.service_end_date); |
| 948 | } |
| 949 | }) |
| 950 | } |
| 951 | }, |
| 952 | |
Vishal Dhayagude | d377018 | 2017-12-14 18:06:02 +0530 | [diff] [blame] | 953 | calculate_net_weight: function(){ |
| 954 | /* Calculate Total Net Weight then further applied shipping rule to calculate shipping charges.*/ |
| 955 | var me = this; |
| 956 | this.frm.doc.total_net_weight= 0.0; |
| 957 | |
| 958 | $.each(this.frm.doc["items"] || [], function(i, item) { |
| 959 | me.frm.doc.total_net_weight += flt(item.total_weight); |
| 960 | }); |
| 961 | refresh_field("total_net_weight"); |
| 962 | this.shipping_rule(); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 963 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 964 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 965 | set_dynamic_labels: function() { |
| 966 | // What TODO? should we make price list system non-mandatory? |
| 967 | this.frm.toggle_reqd("plc_conversion_rate", |
| 968 | !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency)); |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 969 | |
Saif | 3c9155e | 2018-10-18 17:27:56 +0500 | [diff] [blame] | 970 | var company_currency = this.get_company_currency(); |
| 971 | this.change_form_labels(company_currency); |
| 972 | this.change_grid_labels(company_currency); |
| 973 | this.frm.refresh_fields(); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 974 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 975 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 976 | change_form_labels: function(company_currency) { |
| 977 | var me = this; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 978 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 979 | 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] | 980 | "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words", |
| 981 | "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] | 982 | "base_paid_amount", "base_write_off_amount", "base_change_amount", "base_operating_cost", |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 983 | "base_raw_material_cost", "base_total_cost", "base_scrap_material_cost", |
| 984 | "base_rounding_adjustment"], company_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 985 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 986 | 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] | 987 | "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted", |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 988 | "rounded_total", "in_words", "paid_amount", "write_off_amount", "operating_cost", |
| 989 | "scrap_material_cost", "rounding_adjustment", "raw_material_cost", |
| 990 | "total_cost"], this.frm.doc.currency); |
Anand Doshi | 87da662 | 2015-10-19 14:59:32 +0530 | [diff] [blame] | 991 | |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 992 | this.frm.set_currency_labels(["outstanding_amount", "total_advance"], |
| 993 | this.frm.doc.party_account_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 994 | |
| 995 | cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 996 | + " = [?] " + company_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 997 | |
| 998 | if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) { |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 999 | cur_frm.set_df_property("plc_conversion_rate", "description", "1 " |
| 1000 | + this.frm.doc.price_list_currency + " = [?] " + company_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | // toggle fields |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 1004 | this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", |
| 1005 | "base_total_taxes_and_charges", "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 1006 | "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount", |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 1007 | "base_paid_amount", "base_write_off_amount", "base_operating_cost", "base_raw_material_cost", |
| 1008 | "base_total_cost", "base_scrap_material_cost", "base_rounding_adjustment"], |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 1009 | this.frm.doc.currency != company_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 1010 | |
| 1011 | this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"], |
| 1012 | this.frm.doc.price_list_currency != company_currency); |
| 1013 | |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 1014 | var show = cint(cur_frm.doc.discount_amount) || |
Nabin Hait | 903c42a | 2015-02-24 15:24:49 +0530 | [diff] [blame] | 1015 | ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length); |
| 1016 | |
| 1017 | if(frappe.meta.get_docfield(cur_frm.doctype, "net_total")) |
| 1018 | cur_frm.toggle_display("net_total", show); |
| 1019 | |
| 1020 | if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total")) |
| 1021 | cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency))); |
| 1022 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 1023 | }, |
| 1024 | |
| 1025 | change_grid_labels: function(company_currency) { |
| 1026 | var me = this; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 1027 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 1028 | this.frm.set_currency_labels(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"], |
| 1029 | company_currency, "items"); |
| 1030 | |
| 1031 | this.frm.set_currency_labels(["rate", "net_rate", "price_list_rate", "amount", "net_amount"], |
| 1032 | this.frm.doc.currency, "items"); |
| 1033 | |
| 1034 | if(this.frm.fields_dict["operations"]) { |
| 1035 | this.frm.set_currency_labels(["operating_cost", "hour_rate"], this.frm.doc.currency, "operations"); |
| 1036 | this.frm.set_currency_labels(["base_operating_cost", "base_hour_rate"], company_currency, "operations"); |
| 1037 | |
| 1038 | var item_grid = this.frm.fields_dict["operations"].grid; |
| 1039 | $.each(["base_operating_cost", "base_hour_rate"], function(i, fname) { |
| 1040 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 1041 | item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 1042 | }); |
| 1043 | } |
| 1044 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 1045 | if(this.frm.fields_dict["scrap_items"]) { |
| 1046 | this.frm.set_currency_labels(["rate", "amount"], this.frm.doc.currency, "scrap_items"); |
| 1047 | 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] | 1048 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 1049 | var item_grid = this.frm.fields_dict["scrap_items"].grid; |
| 1050 | $.each(["base_rate", "base_amount"], function(i, fname) { |
| 1051 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 1052 | item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); |
| 1053 | }); |
| 1054 | } |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 1055 | |
| 1056 | if(this.frm.fields_dict["taxes"]) { |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 1057 | 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] | 1058 | |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 1059 | 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] | 1060 | } |
| 1061 | |
| 1062 | if(this.frm.fields_dict["advances"]) { |
Rohit Waghchaure | b1b3288 | 2016-10-21 19:31:54 +0530 | [diff] [blame] | 1063 | this.frm.set_currency_labels(["advance_amount", "allocated_amount"], |
Nabin Hait | aa01590 | 2015-10-16 13:08:33 +0530 | [diff] [blame] | 1064 | this.frm.doc.party_account_currency, "advances"); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | // toggle columns |
| 1068 | var item_grid = this.frm.fields_dict["items"].grid; |
| 1069 | $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) { |
| 1070 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 1071 | item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); |
| 1072 | }); |
| 1073 | |
| 1074 | var show = (cint(cur_frm.doc.discount_amount)) || |
| 1075 | ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length); |
| 1076 | |
| 1077 | $.each(["net_rate", "net_amount"], function(i, fname) { |
| 1078 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 1079 | item_grid.set_column_disp(fname, show); |
| 1080 | }); |
| 1081 | |
| 1082 | $.each(["base_net_rate", "base_net_amount"], function(i, fname) { |
| 1083 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
| 1084 | item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency))); |
| 1085 | }); |
| 1086 | |
| 1087 | // set labels |
| 1088 | var $wrapper = $(this.frm.wrapper); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 1089 | }, |
| 1090 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 1091 | recalculate: function() { |
| 1092 | this.calculate_taxes_and_totals(); |
| 1093 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 1094 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 1095 | recalculate_values: function() { |
| 1096 | this.calculate_taxes_and_totals(); |
| 1097 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 1098 | |
Anand Doshi | d0b0072 | 2013-05-28 18:54:48 +0530 | [diff] [blame] | 1099 | calculate_charges: function() { |
| 1100 | this.calculate_taxes_and_totals(); |
| 1101 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 1102 | |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1103 | ignore_pricing_rule: function() { |
Nabin Hait | 8c9fb76 | 2017-04-28 14:23:03 +0530 | [diff] [blame] | 1104 | if(this.frm.doc.ignore_pricing_rule) { |
| 1105 | var me = this; |
| 1106 | var item_list = []; |
Rushabh Mehta | 3631102 | 2017-06-20 09:30:53 +0530 | [diff] [blame] | 1107 | |
Nabin Hait | 8c9fb76 | 2017-04-28 14:23:03 +0530 | [diff] [blame] | 1108 | $.each(this.frm.doc["items"] || [], function(i, d) { |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1109 | if (d.item_code && !d.is_free_item) { |
Nabin Hait | 8c9fb76 | 2017-04-28 14:23:03 +0530 | [diff] [blame] | 1110 | item_list.push({ |
| 1111 | "doctype": d.doctype, |
| 1112 | "name": d.name, |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1113 | "item_code": d.item_code, |
| 1114 | "pricing_rules": d.pricing_rules, |
| 1115 | "parenttype": d.parenttype, |
| 1116 | "parent": d.parent |
Nabin Hait | 8c9fb76 | 2017-04-28 14:23:03 +0530 | [diff] [blame] | 1117 | }) |
| 1118 | } |
| 1119 | }); |
| 1120 | return this.frm.call({ |
| 1121 | method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.remove_pricing_rules", |
| 1122 | args: { item_list: item_list }, |
| 1123 | callback: function(r) { |
| 1124 | if (!r.exc && r.message) { |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1125 | r.message.forEach(row_item => { |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 1126 | me.validate_pricing_rule(row_item); |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1127 | }); |
Nabin Hait | 8c9fb76 | 2017-04-28 14:23:03 +0530 | [diff] [blame] | 1128 | me._set_values_for_item_list(r.message); |
| 1129 | me.calculate_taxes_and_totals(); |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 1130 | if(me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on"); |
Nabin Hait | 8c9fb76 | 2017-04-28 14:23:03 +0530 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | }); |
| 1134 | } else { |
| 1135 | this.apply_pricing_rule(); |
| 1136 | } |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1137 | }, |
| 1138 | |
| 1139 | apply_pricing_rule: function(item, calculate_taxes_and_totals) { |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 1140 | var me = this; |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 1141 | var args = this._get_args(item); |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1142 | if (!(args.items && args.items.length)) { |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 1143 | if(calculate_taxes_and_totals) me.calculate_taxes_and_totals(); |
| 1144 | return; |
| 1145 | } |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1146 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1147 | return this.frm.call({ |
| 1148 | method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule", |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 1149 | args: { args: args, doc: me.frm.doc }, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1150 | callback: function(r) { |
Anand Doshi | f214bfc | 2014-07-16 17:52:08 +0530 | [diff] [blame] | 1151 | if (!r.exc && r.message) { |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1152 | me._set_values_for_item_list(r.message); |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 1153 | if(item) me.set_gross_profit(item); |
Rohit Waghchaure | deaee7c | 2016-09-22 14:02:53 +0530 | [diff] [blame] | 1154 | 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] | 1155 | } |
| 1156 | } |
| 1157 | }); |
| 1158 | }, |
| 1159 | |
| 1160 | _get_args: function(item) { |
| 1161 | var me = this; |
| 1162 | return { |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1163 | "items": this._get_item_list(item), |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1164 | "customer": me.frm.doc.customer, |
| 1165 | "customer_group": me.frm.doc.customer_group, |
| 1166 | "territory": me.frm.doc.territory, |
| 1167 | "supplier": me.frm.doc.supplier, |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 1168 | "supplier_group": me.frm.doc.supplier_group, |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1169 | "currency": me.frm.doc.currency, |
| 1170 | "conversion_rate": me.frm.doc.conversion_rate, |
| 1171 | "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] | 1172 | "price_list_currency": me.frm.doc.price_list_currency, |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1173 | "plc_conversion_rate": me.frm.doc.plc_conversion_rate, |
| 1174 | "company": me.frm.doc.company, |
| 1175 | "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date, |
| 1176 | "campaign": me.frm.doc.campaign, |
| 1177 | "sales_partner": me.frm.doc.sales_partner, |
| 1178 | "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1179 | "doctype": me.frm.doc.doctype, |
Rohit Waghchaure | 5ecbaa4 | 2017-03-28 12:09:07 +0530 | [diff] [blame] | 1180 | "name": me.frm.doc.name, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 1181 | "is_return": cint(me.frm.doc.is_return), |
| 1182 | "update_stock": in_list(['Sales Invoice', 'Purchase Invoice'], me.frm.doc.doctype) ? cint(me.frm.doc.update_stock) : 0, |
rohitwaghchaure | c037dc7 | 2017-11-28 16:11:15 +0530 | [diff] [blame] | 1183 | "conversion_factor": me.frm.doc.conversion_factor, |
| 1184 | "pos_profile": me.frm.doc.doctype == 'Sales Invoice' ? me.frm.doc.pos_profile : '' |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1185 | }; |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1186 | }, |
| 1187 | |
| 1188 | _get_item_list: function(item) { |
| 1189 | var item_list = []; |
| 1190 | var append_item = function(d) { |
Anand Doshi | 39edcbc | 2014-07-28 16:00:19 +0530 | [diff] [blame] | 1191 | if (d.item_code) { |
| 1192 | item_list.push({ |
| 1193 | "doctype": d.doctype, |
| 1194 | "name": d.name, |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1195 | "child_docname": d.name, |
Anand Doshi | 39edcbc | 2014-07-28 16:00:19 +0530 | [diff] [blame] | 1196 | "item_code": d.item_code, |
| 1197 | "item_group": d.item_group, |
| 1198 | "brand": d.brand, |
Anand Doshi | 39a2891 | 2016-02-15 11:42:18 +0530 | [diff] [blame] | 1199 | "qty": d.qty, |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1200 | "stock_qty": d.stock_qty, |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 1201 | "uom": d.uom, |
Suraj Shetty | c87b47a | 2019-04-29 23:18:47 +0530 | [diff] [blame] | 1202 | "stock_uom": d.stock_uom, |
Anand Doshi | 39a2891 | 2016-02-15 11:42:18 +0530 | [diff] [blame] | 1203 | "parenttype": d.parenttype, |
Rohit Waghchaure | 43b5080 | 2016-10-24 14:04:46 +0530 | [diff] [blame] | 1204 | "parent": d.parent, |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1205 | "pricing_rules": d.pricing_rules, |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 1206 | "warehouse": d.warehouse, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 1207 | "serial_no": d.serial_no, |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1208 | "price_list_rate": d.price_list_rate, |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 1209 | "conversion_factor": d.conversion_factor || 1.0 |
Anand Doshi | 39edcbc | 2014-07-28 16:00:19 +0530 | [diff] [blame] | 1210 | }); |
mbauskar | c79415e | 2016-01-18 16:32:24 +0530 | [diff] [blame] | 1211 | |
| 1212 | // 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] | 1213 | if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){ |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 1214 | item_list[0]["margin_type"] = d.margin_type; |
| 1215 | item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount; |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 1216 | } |
Anand Doshi | 39edcbc | 2014-07-28 16:00:19 +0530 | [diff] [blame] | 1217 | } |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1218 | }; |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 1219 | |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1220 | if (item) { |
| 1221 | append_item(item); |
Nabin Hait | c1367d9 | 2014-05-30 11:43:00 +0530 | [diff] [blame] | 1222 | } else { |
Nabin Hait | 9633934 | 2015-01-21 17:22:11 +0530 | [diff] [blame] | 1223 | $.each(this.frm.doc["items"] || [], function(i, d) { |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1224 | append_item(d); |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 1225 | }); |
| 1226 | } |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 1227 | return item_list; |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 1228 | }, |
| 1229 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1230 | _set_values_for_item_list: function(children) { |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 1231 | var me = this; |
Nabin Hait | 7d8fa80 | 2014-12-30 15:35:45 +0530 | [diff] [blame] | 1232 | var price_list_rate_changed = false; |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 1233 | for(var i=0, l=children.length; i<l; i++) { |
| 1234 | var d = children[i]; |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1235 | var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rules"); |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 1236 | for(var k in d) { |
| 1237 | var v = d[k]; |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1238 | if (["doctype", "name"].indexOf(k)===-1) { |
Nabin Hait | 7d8fa80 | 2014-12-30 15:35:45 +0530 | [diff] [blame] | 1239 | if(k=="price_list_rate") { |
| 1240 | if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true; |
| 1241 | } |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 1242 | frappe.model.set_value(d.doctype, d.name, k, v); |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1243 | } |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 1244 | } |
| 1245 | |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 1246 | // if pricing rule set as blank from an existing value, apply price_list |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1247 | if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rules) { |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 1248 | me.apply_price_list(frappe.get_doc(d.doctype, d.name)); |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 1249 | } else { |
| 1250 | me.validate_pricing_rule(frappe.get_doc(d.doctype, d.name)); |
Nabin Hait | 49a2729 | 2014-12-17 11:21:32 +0530 | [diff] [blame] | 1251 | } |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 1252 | } |
Nabin Hait | 7d8fa80 | 2014-12-30 15:35:45 +0530 | [diff] [blame] | 1253 | |
Anand Doshi | dea3fb0 | 2015-02-05 15:05:58 +0530 | [diff] [blame] | 1254 | if(!price_list_rate_changed) me.calculate_taxes_and_totals(); |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1255 | }, |
| 1256 | |
tundebabzy | 6e90f49 | 2018-02-12 10:48:57 +0100 | [diff] [blame] | 1257 | apply_price_list: function(item, reset_plc_conversion) { |
| 1258 | // We need to reset plc_conversion_rate sometimes because the call to |
| 1259 | // `erpnext.stock.get_item_details.apply_price_list` is sensitive to its value |
| 1260 | if (!reset_plc_conversion) { |
| 1261 | this.frm.set_value("plc_conversion_rate", ""); |
| 1262 | } |
| 1263 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1264 | var me = this; |
Rushabh Mehta | 052bd62 | 2015-01-30 16:57:24 +0530 | [diff] [blame] | 1265 | var args = this._get_args(item); |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1266 | if (!((args.items && args.items.length) || args.price_list)) { |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 1267 | return; |
| 1268 | } |
Nabin Hait | 37b047d | 2015-02-23 16:01:33 +0530 | [diff] [blame] | 1269 | |
Rushabh Mehta | 98aa581 | 2017-11-14 09:32:32 +0530 | [diff] [blame] | 1270 | if (me.in_apply_price_list == true) return; |
| 1271 | |
| 1272 | me.in_apply_price_list = true; |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1273 | return this.frm.call({ |
| 1274 | method: "erpnext.stock.get_item_details.apply_price_list", |
Rushabh Mehta | 052bd62 | 2015-01-30 16:57:24 +0530 | [diff] [blame] | 1275 | args: { args: args }, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1276 | callback: function(r) { |
| 1277 | if (!r.exc) { |
Rushabh Mehta | 98aa581 | 2017-11-14 09:32:32 +0530 | [diff] [blame] | 1278 | frappe.run_serially([ |
| 1279 | () => me.frm.set_value("price_list_currency", r.message.parent.price_list_currency), |
| 1280 | () => me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate), |
| 1281 | () => { |
| 1282 | if(args.items.length) { |
| 1283 | me._set_values_for_item_list(r.message.children); |
| 1284 | } |
| 1285 | }, |
| 1286 | () => { me.in_apply_price_list = false; } |
| 1287 | ]); |
Nabin Hait | 37b047d | 2015-02-23 16:01:33 +0530 | [diff] [blame] | 1288 | |
Rushabh Mehta | 98aa581 | 2017-11-14 09:32:32 +0530 | [diff] [blame] | 1289 | } else { |
| 1290 | me.in_apply_price_list = false; |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1291 | } |
| 1292 | } |
Faris Ansari | 404f39d | 2018-05-14 15:46:45 +0530 | [diff] [blame] | 1293 | }).always(() => { |
| 1294 | me.in_apply_price_list = false; |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1295 | }); |
| 1296 | }, |
| 1297 | |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 1298 | validate_pricing_rule: function(item) { |
| 1299 | let me = this; |
| 1300 | const fields = ["discount_percentage", "discount_amount", "pricing_rules"]; |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1301 | |
| 1302 | if (item.pricing_rules) { |
| 1303 | frappe.call({ |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 1304 | method: "erpnext.accounts.doctype.pricing_rule.utils.validate_pricing_rule_for_different_cond", |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1305 | args: { |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 1306 | doc: me.frm.doc |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1307 | }, |
| 1308 | callback: function(r) { |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 1309 | if (r.message) { |
| 1310 | r.message.items.forEach(d => { |
| 1311 | me.frm.doc.items.forEach(row => { |
| 1312 | if(d.name == row.name) { |
| 1313 | fields.forEach(f => { |
| 1314 | row[f] = d[f]; |
| 1315 | }); |
| 1316 | } |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1317 | }); |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1318 | }); |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 1319 | |
| 1320 | me.trigger_price_list_rate(); |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | }); |
| 1324 | } else if(item.remove_free_item) { |
| 1325 | var items = []; |
| 1326 | |
| 1327 | me.frm.doc.items.forEach(d => { |
| 1328 | if(d.item_code != item.remove_free_item || !d.is_free_item) { |
| 1329 | items.push(d); |
| 1330 | } |
| 1331 | }); |
| 1332 | |
| 1333 | me.frm.doc.items = items; |
| 1334 | refresh_field('items'); |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 1335 | } else if(item.applied_on_items && item.apply_on) { |
| 1336 | const applied_on_items = item.applied_on_items.split(','); |
| 1337 | me.frm.doc.items.forEach(row => { |
| 1338 | if(applied_on_items.includes(row[item.apply_on])) { |
| 1339 | fields.forEach(f => { |
| 1340 | row[f] = 0; |
| 1341 | }); |
| 1342 | } |
| 1343 | }); |
| 1344 | |
| 1345 | me.trigger_price_list_rate(); |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1346 | } |
| 1347 | }, |
| 1348 | |
Rohit Waghchaure | e540534 | 2019-03-31 16:42:41 +0530 | [diff] [blame] | 1349 | trigger_price_list_rate: function() { |
| 1350 | var me = this; |
| 1351 | |
| 1352 | this.frm.doc.items.forEach(child_row => { |
| 1353 | me.frm.script_manager.trigger("price_list_rate", |
| 1354 | child_row.doctype, child_row.name); |
| 1355 | }) |
| 1356 | }, |
| 1357 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 1358 | validate_company_and_party: function() { |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 1359 | var me = this; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 1360 | var valid = true; |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 1361 | |
Nabin Hait | 0f23106 | 2014-02-20 11:27:47 +0530 | [diff] [blame] | 1362 | $.each(["company", "customer"], function(i, fieldname) { |
Nabin Hait | ae07f22 | 2017-06-02 13:57:08 +0530 | [diff] [blame] | 1363 | 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] | 1364 | if (!me.frm.doc[fieldname]) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1365 | frappe.msgprint(__("Please specify") + ": " + |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 1366 | 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] | 1367 | ". " + __("It is needed to fetch Item Details.")); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1368 | valid = false; |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 1369 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 1370 | } |
| 1371 | }); |
| 1372 | return valid; |
| 1373 | }, |
Rushabh Mehta | ff93802 | 2014-04-15 18:40:00 +0530 | [diff] [blame] | 1374 | |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 1375 | get_terms: function() { |
| 1376 | var me = this; |
Makarand Bauskar | 157c334 | 2017-05-26 21:32:33 +0530 | [diff] [blame] | 1377 | |
| 1378 | erpnext.utils.get_terms(this.frm.doc.tc_name, this.frm.doc, function(r) { |
| 1379 | if(!r.exc) { |
| 1380 | me.frm.set_value("terms", r.message); |
| 1381 | } |
| 1382 | }); |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 1383 | }, |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1384 | |
| 1385 | taxes_and_charges: function() { |
| 1386 | var me = this; |
| 1387 | if(this.frm.doc.taxes_and_charges) { |
| 1388 | return this.frm.call({ |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 1389 | method: "erpnext.controllers.accounts_controller.get_taxes_and_charges", |
| 1390 | args: { |
Nabin Hait | 03f3a8a | 2014-05-13 16:38:31 +0530 | [diff] [blame] | 1391 | "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges", |
| 1392 | this.frm.doc.name).options, |
Rohit Waghchaure | 72cc37e | 2019-04-11 18:35:15 +0530 | [diff] [blame] | 1393 | "master_name": this.frm.doc.taxes_and_charges |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 1394 | }, |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1395 | callback: function(r) { |
| 1396 | if(!r.exc) { |
Rohit Waghchaure | 72cc37e | 2019-04-11 18:35:15 +0530 | [diff] [blame] | 1397 | me.frm.set_value("taxes", r.message); |
| 1398 | |
| 1399 | if(me.frm.doc.shipping_rule) { |
| 1400 | me.frm.script_manager.trigger("shipping_rule"); |
| 1401 | } else { |
| 1402 | me.calculate_taxes_and_totals(); |
Rohan Bansal | 90972cf | 2019-03-11 14:48:26 +0530 | [diff] [blame] | 1403 | } |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1404 | } |
| 1405 | } |
| 1406 | }); |
Nabin Hait | bc83b9c | 2014-05-02 17:03:18 +0530 | [diff] [blame] | 1407 | } |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 1408 | }, |
| 1409 | |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 1410 | tax_category: function() { |
| 1411 | var me = this; |
Saif Ur Rehman | fd531a6 | 2018-12-29 01:49:11 +0500 | [diff] [blame] | 1412 | if(me.frm.updating_party_details) return; |
| 1413 | |
Saif Ur Rehman | e414a92 | 2018-12-28 04:59:42 +0500 | [diff] [blame] | 1414 | frappe.run_serially([ |
Saif Ur Rehman | eeead1d | 2018-12-29 02:25:00 +0500 | [diff] [blame] | 1415 | () => this.update_item_tax_map(), |
Saif Ur Rehman | e414a92 | 2018-12-28 04:59:42 +0500 | [diff] [blame] | 1416 | () => erpnext.utils.set_taxes(this.frm, "tax_category"), |
| 1417 | ]); |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 1418 | }, |
| 1419 | |
| 1420 | item_tax_template: function(doc, cdt, cdn) { |
| 1421 | var me = this; |
Saif Ur Rehman | fd531a6 | 2018-12-29 01:49:11 +0500 | [diff] [blame] | 1422 | if(me.frm.updating_party_details) return; |
| 1423 | |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 1424 | var item = frappe.get_doc(cdt, cdn); |
| 1425 | |
Saif Ur Rehman | aca374e | 2018-12-28 04:53:25 +0500 | [diff] [blame] | 1426 | if(item.item_tax_template) { |
| 1427 | return this.frm.call({ |
| 1428 | method: "erpnext.stock.get_item_details.get_item_tax_map", |
| 1429 | args: { |
Saif Ur Rehman | eeead1d | 2018-12-29 02:25:00 +0500 | [diff] [blame] | 1430 | company: me.frm.doc.company, |
Saif Ur Rehman | aca374e | 2018-12-28 04:53:25 +0500 | [diff] [blame] | 1431 | item_tax_template: item.item_tax_template, |
| 1432 | as_json: true |
| 1433 | }, |
| 1434 | callback: function(r) { |
| 1435 | if(!r.exc) { |
| 1436 | item.item_tax_rate = r.message; |
| 1437 | me.add_taxes_from_item_tax_template(item.item_tax_rate); |
| 1438 | me.calculate_taxes_and_totals(); |
| 1439 | } |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 1440 | } |
Saif Ur Rehman | aca374e | 2018-12-28 04:53:25 +0500 | [diff] [blame] | 1441 | }); |
| 1442 | } else { |
| 1443 | item.item_tax_rate = "{}"; |
| 1444 | me.calculate_taxes_and_totals(); |
| 1445 | } |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 1446 | }, |
| 1447 | |
Saif Ur Rehman | eeead1d | 2018-12-29 02:25:00 +0500 | [diff] [blame] | 1448 | update_item_tax_map: function() { |
| 1449 | var me = this; |
| 1450 | var item_codes = []; |
| 1451 | $.each(this.frm.doc.items || [], function(i, item) { |
| 1452 | if(item.item_code) { |
| 1453 | item_codes.push(item.item_code); |
| 1454 | } |
| 1455 | }); |
| 1456 | |
| 1457 | if(item_codes.length) { |
| 1458 | return this.frm.call({ |
| 1459 | method: "erpnext.stock.get_item_details.get_item_tax_info", |
| 1460 | args: { |
| 1461 | company: me.frm.doc.company, |
| 1462 | tax_category: cstr(me.frm.doc.tax_category), |
| 1463 | item_codes: item_codes |
| 1464 | }, |
| 1465 | callback: function(r) { |
| 1466 | if(!r.exc) { |
| 1467 | $.each(me.frm.doc.items || [], function(i, item) { |
| 1468 | if(item.item_code && r.message.hasOwnProperty(item.item_code)) { |
| 1469 | item.item_tax_template = r.message[item.item_code].item_tax_template; |
| 1470 | item.item_tax_rate = r.message[item.item_code].item_tax_rate; |
| 1471 | me.add_taxes_from_item_tax_template(item.item_tax_rate); |
| 1472 | } else { |
| 1473 | item.item_tax_template = ""; |
| 1474 | item.item_tax_rate = "{}"; |
| 1475 | } |
| 1476 | }); |
| 1477 | me.calculate_taxes_and_totals(); |
| 1478 | } |
| 1479 | } |
| 1480 | }); |
| 1481 | } |
| 1482 | }, |
| 1483 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 1484 | is_recurring: function() { |
| 1485 | // set default values for recurring documents |
Rushabh Mehta | 756e518 | 2016-03-03 13:00:17 +0530 | [diff] [blame] | 1486 | if(this.frm.doc.is_recurring && this.frm.doc.__islocal) { |
| 1487 | frappe.msgprint(__("Please set recurring after saving")); |
| 1488 | this.frm.set_value('is_recurring', 0); |
| 1489 | return; |
| 1490 | } |
| 1491 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 1492 | if(this.frm.doc.is_recurring) { |
Nabin Hait | 68a628e | 2016-03-21 12:03:16 +0530 | [diff] [blame] | 1493 | if(!this.frm.doc.recurring_id) { |
| 1494 | this.frm.set_value('recurring_id', this.frm.doc.name); |
| 1495 | } |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 1496 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 1497 | var owner_email = this.frm.doc.owner=="Administrator" |
| 1498 | ? frappe.user_info("Administrator").email |
| 1499 | : this.frm.doc.owner; |
| 1500 | |
| 1501 | this.frm.doc.notification_email_address = $.map([cstr(owner_email), |
| 1502 | cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", "); |
| 1503 | this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate(); |
| 1504 | } |
| 1505 | |
| 1506 | refresh_many(["notification_email_address", "repeat_on_day_of_month"]); |
| 1507 | }, |
| 1508 | |
| 1509 | from_date: function() { |
| 1510 | // set to_date |
| 1511 | if(this.frm.doc.from_date) { |
| 1512 | var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6, |
| 1513 | 'Yearly': 12}; |
| 1514 | |
| 1515 | var months = recurring_type_map[this.frm.doc.recurring_type]; |
| 1516 | if(months) { |
| 1517 | var to_date = frappe.datetime.add_months(this.frm.doc.from_date, |
| 1518 | months); |
| 1519 | this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1); |
| 1520 | refresh_field('to_date'); |
| 1521 | } |
| 1522 | } |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 1523 | }, |
Rushabh Mehta | bb8c6de | 2016-03-29 17:52:22 +0530 | [diff] [blame] | 1524 | |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 1525 | set_gross_profit: function(item) { |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 1526 | if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1527 | var rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1); |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 1528 | 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] | 1529 | } |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 1530 | }, |
| 1531 | |
| 1532 | setup_item_selector: function() { |
Rushabh Mehta | bc92824 | 2016-05-29 23:08:22 +0530 | [diff] [blame] | 1533 | // TODO: remove item selector |
| 1534 | |
| 1535 | return; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1536 | // if(!this.item_selector) { |
| 1537 | // this.item_selector = new erpnext.ItemSelector({frm: this.frm}); |
| 1538 | // } |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 1539 | }, |
Rushabh Mehta | 76b1341 | 2016-07-18 14:30:18 +0530 | [diff] [blame] | 1540 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 1541 | get_advances: function() { |
| 1542 | if(!this.frm.is_return) { |
| 1543 | return this.frm.call({ |
| 1544 | method: "set_advances", |
| 1545 | doc: this.frm.doc, |
| 1546 | callback: function(r, rt) { |
| 1547 | refresh_field("advances"); |
| 1548 | } |
| 1549 | }) |
| 1550 | } |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1551 | }, |
Rushabh Mehta | 76b1341 | 2016-07-18 14:30:18 +0530 | [diff] [blame] | 1552 | |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1553 | make_payment_entry: function() { |
| 1554 | return frappe.call({ |
Rohit Waghchaure | b7a5502 | 2016-10-05 16:24:41 +0530 | [diff] [blame] | 1555 | method: cur_frm.cscript.get_method_for_payment(), |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1556 | args: { |
| 1557 | "dt": cur_frm.doc.doctype, |
| 1558 | "dn": cur_frm.doc.name |
| 1559 | }, |
| 1560 | callback: function(r) { |
| 1561 | var doclist = frappe.model.sync(r.message); |
| 1562 | frappe.set_route("Form", doclist[0].doctype, doclist[0].name); |
Nabin Hait | 56548cb | 2016-07-01 15:58:39 +0530 | [diff] [blame] | 1563 | // cur_frm.refresh_fields() |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 1564 | } |
| 1565 | }); |
Rohit Waghchaure | b7a5502 | 2016-10-05 16:24:41 +0530 | [diff] [blame] | 1566 | }, |
| 1567 | |
| 1568 | get_method_for_payment: function(){ |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 1569 | var method = "erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry"; |
Rohit Waghchaure | 7d52989 | 2016-10-06 14:35:04 +0530 | [diff] [blame] | 1570 | 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] | 1571 | if(in_list(['Sales Invoice', 'Purchase Invoice'], cur_frm.doc.doctype)){ |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 1572 | method = "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_invoice"; |
Rohit Waghchaure | b7a5502 | 2016-10-05 16:24:41 +0530 | [diff] [blame] | 1573 | }else { |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 1574 | method= "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_order"; |
Rohit Waghchaure | b7a5502 | 2016-10-05 16:24:41 +0530 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | return method |
Manas Solanki | cc90241 | 2016-11-10 19:15:11 +0530 | [diff] [blame] | 1579 | }, |
Makarand Bauskar | c196d74 | 2017-06-02 17:28:40 +0530 | [diff] [blame] | 1580 | |
| 1581 | set_query_for_batch: function(doc, cdt, cdn) { |
| 1582 | // Show item's batches in the dropdown of batch no |
| 1583 | |
| 1584 | var me = this; |
| 1585 | var item = frappe.get_doc(cdt, cdn); |
| 1586 | |
| 1587 | if(!item.item_code) { |
| 1588 | frappe.throw(__("Please enter Item Code to get batch no")); |
Rushabh Mehta | 3631102 | 2017-06-20 09:30:53 +0530 | [diff] [blame] | 1589 | } else if (doc.doctype == "Purchase Receipt" || |
Makarand Bauskar | c196d74 | 2017-06-02 17:28:40 +0530 | [diff] [blame] | 1590 | (doc.doctype == "Purchase Invoice" && doc.update_stock)) { |
| 1591 | |
| 1592 | return { |
| 1593 | filters: {'item': item.item_code} |
| 1594 | } |
| 1595 | } else { |
Rushabh Mehta | 98aa581 | 2017-11-14 09:32:32 +0530 | [diff] [blame] | 1596 | let filters = { |
Makarand Bauskar | c196d74 | 2017-06-02 17:28:40 +0530 | [diff] [blame] | 1597 | 'item_code': item.item_code, |
| 1598 | 'posting_date': me.frm.doc.posting_date || frappe.datetime.nowdate(), |
| 1599 | } |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 1600 | if (item.warehouse) filters["warehouse"] = item.warehouse; |
Makarand Bauskar | c196d74 | 2017-06-02 17:28:40 +0530 | [diff] [blame] | 1601 | |
KanchanChauhan | 0fe455a | 2017-11-17 20:08:15 +0530 | [diff] [blame] | 1602 | return { |
Makarand Bauskar | c196d74 | 2017-06-02 17:28:40 +0530 | [diff] [blame] | 1603 | query : "erpnext.controllers.queries.get_batch_no", |
| 1604 | filters: filters |
| 1605 | } |
| 1606 | } |
| 1607 | }, |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1608 | |
| 1609 | payment_terms_template: function() { |
| 1610 | var me = this; |
tundebabzy | 8f44d1c | 2018-01-05 08:16:50 +0100 | [diff] [blame] | 1611 | const doc = this.frm.doc; |
| 1612 | if(doc.payment_terms_template && doc.doctype !== 'Delivery Note') { |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1613 | var posting_date = doc.posting_date || doc.transaction_date; |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1614 | frappe.call({ |
| 1615 | method: "erpnext.controllers.accounts_controller.get_payment_terms", |
| 1616 | args: { |
tundebabzy | 8f44d1c | 2018-01-05 08:16:50 +0100 | [diff] [blame] | 1617 | terms_template: doc.payment_terms_template, |
rohitwaghchaure | da941af | 2018-01-17 16:23:04 +0530 | [diff] [blame] | 1618 | posting_date: posting_date, |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1619 | grand_total: doc.rounded_total || doc.grand_total, |
| 1620 | bill_date: doc.bill_date |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1621 | }, |
| 1622 | callback: function(r) { |
| 1623 | if(r.message && !r.exc) { |
| 1624 | me.frm.set_value("payment_schedule", r.message); |
| 1625 | } |
| 1626 | } |
| 1627 | }) |
| 1628 | } |
| 1629 | }, |
| 1630 | |
| 1631 | payment_term: function(doc, cdt, cdn) { |
| 1632 | var row = locals[cdt][cdn]; |
| 1633 | if(row.payment_term) { |
| 1634 | frappe.call({ |
| 1635 | method: "erpnext.controllers.accounts_controller.get_payment_term_details", |
| 1636 | args: { |
| 1637 | term: row.payment_term, |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1638 | bill_date: this.frm.doc.bill_date, |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1639 | posting_date: this.frm.doc.posting_date || this.frm.doc.transaction_date, |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 1640 | grand_total: this.frm.doc.rounded_total || this.frm.doc.grand_total |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1641 | }, |
| 1642 | callback: function(r) { |
| 1643 | if(r.message && !r.exc) { |
| 1644 | for (var d in r.message) { |
| 1645 | frappe.model.set_value(cdt, cdn, d, r.message[d]); |
| 1646 | } |
| 1647 | } |
| 1648 | } |
| 1649 | }) |
| 1650 | } |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 1651 | }, |
| 1652 | |
| 1653 | blanket_order: function(doc, cdt, cdn) { |
| 1654 | var me = this; |
| 1655 | var item = locals[cdt][cdn]; |
| 1656 | if (item.blanket_order && (item.parenttype=="Sales Order" || item.parenttype=="Purchase Order")) { |
| 1657 | frappe.call({ |
| 1658 | method: "erpnext.stock.get_item_details.get_blanket_order_details", |
| 1659 | args: { |
| 1660 | args:{ |
| 1661 | item_code: item.item_code, |
| 1662 | customer: doc.customer, |
| 1663 | supplier: doc.supplier, |
| 1664 | company: doc.company, |
| 1665 | transaction_date: doc.transaction_date, |
| 1666 | blanket_order: item.blanket_order |
| 1667 | } |
| 1668 | }, |
| 1669 | callback: function(r) { |
| 1670 | if (!r.message) { |
Manas Solanki | 2df1b31 | 2018-05-29 12:56:14 +0530 | [diff] [blame] | 1671 | frappe.throw(__("Invalid Blanket Order for the selected Customer and Item")); |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 1672 | } else { |
| 1673 | frappe.run_serially([ |
| 1674 | () => frappe.model.set_value(cdt, cdn, "blanket_order_rate", r.message.blanket_order_rate), |
| 1675 | () => me.frm.script_manager.trigger("price_list_rate", cdt, cdn) |
| 1676 | ]); |
| 1677 | } |
| 1678 | } |
| 1679 | }) |
| 1680 | } |
Saif | 60ec88b | 2018-11-13 12:07:53 +0500 | [diff] [blame] | 1681 | }, |
| 1682 | |
| 1683 | set_warehouse: function() { |
| 1684 | var me = this; |
| 1685 | if(this.frm.doc.set_warehouse) { |
| 1686 | $.each(this.frm.doc.items || [], function(i, item) { |
| 1687 | frappe.model.set_value(me.frm.doctype + " Item", item.name, "warehouse", me.frm.doc.set_warehouse); |
| 1688 | }); |
| 1689 | } |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1690 | } |
Nabin Hait | 6e74079 | 2017-05-06 12:45:16 +0530 | [diff] [blame] | 1691 | }); |
pratu16x7 | ae450fc | 2017-06-26 15:31:46 +0530 | [diff] [blame] | 1692 | |
rohitwaghchaure | 2b95a54 | 2017-12-14 16:06:23 +0530 | [diff] [blame] | 1693 | erpnext.show_serial_batch_selector = function(frm, d, callback, on_close, show_dialog) { |
pratu16x7 | ae450fc | 2017-06-26 15:31:46 +0530 | [diff] [blame] | 1694 | frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function() { |
pratu16x7 | 89d001c | 2017-06-26 17:45:49 +0530 | [diff] [blame] | 1695 | new erpnext.SerialNoBatchSelector({ |
pratu16x7 | ae450fc | 2017-06-26 15:31:46 +0530 | [diff] [blame] | 1696 | frm: frm, |
| 1697 | item: d, |
| 1698 | warehouse_details: { |
| 1699 | type: "Warehouse", |
| 1700 | name: d.warehouse |
| 1701 | }, |
rohitwaghchaure | 2b95a54 | 2017-12-14 16:06:23 +0530 | [diff] [blame] | 1702 | callback: callback, |
| 1703 | on_close: on_close |
Rohit Waghchaure | 6ab6303 | 2017-09-01 15:28:44 +0530 | [diff] [blame] | 1704 | }, show_dialog); |
pratu16x7 | ae450fc | 2017-06-26 15:31:46 +0530 | [diff] [blame] | 1705 | }); |
| 1706 | } |