Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 1 | import frappe |
| 2 | from frappe import _ |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 3 | from frappe.email import sendmail_to_system_managers |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | from frappe.utils import ( |
| 5 | add_days, |
| 6 | add_months, |
| 7 | cint, |
| 8 | date_diff, |
| 9 | flt, |
| 10 | get_first_day, |
| 11 | get_last_day, |
| 12 | get_link_to_form, |
| 13 | getdate, |
| 14 | rounded, |
| 15 | today, |
| 16 | ) |
| 17 | |
| 18 | from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( |
| 19 | get_accounting_dimensions, |
| 20 | ) |
| 21 | from erpnext.accounts.utils import get_account_currency |
| 22 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 23 | |
| 24 | def validate_service_stop_date(doc): |
| 25 | ''' Validates service_stop_date for Purchase Invoice and Sales Invoice ''' |
| 26 | |
| 27 | enable_check = "enable_deferred_revenue" \ |
| 28 | if doc.doctype=="Sales Invoice" else "enable_deferred_expense" |
| 29 | |
| 30 | old_stop_dates = {} |
| 31 | old_doc = frappe.db.get_all("{0} Item".format(doc.doctype), |
| 32 | {"parent": doc.name}, ["name", "service_stop_date"]) |
| 33 | |
| 34 | for d in old_doc: |
| 35 | old_stop_dates[d.name] = d.service_stop_date or "" |
| 36 | |
| 37 | for item in doc.items: |
| 38 | if not item.get(enable_check): continue |
| 39 | |
| 40 | if item.service_stop_date: |
| 41 | if date_diff(item.service_stop_date, item.service_start_date) < 0: |
| 42 | frappe.throw(_("Service Stop Date cannot be before Service Start Date")) |
| 43 | |
| 44 | if date_diff(item.service_stop_date, item.service_end_date) > 0: |
| 45 | frappe.throw(_("Service Stop Date cannot be after Service End Date")) |
| 46 | |
Rohit Waghchaure | c699b2a | 2018-09-24 11:57:48 +0530 | [diff] [blame] | 47 | if old_stop_dates and old_stop_dates.get(item.name) and item.service_stop_date!=old_stop_dates.get(item.name): |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 48 | frappe.throw(_("Cannot change Service Stop Date for item in row {0}").format(item.idx)) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 49 | |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 50 | def build_conditions(process_type, account, company): |
| 51 | conditions='' |
| 52 | deferred_account = "item.deferred_revenue_account" if process_type=="Income" else "item.deferred_expense_account" |
| 53 | |
| 54 | if account: |
| 55 | conditions += "AND %s='%s'"%(deferred_account, account) |
| 56 | elif company: |
Ankush Menat | f3b3d81 | 2021-05-17 16:43:38 +0530 | [diff] [blame] | 57 | conditions += f"AND p.company = {frappe.db.escape(company)}" |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 58 | |
| 59 | return conditions |
| 60 | |
| 61 | def convert_deferred_expense_to_expense(deferred_process, start_date=None, end_date=None, conditions=''): |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 62 | # book the expense/income on the last day, but it will be trigger on the 1st of month at 12:00 AM |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 63 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 64 | if not start_date: |
| 65 | start_date = add_months(today(), -1) |
| 66 | if not end_date: |
| 67 | end_date = add_days(today(), -1) |
| 68 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 69 | # check for the purchase invoice for which GL entries has to be done |
| 70 | invoices = frappe.db.sql_list(''' |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 71 | select distinct item.parent |
| 72 | from `tabPurchase Invoice Item` item, `tabPurchase Invoice` p |
| 73 | where item.service_start_date<=%s and item.service_end_date>=%s |
| 74 | and item.enable_deferred_expense = 1 and item.parent=p.name |
| 75 | and item.docstatus = 1 and ifnull(item.amount, 0) > 0 |
| 76 | {0} |
| 77 | '''.format(conditions), (end_date, start_date)) #nosec |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 78 | |
| 79 | # For each invoice, book deferred expense |
| 80 | for invoice in invoices: |
| 81 | doc = frappe.get_doc("Purchase Invoice", invoice) |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 82 | book_deferred_income_or_expense(doc, deferred_process, end_date) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 83 | |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 84 | if frappe.flags.deferred_accounting_error: |
| 85 | send_mail(deferred_process) |
| 86 | |
| 87 | def convert_deferred_revenue_to_income(deferred_process, start_date=None, end_date=None, conditions=''): |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 88 | # book the expense/income on the last day, but it will be trigger on the 1st of month at 12:00 AM |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 89 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 90 | if not start_date: |
| 91 | start_date = add_months(today(), -1) |
| 92 | if not end_date: |
| 93 | end_date = add_days(today(), -1) |
| 94 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 95 | # check for the sales invoice for which GL entries has to be done |
| 96 | invoices = frappe.db.sql_list(''' |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 97 | select distinct item.parent |
| 98 | from `tabSales Invoice Item` item, `tabSales Invoice` p |
| 99 | where item.service_start_date<=%s and item.service_end_date>=%s |
| 100 | and item.enable_deferred_revenue = 1 and item.parent=p.name |
| 101 | and item.docstatus = 1 and ifnull(item.amount, 0) > 0 |
| 102 | {0} |
| 103 | '''.format(conditions), (end_date, start_date)) #nosec |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 104 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 105 | for invoice in invoices: |
| 106 | doc = frappe.get_doc("Sales Invoice", invoice) |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 107 | book_deferred_income_or_expense(doc, deferred_process, end_date) |
| 108 | |
| 109 | if frappe.flags.deferred_accounting_error: |
| 110 | send_mail(deferred_process) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 111 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 112 | def get_booking_dates(doc, item, posting_date=None): |
| 113 | if not posting_date: |
| 114 | posting_date = add_days(today(), -1) |
| 115 | |
| 116 | last_gl_entry = False |
| 117 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 118 | 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] | 119 | |
| 120 | prev_gl_entry = frappe.db.sql(''' |
| 121 | select name, posting_date from `tabGL Entry` where company=%s and account=%s and |
| 122 | voucher_type=%s and voucher_no=%s and voucher_detail_no=%s |
Deepesh Garg | 366120f | 2022-03-01 11:56:20 +0530 | [diff] [blame] | 123 | and is_cancelled = 0 |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 124 | order by posting_date desc limit 1 |
| 125 | ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True) |
| 126 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 127 | prev_gl_via_je = frappe.db.sql(''' |
| 128 | SELECT p.name, p.posting_date FROM `tabJournal Entry` p, `tabJournal Entry Account` c |
| 129 | WHERE p.name = c.parent and p.company=%s and c.account=%s |
| 130 | and c.reference_type=%s and c.reference_name=%s |
| 131 | and c.reference_detail_no=%s and c.docstatus < 2 order by posting_date desc limit 1 |
| 132 | ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True) |
| 133 | |
| 134 | if prev_gl_via_je: |
| 135 | if (not prev_gl_entry) or (prev_gl_entry and |
| 136 | prev_gl_entry[0].posting_date < prev_gl_via_je[0].posting_date): |
| 137 | prev_gl_entry = prev_gl_via_je |
| 138 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 139 | if prev_gl_entry: |
| 140 | start_date = getdate(add_days(prev_gl_entry[0].posting_date, 1)) |
| 141 | else: |
| 142 | start_date = item.service_start_date |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 143 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 144 | end_date = get_last_day(start_date) |
| 145 | if end_date >= item.service_end_date: |
| 146 | end_date = item.service_end_date |
| 147 | last_gl_entry = True |
Nabin Hait | 66d07c2 | 2019-03-29 13:25:11 +0530 | [diff] [blame] | 148 | elif item.service_stop_date and end_date >= item.service_stop_date: |
| 149 | end_date = item.service_stop_date |
| 150 | last_gl_entry = True |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 151 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 152 | if end_date > getdate(posting_date): |
| 153 | end_date = posting_date |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 154 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 155 | if getdate(start_date) <= getdate(end_date): |
| 156 | return start_date, end_date, last_gl_entry |
| 157 | else: |
| 158 | return None, None, None |
| 159 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 160 | def calculate_monthly_amount(doc, item, last_gl_entry, start_date, end_date, total_days, total_booking_days, account_currency): |
| 161 | amount, base_amount = 0, 0 |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 162 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 163 | if not last_gl_entry: |
| 164 | total_months = (item.service_end_date.year - item.service_start_date.year) * 12 + \ |
| 165 | (item.service_end_date.month - item.service_start_date.month) + 1 |
| 166 | |
| 167 | prorate_factor = flt(date_diff(item.service_end_date, item.service_start_date)) \ |
| 168 | / flt(date_diff(get_last_day(item.service_end_date), get_first_day(item.service_start_date))) |
| 169 | |
| 170 | actual_months = rounded(total_months * prorate_factor, 1) |
| 171 | |
| 172 | already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item) |
| 173 | base_amount = flt(item.base_net_amount / actual_months, item.precision("base_net_amount")) |
| 174 | |
| 175 | if base_amount + already_booked_amount > item.base_net_amount: |
| 176 | base_amount = item.base_net_amount - already_booked_amount |
| 177 | |
| 178 | if account_currency==doc.company_currency: |
| 179 | amount = base_amount |
| 180 | else: |
| 181 | amount = flt(item.net_amount/actual_months, item.precision("net_amount")) |
| 182 | if amount + already_booked_amount_in_account_currency > item.net_amount: |
| 183 | amount = item.net_amount - already_booked_amount_in_account_currency |
| 184 | |
| 185 | if not (get_first_day(start_date) == start_date and get_last_day(end_date) == end_date): |
| 186 | partial_month = flt(date_diff(end_date, start_date)) \ |
| 187 | / flt(date_diff(get_last_day(end_date), get_first_day(start_date))) |
| 188 | |
| 189 | base_amount = rounded(partial_month, 1) * base_amount |
| 190 | amount = rounded(partial_month, 1) * amount |
| 191 | else: |
| 192 | already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item) |
| 193 | base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount")) |
| 194 | if account_currency==doc.company_currency: |
| 195 | amount = base_amount |
| 196 | else: |
| 197 | amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount")) |
| 198 | |
| 199 | return amount, base_amount |
| 200 | |
| 201 | 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] | 202 | amount, base_amount = 0, 0 |
| 203 | if not last_gl_entry: |
| 204 | base_amount = flt(item.base_net_amount*total_booking_days/flt(total_days), item.precision("base_net_amount")) |
| 205 | if account_currency==doc.company_currency: |
| 206 | amount = base_amount |
| 207 | else: |
| 208 | amount = flt(item.net_amount*total_booking_days/flt(total_days), item.precision("net_amount")) |
| 209 | else: |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 210 | already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item) |
| 211 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 212 | base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount")) |
| 213 | if account_currency==doc.company_currency: |
| 214 | amount = base_amount |
| 215 | else: |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 216 | amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount")) |
| 217 | |
| 218 | return amount, base_amount |
| 219 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 220 | def get_already_booked_amount(doc, item): |
| 221 | if doc.doctype == "Sales Invoice": |
| 222 | total_credit_debit, total_credit_debit_currency = "debit", "debit_in_account_currency" |
| 223 | deferred_account = "deferred_revenue_account" |
| 224 | else: |
| 225 | total_credit_debit, total_credit_debit_currency = "credit", "credit_in_account_currency" |
| 226 | deferred_account = "deferred_expense_account" |
| 227 | |
| 228 | gl_entries_details = frappe.db.sql(''' |
| 229 | select sum({0}) as total_credit, sum({1}) as total_credit_in_account_currency, voucher_detail_no |
| 230 | from `tabGL Entry` where company=%s and account=%s and voucher_type=%s and voucher_no=%s and voucher_detail_no=%s |
Deepesh Garg | 366120f | 2022-03-01 11:56:20 +0530 | [diff] [blame] | 231 | and is_cancelled = 0 |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 232 | group by voucher_detail_no |
| 233 | '''.format(total_credit_debit, total_credit_debit_currency), |
| 234 | (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True) |
| 235 | |
| 236 | journal_entry_details = frappe.db.sql(''' |
| 237 | SELECT sum(c.{0}) as total_credit, sum(c.{1}) as total_credit_in_account_currency, reference_detail_no |
| 238 | FROM `tabJournal Entry` p , `tabJournal Entry Account` c WHERE p.name = c.parent and |
| 239 | p.company = %s and c.account=%s and c.reference_type=%s and c.reference_name=%s and c.reference_detail_no=%s |
| 240 | and p.docstatus < 2 group by reference_detail_no |
| 241 | '''.format(total_credit_debit, total_credit_debit_currency), |
| 242 | (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True) |
| 243 | |
| 244 | already_booked_amount = gl_entries_details[0].total_credit if gl_entries_details else 0 |
| 245 | already_booked_amount += journal_entry_details[0].total_credit if journal_entry_details else 0 |
| 246 | |
| 247 | if doc.currency == doc.company_currency: |
| 248 | already_booked_amount_in_account_currency = already_booked_amount |
| 249 | else: |
| 250 | already_booked_amount_in_account_currency = gl_entries_details[0].total_credit_in_account_currency if gl_entries_details else 0 |
| 251 | already_booked_amount_in_account_currency += journal_entry_details[0].total_credit_in_account_currency if journal_entry_details else 0 |
| 252 | |
| 253 | return already_booked_amount, already_booked_amount_in_account_currency |
| 254 | |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 255 | def book_deferred_income_or_expense(doc, deferred_process, posting_date=None): |
Nabin Hait | 27af6b3 | 2019-02-08 16:52:13 +0530 | [diff] [blame] | 256 | enable_check = "enable_deferred_revenue" \ |
| 257 | if doc.doctype=="Sales Invoice" else "enable_deferred_expense" |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 258 | |
Deepesh Garg | 30a647f | 2022-01-07 19:52:38 +0530 | [diff] [blame] | 259 | accounts_frozen_upto = frappe.get_cached_value('Accounts Settings', 'None', 'acc_frozen_upto') |
| 260 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 261 | def _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on): |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 262 | start_date, end_date, last_gl_entry = get_booking_dates(doc, item, posting_date=posting_date) |
| 263 | if not (start_date and end_date): return |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 264 | |
Deepesh Garg | 98f294a | 2022-01-03 19:40:47 +0530 | [diff] [blame] | 265 | account_currency = get_account_currency(item.expense_account or item.income_account) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 266 | if doc.doctype == "Sales Invoice": |
| 267 | against, project = doc.customer, doc.project |
| 268 | credit_account, debit_account = item.income_account, item.deferred_revenue_account |
| 269 | else: |
| 270 | against, project = doc.supplier, item.project |
| 271 | credit_account, debit_account = item.deferred_expense_account, item.expense_account |
| 272 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 273 | total_days = date_diff(item.service_end_date, item.service_start_date) + 1 |
| 274 | total_booking_days = date_diff(end_date, start_date) + 1 |
| 275 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 276 | if book_deferred_entries_based_on == 'Months': |
| 277 | amount, base_amount = calculate_monthly_amount(doc, item, last_gl_entry, |
| 278 | start_date, end_date, total_days, total_booking_days, account_currency) |
| 279 | else: |
| 280 | amount, base_amount = calculate_amount(doc, item, last_gl_entry, |
| 281 | total_days, total_booking_days, account_currency) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 282 | |
Deepesh Garg | f79a72d | 2021-06-24 14:14:46 +0530 | [diff] [blame] | 283 | if not amount: |
| 284 | return |
| 285 | |
Deepesh Garg | 30a647f | 2022-01-07 19:52:38 +0530 | [diff] [blame] | 286 | # check if books nor frozen till endate: |
Deepesh Garg | 366120f | 2022-03-01 11:56:20 +0530 | [diff] [blame] | 287 | if accounts_frozen_upto and (end_date) <= getdate(accounts_frozen_upto): |
Deepesh Garg | 30a647f | 2022-01-07 19:52:38 +0530 | [diff] [blame] | 288 | end_date = get_last_day(add_days(accounts_frozen_upto, 1)) |
| 289 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 290 | if via_journal_entry: |
| 291 | book_revenue_via_journal_entry(doc, credit_account, debit_account, against, amount, |
| 292 | base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process, submit_journal_entry) |
| 293 | else: |
| 294 | make_gl_entries(doc, credit_account, debit_account, against, |
| 295 | amount, base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process) |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 296 | |
| 297 | # Returned in case of any errors because it tries to submit the same record again and again in case of errors |
| 298 | if frappe.flags.deferred_accounting_error: |
| 299 | return |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 300 | |
| 301 | if getdate(end_date) < getdate(posting_date) and not last_gl_entry: |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 302 | _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 303 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 304 | via_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_via_journal_entry')) |
| 305 | submit_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'submit_journal_entries')) |
| 306 | book_deferred_entries_based_on = frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_based_on') |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 307 | |
| 308 | for item in doc.get('items'): |
| 309 | if item.get(enable_check): |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 310 | _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 311 | |
Chinmay D. Pai | 0d147b0 | 2020-05-24 00:54:04 +0530 | [diff] [blame] | 312 | def process_deferred_accounting(posting_date=None): |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 313 | ''' Converts deferred income/expense into income/expense |
| 314 | Executed via background jobs on every month end ''' |
| 315 | |
Chinmay D. Pai | 0d147b0 | 2020-05-24 00:54:04 +0530 | [diff] [blame] | 316 | if not posting_date: |
| 317 | posting_date = today() |
| 318 | |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 319 | if not cint(frappe.db.get_singles_value('Accounts Settings', 'automatically_process_deferred_accounting_entry')): |
| 320 | return |
| 321 | |
| 322 | start_date = add_months(today(), -1) |
| 323 | end_date = add_days(today(), -1) |
| 324 | |
Deepesh Garg | 5069095 | 2021-07-01 09:31:31 +0530 | [diff] [blame] | 325 | companies = frappe.get_all('Company') |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 326 | |
Deepesh Garg | 5069095 | 2021-07-01 09:31:31 +0530 | [diff] [blame] | 327 | for company in companies: |
| 328 | for record_type in ('Income', 'Expense'): |
| 329 | doc = frappe.get_doc(dict( |
| 330 | doctype='Process Deferred Accounting', |
| 331 | company=company.name, |
| 332 | posting_date=posting_date, |
| 333 | start_date=start_date, |
| 334 | end_date=end_date, |
| 335 | type=record_type |
| 336 | )) |
| 337 | |
| 338 | doc.insert() |
| 339 | doc.submit() |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 340 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 341 | def make_gl_entries(doc, credit_account, debit_account, against, |
Deepesh Garg | 7d61c03 | 2020-05-15 12:58:48 +0530 | [diff] [blame] | 342 | amount, base_amount, posting_date, project, account_currency, cost_center, item, deferred_process=None): |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 343 | # GL Entry for crediting the amount in the deferred expense |
| 344 | from erpnext.accounts.general_ledger import make_gl_entries |
| 345 | |
rohitwaghchaure | e123ec6 | 2019-11-06 15:25:00 +0530 | [diff] [blame] | 346 | if amount == 0: return |
| 347 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 348 | gl_entries = [] |
| 349 | gl_entries.append( |
| 350 | doc.get_gl_dict({ |
| 351 | "account": credit_account, |
| 352 | "against": against, |
| 353 | "credit": base_amount, |
| 354 | "credit_in_account_currency": amount, |
| 355 | "cost_center": cost_center, |
Deepesh Garg | 7d61c03 | 2020-05-15 12:58:48 +0530 | [diff] [blame] | 356 | "voucher_detail_no": item.name, |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 357 | 'posting_date': posting_date, |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 358 | 'project': project, |
| 359 | 'against_voucher_type': 'Process Deferred Accounting', |
| 360 | 'against_voucher': deferred_process |
Deepesh Garg | 7d61c03 | 2020-05-15 12:58:48 +0530 | [diff] [blame] | 361 | }, account_currency, item=item) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 362 | ) |
| 363 | # GL Entry to debit the amount from the expense |
| 364 | gl_entries.append( |
| 365 | doc.get_gl_dict({ |
| 366 | "account": debit_account, |
| 367 | "against": against, |
| 368 | "debit": base_amount, |
| 369 | "debit_in_account_currency": amount, |
| 370 | "cost_center": cost_center, |
Deepesh Garg | 7d61c03 | 2020-05-15 12:58:48 +0530 | [diff] [blame] | 371 | "voucher_detail_no": item.name, |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 372 | 'posting_date': posting_date, |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 373 | 'project': project, |
| 374 | 'against_voucher_type': 'Process Deferred Accounting', |
| 375 | 'against_voucher': deferred_process |
Deepesh Garg | 7d61c03 | 2020-05-15 12:58:48 +0530 | [diff] [blame] | 376 | }, account_currency, item=item) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 377 | ) |
| 378 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 379 | if gl_entries: |
Nabin Hait | 29fcb14 | 2019-02-13 17:18:12 +0530 | [diff] [blame] | 380 | try: |
| 381 | make_gl_entries(gl_entries, cancel=(doc.docstatus == 2), merge_entries=True) |
| 382 | frappe.db.commit() |
Deepesh Garg | f1a669c | 2021-09-29 22:26:33 +0530 | [diff] [blame] | 383 | except Exception as e: |
| 384 | if frappe.flags.in_test: |
Deepesh Garg | 67a001d | 2021-12-04 19:19:03 +0530 | [diff] [blame] | 385 | traceback = frappe.get_traceback() |
| 386 | frappe.log_error(title=_('Error while processing deferred accounting for Invoice {0}').format(doc.name), message=traceback) |
Deepesh Garg | 3c64e20 | 2021-12-04 20:05:37 +0530 | [diff] [blame] | 387 | raise e |
Deepesh Garg | f1a669c | 2021-09-29 22:26:33 +0530 | [diff] [blame] | 388 | else: |
| 389 | frappe.db.rollback() |
| 390 | traceback = frappe.get_traceback() |
Deepesh Garg | 67a001d | 2021-12-04 19:19:03 +0530 | [diff] [blame] | 391 | frappe.log_error(title=_('Error while processing deferred accounting for Invoice {0}').format(doc.name), message=traceback) |
Deepesh Garg | f1a669c | 2021-09-29 22:26:33 +0530 | [diff] [blame] | 392 | frappe.flags.deferred_accounting_error = True |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 393 | |
| 394 | def send_mail(deferred_process): |
Ankush Menat | b6783b1 | 2021-05-17 17:06:12 +0530 | [diff] [blame] | 395 | title = _("Error while processing deferred accounting for {0}").format(deferred_process) |
| 396 | link = get_link_to_form('Process Deferred Accounting', deferred_process) |
| 397 | content = _("Deferred accounting failed for some invoices:") + "\n" |
| 398 | content += _("Please check Process Deferred Accounting {0} and submit manually after resolving errors.").format(link) |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 399 | sendmail_to_system_managers(title, content) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 400 | |
| 401 | def book_revenue_via_journal_entry(doc, credit_account, debit_account, against, |
| 402 | amount, base_amount, posting_date, project, account_currency, cost_center, item, |
| 403 | deferred_process=None, submit='No'): |
| 404 | |
| 405 | if amount == 0: return |
| 406 | |
| 407 | journal_entry = frappe.new_doc('Journal Entry') |
| 408 | journal_entry.posting_date = posting_date |
| 409 | journal_entry.company = doc.company |
| 410 | journal_entry.voucher_type = 'Deferred Revenue' if doc.doctype == 'Sales Invoice' \ |
| 411 | else 'Deferred Expense' |
| 412 | |
| 413 | debit_entry = { |
| 414 | 'account': credit_account, |
| 415 | 'credit': base_amount, |
| 416 | 'credit_in_account_currency': amount, |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 417 | 'account_currency': account_currency, |
| 418 | 'reference_name': doc.name, |
| 419 | 'reference_type': doc.doctype, |
| 420 | 'reference_detail_no': item.name, |
| 421 | 'cost_center': cost_center, |
| 422 | 'project': project, |
| 423 | } |
| 424 | |
| 425 | credit_entry = { |
| 426 | 'account': debit_account, |
| 427 | 'debit': base_amount, |
| 428 | 'debit_in_account_currency': amount, |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 429 | 'account_currency': account_currency, |
| 430 | 'reference_name': doc.name, |
| 431 | 'reference_type': doc.doctype, |
| 432 | 'reference_detail_no': item.name, |
| 433 | 'cost_center': cost_center, |
| 434 | 'project': project, |
| 435 | } |
| 436 | |
| 437 | for dimension in get_accounting_dimensions(): |
| 438 | debit_entry.update({ |
| 439 | dimension: item.get(dimension) |
| 440 | }) |
| 441 | |
| 442 | credit_entry.update({ |
| 443 | dimension: item.get(dimension) |
| 444 | }) |
| 445 | |
| 446 | journal_entry.append('accounts', debit_entry) |
| 447 | journal_entry.append('accounts', credit_entry) |
| 448 | |
| 449 | try: |
| 450 | journal_entry.save() |
| 451 | |
| 452 | if submit: |
| 453 | journal_entry.submit() |
Deepesh Garg | 0ba4fce | 2021-12-04 19:25:44 +0530 | [diff] [blame] | 454 | |
| 455 | frappe.db.commit() |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 456 | except Exception: |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 457 | frappe.db.rollback() |
| 458 | traceback = frappe.get_traceback() |
Deepesh Garg | 67a001d | 2021-12-04 19:19:03 +0530 | [diff] [blame] | 459 | frappe.log_error(title=_('Error while processing deferred accounting for Invoice {0}').format(doc.name), message=traceback) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 460 | |
| 461 | frappe.flags.deferred_accounting_error = True |
| 462 | |
| 463 | def get_deferred_booking_accounts(doctype, voucher_detail_no, dr_or_cr): |
| 464 | |
| 465 | if doctype == 'Sales Invoice': |
| 466 | credit_account, debit_account = frappe.db.get_value('Sales Invoice Item', {'name': voucher_detail_no}, |
| 467 | ['income_account', 'deferred_revenue_account']) |
| 468 | else: |
| 469 | credit_account, debit_account = frappe.db.get_value('Purchase Invoice Item', {'name': voucher_detail_no}, |
| 470 | ['deferred_expense_account', 'expense_account']) |
| 471 | |
| 472 | if dr_or_cr == 'Debit': |
| 473 | return debit_account |
| 474 | else: |
| 475 | return credit_account |