Skip to content

Commit

Permalink
feat: validate repost item valuation against accounts freeze date
Browse files Browse the repository at this point in the history
(cherry picked from commit 61f0513)

# Conflicts:
#	erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py
  • Loading branch information
rtdany10 authored and mergify[bot] committed May 1, 2023
1 parent d3c769c commit a852dc1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from frappe import _
from frappe.exceptions import QueryDeadlockError, QueryTimeoutError
from frappe.model.document import Document
from frappe.utils import cint, get_link_to_form, get_weekday, now, nowtime
from frappe.utils import cint, get_link_to_form, get_weekday, getdate, now, nowtime
from frappe.utils.user import get_users_with_role
from rq.timeouts import JobTimeoutException

Expand All @@ -25,6 +25,21 @@ def validate(self):
self.set_status(write=False)
self.reset_field_values()
self.set_company()
self.validate_accounts_freeze()

def validate_accounts_freeze(self):
acc_settings = frappe.db.get_value(
'Accounts Settings',
'Accounts Settings',
['acc_frozen_upto', 'frozen_accounts_modifier'],
as_dict=1
)
if not acc_settings.acc_frozen_upto:
return
if acc_settings.frozen_accounts_modifier and self.owner in get_users_with_role(acc_settings.frozen_accounts_modifier):
return
if getdate(self.posting_date) <= getdate(acc_settings.acc_frozen_upto):
frappe.throw(_("You cannot repost item valuation before {}").format(acc_settings.acc_frozen_upto))

def reset_field_values(self):
if self.based_on == "Transaction":
Expand Down Expand Up @@ -235,7 +250,7 @@ def _get_directly_dependent_vouchers(doc):
def notify_error_to_stock_managers(doc, traceback):
recipients = get_users_with_role("Stock Manager")
if not recipients:
get_users_with_role("System Manager")
recipients = get_users_with_role("System Manager")

subject = _("Error while reposting item valuation")
message = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,83 @@ def test_gl_complete_gl_reposting(self):
[{"credit": 50, "debit": 0}],
gle_filters={"account": "Stock In Hand - TCP1"},
)
<<<<<<< HEAD
=======

def test_duplicate_ple_on_repost(self):
from erpnext.accounts import utils

# lower numbers to simplify test
orig_chunk_size = utils.GL_REPOSTING_CHUNK
utils.GL_REPOSTING_CHUNK = 2
self.addCleanup(setattr, utils, "GL_REPOSTING_CHUNK", orig_chunk_size)

rate = 100
item = self.make_item()
item.valuation_rate = 90
item.allow_negative_stock = 1
item.save()

company = "_Test Company with perpetual inventory"

# consume non-existing stock
sinv = create_sales_invoice(
company=company,
posting_date=today(),
debit_to="Debtors - TCP1",
income_account="Sales - TCP1",
expense_account="Cost of Goods Sold - TCP1",
warehouse="Stores - TCP1",
update_stock=1,
currency="INR",
item_code=item.name,
cost_center="Main - TCP1",
qty=1,
rate=rate,
)

# backdated receipt triggers repost
make_stock_entry(
item=item.name,
company=company,
qty=5,
rate=rate,
target="Stores - TCP1",
posting_date=add_to_date(today(), days=-1),
)

ple_entries = frappe.db.get_list(
"Payment Ledger Entry",
filters={"voucher_type": sinv.doctype, "voucher_no": sinv.name, "delinked": 0},
)

# assert successful deduplication on PLE
self.assertEqual(len(ple_entries), 1)

# outstanding should not be affected
sinv.reload()
self.assertEqual(sinv.outstanding_amount, 100)

def test_account_freeze_validation(self):
today = nowdate()

riv = frappe.get_doc(
doctype="Repost Item Valuation",
item_code="_Test Item",
warehouse="_Test Warehouse - _TC",
based_on="Item and Warehouse",
posting_date=today,
posting_time="00:01:00",
)
riv.flags.dont_run_in_test = True # keep it queued

accounts_settings = frappe.get_doc("Accounts Settings")
accounts_settings.acc_frozen_upto = today
accounts_settings.frozen_accounts_modifier = ''
accounts_settings.save()

self.assertRaises(frappe.ValidationError, riv.save)

accounts_settings.acc_frozen_upto = ''
accounts_settings.save()
>>>>>>> 61f05132db (feat: validate repost item valuation against accounts freeze date)

0 comments on commit a852dc1

Please sign in to comment.