diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js index 2d5cbb9e6c3a..c78db5b86d6c 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -561,11 +561,12 @@ frappe.ui.form.on("Purchase Invoice", { frm.custom_make_buttons = { "Purchase Invoice": "Return / Debit Note", "Payment Entry": "Payment", - "Landed Cost Voucher": function () { - frm.trigger("create_landed_cost_voucher"); - }, }; + if (frm.doc.update_stock) { + frm.custom_make_buttons["Landed Cost Voucher"] = "Landed Cost Voucher"; + } + frm.set_query("additional_discount_account", function () { return { filters: { @@ -607,20 +608,6 @@ frappe.ui.form.on("Purchase Invoice", { }); }, - create_landed_cost_voucher: function (frm) { - let lcv = frappe.model.get_new_doc("Landed Cost Voucher"); - lcv.company = frm.doc.company; - - let lcv_receipt = frappe.model.get_new_doc("Landed Cost Purchase Invoice"); - lcv_receipt.receipt_document_type = "Purchase Invoice"; - lcv_receipt.receipt_document = frm.doc.name; - lcv_receipt.supplier = frm.doc.supplier; - lcv_receipt.grand_total = frm.doc.grand_total; - lcv.purchase_receipts = [lcv_receipt]; - - frappe.set_route("Form", lcv.doctype, lcv.name); - }, - add_custom_buttons: function (frm) { if (frm.doc.docstatus == 1 && frm.doc.per_received < 100) { frm.add_custom_button( @@ -645,6 +632,32 @@ frappe.ui.form.on("Purchase Invoice", { __("View") ); } + + if (frm.doc.docstatus === 1 && frm.doc.update_stock) { + frm.add_custom_button( + __("Landed Cost Voucher"), + () => { + frm.events.make_lcv(frm); + }, + __("Create") + ); + } + }, + + make_lcv(frm) { + frappe.call({ + method: "erpnext.stock.doctype.purchase_receipt.purchase_receipt.make_lcv", + args: { + doctype: frm.doc.doctype, + docname: frm.doc.name, + }, + callback: (r) => { + if (r.message) { + var doc = frappe.model.sync(r.message); + frappe.set_route("Form", doc[0].doctype, doc[0].name); + } + }, + }); }, onload: function (frm) { diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js index bfac4381a060..bcecf8be14de 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js @@ -11,25 +11,10 @@ erpnext.buying.setup_buying_controller(); frappe.ui.form.on("Purchase Receipt", { setup: (frm) => { - frm.make_methods = { - "Landed Cost Voucher": () => { - let lcv = frappe.model.get_new_doc("Landed Cost Voucher"); - lcv.company = frm.doc.company; - - let lcv_receipt = frappe.model.get_new_doc("Landed Cost Purchase Receipt"); - lcv_receipt.receipt_document_type = "Purchase Receipt"; - lcv_receipt.receipt_document = frm.doc.name; - lcv_receipt.supplier = frm.doc.supplier; - lcv_receipt.grand_total = frm.doc.grand_total; - lcv.purchase_receipts = [lcv_receipt]; - - frappe.set_route("Form", lcv.doctype, lcv.name); - }, - }; - frm.custom_make_buttons = { "Stock Entry": "Return", "Purchase Invoice": "Purchase Invoice", + "Landed Cost Voucher": "Landed Cost Voucher", }; frm.set_query("expense_account", "items", function () { @@ -114,9 +99,35 @@ frappe.ui.form.on("Purchase Receipt", { } } + if (frm.doc.docstatus === 1) { + frm.add_custom_button( + __("Landed Cost Voucher"), + () => { + frm.events.make_lcv(frm); + }, + __("Create") + ); + } + frm.events.add_custom_buttons(frm); }, + make_lcv(frm) { + frappe.call({ + method: "erpnext.stock.doctype.purchase_receipt.purchase_receipt.make_lcv", + args: { + doctype: frm.doc.doctype, + docname: frm.doc.name, + }, + callback: (r) => { + if (r.message) { + var doc = frappe.model.sync(r.message); + frappe.set_route("Form", doc[0].doctype, doc[0].name); + } + }, + }); + }, + add_custom_buttons: function (frm) { if (frm.doc.docstatus == 0) { frm.add_custom_button( diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 8b046203eeee..228bc35693b1 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -1370,3 +1370,26 @@ def get_item_account_wise_additional_cost(purchase_document): @erpnext.allow_regional def update_regional_gl_entries(gl_list, doc): return + + +@frappe.whitelist() +def make_lcv(doctype, docname): + landed_cost_voucher = frappe.new_doc("Landed Cost Voucher") + + details = frappe.db.get_value(doctype, docname, ["supplier", "company", "base_grand_total"], as_dict=1) + + landed_cost_voucher.company = details.company + + landed_cost_voucher.append( + "purchase_receipts", + { + "receipt_document_type": doctype, + "receipt_document": docname, + "grand_total": details.base_grand_total, + "supplier": details.supplier, + }, + ) + + landed_cost_voucher.get_items_from_purchase_receipts() + + return landed_cost_voucher.as_dict()