Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: split party GLE according to against #39049

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 71 additions & 41 deletions erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,6 @@ def get_gl_entries(self, warehouse_account=None):
self.negative_expense_to_be_booked = 0.0
gl_entries = []

self.make_supplier_gl_entry(gl_entries)
self.make_item_gl_entries(gl_entries)
self.make_precision_loss_gl_entry(gl_entries)

Expand All @@ -805,45 +804,6 @@ def check_asset_cwip_enabled(self):
return 1
return 0

def make_supplier_gl_entry(self, gl_entries):
# Checked both rounding_adjustment and rounded_total
# because rounded_total had value even before introduction of posting GLE based on rounded total
grand_total = (
self.rounded_total if (self.rounding_adjustment and self.rounded_total) else self.grand_total
)
base_grand_total = flt(
self.base_rounded_total
if (self.base_rounding_adjustment and self.base_rounded_total)
else self.base_grand_total,
self.precision("base_grand_total"),
)

if grand_total and not self.is_internal_transfer():
# Did not use base_grand_total to book rounding loss gle
gl_entries.append(
self.get_gl_dict(
{
"account": self.credit_to,
"party_type": "Supplier",
"party": self.supplier,
"due_date": self.due_date,
"against_type": "Account",
"against": self.against_expense_account,
"against_link": self.against_expense_account,
"credit": base_grand_total,
"credit_in_account_currency": base_grand_total
if self.party_account_currency == self.company_currency
else grand_total,
"against_voucher": self.name,
"against_voucher_type": self.doctype,
"project": self.project,
"cost_center": self.cost_center,
},
self.party_account_currency,
item=self,
)
)

def make_item_gl_entries(self, gl_entries):
# item gl entries
stock_items = self.get_stock_items()
Expand Down Expand Up @@ -956,6 +916,17 @@ def make_item_gl_entries(self, gl_entries):
)
)

party_entry_args = {
"against": item.expense_account,
"credit": flt(item.base_net_amount, item.precision("base_net_amount")),
"credit_in_account_currency": (
flt(item.base_net_amount, item.precision("base_net_amount"))
if self.party_account_currency == self.company_currency
else flt(item.net_amount, item.precision("net_amount"))
),
}
self.make_party_gl_entry(gl_entries, party_entry_args)

else:
if not self.is_internal_transfer():
gl_entries.append(
Expand All @@ -975,6 +946,12 @@ def make_item_gl_entries(self, gl_entries):
)
)

party_entry_args = {
"against": item.expense_account,
"credit": warehouse_debit_amount,
}
self.make_party_gl_entry(gl_entries, party_entry_args)

# Amount added through landed-cost-voucher
if landed_cost_entries:
if (item.item_code, item.name) in landed_cost_entries:
Expand All @@ -996,6 +973,13 @@ def make_item_gl_entries(self, gl_entries):
)
)

party_entry_args = {
"against": account,
"debit": flt(amount["base_amount"]),
"debit_in_account_currency": flt(amount["amount"]),
}
self.make_party_gl_entry(gl_entries, party_entry_args)

# sub-contracting warehouse
if flt(item.rm_supp_cost):
supplier_warehouse_account = warehouse_account[self.supplier_warehouse]["account"]
Expand Down Expand Up @@ -1074,6 +1058,12 @@ def make_item_gl_entries(self, gl_entries):
)
)

party_entry_args = {
"against": expense_account,
"credit": amount,
}
self.make_party_gl_entry(gl_entries, party_entry_args)

# check if the exchange rate has changed
if item.get("purchase_receipt"):
if (
Expand Down Expand Up @@ -1101,6 +1091,13 @@ def make_item_gl_entries(self, gl_entries):
item=item,
)
)

party_entry_args = {
"against": expense_account,
"credit": discrepancy_caused_by_exchange_rate_difference,
}
self.make_party_gl_entry(gl_entries, party_entry_args)

gl_entries.append(
self.get_gl_dict(
{
Expand All @@ -1116,6 +1113,13 @@ def make_item_gl_entries(self, gl_entries):
item=item,
)
)

party_entry_args = {
"against": self.get_company_default("exchange_gain_loss_account"),
"debit": discrepancy_caused_by_exchange_rate_difference,
}
self.make_party_gl_entry(gl_entries, party_entry_args)

if (
self.auto_accounting_for_stock
and self.is_opening == "No"
Expand Down Expand Up @@ -1217,6 +1221,12 @@ def make_stock_adjustment_entry(
)
)

party_entry_args = {
"against": cost_of_goods_sold_account,
"credit": stock_adjustment_amt,
}
self.make_party_gl_entry(gl_entries, party_entry_args)

warehouse_debit_amount = stock_amount

return warehouse_debit_amount
Expand All @@ -1230,7 +1240,9 @@ def make_tax_gl_entries(self, gl_entries):
if tax.category in ("Total", "Valuation and Total") and flt(base_amount):
account_currency = get_account_currency(tax.account_head)

dr_or_cr = "debit" if tax.add_deduct_tax == "Add" else "credit"
(dr_or_cr, rev_dr_or_cr) = (
("debit", "credit") if tax.add_deduct_tax == "Add" else ("credit", "debit")
)

gl_entries.append(
self.get_gl_dict(
Expand All @@ -1249,6 +1261,17 @@ def make_tax_gl_entries(self, gl_entries):
item=tax,
)
)

if not self.is_internal_transfer():
party_entry_args = {
"against": tax.account_head,
rev_dr_or_cr: base_amount,
rev_dr_or_cr + "_in_account_currency": base_amount
if account_currency == self.company_currency
else amount,
}
self.make_party_gl_entry(gl_entries, party_entry_args)

# accumulate valuation tax
if (
self.is_opening == "No"
Expand Down Expand Up @@ -1460,6 +1483,13 @@ def make_gle_for_rounding_adjustment(self, gl_entries):
)
)

party_entry_args = {
"against": round_off_account,
"credit_in_account_currency": self.rounding_adjustment,
"credit": self.base_rounding_adjustment,
}
self.make_party_gl_entry(gl_entries, party_entry_args)

def on_cancel(self):
check_if_return_invoice_linked_with_payment_entry(self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest

import frappe
from frappe.query_builder.functions import Sum
from frappe.tests.utils import FrappeTestCase, change_settings
from frappe.utils import add_days, cint, flt, getdate, nowdate, today

Expand Down Expand Up @@ -92,7 +93,7 @@ def test_gl_entries_without_perpetual_inventory(self):
pi.submit()

expected_gl_entries = {
"_Test Payable - _TC": [0, 1512.0],
"_Test Payable - _TC": [168.33, 1680.33],
"_Test Account Cost for Goods Sold - _TC": [1250, 0],
"_Test Account Shipping Charges - _TC": [100, 0],
"_Test Account Excise Duty - _TC": [140, 0],
Expand All @@ -103,12 +104,9 @@ def test_gl_entries_without_perpetual_inventory(self):
"_Test Account Discount - _TC": [0, 168.03],
"Round Off - _TC": [0, 0.3],
}
gl_entries = frappe.db.sql(
"""select account, debit, credit from `tabGL Entry`
where voucher_type = 'Purchase Invoice' and voucher_no = %s""",
pi.name,
as_dict=1,
)

gl_entries = get_pi_gl_entries_with_consolidated_values(pi.name)

for d in gl_entries:
self.assertEqual([d.debit, d.credit], expected_gl_entries.get(d.account))

Expand Down Expand Up @@ -298,13 +296,7 @@ def test_gl_entries_with_perpetual_inventory_against_pr(self):
self.check_gle_for_pi(pi.name)

def check_gle_for_pi(self, pi):
gl_entries = frappe.db.sql(
"""select account, sum(debit) as debit, sum(credit) as credit
from `tabGL Entry` where voucher_type='Purchase Invoice' and voucher_no=%s
group by account""",
pi,
as_dict=1,
)
gl_entries = get_pi_gl_entries_with_consolidated_values(pi)

self.assertTrue(gl_entries)

Expand Down Expand Up @@ -2016,6 +2008,17 @@ def test_debit_note_without_item(self):
self.assertEqual(return_pi.docstatus, 1)


def get_pi_gl_entries_with_consolidated_values(voucher_no, as_dict=True):
gle = frappe.qb.DocType("GL Entry")
return (
frappe.qb.from_(gle)
.select(gle.account, Sum(gle.debit).as_("debit"), Sum(gle.credit).as_("credit"))
.where((gle.voucher_type == "Purchase Invoice") & (gle.voucher_no == voucher_no))
.groupby(gle.account)
.orderby(gle.account)
).run(as_dict=as_dict)


def set_advance_flag(company, flag, default_account):
frappe.db.set_value(
"Company",
Expand Down Expand Up @@ -2045,7 +2048,7 @@ def check_gl_entries(
& (gl.posting_date >= posting_date)
& (gl.is_cancelled == 0)
)
.orderby(gl.posting_date, gl.account, gl.creation)
.orderby(gl.posting_date, gl.account, gl.credit, gl.creation)
)

if additional_columns:
Expand Down
Loading
Loading