Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | |
| 3 | import frappe |
| 4 | from frappe import _ |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 5 | from frappe.utils import date_diff, add_months, today, getdate, add_days, flt, get_last_day |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 6 | from erpnext.accounts.utils import get_account_currency |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 7 | from frappe.email import sendmail_to_system_managers |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 8 | |
| 9 | def validate_service_stop_date(doc): |
| 10 | ''' Validates service_stop_date for Purchase Invoice and Sales Invoice ''' |
| 11 | |
| 12 | enable_check = "enable_deferred_revenue" \ |
| 13 | if doc.doctype=="Sales Invoice" else "enable_deferred_expense" |
| 14 | |
| 15 | old_stop_dates = {} |
| 16 | old_doc = frappe.db.get_all("{0} Item".format(doc.doctype), |
| 17 | {"parent": doc.name}, ["name", "service_stop_date"]) |
| 18 | |
| 19 | for d in old_doc: |
| 20 | old_stop_dates[d.name] = d.service_stop_date or "" |
| 21 | |
| 22 | for item in doc.items: |
| 23 | if not item.get(enable_check): continue |
| 24 | |
| 25 | if item.service_stop_date: |
| 26 | if date_diff(item.service_stop_date, item.service_start_date) < 0: |
| 27 | frappe.throw(_("Service Stop Date cannot be before Service Start Date")) |
| 28 | |
| 29 | if date_diff(item.service_stop_date, item.service_end_date) > 0: |
| 30 | frappe.throw(_("Service Stop Date cannot be after Service End Date")) |
| 31 | |
Rohit Waghchaure | c699b2a | 2018-09-24 11:57:48 +0530 | [diff] [blame] | 32 | if old_stop_dates and old_stop_dates.get(item.name) and item.service_stop_date!=old_stop_dates.get(item.name): |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 33 | frappe.throw(_("Cannot change Service Stop Date for item in row {0}".format(item.idx))) |
| 34 | |
| 35 | def convert_deferred_expense_to_expense(start_date=None, end_date=None): |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 36 | # book the expense/income on the last day, but it will be trigger on the 1st of month at 12:00 AM |
| 37 | if not start_date: |
| 38 | start_date = add_months(today(), -1) |
| 39 | if not end_date: |
| 40 | end_date = add_days(today(), -1) |
| 41 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 42 | # check for the purchase invoice for which GL entries has to be done |
| 43 | invoices = frappe.db.sql_list(''' |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 44 | select distinct parent from `tabPurchase Invoice Item` |
| 45 | where service_start_date<=%s and service_end_date>=%s |
rohitwaghchaure | 90f9f9d | 2018-09-30 21:12:50 +0530 | [diff] [blame] | 46 | and enable_deferred_expense = 1 and docstatus = 1 and ifnull(amount, 0) > 0 |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 47 | ''', (end_date, start_date)) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 48 | |
| 49 | # For each invoice, book deferred expense |
| 50 | for invoice in invoices: |
| 51 | doc = frappe.get_doc("Purchase Invoice", invoice) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 52 | book_deferred_income_or_expense(doc, end_date) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 53 | |
| 54 | def convert_deferred_revenue_to_income(start_date=None, end_date=None): |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 55 | # book the expense/income on the last day, but it will be trigger on the 1st of month at 12:00 AM |
| 56 | if not start_date: |
| 57 | start_date = add_months(today(), -1) |
| 58 | if not end_date: |
| 59 | end_date = add_days(today(), -1) |
| 60 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 61 | # check for the sales invoice for which GL entries has to be done |
| 62 | invoices = frappe.db.sql_list(''' |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 63 | select distinct parent from `tabSales Invoice Item` |
| 64 | where service_start_date<=%s and service_end_date>=%s |
rohitwaghchaure | 90f9f9d | 2018-09-30 21:12:50 +0530 | [diff] [blame] | 65 | and enable_deferred_revenue = 1 and docstatus = 1 and ifnull(amount, 0) > 0 |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 66 | ''', (end_date, start_date)) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 67 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 68 | for invoice in invoices: |
| 69 | doc = frappe.get_doc("Sales Invoice", invoice) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 70 | book_deferred_income_or_expense(doc, end_date) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 71 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 72 | def get_booking_dates(doc, item, posting_date=None): |
| 73 | if not posting_date: |
| 74 | posting_date = add_days(today(), -1) |
| 75 | |
| 76 | last_gl_entry = False |
| 77 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 78 | deferred_account = "deferred_revenue_account" if doc.doctype=="Sales Invoice" else "deferred_expense_account" |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 79 | |
| 80 | prev_gl_entry = frappe.db.sql(''' |
| 81 | select name, posting_date from `tabGL Entry` where company=%s and account=%s and |
| 82 | voucher_type=%s and voucher_no=%s and voucher_detail_no=%s |
| 83 | order by posting_date desc limit 1 |
| 84 | ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True) |
| 85 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 86 | if prev_gl_entry: |
| 87 | start_date = getdate(add_days(prev_gl_entry[0].posting_date, 1)) |
| 88 | else: |
| 89 | start_date = item.service_start_date |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 90 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 91 | end_date = get_last_day(start_date) |
| 92 | if end_date >= item.service_end_date: |
| 93 | end_date = item.service_end_date |
| 94 | last_gl_entry = True |
Nabin Hait | 66d07c2 | 2019-03-29 13:25:11 +0530 | [diff] [blame] | 95 | elif item.service_stop_date and end_date >= item.service_stop_date: |
| 96 | end_date = item.service_stop_date |
| 97 | last_gl_entry = True |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 98 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 99 | if end_date > getdate(posting_date): |
| 100 | end_date = posting_date |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 101 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 102 | if getdate(start_date) <= getdate(end_date): |
| 103 | return start_date, end_date, last_gl_entry |
| 104 | else: |
| 105 | return None, None, None |
| 106 | |
| 107 | def calculate_amount(doc, item, last_gl_entry, total_days, total_booking_days, account_currency): |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 108 | if doc.doctype == "Sales Invoice": |
| 109 | total_credit_debit, total_credit_debit_currency = "debit", "debit_in_account_currency" |
| 110 | deferred_account = "deferred_revenue_account" |
| 111 | else: |
| 112 | total_credit_debit, total_credit_debit_currency = "credit", "credit_in_account_currency" |
| 113 | deferred_account = "deferred_expense_account" |
| 114 | |
| 115 | amount, base_amount = 0, 0 |
| 116 | if not last_gl_entry: |
| 117 | base_amount = flt(item.base_net_amount*total_booking_days/flt(total_days), item.precision("base_net_amount")) |
| 118 | if account_currency==doc.company_currency: |
| 119 | amount = base_amount |
| 120 | else: |
| 121 | amount = flt(item.net_amount*total_booking_days/flt(total_days), item.precision("net_amount")) |
| 122 | else: |
| 123 | gl_entries_details = frappe.db.sql(''' |
| 124 | select sum({0}) as total_credit, sum({1}) as total_credit_in_account_currency, voucher_detail_no |
| 125 | from `tabGL Entry` where company=%s and account=%s and voucher_type=%s and voucher_no=%s and voucher_detail_no=%s |
| 126 | group by voucher_detail_no |
| 127 | '''.format(total_credit_debit, total_credit_debit_currency), |
| 128 | (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 129 | already_booked_amount = gl_entries_details[0].total_credit if gl_entries_details else 0 |
| 130 | base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount")) |
| 131 | if account_currency==doc.company_currency: |
| 132 | amount = base_amount |
| 133 | else: |
| 134 | already_booked_amount_in_account_currency = gl_entries_details[0].total_credit_in_account_currency if gl_entries_details else 0 |
| 135 | amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount")) |
| 136 | |
| 137 | return amount, base_amount |
| 138 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 139 | def book_deferred_income_or_expense(doc, posting_date=None): |
Nabin Hait | 27af6b3 | 2019-02-08 16:52:13 +0530 | [diff] [blame] | 140 | enable_check = "enable_deferred_revenue" \ |
| 141 | if doc.doctype=="Sales Invoice" else "enable_deferred_expense" |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 142 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 143 | def _book_deferred_revenue_or_expense(item): |
| 144 | start_date, end_date, last_gl_entry = get_booking_dates(doc, item, posting_date=posting_date) |
| 145 | if not (start_date and end_date): return |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 146 | |
| 147 | account_currency = get_account_currency(item.expense_account) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 148 | if doc.doctype == "Sales Invoice": |
| 149 | against, project = doc.customer, doc.project |
| 150 | credit_account, debit_account = item.income_account, item.deferred_revenue_account |
| 151 | else: |
| 152 | against, project = doc.supplier, item.project |
| 153 | credit_account, debit_account = item.deferred_expense_account, item.expense_account |
| 154 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 155 | total_days = date_diff(item.service_end_date, item.service_start_date) + 1 |
| 156 | total_booking_days = date_diff(end_date, start_date) + 1 |
| 157 | |
| 158 | amount, base_amount = calculate_amount(doc, item, last_gl_entry, |
| 159 | total_days, total_booking_days, account_currency) |
| 160 | |
| 161 | make_gl_entries(doc, credit_account, debit_account, against, |
| 162 | amount, base_amount, end_date, project, account_currency, item.cost_center, item.name) |
| 163 | |
| 164 | if getdate(end_date) < getdate(posting_date) and not last_gl_entry: |
| 165 | _book_deferred_revenue_or_expense(item) |
| 166 | |
| 167 | |
| 168 | for item in doc.get('items'): |
| 169 | if item.get(enable_check): |
| 170 | _book_deferred_revenue_or_expense(item) |
| 171 | |
| 172 | def make_gl_entries(doc, credit_account, debit_account, against, |
| 173 | amount, base_amount, posting_date, project, account_currency, cost_center, voucher_detail_no): |
| 174 | # GL Entry for crediting the amount in the deferred expense |
| 175 | from erpnext.accounts.general_ledger import make_gl_entries |
| 176 | |
| 177 | gl_entries = [] |
| 178 | gl_entries.append( |
| 179 | doc.get_gl_dict({ |
| 180 | "account": credit_account, |
| 181 | "against": against, |
| 182 | "credit": base_amount, |
| 183 | "credit_in_account_currency": amount, |
| 184 | "cost_center": cost_center, |
| 185 | "voucher_detail_no": voucher_detail_no, |
| 186 | 'posting_date': posting_date, |
| 187 | 'project': project |
| 188 | }, account_currency) |
| 189 | ) |
| 190 | # GL Entry to debit the amount from the expense |
| 191 | gl_entries.append( |
| 192 | doc.get_gl_dict({ |
| 193 | "account": debit_account, |
| 194 | "against": against, |
| 195 | "debit": base_amount, |
| 196 | "debit_in_account_currency": amount, |
| 197 | "cost_center": cost_center, |
| 198 | "voucher_detail_no": voucher_detail_no, |
| 199 | 'posting_date': posting_date, |
| 200 | 'project': project |
| 201 | }, account_currency) |
| 202 | ) |
| 203 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 204 | if gl_entries: |
Nabin Hait | 29fcb14 | 2019-02-13 17:18:12 +0530 | [diff] [blame] | 205 | try: |
| 206 | make_gl_entries(gl_entries, cancel=(doc.docstatus == 2), merge_entries=True) |
| 207 | frappe.db.commit() |
| 208 | except: |
| 209 | frappe.db.rollback() |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 210 | title = _("Error while processing deferred accounting for {0}").format(doc.name) |
| 211 | traceback = frappe.get_traceback() |
| 212 | frappe.log_error(message=traceback , title=title) |
| 213 | sendmail_to_system_managers(title, traceback) |