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): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 25 | """Validates service_stop_date for Purchase Invoice and Sales Invoice""" |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 26 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 27 | enable_check = ( |
| 28 | "enable_deferred_revenue" if doc.doctype == "Sales Invoice" else "enable_deferred_expense" |
| 29 | ) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 30 | |
| 31 | old_stop_dates = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 32 | old_doc = frappe.db.get_all( |
| 33 | "{0} Item".format(doc.doctype), {"parent": doc.name}, ["name", "service_stop_date"] |
| 34 | ) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 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: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 40 | if not item.get(enable_check): |
| 41 | continue |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 42 | |
| 43 | if item.service_stop_date: |
| 44 | if date_diff(item.service_stop_date, item.service_start_date) < 0: |
| 45 | frappe.throw(_("Service Stop Date cannot be before Service Start Date")) |
| 46 | |
| 47 | if date_diff(item.service_stop_date, item.service_end_date) > 0: |
| 48 | frappe.throw(_("Service Stop Date cannot be after Service End Date")) |
| 49 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 50 | if ( |
| 51 | old_stop_dates |
| 52 | and old_stop_dates.get(item.name) |
| 53 | and item.service_stop_date != old_stop_dates.get(item.name) |
| 54 | ): |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 55 | 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] | 56 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 57 | |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 58 | def build_conditions(process_type, account, company): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 59 | conditions = "" |
| 60 | deferred_account = ( |
| 61 | "item.deferred_revenue_account" if process_type == "Income" else "item.deferred_expense_account" |
| 62 | ) |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 63 | |
| 64 | if account: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 65 | conditions += "AND %s='%s'" % (deferred_account, account) |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 66 | elif company: |
Ankush Menat | f3b3d81 | 2021-05-17 16:43:38 +0530 | [diff] [blame] | 67 | conditions += f"AND p.company = {frappe.db.escape(company)}" |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 68 | |
| 69 | return conditions |
| 70 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 71 | |
| 72 | def convert_deferred_expense_to_expense( |
| 73 | deferred_process, start_date=None, end_date=None, conditions="" |
| 74 | ): |
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 purchase invoice for which GL entries has to be done |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 83 | invoices = frappe.db.sql_list( |
| 84 | """ |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 85 | select distinct item.parent |
| 86 | from `tabPurchase Invoice Item` item, `tabPurchase Invoice` p |
| 87 | where item.service_start_date<=%s and item.service_end_date>=%s |
| 88 | and item.enable_deferred_expense = 1 and item.parent=p.name |
| 89 | and item.docstatus = 1 and ifnull(item.amount, 0) > 0 |
| 90 | {0} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 91 | """.format( |
| 92 | conditions |
| 93 | ), |
| 94 | (end_date, start_date), |
| 95 | ) # nosec |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 96 | |
| 97 | # For each invoice, book deferred expense |
| 98 | for invoice in invoices: |
| 99 | doc = frappe.get_doc("Purchase Invoice", invoice) |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 100 | book_deferred_income_or_expense(doc, deferred_process, end_date) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 101 | |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 102 | if frappe.flags.deferred_accounting_error: |
| 103 | send_mail(deferred_process) |
| 104 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 105 | |
| 106 | def convert_deferred_revenue_to_income( |
| 107 | deferred_process, start_date=None, end_date=None, conditions="" |
| 108 | ): |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 109 | # 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] | 110 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 111 | if not start_date: |
| 112 | start_date = add_months(today(), -1) |
| 113 | if not end_date: |
| 114 | end_date = add_days(today(), -1) |
| 115 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 116 | # check for the sales invoice for which GL entries has to be done |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 117 | invoices = frappe.db.sql_list( |
| 118 | """ |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 119 | select distinct item.parent |
| 120 | from `tabSales Invoice Item` item, `tabSales Invoice` p |
| 121 | where item.service_start_date<=%s and item.service_end_date>=%s |
| 122 | and item.enable_deferred_revenue = 1 and item.parent=p.name |
| 123 | and item.docstatus = 1 and ifnull(item.amount, 0) > 0 |
| 124 | {0} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 125 | """.format( |
| 126 | conditions |
| 127 | ), |
| 128 | (end_date, start_date), |
| 129 | ) # nosec |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 130 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 131 | for invoice in invoices: |
| 132 | doc = frappe.get_doc("Sales Invoice", invoice) |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 133 | book_deferred_income_or_expense(doc, deferred_process, end_date) |
| 134 | |
| 135 | if frappe.flags.deferred_accounting_error: |
| 136 | send_mail(deferred_process) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 137 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 138 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 139 | def get_booking_dates(doc, item, posting_date=None): |
| 140 | if not posting_date: |
| 141 | posting_date = add_days(today(), -1) |
| 142 | |
| 143 | last_gl_entry = False |
| 144 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 145 | deferred_account = ( |
| 146 | "deferred_revenue_account" if doc.doctype == "Sales Invoice" else "deferred_expense_account" |
| 147 | ) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 148 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 149 | prev_gl_entry = frappe.db.sql( |
| 150 | """ |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 151 | select name, posting_date from `tabGL Entry` where company=%s and account=%s and |
| 152 | voucher_type=%s and voucher_no=%s and voucher_detail_no=%s |
Deepesh Garg | 366120f | 2022-03-01 11:56:20 +0530 | [diff] [blame] | 153 | and is_cancelled = 0 |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 154 | order by posting_date desc limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 155 | """, |
| 156 | (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), |
| 157 | as_dict=True, |
| 158 | ) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 159 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 160 | prev_gl_via_je = frappe.db.sql( |
| 161 | """ |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 162 | SELECT p.name, p.posting_date FROM `tabJournal Entry` p, `tabJournal Entry Account` c |
| 163 | WHERE p.name = c.parent and p.company=%s and c.account=%s |
| 164 | and c.reference_type=%s and c.reference_name=%s |
| 165 | and c.reference_detail_no=%s and c.docstatus < 2 order by posting_date desc limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 166 | """, |
| 167 | (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), |
| 168 | as_dict=True, |
| 169 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 170 | |
| 171 | if prev_gl_via_je: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 172 | if (not prev_gl_entry) or ( |
| 173 | prev_gl_entry and prev_gl_entry[0].posting_date < prev_gl_via_je[0].posting_date |
| 174 | ): |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 175 | prev_gl_entry = prev_gl_via_je |
| 176 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 177 | if prev_gl_entry: |
| 178 | start_date = getdate(add_days(prev_gl_entry[0].posting_date, 1)) |
| 179 | else: |
| 180 | start_date = item.service_start_date |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 181 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 182 | end_date = get_last_day(start_date) |
| 183 | if end_date >= item.service_end_date: |
| 184 | end_date = item.service_end_date |
| 185 | last_gl_entry = True |
Nabin Hait | 66d07c2 | 2019-03-29 13:25:11 +0530 | [diff] [blame] | 186 | elif item.service_stop_date and end_date >= item.service_stop_date: |
| 187 | end_date = item.service_stop_date |
| 188 | last_gl_entry = True |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 189 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 190 | if end_date > getdate(posting_date): |
| 191 | end_date = posting_date |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 192 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 193 | if getdate(start_date) <= getdate(end_date): |
| 194 | return start_date, end_date, last_gl_entry |
| 195 | else: |
| 196 | return None, None, None |
| 197 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 198 | |
| 199 | def calculate_monthly_amount( |
| 200 | doc, item, last_gl_entry, start_date, end_date, total_days, total_booking_days, account_currency |
| 201 | ): |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 202 | amount, base_amount = 0, 0 |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 203 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 204 | if not last_gl_entry: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 205 | total_months = ( |
| 206 | (item.service_end_date.year - item.service_start_date.year) * 12 |
| 207 | + (item.service_end_date.month - item.service_start_date.month) |
| 208 | + 1 |
| 209 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 210 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 211 | prorate_factor = flt(date_diff(item.service_end_date, item.service_start_date)) / flt( |
| 212 | date_diff(get_last_day(item.service_end_date), get_first_day(item.service_start_date)) |
| 213 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 214 | |
| 215 | actual_months = rounded(total_months * prorate_factor, 1) |
| 216 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 217 | already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount( |
| 218 | doc, item |
| 219 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 220 | base_amount = flt(item.base_net_amount / actual_months, item.precision("base_net_amount")) |
| 221 | |
| 222 | if base_amount + already_booked_amount > item.base_net_amount: |
| 223 | base_amount = item.base_net_amount - already_booked_amount |
| 224 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 225 | if account_currency == doc.company_currency: |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 226 | amount = base_amount |
| 227 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 228 | amount = flt(item.net_amount / actual_months, item.precision("net_amount")) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 229 | if amount + already_booked_amount_in_account_currency > item.net_amount: |
| 230 | amount = item.net_amount - already_booked_amount_in_account_currency |
| 231 | |
| 232 | if not (get_first_day(start_date) == start_date and get_last_day(end_date) == end_date): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 233 | partial_month = flt(date_diff(end_date, start_date)) / flt( |
| 234 | date_diff(get_last_day(end_date), get_first_day(start_date)) |
| 235 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 236 | |
| 237 | base_amount = rounded(partial_month, 1) * base_amount |
| 238 | amount = rounded(partial_month, 1) * amount |
| 239 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 240 | already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount( |
| 241 | doc, item |
| 242 | ) |
| 243 | base_amount = flt( |
| 244 | item.base_net_amount - already_booked_amount, item.precision("base_net_amount") |
| 245 | ) |
| 246 | if account_currency == doc.company_currency: |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 247 | amount = base_amount |
| 248 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 249 | amount = flt( |
| 250 | item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount") |
| 251 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 252 | |
| 253 | return amount, base_amount |
| 254 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 255 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 256 | 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] | 257 | amount, base_amount = 0, 0 |
| 258 | if not last_gl_entry: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 259 | base_amount = flt( |
| 260 | item.base_net_amount * total_booking_days / flt(total_days), item.precision("base_net_amount") |
| 261 | ) |
| 262 | if account_currency == doc.company_currency: |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 263 | amount = base_amount |
| 264 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 265 | amount = flt( |
| 266 | item.net_amount * total_booking_days / flt(total_days), item.precision("net_amount") |
| 267 | ) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 268 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 269 | already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount( |
| 270 | doc, item |
| 271 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 272 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 273 | base_amount = flt( |
| 274 | item.base_net_amount - already_booked_amount, item.precision("base_net_amount") |
| 275 | ) |
| 276 | if account_currency == doc.company_currency: |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 277 | amount = base_amount |
| 278 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 279 | amount = flt( |
| 280 | item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount") |
| 281 | ) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 282 | |
| 283 | return amount, base_amount |
| 284 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 285 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 286 | def get_already_booked_amount(doc, item): |
| 287 | if doc.doctype == "Sales Invoice": |
| 288 | total_credit_debit, total_credit_debit_currency = "debit", "debit_in_account_currency" |
| 289 | deferred_account = "deferred_revenue_account" |
| 290 | else: |
| 291 | total_credit_debit, total_credit_debit_currency = "credit", "credit_in_account_currency" |
| 292 | deferred_account = "deferred_expense_account" |
| 293 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 294 | gl_entries_details = frappe.db.sql( |
| 295 | """ |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 296 | select sum({0}) as total_credit, sum({1}) as total_credit_in_account_currency, voucher_detail_no |
| 297 | 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] | 298 | and is_cancelled = 0 |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 299 | group by voucher_detail_no |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 300 | """.format( |
| 301 | total_credit_debit, total_credit_debit_currency |
| 302 | ), |
| 303 | (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), |
| 304 | as_dict=True, |
| 305 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 306 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 307 | journal_entry_details = frappe.db.sql( |
| 308 | """ |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 309 | SELECT sum(c.{0}) as total_credit, sum(c.{1}) as total_credit_in_account_currency, reference_detail_no |
| 310 | FROM `tabJournal Entry` p , `tabJournal Entry Account` c WHERE p.name = c.parent and |
| 311 | p.company = %s and c.account=%s and c.reference_type=%s and c.reference_name=%s and c.reference_detail_no=%s |
| 312 | and p.docstatus < 2 group by reference_detail_no |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 313 | """.format( |
| 314 | total_credit_debit, total_credit_debit_currency |
| 315 | ), |
| 316 | (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), |
| 317 | as_dict=True, |
| 318 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 319 | |
| 320 | already_booked_amount = gl_entries_details[0].total_credit if gl_entries_details else 0 |
| 321 | already_booked_amount += journal_entry_details[0].total_credit if journal_entry_details else 0 |
| 322 | |
| 323 | if doc.currency == doc.company_currency: |
| 324 | already_booked_amount_in_account_currency = already_booked_amount |
| 325 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 326 | already_booked_amount_in_account_currency = ( |
| 327 | gl_entries_details[0].total_credit_in_account_currency if gl_entries_details else 0 |
| 328 | ) |
| 329 | already_booked_amount_in_account_currency += ( |
| 330 | journal_entry_details[0].total_credit_in_account_currency if journal_entry_details else 0 |
| 331 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 332 | |
| 333 | return already_booked_amount, already_booked_amount_in_account_currency |
| 334 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 335 | |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 336 | def book_deferred_income_or_expense(doc, deferred_process, posting_date=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 337 | enable_check = ( |
| 338 | "enable_deferred_revenue" if doc.doctype == "Sales Invoice" else "enable_deferred_expense" |
| 339 | ) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 340 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 341 | accounts_frozen_upto = frappe.get_cached_value("Accounts Settings", "None", "acc_frozen_upto") |
Deepesh Garg | 30a647f | 2022-01-07 19:52:38 +0530 | [diff] [blame] | 342 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 343 | def _book_deferred_revenue_or_expense( |
| 344 | item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on |
| 345 | ): |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 346 | start_date, end_date, last_gl_entry = get_booking_dates(doc, item, posting_date=posting_date) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 347 | if not (start_date and end_date): |
| 348 | return |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 349 | |
Deepesh Garg | 98f294a | 2022-01-03 19:40:47 +0530 | [diff] [blame] | 350 | account_currency = get_account_currency(item.expense_account or item.income_account) |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 351 | if doc.doctype == "Sales Invoice": |
| 352 | against, project = doc.customer, doc.project |
| 353 | credit_account, debit_account = item.income_account, item.deferred_revenue_account |
| 354 | else: |
| 355 | against, project = doc.supplier, item.project |
| 356 | credit_account, debit_account = item.deferred_expense_account, item.expense_account |
| 357 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 358 | total_days = date_diff(item.service_end_date, item.service_start_date) + 1 |
| 359 | total_booking_days = date_diff(end_date, start_date) + 1 |
| 360 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 361 | if book_deferred_entries_based_on == "Months": |
| 362 | amount, base_amount = calculate_monthly_amount( |
| 363 | doc, |
| 364 | item, |
| 365 | last_gl_entry, |
| 366 | start_date, |
| 367 | end_date, |
| 368 | total_days, |
| 369 | total_booking_days, |
| 370 | account_currency, |
| 371 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 372 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 373 | amount, base_amount = calculate_amount( |
| 374 | doc, item, last_gl_entry, total_days, total_booking_days, account_currency |
| 375 | ) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 376 | |
Deepesh Garg | f79a72d | 2021-06-24 14:14:46 +0530 | [diff] [blame] | 377 | if not amount: |
| 378 | return |
| 379 | |
Deepesh Garg | 30a647f | 2022-01-07 19:52:38 +0530 | [diff] [blame] | 380 | # check if books nor frozen till endate: |
Deepesh Garg | a3ab8f9 | 2023-01-03 17:51:41 +0530 | [diff] [blame] | 381 | if accounts_frozen_upto and getdate(end_date) <= getdate(accounts_frozen_upto): |
Deepesh Garg | 30a647f | 2022-01-07 19:52:38 +0530 | [diff] [blame] | 382 | end_date = get_last_day(add_days(accounts_frozen_upto, 1)) |
| 383 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 384 | if via_journal_entry: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 385 | book_revenue_via_journal_entry( |
| 386 | doc, |
| 387 | credit_account, |
| 388 | debit_account, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 389 | amount, |
| 390 | base_amount, |
| 391 | end_date, |
| 392 | project, |
| 393 | account_currency, |
| 394 | item.cost_center, |
| 395 | item, |
| 396 | deferred_process, |
| 397 | submit_journal_entry, |
| 398 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 399 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 400 | make_gl_entries( |
| 401 | doc, |
| 402 | credit_account, |
| 403 | debit_account, |
| 404 | against, |
| 405 | amount, |
| 406 | base_amount, |
| 407 | end_date, |
| 408 | project, |
| 409 | account_currency, |
| 410 | item.cost_center, |
| 411 | item, |
| 412 | deferred_process, |
| 413 | ) |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 414 | |
| 415 | # Returned in case of any errors because it tries to submit the same record again and again in case of errors |
| 416 | if frappe.flags.deferred_accounting_error: |
| 417 | return |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 418 | |
| 419 | if getdate(end_date) < getdate(posting_date) and not last_gl_entry: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 420 | _book_deferred_revenue_or_expense( |
| 421 | item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on |
| 422 | ) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 423 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 424 | via_journal_entry = cint( |
| 425 | frappe.db.get_singles_value("Accounts Settings", "book_deferred_entries_via_journal_entry") |
| 426 | ) |
| 427 | submit_journal_entry = cint( |
| 428 | frappe.db.get_singles_value("Accounts Settings", "submit_journal_entries") |
| 429 | ) |
| 430 | book_deferred_entries_based_on = frappe.db.get_singles_value( |
| 431 | "Accounts Settings", "book_deferred_entries_based_on" |
| 432 | ) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 433 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 434 | for item in doc.get("items"): |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 435 | if item.get(enable_check): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 436 | _book_deferred_revenue_or_expense( |
| 437 | item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on |
| 438 | ) |
| 439 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 440 | |
Chinmay D. Pai | 0d147b0 | 2020-05-24 00:54:04 +0530 | [diff] [blame] | 441 | def process_deferred_accounting(posting_date=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 442 | """Converts deferred income/expense into income/expense |
| 443 | Executed via background jobs on every month end""" |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 444 | |
Chinmay D. Pai | 0d147b0 | 2020-05-24 00:54:04 +0530 | [diff] [blame] | 445 | if not posting_date: |
| 446 | posting_date = today() |
| 447 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 448 | if not cint( |
| 449 | frappe.db.get_singles_value( |
| 450 | "Accounts Settings", "automatically_process_deferred_accounting_entry" |
| 451 | ) |
| 452 | ): |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 453 | return |
| 454 | |
| 455 | start_date = add_months(today(), -1) |
| 456 | end_date = add_days(today(), -1) |
| 457 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 458 | companies = frappe.get_all("Company") |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 459 | |
Deepesh Garg | 5069095 | 2021-07-01 09:31:31 +0530 | [diff] [blame] | 460 | for company in companies: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 461 | for record_type in ("Income", "Expense"): |
| 462 | doc = frappe.get_doc( |
| 463 | dict( |
| 464 | doctype="Process Deferred Accounting", |
| 465 | company=company.name, |
| 466 | posting_date=posting_date, |
| 467 | start_date=start_date, |
| 468 | end_date=end_date, |
| 469 | type=record_type, |
| 470 | ) |
| 471 | ) |
Deepesh Garg | 5069095 | 2021-07-01 09:31:31 +0530 | [diff] [blame] | 472 | |
| 473 | doc.insert() |
| 474 | doc.submit() |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 475 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 476 | |
| 477 | def make_gl_entries( |
| 478 | doc, |
| 479 | credit_account, |
| 480 | debit_account, |
| 481 | against, |
| 482 | amount, |
| 483 | base_amount, |
| 484 | posting_date, |
| 485 | project, |
| 486 | account_currency, |
| 487 | cost_center, |
| 488 | item, |
| 489 | deferred_process=None, |
| 490 | ): |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 491 | # GL Entry for crediting the amount in the deferred expense |
| 492 | from erpnext.accounts.general_ledger import make_gl_entries |
| 493 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 494 | if amount == 0: |
| 495 | return |
rohitwaghchaure | e123ec6 | 2019-11-06 15:25:00 +0530 | [diff] [blame] | 496 | |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 497 | gl_entries = [] |
| 498 | gl_entries.append( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 499 | doc.get_gl_dict( |
| 500 | { |
| 501 | "account": credit_account, |
| 502 | "against": against, |
| 503 | "credit": base_amount, |
| 504 | "credit_in_account_currency": amount, |
| 505 | "cost_center": cost_center, |
| 506 | "voucher_detail_no": item.name, |
| 507 | "posting_date": posting_date, |
| 508 | "project": project, |
| 509 | "against_voucher_type": "Process Deferred Accounting", |
| 510 | "against_voucher": deferred_process, |
| 511 | }, |
| 512 | account_currency, |
| 513 | item=item, |
| 514 | ) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 515 | ) |
| 516 | # GL Entry to debit the amount from the expense |
| 517 | gl_entries.append( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 518 | doc.get_gl_dict( |
| 519 | { |
| 520 | "account": debit_account, |
| 521 | "against": against, |
| 522 | "debit": base_amount, |
| 523 | "debit_in_account_currency": amount, |
| 524 | "cost_center": cost_center, |
| 525 | "voucher_detail_no": item.name, |
| 526 | "posting_date": posting_date, |
| 527 | "project": project, |
| 528 | "against_voucher_type": "Process Deferred Accounting", |
| 529 | "against_voucher": deferred_process, |
| 530 | }, |
| 531 | account_currency, |
| 532 | item=item, |
| 533 | ) |
Nabin Hait | 0a90ce5 | 2019-03-28 19:43:02 +0530 | [diff] [blame] | 534 | ) |
| 535 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 536 | if gl_entries: |
Nabin Hait | 29fcb14 | 2019-02-13 17:18:12 +0530 | [diff] [blame] | 537 | try: |
| 538 | make_gl_entries(gl_entries, cancel=(doc.docstatus == 2), merge_entries=True) |
| 539 | frappe.db.commit() |
Deepesh Garg | f1a669c | 2021-09-29 22:26:33 +0530 | [diff] [blame] | 540 | except Exception as e: |
| 541 | if frappe.flags.in_test: |
Rushabh Mehta | 548afba | 2022-05-02 15:04:26 +0530 | [diff] [blame] | 542 | doc.log_error(f"Error while processing deferred accounting for Invoice {doc.name}") |
Deepesh Garg | 3c64e20 | 2021-12-04 20:05:37 +0530 | [diff] [blame] | 543 | raise e |
Deepesh Garg | f1a669c | 2021-09-29 22:26:33 +0530 | [diff] [blame] | 544 | else: |
| 545 | frappe.db.rollback() |
Rushabh Mehta | 548afba | 2022-05-02 15:04:26 +0530 | [diff] [blame] | 546 | doc.log_error(f"Error while processing deferred accounting for Invoice {doc.name}") |
Deepesh Garg | f1a669c | 2021-09-29 22:26:33 +0530 | [diff] [blame] | 547 | frappe.flags.deferred_accounting_error = True |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 548 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 549 | |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 550 | def send_mail(deferred_process): |
Ankush Menat | b6783b1 | 2021-05-17 17:06:12 +0530 | [diff] [blame] | 551 | title = _("Error while processing deferred accounting for {0}").format(deferred_process) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 552 | link = get_link_to_form("Process Deferred Accounting", deferred_process) |
Ankush Menat | b6783b1 | 2021-05-17 17:06:12 +0530 | [diff] [blame] | 553 | content = _("Deferred accounting failed for some invoices:") + "\n" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 554 | content += _( |
| 555 | "Please check Process Deferred Accounting {0} and submit manually after resolving errors." |
| 556 | ).format(link) |
Mangesh-Khairnar | 2f7861a | 2020-05-02 20:09:33 +0530 | [diff] [blame] | 557 | sendmail_to_system_managers(title, content) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 558 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 559 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 560 | def book_revenue_via_journal_entry( |
| 561 | doc, |
| 562 | credit_account, |
| 563 | debit_account, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 564 | amount, |
| 565 | base_amount, |
| 566 | posting_date, |
| 567 | project, |
| 568 | account_currency, |
| 569 | cost_center, |
| 570 | item, |
| 571 | deferred_process=None, |
| 572 | submit="No", |
| 573 | ): |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 574 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 575 | if amount == 0: |
| 576 | return |
| 577 | |
| 578 | journal_entry = frappe.new_doc("Journal Entry") |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 579 | journal_entry.posting_date = posting_date |
| 580 | journal_entry.company = doc.company |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 581 | journal_entry.voucher_type = ( |
| 582 | "Deferred Revenue" if doc.doctype == "Sales Invoice" else "Deferred Expense" |
| 583 | ) |
Deepesh Garg | 9bf5f76 | 2022-04-06 17:33:46 +0530 | [diff] [blame] | 584 | journal_entry.process_deferred_accounting = deferred_process |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 585 | |
| 586 | debit_entry = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 587 | "account": credit_account, |
| 588 | "credit": base_amount, |
| 589 | "credit_in_account_currency": amount, |
| 590 | "account_currency": account_currency, |
| 591 | "reference_name": doc.name, |
| 592 | "reference_type": doc.doctype, |
| 593 | "reference_detail_no": item.name, |
| 594 | "cost_center": cost_center, |
| 595 | "project": project, |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | credit_entry = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 599 | "account": debit_account, |
| 600 | "debit": base_amount, |
| 601 | "debit_in_account_currency": amount, |
| 602 | "account_currency": account_currency, |
| 603 | "reference_name": doc.name, |
| 604 | "reference_type": doc.doctype, |
| 605 | "reference_detail_no": item.name, |
| 606 | "cost_center": cost_center, |
| 607 | "project": project, |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | for dimension in get_accounting_dimensions(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 611 | debit_entry.update({dimension: item.get(dimension)}) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 612 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 613 | credit_entry.update({dimension: item.get(dimension)}) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 614 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 615 | journal_entry.append("accounts", debit_entry) |
| 616 | journal_entry.append("accounts", credit_entry) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 617 | |
| 618 | try: |
| 619 | journal_entry.save() |
| 620 | |
| 621 | if submit: |
| 622 | journal_entry.submit() |
Deepesh Garg | 0ba4fce | 2021-12-04 19:25:44 +0530 | [diff] [blame] | 623 | |
| 624 | frappe.db.commit() |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 625 | except Exception: |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 626 | frappe.db.rollback() |
Rushabh Mehta | 548afba | 2022-05-02 15:04:26 +0530 | [diff] [blame] | 627 | doc.log_error(f"Error while processing deferred accounting for Invoice {doc.name}") |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 628 | frappe.flags.deferred_accounting_error = True |
| 629 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 630 | |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 631 | def get_deferred_booking_accounts(doctype, voucher_detail_no, dr_or_cr): |
| 632 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 633 | if doctype == "Sales Invoice": |
| 634 | credit_account, debit_account = frappe.db.get_value( |
| 635 | "Sales Invoice Item", |
| 636 | {"name": voucher_detail_no}, |
| 637 | ["income_account", "deferred_revenue_account"], |
| 638 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 639 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 640 | credit_account, debit_account = frappe.db.get_value( |
| 641 | "Purchase Invoice Item", |
| 642 | {"name": voucher_detail_no}, |
| 643 | ["deferred_expense_account", "expense_account"], |
| 644 | ) |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 645 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 646 | if dr_or_cr == "Debit": |
Deepesh Garg | 7cc1cf3 | 2020-06-23 09:57:56 +0530 | [diff] [blame] | 647 | return debit_account |
| 648 | else: |
| 649 | return credit_account |