blob: d5ab1c170425d0fe6a2474225f79788d40514779 [file] [log] [blame]
Zarrare83ff382018-09-21 15:45:40 +05301from __future__ import unicode_literals
2
3import frappe
4from frappe import _
Deepesh Garg7cc1cf32020-06-23 09:57:56 +05305from frappe.utils import date_diff, add_months, today, getdate, add_days, flt, get_last_day, get_first_day, cint, get_link_to_form, rounded
Zarrare83ff382018-09-21 15:45:40 +05306from erpnext.accounts.utils import get_account_currency
Nabin Hait0a90ce52019-03-28 19:43:02 +05307from frappe.email import sendmail_to_system_managers
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +05308from frappe.utils.background_jobs import enqueue
Deepesh Garg7cc1cf32020-06-23 09:57:56 +05309from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_accounting_dimensions
Zarrare83ff382018-09-21 15:45:40 +053010
11def validate_service_stop_date(doc):
12 ''' Validates service_stop_date for Purchase Invoice and Sales Invoice '''
13
14 enable_check = "enable_deferred_revenue" \
15 if doc.doctype=="Sales Invoice" else "enable_deferred_expense"
16
17 old_stop_dates = {}
18 old_doc = frappe.db.get_all("{0} Item".format(doc.doctype),
19 {"parent": doc.name}, ["name", "service_stop_date"])
20
21 for d in old_doc:
22 old_stop_dates[d.name] = d.service_stop_date or ""
23
24 for item in doc.items:
25 if not item.get(enable_check): continue
26
27 if item.service_stop_date:
28 if date_diff(item.service_stop_date, item.service_start_date) < 0:
29 frappe.throw(_("Service Stop Date cannot be before Service Start Date"))
30
31 if date_diff(item.service_stop_date, item.service_end_date) > 0:
32 frappe.throw(_("Service Stop Date cannot be after Service End Date"))
33
Rohit Waghchaurec699b2a2018-09-24 11:57:48 +053034 if old_stop_dates and old_stop_dates.get(item.name) and item.service_stop_date!=old_stop_dates.get(item.name):
Suraj Shetty48e9bc32020-01-29 15:06:18 +053035 frappe.throw(_("Cannot change Service Stop Date for item in row {0}").format(item.idx))
Zarrare83ff382018-09-21 15:45:40 +053036
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053037def build_conditions(process_type, account, company):
38 conditions=''
39 deferred_account = "item.deferred_revenue_account" if process_type=="Income" else "item.deferred_expense_account"
40
41 if account:
42 conditions += "AND %s='%s'"%(deferred_account, account)
43 elif company:
44 conditions += "AND p.company='%s'"%(company)
45
46 return conditions
47
48def convert_deferred_expense_to_expense(deferred_process, start_date=None, end_date=None, conditions=''):
Nabin Hait0a90ce52019-03-28 19:43:02 +053049 # book the expense/income on the last day, but it will be trigger on the 1st of month at 12:00 AM
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053050
Nabin Hait0a90ce52019-03-28 19:43:02 +053051 if not start_date:
52 start_date = add_months(today(), -1)
53 if not end_date:
54 end_date = add_days(today(), -1)
55
Zarrare83ff382018-09-21 15:45:40 +053056 # check for the purchase invoice for which GL entries has to be done
57 invoices = frappe.db.sql_list('''
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053058 select distinct item.parent
59 from `tabPurchase Invoice Item` item, `tabPurchase Invoice` p
60 where item.service_start_date<=%s and item.service_end_date>=%s
61 and item.enable_deferred_expense = 1 and item.parent=p.name
62 and item.docstatus = 1 and ifnull(item.amount, 0) > 0
63 {0}
64 '''.format(conditions), (end_date, start_date)) #nosec
Zarrare83ff382018-09-21 15:45:40 +053065
66 # For each invoice, book deferred expense
67 for invoice in invoices:
68 doc = frappe.get_doc("Purchase Invoice", invoice)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053069 book_deferred_income_or_expense(doc, deferred_process, end_date)
Zarrare83ff382018-09-21 15:45:40 +053070
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053071 if frappe.flags.deferred_accounting_error:
72 send_mail(deferred_process)
73
74def convert_deferred_revenue_to_income(deferred_process, start_date=None, end_date=None, conditions=''):
Nabin Hait0a90ce52019-03-28 19:43:02 +053075 # book the expense/income on the last day, but it will be trigger on the 1st of month at 12:00 AM
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053076
Nabin Hait0a90ce52019-03-28 19:43:02 +053077 if not start_date:
78 start_date = add_months(today(), -1)
79 if not end_date:
80 end_date = add_days(today(), -1)
81
Zarrare83ff382018-09-21 15:45:40 +053082 # check for the sales invoice for which GL entries has to be done
83 invoices = frappe.db.sql_list('''
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053084 select distinct item.parent
85 from `tabSales Invoice Item` item, `tabSales Invoice` p
86 where item.service_start_date<=%s and item.service_end_date>=%s
87 and item.enable_deferred_revenue = 1 and item.parent=p.name
88 and item.docstatus = 1 and ifnull(item.amount, 0) > 0
89 {0}
90 '''.format(conditions), (end_date, start_date)) #nosec
Zarrare83ff382018-09-21 15:45:40 +053091
Zarrare83ff382018-09-21 15:45:40 +053092 for invoice in invoices:
93 doc = frappe.get_doc("Sales Invoice", invoice)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053094 book_deferred_income_or_expense(doc, deferred_process, end_date)
95
96 if frappe.flags.deferred_accounting_error:
97 send_mail(deferred_process)
Zarrare83ff382018-09-21 15:45:40 +053098
Nabin Hait0a90ce52019-03-28 19:43:02 +053099def get_booking_dates(doc, item, posting_date=None):
100 if not posting_date:
101 posting_date = add_days(today(), -1)
102
103 last_gl_entry = False
104
Zarrare83ff382018-09-21 15:45:40 +0530105 deferred_account = "deferred_revenue_account" if doc.doctype=="Sales Invoice" else "deferred_expense_account"
Zarrare83ff382018-09-21 15:45:40 +0530106
107 prev_gl_entry = frappe.db.sql('''
108 select name, posting_date from `tabGL Entry` where company=%s and account=%s and
109 voucher_type=%s and voucher_no=%s and voucher_detail_no=%s
110 order by posting_date desc limit 1
111 ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
112
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530113 prev_gl_via_je = frappe.db.sql('''
114 SELECT p.name, p.posting_date FROM `tabJournal Entry` p, `tabJournal Entry Account` c
115 WHERE p.name = c.parent and p.company=%s and c.account=%s
116 and c.reference_type=%s and c.reference_name=%s
117 and c.reference_detail_no=%s and c.docstatus < 2 order by posting_date desc limit 1
118 ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
119
120 if prev_gl_via_je:
121 if (not prev_gl_entry) or (prev_gl_entry and
122 prev_gl_entry[0].posting_date < prev_gl_via_je[0].posting_date):
123 prev_gl_entry = prev_gl_via_je
124
Nabin Hait0a90ce52019-03-28 19:43:02 +0530125 if prev_gl_entry:
126 start_date = getdate(add_days(prev_gl_entry[0].posting_date, 1))
127 else:
128 start_date = item.service_start_date
Zarrare83ff382018-09-21 15:45:40 +0530129
Nabin Hait0a90ce52019-03-28 19:43:02 +0530130 end_date = get_last_day(start_date)
131 if end_date >= item.service_end_date:
132 end_date = item.service_end_date
133 last_gl_entry = True
Nabin Hait66d07c22019-03-29 13:25:11 +0530134 elif item.service_stop_date and end_date >= item.service_stop_date:
135 end_date = item.service_stop_date
136 last_gl_entry = True
Zarrare83ff382018-09-21 15:45:40 +0530137
Nabin Hait0a90ce52019-03-28 19:43:02 +0530138 if end_date > getdate(posting_date):
139 end_date = posting_date
Zarrare83ff382018-09-21 15:45:40 +0530140
Nabin Hait0a90ce52019-03-28 19:43:02 +0530141 if getdate(start_date) <= getdate(end_date):
142 return start_date, end_date, last_gl_entry
143 else:
144 return None, None, None
145
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530146def calculate_monthly_amount(doc, item, last_gl_entry, start_date, end_date, total_days, total_booking_days, account_currency):
147 amount, base_amount = 0, 0
Zarrare83ff382018-09-21 15:45:40 +0530148
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530149 if not last_gl_entry:
150 total_months = (item.service_end_date.year - item.service_start_date.year) * 12 + \
151 (item.service_end_date.month - item.service_start_date.month) + 1
152
153 prorate_factor = flt(date_diff(item.service_end_date, item.service_start_date)) \
154 / flt(date_diff(get_last_day(item.service_end_date), get_first_day(item.service_start_date)))
155
156 actual_months = rounded(total_months * prorate_factor, 1)
157
158 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
159 base_amount = flt(item.base_net_amount / actual_months, item.precision("base_net_amount"))
160
161 if base_amount + already_booked_amount > item.base_net_amount:
162 base_amount = item.base_net_amount - already_booked_amount
163
164 if account_currency==doc.company_currency:
165 amount = base_amount
166 else:
167 amount = flt(item.net_amount/actual_months, item.precision("net_amount"))
168 if amount + already_booked_amount_in_account_currency > item.net_amount:
169 amount = item.net_amount - already_booked_amount_in_account_currency
170
171 if not (get_first_day(start_date) == start_date and get_last_day(end_date) == end_date):
172 partial_month = flt(date_diff(end_date, start_date)) \
173 / flt(date_diff(get_last_day(end_date), get_first_day(start_date)))
174
175 base_amount = rounded(partial_month, 1) * base_amount
176 amount = rounded(partial_month, 1) * amount
177 else:
178 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
179 base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount"))
180 if account_currency==doc.company_currency:
181 amount = base_amount
182 else:
183 amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount"))
184
185 return amount, base_amount
186
187def calculate_amount(doc, item, last_gl_entry, total_days, total_booking_days, account_currency):
Zarrare83ff382018-09-21 15:45:40 +0530188 amount, base_amount = 0, 0
189 if not last_gl_entry:
190 base_amount = flt(item.base_net_amount*total_booking_days/flt(total_days), item.precision("base_net_amount"))
191 if account_currency==doc.company_currency:
192 amount = base_amount
193 else:
194 amount = flt(item.net_amount*total_booking_days/flt(total_days), item.precision("net_amount"))
195 else:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530196 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
197
Zarrare83ff382018-09-21 15:45:40 +0530198 base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount"))
199 if account_currency==doc.company_currency:
200 amount = base_amount
201 else:
Zarrare83ff382018-09-21 15:45:40 +0530202 amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount"))
203
204 return amount, base_amount
205
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530206def get_already_booked_amount(doc, item):
207 if doc.doctype == "Sales Invoice":
208 total_credit_debit, total_credit_debit_currency = "debit", "debit_in_account_currency"
209 deferred_account = "deferred_revenue_account"
210 else:
211 total_credit_debit, total_credit_debit_currency = "credit", "credit_in_account_currency"
212 deferred_account = "deferred_expense_account"
213
214 gl_entries_details = frappe.db.sql('''
215 select sum({0}) as total_credit, sum({1}) as total_credit_in_account_currency, voucher_detail_no
216 from `tabGL Entry` where company=%s and account=%s and voucher_type=%s and voucher_no=%s and voucher_detail_no=%s
217 group by voucher_detail_no
218 '''.format(total_credit_debit, total_credit_debit_currency),
219 (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
220
221 journal_entry_details = frappe.db.sql('''
222 SELECT sum(c.{0}) as total_credit, sum(c.{1}) as total_credit_in_account_currency, reference_detail_no
223 FROM `tabJournal Entry` p , `tabJournal Entry Account` c WHERE p.name = c.parent and
224 p.company = %s and c.account=%s and c.reference_type=%s and c.reference_name=%s and c.reference_detail_no=%s
225 and p.docstatus < 2 group by reference_detail_no
226 '''.format(total_credit_debit, total_credit_debit_currency),
227 (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
228
229 already_booked_amount = gl_entries_details[0].total_credit if gl_entries_details else 0
230 already_booked_amount += journal_entry_details[0].total_credit if journal_entry_details else 0
231
232 if doc.currency == doc.company_currency:
233 already_booked_amount_in_account_currency = already_booked_amount
234 else:
235 already_booked_amount_in_account_currency = gl_entries_details[0].total_credit_in_account_currency if gl_entries_details else 0
236 already_booked_amount_in_account_currency += journal_entry_details[0].total_credit_in_account_currency if journal_entry_details else 0
237
238 return already_booked_amount, already_booked_amount_in_account_currency
239
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530240def book_deferred_income_or_expense(doc, deferred_process, posting_date=None):
Nabin Hait27af6b32019-02-08 16:52:13 +0530241 enable_check = "enable_deferred_revenue" \
242 if doc.doctype=="Sales Invoice" else "enable_deferred_expense"
Zarrare83ff382018-09-21 15:45:40 +0530243
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530244 def _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on):
Nabin Hait0a90ce52019-03-28 19:43:02 +0530245 start_date, end_date, last_gl_entry = get_booking_dates(doc, item, posting_date=posting_date)
246 if not (start_date and end_date): return
Zarrare83ff382018-09-21 15:45:40 +0530247
248 account_currency = get_account_currency(item.expense_account)
Zarrare83ff382018-09-21 15:45:40 +0530249 if doc.doctype == "Sales Invoice":
250 against, project = doc.customer, doc.project
251 credit_account, debit_account = item.income_account, item.deferred_revenue_account
252 else:
253 against, project = doc.supplier, item.project
254 credit_account, debit_account = item.deferred_expense_account, item.expense_account
255
Nabin Hait0a90ce52019-03-28 19:43:02 +0530256 total_days = date_diff(item.service_end_date, item.service_start_date) + 1
257 total_booking_days = date_diff(end_date, start_date) + 1
258
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530259 if book_deferred_entries_based_on == 'Months':
260 amount, base_amount = calculate_monthly_amount(doc, item, last_gl_entry,
261 start_date, end_date, total_days, total_booking_days, account_currency)
262 else:
263 amount, base_amount = calculate_amount(doc, item, last_gl_entry,
264 total_days, total_booking_days, account_currency)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530265
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530266 if via_journal_entry:
267 book_revenue_via_journal_entry(doc, credit_account, debit_account, against, amount,
268 base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process, submit_journal_entry)
269 else:
270 make_gl_entries(doc, credit_account, debit_account, against,
271 amount, base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530272
273 # Returned in case of any errors because it tries to submit the same record again and again in case of errors
274 if frappe.flags.deferred_accounting_error:
275 return
Nabin Hait0a90ce52019-03-28 19:43:02 +0530276
277 if getdate(end_date) < getdate(posting_date) and not last_gl_entry:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530278 _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530279
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530280 via_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_via_journal_entry'))
281 submit_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'submit_journal_entries'))
282 book_deferred_entries_based_on = frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_based_on')
Nabin Hait0a90ce52019-03-28 19:43:02 +0530283
284 for item in doc.get('items'):
285 if item.get(enable_check):
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530286 _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530287
Chinmay D. Pai0d147b02020-05-24 00:54:04 +0530288def process_deferred_accounting(posting_date=None):
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530289 ''' Converts deferred income/expense into income/expense
290 Executed via background jobs on every month end '''
291
Chinmay D. Pai0d147b02020-05-24 00:54:04 +0530292 if not posting_date:
293 posting_date = today()
294
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530295 if not cint(frappe.db.get_singles_value('Accounts Settings', 'automatically_process_deferred_accounting_entry')):
296 return
297
298 start_date = add_months(today(), -1)
299 end_date = add_days(today(), -1)
300
301 for record_type in ('Income', 'Expense'):
302 doc = frappe.get_doc(dict(
303 doctype='Process Deferred Accounting',
304 posting_date=posting_date,
305 start_date=start_date,
306 end_date=end_date,
307 type=record_type
308 ))
309
310 doc.insert()
311 doc.submit()
312
Nabin Hait0a90ce52019-03-28 19:43:02 +0530313def make_gl_entries(doc, credit_account, debit_account, against,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530314 amount, base_amount, posting_date, project, account_currency, cost_center, item, deferred_process=None):
Nabin Hait0a90ce52019-03-28 19:43:02 +0530315 # GL Entry for crediting the amount in the deferred expense
316 from erpnext.accounts.general_ledger import make_gl_entries
317
rohitwaghchauree123ec62019-11-06 15:25:00 +0530318 if amount == 0: return
319
Nabin Hait0a90ce52019-03-28 19:43:02 +0530320 gl_entries = []
321 gl_entries.append(
322 doc.get_gl_dict({
323 "account": credit_account,
324 "against": against,
325 "credit": base_amount,
326 "credit_in_account_currency": amount,
327 "cost_center": cost_center,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530328 "voucher_detail_no": item.name,
Nabin Hait0a90ce52019-03-28 19:43:02 +0530329 'posting_date': posting_date,
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530330 'project': project,
331 'against_voucher_type': 'Process Deferred Accounting',
332 'against_voucher': deferred_process
Deepesh Garg7d61c032020-05-15 12:58:48 +0530333 }, account_currency, item=item)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530334 )
335 # GL Entry to debit the amount from the expense
336 gl_entries.append(
337 doc.get_gl_dict({
338 "account": debit_account,
339 "against": against,
340 "debit": base_amount,
341 "debit_in_account_currency": amount,
342 "cost_center": cost_center,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530343 "voucher_detail_no": item.name,
Nabin Hait0a90ce52019-03-28 19:43:02 +0530344 'posting_date': posting_date,
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530345 'project': project,
346 'against_voucher_type': 'Process Deferred Accounting',
347 'against_voucher': deferred_process
Deepesh Garg7d61c032020-05-15 12:58:48 +0530348 }, account_currency, item=item)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530349 )
350
Zarrare83ff382018-09-21 15:45:40 +0530351 if gl_entries:
Nabin Hait29fcb142019-02-13 17:18:12 +0530352 try:
353 make_gl_entries(gl_entries, cancel=(doc.docstatus == 2), merge_entries=True)
354 frappe.db.commit()
355 except:
356 frappe.db.rollback()
Nabin Hait0a90ce52019-03-28 19:43:02 +0530357 traceback = frappe.get_traceback()
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530358 frappe.log_error(message=traceback)
359
360 frappe.flags.deferred_accounting_error = True
361
362def send_mail(deferred_process):
363 title = _("Error while processing deferred accounting for {0}".format(deferred_process))
364 content = _("""
365 Deferred accounting failed for some invoices:
366 Please check Process Deferred Accounting {0}
367 and submit manually after resolving errors
368 """).format(get_link_to_form('Process Deferred Accounting', deferred_process))
369 sendmail_to_system_managers(title, content)
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530370
371def book_revenue_via_journal_entry(doc, credit_account, debit_account, against,
372 amount, base_amount, posting_date, project, account_currency, cost_center, item,
373 deferred_process=None, submit='No'):
374
375 if amount == 0: return
376
377 journal_entry = frappe.new_doc('Journal Entry')
378 journal_entry.posting_date = posting_date
379 journal_entry.company = doc.company
380 journal_entry.voucher_type = 'Deferred Revenue' if doc.doctype == 'Sales Invoice' \
381 else 'Deferred Expense'
382
383 debit_entry = {
384 'account': credit_account,
385 'credit': base_amount,
386 'credit_in_account_currency': amount,
387 'party_type': 'Customer' if doc.doctype == 'Sales Invoice' else 'Supplier',
388 'party': against,
389 'account_currency': account_currency,
390 'reference_name': doc.name,
391 'reference_type': doc.doctype,
392 'reference_detail_no': item.name,
393 'cost_center': cost_center,
394 'project': project,
395 }
396
397 credit_entry = {
398 'account': debit_account,
399 'debit': base_amount,
400 'debit_in_account_currency': amount,
401 'party_type': 'Customer' if doc.doctype == 'Sales Invoice' else 'Supplier',
402 'party': against,
403 'account_currency': account_currency,
404 'reference_name': doc.name,
405 'reference_type': doc.doctype,
406 'reference_detail_no': item.name,
407 'cost_center': cost_center,
408 'project': project,
409 }
410
411 for dimension in get_accounting_dimensions():
412 debit_entry.update({
413 dimension: item.get(dimension)
414 })
415
416 credit_entry.update({
417 dimension: item.get(dimension)
418 })
419
420 journal_entry.append('accounts', debit_entry)
421 journal_entry.append('accounts', credit_entry)
422
423 try:
424 journal_entry.save()
425
426 if submit:
427 journal_entry.submit()
428 except:
429 frappe.db.rollback()
430 traceback = frappe.get_traceback()
431 frappe.log_error(message=traceback)
432
433 frappe.flags.deferred_accounting_error = True
434
435def get_deferred_booking_accounts(doctype, voucher_detail_no, dr_or_cr):
436
437 if doctype == 'Sales Invoice':
438 credit_account, debit_account = frappe.db.get_value('Sales Invoice Item', {'name': voucher_detail_no},
439 ['income_account', 'deferred_revenue_account'])
440 else:
441 credit_account, debit_account = frappe.db.get_value('Purchase Invoice Item', {'name': voucher_detail_no},
442 ['deferred_expense_account', 'expense_account'])
443
444 if dr_or_cr == 'Debit':
445 return debit_account
446 else:
447 return credit_account
448
449