fix: Linter and incorrect cost center in test records

This commit is contained in:
marination 2023-06-14 16:48:18 +05:30
parent 18495ed624
commit 772f6ffd21
6 changed files with 80 additions and 67 deletions

View File

@ -23,7 +23,6 @@ from erpnext.controllers.accounts_controller import AccountsController
class Dunning(AccountsController):
def validate(self):
self.validate_same_currency()
self.validate_overdue_payments()
@ -37,7 +36,11 @@ class Dunning(AccountsController):
for row in self.overdue_payments:
invoice_currency = frappe.get_value("Sales Invoice", row.sales_invoice, "currency")
if invoice_currency != self.currency:
frappe.throw(_("The currency of invoice {} ({}) is different from the currency of this dunning ({}).").format(row.sales_invoice, invoice_currency, self.currency))
frappe.throw(
_(
"The currency of invoice {} ({}) is different from the currency of this dunning ({})."
).format(row.sales_invoice, invoice_currency, self.currency)
)
def validate_overdue_payments(self):
daily_interest = self.rate_of_interest / 100 / 365
@ -55,12 +58,13 @@ class Dunning(AccountsController):
def set_dunning_level(self):
for row in self.overdue_payments:
past_dunnings = frappe.get_all("Overdue Payment",
past_dunnings = frappe.get_all(
"Overdue Payment",
filters={
"payment_schedule": row.payment_schedule,
"parent": ("!=", row.parent),
"docstatus": 1
}
"docstatus": 1,
},
)
row.dunning_level = len(past_dunnings) + 1
@ -72,21 +76,26 @@ def resolve_dunning(doc, state):
"""
for reference in doc.references:
if reference.reference_doctype == "Sales Invoice" and reference.outstanding_amount <= 0:
unresolved_dunnings = frappe.get_all("Dunning",
unresolved_dunnings = frappe.get_all(
"Dunning",
filters={
"sales_invoice": reference.reference_name,
"status": ("!=", "Resolved"),
"docstatus": ("!=", 2),
},
pluck="name"
pluck="name",
)
for dunning_name in unresolved_dunnings:
resolve = True
dunning = frappe.get_doc("Dunning", dunning_name)
for overdue_payment in dunning.overdue_payments:
outstanding_inv = frappe.get_value("Sales Invoice", overdue_payment.sales_invoice, "outstanding_amount")
outstanding_ps = frappe.get_value("Payment Schedule", overdue_payment.payment_schedule, "outstanding")
outstanding_inv = frappe.get_value(
"Sales Invoice", overdue_payment.sales_invoice, "outstanding_amount"
)
outstanding_ps = frappe.get_value(
"Payment Schedule", overdue_payment.payment_schedule, "outstanding"
)
if outstanding_ps > 0 and outstanding_inv > 0:
resolve = False
@ -95,7 +104,6 @@ def resolve_dunning(doc, state):
dunning.save()
@frappe.whitelist()
def get_dunning_letter_text(dunning_type, doc, language=None):
if isinstance(doc, str):

View File

@ -112,13 +112,16 @@ def create_dunning_type(title, fee, interest, is_default):
def get_income_account(company):
return frappe.get_value("Company", company, "default_income_account") or frappe.get_all(
"Account",
filters={"is_group": 0, "company": company},
or_filters={
"report_type": "Profit and Loss",
"account_type": ("in", ("Income Account", "Temporary")),
},
limit=1,
pluck="name",
)[0]
return (
frappe.get_value("Company", company, "default_income_account")
or frappe.get_all(
"Account",
filters={"is_group": 0, "company": company},
or_filters={
"report_type": "Profit and Loss",
"account_type": ("in", ("Income Account", "Temporary")),
},
limit=1,
pluck="name",
)[0]
)

View File

@ -14,7 +14,7 @@
}
],
"income_account": "Sales - _TC",
"cost_center": "_Test Cost Center"
"cost_center": "_Test Cost Center - _TC"
},
{
"doctype": "Dunning Type",
@ -31,6 +31,6 @@
}
],
"income_account": "Sales - _TC",
"cost_center": "_Test Cost Center"
"cost_center": "_Test Cost Center - _TC"
}
]

View File

@ -1850,22 +1850,28 @@ def get_payment_entry(
else:
if dt == "Dunning":
for overdue_payment in doc.overdue_payments:
pe.append("references", {
"reference_doctype": "Sales Invoice",
"reference_name": overdue_payment.sales_invoice,
"payment_term": overdue_payment.payment_term,
"due_date": overdue_payment.due_date,
"total_amount": overdue_payment.outstanding,
"outstanding_amount": overdue_payment.outstanding,
"allocated_amount": overdue_payment.outstanding
})
pe.append(
"references",
{
"reference_doctype": "Sales Invoice",
"reference_name": overdue_payment.sales_invoice,
"payment_term": overdue_payment.payment_term,
"due_date": overdue_payment.due_date,
"total_amount": overdue_payment.outstanding,
"outstanding_amount": overdue_payment.outstanding,
"allocated_amount": overdue_payment.outstanding,
},
)
pe.append("deductions", {
"account": doc.income_account,
"cost_center": doc.cost_center,
"amount": -1 * doc.dunning_amount,
"description": _("Interest and/or dunning fee")
})
pe.append(
"deductions",
{
"account": doc.income_account,
"cost_center": doc.cost_center,
"amount": -1 * doc.dunning_amount,
"description": _("Interest and/or dunning fee"),
},
)
else:
pe.append(
"references",
@ -1957,8 +1963,10 @@ def set_party_account_currency(dt, party_account, doc):
def set_payment_type(dt, doc):
if (
dt == "Sales Order" or (dt == "Sales Invoice" and doc.outstanding_amount > 0)
) or (dt == "Purchase Invoice" and doc.outstanding_amount < 0) or dt == "Dunning":
(dt == "Sales Order" or (dt == "Sales Invoice" and doc.outstanding_amount > 0))
or (dt == "Purchase Invoice" and doc.outstanding_amount < 0)
or dt == "Dunning"
):
payment_type = "Receive"
else:
payment_type = "Pay"

View File

@ -622,7 +622,9 @@ class SalesInvoice(SellingController):
return
if not self.account_for_change_amount:
self.account_for_change_amount = frappe.get_cached_value('Company', self.company, 'default_cash_account')
self.account_for_change_amount = frappe.get_cached_value(
"Company", self.company, "default_cash_account"
)
from erpnext.stock.get_item_details import get_pos_profile, get_pos_profile_item_details
@ -1907,17 +1909,17 @@ def get_bank_cash_account(mode_of_payment, company):
@frappe.whitelist()
def make_maintenance_schedule(source_name, target_doc=None):
doclist = get_mapped_doc("Sales Invoice", source_name, {
"Sales Invoice": {
"doctype": "Maintenance Schedule",
"validation": {
"docstatus": ["=", 1]
}
doclist = get_mapped_doc(
"Sales Invoice",
source_name,
{
"Sales Invoice": {"doctype": "Maintenance Schedule", "validation": {"docstatus": ["=", 1]}},
"Sales Invoice Item": {
"doctype": "Maintenance Schedule Item",
},
},
"Sales Invoice Item": {
"doctype": "Maintenance Schedule Item",
},
}, target_doc)
target_doc,
)
return doclist
@ -2523,9 +2525,7 @@ def create_dunning(source_name, target_doc=None, ignore_permissions=False):
target.income_account = dunning_type.income_account
target.cost_center = dunning_type.cost_center
letter_text = get_dunning_letter_text(
dunning_type=dunning_type.name,
doc=target.as_dict(),
language=source.language
dunning_type=dunning_type.name, doc=target.as_dict(), language=source.language
)
if letter_text:
@ -2542,26 +2542,19 @@ def create_dunning(source_name, target_doc=None, ignore_permissions=False):
table_maps={
"Sales Invoice": {
"doctype": "Dunning",
"field_map": {
"customer_address": "customer_address",
"parent": "sales_invoice"
},
"field_map": {"customer_address": "customer_address", "parent": "sales_invoice"},
},
"Payment Schedule": {
"doctype": "Overdue Payment",
"field_map": {
"name": "payment_schedule",
"parent": "sales_invoice"
},
"field_map": {"name": "payment_schedule", "parent": "sales_invoice"},
"condition": lambda doc: doc.outstanding > 0 and getdate(doc.due_date) < getdate(),
}
},
},
postprocess=postprocess_dunning,
ignore_permissions=ignore_permissions
ignore_permissions=ignore_permissions,
)
def check_if_return_invoice_linked_with_payment_entry(self):
# If a Return invoice is linked with payment entry along with other invoices,
# the cancellation of the Return causes allocated amount to be greater than paid

View File

@ -18,7 +18,8 @@ def execute():
# something's already here, doesn't need patching
continue
payment_schedules = frappe.get_all("Payment Schedule",
payment_schedules = frappe.get_all(
"Payment Schedule",
filters={"parent": dunning.sales_invoice},
fields=[
"parent as sales_invoice",
@ -30,8 +31,8 @@ def execute():
# at the time of creating this dunning, the full amount was outstanding
"payment_amount as outstanding",
"'0' as paid_amount",
"discounted_amount"
]
"discounted_amount",
],
)
dunning.extend("overdue_payments", payment_schedules)