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 _ |
| 5 | from frappe.utils import date_diff, add_months, today, getdate, add_days, flt |
| 6 | from erpnext.accounts.utils import get_account_currency |
| 7 | from erpnext.accounts.general_ledger import make_gl_entries |
| 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 | |
Nabin Hait | 3476a45 | 2019-01-25 16:22:36 +0530 | [diff] [blame] | 32 | if old_stop_dates and old_stop_dates.get(item.name) and item.service_stop_date!=old_stop_dates[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): |
| 36 | # check for the purchase invoice for which GL entries has to be done |
| 37 | invoices = frappe.db.sql_list(''' |
rohitwaghchaure | 90f9f9d | 2018-09-30 21:12:50 +0530 | [diff] [blame] | 38 | select distinct parent from `tabPurchase Invoice Item` where service_start_date<=%s and service_end_date>=%s |
| 39 | and enable_deferred_expense = 1 and docstatus = 1 and ifnull(amount, 0) > 0 |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 40 | ''', (end_date or today(), start_date or add_months(today(), -1))) |
| 41 | |
| 42 | # For each invoice, book deferred expense |
| 43 | for invoice in invoices: |
| 44 | doc = frappe.get_doc("Purchase Invoice", invoice) |
| 45 | book_deferred_income_or_expense(doc, start_date, end_date) |
| 46 | |
| 47 | def convert_deferred_revenue_to_income(start_date=None, end_date=None): |
| 48 | # check for the sales invoice for which GL entries has to be done |
| 49 | invoices = frappe.db.sql_list(''' |
rohitwaghchaure | 90f9f9d | 2018-09-30 21:12:50 +0530 | [diff] [blame] | 50 | select distinct parent from `tabSales Invoice Item` where service_start_date<=%s and service_end_date>=%s |
| 51 | and enable_deferred_revenue = 1 and docstatus = 1 and ifnull(amount, 0) > 0 |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 52 | ''', (end_date or today(), start_date or add_months(today(), -1))) |
| 53 | |
| 54 | # For each invoice, book deferred revenue |
| 55 | for invoice in invoices: |
| 56 | doc = frappe.get_doc("Sales Invoice", invoice) |
| 57 | book_deferred_income_or_expense(doc, start_date, end_date) |
| 58 | |
| 59 | def get_booking_dates(doc, item, start_date=None, end_date=None): |
| 60 | deferred_account = "deferred_revenue_account" if doc.doctype=="Sales Invoice" else "deferred_expense_account" |
| 61 | last_gl_entry, skip = False, False |
| 62 | |
| 63 | booking_end_date = getdate(add_days(today(), -1)) if not end_date else end_date |
| 64 | if booking_end_date < item.service_start_date or \ |
| 65 | (item.service_stop_date and booking_end_date.month > item.service_stop_date.month): |
| 66 | return None, None, None, True |
| 67 | elif booking_end_date >= item.service_end_date: |
| 68 | last_gl_entry = True |
| 69 | booking_end_date = item.service_end_date |
| 70 | elif item.service_stop_date and item.service_stop_date <= booking_end_date: |
| 71 | last_gl_entry = True |
| 72 | booking_end_date = item.service_stop_date |
| 73 | |
| 74 | booking_start_date = getdate(add_months(today(), -1)) if not start_date else start_date |
| 75 | booking_start_date = booking_start_date \ |
| 76 | if booking_start_date > item.service_start_date else item.service_start_date |
| 77 | |
| 78 | prev_gl_entry = frappe.db.sql(''' |
| 79 | select name, posting_date from `tabGL Entry` where company=%s and account=%s and |
| 80 | voucher_type=%s and voucher_no=%s and voucher_detail_no=%s |
| 81 | order by posting_date desc limit 1 |
| 82 | ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True) |
| 83 | |
| 84 | if not prev_gl_entry and item.service_start_date < booking_start_date: |
| 85 | booking_start_date = item.service_start_date |
| 86 | elif prev_gl_entry: |
| 87 | booking_start_date = getdate(add_days(prev_gl_entry[0].posting_date, 1)) |
| 88 | skip = True if booking_start_date > booking_end_date else False |
| 89 | |
| 90 | return last_gl_entry, booking_start_date, booking_end_date, skip |
| 91 | |
| 92 | def calculate_amount_and_base_amount(doc, item, last_gl_entry, total_days, total_booking_days): |
| 93 | account_currency = get_account_currency(item.expense_account) |
| 94 | |
| 95 | if doc.doctype == "Sales Invoice": |
| 96 | total_credit_debit, total_credit_debit_currency = "debit", "debit_in_account_currency" |
| 97 | deferred_account = "deferred_revenue_account" |
| 98 | else: |
| 99 | total_credit_debit, total_credit_debit_currency = "credit", "credit_in_account_currency" |
| 100 | deferred_account = "deferred_expense_account" |
| 101 | |
| 102 | amount, base_amount = 0, 0 |
| 103 | if not last_gl_entry: |
| 104 | base_amount = flt(item.base_net_amount*total_booking_days/flt(total_days), item.precision("base_net_amount")) |
| 105 | if account_currency==doc.company_currency: |
| 106 | amount = base_amount |
| 107 | else: |
| 108 | amount = flt(item.net_amount*total_booking_days/flt(total_days), item.precision("net_amount")) |
| 109 | else: |
| 110 | gl_entries_details = frappe.db.sql(''' |
| 111 | select sum({0}) as total_credit, sum({1}) as total_credit_in_account_currency, voucher_detail_no |
| 112 | from `tabGL Entry` where company=%s and account=%s and voucher_type=%s and voucher_no=%s and voucher_detail_no=%s |
| 113 | group by voucher_detail_no |
| 114 | '''.format(total_credit_debit, total_credit_debit_currency), |
| 115 | (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True) |
| 116 | |
| 117 | already_booked_amount = gl_entries_details[0].total_credit if gl_entries_details else 0 |
| 118 | base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount")) |
| 119 | if account_currency==doc.company_currency: |
| 120 | amount = base_amount |
| 121 | else: |
| 122 | already_booked_amount_in_account_currency = gl_entries_details[0].total_credit_in_account_currency if gl_entries_details else 0 |
| 123 | amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount")) |
| 124 | |
| 125 | return amount, base_amount |
| 126 | |
| 127 | def book_deferred_income_or_expense(doc, start_date=None, end_date=None): |
| 128 | # book the expense/income on the last day, but it will be trigger on the 1st of month at 12:00 AM |
| 129 | # start_date: 1st of the last month or the start date |
| 130 | # end_date: end_date or today-1 |
| 131 | |
| 132 | gl_entries = [] |
| 133 | for item in doc.get('items'): |
| 134 | skip = False |
| 135 | last_gl_entry, booking_start_date, booking_end_date, skip = \ |
| 136 | get_booking_dates(doc, item, start_date, end_date) |
| 137 | |
| 138 | if skip: continue |
| 139 | total_days = date_diff(item.service_end_date, item.service_start_date) |
| 140 | total_booking_days = date_diff(booking_end_date, booking_start_date) + 1 |
| 141 | |
| 142 | account_currency = get_account_currency(item.expense_account) |
| 143 | amount, base_amount = calculate_amount_and_base_amount(doc, item, last_gl_entry, total_days, total_booking_days) |
| 144 | |
| 145 | if doc.doctype == "Sales Invoice": |
| 146 | against, project = doc.customer, doc.project |
| 147 | credit_account, debit_account = item.income_account, item.deferred_revenue_account |
| 148 | else: |
| 149 | against, project = doc.supplier, item.project |
| 150 | credit_account, debit_account = item.deferred_expense_account, item.expense_account |
| 151 | |
| 152 | # GL Entry for crediting the amount in the deferred expense |
| 153 | gl_entries.append( |
| 154 | doc.get_gl_dict({ |
| 155 | "account": credit_account, |
| 156 | "against": against, |
| 157 | "credit": base_amount, |
| 158 | "credit_in_account_currency": amount, |
| 159 | "cost_center": item.cost_center, |
Zlash65 | 58a080a | 2018-10-06 16:17:12 +0530 | [diff] [blame] | 160 | "voucher_detail_no": item.name, |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 161 | 'posting_date': booking_end_date, |
| 162 | 'project': project |
| 163 | }, account_currency) |
| 164 | ) |
| 165 | # GL Entry to debit the amount from the expense |
| 166 | gl_entries.append( |
| 167 | doc.get_gl_dict({ |
| 168 | "account": debit_account, |
| 169 | "against": against, |
| 170 | "debit": base_amount, |
| 171 | "debit_in_account_currency": amount, |
| 172 | "cost_center": item.cost_center, |
| 173 | "voucher_detail_no": item.name, |
| 174 | 'posting_date': booking_end_date, |
| 175 | 'project': project |
| 176 | }, account_currency) |
| 177 | ) |
| 178 | |
| 179 | if gl_entries: |
| 180 | make_gl_entries(gl_entries, cancel=(doc.docstatus == 2), merge_entries=True) |