Skip to content

Commit

Permalink
refactor: stock closing balance
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure committed Dec 10, 2024
1 parent f2783fb commit 65839f3
Show file tree
Hide file tree
Showing 20 changed files with 1,021 additions and 380 deletions.
3 changes: 2 additions & 1 deletion erpnext/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,5 @@ erpnext.patches.v14_0.update_currency_exchange_settings_for_frankfurter
erpnext.patches.v15_0.migrate_old_item_wise_tax_detail_data_format
erpnext.patches.v15_0.set_is_exchange_gain_loss_in_payment_entry_deductions
erpnext.patches.v14_0.update_stock_uom_in_work_order_item
erpnext.patches.v15_0.enable_allow_existing_serial_no
erpnext.patches.v15_0.enable_allow_existing_serial_no
erpnext.patches.v15_0.refactor_closing_stock_balance #5
60 changes: 60 additions & 0 deletions erpnext/patches/v15_0/refactor_closing_stock_balance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import frappe
from frappe import _
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields

from erpnext.stock.doctype.inventory_dimension.inventory_dimension import (
field_exists,
get_inventory_dimensions,
)


def execute():
add_inventory_dimensions_to_stock_closing_balance()
create_stock_closing_entries()


def add_inventory_dimensions_to_stock_closing_balance():
inventory_dimensions = get_inventory_dimensions()

dimension_fields_list = []
for inv_dim in inventory_dimensions:
if not frappe.db.get_value(
"Custom Field", {"dt": "Stock Closing Balance", "fieldname": inv_dim.fieldname}
) and not field_exists("Stock Closing Balance", inv_dim.fieldname):
dimension_field = frappe._dict()
dimension_field["mandatory_depends_on"] = ""
dimension_field["reqd"] = 0
dimension_field["fieldname"] = inv_dim.fieldname
dimension_field["label"] = inv_dim.dimension_name
dimension_field["fieldtype"] = "Link"
dimension_field["options"] = inv_dim.doctype
dimension_field["read_only"] = 1
dimension_field["insert_after"] = "inventory_dimension_section"
dimension_field["search_index"] = 1
dimension_fields_list.append(dimension_field)

if dimension_fields_list:
dimension_fields_list.insert(
0,
{
"label": _("Inventory Dimension"),
"fieldtype": "Section Break",
"fieldname": "inventory_dimension_section",
"insert_after": "stock_uom",
},
)
create_custom_fields({"Stock Closing Balance": dimension_fields_list})


def create_stock_closing_entries():
for row in frappe.get_all(
"Closing Stock Balance",
fields=["company", "status", "from_date", "to_date"],
filters={"docstatus": 1},
group_by="company",
order_by="creation desc",
):
new_entry = frappe.new_doc("Stock Closing Entry")
new_entry.update(row)
new_entry.save(ignore_permissions=True)
new_entry.submit()
154 changes: 0 additions & 154 deletions erpnext/stock/doctype/closing_stock_balance/closing_stock_balance.py

This file was deleted.

This file was deleted.

25 changes: 13 additions & 12 deletions erpnext/stock/doctype/inventory_dimension/inventory_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,17 @@ def add_custom_fields(self):
self.add_transfer_field(self.document_type, dimension_fields)
custom_fields.setdefault(self.document_type, dimension_fields)

if (
dimension_fields
and not frappe.db.get_value(
"Custom Field", {"dt": "Stock Ledger Entry", "fieldname": self.target_fieldname}
)
and not field_exists("Stock Ledger Entry", self.target_fieldname)
):
dimension_field = dimension_fields[1]
dimension_field["mandatory_depends_on"] = ""
dimension_field["reqd"] = 0
dimension_field["fieldname"] = self.target_fieldname
custom_fields["Stock Ledger Entry"] = dimension_field
for dt in ["Stock Ledger Entry", "Stock Closing Balance"]:
if (
dimension_fields
and not frappe.db.get_value("Custom Field", {"dt": dt, "fieldname": self.target_fieldname})
and not field_exists(dt, self.target_fieldname)
):
dimension_field = dimension_fields[1]
dimension_field["mandatory_depends_on"] = ""
dimension_field["reqd"] = 0
dimension_field["fieldname"] = self.target_fieldname
custom_fields[dt] = dimension_field

filter_custom_fields = {}
if custom_fields:
Expand Down Expand Up @@ -390,8 +389,10 @@ def get_inventory_dimensions():
"distinct target_fieldname as fieldname",
"reference_document as doctype",
"validate_negative_stock",
"name as dimension_name",
],
filters={"disabled": 0},
order_by="creation",
)

frappe.local.inventory_dimensions = dimensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ def validate_period_closing_voucher(self):
]
)

# Closing Stock Balance
# Stock Closing Balance
closing_stock = self.get_closing_stock_balance()
if closing_stock and closing_stock[0].name:
name = get_link_to_form("Closing Stock Balance", closing_stock[0].name)
name = get_link_to_form("Stock Closing Balance", closing_stock[0].name)
to_date = frappe.format(closing_stock[0].to_date, "Date")
msg = f"Due to closing stock balance {name}, you cannot repost item valuation before {to_date}"
frappe.throw(_(msg))
frappe.throw(
_("Due to stock closing balance {0}, you cannot repost item valuation before {1}").format(
name, to_date
)
)

def get_closing_stock_balance(self):
filters = {
Expand All @@ -117,7 +120,7 @@ def get_closing_stock_balance(self):
if self.get(field):
filters.update({field: ("in", ["", self.get(field)])})

return frappe.get_all("Closing Stock Balance", fields=["name", "to_date"], filters=filters)
return frappe.get_all("Stock Closing Balance", fields=["name", "posting_date"], filters=filters, limit=1)

@staticmethod
def get_max_period_closing_date(company):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def test_repost_item_valuation_for_closing_stock_balance(self):
prepare_closing_stock_balance,
)

doc = frappe.new_doc("Closing Stock Balance")
doc = frappe.new_doc("Stock Closing Entry")
doc.company = "_Test Company"
doc.from_date = today()
doc.to_date = today()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt

// frappe.ui.form.on("Stock Closing Balance", {
// refresh(frm) {

// },
// });
Loading

0 comments on commit 65839f3

Please sign in to comment.