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