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