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