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