From 0e68da5a2ab2855635cd292ea478d727a0ab7601 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 22 Jun 2023 15:43:32 +0530 Subject: [PATCH] feat: Show stock ledger preview --- .../doctype/payment_entry/payment_entry.js | 1 + .../purchase_invoice/purchase_invoice.js | 4 +- .../doctype/sales_invoice/sales_invoice.js | 7 +- erpnext/controllers/stock_controller.py | 34 +++++++-- .../public/js/controllers/stock_controller.js | 65 ---------------- erpnext/public/js/erpnext.bundle.js | 1 + erpnext/public/js/utils/ledger_preview.js | 76 +++++++++++++++++++ .../doctype/delivery_note/delivery_note.js | 3 + .../purchase_receipt/purchase_receipt.js | 4 + 9 files changed, 119 insertions(+), 76 deletions(-) create mode 100644 erpnext/public/js/utils/ledger_preview.js diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.js b/erpnext/accounts/doctype/payment_entry/payment_entry.js index 9f55ba1167..bac84db231 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.js +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.js @@ -155,6 +155,7 @@ frappe.ui.form.on('Payment Entry', { frm.events.hide_unhide_fields(frm); frm.events.set_dynamic_labels(frm); frm.events.show_general_ledger(frm); + erpnext.accounts.ledger_preview.show_accounting_ledger_preview(frm); }, validate_company: (frm) => { diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js index ab7884d520..6a558ca606 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -54,9 +54,11 @@ erpnext.accounts.PurchaseInvoice = class PurchaseInvoice extends erpnext.buying. hide_fields(this.frm.doc); // Show / Hide button this.show_general_ledger(); + erpnext.accounts.ledger_preview.show_accounting_ledger_preview(this.frm); - if(doc.update_stock==1 && doc.docstatus==1) { + if(doc.update_stock==1) { this.show_stock_ledger(); + erpnext.accounts.ledger_preview.show_stock_ledger_preview(this.frm); } if(!doc.is_return && doc.docstatus == 1 && doc.outstanding_amount != 0){ diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 1ef0c51cba..68407e0221 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -88,9 +88,12 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends e } this.show_general_ledger(); - this.show_ledger_preview(); + erpnext.accounts.ledger_preview.show_accounting_ledger_preview(this.frm); - if(doc.update_stock) this.show_stock_ledger(); + if(doc.update_stock){ + this.show_stock_ledger(); + erpnext.accounts.ledger_preview.show_stock_ledger_preview(this.frm); + } if (doc.docstatus == 1 && doc.outstanding_amount!=0 && !(cint(doc.is_return) && doc.return_against)) { diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index 0b057bc748..903ef658e3 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -846,20 +846,31 @@ class StockController(AccountsController): @frappe.whitelist() -def show_ledger_preview(company, doctype, docname): - frappe.db.savepoint("show_ledger_preview") +def show_accounting_ledger_preview(company, doctype, docname): + frappe.db.savepoint("show_accounting_ledger_preview") + + filters = {"company": company, "include_dimensions": 1} + doc = frappe.get_doc(doctype, docname) + + gl_columns, gl_data = get_accounting_ledger_preview(doc, filters) + + frappe.db.rollback(save_point="show_accounting_ledger_preview") + + return {"gl_columns": gl_columns, "gl_data": gl_data} + + +@frappe.whitelist() +def show_stock_ledger_preview(company, doctype, docname): + frappe.db.savepoint("show_stock_ledger_preview") filters = {"company": company} doc = frappe.get_doc(doctype, docname) sl_columns, sl_data = get_stock_ledger_preview(doc, filters) - gl_columns, gl_data = get_accounting_ledger_preview(doc, filters) - frappe.db.rollback(save_point="show_ledger_preview") + frappe.db.rollback(save_point="show_stock_ledger_preview") return { - "gl_columns": gl_columns, - "gl_data": gl_data, "sl_columns": sl_columns, "sl_data": sl_data, } @@ -877,11 +888,16 @@ def get_accounting_ledger_preview(doc, filters): "against", "party", "party_type", + "cost_center", "against_voucher_type", "against_voucher", ] doc.docstatus = 1 + + if doc.get("update_stock") or doc.doctype in ("Purchase Receipt", "Delivery Note"): + doc.update_stock_ledger() + doc.make_gl_entries() columns = get_gl_columns(filters) gl_entries = get_gl_entries_for_preview(doc.doctype, doc.name, fields) @@ -915,12 +931,12 @@ def get_stock_ledger_preview(doc, filters): "qty_after_transaction", "warehouse", "incoming_rate", - "valuation_rate", + "in_out_rate", "stock_value", "stock_value_difference", ] - if doc.update_stock or doc.doctype in ("Purchase Receipt", "Delivery Note"): + if doc.get("update_stock") or doc.doctype in ("Purchase Receipt", "Delivery Note"): doc.docstatus = 1 doc.update_stock_ledger() columns = get_sl_columns(filters) @@ -945,6 +961,8 @@ def get_sl_entries_for_preview(doctype, docname, fields): entry["out_qty"] = abs(entry.actual_qty) entry["in_qty"] = 0 + entry["in_out_rate"] = entry["valuation_rate"] + return sl_entries diff --git a/erpnext/public/js/controllers/stock_controller.js b/erpnext/public/js/controllers/stock_controller.js index ff593485be..720423b0a4 100644 --- a/erpnext/public/js/controllers/stock_controller.js +++ b/erpnext/public/js/controllers/stock_controller.js @@ -81,69 +81,4 @@ erpnext.stock.StockController = class StockController extends frappe.ui.form.Con }, __("View")); } } - - show_ledger_preview() { - let me = this - if(this.frm.doc.docstatus == 0) { - cur_frm.add_custom_button(__('Accounting Ledger Preview'), function() { - frappe.call({ - "method": "erpnext.controllers.stock_controller.show_ledger_preview", - "args": { - "company": me.frm.doc.company, - "doctype": me.frm.doc.doctype, - "docname": me.frm.doc.name - }, - "callback": function(response) { - me.make_dialog(response.message); - } - }) - }, __("View")); - } - } - - make_dialog(data) { - let me = this; - let gl_columns = data.gl_columns; - let gl_data = data.gl_data; - let sl_columns = data.sl_columns; - let sl_data = data.sl_data; - - let dialog = new frappe.ui.Dialog({ - "size": "extra-large", - "title": __("Ledger Preview"), - "fields": [ - { - "fieldtype": "HTML", - "fieldname": "accounting_ledger_preview_html", - "label": __("Accounting Ledger"), - }, - { - "fieldtype": "HTML", - "fieldname": "stock_ledger_preview_html", - "label": __("Stock Ledger"), - } - ] - }); - - setTimeout(function() { - me.get_datatable(gl_columns, gl_data, dialog.get_field("accounting_ledger_preview_html").wrapper); - }, 200); - - dialog.show(); - } - - get_datatable(columns, data, wrapper) { - const datatable_options = { - columns: columns, - data: data, - dynamicRowHeight: true, - checkboxColumn: false, - inlineFilters: true, - }; - - new frappe.DataTable( - wrapper, - datatable_options - ); - } }; diff --git a/erpnext/public/js/erpnext.bundle.js b/erpnext/public/js/erpnext.bundle.js index cc020fc2f1..4e028e4c31 100644 --- a/erpnext/public/js/erpnext.bundle.js +++ b/erpnext/public/js/erpnext.bundle.js @@ -17,6 +17,7 @@ import "./utils/customer_quick_entry"; import "./utils/supplier_quick_entry"; import "./call_popup/call_popup"; import "./utils/dimension_tree_filter"; +import "./utils/ledger_preview.js" import "./utils/barcode_scanner"; import "./telephony"; import "./templates/call_link.html"; diff --git a/erpnext/public/js/utils/ledger_preview.js b/erpnext/public/js/utils/ledger_preview.js new file mode 100644 index 0000000000..d1ee67b9c2 --- /dev/null +++ b/erpnext/public/js/utils/ledger_preview.js @@ -0,0 +1,76 @@ +frappe.provide('erpnext.accounts'); + +erpnext.accounts.ledger_preview = { + show_accounting_ledger_preview(frm) { + let me = this; + if(!frm.is_new() && frm.doc.docstatus == 0) { + frm.add_custom_button(__('Accounting Ledger'), function() { + frappe.call({ + "method": "erpnext.controllers.stock_controller.show_accounting_ledger_preview", + "args": { + "company": frm.doc.company, + "doctype": frm.doc.doctype, + "docname": frm.doc.name + }, + "callback": function(response) { + me.make_dialog("Accounting Ledger Preview", "accounting_ledger_preview_html", response.message.gl_columns, response.message.gl_data); + } + }) + }, __("Preview")); + } + }, + + show_stock_ledger_preview(frm) { + let me = this + if(!frm.is_new() && frm.doc.docstatus == 0) { + frm.add_custom_button(__('Stock Ledger'), function() { + frappe.call({ + "method": "erpnext.controllers.stock_controller.show_stock_ledger_preview", + "args": { + "company": frm.doc.company, + "doctype": frm.doc.doctype, + "docname": frm.doc.name + }, + "callback": function(response) { + me.make_dialog("Stock Ledger Preview", "stock_ledger_preview_html", response.message.sl_columns, response.message.sl_data); + } + }) + }, __("Preview")); + } + }, + + make_dialog(label, fieldname, columns, data) { + let me = this; + let dialog = new frappe.ui.Dialog({ + "size": "extra-large", + "title": __(label), + "fields": [ + { + "fieldtype": "HTML", + "fieldname": fieldname, + }, + ] + }); + + setTimeout(function() { + me.get_datatable(columns, data, dialog.get_field(fieldname).wrapper); + }, 200); + + dialog.show(); + }, + + get_datatable(columns, data, wrapper) { + const datatable_options = { + columns: columns, + data: data, + dynamicRowHeight: true, + checkboxColumn: false, + inlineFilters: true, + }; + + new frappe.DataTable( + wrapper, + datatable_options + ); + } +} \ No newline at end of file diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.js b/erpnext/stock/doctype/delivery_note/delivery_note.js index 77545e0e1a..a648195933 100644 --- a/erpnext/stock/doctype/delivery_note/delivery_note.js +++ b/erpnext/stock/doctype/delivery_note/delivery_note.js @@ -200,6 +200,9 @@ erpnext.stock.DeliveryNoteController = class DeliveryNoteController extends erpn } } + erpnext.accounts.ledger_preview.show_accounting_ledger_preview(this.frm); + erpnext.accounts.ledger_preview.show_stock_ledger_preview(this.frm); + if (doc.docstatus > 0) { this.show_stock_ledger(); if (erpnext.is_perpetual_inventory_enabled(doc.company)) { diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js index 312c166f8b..685209c02e 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js @@ -121,6 +121,10 @@ erpnext.stock.PurchaseReceiptController = class PurchaseReceiptController extend refresh() { var me = this; super.refresh(); + + erpnext.accounts.ledger_preview.show_accounting_ledger_preview(this.frm); + erpnext.accounts.ledger_preview.show_stock_ledger_preview(this.frm); + if(this.frm.doc.docstatus > 0) { this.show_stock_ledger(); //removed for temporary