test: Added test for PCV cancellation
This commit is contained in:
parent
4caaab32d1
commit
914a388ee3
@ -8,7 +8,6 @@ from frappe.utils import flt
|
|||||||
|
|
||||||
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
||||||
get_accounting_dimensions,
|
get_accounting_dimensions,
|
||||||
get_dimensions,
|
|
||||||
)
|
)
|
||||||
from erpnext.accounts.utils import get_account_currency
|
from erpnext.accounts.utils import get_account_currency
|
||||||
from erpnext.controllers.accounts_controller import AccountsController
|
from erpnext.controllers.accounts_controller import AccountsController
|
||||||
@ -27,17 +26,19 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
self.status = "In Progress"
|
self.status = "In Progress"
|
||||||
self.ignore_linked_doctypes = ("GL Entry", "Stock Ledger Entry")
|
self.ignore_linked_doctypes = ("GL Entry", "Stock Ledger Entry")
|
||||||
gle_count = frappe.db.count(
|
gle_count = frappe.db.count(
|
||||||
"GL Entry",
|
"GL Entry",
|
||||||
{
|
{"voucher_type": "Period Closing Voucher", "voucher_no": self.name, "is_cancelled": 0},
|
||||||
"voucher_type": "Period Closing Voucher",
|
|
||||||
"voucher_no": self.name,
|
|
||||||
"is_cancelled": 0
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
if gle_count > 5000:
|
if gle_count > 5000:
|
||||||
frappe.enqueue(make_reverse_gl_entries, voucher_type="Period Closing Voucher",
|
frappe.enqueue(
|
||||||
voucher_no=self.name, queue="long")
|
make_reverse_gl_entries,
|
||||||
frappe.msgprint(_("The GL Entries will be cancelled in the background, it can take a few minutes."), alert=True)
|
voucher_type="Period Closing Voucher",
|
||||||
|
voucher_no=self.name,
|
||||||
|
queue="long",
|
||||||
|
)
|
||||||
|
frappe.msgprint(
|
||||||
|
_("The GL Entries will be cancelled in the background, it can take a few minutes."), alert=True
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
make_reverse_gl_entries(voucher_type="Period Closing Voucher", voucher_no=self.name)
|
make_reverse_gl_entries(voucher_type="Period Closing Voucher", voucher_no=self.name)
|
||||||
|
|
||||||
@ -76,13 +77,16 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
pce[0][0], self.posting_date
|
pce[0][0], self.posting_date
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def make_gl_entries(self):
|
def make_gl_entries(self):
|
||||||
gl_entries = self.get_gl_entries()
|
gl_entries = self.get_gl_entries()
|
||||||
if gl_entries:
|
if gl_entries:
|
||||||
if len(gl_entries) > 5000:
|
if len(gl_entries) > 5000:
|
||||||
frappe.enqueue(process_gl_entries, gl_entries=gl_entries, queue="long")
|
frappe.enqueue(process_gl_entries, gl_entries=gl_entries, queue="long")
|
||||||
frappe.msgprint(_("The GL Entries will be processed in the background, it can take a few minutes."), alert=True)
|
frappe.msgprint(
|
||||||
|
_("The GL Entries will be processed in the background, it can take a few minutes."),
|
||||||
|
alert=True,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
process_gl_entries(gl_entries)
|
process_gl_entries(gl_entries)
|
||||||
|
|
||||||
@ -91,7 +95,7 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
|
|
||||||
# pl account
|
# pl account
|
||||||
for acc in self.get_pl_balances_based_on_dimensions(group_by_account=True):
|
for acc in self.get_pl_balances_based_on_dimensions(group_by_account=True):
|
||||||
if flt(acc.bal_in_company_currency):
|
if flt(acc.bal_in_company_currency):
|
||||||
gl_entries.append(self.get_gle_for_pl_account(acc))
|
gl_entries.append(self.get_gle_for_pl_account(acc))
|
||||||
|
|
||||||
# closing liability account
|
# closing liability account
|
||||||
@ -100,7 +104,7 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
gl_entries.append(self.get_gle_for_closing_account(acc))
|
gl_entries.append(self.get_gle_for_closing_account(acc))
|
||||||
|
|
||||||
return gl_entries
|
return gl_entries
|
||||||
|
|
||||||
def get_gle_for_pl_account(self, acc):
|
def get_gle_for_pl_account(self, acc):
|
||||||
gl_entry = self.get_gl_dict(
|
gl_entry = self.get_gl_dict(
|
||||||
{
|
{
|
||||||
@ -109,23 +113,19 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
"finance_book": acc.finance_book,
|
"finance_book": acc.finance_book,
|
||||||
"account_currency": acc.account_currency,
|
"account_currency": acc.account_currency,
|
||||||
"debit_in_account_currency": abs(flt(acc.bal_in_account_currency))
|
"debit_in_account_currency": abs(flt(acc.bal_in_account_currency))
|
||||||
if flt(acc.bal_in_account_currency) < 0
|
if flt(acc.bal_in_account_currency) < 0
|
||||||
else 0,
|
else 0,
|
||||||
"debit": abs(flt(acc.bal_in_company_currency))
|
"debit": abs(flt(acc.bal_in_company_currency)) if flt(acc.bal_in_company_currency) < 0 else 0,
|
||||||
if flt(acc.bal_in_company_currency) < 0
|
|
||||||
else 0,
|
|
||||||
"credit_in_account_currency": abs(flt(acc.bal_in_account_currency))
|
"credit_in_account_currency": abs(flt(acc.bal_in_account_currency))
|
||||||
if flt(acc.bal_in_account_currency) > 0
|
if flt(acc.bal_in_account_currency) > 0
|
||||||
else 0,
|
else 0,
|
||||||
"credit": abs(flt(acc.bal_in_company_currency))
|
"credit": abs(flt(acc.bal_in_company_currency)) if flt(acc.bal_in_company_currency) > 0 else 0,
|
||||||
if flt(acc.bal_in_company_currency) > 0
|
|
||||||
else 0,
|
|
||||||
},
|
},
|
||||||
item=acc,
|
item=acc,
|
||||||
)
|
)
|
||||||
self.update_default_dimensions(gl_entry, acc)
|
self.update_default_dimensions(gl_entry, acc)
|
||||||
return gl_entry
|
return gl_entry
|
||||||
|
|
||||||
def get_gle_for_closing_account(self, acc):
|
def get_gle_for_closing_account(self, acc):
|
||||||
gl_entry = self.get_gl_dict(
|
gl_entry = self.get_gl_dict(
|
||||||
{
|
{
|
||||||
@ -136,15 +136,11 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
"debit_in_account_currency": abs(flt(acc.bal_in_account_currency))
|
"debit_in_account_currency": abs(flt(acc.bal_in_account_currency))
|
||||||
if flt(acc.bal_in_account_currency) > 0
|
if flt(acc.bal_in_account_currency) > 0
|
||||||
else 0,
|
else 0,
|
||||||
"debit": abs(flt(acc.bal_in_company_currency))
|
"debit": abs(flt(acc.bal_in_company_currency)) if flt(acc.bal_in_company_currency) > 0 else 0,
|
||||||
if flt(acc.bal_in_company_currency) > 0
|
|
||||||
else 0,
|
|
||||||
"credit_in_account_currency": abs(flt(acc.bal_in_account_currency))
|
"credit_in_account_currency": abs(flt(acc.bal_in_account_currency))
|
||||||
if flt(acc.bal_in_account_currency) < 0
|
if flt(acc.bal_in_account_currency) < 0
|
||||||
else 0,
|
else 0,
|
||||||
"credit": abs(flt(acc.bal_in_company_currency))
|
"credit": abs(flt(acc.bal_in_company_currency)) if flt(acc.bal_in_company_currency) < 0 else 0,
|
||||||
if flt(acc.bal_in_company_currency) < 0
|
|
||||||
else 0,
|
|
||||||
},
|
},
|
||||||
item=acc,
|
item=acc,
|
||||||
)
|
)
|
||||||
@ -162,7 +158,7 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
"""Get balance for dimension-wise pl accounts"""
|
"""Get balance for dimension-wise pl accounts"""
|
||||||
|
|
||||||
dimension_fields = ["t1.cost_center", "t1.finance_book"]
|
dimension_fields = ["t1.cost_center", "t1.finance_book"]
|
||||||
|
|
||||||
self.accounting_dimensions = get_accounting_dimensions()
|
self.accounting_dimensions = get_accounting_dimensions()
|
||||||
for dimension in self.accounting_dimensions:
|
for dimension in self.accounting_dimensions:
|
||||||
dimension_fields.append("t1.{0}".format(dimension))
|
dimension_fields.append("t1.{0}".format(dimension))
|
||||||
@ -170,7 +166,8 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
if group_by_account:
|
if group_by_account:
|
||||||
dimension_fields.append("t1.account")
|
dimension_fields.append("t1.account")
|
||||||
|
|
||||||
return frappe.db.sql("""
|
return frappe.db.sql(
|
||||||
|
"""
|
||||||
select
|
select
|
||||||
t2.account_currency,
|
t2.account_currency,
|
||||||
{dimension_fields},
|
{dimension_fields},
|
||||||
@ -192,22 +189,30 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
as_dict=1,
|
as_dict=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def process_gl_entries(gl_entries):
|
def process_gl_entries(gl_entries):
|
||||||
from erpnext.accounts.general_ledger import make_gl_entries
|
from erpnext.accounts.general_ledger import make_gl_entries
|
||||||
|
|
||||||
try:
|
try:
|
||||||
make_gl_entries(gl_entries, merge_entries=False)
|
make_gl_entries(gl_entries, merge_entries=False)
|
||||||
frappe.db.set_value("Period Closing Voucher", gl_entries[0].get("voucher_no"), "status", "Completed")
|
frappe.db.set_value(
|
||||||
|
"Period Closing Voucher", gl_entries[0].get("voucher_no"), "status", "Completed"
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
frappe.db.rollback()
|
frappe.db.rollback()
|
||||||
frappe.log_error(e)
|
frappe.log_error(e)
|
||||||
frappe.db.set_value("Period Closing Voucher", gl_entries[0].get("voucher_no"), "status", "Failed")
|
frappe.db.set_value(
|
||||||
|
"Period Closing Voucher", gl_entries[0].get("voucher_no"), "status", "Failed"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def make_reverse_gl_entries(voucher_type, voucher_no):
|
def make_reverse_gl_entries(voucher_type, voucher_no):
|
||||||
from erpnext.accounts.general_ledger import make_reverse_gl_entries
|
from erpnext.accounts.general_ledger import make_reverse_gl_entries
|
||||||
|
|
||||||
try:
|
try:
|
||||||
make_reverse_gl_entries(voucher_type=voucher_type, voucher_no=voucher_no)
|
make_reverse_gl_entries(voucher_type=voucher_type, voucher_no=voucher_no)
|
||||||
frappe.db.set_value("Period Closing Voucher", voucher_no, "status", "Completed")
|
frappe.db.set_value("Period Closing Voucher", voucher_no, "status", "Completed")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
frappe.db.rollback()
|
frappe.db.rollback()
|
||||||
frappe.log_error(e)
|
frappe.log_error(e)
|
||||||
frappe.db.set_value("Period Closing Voucher", gl_entries[0].get("voucher_no"), "status", "Failed")
|
frappe.db.set_value("Period Closing Voucher", voucher_no, "status", "Failed")
|
||||||
|
@ -92,7 +92,6 @@ class TestPeriodClosingVoucher(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
pcv = self.make_period_closing_voucher(submit=False)
|
pcv = self.make_period_closing_voucher(submit=False)
|
||||||
pcv.cost_center_wise_pnl = 1
|
|
||||||
pcv.save()
|
pcv.save()
|
||||||
pcv.submit()
|
pcv.submit()
|
||||||
surplus_account = pcv.closing_account_head
|
surplus_account = pcv.closing_account_head
|
||||||
@ -115,6 +114,16 @@ class TestPeriodClosingVoucher(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(pcv_gle, expected_gle)
|
self.assertEqual(pcv_gle, expected_gle)
|
||||||
|
|
||||||
|
pcv.reload()
|
||||||
|
pcv.cancel()
|
||||||
|
|
||||||
|
self.assertFalse(
|
||||||
|
frappe.db.get_value(
|
||||||
|
"GL Entry",
|
||||||
|
{"voucher_type": "Period Closing Voucher", "voucher_no": pcv.name, "is_cancelled": 0},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def test_period_closing_with_finance_book_entries(self):
|
def test_period_closing_with_finance_book_entries(self):
|
||||||
frappe.db.sql("delete from `tabGL Entry` where company='Test PCV Company'")
|
frappe.db.sql("delete from `tabGL Entry` where company='Test PCV Company'")
|
||||||
|
|
||||||
|
@ -168,7 +168,7 @@ def get_cost_center_allocation_data(company, posting_date):
|
|||||||
def merge_similar_entries(gl_map, precision=None):
|
def merge_similar_entries(gl_map, precision=None):
|
||||||
merged_gl_map = []
|
merged_gl_map = []
|
||||||
accounting_dimensions = get_accounting_dimensions()
|
accounting_dimensions = get_accounting_dimensions()
|
||||||
|
|
||||||
for entry in gl_map:
|
for entry in gl_map:
|
||||||
# if there is already an entry in this account then just add it
|
# if there is already an entry in this account then just add it
|
||||||
# to that entry
|
# to that entry
|
||||||
@ -291,6 +291,7 @@ def save_entries(gl_map, adv_adj, update_outstanding, from_repost=False):
|
|||||||
for entry in gl_map:
|
for entry in gl_map:
|
||||||
make_entry(entry, adv_adj, update_outstanding, from_repost)
|
make_entry(entry, adv_adj, update_outstanding, from_repost)
|
||||||
|
|
||||||
|
|
||||||
def make_entry(args, adv_adj, update_outstanding, from_repost=False):
|
def make_entry(args, adv_adj, update_outstanding, from_repost=False):
|
||||||
gle = frappe.new_doc("GL Entry")
|
gle = frappe.new_doc("GL Entry")
|
||||||
gle.update(args)
|
gle.update(args)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user