Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | // License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | frappe.provide("erpnext.buying"); |
| 5 | |
| 6 | cur_frm.cscript.tax_table = "Purchase Taxes and Charges"; |
| 7 | |
| 8 | {% include 'erpnext/accounts/doctype/purchase_taxes_and_charges_template/purchase_taxes_and_charges_template.js' %} |
| 9 | |
| 10 | cur_frm.email_field = "contact_email"; |
| 11 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 12 | erpnext.buying.BuyingController = class BuyingController extends erpnext.TransactionController { |
| 13 | setup() { |
| 14 | super.setup(); |
| 15 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 16 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 17 | onload(doc, cdt, cdn) { |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 18 | this.setup_queries(doc, cdt, cdn); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 19 | super.onload(); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 20 | |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 21 | this.frm.set_query('shipping_rule', function() { |
| 22 | return { |
| 23 | filters: { |
| 24 | "shipping_rule_type": "Buying" |
| 25 | } |
| 26 | }; |
| 27 | }); |
| 28 | |
rohitwaghchaure | 8356d4b | 2018-08-09 15:51:11 +0530 | [diff] [blame] | 29 | if (this.frm.doc.__islocal |
| 30 | && frappe.meta.has_field(this.frm.doc.doctype, "disable_rounded_total")) { |
Nabin Hait | a39f324 | 2019-01-21 18:50:50 +0530 | [diff] [blame] | 31 | |
| 32 | var df = frappe.meta.get_docfield(this.frm.doc.doctype, "disable_rounded_total"); |
marination | ca6dbad | 2019-12-11 12:10:05 +0530 | [diff] [blame] | 33 | var disable = cint(df.default) || cint(frappe.sys_defaults.disable_rounded_total); |
Nabin Hait | a39f324 | 2019-01-21 18:50:50 +0530 | [diff] [blame] | 34 | this.frm.set_value("disable_rounded_total", disable); |
rohitwaghchaure | ae4ff5a | 2018-08-01 18:09:51 +0530 | [diff] [blame] | 35 | } |
| 36 | |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 37 | /* eslint-disable */ |
| 38 | // no idea where me is coming from |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 39 | if(this.frm.get_field('shipping_address')) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 40 | this.frm.set_query("shipping_address", function() { |
| 41 | if(me.frm.doc.customer) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 42 | return { |
KanchanChauhan | 1dc26b1 | 2017-06-13 15:26:35 +0530 | [diff] [blame] | 43 | query: 'frappe.contacts.doctype.address.address.address_query', |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 44 | filters: { link_doctype: 'Customer', link_name: me.frm.doc.customer } |
| 45 | }; |
| 46 | } else |
| 47 | return erpnext.queries.company_address_query(me.frm.doc) |
| 48 | }); |
| 49 | } |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 50 | /* eslint-enable */ |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 51 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 52 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 53 | setup_queries(doc, cdt, cdn) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 54 | var me = this; |
| 55 | |
| 56 | if(this.frm.fields_dict.buying_price_list) { |
| 57 | this.frm.set_query("buying_price_list", function() { |
| 58 | return{ |
| 59 | filters: { 'buying': 1 } |
| 60 | } |
| 61 | }); |
| 62 | } |
| 63 | |
karthikeyan5 | 7fc6021 | 2019-07-04 22:46:16 +0530 | [diff] [blame] | 64 | if(this.frm.fields_dict.tc_name) { |
| 65 | this.frm.set_query("tc_name", function() { |
| 66 | return{ |
| 67 | filters: { 'buying': 1 } |
| 68 | } |
| 69 | }); |
| 70 | } |
| 71 | |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 72 | me.frm.set_query('supplier', erpnext.queries.supplier); |
| 73 | me.frm.set_query('contact_person', erpnext.queries.contact_query); |
| 74 | me.frm.set_query('supplier_address', erpnext.queries.address_query); |
| 75 | |
Deepesh Garg | 6760077 | 2020-06-13 22:40:23 +0530 | [diff] [blame] | 76 | me.frm.set_query('billing_address', erpnext.queries.company_address_query); |
Deepesh Garg | 3f376cc | 2022-06-02 13:57:54 +0530 | [diff] [blame] | 77 | erpnext.accounts.dimensions.setup_dimension_filters(me.frm, me.frm.doctype); |
Deepesh Garg | 6760077 | 2020-06-13 22:40:23 +0530 | [diff] [blame] | 78 | |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 79 | if(this.frm.fields_dict.supplier) { |
| 80 | this.frm.set_query("supplier", function() { |
| 81 | return{ query: "erpnext.controllers.queries.supplier_query" }}); |
| 82 | } |
| 83 | |
| 84 | this.frm.set_query("item_code", "items", function() { |
Sagar Sharma | d074c93 | 2022-03-31 19:57:42 +0530 | [diff] [blame] | 85 | if (me.frm.doc.is_subcontracted) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 86 | return{ |
| 87 | query: "erpnext.controllers.queries.item_query", |
noahjacob | ca2fb47 | 2021-05-12 16:25:07 +0530 | [diff] [blame] | 88 | filters:{ 'supplier': me.frm.doc.supplier, 'is_sub_contracted_item': 1 } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 89 | } |
Doridel Cahanap | 2b14d6a | 2018-11-13 14:37:16 +0800 | [diff] [blame] | 90 | } |
Doridel Cahanap | 2b14d6a | 2018-11-13 14:37:16 +0800 | [diff] [blame] | 91 | else { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 92 | return{ |
| 93 | query: "erpnext.controllers.queries.item_query", |
Ankush Menat | 0e875f5 | 2022-05-16 12:14:07 +0530 | [diff] [blame] | 94 | filters: { 'supplier': me.frm.doc.supplier, 'is_purchase_item': 1, 'has_variants': 0} |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | }); |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 98 | |
| 99 | |
| 100 | this.frm.set_query("manufacturer", "items", function(doc, cdt, cdn) { |
| 101 | const row = locals[cdt][cdn]; |
| 102 | return { |
| 103 | query: "erpnext.controllers.queries.item_manufacturer_query", |
| 104 | filters:{ 'item_code': row.item_code } |
| 105 | } |
| 106 | }); |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 107 | |
| 108 | if(this.frm.fields_dict["items"].grid.get_field('item_code')) { |
| 109 | this.frm.set_query("item_tax_template", "items", function(doc, cdt, cdn) { |
| 110 | return me.set_query_for_item_tax_template(doc, cdt, cdn) |
| 111 | }); |
| 112 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 113 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 114 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 115 | refresh(doc) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 116 | frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'}; |
| 117 | |
| 118 | this.frm.toggle_display("supplier_name", |
| 119 | (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier)); |
| 120 | |
| 121 | if(this.frm.doc.docstatus==0 && |
| 122 | (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) { |
| 123 | this.set_from_product_bundle(); |
| 124 | } |
| 125 | |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 126 | this.toggle_subcontracting_fields(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 127 | super.refresh(); |
Rohit Waghchaure | f0dfea1 | 2021-06-20 10:20:35 +0530 | [diff] [blame] | 128 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 129 | |
Rohit Waghchaure | f0dfea1 | 2021-06-20 10:20:35 +0530 | [diff] [blame] | 130 | toggle_subcontracting_fields() { |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 131 | if (in_list(['Purchase Receipt', 'Purchase Invoice'], this.frm.doc.doctype)) { |
| 132 | this.frm.fields_dict.supplied_items.grid.update_docfield_property('consumed_qty', |
| 133 | 'read_only', this.frm.doc.__onload && this.frm.doc.__onload.backflush_based_on === 'BOM'); |
| 134 | |
| 135 | this.frm.set_df_property('supplied_items', 'cannot_add_rows', 1); |
| 136 | this.frm.set_df_property('supplied_items', 'cannot_delete_rows', 1); |
| 137 | } |
Rohit Waghchaure | f0dfea1 | 2021-06-20 10:20:35 +0530 | [diff] [blame] | 138 | } |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 139 | |
Rohit Waghchaure | f0dfea1 | 2021-06-20 10:20:35 +0530 | [diff] [blame] | 140 | supplier() { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 141 | var me = this; |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 142 | erpnext.utils.get_party_details(this.frm, null, null, function(){ |
| 143 | me.apply_price_list(); |
| 144 | }); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 145 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 146 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 147 | supplier_address() { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 148 | erpnext.utils.get_address_display(this.frm); |
Saif Ur Rehman | fd531a6 | 2018-12-29 01:49:11 +0500 | [diff] [blame] | 149 | erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address"); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 150 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 151 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 152 | buying_price_list() { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 153 | this.apply_price_list(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 154 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 155 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 156 | discount_percentage(doc, cdt, cdn) { |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 157 | var item = frappe.get_doc(cdt, cdn); |
| 158 | item.discount_amount = 0.0; |
| 159 | this.price_list_rate(doc, cdt, cdn); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 160 | } |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 161 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 162 | discount_amount(doc, cdt, cdn) { |
Nabin Hait | 593242f | 2019-04-05 19:35:02 +0530 | [diff] [blame] | 163 | var item = frappe.get_doc(cdt, cdn); |
| 164 | item.discount_percentage = 0.0; |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 165 | this.price_list_rate(doc, cdt, cdn); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 166 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 167 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 168 | qty(doc, cdt, cdn) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 169 | if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) { |
marination | 5fb5a75 | 2021-11-08 13:22:57 +0530 | [diff] [blame] | 170 | this.calculate_received_qty(doc, cdt, cdn) |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 171 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 172 | super.qty(doc, cdt, cdn); |
| 173 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 174 | |
marination | 5fb5a75 | 2021-11-08 13:22:57 +0530 | [diff] [blame] | 175 | rejected_qty(doc, cdt, cdn) { |
| 176 | this.calculate_received_qty(doc, cdt, cdn) |
| 177 | } |
| 178 | |
| 179 | calculate_received_qty(doc, cdt, cdn){ |
| 180 | var item = frappe.get_doc(cdt, cdn); |
| 181 | frappe.model.round_floats_in(item, ["qty", "rejected_qty"]); |
| 182 | |
| 183 | if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "rejected_qty"])){ return } |
| 184 | |
| 185 | let received_qty = flt(item.qty + item.rejected_qty, precision("received_qty", item)); |
| 186 | let received_stock_qty = flt(item.conversion_factor, precision("conversion_factor", item)) * flt(received_qty); |
| 187 | |
| 188 | frappe.model.set_value(cdt, cdn, "received_qty", received_qty); |
| 189 | frappe.model.set_value(cdt, cdn, "received_stock_qty", received_stock_qty); |
| 190 | } |
| 191 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 192 | batch_no(doc, cdt, cdn) { |
| 193 | super.batch_no(doc, cdt, cdn); |
| 194 | } |
Deepesh Garg | bd9bdc5 | 2021-01-28 12:05:57 +0530 | [diff] [blame] | 195 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 196 | validate_negative_quantity(cdt, cdn, item, fieldnames){ |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 197 | if(!item || !fieldnames) { return } |
| 198 | |
| 199 | var is_negative_qty = false; |
| 200 | for(var i = 0; i<fieldnames.length; i++) { |
| 201 | if(item[fieldnames[i]] < 0){ |
Mohammad Hasnain Mohsin Rajan | 90e33e5 | 2020-11-25 15:37:54 +0530 | [diff] [blame] | 202 | frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}", [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code])); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 203 | is_negative_qty = true; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return is_negative_qty |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 209 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 210 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 211 | warehouse(doc, cdt, cdn) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 212 | var item = frappe.get_doc(cdt, cdn); |
| 213 | if(item.item_code && item.warehouse) { |
| 214 | return this.frm.call({ |
| 215 | method: "erpnext.stock.get_item_details.get_bin_details", |
| 216 | child: item, |
| 217 | args: { |
| 218 | item_code: item.item_code, |
Anupam | 4980dd6 | 2021-03-15 11:37:46 +0530 | [diff] [blame] | 219 | warehouse: item.warehouse, |
| 220 | company: doc.company |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 221 | } |
| 222 | }); |
| 223 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 224 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 225 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 226 | project(doc, cdt, cdn) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 227 | var item = frappe.get_doc(cdt, cdn); |
| 228 | if(item.project) { |
| 229 | $.each(this.frm.doc["items"] || [], |
| 230 | function(i, other_item) { |
| 231 | if(!other_item.project) { |
| 232 | other_item.project = item.project; |
| 233 | refresh_field("project", other_item.name, other_item.parentfield); |
| 234 | } |
| 235 | }); |
| 236 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 237 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 238 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 239 | rejected_warehouse(doc, cdt) { |
marination | 8a30cb2 | 2020-04-23 00:18:21 +0530 | [diff] [blame] | 240 | // trigger autofill_warehouse only if parent rejected_warehouse field is triggered |
| 241 | if (["Purchase Invoice", "Purchase Receipt"].includes(cdt)) { |
| 242 | this.autofill_warehouse(doc.items, "rejected_warehouse", doc.rejected_warehouse); |
| 243 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 244 | } |
marination | 8a30cb2 | 2020-04-23 00:18:21 +0530 | [diff] [blame] | 245 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 246 | category(doc, cdt, cdn) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 247 | // should be the category field of tax table |
| 248 | if(cdt != doc.doctype) { |
| 249 | this.calculate_taxes_and_totals(); |
| 250 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 251 | } |
| 252 | add_deduct_tax(doc, cdt, cdn) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 253 | this.calculate_taxes_and_totals(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 254 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 255 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 256 | set_from_product_bundle() { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 257 | var me = this; |
| 258 | this.frm.add_custom_button(__("Product Bundle"), function() { |
| 259 | erpnext.buying.get_items_from_product_bundle(me.frm); |
marination | e14fdf4 | 2020-10-30 17:06:00 +0530 | [diff] [blame] | 260 | }, __("Get Items From")); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 261 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 262 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 263 | shipping_address(){ |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 264 | var me = this; |
| 265 | erpnext.utils.get_address_display(this.frm, "shipping_address", |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 266 | "shipping_address_display", true); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 267 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 268 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 269 | billing_address() { |
Deepesh Garg | 6760077 | 2020-06-13 22:40:23 +0530 | [diff] [blame] | 270 | erpnext.utils.get_address_display(this.frm, "billing_address", |
| 271 | "billing_address_display", true); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 272 | } |
Deepesh Garg | 6760077 | 2020-06-13 22:40:23 +0530 | [diff] [blame] | 273 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 274 | tc_name() { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 275 | this.get_terms(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 276 | } |
Charles-Henri Decultot | 8b22310 | 2018-06-28 10:57:58 +0200 | [diff] [blame] | 277 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 278 | update_auto_repeat_reference(doc) { |
Charles-Henri Decultot | 8b22310 | 2018-06-28 10:57:58 +0200 | [diff] [blame] | 279 | if (doc.auto_repeat) { |
| 280 | frappe.call({ |
Rucha Mahabal | 65a627c | 2019-07-17 13:50:32 +0530 | [diff] [blame] | 281 | method:"frappe.automation.doctype.auto_repeat.auto_repeat.update_reference", |
Nabin Hait | a39f324 | 2019-01-21 18:50:50 +0530 | [diff] [blame] | 282 | args:{ |
Charles-Henri Decultot | 8b22310 | 2018-06-28 10:57:58 +0200 | [diff] [blame] | 283 | docname: doc.auto_repeat, |
| 284 | reference:doc.name |
| 285 | }, |
| 286 | callback: function(r){ |
| 287 | if (r.message=="success") { |
| 288 | frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'}); |
| 289 | } else { |
| 290 | frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'}); |
| 291 | } |
| 292 | } |
| 293 | }) |
| 294 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 295 | } |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 296 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 297 | manufacturer(doc, cdt, cdn) { |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 298 | const row = locals[cdt][cdn]; |
| 299 | |
| 300 | if(row.manufacturer) { |
| 301 | frappe.call({ |
| 302 | method: "erpnext.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no", |
| 303 | args: { |
| 304 | 'item_code': row.item_code, |
| 305 | 'manufacturer': row.manufacturer |
| 306 | }, |
| 307 | callback: function(r) { |
| 308 | if (r.message) { |
| 309 | frappe.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message); |
| 310 | } |
| 311 | } |
| 312 | }); |
| 313 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 314 | } |
marination | 3e7a8ab | 2020-04-07 21:00:57 +0530 | [diff] [blame] | 315 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 316 | manufacturer_part_no(doc, cdt, cdn) { |
marination | 3e7a8ab | 2020-04-07 21:00:57 +0530 | [diff] [blame] | 317 | const row = locals[cdt][cdn]; |
| 318 | |
| 319 | if (row.manufacturer_part_no) { |
| 320 | frappe.model.get_value('Item Manufacturer', |
| 321 | { |
| 322 | 'item_code': row.item_code, |
| 323 | 'manufacturer': row.manufacturer, |
| 324 | 'manufacturer_part_no': row.manufacturer_part_no |
| 325 | }, |
| 326 | 'name', |
| 327 | function(data) { |
| 328 | if (!data) { |
| 329 | let msg = { |
| 330 | message: __("Manufacturer Part Number <b>{0}</b> is invalid", [row.manufacturer_part_no]), |
| 331 | title: __("Invalid Part Number") |
| 332 | } |
| 333 | frappe.throw(msg); |
| 334 | } |
| 335 | }); |
| 336 | |
| 337 | } |
| 338 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 339 | }; |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 340 | |
| 341 | cur_frm.add_fetch('project', 'cost_center', 'cost_center'); |
| 342 | |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 343 | erpnext.buying.link_to_mrs = function(frm) { |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 344 | frappe.call({ |
| 345 | method: "erpnext.buying.utils.get_linked_material_requests", |
| 346 | args:{ |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 347 | items: frm.doc.items.map((item) => item.item_code) |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 348 | }, |
| 349 | callback: function(r) { |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 350 | if (!r.message || r.message.length == 0) { |
| 351 | frappe.throw({ |
| 352 | message: __("No pending Material Requests found to link for the given items."), |
| 353 | title: __("Note") |
| 354 | }); |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 355 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 356 | |
| 357 | var item_length = frm.doc.items.length; |
| 358 | for (let item of frm.doc.items) { |
| 359 | var qty = item.qty; |
| 360 | (r.message[0] || []).forEach(function(d) { |
| 361 | if (d.qty > 0 && qty > 0 && item.item_code == d.item_code && !item.material_request_item) |
| 362 | { |
| 363 | item.material_request = d.mr_name; |
| 364 | item.material_request_item = d.mr_item; |
| 365 | var my_qty = Math.min(qty, d.qty); |
| 366 | qty = qty - my_qty; |
| 367 | d.qty = d.qty - my_qty; |
| 368 | item.stock_qty = my_qty*item.conversion_factor; |
| 369 | item.qty = my_qty; |
| 370 | |
| 371 | frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + item.idx + ")"); |
| 372 | if (qty > 0) |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 373 | { |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 374 | frappe.msgprint("Splitting " + qty + " units of " + d.item_code); |
| 375 | var newrow = frappe.model.add_child(frm.doc, item.doctype, "items"); |
| 376 | item_length++; |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 377 | |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 378 | for (var key in item) |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 379 | { |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 380 | newrow[key] = item[key]; |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 381 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 382 | |
| 383 | newrow.idx = item_length; |
| 384 | newrow["stock_qty"] = newrow.conversion_factor*qty; |
| 385 | newrow["qty"] = qty; |
| 386 | |
| 387 | newrow["material_request"] = ""; |
| 388 | newrow["material_request_item"] = ""; |
| 389 | |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 390 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 391 | } |
| 392 | }); |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 393 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 394 | refresh_field("items"); |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 395 | } |
| 396 | }); |
| 397 | } |
| 398 | |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 399 | erpnext.buying.get_default_bom = function(frm) { |
| 400 | $.each(frm.doc["items"] || [], function(i, d) { |
| 401 | if (d.item_code && d.bom === "") { |
| 402 | return frappe.call({ |
| 403 | type: "GET", |
| 404 | method: "erpnext.stock.get_item_details.get_default_bom", |
| 405 | args: { |
| 406 | "item_code": d.item_code, |
| 407 | }, |
| 408 | callback: function(r) { |
| 409 | if(r) { |
| 410 | frappe.model.set_value(d.doctype, d.name, "bom", r.message); |
| 411 | } |
| 412 | } |
| 413 | }) |
| 414 | } |
| 415 | }); |
| 416 | } |
| 417 | |
| 418 | erpnext.buying.get_items_from_product_bundle = function(frm) { |
| 419 | var dialog = new frappe.ui.Dialog({ |
| 420 | title: __("Get Items from Product Bundle"), |
| 421 | fields: [ |
| 422 | { |
| 423 | "fieldtype": "Link", |
| 424 | "label": __("Product Bundle"), |
| 425 | "fieldname": "product_bundle", |
| 426 | "options":"Product Bundle", |
| 427 | "reqd": 1 |
| 428 | }, |
| 429 | { |
| 430 | "fieldtype": "Currency", |
| 431 | "label": __("Quantity"), |
| 432 | "fieldname": "quantity", |
| 433 | "reqd": 1, |
| 434 | "default": 1 |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 435 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 436 | ], |
| 437 | primary_action_label: 'Get Items', |
| 438 | primary_action(args){ |
| 439 | if(!args) return; |
| 440 | dialog.hide(); |
| 441 | return frappe.call({ |
| 442 | type: "GET", |
| 443 | method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle", |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 444 | args: { |
marination | 4c677ea | 2022-01-25 23:52:52 +0530 | [diff] [blame] | 445 | row: { |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 446 | item_code: args.product_bundle, |
| 447 | quantity: args.quantity, |
| 448 | parenttype: frm.doc.doctype, |
| 449 | parent: frm.doc.name, |
| 450 | supplier: frm.doc.supplier, |
| 451 | currency: frm.doc.currency, |
| 452 | conversion_rate: frm.doc.conversion_rate, |
| 453 | price_list: frm.doc.buying_price_list, |
| 454 | price_list_currency: frm.doc.price_list_currency, |
| 455 | plc_conversion_rate: frm.doc.plc_conversion_rate, |
| 456 | company: frm.doc.company, |
| 457 | is_subcontracted: frm.doc.is_subcontracted, |
| 458 | transaction_date: frm.doc.transaction_date || frm.doc.posting_date, |
| 459 | ignore_pricing_rule: frm.doc.ignore_pricing_rule, |
| 460 | doctype: frm.doc.doctype |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 461 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 462 | }, |
| 463 | freeze: true, |
| 464 | callback: function(r) { |
| 465 | const first_row_is_empty = function(child_table){ |
| 466 | if($.isArray(child_table) && child_table.length > 0) { |
| 467 | return !child_table[0].item_code; |
| 468 | } |
| 469 | return false; |
| 470 | }; |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 471 | |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 472 | const remove_empty_first_row = function(frm){ |
| 473 | if (first_row_is_empty(frm.doc.items)){ |
| 474 | frm.doc.items = frm.doc.items.splice(1); |
| 475 | } |
| 476 | }; |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 477 | |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 478 | if(!r.exc && r.message) { |
| 479 | remove_empty_first_row(frm); |
Afshan | eb4b2aa | 2020-09-01 15:01:32 +0530 | [diff] [blame] | 480 | for (var i=0; i< r.message.length; i++) { |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 481 | var d = frm.add_child("items"); |
| 482 | var item = r.message[i]; |
Afshan | eb4b2aa | 2020-09-01 15:01:32 +0530 | [diff] [blame] | 483 | for (var key in item) { |
| 484 | if (!is_null(item[key]) && key !== "doctype") { |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 485 | d[key] = item[key]; |
| 486 | } |
| 487 | } |
| 488 | if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) { |
| 489 | frm.script_manager.trigger("price_list_rate", d.doctype, d.name); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 490 | } |
| 491 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 492 | frm.refresh_field("items"); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 493 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 494 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 495 | }) |
| 496 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 497 | }); |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 498 | |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 499 | dialog.show(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 500 | } |