2018-09-21 10:15:40 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
2019-03-28 14:13:02 +00:00
|
|
|
from frappe.email import sendmail_to_system_managers
|
2021-09-02 11:14:59 +00:00
|
|
|
from frappe.utils import (
|
|
|
|
add_days,
|
|
|
|
add_months,
|
|
|
|
cint,
|
|
|
|
date_diff,
|
|
|
|
flt,
|
|
|
|
get_first_day,
|
|
|
|
get_last_day,
|
|
|
|
get_link_to_form,
|
|
|
|
getdate,
|
|
|
|
rounded,
|
|
|
|
today,
|
|
|
|
)
|
|
|
|
|
|
|
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
|
|
|
get_accounting_dimensions,
|
|
|
|
)
|
|
|
|
from erpnext.accounts.utils import get_account_currency
|
|
|
|
|
2018-09-21 10:15:40 +00:00
|
|
|
|
|
|
|
def validate_service_stop_date(doc):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Validates service_stop_date for Purchase Invoice and Sales Invoice"""
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
enable_check = (
|
|
|
|
"enable_deferred_revenue" if doc.doctype == "Sales Invoice" else "enable_deferred_expense"
|
|
|
|
)
|
2018-09-21 10:15:40 +00:00
|
|
|
|
|
|
|
old_stop_dates = {}
|
2022-03-28 13:22:46 +00:00
|
|
|
old_doc = frappe.db.get_all(
|
|
|
|
"{0} Item".format(doc.doctype), {"parent": doc.name}, ["name", "service_stop_date"]
|
|
|
|
)
|
2018-09-21 10:15:40 +00:00
|
|
|
|
|
|
|
for d in old_doc:
|
|
|
|
old_stop_dates[d.name] = d.service_stop_date or ""
|
|
|
|
|
|
|
|
for item in doc.items:
|
2022-03-28 13:22:46 +00:00
|
|
|
if not item.get(enable_check):
|
|
|
|
continue
|
2018-09-21 10:15:40 +00:00
|
|
|
|
|
|
|
if item.service_stop_date:
|
|
|
|
if date_diff(item.service_stop_date, item.service_start_date) < 0:
|
|
|
|
frappe.throw(_("Service Stop Date cannot be before Service Start Date"))
|
|
|
|
|
|
|
|
if date_diff(item.service_stop_date, item.service_end_date) > 0:
|
|
|
|
frappe.throw(_("Service Stop Date cannot be after Service End Date"))
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if (
|
|
|
|
old_stop_dates
|
|
|
|
and old_stop_dates.get(item.name)
|
|
|
|
and item.service_stop_date != old_stop_dates.get(item.name)
|
|
|
|
):
|
2020-01-29 09:36:18 +00:00
|
|
|
frappe.throw(_("Cannot change Service Stop Date for item in row {0}").format(item.idx))
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-05-02 14:39:33 +00:00
|
|
|
def build_conditions(process_type, account, company):
|
2022-03-28 13:22:46 +00:00
|
|
|
conditions = ""
|
|
|
|
deferred_account = (
|
|
|
|
"item.deferred_revenue_account" if process_type == "Income" else "item.deferred_expense_account"
|
|
|
|
)
|
2020-05-02 14:39:33 +00:00
|
|
|
|
|
|
|
if account:
|
2022-03-28 13:22:46 +00:00
|
|
|
conditions += "AND %s='%s'" % (deferred_account, account)
|
2020-05-02 14:39:33 +00:00
|
|
|
elif company:
|
2021-05-17 11:13:38 +00:00
|
|
|
conditions += f"AND p.company = {frappe.db.escape(company)}"
|
2020-05-02 14:39:33 +00:00
|
|
|
|
|
|
|
return conditions
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
|
|
|
def convert_deferred_expense_to_expense(
|
|
|
|
deferred_process, start_date=None, end_date=None, conditions=""
|
|
|
|
):
|
2019-03-28 14:13:02 +00:00
|
|
|
# book the expense/income on the last day, but it will be trigger on the 1st of month at 12:00 AM
|
2020-05-02 14:39:33 +00:00
|
|
|
|
2019-03-28 14:13:02 +00:00
|
|
|
if not start_date:
|
|
|
|
start_date = add_months(today(), -1)
|
|
|
|
if not end_date:
|
|
|
|
end_date = add_days(today(), -1)
|
|
|
|
|
2018-09-21 10:15:40 +00:00
|
|
|
# check for the purchase invoice for which GL entries has to be done
|
2022-03-28 13:22:46 +00:00
|
|
|
invoices = frappe.db.sql_list(
|
|
|
|
"""
|
2020-05-02 14:39:33 +00:00
|
|
|
select distinct item.parent
|
|
|
|
from `tabPurchase Invoice Item` item, `tabPurchase Invoice` p
|
|
|
|
where item.service_start_date<=%s and item.service_end_date>=%s
|
|
|
|
and item.enable_deferred_expense = 1 and item.parent=p.name
|
|
|
|
and item.docstatus = 1 and ifnull(item.amount, 0) > 0
|
|
|
|
{0}
|
2022-03-28 13:22:46 +00:00
|
|
|
""".format(
|
|
|
|
conditions
|
|
|
|
),
|
|
|
|
(end_date, start_date),
|
|
|
|
) # nosec
|
2018-09-21 10:15:40 +00:00
|
|
|
|
|
|
|
# For each invoice, book deferred expense
|
|
|
|
for invoice in invoices:
|
|
|
|
doc = frappe.get_doc("Purchase Invoice", invoice)
|
2020-05-02 14:39:33 +00:00
|
|
|
book_deferred_income_or_expense(doc, deferred_process, end_date)
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2020-05-02 14:39:33 +00:00
|
|
|
if frappe.flags.deferred_accounting_error:
|
|
|
|
send_mail(deferred_process)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
|
|
|
def convert_deferred_revenue_to_income(
|
|
|
|
deferred_process, start_date=None, end_date=None, conditions=""
|
|
|
|
):
|
2019-03-28 14:13:02 +00:00
|
|
|
# book the expense/income on the last day, but it will be trigger on the 1st of month at 12:00 AM
|
2020-05-02 14:39:33 +00:00
|
|
|
|
2019-03-28 14:13:02 +00:00
|
|
|
if not start_date:
|
|
|
|
start_date = add_months(today(), -1)
|
|
|
|
if not end_date:
|
|
|
|
end_date = add_days(today(), -1)
|
|
|
|
|
2018-09-21 10:15:40 +00:00
|
|
|
# check for the sales invoice for which GL entries has to be done
|
2022-03-28 13:22:46 +00:00
|
|
|
invoices = frappe.db.sql_list(
|
|
|
|
"""
|
2020-05-02 14:39:33 +00:00
|
|
|
select distinct item.parent
|
|
|
|
from `tabSales Invoice Item` item, `tabSales Invoice` p
|
|
|
|
where item.service_start_date<=%s and item.service_end_date>=%s
|
|
|
|
and item.enable_deferred_revenue = 1 and item.parent=p.name
|
|
|
|
and item.docstatus = 1 and ifnull(item.amount, 0) > 0
|
|
|
|
{0}
|
2022-03-28 13:22:46 +00:00
|
|
|
""".format(
|
|
|
|
conditions
|
|
|
|
),
|
|
|
|
(end_date, start_date),
|
|
|
|
) # nosec
|
2018-09-21 10:15:40 +00:00
|
|
|
|
|
|
|
for invoice in invoices:
|
|
|
|
doc = frappe.get_doc("Sales Invoice", invoice)
|
2020-05-02 14:39:33 +00:00
|
|
|
book_deferred_income_or_expense(doc, deferred_process, end_date)
|
|
|
|
|
|
|
|
if frappe.flags.deferred_accounting_error:
|
|
|
|
send_mail(deferred_process)
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-03-28 14:13:02 +00:00
|
|
|
def get_booking_dates(doc, item, posting_date=None):
|
|
|
|
if not posting_date:
|
|
|
|
posting_date = add_days(today(), -1)
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2019-03-28 14:13:02 +00:00
|
|
|
last_gl_entry = False
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
deferred_account = (
|
|
|
|
"deferred_revenue_account" if doc.doctype == "Sales Invoice" else "deferred_expense_account"
|
|
|
|
)
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
prev_gl_entry = frappe.db.sql(
|
|
|
|
"""
|
2018-09-21 10:15:40 +00:00
|
|
|
select name, posting_date from `tabGL Entry` where company=%s and account=%s and
|
|
|
|
voucher_type=%s and voucher_no=%s and voucher_detail_no=%s
|
2022-03-01 06:26:20 +00:00
|
|
|
and is_cancelled = 0
|
2018-09-21 10:15:40 +00:00
|
|
|
order by posting_date desc limit 1
|
2022-03-28 13:22:46 +00:00
|
|
|
""",
|
|
|
|
(doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name),
|
|
|
|
as_dict=True,
|
|
|
|
)
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
prev_gl_via_je = frappe.db.sql(
|
|
|
|
"""
|
2020-06-23 04:27:56 +00:00
|
|
|
SELECT p.name, p.posting_date FROM `tabJournal Entry` p, `tabJournal Entry Account` c
|
|
|
|
WHERE p.name = c.parent and p.company=%s and c.account=%s
|
|
|
|
and c.reference_type=%s and c.reference_name=%s
|
|
|
|
and c.reference_detail_no=%s and c.docstatus < 2 order by posting_date desc limit 1
|
2022-03-28 13:22:46 +00:00
|
|
|
""",
|
|
|
|
(doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name),
|
|
|
|
as_dict=True,
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
|
|
|
if prev_gl_via_je:
|
2022-03-28 13:22:46 +00:00
|
|
|
if (not prev_gl_entry) or (
|
|
|
|
prev_gl_entry and prev_gl_entry[0].posting_date < prev_gl_via_je[0].posting_date
|
|
|
|
):
|
2020-06-23 04:27:56 +00:00
|
|
|
prev_gl_entry = prev_gl_via_je
|
|
|
|
|
2019-03-28 14:13:02 +00:00
|
|
|
if prev_gl_entry:
|
|
|
|
start_date = getdate(add_days(prev_gl_entry[0].posting_date, 1))
|
|
|
|
else:
|
|
|
|
start_date = item.service_start_date
|
|
|
|
|
|
|
|
end_date = get_last_day(start_date)
|
|
|
|
if end_date >= item.service_end_date:
|
|
|
|
end_date = item.service_end_date
|
|
|
|
last_gl_entry = True
|
2019-03-29 07:55:11 +00:00
|
|
|
elif item.service_stop_date and end_date >= item.service_stop_date:
|
|
|
|
end_date = item.service_stop_date
|
|
|
|
last_gl_entry = True
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2019-03-28 14:13:02 +00:00
|
|
|
if end_date > getdate(posting_date):
|
|
|
|
end_date = posting_date
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2019-03-28 14:13:02 +00:00
|
|
|
if getdate(start_date) <= getdate(end_date):
|
|
|
|
return start_date, end_date, last_gl_entry
|
|
|
|
else:
|
|
|
|
return None, None, None
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
|
|
|
def calculate_monthly_amount(
|
|
|
|
doc, item, last_gl_entry, start_date, end_date, total_days, total_booking_days, account_currency
|
|
|
|
):
|
2020-06-23 04:27:56 +00:00
|
|
|
amount, base_amount = 0, 0
|
|
|
|
|
|
|
|
if not last_gl_entry:
|
2022-03-28 13:22:46 +00:00
|
|
|
total_months = (
|
|
|
|
(item.service_end_date.year - item.service_start_date.year) * 12
|
|
|
|
+ (item.service_end_date.month - item.service_start_date.month)
|
|
|
|
+ 1
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
prorate_factor = flt(date_diff(item.service_end_date, item.service_start_date)) / flt(
|
|
|
|
date_diff(get_last_day(item.service_end_date), get_first_day(item.service_start_date))
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
|
|
|
actual_months = rounded(total_months * prorate_factor, 1)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(
|
|
|
|
doc, item
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
base_amount = flt(item.base_net_amount / actual_months, item.precision("base_net_amount"))
|
|
|
|
|
|
|
|
if base_amount + already_booked_amount > item.base_net_amount:
|
|
|
|
base_amount = item.base_net_amount - already_booked_amount
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if account_currency == doc.company_currency:
|
2020-06-23 04:27:56 +00:00
|
|
|
amount = base_amount
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
amount = flt(item.net_amount / actual_months, item.precision("net_amount"))
|
2020-06-23 04:27:56 +00:00
|
|
|
if amount + already_booked_amount_in_account_currency > item.net_amount:
|
|
|
|
amount = item.net_amount - already_booked_amount_in_account_currency
|
|
|
|
|
|
|
|
if not (get_first_day(start_date) == start_date and get_last_day(end_date) == end_date):
|
2022-03-28 13:22:46 +00:00
|
|
|
partial_month = flt(date_diff(end_date, start_date)) / flt(
|
|
|
|
date_diff(get_last_day(end_date), get_first_day(start_date))
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
|
|
|
base_amount = rounded(partial_month, 1) * base_amount
|
|
|
|
amount = rounded(partial_month, 1) * amount
|
2018-09-21 10:15:40 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(
|
|
|
|
doc, item
|
|
|
|
)
|
|
|
|
base_amount = flt(
|
|
|
|
item.base_net_amount - already_booked_amount, item.precision("base_net_amount")
|
|
|
|
)
|
|
|
|
if account_currency == doc.company_currency:
|
2020-06-23 04:27:56 +00:00
|
|
|
amount = base_amount
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
amount = flt(
|
|
|
|
item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount")
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
|
|
|
return amount, base_amount
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-06-23 04:27:56 +00:00
|
|
|
def calculate_amount(doc, item, last_gl_entry, total_days, total_booking_days, account_currency):
|
2018-09-21 10:15:40 +00:00
|
|
|
amount, base_amount = 0, 0
|
|
|
|
if not last_gl_entry:
|
2022-03-28 13:22:46 +00:00
|
|
|
base_amount = flt(
|
|
|
|
item.base_net_amount * total_booking_days / flt(total_days), item.precision("base_net_amount")
|
|
|
|
)
|
|
|
|
if account_currency == doc.company_currency:
|
2018-09-21 10:15:40 +00:00
|
|
|
amount = base_amount
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
amount = flt(
|
|
|
|
item.net_amount * total_booking_days / flt(total_days), item.precision("net_amount")
|
|
|
|
)
|
2018-09-21 10:15:40 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(
|
|
|
|
doc, item
|
|
|
|
)
|
|
|
|
|
|
|
|
base_amount = flt(
|
|
|
|
item.base_net_amount - already_booked_amount, item.precision("base_net_amount")
|
|
|
|
)
|
|
|
|
if account_currency == doc.company_currency:
|
2018-09-21 10:15:40 +00:00
|
|
|
amount = base_amount
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
amount = flt(
|
|
|
|
item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount")
|
|
|
|
)
|
2018-09-21 10:15:40 +00:00
|
|
|
|
|
|
|
return amount, base_amount
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-06-23 04:27:56 +00:00
|
|
|
def get_already_booked_amount(doc, item):
|
|
|
|
if doc.doctype == "Sales Invoice":
|
|
|
|
total_credit_debit, total_credit_debit_currency = "debit", "debit_in_account_currency"
|
|
|
|
deferred_account = "deferred_revenue_account"
|
|
|
|
else:
|
|
|
|
total_credit_debit, total_credit_debit_currency = "credit", "credit_in_account_currency"
|
|
|
|
deferred_account = "deferred_expense_account"
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
gl_entries_details = frappe.db.sql(
|
|
|
|
"""
|
2020-06-23 04:27:56 +00:00
|
|
|
select sum({0}) as total_credit, sum({1}) as total_credit_in_account_currency, voucher_detail_no
|
|
|
|
from `tabGL Entry` where company=%s and account=%s and voucher_type=%s and voucher_no=%s and voucher_detail_no=%s
|
2022-03-01 06:26:20 +00:00
|
|
|
and is_cancelled = 0
|
2020-06-23 04:27:56 +00:00
|
|
|
group by voucher_detail_no
|
2022-03-28 13:22:46 +00:00
|
|
|
""".format(
|
|
|
|
total_credit_debit, total_credit_debit_currency
|
|
|
|
),
|
|
|
|
(doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name),
|
|
|
|
as_dict=True,
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
journal_entry_details = frappe.db.sql(
|
|
|
|
"""
|
2020-06-23 04:27:56 +00:00
|
|
|
SELECT sum(c.{0}) as total_credit, sum(c.{1}) as total_credit_in_account_currency, reference_detail_no
|
|
|
|
FROM `tabJournal Entry` p , `tabJournal Entry Account` c WHERE p.name = c.parent and
|
|
|
|
p.company = %s and c.account=%s and c.reference_type=%s and c.reference_name=%s and c.reference_detail_no=%s
|
|
|
|
and p.docstatus < 2 group by reference_detail_no
|
2022-03-28 13:22:46 +00:00
|
|
|
""".format(
|
|
|
|
total_credit_debit, total_credit_debit_currency
|
|
|
|
),
|
|
|
|
(doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name),
|
|
|
|
as_dict=True,
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
|
|
|
already_booked_amount = gl_entries_details[0].total_credit if gl_entries_details else 0
|
|
|
|
already_booked_amount += journal_entry_details[0].total_credit if journal_entry_details else 0
|
|
|
|
|
|
|
|
if doc.currency == doc.company_currency:
|
|
|
|
already_booked_amount_in_account_currency = already_booked_amount
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
already_booked_amount_in_account_currency = (
|
|
|
|
gl_entries_details[0].total_credit_in_account_currency if gl_entries_details else 0
|
|
|
|
)
|
|
|
|
already_booked_amount_in_account_currency += (
|
|
|
|
journal_entry_details[0].total_credit_in_account_currency if journal_entry_details else 0
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
|
|
|
return already_booked_amount, already_booked_amount_in_account_currency
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-05-02 14:39:33 +00:00
|
|
|
def book_deferred_income_or_expense(doc, deferred_process, posting_date=None):
|
2022-03-28 13:22:46 +00:00
|
|
|
enable_check = (
|
|
|
|
"enable_deferred_revenue" if doc.doctype == "Sales Invoice" else "enable_deferred_expense"
|
|
|
|
)
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
accounts_frozen_upto = frappe.get_cached_value("Accounts Settings", "None", "acc_frozen_upto")
|
2022-01-07 14:22:38 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
def _book_deferred_revenue_or_expense(
|
|
|
|
item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on
|
|
|
|
):
|
2019-03-28 14:13:02 +00:00
|
|
|
start_date, end_date, last_gl_entry = get_booking_dates(doc, item, posting_date=posting_date)
|
2022-03-28 13:22:46 +00:00
|
|
|
if not (start_date and end_date):
|
|
|
|
return
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2022-01-03 14:10:47 +00:00
|
|
|
account_currency = get_account_currency(item.expense_account or item.income_account)
|
2018-09-21 10:15:40 +00:00
|
|
|
if doc.doctype == "Sales Invoice":
|
|
|
|
against, project = doc.customer, doc.project
|
|
|
|
credit_account, debit_account = item.income_account, item.deferred_revenue_account
|
|
|
|
else:
|
|
|
|
against, project = doc.supplier, item.project
|
|
|
|
credit_account, debit_account = item.deferred_expense_account, item.expense_account
|
|
|
|
|
2019-03-28 14:13:02 +00:00
|
|
|
total_days = date_diff(item.service_end_date, item.service_start_date) + 1
|
|
|
|
total_booking_days = date_diff(end_date, start_date) + 1
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if book_deferred_entries_based_on == "Months":
|
|
|
|
amount, base_amount = calculate_monthly_amount(
|
|
|
|
doc,
|
|
|
|
item,
|
|
|
|
last_gl_entry,
|
|
|
|
start_date,
|
|
|
|
end_date,
|
|
|
|
total_days,
|
|
|
|
total_booking_days,
|
|
|
|
account_currency,
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
amount, base_amount = calculate_amount(
|
|
|
|
doc, item, last_gl_entry, total_days, total_booking_days, account_currency
|
|
|
|
)
|
2019-03-28 14:13:02 +00:00
|
|
|
|
2021-06-24 08:44:46 +00:00
|
|
|
if not amount:
|
|
|
|
return
|
|
|
|
|
2022-01-07 14:22:38 +00:00
|
|
|
# check if books nor frozen till endate:
|
2022-03-01 06:26:20 +00:00
|
|
|
if accounts_frozen_upto and (end_date) <= getdate(accounts_frozen_upto):
|
2022-01-07 14:22:38 +00:00
|
|
|
end_date = get_last_day(add_days(accounts_frozen_upto, 1))
|
|
|
|
|
2020-06-23 04:27:56 +00:00
|
|
|
if via_journal_entry:
|
2022-03-28 13:22:46 +00:00
|
|
|
book_revenue_via_journal_entry(
|
|
|
|
doc,
|
|
|
|
credit_account,
|
|
|
|
debit_account,
|
|
|
|
amount,
|
|
|
|
base_amount,
|
|
|
|
end_date,
|
|
|
|
project,
|
|
|
|
account_currency,
|
|
|
|
item.cost_center,
|
|
|
|
item,
|
|
|
|
deferred_process,
|
|
|
|
submit_journal_entry,
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
make_gl_entries(
|
|
|
|
doc,
|
|
|
|
credit_account,
|
|
|
|
debit_account,
|
|
|
|
against,
|
|
|
|
amount,
|
|
|
|
base_amount,
|
|
|
|
end_date,
|
|
|
|
project,
|
|
|
|
account_currency,
|
|
|
|
item.cost_center,
|
|
|
|
item,
|
|
|
|
deferred_process,
|
|
|
|
)
|
2020-05-02 14:39:33 +00:00
|
|
|
|
|
|
|
# Returned in case of any errors because it tries to submit the same record again and again in case of errors
|
|
|
|
if frappe.flags.deferred_accounting_error:
|
|
|
|
return
|
2019-03-28 14:13:02 +00:00
|
|
|
|
|
|
|
if getdate(end_date) < getdate(posting_date) and not last_gl_entry:
|
2022-03-28 13:22:46 +00:00
|
|
|
_book_deferred_revenue_or_expense(
|
|
|
|
item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on
|
|
|
|
)
|
2019-03-28 14:13:02 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
via_journal_entry = cint(
|
|
|
|
frappe.db.get_singles_value("Accounts Settings", "book_deferred_entries_via_journal_entry")
|
|
|
|
)
|
|
|
|
submit_journal_entry = cint(
|
|
|
|
frappe.db.get_singles_value("Accounts Settings", "submit_journal_entries")
|
|
|
|
)
|
|
|
|
book_deferred_entries_based_on = frappe.db.get_singles_value(
|
|
|
|
"Accounts Settings", "book_deferred_entries_based_on"
|
|
|
|
)
|
2019-03-28 14:13:02 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
for item in doc.get("items"):
|
2019-03-28 14:13:02 +00:00
|
|
|
if item.get(enable_check):
|
2022-03-28 13:22:46 +00:00
|
|
|
_book_deferred_revenue_or_expense(
|
|
|
|
item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on
|
|
|
|
)
|
|
|
|
|
2019-03-28 14:13:02 +00:00
|
|
|
|
2020-05-23 19:24:04 +00:00
|
|
|
def process_deferred_accounting(posting_date=None):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Converts deferred income/expense into income/expense
|
|
|
|
Executed via background jobs on every month end"""
|
2020-05-02 14:39:33 +00:00
|
|
|
|
2020-05-23 19:24:04 +00:00
|
|
|
if not posting_date:
|
|
|
|
posting_date = today()
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if not cint(
|
|
|
|
frappe.db.get_singles_value(
|
|
|
|
"Accounts Settings", "automatically_process_deferred_accounting_entry"
|
|
|
|
)
|
|
|
|
):
|
2020-05-02 14:39:33 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
start_date = add_months(today(), -1)
|
|
|
|
end_date = add_days(today(), -1)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
companies = frappe.get_all("Company")
|
2021-07-01 04:01:31 +00:00
|
|
|
|
|
|
|
for company in companies:
|
2022-03-28 13:22:46 +00:00
|
|
|
for record_type in ("Income", "Expense"):
|
|
|
|
doc = frappe.get_doc(
|
|
|
|
dict(
|
|
|
|
doctype="Process Deferred Accounting",
|
|
|
|
company=company.name,
|
|
|
|
posting_date=posting_date,
|
|
|
|
start_date=start_date,
|
|
|
|
end_date=end_date,
|
|
|
|
type=record_type,
|
|
|
|
)
|
|
|
|
)
|
2021-07-01 04:01:31 +00:00
|
|
|
|
|
|
|
doc.insert()
|
|
|
|
doc.submit()
|
2020-05-02 14:39:33 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
|
|
|
def make_gl_entries(
|
|
|
|
doc,
|
|
|
|
credit_account,
|
|
|
|
debit_account,
|
|
|
|
against,
|
|
|
|
amount,
|
|
|
|
base_amount,
|
|
|
|
posting_date,
|
|
|
|
project,
|
|
|
|
account_currency,
|
|
|
|
cost_center,
|
|
|
|
item,
|
|
|
|
deferred_process=None,
|
|
|
|
):
|
2019-03-28 14:13:02 +00:00
|
|
|
# GL Entry for crediting the amount in the deferred expense
|
|
|
|
from erpnext.accounts.general_ledger import make_gl_entries
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if amount == 0:
|
|
|
|
return
|
2019-11-06 09:55:00 +00:00
|
|
|
|
2019-03-28 14:13:02 +00:00
|
|
|
gl_entries = []
|
|
|
|
gl_entries.append(
|
2022-03-28 13:22:46 +00:00
|
|
|
doc.get_gl_dict(
|
|
|
|
{
|
|
|
|
"account": credit_account,
|
|
|
|
"against": against,
|
|
|
|
"credit": base_amount,
|
|
|
|
"credit_in_account_currency": amount,
|
|
|
|
"cost_center": cost_center,
|
|
|
|
"voucher_detail_no": item.name,
|
|
|
|
"posting_date": posting_date,
|
|
|
|
"project": project,
|
|
|
|
"against_voucher_type": "Process Deferred Accounting",
|
|
|
|
"against_voucher": deferred_process,
|
|
|
|
},
|
|
|
|
account_currency,
|
|
|
|
item=item,
|
|
|
|
)
|
2019-03-28 14:13:02 +00:00
|
|
|
)
|
|
|
|
# GL Entry to debit the amount from the expense
|
|
|
|
gl_entries.append(
|
2022-03-28 13:22:46 +00:00
|
|
|
doc.get_gl_dict(
|
|
|
|
{
|
|
|
|
"account": debit_account,
|
|
|
|
"against": against,
|
|
|
|
"debit": base_amount,
|
|
|
|
"debit_in_account_currency": amount,
|
|
|
|
"cost_center": cost_center,
|
|
|
|
"voucher_detail_no": item.name,
|
|
|
|
"posting_date": posting_date,
|
|
|
|
"project": project,
|
|
|
|
"against_voucher_type": "Process Deferred Accounting",
|
|
|
|
"against_voucher": deferred_process,
|
|
|
|
},
|
|
|
|
account_currency,
|
|
|
|
item=item,
|
|
|
|
)
|
2019-03-28 14:13:02 +00:00
|
|
|
)
|
|
|
|
|
2018-09-21 10:15:40 +00:00
|
|
|
if gl_entries:
|
2019-02-13 11:48:12 +00:00
|
|
|
try:
|
|
|
|
make_gl_entries(gl_entries, cancel=(doc.docstatus == 2), merge_entries=True)
|
|
|
|
frappe.db.commit()
|
2021-09-29 16:56:33 +00:00
|
|
|
except Exception as e:
|
|
|
|
if frappe.flags.in_test:
|
2022-05-02 09:34:26 +00:00
|
|
|
doc.log_error(f"Error while processing deferred accounting for Invoice {doc.name}")
|
2021-12-04 14:35:37 +00:00
|
|
|
raise e
|
2021-09-29 16:56:33 +00:00
|
|
|
else:
|
|
|
|
frappe.db.rollback()
|
2022-05-02 09:34:26 +00:00
|
|
|
doc.log_error(f"Error while processing deferred accounting for Invoice {doc.name}")
|
2021-09-29 16:56:33 +00:00
|
|
|
frappe.flags.deferred_accounting_error = True
|
2020-05-02 14:39:33 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-05-02 14:39:33 +00:00
|
|
|
def send_mail(deferred_process):
|
2021-05-17 11:36:12 +00:00
|
|
|
title = _("Error while processing deferred accounting for {0}").format(deferred_process)
|
2022-03-28 13:22:46 +00:00
|
|
|
link = get_link_to_form("Process Deferred Accounting", deferred_process)
|
2021-05-17 11:36:12 +00:00
|
|
|
content = _("Deferred accounting failed for some invoices:") + "\n"
|
2022-03-28 13:22:46 +00:00
|
|
|
content += _(
|
|
|
|
"Please check Process Deferred Accounting {0} and submit manually after resolving errors."
|
|
|
|
).format(link)
|
2020-05-02 14:39:33 +00:00
|
|
|
sendmail_to_system_managers(title, content)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
def book_revenue_via_journal_entry(
|
|
|
|
doc,
|
|
|
|
credit_account,
|
|
|
|
debit_account,
|
|
|
|
amount,
|
|
|
|
base_amount,
|
|
|
|
posting_date,
|
|
|
|
project,
|
|
|
|
account_currency,
|
|
|
|
cost_center,
|
|
|
|
item,
|
|
|
|
deferred_process=None,
|
|
|
|
submit="No",
|
|
|
|
):
|
|
|
|
|
|
|
|
if amount == 0:
|
|
|
|
return
|
2020-06-23 04:27:56 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
journal_entry = frappe.new_doc("Journal Entry")
|
2020-06-23 04:27:56 +00:00
|
|
|
journal_entry.posting_date = posting_date
|
|
|
|
journal_entry.company = doc.company
|
2022-03-28 13:22:46 +00:00
|
|
|
journal_entry.voucher_type = (
|
|
|
|
"Deferred Revenue" if doc.doctype == "Sales Invoice" else "Deferred Expense"
|
|
|
|
)
|
2022-04-06 12:03:46 +00:00
|
|
|
journal_entry.process_deferred_accounting = deferred_process
|
2020-06-23 04:27:56 +00:00
|
|
|
|
|
|
|
debit_entry = {
|
2022-03-28 13:22:46 +00:00
|
|
|
"account": credit_account,
|
|
|
|
"credit": base_amount,
|
|
|
|
"credit_in_account_currency": amount,
|
|
|
|
"account_currency": account_currency,
|
|
|
|
"reference_name": doc.name,
|
|
|
|
"reference_type": doc.doctype,
|
|
|
|
"reference_detail_no": item.name,
|
|
|
|
"cost_center": cost_center,
|
|
|
|
"project": project,
|
2020-06-23 04:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
credit_entry = {
|
2022-03-28 13:22:46 +00:00
|
|
|
"account": debit_account,
|
|
|
|
"debit": base_amount,
|
|
|
|
"debit_in_account_currency": amount,
|
|
|
|
"account_currency": account_currency,
|
|
|
|
"reference_name": doc.name,
|
|
|
|
"reference_type": doc.doctype,
|
|
|
|
"reference_detail_no": item.name,
|
|
|
|
"cost_center": cost_center,
|
|
|
|
"project": project,
|
2020-06-23 04:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for dimension in get_accounting_dimensions():
|
2022-03-28 13:22:46 +00:00
|
|
|
debit_entry.update({dimension: item.get(dimension)})
|
2020-06-23 04:27:56 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
credit_entry.update({dimension: item.get(dimension)})
|
2020-06-23 04:27:56 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
journal_entry.append("accounts", debit_entry)
|
|
|
|
journal_entry.append("accounts", credit_entry)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
journal_entry.save()
|
|
|
|
|
|
|
|
if submit:
|
|
|
|
journal_entry.submit()
|
2021-12-04 13:55:44 +00:00
|
|
|
|
|
|
|
frappe.db.commit()
|
2021-09-01 09:10:56 +00:00
|
|
|
except Exception:
|
2020-06-23 04:27:56 +00:00
|
|
|
frappe.db.rollback()
|
2022-05-02 09:34:26 +00:00
|
|
|
doc.log_error(f"Error while processing deferred accounting for Invoice {doc.name}")
|
2020-06-23 04:27:56 +00:00
|
|
|
frappe.flags.deferred_accounting_error = True
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-06-23 04:27:56 +00:00
|
|
|
def get_deferred_booking_accounts(doctype, voucher_detail_no, dr_or_cr):
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if doctype == "Sales Invoice":
|
|
|
|
credit_account, debit_account = frappe.db.get_value(
|
|
|
|
"Sales Invoice Item",
|
|
|
|
{"name": voucher_detail_no},
|
|
|
|
["income_account", "deferred_revenue_account"],
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
credit_account, debit_account = frappe.db.get_value(
|
|
|
|
"Purchase Invoice Item",
|
|
|
|
{"name": voucher_detail_no},
|
|
|
|
["deferred_expense_account", "expense_account"],
|
|
|
|
)
|
2020-06-23 04:27:56 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if dr_or_cr == "Debit":
|
2020-06-23 04:27:56 +00:00
|
|
|
return debit_account
|
|
|
|
else:
|
|
|
|
return credit_account
|